Chi-Tech
lbs_01c_initmaterials.cc
Go to the documentation of this file.
1#include "lbs_solver.h"
2
5
6#include "chi_runtime.h"
7#include "chi_log.h"
8
9#include "chi_mpi.h"
10
11
12#include <algorithm>
13
14//###################################################################
15/**Initializes default materials and physics materials.*/
17{
18 const std::string fname = "lbs::SteadyStateSolver::InitMaterials";
19 Chi::log.Log0Verbose1() << "Initializing Materials";
20
21 //================================================== Create set of material
22 // ids locally relevant
23 std::set<int> unique_material_ids;
24 int invalid_mat_cell_count = 0;
25 for (auto& cell : grid_ptr_->local_cells)
26 {
27 unique_material_ids.insert(cell.material_id_);
28 if (cell.material_id_ < 0)
29 ++invalid_mat_cell_count;
30 }
31 const auto& ghost_cell_ids = grid_ptr_->cells.GetGhostGlobalIDs();
32 for (uint64_t cell_id : ghost_cell_ids)
33 {
34 const auto& cell = grid_ptr_->cells[cell_id];
35 unique_material_ids.insert(cell.material_id_);
36 if (cell.material_id_ < 0)
37 ++invalid_mat_cell_count;
38 }
39
40 if (invalid_mat_cell_count>0)
41 {
43 << "Number of invalid material cells: " << invalid_mat_cell_count;
44 }
45
46 //================================================== Get ready for processing
47 std::stringstream materials_list;
48 matid_to_xs_map_.clear();
49 matid_to_src_map_.clear();
50
51 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Process materials found
52 const size_t num_physics_mats = Chi::material_stack.size();
53
54 for (const int& mat_id : unique_material_ids)
55 {
56 materials_list << "Material id " << mat_id;
57
58 //====================================== Check valid ids
59 if (mat_id < 0)
60 throw std::logic_error(fname + ": Cells encountered with no assigned "
61 "material.");
62 if (static_cast<size_t>(mat_id) >= num_physics_mats)
63 throw std::logic_error(fname + ": Cells encountered with material id that"
64 " matches no material in physics material "
65 "library.");
66
67 auto current_material =
69 mat_id, fname);
70
71 //====================================== Extract properties
72 using MatProperty = chi_physics::PropertyType;
73 bool found_transport_xs = false;
74 for (const auto& property : current_material->properties_)
75 {
76 if (property->Type() == MatProperty::TRANSPORT_XSECTIONS)
77 {
78 auto transp_xs =
79 std::static_pointer_cast<chi_physics::MultiGroupXS>(property);
80 matid_to_xs_map_[mat_id] = transp_xs;
81 found_transport_xs = true;
82 }//transport xs
83 if (property->Type() == MatProperty::ISOTROPIC_MG_SOURCE)
84 {
85 auto mg_source =
86 std::static_pointer_cast<chi_physics::IsotropicMultiGrpSource>(property);
87
88 if (mg_source->source_value_g_.size() < groups_.size())
89 {
91 << fname + ": Isotropic Multigroup source specified in "
92 << "material \"" << current_material->name_ << "\" has fewer "
93 << "energy groups than called for in the simulation. "
94 << "Source will be ignored.";
95 }
96 else
97 {
98 matid_to_src_map_[mat_id] = mg_source;
99 }
100 }//P0 source
101 }//for property
102
103 //====================================== Check valid property
104 if (!found_transport_xs)
105 {
107 << fname + ": Found no transport cross-section property for "
108 << "material \"" << current_material->name_ << "\".";
109 Chi::Exit(EXIT_FAILURE);
110 }
111 //====================================== Check number of groups legal
112 if (matid_to_xs_map_[mat_id]->NumGroups() < groups_.size())
113 {
115 << fname + ": Found material \"" << current_material->name_ << "\" has "
116 << matid_to_xs_map_[mat_id]->NumGroups() << " groups and"
117 << " the simulation has " << groups_.size() << " groups."
118 << " The material must have a greater or equal amount of groups.";
119 Chi::Exit(EXIT_FAILURE);
120 }
121
122 //====================================== Check number of moments
123 if (matid_to_xs_map_[mat_id]->ScatteringOrder() < options_.scattering_order)
124 {
126 << fname + ": Found material \"" << current_material->name_
127 << "\" has a scattering order of "
128 << matid_to_xs_map_[mat_id]->ScatteringOrder()
129 << " and the simulation has a scattering order of "
131 << ". The higher moments will therefore not be used.";
132 }
133
134 materials_list
135 << " number of moments "
136 << matid_to_xs_map_[mat_id]->ScatteringOrder() + 1 << "\n";
137 }//for material id
138
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Initialize precursor
140 // properties
141
142 num_precursors_ = 0;
144 for (const auto& mat_id_xs : matid_to_xs_map_)
145 {
146 const auto& xs = mat_id_xs.second;
147 num_precursors_ += xs->NumPrecursors();
148 if (xs->NumPrecursors() > max_precursors_per_material_)
149 max_precursors_per_material_ = xs->NumPrecursors();
150 }
151
152 //if no precursors, turn off precursors
153 if (num_precursors_ == 0)
154 options_.use_precursors = false;
155
156 //check compatibility when precursors are on
158 {
159 for (const auto& mat_id_xs: matid_to_xs_map_)
160 {
161 const auto& xs = mat_id_xs.second;
162 if (xs->IsFissionable() && num_precursors_ == 0)
163 throw std::logic_error(
164 "Incompatible cross section data encountered."
165 "When delayed neutron data is present for one "
166 "fissionable material, it must be present for "
167 "all fissionable materials.");
168 }
169 }
170
171 //================================================== Update transport views
172 // if available
173 if (grid_ptr_->local_cells.size() == cell_transport_views_.size())
174 for (const auto& cell : grid_ptr_->local_cells)
175 {
176 const auto& xs_ptr = matid_to_xs_map_[cell.material_id_];
177 auto& transport_view = cell_transport_views_[cell.local_id_];
178
179 transport_view.ReassingXS(*xs_ptr);
180 }
181
183 << "Materials Initialized:\n" << materials_list.str() << "\n";
184
185 MPI_Barrier(Chi::mpi.comm);
186}
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 LogAllWarning()
Definition: chi_log.h:238
LogStream Log0Verbose1()
Definition: chi_log.h:234
chi_mesh::MeshContinuumPtr grid_ptr_
Definition: lbs_solver.h:75
std::vector< lbs::CellLBSView > cell_transport_views_
Definition: lbs_solver.h:83
size_t NumGroups() const
std::vector< LBSGroup > groups_
Definition: lbs_solver.h:67
size_t num_precursors_
Definition: lbs_solver.h:64
lbs::Options options_
Definition: lbs_solver.h:61
std::map< int, IsotropicSrcPtr > matid_to_src_map_
Definition: lbs_solver.h:72
std::map< int, XSPtr > matid_to_xs_map_
Definition: lbs_solver.h:71
size_t max_precursors_per_material_
Definition: lbs_solver.h:65
unsigned int scattering_order
Definition: lbs_structs.h:126
bool use_precursors
Definition: lbs_structs.h:138