Chi-Tech
mgd_01a_init_materials.cc
Go to the documentation of this file.
2
3#include "chi_runtime.h"
4#include "chi_log.h"
5
7
12
13#include <algorithm>
14//============================================= assemble matrix A
15void mg_diffusion::Solver::Initialize_Materials(std::set<int>& material_ids)
16{
17 Chi::log.Log0Verbose1() << "Initializing Materials";
18
19 std::stringstream materials_list;
20
21 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Process materials found
22 const size_t num_physics_mats = Chi::material_stack.size();
23 bool first_material_read = true;
24
25 for (const int& mat_id : material_ids)
26 {
27 auto current_material =
29 mat_id, __FUNCTION__);
30 materials_list << "Material id " << mat_id;
31
32 //====================================== Check valid ids
33 if (mat_id < 0) {throw std::logic_error(
34 "MG-diff-InitMaterials: Cells encountered with no assigned material.");}
35 if (static_cast<size_t>(mat_id) >= num_physics_mats) {
36 throw std::logic_error("MG-diff-InitMaterials: Cells encountered with "
37 "material id that matches no material in physics material library.");}
38
39 //====================================== Extract properties
40 using MatProperty = chi_physics::PropertyType;
41 bool found_transport_xs = false;
42 for (const auto& property : current_material->properties_)
43 {
44 if (property->Type() == MatProperty::TRANSPORT_XSECTIONS)
45 {
46 auto transp_xs =
47 std::static_pointer_cast<chi_physics::MultiGroupXS>(property);
48 matid_to_xs_map[mat_id] = transp_xs;
49 found_transport_xs = true;
50 if (first_material_read)
51 num_groups_ = transp_xs->NumGroups();
52
53 }//transport xs
54 if (property->Type() == MatProperty::ISOTROPIC_MG_SOURCE)
55 {
56 auto mg_source =
57 std::static_pointer_cast<chi_physics::IsotropicMultiGrpSource>(property);
58
59 if (mg_source->source_value_g_.size() < num_groups_)
60 {
62 << "MG-Diff-InitMaterials: Isotropic Multigroup source specified in "
63 << "material \"" << current_material->name_ << "\" has fewer "
64 << "energy groups than called for in the simulation. "
65 << "Source will be ignored.";
66 }
67 else
68 {
69 matid_to_src_map[mat_id] = mg_source;
70 }
71 }//P0 source
72 }//for property
73
74 //====================================== Check valid property
75 if (!found_transport_xs)
76 {
78 << "MG-Diff-InitMaterials: Found no transport cross-section property for "
79 << "material \"" << current_material->name_ << "\".";
80 Chi::Exit(EXIT_FAILURE);
81 }
82 //====================================== Check number of groups legal
83 if (matid_to_xs_map[mat_id]->NumGroups() != num_groups_)
84 {
86 << "MG-Diff-InitMaterials: Found material \""
87 << current_material->name_ << "\" has "
88 << matid_to_xs_map[mat_id]->NumGroups() << " groups and "
89 << "the simulation has " << num_groups_ << " groups. The material "
90 << "must have the same number of groups.";
91 Chi::Exit(EXIT_FAILURE);
92 }
93
94 //====================================== Check number of moments
95 if (matid_to_xs_map[mat_id]->ScatteringOrder() > 1)
96 {
98 << "MG-Diff-InitMaterials: Found material \""
99 << current_material->name_ << "\" has a scattering order of "
100 << matid_to_xs_map[mat_id]->ScatteringOrder() << " and"
101 << " the simulation has a scattering order of One (MG-Diff)"
102 << " The higher moments will therefore not be used.";
103 }
104
105 materials_list
106 << " number of moments "
107 << matid_to_xs_map[mat_id]->ScatteringOrder() + 1 << "\n";
108
109 first_material_read = false;
110 }//for material id
111
112 Chi::log.Log()
113 << "Materials Initialized:\n" << materials_list.str() << "\n";
114
116
117 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Compute last fast group
118 // initialize last fast group
119 Chi::log.Log() << "Computing last fast group.";
120 unsigned int lfg = num_groups_;
121
122 if (num_groups_ > 1)
123 {
124 // loop over all materials
125 for (const auto &mat_id_xs: matid_to_xs_map)
126 {
127 // get the P0 transfer matrix
128 const auto &S = mat_id_xs.second->TransferMatrix(0);
129 // loop over all row of the transfer matrix
130 const int G = static_cast<int>(num_groups_);
131 for (int g = G-1; g >=0 ; --g)
132 {
133 for (const auto &[row_g, gp, sigma_sm]: S.Row(g))
134 {
135 if ((std::fabs(sigma_sm) > 1e-10) && (gp > row_g))
136 lfg = std::min(lfg, static_cast<unsigned int>(row_g));
137 }
138 }
139 }// loop on materials
140 }//if num_groups>1
141
142 last_fast_group_ = lfg;
143
144 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Compute two-grid params
145 do_two_grid_ = basic_options_("do_two_grid").BoolValue();
146 if ((lfg == num_groups_) && do_two_grid_)
147 {
148 Chi::log.Log0Error() << "Two-grid is not possible with no upscattering.";
149 do_two_grid_ = false;
150 Chi::Exit(EXIT_FAILURE);
151 }
152 if (do_two_grid_)
153 {
154 Chi::log.Log() << "Compute_TwoGrid_Params";
156 }
157
158}
static std::vector< chi_physics::MaterialPtr > material_stack
Definition: chi_runtime.h:90
static void Exit(int error_code)
Definition: chi_runtime.cc:342
static chi::ChiLog & log
Definition: chi_runtime.h:81
static std::shared_ptr< T > & GetStackItemPtr(std::vector< std::shared_ptr< T > > &stack, const size_t handle, const std::string &calling_function_name="Unknown")
Definition: chi_runtime.h:258
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
LogStream LogAllError()
Definition: chi_log.h:239
LogStream Log0Warning()
Definition: chi_log.h:231
LogStream Log0Error()
Definition: chi_log.h:232
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
LogStream LogAllWarning()
Definition: chi_log.h:238
LogStream Log0Verbose1()
Definition: chi_log.h:234
void Barrier() const
Definition: mpi_info.cc:38
BasicOptions basic_options_
Definition: chi_solver.h:57
void Initialize_Materials(std::set< int > &material_ids)
std::map< int, std::shared_ptr< chi_physics::MultiGroupXS > > matid_to_xs_map
std::map< int, std::shared_ptr< chi_physics::IsotropicMultiGrpSource > > matid_to_src_map