• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Open-Sn / opensn / 18961426277

30 Oct 2025 02:48PM UTC coverage: 74.746% (-0.03%) from 74.771%
18961426277

push

github

web-flow
Merge pull request #808 from eappen-nelluvelil/cbc-fluds-only-storing-angular-fluxes

Modified CBC FLUDS to have its own vector that stores local cell angular fluxes

61 of 64 new or added lines in 3 files covered. (95.31%)

214 existing lines in 7 files now uncovered.

18206 of 24357 relevant lines covered (74.75%)

54465966.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.62
/modules/linear_boltzmann_solvers/lbs_problem/lbs_problem.cc
1
// SPDX-FileCopyrightText: 2024 The OpenSn Authors <https://open-sn.github.io/opensn/>
2
// SPDX-License-Identifier: MIT
3

4
#include "modules/linear_boltzmann_solvers/lbs_problem/lbs_problem.h"
5
#include "modules/linear_boltzmann_solvers/lbs_problem/iterative_methods/wgs_context.h"
6
#include "modules/linear_boltzmann_solvers/lbs_problem/iterative_methods/ags_linear_solver.h"
7
#include "modules/linear_boltzmann_solvers/lbs_problem/point_source/point_source.h"
8
#include "modules/linear_boltzmann_solvers/lbs_problem/groupset/lbs_groupset.h"
9
#include "framework/math/spatial_discretization/finite_element/piecewise_linear/piecewise_linear_discontinuous.h"
10
#include "framework/field_functions/field_function_grid_based.h"
11
#include "framework/materials/multi_group_xs/multi_group_xs.h"
12
#include "framework/mesh/mesh_continuum/mesh_continuum.h"
13
#include "framework/utils/hdf_utils.h"
14
#include "framework/object_factory.h"
15
#include "framework/logging/log.h"
16
#include "framework/runtime.h"
17
#include "caliper/cali.h"
18
#include <algorithm>
19
#include <iomanip>
20
#include <fstream>
21
#include <cstring>
22
#include <cassert>
23
#include <memory>
24
#include <sys/stat.h>
25

