Chi-Tech
mgd_01_main_init.cc
Go to the documentation of this file.
2
3#include "chi_runtime.h"
4#include "chi_log.h"
5#include "utils/chi_timer.h"
6
9
11
13
14//============================================= constructor
15mg_diffusion::Solver::Solver(const std::string& in_solver_name):
16 chi_physics::Solver(in_solver_name, { {"max_inner_iters" , int64_t(500)},
17 {"residual_tolerance", 1.0e-2},
18 {"verbose_level" , int64_t (0) },
19 {"thermal_flux_tolerance", 1.0e-2},
20 {"max_thermal_iters" , int64_t(500)},
21 {"do_two_grid" , false}
22 })
23{}
24
25//============================================= destructor
27{
28 for (uint g=0; g < num_groups_; ++g)
29 {
30 VecDestroy(&x_[g]);
31 VecDestroy(&bext_[g]);
32 MatDestroy(&A_[g]);
33 }
34 VecDestroy(&b_);
35
36 if (last_fast_group_ < num_groups_)
37 {
38 VecDestroy(&thermal_dphi_);
39 for (uint g=last_fast_group_; g < num_groups_; ++g)
40 VecDestroy(&x_old_[g]);
41 }
42
43 if (do_two_grid_)
44 {
45 VecDestroy(&x_[num_groups_]);
46 MatDestroy(&A_[num_groups_]);
47 }
48}
49
50//============================================= Initialize
52{
53 Chi::log.Log() << "\n"
55 << TextName() << ": Initializing CFEM Multigroup Diffusion solver ";
56
57 //============================================= Get grid
59 const auto& grid = *grid_ptr_;
60 if (grid_ptr_ == nullptr)
61 throw std::logic_error(std::string(__PRETTY_FUNCTION__) +
62 " No grid defined.");
63
64 Chi::log.Log() << "Global num cells: " << grid.GetGlobalNumberOfCells();
65
66 //================================================== Add unique material ids
67 std::set<int> unique_material_ids;
68 int invalid_mat_cell_count = 0;
69 for (auto& cell : grid.local_cells)
70 {
71 unique_material_ids.insert(cell.material_id_);
72 if (cell.material_id_ < 0)
73 ++invalid_mat_cell_count;
74 }
75 const auto& ghost_cell_ids = grid.cells.GetGhostGlobalIDs();
76 for (uint64_t cell_id : ghost_cell_ids)
77 {
78 const auto& cell = grid.cells[cell_id];
79 unique_material_ids.insert(cell.material_id_);
80 if (cell.material_id_ < 0)
81 ++invalid_mat_cell_count;
82 }
83
84 if (invalid_mat_cell_count>0)
85 {
87 << "Number of invalid material cells: " << invalid_mat_cell_count;
88 }
89
90 //================================================== Initialize materials
92
93 //============================================= BIDs
94 auto globl_unique_bndry_ids = grid.GetDomainUniqueBoundaryIDs();
95 mg_diffusion::Solver::Set_BCs(globl_unique_bndry_ids);
96
97 //============================================= Make SDM
99 const auto& sdm = *sdm_ptr_;
100
101 const auto& OneDofPerNode = sdm.UNITARY_UNKNOWN_MANAGER;
102 num_local_dofs_ = sdm.GetNumLocalDOFs(OneDofPerNode);
103 num_globl_dofs_ = sdm.GetNumGlobalDOFs(OneDofPerNode);
104
105 Chi::log.Log() << "Num local DOFs: " << num_local_dofs_;
106 Chi::log.Log() << "Num globl DOFs: " << num_globl_dofs_;
107
108 //============================================= Initializes Mats and Vecs
109 const auto n = static_cast<int64_t>(num_local_dofs_);
110 const auto N = static_cast<int64_t>(num_globl_dofs_);
111
112 std::vector<int64_t> nodal_nnz_in_diag;
113 std::vector<int64_t> nodal_nnz_off_diag;
114 sdm.BuildSparsityPattern(nodal_nnz_in_diag,nodal_nnz_off_diag, OneDofPerNode);
115
116 unsigned int i_two_grid = do_two_grid_ ? 1 : 0;
117// std::cout << "i_two_grid = " << i_two_grid << std::endl;
118
119 A_.resize(num_groups_ + i_two_grid, nullptr);
120 x_.resize(num_groups_ + i_two_grid, nullptr);
121 bext_.resize(num_groups_, nullptr);
122
123 auto ghost_dof_indices = sdm.GetGhostDOFIndices(OneDofPerNode);
124
125 for (uint g=0; g < num_groups_; ++g)
126 {
127 // x[g] = chi_math::PETScUtils::CreateVector(n,N);
129 static_cast<int64_t>(ghost_dof_indices.size()),
130 ghost_dof_indices);
131 VecSet(x_[g], 0.0);
132 bext_[g] = chi_math::PETScUtils::CreateVector(n, N);
133
136 nodal_nnz_in_diag,
137 nodal_nnz_off_diag);
138 }
139 // initialize b
140 VecDuplicate(bext_.front(), &b_);
141 // initialize old flux for thermal groups
142 if (last_fast_group_ < num_groups_)
143 {
144 VecDuplicate(x_.front(), &thermal_dphi_);
145 x_old_.resize(num_groups_, nullptr);
146 for (uint g=0; g < num_groups_; ++g)
147 {
148 VecDuplicate(x_.front(), &x_old_[g]); //jcr is x_old like bext or like x?
149 VecSet(x_old_[g], 0.0);
150 }
151 }
152 // add two-grid mat and vec, if needed
153 if (do_two_grid_)
154 {
155 A_[num_groups_] = chi_math::PETScUtils::CreateSquareMatrix(n, N);
157 nodal_nnz_in_diag,
158 nodal_nnz_off_diag);
159 VecDuplicate(x_.front(), &x_[num_groups_]); // jcr needed?
160// x[num_groups] = chi_math::PETScUtils::CreateVectorWithGhosts(n,N,
161// static_cast<int64_t>(ghost_dof_indices.size()),
162// ghost_dof_indices);
163 }
164
165 if (do_two_grid_)
167
168 //============================================= Create Mats and ExtVecs
170
171 //============================================= Volume fraction for two-grid
172 // update
173
174 //============================================= Field Function
175 if (field_functions_.empty())
176 {
178 {
179 std::string solver_name;
180 if (not TextName().empty()) solver_name = TextName() + "-";
181
182 char buff[100];
183 int dummy = snprintf(buff,4,"%03d",g);
184
185 std::string text_name = solver_name + "Flux_g" + std::string(buff);
186
187 using namespace chi_math;
188 auto initial_field_function =
189 std::make_shared<chi_physics::FieldFunctionGridBased>(
190 text_name, //Text name
191 sdm_ptr_, //Spatial Discretization
192 Unknown(UnknownType::SCALAR)); //Unknown Manager
193
194 field_functions_.push_back(initial_field_function);
195 Chi::field_function_stack.push_back(initial_field_function);
196 }//for g
197 }//if not ff set
198
199}//end initialize
static std::vector< chi_physics::FieldFunctionPtr > field_function_stack
Definition: chi_runtime.h:92
static chi::Timer program_timer
Definition: chi_runtime.h:79
static chi::ChiLog & log
Definition: chi_runtime.h:81
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
LogStream LogAllWarning()
Definition: chi_log.h:238
std::string GetTimeString() const
Definition: chi_timer.cc:38
static std::shared_ptr< PieceWiseLinearContinuous > New(const chi_mesh::MeshContinuum &grid, QuadratureOrder q_order=QuadratureOrder::SECOND, CoordinateSystemType cs_type=CoordinateSystemType::CARTESIAN)
chi_mesh::MeshContinuumPtr & GetGrid() const
void Initialize_Materials(std::set< int > &material_ids)
Solver(const std::string &in_solver_name)
void Set_BCs(const std::vector< uint64_t > &globl_unique_bndry_ids)
void Initialize() override
void InitMatrixSparsity(Mat &A, const std::vector< int64_t > &nodal_nnz_in_diag, const std::vector< int64_t > &nodal_nnz_off_diag)
Vec CreateVectorWithGhosts(int64_t local_size, int64_t global_size, int64_t nghosts, const std::vector< int64_t > &ghost_indices)
Mat CreateSquareMatrix(int64_t local_size, int64_t global_size)
Vec CreateVector(int64_t local_size, int64_t global_size)
MeshHandler & GetCurrentHandler()
#define uint