Chi-Tech
lbs_groupset.cc
Go to the documentation of this file.
1#include "lbs_groupset.h"
2
4
5#include "chi_runtime.h"
6#include "chi_log.h"
7#include "mpi/chi_mpi.h"
8
9#include <fstream>
10
11#include "ChiObjectFactory.h"
12
13namespace lbs
14{
16}
17
18// ##################################################################
19/***/
21{
23
24 // clang-format off
25 params.SetGeneralDescription("Input Parameters for groupsets.");
26 params.SetDocGroup("LuaLBSGroupsets");
27
29 "groups_from_to", "The first and last group id this groupset operates on."
30 " e.g. A 4 group problem <TT>groups_from_to= {0, 3}</TT>");
31
32 params.AddRequiredParameter<size_t>("angular_quadrature_handle",
33 "A handle to an angular quadrature");
35 "angle_aggregation_type",
36 "polar",
37 "The angle aggregation method to use during sweeping");
38
40 "angle_aggregation_num_subsets",
41 1,
42 "The number of subsets to apply to sets of "
43 "angles that have been aggregated. This is useful for increasing "
44 "pipeline size for parallel simulations");
45
47 "groupset_num_subsets",
48 1,
49 "The number of subsets to apply to the set of groups in this set. This is "
50 "useful for increasing pipeline size for parallel simulations");
51
53 "inner_linear_method",
54 "richardson",
55 "The iterative method to use for inner linear solves");
56
58 "l_abs_tol", 1.0e-6, "Inner linear solver residual absolute tolerance");
60 "l_max_its", 200, "Inner linear solver maximum iterations");
61 params.AddOptionalParameter("gmres_restart_interval",
62 30,
63 "If this inner linear solver is gmres, sets the"
64 " number of iterations before a restart occurs.");
65
67 "allow_cycles",
68 true,
69 "Flag indicating whether cycles are to be allowed or not");
70
72 "log_sweep_events", false, "Turns on a log of sweep events");
73
74 // WG DSA options
75 params.AddOptionalParameter("apply_wgdsa",
76 false,
77 "Flag to turn on within-group Diffusion "
78 "Synthetic Acceleration for this groupset");
80 "wgdsa_l_abs_tol", 1.0e-4, "Within-group DSA linear absolute tolerance");
82 "wgdsa_l_max_its", 30, "Within-group DSA linear maximum iterations");
84 "wgdsa_verbose", false, "If true, WGDSA routines will print verbosely");
86 "wgdsa_petsc_options", "", "PETSc options to pass to WGDSA solver");
87
88 // TG DSA options
90 "apply_tgdsa",
91 false,
92 "Flag to turn on Two-Grid Acceleration for this groupset");
94 "tgdsa_l_abs_tol", 1.0e-4, "Two-Grid DSA linear absolute tolerance");
96 "tgdsa_l_max_its", 30, "Two-Grid DSA linear maximum iterations");
98 "tgdsa_verbose", false, "If true, TGDSA routines will print verbosely");
100 "tgdsa_petsc_options", "", "PETSc options to pass to TGDSA solver");
101
102 // ============================================ Constraints
103 using namespace chi_data_types;
104
106 "angle_aggregation_type",
107 AllowableRangeList::New({"polar", "single", "azimuthal"}));
108
109 params.ConstrainParameterRange("angle_aggregation_num_subsets",
110 AllowableRangeLowLimit::New(1));
111
112 params.ConstrainParameterRange("groupset_num_subsets",
113 AllowableRangeLowLimit::New(1));
114
116 "inner_linear_method",
117 AllowableRangeList::New({"richardson", "gmres", "bicgstab"}));
118
119 params.ConstrainParameterRange("l_abs_tol",
120 AllowableRangeLowLimit::New(1.0e-18));
121 params.ConstrainParameterRange("l_max_its", AllowableRangeLowLimit::New(0));
122 params.ConstrainParameterRange("gmres_restart_interval",
123 AllowableRangeLowLimit::New(1));
124
125 // clang-format on
126
127 return params;
128}
129
130// ##################################################################
131/**Input parameters based constructor.*/
133 const int id,
134 const LBSSolver& lbs_solver)
135 : ChiObject(params), id_(id)
136{
137 const std::string fname = __FUNCTION__;
138
139 // ============================================ Add groups
140 const auto groups_from_to =
141 params.GetParamVectorValue<size_t>("groups_from_to");
142 ChiInvalidArgumentIf(groups_from_to.size() != 2,
143 "Parameter \"groups_from_to\" can only have 2 entries");
144
145 const size_t from = groups_from_to[0];
146 const size_t to = groups_from_to[1];
147 ChiInvalidArgumentIf(to < from,
148 "\"to\" field is less than the \"from\" field.");
149
150 try
151 {
152 for (size_t g = from; g <= to; ++g)
153 {
154 groups_.push_back(lbs_solver.Groups().at(g));
155 } // for g
156 }
157 catch (const std::exception& exc)
158 {
159 throw std::invalid_argument("Attempting to group to groupset that is not"
160 "part of the solver.");
161 }
162
163 master_num_grp_subsets_ = params.GetParamValue<int>("groupset_num_subsets");
164
165 // ============================================ Add quadrature
166 const size_t quad_handle =
167 params.GetParamValue<size_t>("angular_quadrature_handle");
168 quadrature_ = Chi::GetStackItemPtr<chi_math::AngularQuadrature>(
169 Chi::angular_quadrature_stack, quad_handle, fname);
170
171 // ============================================ Angle aggregation
172 const auto angle_agg_typestr =
173 params.GetParamValue<std::string>("angle_aggregation_type");
174 if (angle_agg_typestr == "polar")
176 else if (angle_agg_typestr == "single")
178 else if (angle_agg_typestr == "azimuthal")
180
182 params.GetParamValue<int>("angle_aggregation_num_subsets");
183
184 // ============================================ Inner solver
185 const auto inner_linear_method =
186 params.GetParamValue<std::string>("inner_linear_method");
187 if (inner_linear_method == "richardson")
189 else if (inner_linear_method == "gmres")
191 else if (inner_linear_method == "bicgstab")
193
194 allow_cycles_ = params.GetParamValue<bool>("allow_cycles");
195 residual_tolerance_ = params.GetParamValue<double>("l_abs_tol");
196 max_iterations_ = params.GetParamValue<int>("l_max_its");
197
198 // ============================================ Misc.
199 log_sweep_events_ = params.GetParamValue<bool>("log_sweep_events");
200
201 // ============================================ DSA
202 apply_wgdsa_ = params.GetParamValue<bool>("apply_wgdsa");
203 apply_tgdsa_ = params.GetParamValue<bool>("apply_tgdsa");
204
205 wgdsa_tol_ = params.GetParamValue<double>("wgdsa_l_abs_tol");
206 tgdsa_tol_ = params.GetParamValue<double>("tgdsa_l_abs_tol");
207
208 wgdsa_max_iters_ = params.GetParamValue<int>("wgdsa_l_max_its");
209 tgdsa_max_iters_ = params.GetParamValue<int>("tgdsa_l_max_its");
210
211 wgdsa_verbose_ = params.GetParamValue<bool>("wgdsa_verbose");
212 tgdsa_verbose_ = params.GetParamValue<bool>("tgdsa_verbose");
213
214 wgdsa_string_ = params.GetParamValue<std::string>("wgdsa_petsc_options");
215 tgdsa_string_ = params.GetParamValue<std::string>("tgdsa_petsc_options");
216}
217
218// ##################################################################
219/**Computes the discrete to moment operator.*/
220void lbs::LBSGroupset::BuildDiscMomOperator(unsigned int scattering_order,
221 lbs::GeometryType geometry_type)
222{
223 if (geometry_type == lbs::GeometryType::ONED_SLAB ||
224 geometry_type == lbs::GeometryType::ONED_CYLINDRICAL ||
225 geometry_type == lbs::GeometryType::ONED_SPHERICAL)
226 {
227 quadrature_->BuildDiscreteToMomentOperator(scattering_order, 1);
228 }
229 else if (geometry_type == lbs::GeometryType::TWOD_CARTESIAN ||
231 {
232 quadrature_->BuildDiscreteToMomentOperator(scattering_order, 2);
233 }
234 else if (geometry_type == lbs::GeometryType::THREED_CARTESIAN)
235 {
236 quadrature_->BuildDiscreteToMomentOperator(scattering_order, 3);
237 }
238}
239
240// ##################################################################
241/**Computes the moment to discrete operator.*/
242void lbs::LBSGroupset::BuildMomDiscOperator(unsigned int scattering_order,
243 lbs::GeometryType geometry_type)
244{
245 if (geometry_type == lbs::GeometryType::ONED_SLAB ||
246 geometry_type == lbs::GeometryType::ONED_CYLINDRICAL ||
247 geometry_type == lbs::GeometryType::ONED_SPHERICAL)
248 {
249 quadrature_->BuildMomentToDiscreteOperator(scattering_order, 1);
250 }
251 else if (geometry_type == lbs::GeometryType::TWOD_CARTESIAN ||
253 {
254 quadrature_->BuildMomentToDiscreteOperator(scattering_order, 2);
255 }
256 else if (geometry_type == lbs::GeometryType::THREED_CARTESIAN)
257 {
258 quadrature_->BuildMomentToDiscreteOperator(scattering_order, 3);
259 }
260}
261
262// ##################################################################
263/**Constructs the groupset subsets.*/
265{
266 grp_subset_infos_ = chi::MakeSubSets(groups_.size(), master_num_grp_subsets_);
267 {
268 size_t ss = 0;
269 for (const auto& info : grp_subset_infos_)
270 {
271 Chi::log.Log() << "Groupset " << id_ << " has group-subset " << ss << " "
272 << info.ss_begin << "->" << info.ss_end;
273 ++ss;
274 }
275 }
276}
277
278// ##################################################################
279/**Constructs the groupset subsets.*/
281 const std::string& file_name)
282{
283 if (not log_sweep_events_) return;
284
285 std::ofstream ofile;
286 ofile.open(file_name, std::ofstream::out);
287
288 ofile << "Groupset Sweep information "
289 << "location " << Chi::mpi.location_id << "\n";
290
291 //======================================== Print all anglesets
292 for (int q = 0; q < angle_agg_->angle_set_groups.size(); ++q)
293 {
294 ofile << "Angle-set group " << q << ":\n";
295 auto& ang_set_grp = angle_agg_->angle_set_groups[q];
296 int num_ang_sets_per_grp = (int)ang_set_grp.AngleSets().size();
297 for (int as = 0; as < num_ang_sets_per_grp; ++as)
298 {
299 auto ang_set = ang_set_grp.AngleSets()[as];
300
301 int ang_set_num = as + q * num_ang_sets_per_grp;
302
303 ofile << " Angle-set " << ang_set_num << " angles [# varphi theta]:\n";
304
305 for (auto& ang_num : ang_set->GetAngleIndices())
306 {
307 const auto& angle = quadrature_->abscissae_[ang_num];
308
309 ofile << " " << ang_num << " " << angle.phi << " " << angle.theta
310 << "\n";
311 }
312 }
313 }
314
315 //======================================== Print event history
316 ofile << Chi::log.PrintEventHistory(ev_tag);
317
318 ofile.close();
319}
#define ChiInvalidArgumentIf(condition, message)
static std::vector< chi_math::AngularQuadraturePtr > angular_quadrature_stack
Definition: chi_runtime.h:94
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
static chi::InputParameters GetInputParameters()
Definition: ChiObject.cc:4
std::string PrintEventHistory(size_t ev_tag)
Definition: chi_log.cc:234
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
void SetDocGroup(const std::string &doc_group)
void AddRequiredParameter(const std::string &name, const std::string &doc_string)
void AddRequiredParameterArray(const std::string &name, const std::string &doc_string)
void ConstrainParameterRange(const std::string &param_name, AllowableRangePtr allowable_range)
void AddOptionalParameter(const std::string &name, T value, const std::string &doc_string)
void SetGeneralDescription(const std::string &description)
const int & location_id
Current process rank.
Definition: mpi_info.h:26
T GetParamValue(const std::string &param_name) const
std::vector< T > GetParamVectorValue(const std::string &param_name) const
double residual_tolerance_
Definition: lbs_groupset.h:54
AngleAggregationType angleagg_method_
Definition: lbs_groupset.h:53
std::string wgdsa_string_
Definition: lbs_groupset.h:69
void PrintSweepInfoFile(size_t ev_tag, const std::string &file_name)
int master_num_ang_subsets_
Definition: lbs_groupset.h:48
void BuildDiscMomOperator(unsigned int scattering_order, GeometryType geometry_type)
std::shared_ptr< chi_math::AngularQuadrature > quadrature_
Definition: lbs_groupset.h:42
void BuildMomDiscOperator(unsigned int scattering_order, GeometryType geometry_type)
static chi::InputParameters GetInputParameters()
Definition: lbs_groupset.cc:20
int master_num_grp_subsets_
Definition: lbs_groupset.h:47
std::vector< LBSGroup > groups_
Definition: lbs_groupset.h:41
std::string tgdsa_string_
Definition: lbs_groupset.h:70
IterativeMethod iterative_method_
Definition: lbs_groupset.h:52
const std::vector< LBSGroup > & Groups() const
std::vector< SubSetInfo > MakeSubSets(size_t num_items, size_t desired_num_subsets)
Definition: subsets.cc:10
@ KRYLOV_BICGSTAB
BiCGStab iterative algorithm.
@ KRYLOV_RICHARDSON
Richardson iteration.
@ KRYLOV_GMRES
GMRES iterative algorithm.
RegisterChiObjectParametersOnly(lbs, LBSGroupset)
GeometryType
Definition: lbs_structs.h:34