26
namespace opensn
27
{
28

29
std::map<std::string, uint64_t> LBSProblem::supported_boundary_names = {
30
  {"xmin", XMIN}, {"xmax", XMAX}, {"ymin", YMIN}, {"ymax", YMAX}, {"zmin", ZMIN}, {"zmax", ZMAX}};
31

32
std::map<uint64_t, std::string> LBSProblem::supported_boundary_ids = {
33
  {XMIN, "xmin"}, {XMAX, "xmax"}, {YMIN, "ymin"}, {YMAX, "ymax"}, {ZMIN, "zmin"}, {ZMAX, "zmax"}};
34

35
LBSProblem::LBSProblem(const std::string& name, std::shared_ptr<MeshContinuum> grid)
×
36
  : Problem(name), grid_(grid), use_gpus_(false)
×
37
{
38
}
×
39

40
InputParameters
41
LBSProblem::GetInputParameters()
299✔
42
{
43
  InputParameters params = Problem::GetInputParameters();
299✔
44

45
  params.ChangeExistingParamToOptional("name", "LBSProblem");
598✔
46

47
  params.AddRequiredParameter<std::shared_ptr<MeshContinuum>>("mesh", "Mesh");
598✔
48

49
  params.AddRequiredParameter<size_t>("num_groups", "The total number of groups within the solver");
598✔
50

51
  params.AddRequiredParameterArray("groupsets",
598✔
52
                                   "An array of blocks each specifying the input parameters for a "
53
                                   "<TT>LBSGroupset</TT>.");
54
  params.LinkParameterToBlock("groupsets", "LBSGroupset");
598✔
55

56
  params.AddRequiredParameterArray("xs_map",
598✔
57
                                   "Cross-section map from block IDs to cross-section objects.");
58

59
  params.AddOptionalParameterArray<std::shared_ptr<VolumetricSource>>(
598✔
60
    "volumetric_sources", {}, "An array of handles to volumetric sources.");
61

62
  params.AddOptionalParameterArray<std::shared_ptr<PointSource>>(
598✔
63
    "point_sources", {}, "An array of point sources.");
64

65
  params.AddOptionalParameterArray(
598✔
66
    "boundary_conditions", {}, "An array containing tables for each boundary specification.");
67
  params.LinkParameterToBlock("boundary_conditions", "BoundaryOptionsBlock");
598✔
68

69
  params.AddOptionalParameterBlock(
598✔
70
    "options", ParameterBlock(), "Block of options. See <TT>OptionsBlock</TT>.");
598✔
71
  params.LinkParameterToBlock("options", "OptionsBlock");
598✔
72

73
  params.AddOptionalParameter("use_gpus", false, "Offload the sweep computation to GPUs.");
598✔
74

75
  return params;
299✔
76
}
×
77

78
LBSProblem::LBSProblem(const InputParameters& params)
299✔
79
  : Problem(params),
80
    num_groups_(params.GetParamValue<size_t>("num_groups")),
299✔
81
    grid_(params.GetSharedPtrParam<MeshContinuum>("mesh")),
299✔
82
    use_gpus_(params.GetParamValue<bool>("use_gpus"))
897✔
83
{
84
  // Check system for GPU acceleration
85
  if (use_gpus_)
299✔
86
  {
87
#ifdef __OPENSN_USE_CUDA__
88
    CheckCapableDevices();
89
#else
90
    throw std::invalid_argument(
×
91
      GetName() + ": GPU support was requested, but OpenSn was built without CUDA enabled.");
×
92
#endif // __OPENSN_USE_CUDA__
93
  }
94

95
  // Initialize options
96
  if (params.IsParameterValid("options"))
299✔
97
  {
98
    auto options_params = LBSProblem::GetOptionsBlock();
174✔
99
    options_params.AssignParameters(params.GetParam("options"));
176✔
100
    SetOptions(options_params);
172✔
101
  }
174✔
102

103
  // Set geometry type
104
  geometry_type_ = grid_->GetGeometryType();
297✔
105
  if (geometry_type_ == GeometryType::INVALID)
297✔
UNCOV
106
    throw std::runtime_error(GetName() + ": Invalid geometry type.");
×
107

108
  // Set boundary conditions
109
  if (params.Has("boundary_conditions"))
297✔
110
  {
111
    const auto& bcs = params.GetParam("boundary_conditions");
297✔
112
    bcs.RequireBlockTypeIs(ParameterBlockType::ARRAY);
297✔
113
    for (size_t b = 0; b < bcs.GetNumParameters(); ++b)
908✔
114
    {
115
      auto bndry_params = GetBoundaryOptionsBlock();
611✔
116
      bndry_params.AssignParameters(bcs.GetParam(b));
611✔
117
      SetBoundaryOptions(bndry_params);
611✔
118
    }
611✔
119
  }
120

121
  InitializeGroupsets(params);
297✔
122
  InitializeSources(params);
297✔
123
  InitializeXSmapAndDensities(params);
297✔
124
  InitializeMaterials();
297✔
125
}
317✔
126

127
LBSOptions&
128
LBSProblem::GetOptions()
17,232✔
129
{
130
  return options_;
17,232✔
131
}
132

133
const LBSOptions&
134
LBSProblem::GetOptions() const
473,391,074✔
135
{
136
  return options_;
473,391,074✔
137
}
138

139
GeometryType
140
LBSProblem::GetGeometryType() const
4✔
141
{
142
  return geometry_type_;
4✔
143
}
144

145
size_t
146
LBSProblem::GetNumMoments() const
128,793✔
147
{
148
  return num_moments_;
128,793✔
149
}
150

151
size_t
152
LBSProblem::GetNumGroups() const
52,309✔
153
{
154
  return num_groups_;
52,309✔
155
}
156

157
unsigned int
158
LBSProblem::GetScatteringOrder() const
4✔
159
{
160
  return scattering_order_;
4✔
161
}
162

163
size_t
164
LBSProblem::GetNumPrecursors() const
×
165
{
166
  return num_precursors_;
×
167
}
168

169
size_t
170
LBSProblem::GetMaxPrecursorsPerMaterial() const
8✔
171
{
172
  return max_precursors_per_material_;
8✔
173
}
174

175
const std::vector<LBSGroup>&
176
LBSProblem::GetGroups() const
138,196✔
177
{
178
  return groups_;
138,196✔
179
}
180

181
std::vector<LBSGroupset>&
182
LBSProblem::GetGroupsets()
8,170,799✔
183
{
184
  return groupsets_;
8,170,799✔
185
}
186

187
const std::vector<LBSGroupset>&
188
LBSProblem::GetGroupsets() const
×
189
{
190
  return groupsets_;
×
191
}
192

193
void
194
LBSProblem::AddPointSource(std::shared_ptr<PointSource> point_source)
×
195
{
196
  point_sources_.push_back(point_source);
×
197
  if (discretization_)
×
198
    point_sources_.back()->Initialize(*this);
×
199
}
×
200

201
void
202
LBSProblem::ClearPointSources()
×
203
{
204
  point_sources_.clear();
×
205
}
×
206

207
const std::vector<std::shared_ptr<PointSource>>&
208
LBSProblem::GetPointSources() const
7,422✔
209
{
210
  return point_sources_;
7,422✔
211
}
212

213
void
214
LBSProblem::AddVolumetricSource(std::shared_ptr<VolumetricSource> volumetric_source)
16✔
215
{
216
  volumetric_sources_.push_back(volumetric_source);
16✔
217
  if (discretization_)
16✔
218
    volumetric_sources_.back()->Initialize(*this);
16✔
219
}
16✔
220

221
void
222
LBSProblem::ClearVolumetricSources()
4✔
223
{
224
  volumetric_sources_.clear();
4✔
225
}
4✔
226

227
const std::vector<std::shared_ptr<VolumetricSource>>&
228
LBSProblem::GetVolumetricSources() const
7,422✔
229
{
230
  return volumetric_sources_;
7,422✔
231
}
232

233
void
234
LBSProblem::ClearBoundaries()
×
235
{
236
  boundary_preferences_.clear();
×
237
}
×
238

239
const std::map<int, std::shared_ptr<MultiGroupXS>>&
240
LBSProblem::GetMatID2XSMap() const
6,681✔
241
{
242
  return block_id_to_xs_map_;
6,681✔
243
}
244

245
std::shared_ptr<MeshContinuum>
246
LBSProblem::GetGrid() const
180,180✔
247
{
248
  return grid_;
180,180✔
249
}
250

251
const SpatialDiscretization&
252
LBSProblem::GetSpatialDiscretization() const
64,044✔
253
{
254
  return *discretization_;
64,044✔
255
}
256

257
const std::vector<UnitCellMatrices>&
258
LBSProblem::GetUnitCellMatrices() const
7,279✔
259
{
260
  return unit_cell_matrices_;
7,279✔
261
}
262

263
const std::map<uint64_t, UnitCellMatrices>&
264
LBSProblem::GetUnitGhostCellMatrices() const
17✔
265
{
266
  return unit_ghost_cell_matrices_;
17✔
267
}
268

269
const std::vector<CellLBSView>&
270
LBSProblem::GetCellTransportViews() const
190,176✔
271
{
272
  return cell_transport_views_;
190,176✔
273
}
274

275
const UnknownManager&
276
LBSProblem::GetUnknownManager() const
26,695✔
277
{
278
  return flux_moments_uk_man_;
26,695✔
279
}
280

281
size_t
282
LBSProblem::GetLocalNodeCount() const
3,053✔
283
{
284
  return local_node_count_;
3,053✔
285
}
286

287
size_t
288
LBSProblem::GetGlobalNodeCount() const
1,494✔
289
{
290
  return global_node_count_;
1,494✔
291
}
292

293
std::vector<double>&
294
LBSProblem::GetQMomentsLocal()
37,457✔
295
{
296
  return q_moments_local_;
37,457✔
297
}
298

299
const std::vector<double>&
300
LBSProblem::GetQMomentsLocal() const
×
301
{
302
  return q_moments_local_;
×
303
}
304

305
std::vector<double>&
306
LBSProblem::GetExtSrcMomentsLocal()
4✔
307
{
308
  return ext_src_moments_local_;
4✔
309
}
310

311
const std::vector<double>&
312
LBSProblem::GetExtSrcMomentsLocal() const
37,139✔
313
{
314
  return ext_src_moments_local_;
37,139✔
315
}
316

317
std::vector<double>&
318
LBSProblem::GetPhiOldLocal()
56,799✔
319
{
320
  return phi_old_local_;
56,799✔
321
}
322

323
const std::vector<double>&
324
LBSProblem::GetPhiOldLocal() const
×
325
{
326
  return phi_old_local_;
×
327
}
328

329
std::vector<double>&
330
LBSProblem::GetPhiNewLocal()
59,349✔
331
{
332
  return phi_new_local_;
59,349✔
333
}
334

335
const std::vector<double>&
336
LBSProblem::GetPhiNewLocal() const
×
337
{
338
  return phi_new_local_;
×
339
}
340

341
std::vector<double>&
342
LBSProblem::GetPrecursorsNewLocal()
16✔
343
{
344
  return precursor_new_local_;
16✔
345
}
346

347
const std::vector<double>&
348
LBSProblem::GetPrecursorsNewLocal() const
×
349
{
350
  return precursor_new_local_;
×
351
}
352

353
std::vector<double>&
354
LBSProblem::GetDensitiesLocal()
250✔
355
{
356
  return densities_local_;
250✔
357
}
358

359
const std::vector<double>&
360
LBSProblem::GetDensitiesLocal() const
37,139✔
361
{
362
  return densities_local_;
37,139✔
363
}
364

365
SetSourceFunction
366
LBSProblem::GetActiveSetSourceFunction() const
4,154✔
367
{
368
  return active_set_source_function_;
4,154✔
369
}
370

371
std::shared_ptr<AGSLinearSolver>
372
LBSProblem::GetAGSSolver()
281✔
373
{
374
  return ags_solver_;
281✔
375
}
376

377
std::vector<std::shared_ptr<LinearSolver>>&
378
LBSProblem::GetWGSSolvers()
113✔
379
{
380
  return wgs_solvers_;
113✔
381
}
382

383
WGSContext&
384
LBSProblem::GetWGSContext(int groupset_id)
11,508✔
385
{
386
  auto& wgs_solver = wgs_solvers_[groupset_id];
11,508✔
387
  auto raw_context = wgs_solver->GetContext();
11,508✔
388
  auto wgs_context_ptr = std::dynamic_pointer_cast<WGSContext>(raw_context);
11,508✔
389
  OpenSnLogicalErrorIf(not wgs_context_ptr, "Failed to cast WGSContext");
11,508✔
390
  return *wgs_context_ptr;
11,508✔
391
}
23,016✔
392

393
std::map<uint64_t, BoundaryPreference>&
394
LBSProblem::GetBoundaryPreferences()
4✔
395
{
396
  return boundary_preferences_;
4✔
397
}
398

399
std::pair<size_t, size_t>
400
LBSProblem::GetNumPhiIterativeUnknowns()
×
401
{
402
  const auto& sdm = *discretization_;
×
403
  const size_t num_local_phi_dofs = sdm.GetNumLocalDOFs(flux_moments_uk_man_);
×
404
  const size_t num_global_phi_dofs = sdm.GetNumGlobalDOFs(flux_moments_uk_man_);
×
405

406
  return {num_local_phi_dofs, num_global_phi_dofs};
×
407
}
408

409
size_t
410
LBSProblem::MapPhiFieldFunction(size_t g, size_t m) const
22,826✔
411
{
412
  OpenSnLogicalErrorIf(phi_field_functions_local_map_.count({g, m}) == 0,
22,826✔
413
                       std::string("Failure to map phi field function g") + std::to_string(g) +
414
                         " m" + std::to_string(m));
415

416
  return phi_field_functions_local_map_.at({g, m});
22,826✔
417
}
418

419
std::shared_ptr<FieldFunctionGridBased>
420
LBSProblem::GetPowerFieldFunction() const
×
421
{
422
  OpenSnLogicalErrorIf(not options_.power_field_function_on,
×
423
                       "Called when options_.power_field_function_on == false");
424

425
  return field_functions_[power_gen_fieldfunc_local_handle_];
×
426
}
427

428
InputParameters
429
LBSProblem::GetOptionsBlock()
370✔
430
{
431
  InputParameters params;
370✔
432

433
  params.SetGeneralDescription("Set options from a large list of parameters");
740✔
434
  params.AddOptionalParameter("max_mpi_message_size",
740✔
435
                              32768,
436
                              "The maximum MPI message size used during sweep initialization.");
437
  params.AddOptionalParameter(
740✔
438
    "restart_writes_enabled", false, "Flag that controls writing of restart dumps");
439
  params.AddOptionalParameter("write_delayed_psi_to_restart",
740✔
440
                              true,
441
                              "Flag that controls writing of delayed angular fluxes to restarts.");
442
  params.AddOptionalParameter(
740✔
443
    "read_restart_path", "", "Full path for reading restart dumps including file stem.");
444
  params.AddOptionalParameter(
740✔
445
    "write_restart_path", "", "Full path for writing restart dumps including file stem.");
446
  params.AddOptionalParameter("write_restart_time_interval",
740✔
447
                              0,
448
                              "Time interval in seconds at which restart data is to be written.");
449
  params.AddOptionalParameter(
740✔
450
    "use_precursors", false, "Flag for using delayed neutron precursors.");
451
  params.AddOptionalParameter("use_source_moments",
740✔
452
                              false,
453
                              "Flag for ignoring fixed sources and selectively using source "
454
                              "moments obtained elsewhere.");
455
  params.AddOptionalParameter(
740✔
456
    "save_angular_flux", false, "Flag indicating whether angular fluxes are to be stored or not.");
457
  params.AddOptionalParameter(
740✔
458
    "adjoint", false, "Flag for toggling whether the solver is in adjoint mode.");
459
  params.AddOptionalParameter(
740✔
460
    "verbose_inner_iterations", true, "Flag to control verbosity of inner iterations.");
461
  params.AddOptionalParameter(
740✔
462
    "verbose_outer_iterations", true, "Flag to control verbosity of across-groupset iterations.");
463
  params.AddOptionalParameter(
740✔
464
    "max_ags_iterations", 100, "Maximum number of across-groupset iterations.");
465
  params.AddOptionalParameter("ags_tolerance", 1.0e-6, "Across-groupset iterations tolerance.");
740✔
466
  params.AddOptionalParameter("ags_convergence_check",
740✔
467
                              "l2",
468
                              "Type of convergence check for AGS iterations. Valid values are "
469
                              "`\"l2\"` and '\"pointwise\"'");
470
  params.AddOptionalParameter(
740✔
471
    "verbose_ags_iterations", true, "Flag to control verbosity of across-groupset iterations.");
472
  params.AddOptionalParameter("power_field_function_on",
740✔
473
                              false,
474
                              "Flag to control the creation of the power generation field "
475
                              "function. If set to `true` then a field function will be created "
476
                              "with the general name <solver_name>_power_generation`.");
477
  params.AddOptionalParameter("power_default_kappa",
740✔
478
                              3.20435e-11,
479
                              "Default `kappa` value (Energy released per fission) to use for "
480
                              "power generation when cross sections do not have `kappa` values. "
481
                              "Default: 3.20435e-11 Joule (corresponding to 200 MeV per fission).");
482
  params.AddOptionalParameter("power_normalization",
740✔
483
                              -1.0,
484
                              "Power normalization factor to use. Supply a negative or zero number "
485
                              "to turn this off.");
486
  params.AddOptionalParameter("field_function_prefix_option",
740✔
487
                              "prefix",
488
                              "Prefix option on field function names. Default: `\"prefix\"`. Can "
489
                              "be `\"prefix\"` or `\"solver_name\"`. By default this option uses "
490
                              "the value of the `field_function_prefix` parameter. If this "
491
                              "parameter is not set, flux field functions will be exported as "
492
                              "`phi_gXXX_mYYY` where `XXX` is the zero padded 3 digit group number "
493
                              "and `YYY` is the zero padded 3 digit moment.");
494
  params.AddOptionalParameter("field_function_prefix",
740✔
495
                              "",
496
                              "Prefix to use on all field functions. Default: `\"\"`. By default "
497
                              "this option is empty. Ff specified, flux moments will be exported "
498
                              "as `prefix_phi_gXXX_mYYY` where `XXX` is the zero padded 3 digit "
499
                              "group number and `YYY` is the zero padded 3 digit moment. The "
500
                              "underscore after \"prefix\" is added automatically.");
501
  params.ConstrainParameterRange("ags_convergence_check",
1,110✔
502
                                 AllowableRangeList::New({"l2", "pointwise"}));
370✔
503
  params.ConstrainParameterRange("field_function_prefix_option",
1,110✔
504
                                 AllowableRangeList::New({"prefix", "solver_name"}));
370✔
505

506
  return params;
370✔
507
}
×
508

509
InputParameters
510
LBSProblem::GetBoundaryOptionsBlock()
611✔
511
{
512
  InputParameters params;
611✔
513

514
  params.SetGeneralDescription("Set options for boundary conditions.");
1,222✔
515
  params.AddRequiredParameter<std::string>("name",
1,222✔
516
                                           "Boundary name that identifies the specific boundary");
517
  params.AddRequiredParameter<std::string>("type", "Boundary type specification.");
1,222✔
518
  params.AddOptionalParameterArray<double>("group_strength",
1,222✔
519
                                           {},
520
                                           "Required only if \"type\" is \"isotropic\". An array "
521
                                           "of isotropic strength per group");
522
  params.AddOptionalParameter(
1,222✔
523
    "function_name", "", "Text name of the function to be called for this boundary condition.");
524
  params.ConstrainParameterRange(
1,833✔
525
    "name", AllowableRangeList::New({"xmin", "xmax", "ymin", "ymax", "zmin", "zmax"}));
611✔
526
  params.ConstrainParameterRange("type",
1,833✔
527
                                 AllowableRangeList::New({"vacuum", "isotropic", "reflecting"}));
611✔
528

529
  return params;
611✔
530
}
×
531

532
InputParameters
533
LBSProblem::GetXSMapEntryBlock()
546✔
534
{
535
  InputParameters params;
546✔
536
  params.SetGeneralDescription("Set the cross-section map for the solver.");
1,092✔
537
  params.AddRequiredParameterArray("block_ids", "Mesh block IDs");
1,092✔
538
  params.AddRequiredParameter<std::shared_ptr<MultiGroupXS>>("xs", "Cross-section object");
1,092✔
539
  return params;
546✔
540
}
×
541

542
void
543
LBSProblem::SetOptions(const InputParameters& input)
184✔
544
{
545
  auto params = LBSProblem::GetOptionsBlock();
184✔
546
  params.AssignParameters(input);
184✔
547

548
  // Handle order insensitive options
549
  for (size_t p = 0; p < params.GetNumParameters(); ++p)
4,048✔
550
  {
551
    const auto& spec = params.GetParam(p);
3,864✔
552

553
    if (spec.GetName() == "max_mpi_message_size")
3,864✔
554
      options_.max_mpi_message_size = spec.GetValue<int>();
184✔
555

556
    else if (spec.GetName() == "restart_writes_enabled")
3,680✔
557
      options_.restart_writes_enabled = spec.GetValue<bool>();
184✔
558

559
    else if (spec.GetName() == "write_delayed_psi_to_restart")
3,496✔
560
      options_.write_delayed_psi_to_restart = spec.GetValue<bool>();
184✔
561

562
    else if (spec.GetName() == "read_restart_path")
3,312✔
563
    {
564
      options_.read_restart_path = spec.GetValue<std::string>();
184✔
565
      if (not options_.read_restart_path.empty())
184✔
566
        options_.read_restart_path += std::to_string(opensn::mpi_comm.rank()) + ".restart.h5";
36✔
567
    }
568

569
    else if (spec.GetName() == "write_restart_path")
3,128✔
570
    {
571
      options_.write_restart_path = spec.GetValue<std::string>();
184✔
572
      if (not options_.write_restart_path.empty())
184✔
573
        options_.write_restart_path += std::to_string(opensn::mpi_comm.rank()) + ".restart.h5";
×
574
    }
575

576
    else if (spec.GetName() == "write_restart_time_interval")
2,944✔
577
      options_.write_restart_time_interval = std::chrono::seconds(spec.GetValue<int>());
184✔
578

579
    else if (spec.GetName() == "use_precursors")
2,760✔
580
      options_.use_precursors = spec.GetValue<bool>();
184✔
581

582
    else if (spec.GetName() == "use_source_moments")
2,576✔
583
      options_.use_src_moments = spec.GetValue<bool>();
184✔
584

585
    else if (spec.GetName() == "save_angular_flux")
2,392✔
586
      options_.save_angular_flux = spec.GetValue<bool>();
184✔
587

588
    else if (spec.GetName() == "verbose_inner_iterations")
2,208✔
589
      options_.verbose_inner_iterations = spec.GetValue<bool>();
184✔
590

591
    else if (spec.GetName() == "max_ags_iterations")
2,024✔
592
      options_.max_ags_iterations = spec.GetValue<int>();
184✔
593

594
    else if (spec.GetName() == "ags_tolerance")
1,840✔
595
      options_.ags_tolerance = spec.GetValue<double>();
184✔
596

597
    else if (spec.GetName() == "ags_convergence_check")
1,656✔
598
    {
599
      auto check = spec.GetValue<std::string>();
184✔
600
      if (check == "pointwise")
184✔
601
        options_.ags_pointwise_convergence = true;
×
602
    }
184✔
603

604
    else if (spec.GetName() == "verbose_ags_iterations")
1,472✔
605
      options_.verbose_ags_iterations = spec.GetValue<bool>();
184✔
606

607
    else if (spec.GetName() == "verbose_outer_iterations")
1,288✔
608
      options_.verbose_outer_iterations = spec.GetValue<bool>();
184✔
609

610
    else if (spec.GetName() == "power_field_function_on")
1,104✔
611
      options_.power_field_function_on = spec.GetValue<bool>();
184✔
612

613
    else if (spec.GetName() == "power_default_kappa")
920✔
614
      options_.power_default_kappa = spec.GetValue<double>();
184✔
615

616
    else if (spec.GetName() == "power_normalization")
736✔
617
      options_.power_normalization = spec.GetValue<double>();
184✔
618

619
    else if (spec.GetName() == "field_function_prefix_option")
552✔
620
    {
621
      options_.field_function_prefix_option = spec.GetValue<std::string>();
184✔
622
    }
623

624
    else if (spec.GetName() == "field_function_prefix")
368✔
625
      options_.field_function_prefix = spec.GetValue<std::string>();
184✔
626
  } // for p
627

628
  if (options_.restart_writes_enabled)
184✔
629
  {
630
    // Create restart directory if necessary
631
    auto dir = options_.write_restart_path.parent_path();
×
632
    if (opensn::mpi_comm.rank() == 0)
×
633
    {
634
      if (not std::filesystem::exists(dir))
×
635
      {
636
        if (not std::filesystem::create_directories(dir))
×
637
          throw std::runtime_error(GetName() + ": Failed to create restart directory " +
×
638
                                   dir.string());
×
639
      }
640
      else if (not std::filesystem::is_directory(dir))
×
641
        throw std::runtime_error(GetName() + ": Restart path exists but is not a directory " +
×
642
                                 dir.string());
×
643
    }
644
    opensn::mpi_comm.barrier();
×
645
    UpdateRestartWriteTime();
×
646
  }
×
647
}
184✔
648

649
void
650
LBSProblem::SetBoundaryOptions(const InputParameters& params)
611✔
651
{
652
  const auto boundary_name = params.GetParamValue<std::string>("name");
611✔
653
  const auto bndry_type = params.GetParamValue<std::string>("type");
611✔
654

655
  const auto bid = supported_boundary_names.at(boundary_name);
611✔
656
  const std::map<std::string, LBSBoundaryType> type_list = {
611✔
657
    {"vacuum", LBSBoundaryType::VACUUM},
611✔
658
    {"isotropic", LBSBoundaryType::ISOTROPIC},
611✔
659
    {"reflecting", LBSBoundaryType::REFLECTING}};
2,444✔
660

661
  const auto type = type_list.at(bndry_type);
611✔
662
  switch (type)
611✔
663
  {
664
    case LBSBoundaryType::VACUUM:
526✔
665
    case LBSBoundaryType::REFLECTING:
526✔
666
    {
526✔
667
      boundary_preferences_[bid] = {type};
526✔
668
      break;
526✔
669
    }
670
    case LBSBoundaryType::ISOTROPIC:
85✔
671
    {
85✔
672
      OpenSnInvalidArgumentIf(not params.Has("group_strength"),
85✔
673
                              "Boundary conditions with type=\"isotropic\" require parameter "
674
                              "\"group_strength\"");
675

676
      params.RequireParameterBlockTypeIs("group_strength", ParameterBlockType::ARRAY);
85✔
677
      const auto group_strength = params.GetParamVectorValue<double>("group_strength");
85✔
678
      boundary_preferences_[bid] = {type, group_strength};
85✔
679
      break;
85✔
680
    }
85✔
681
    case LBSBoundaryType::ARBITRARY:
×
682
    {
×
683
      throw std::runtime_error(GetName() +
×
684
                               ": Arbitrary boundary conditions are not currently supported");
×
685
      break;
611✔
686
    }
687
  }
688
}
1,833✔
689

690
void
691
LBSProblem::Initialize()
297✔
692
{
693
  CALI_CXX_MARK_SCOPE("LBSProblem::Initialize");
297✔
694

695
  PrintSimHeader();
297✔
696
  mpi_comm.barrier();
297✔
697

698
  InitializeSpatialDiscretization();
297✔
699
  InitializeParrays();
297✔
700
  InitializeBoundaries();
297✔
701
  InitializeGPUExtras();
297✔
702
  SetAdjoint(false);
297✔
703

704
  // Initialize point sources
705
  for (auto& point_source : point_sources_)
306✔
706
    point_source->Initialize(*this);
9✔
707

708
  // Initialize volumetric sources
709
  for (auto& volumetric_source : volumetric_sources_)
622✔
710
    volumetric_source->Initialize(*this);
325✔
711
}
297✔
712

713
void
714
LBSProblem::PrintSimHeader()
×
715
{
716
  if (opensn::mpi_comm.rank() == 0)
×
717
  {
718
    std::stringstream outstr;
×
719
    outstr << "\n"
×
720
           << "Initializing " << GetName() << "\n\n"
×
721
           << "Scattering order    : " << scattering_order_ << "\n"
×
722
           << "Number of moments   : " << num_moments_ << "\n"
×
723
           << "Number of groups    : " << groups_.size() << "\n"
×
724
           << "Number of groupsets : " << groupsets_.size() << "\n\n";
×
725

726
    for (const auto& groupset : groupsets_)
×
727
    {
728
      outstr << "***** Groupset " << groupset.id << " *****\n"
×
729
             << "Groups:\n";
×
730
      const auto& groups = groupset.groups;
731
      constexpr int groups_per_line = 12;
732
      for (size_t i = 0; i < groups.size(); ++i)
×
733
      {
734
        outstr << std::setw(5) << groups[i].id << ' ';
×
735
        if ((i + 1) % groups_per_line == 0)
×
736
          outstr << '\n';
×
737
      }
738
      if (!groups.empty() && groups.size() % groups_per_line != 0)
×
739
        outstr << '\n';
×
740
    }
741

742
    log.Log() << outstr.str() << '\n';
×
743
  }
×
744
}
×
745

746
void
747
LBSProblem::InitializeSources(const InputParameters& params)
297✔
748
{
749
  if (params.Has("volumetric_sources"))
297✔
750
  {
751
    const auto& vol_srcs = params.GetParam("volumetric_sources");
297✔
752
    vol_srcs.RequireBlockTypeIs(ParameterBlockType::ARRAY);
297✔
753
    for (const auto& src : vol_srcs)
622✔
754
      volumetric_sources_.push_back(src.GetValue<std::shared_ptr<VolumetricSource>>());
650✔
755
  }
756

757
  if (params.Has("point_sources"))
297✔
758
  {
759
    const auto& pt_srcs = params.GetParam("point_sources");
297✔
760
    pt_srcs.RequireBlockTypeIs(ParameterBlockType::ARRAY);
297✔
761
    for (const auto& src : pt_srcs)
306✔
762
      point_sources_.push_back(src.GetValue<std::shared_ptr<PointSource>>());
18✔
763
  }
764
}
297✔
765

766
void
767
LBSProblem::InitializeGroupsets(const InputParameters& params)
297✔
768
{
769
  // Initialize groups
770
  if (num_groups_ == 0)
297✔
771
    throw std::invalid_argument(GetName() + ": Number of groups must be > 0");
×
772
  for (size_t g = 0; g < num_groups_; ++g)
20,024✔
773
    groups_.emplace_back(static_cast<int>(g));
19,727✔
774

775
  // Initialize groupsets
776
  const auto& groupsets_array = params.GetParam("groupsets");
297✔
777
  const size_t num_gs = groupsets_array.GetNumParameters();
297✔
778
  if (num_gs == 0)
297✔
779
    throw std::invalid_argument(GetName() + ": At least one groupset must be specified");
×
780
  for (size_t gs = 0; gs < num_gs; ++gs)
649✔
781
  {
782
    const auto& groupset_params = groupsets_array.GetParam(gs);
352✔
783
    InputParameters gs_input_params = LBSGroupset::GetInputParameters();
352✔
784
    gs_input_params.SetObjectType("LBSProblem:LBSGroupset");
352✔
785
    gs_input_params.AssignParameters(groupset_params);
352✔
786
    groupsets_.emplace_back(gs_input_params, gs, *this);
352✔
787
    if (groupsets_.back().groups.empty())
352✔
788
    {
789
      std::stringstream oss;
×
790
      oss << GetName() << ": No groups added to groupset " << groupsets_.back().id;
×
791
      throw std::runtime_error(oss.str());
×
792
    }
×
793
  }
352✔
794
}
297✔
795

796
void
797
LBSProblem::InitializeXSmapAndDensities(const InputParameters& params)
297✔
798
{
799
  // Build XS map
800
  const auto& xs_array = params.GetParam("xs_map");
297✔
801
  const size_t num_xs = xs_array.GetNumParameters();
297✔
802
  for (size_t i = 0; i < num_xs; ++i)
843✔
803
  {
804
    const auto& item_params = xs_array.GetParam(i);
546✔
805
    InputParameters xs_entry_pars = GetXSMapEntryBlock();
546✔
806
    xs_entry_pars.AssignParameters(item_params);
546✔
807

808
    const auto& block_ids_param = xs_entry_pars.GetParam("block_ids");
546✔
809
    block_ids_param.RequireBlockTypeIs(ParameterBlockType::ARRAY);
546✔
810
    const auto& block_ids = block_ids_param.GetVectorValue<int>();
546✔
811
    auto xs = xs_entry_pars.GetSharedPtrParam<MultiGroupXS>("xs");
546✔
812
    for (const auto& block_id : block_ids)
1,181✔
813
      block_id_to_xs_map_[block_id] = xs;
635✔
814
  }
546✔
815

816
  // Assign placeholder unit densities
817
  densities_local_.assign(grid_->local_cells.size(), 1.0);
297✔
818
}
297✔
819

820
void
821
LBSProblem::InitializeMaterials()
309✔
822
{
823
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeMaterials");
309✔
824

825
  log.Log0Verbose1() << "Initializing Materials";
618✔
826

827
  // Create set of material ids locally relevant
828
  int invalid_mat_cell_count = 0;
309✔
829
  std::set<int> unique_block_ids;
309✔
830
  for (auto& cell : grid_->local_cells)
325,889✔
831
  {
832
    unique_block_ids.insert(cell.block_id);
325,580✔
833
    if (cell.block_id < 0 or (block_id_to_xs_map_.find(cell.block_id) == block_id_to_xs_map_.end()))
325,580✔
834
      ++invalid_mat_cell_count;
×
835
  }
836
  const auto& ghost_cell_ids = grid_->cells.GetGhostGlobalIDs();
309✔
837
  for (uint64_t cell_id : ghost_cell_ids)
46,644✔
838
  {
839
    const auto& cell = grid_->cells[cell_id];
46,335✔
840
    unique_block_ids.insert(cell.block_id);
46,335✔
841
    if (cell.block_id < 0 or (block_id_to_xs_map_.find(cell.block_id) == block_id_to_xs_map_.end()))
46,335✔
842
      ++invalid_mat_cell_count;
×
843
  }
844
  OpenSnLogicalErrorIf(invalid_mat_cell_count > 0,
309✔
845
                       std::to_string(invalid_mat_cell_count) +
846
                         " cells encountered with an invalid material id.");
847

848
  // Get ready for processing
849
  for (const auto& [blk_id, mat] : block_id_to_xs_map_)
972✔
850
  {
851
    mat->SetAdjointMode(options_.adjoint);
663✔
852

853
    OpenSnLogicalErrorIf(mat->GetNumGroups() < groups_.size(),
663✔
854
                         "Cross-sections for block \"" + std::to_string(blk_id) +
855
                           "\" have fewer groups (" + std::to_string(mat->GetNumGroups()) +
856
                           ") than the simulation (" + std::to_string(groups_.size()) + "). " +
857
                           "Cross-sections must have at least as many groups as the simulation.");
858
  }
859

860
  // Initialize precursor properties
861
  num_precursors_ = 0;
309✔
862
  max_precursors_per_material_ = 0;
309✔
863
  for (const auto& mat_id_xs : block_id_to_xs_map_)
972✔
864
  {
865
    const auto& xs = mat_id_xs.second;
663✔
866
    num_precursors_ += xs->GetNumPrecursors();
663✔
867
    if (xs->GetNumPrecursors() > max_precursors_per_material_)
663✔
868
      max_precursors_per_material_ = xs->GetNumPrecursors();
8✔
869
  }
870

871
  // if no precursors, turn off precursors
872
  if (num_precursors_ == 0)
309✔
873
    options_.use_precursors = false;
301✔
874

875
  // check compatibility when precursors are on
876
  if (options_.use_precursors)
309✔
877
  {
878
    for (const auto& [mat_id, xs] : block_id_to_xs_map_)
16✔
879
    {
880
      OpenSnLogicalErrorIf(xs->IsFissionable() and num_precursors_ == 0,
8✔
881
                           "Incompatible cross-section data encountered for material id " +
882
                             std::to_string(mat_id) + ". When delayed neutron data is present " +
883
                             "for one fissionable matrial, it must be present for all fissionable "
884
                             "materials.");
885
    }
886
  }
887

888
  // Update transport views if available
889
  if (grid_->local_cells.size() == cell_transport_views_.size())
309✔
890
    for (const auto& cell : grid_->local_cells)
10,812✔
891
    {
892
      const auto& xs_ptr = block_id_to_xs_map_[cell.block_id];
10,800✔
893
      auto& transport_view = cell_transport_views_[cell.local_id];
10,800✔
894
      transport_view.ReassignXS(*xs_ptr);
10,800✔
895
    }
896

897
  mpi_comm.barrier();
309✔
898
}
309✔
899

900
void
901
LBSProblem::InitializeSpatialDiscretization()
289✔
902
{
903
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeSpatialDiscretization");
289✔
904

905
  log.Log() << "Initializing spatial discretization.\n";
578✔
906
  discretization_ = PieceWiseLinearDiscontinuous::New(grid_);
289✔
907

908
  ComputeUnitIntegrals();
289✔
909
}
289✔
910

911
void
912
LBSProblem::ComputeUnitIntegrals()
297✔
913
{
914
  CALI_CXX_MARK_SCOPE("LBSProblem::ComputeUnitIntegrals");
297✔
915

916
  log.Log() << "Computing unit integrals.\n";
594✔
917
  const auto& sdm = *discretization_;
297✔
918

919
  const size_t num_local_cells = grid_->local_cells.size();
297✔
920
  unit_cell_matrices_.resize(num_local_cells);
297✔
921

922
  for (const auto& cell : grid_->local_cells)
315,077✔
923
    unit_cell_matrices_[cell.local_id] =
314,780✔
924
      ComputeUnitCellIntegrals(sdm, cell, grid_->GetCoordinateSystem());
314,780✔
925

926
  const auto ghost_ids = grid_->cells.GetGhostGlobalIDs();
297✔
927
  for (auto ghost_id : ghost_ids)
45,876✔
928
    unit_ghost_cell_matrices_[ghost_id] =
45,579✔
929
      ComputeUnitCellIntegrals(sdm, grid_->cells[ghost_id], grid_->GetCoordinateSystem());
91,158✔
930

931
  // Assessing global unit cell matrix storage
932
  std::array<size_t, 2> num_local_ucms = {unit_cell_matrices_.size(),
297✔
933
                                          unit_ghost_cell_matrices_.size()};
297✔
934
  std::array<size_t, 2> num_global_ucms = {0, 0};
297✔
935

936
  mpi_comm.all_reduce(num_local_ucms.data(), 2, num_global_ucms.data(), mpi::op::sum<size_t>());
297✔
937

938
  opensn::mpi_comm.barrier();
297✔
939
  log.Log() << "Ghost cell unit cell-matrix ratio: "
297✔
940
            << (double)num_global_ucms[1] * 100 / (double)num_global_ucms[0] << "%";
594✔
941
  log.Log() << "Cell matrices computed.";
594✔
942
}
297✔
943

944
void
945
LBSProblem::InitializeParrays()
297✔
946
{
947
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeParrays");
297✔
948

949
  log.Log() << "Initializing parallel arrays."
594✔
950
            << " G=" << num_groups_ << " M=" << num_moments_ << std::endl;
297✔
951

952
  // Initialize unknown
953
  // structure
954
  flux_moments_uk_man_.unknowns.clear();
297✔
955
  for (size_t m = 0; m < num_moments_; ++m)
1,140✔
956
  {
957
    flux_moments_uk_man_.AddUnknown(UnknownType::VECTOR_N, groups_.size());
843✔
958
    flux_moments_uk_man_.unknowns.back().name = "m" + std::to_string(m);
843✔
959
  }
960

961
  // Compute local # of dof
962
  local_node_count_ = discretization_->GetNumLocalNodes();
297✔
963
  global_node_count_ = discretization_->GetNumGlobalNodes();
297✔
964

965
  // Compute num of unknowns
966
  size_t num_grps = groups_.size();
297✔
967
  size_t local_unknown_count = local_node_count_ * num_grps * num_moments_;
297✔
968

969
  log.LogAllVerbose1() << "LBS Number of phi unknowns: " << local_unknown_count;
594✔
970

971
  // Size local vectors
972
  q_moments_local_.assign(local_unknown_count, 0.0);
297✔
973
  phi_old_local_.assign(local_unknown_count, 0.0);
297✔
974
  phi_new_local_.assign(local_unknown_count, 0.0);
297✔
975

976
  // Setup precursor vector
977
  if (options_.use_precursors)
297✔
978
  {
979
    size_t num_precursor_dofs = grid_->local_cells.size() * max_precursors_per_material_;
8✔
980
    precursor_new_local_.assign(num_precursor_dofs, 0.0);
8✔
981
  }
982

983
  // Initialize transport views
984
  // Transport views act as a data structure to store information
985
  // related to the transport simulation. The most prominent function
986
  // here is that it holds the means to know where a given cell's
987
  // transport quantities are located in the unknown vectors (i.e. phi)
988
  //
989
  // Also, for a given cell, within a given sweep chunk,
990
  // we need to solve a matrix which square size is the
991
  // amount of nodes on the cell. max_cell_dof_count is
992
  // initialized here.
993
  //
994
  size_t block_MG_counter = 0; // Counts the strides of moment and group
297✔
995

996
  const Vector3 ihat(1.0, 0.0, 0.0);
297✔
997
  const Vector3 jhat(0.0, 1.0, 0.0);
297✔
998
  const Vector3 khat(0.0, 0.0, 1.0);
297✔
999

1000
  min_cell_dof_count_ = static_cast<size_t>(-1);
297✔
1001
  max_cell_dof_count_ = 0;
297✔
1002
  cell_transport_views_.clear();
297✔
1003
  cell_transport_views_.reserve(grid_->local_cells.size());
297✔
1004
  for (auto& cell : grid_->local_cells)
315,077✔
1005
  {
1006
    size_t num_nodes = discretization_->GetCellNumNodes(cell);
314,780✔
1007

1008
    // compute cell volumes
1009
    double cell_volume = 0.0;
314,780✔
1010
    const auto& IntV_shapeI = unit_cell_matrices_[cell.local_id].intV_shapeI;
314,780✔
1011
    for (size_t i = 0; i < num_nodes; ++i)
2,665,842✔
1012
      cell_volume += IntV_shapeI(i);
2,351,062✔
1013

1014
    size_t cell_phi_address = block_MG_counter;
314,780✔
1015

1016
    const size_t num_faces = cell.faces.size();
314,780✔
1017
    std::vector<bool> face_local_flags(num_faces, true);
314,780✔
1018
    std::vector<int> face_locality(num_faces, opensn::mpi_comm.rank());
314,780✔
1019
    std::vector<const Cell*> neighbor_cell_ptrs(num_faces, nullptr);
314,780✔
1020
    bool cell_on_boundary = false;
314,780✔
1021
    int f = 0;
314,780✔
1022
    for (auto& face : cell.faces)
2,111,370✔
1023
    {
1024
      if (not face.has_neighbor)
1,796,590✔
1025
      {
1026
        Vector3& n = face.normal;
60,132✔
1027

1028
        int boundary_id = -1;
60,132✔
1029
        if (n.Dot(ihat) < -0.999)
60,132✔
1030
          boundary_id = XMIN;
1031
        else if (n.Dot(ihat) > 0.999)
51,639✔
1032
          boundary_id = XMAX;
1033
        else if (n.Dot(jhat) < -0.999)
43,308✔
1034
          boundary_id = YMIN;
1035
        else if (n.Dot(jhat) > 0.999)
35,017✔
1036
          boundary_id = YMAX;
1037
        else if (n.Dot(khat) < -0.999)
26,888✔
1038
          boundary_id = ZMIN;
1039
        else if (n.Dot(khat) > 0.999)
13,444✔
1040
          boundary_id = ZMAX;
1041

1042
        if (boundary_id >= 0)
1043
          face.neighbor_id = boundary_id;
60,132✔
1044
        cell_on_boundary = true;
60,132✔
1045

1046
        face_local_flags[f] = false;
60,132✔
1047
        face_locality[f] = -1;
60,132✔
1048
      } // if bndry
1049
      else
1050
      {
1051
        const int neighbor_partition = face.GetNeighborPartitionID(grid_.get());
1,736,458✔
1052
        face_local_flags[f] = (neighbor_partition == opensn::mpi_comm.rank());
1,736,458✔
1053
        face_locality[f] = neighbor_partition;
1,736,458✔
1054
        neighbor_cell_ptrs[f] = &grid_->cells[face.neighbor_id];
1,736,458✔
1055
      }
1056

1057
      ++f;
1,796,590✔
1058
    } // for f
1059

1060
    max_cell_dof_count_ = std::max(max_cell_dof_count_, num_nodes);
314,780✔
1061
    min_cell_dof_count_ = std::min(min_cell_dof_count_, num_nodes);
314,780✔
1062
    cell_transport_views_.emplace_back(cell_phi_address,
629,560✔
1063
                                       num_nodes,
1064
                                       num_grps,
1065
                                       num_moments_,
314,780✔
1066
                                       num_faces,
1067
                                       *block_id_to_xs_map_[cell.block_id],
314,780✔
1068
                                       cell_volume,
1069
                                       face_local_flags,
1070
                                       face_locality,
1071
                                       neighbor_cell_ptrs,
1072
                                       cell_on_boundary);
1073
    block_MG_counter += num_nodes * num_grps * num_moments_;
314,780✔
1074
  } // for local cell
314,780✔
1075

1076
  // Populate grid nodal mappings
1077
  // This is used in the Flux Data Structures (FLUDS)
1078
  grid_nodal_mappings_.clear();
297✔
1079
  grid_nodal_mappings_.reserve(grid_->local_cells.size());
297✔
1080
  for (auto& cell : grid_->local_cells)
315,077✔
1081
  {
1082
    CellFaceNodalMapping cell_nodal_mapping;
314,780✔
1083
    cell_nodal_mapping.reserve(cell.faces.size());
314,780✔
1084

1085
    for (auto& face : cell.faces)
2,111,370✔
1086
    {
1087
      std::vector<short> face_node_mapping;
1,796,590✔
1088
      std::vector<short> cell_node_mapping;
1,796,590✔
1089
      int adj_face_idx = -1;
1,796,590✔
1090

1091
      if (face.has_neighbor)
1,796,590✔
1092
      {
1093
        grid_->FindAssociatedVertices(face, face_node_mapping);
1,736,458✔
1094
        grid_->FindAssociatedCellVertices(face, cell_node_mapping);
1,736,458✔
1095
        adj_face_idx = face.GetNeighborAdjacentFaceIndex(grid_.get());
1,736,458✔
1096
      }
1097

1098
      cell_nodal_mapping.emplace_back(adj_face_idx, face_node_mapping, cell_node_mapping);
1,796,590✔
1099
    } // for f
1,796,590✔
1100

1101
    grid_nodal_mappings_.push_back(cell_nodal_mapping);
314,780✔
1102
  } // for local cell
314,780✔
1103

1104
  // Get grid localized communicator set
1105
  grid_local_comm_set_ = grid_->MakeMPILocalCommunicatorSet();
297✔
1106

1107
  // Initialize Field Functions
1108
  InitializeFieldFunctions();
297✔
1109

1110
  opensn::mpi_comm.barrier();
297✔
1111
  log.Log() << "Done with parallel arrays." << std::endl;
594✔
1112
}
297✔
1113

1114
void
1115
LBSProblem::InitializeFieldFunctions()
297✔
1116
{
1117
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeFieldFunctions");
297✔
1118

1119
  if (not field_functions_.empty())
297✔
1120
    return;
×
1121

1122
  // Initialize Field Functions for flux moments
1123
  phi_field_functions_local_map_.clear();
297✔
1124

1125
  for (size_t g = 0; g < groups_.size(); ++g)
20,024✔
1126
  {
1127
    for (size_t m = 0; m < num_moments_; ++m)
92,814✔
1128
    {
1129
      std::string prefix;
73,087✔
1130
      if (options_.field_function_prefix_option == "prefix")
73,087✔
1131
      {
1132
        prefix = options_.field_function_prefix;
73,087✔
1133
        if (not prefix.empty())
73,087✔
1134
          prefix += "_";
1✔
1135
      }
1136
      if (options_.field_function_prefix_option == "solver_name")
73,087✔
1137
        prefix = GetName() + "_";
×
1138

1139
      char buff[100];
73,087✔
1140
      snprintf(
73,087✔
1141
        buff, 99, "%sphi_g%03d_m%02d", prefix.c_str(), static_cast<int>(g), static_cast<int>(m));
1142
      const std::string name = std::string(buff);
73,087✔
1143

1144
      auto group_ff = std::make_shared<FieldFunctionGridBased>(
73,087✔
1145
        name, discretization_, Unknown(UnknownType::SCALAR));
73,087✔
1146

1147
      field_function_stack.push_back(group_ff);
146,174✔
1148
      field_functions_.push_back(group_ff);
73,087✔
1149

1150
      phi_field_functions_local_map_[{g, m}] = field_functions_.size() - 1;
73,087✔
1151
    } // for m
73,087✔
1152
  } // for g
1153

1154
  // Initialize power generation field function
1155
  if (options_.power_field_function_on)
297✔
1156
  {
1157
    std::string prefix;
4✔
1158
    if (options_.field_function_prefix_option == "prefix")
4✔
1159
    {
1160
      prefix = options_.field_function_prefix;
4✔
1161
      if (not prefix.empty())
4✔
1162
        prefix += "_";
×
1163
    }
1164
    if (options_.field_function_prefix_option == "solver_name")
4✔
1165
      prefix = GetName() + "_";
×
1166

1167
    auto power_ff = std::make_shared<FieldFunctionGridBased>(
4✔
1168
      prefix + "power_generation", discretization_, Unknown(UnknownType::SCALAR));
8✔
1169

1170
    field_function_stack.push_back(power_ff);
8✔
1171
    field_functions_.push_back(power_ff);
4✔
1172

1173
    power_gen_fieldfunc_local_handle_ = field_functions_.size() - 1;
4✔
1174
  }
4✔
1175
}
297✔
1176

1177
void
1178
LBSProblem::InitializeSolverSchemes()
297✔
1179
{
1180
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeSolverSchemes");
297✔
1181

1182
  log.Log() << "Initializing WGS and AGS solvers";
594✔
1183

1184
  InitializeWGSSolvers();
297✔
1185

1186
  ags_solver_ = std::make_shared<AGSLinearSolver>(*this, wgs_solvers_);
297✔
1187
  if (groupsets_.size() == 1)
297✔
1188
  {
1189
    ags_solver_->SetMaxIterations(1);
246✔
1190
    ags_solver_->SetVerbosity(false);
246✔
1191
  }
1192
  else
1193
  {
1194
    ags_solver_->SetMaxIterations(options_.max_ags_iterations);
51✔
1195
    ags_solver_->SetVerbosity(options_.verbose_ags_iterations);
51✔
1196
  }
1197
  ags_solver_->SetTolerance(options_.ags_tolerance);
297✔
1198
}
297✔
1199

1200
#ifndef __OPENSN_USE_CUDA__
1201
void
1202
LBSProblem::InitializeGPUExtras()
297✔
1203
{
1204
}
297✔
1205

1206
void
1207
LBSProblem::ResetGPUCarriers()
293✔
1208
{
1209
}
293✔
1210

1211
void
1212
LBSProblem::CheckCapableDevices()
×
1213
{
1214
}
×
1215
#endif // __OPENSN_USE_CUDA__
1216

1217
std::vector<double>
1218
LBSProblem::MakeSourceMomentsFromPhi()
4✔
1219
{
1220
  CALI_CXX_MARK_SCOPE("LBSProblem::MakeSourceMomentsFromPhi");
4✔
1221

1222
  size_t num_local_dofs = discretization_->GetNumLocalDOFs(flux_moments_uk_man_);
4✔
1223

1224
  std::vector<double> source_moments(num_local_dofs, 0.0);
4✔
1225
  for (auto& groupset : groupsets_)
8✔
1226
  {
1227
    active_set_source_function_(groupset,
4✔
1228
                                source_moments,
1229
                                phi_new_local_,
4✔
1230
                                APPLY_AGS_SCATTER_SOURCES | APPLY_WGS_SCATTER_SOURCES |
1231
                                  APPLY_AGS_FISSION_SOURCES | APPLY_WGS_FISSION_SOURCES);
4✔
1232
  }
1233

1234
  return source_moments;
4✔
1235
}
4✔
1236

1237
void
1238
LBSProblem::UpdateFieldFunctions()
309✔
1239
{
1240
  CALI_CXX_MARK_SCOPE("LBSProblem::UpdateFieldFunctions");
309✔
1241

1242
  const auto& sdm = *discretization_;
309✔
1243
  const auto& phi_uk_man = flux_moments_uk_man_;
309✔
1244

1245
  // Update flux moments
1246
  for (const auto& [g_and_m, ff_index] : phi_field_functions_local_map_)
73,444✔
1247
  {
1248
    const size_t g = g_and_m.first;
73,135✔
1249
    const size_t m = g_and_m.second;
73,135✔
1250

1251
    std::vector<double> data_vector_local(local_node_count_, 0.0);
73,135✔
1252

1253
    for (const auto& cell : grid_->local_cells)
17,970,968✔
1254
    {
1255
      const auto& cell_mapping = sdm.GetCellMapping(cell);
17,897,833✔
1256
      const size_t num_nodes = cell_mapping.GetNumNodes();
17,897,833✔
1257

1258
      for (size_t i = 0; i < num_nodes; ++i)
126,646,069✔
1259
      {
1260
        const auto imapA = sdm.MapDOFLocal(cell, i, phi_uk_man, m, g);
108,748,236✔
1261
        const auto imapB = sdm.MapDOFLocal(cell, i);
108,748,236✔
1262

1263
        data_vector_local[imapB] = phi_new_local_[imapA];
108,748,236✔
1264
      } // for node
1265
    } // for cell
1266

1267
    auto& ff_ptr = field_functions_.at(ff_index);
73,135✔
1268
    ff_ptr->UpdateFieldVector(data_vector_local);
73,135✔
1269
  }
73,135✔
1270

1271
  // Update power generation
1272
  if (options_.power_field_function_on)
309✔
1273
  {
1274
    std::vector<double> data_vector_local(local_node_count_, 0.0);
4✔
1275

1276
    double local_total_power = 0.0;
4✔
1277
    for (const auto& cell : grid_->local_cells)
83,268✔
1278
    {
1279
      const auto& cell_mapping = sdm.GetCellMapping(cell);
83,264✔
1280
      const size_t num_nodes = cell_mapping.GetNumNodes();
83,264✔
1281

1282
      const auto& Vi = unit_cell_matrices_[cell.local_id].intV_shapeI;
83,264✔
1283

1284
      const auto& xs = block_id_to_xs_map_.at(cell.block_id);
83,264✔
1285

1286
      if (not xs->IsFissionable())
83,264✔
1287
        continue;
56,360✔
1288

1289
      for (size_t i = 0; i < num_nodes; ++i)
134,520✔
1290
      {
1291
        const auto imapA = sdm.MapDOFLocal(cell, i);
107,616✔
1292
        const auto imapB = sdm.MapDOFLocal(cell, i, phi_uk_man, 0, 0);
107,616✔
1293

1294
        double nodal_power = 0.0;
1295
        for (size_t g = 0; g < groups_.size(); ++g)
860,928✔
1296
        {
1297
          const double sigma_fg = xs->GetSigmaFission()[g];
753,312✔
1298
          // const double kappa_g = xs->Kappa()[g];
1299
          const double kappa_g = options_.power_default_kappa;
753,312✔
1300

1301
          nodal_power += kappa_g * sigma_fg * phi_new_local_[imapB + g];
753,312✔
1302
        } // for g
1303

1304
        data_vector_local[imapA] = nodal_power;
107,616✔
1305
        local_total_power += nodal_power * Vi(i);
107,616✔
1306
      } // for node
1307
    } // for cell
1308

1309
    if (options_.power_normalization > 0.0)
4✔
1310
    {
1311
      double global_total_power = 0.0;
4✔
1312
      mpi_comm.all_reduce(local_total_power, global_total_power, mpi::op::sum<double>());
4✔
1313

1314
      Scale(data_vector_local, options_.power_normalization / global_total_power);
4✔
1315
    }
1316

1317
    const size_t ff_index = power_gen_fieldfunc_local_handle_;
4✔
1318

1319
    auto& ff_ptr = field_functions_.at(ff_index);
4✔
1320
    ff_ptr->UpdateFieldVector(data_vector_local);
4✔
1321

1322
  } // if power enabled
4✔
1323
}
309✔
1324

1325
void
1326
LBSProblem::SetPhiFromFieldFunctions(PhiSTLOption which_phi,
×
1327
                                     const std::vector<size_t>& m_indices,
1328
                                     const std::vector<size_t>& g_indices)
1329
{
1330
  CALI_CXX_MARK_SCOPE("LBSProblem::SetPhiFromFieldFunctions");
×
1331

1332
  std::vector<size_t> m_ids_to_copy = m_indices;
×
1333
  std::vector<size_t> g_ids_to_copy = g_indices;
×
1334
  if (m_indices.empty())
×
1335
    for (size_t m = 0; m < num_moments_; ++m)
×
1336
      m_ids_to_copy.push_back(m);
×
1337
  if (g_ids_to_copy.empty())
×
1338
    for (size_t g = 0; g < num_groups_; ++g)
×
1339
      g_ids_to_copy.push_back(g);
×
1340

1341
  const auto& sdm = *discretization_;
×
1342
  const auto& phi_uk_man = flux_moments_uk_man_;
×
1343

1344
  for (const size_t m : m_ids_to_copy)
×
1345
  {
1346
    for (const size_t g : g_ids_to_copy)
×
1347
    {
1348
      const size_t ff_index = phi_field_functions_local_map_.at({g, m});
×
1349
      const auto& ff_ptr = field_functions_.at(ff_index);
×
1350
      const auto& ff_data = ff_ptr->GetLocalFieldVector();
×
1351

1352
      for (const auto& cell : grid_->local_cells)
×
1353
      {
1354
        const auto& cell_mapping = sdm.GetCellMapping(cell);
×
1355
        const size_t num_nodes = cell_mapping.GetNumNodes();
×
1356

1357
        for (size_t i = 0; i < num_nodes; ++i)
×
1358
        {
1359
          const auto imapA = sdm.MapDOFLocal(cell, i);
×
1360
          const auto imapB = sdm.MapDOFLocal(cell, i, phi_uk_man, m, g);
×
1361

1362
          if (which_phi == PhiSTLOption::PHI_OLD)
×
1363
            phi_old_local_[imapB] = ff_data[imapA];
×
1364
          else if (which_phi == PhiSTLOption::PHI_NEW)
×
1365
            phi_new_local_[imapB] = ff_data[imapA];
×
1366
        } // for node
1367
      } // for cell
1368
    } // for g
1369
  } // for m
1370
}
×
1371

1372
LBSProblem::~LBSProblem()
293✔
1373
{
1374
  ResetGPUCarriers();
1375
}
1,465✔
1376

293✔
1377
void
1378
LBSProblem::SetAdjoint(bool adjoint)
309✔
1379
{
1380
  if (adjoint != options_.adjoint)
309✔
1381
  {
1382
    options_.adjoint = adjoint;
12✔
1383

1384
    // If a discretization exists, the solver has already been initialized.
1385
    // Reinitialize the materials to obtain the appropriate xs and clear the
1386
    // sources to prepare for defining the adjoint problem
1387
    if (discretization_)
12✔
1388
    {
1389
      // The materials are reinitialized here to ensure that the proper cross sections
1390
      // are available to the solver. Because an adjoint solve requires volumetric or
1391
      // point sources, the material-based sources are not set within the initialize routine.
1392
      InitializeMaterials();
12✔
1393

1394
      // Forward and adjoint sources are fundamentally different, so any existing sources
1395
      // should be cleared and reset through options upon changing modes.
1396
      point_sources_.clear();
12✔
1397
      volumetric_sources_.clear();
12✔
1398
      boundary_preferences_.clear();
12✔
1399

1400
      // Set all solutions to zero.
1401
      phi_old_local_.assign(phi_old_local_.size(), 0.0);
12✔
1402
      phi_new_local_.assign(phi_new_local_.size(), 0.0);
12✔
1403
      ZeroSolutions();
12✔
1404
      precursor_new_local_.assign(precursor_new_local_.size(), 0.0);
12✔
1405
    }
1406
  }
1407
}
309✔
1408

1409
} // namespace opensn
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc