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

Open-Sn / opensn / 20943481625

13 Jan 2026 01:13AM UTC coverage: 74.47% (+0.03%) from 74.437%
20943481625

push

github

web-flow
Merge pull request #892 from andrsd/issue/90-quad-order

Quadrature order, number of polar, number of azimuthal is `unsigned int`

15 of 17 new or added lines in 5 files covered. (88.24%)

248 existing lines in 10 files now uncovered.

18733 of 25155 relevant lines covered (74.47%)

66799244.41 hits per line

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

82.83
/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 "framework/data_types/allowable_range.h"
18
#include "caliper/cali.h"
19
#include <algorithm>
20
#include <iomanip>
21
#include <fstream>
22
#include <cstring>
23
#include <cassert>
24
#include <memory>
25
#include <stdexcept>
26
#include <sys/stat.h>
27

28
namespace opensn
29
{
30

31
LBSProblem::LBSProblem(std::string name, std::shared_ptr<MeshContinuum> grid)
×
32
  : Problem(std::move(name)), grid_(std::move(grid)), use_gpus_(false)
×
33
{
34
}
×
35

36
InputParameters
37
LBSProblem::GetInputParameters()
362✔
38
{
39
  InputParameters params = Problem::GetInputParameters();
362✔
40

41
  params.ChangeExistingParamToOptional("name", "LBSProblem");
724✔
42

43
  params.AddRequiredParameter<std::shared_ptr<MeshContinuum>>("mesh", "Mesh");
724✔
44

45
  params.AddRequiredParameter<size_t>("num_groups", "The total number of groups within the solver");
724✔
46

47
  params.AddRequiredParameterArray("groupsets",
724✔
48
                                   "An array of blocks each specifying the input parameters for a "
49
                                   "<TT>LBSGroupset</TT>.");
50
  params.LinkParameterToBlock("groupsets", "LBSGroupset");
724✔
51

52
  params.AddRequiredParameterArray("xs_map",
724✔
53
                                   "Cross-section map from block IDs to cross-section objects.");
54

55
  params.AddOptionalParameterArray<std::shared_ptr<VolumetricSource>>(
724✔
56
    "volumetric_sources", {}, "An array of handles to volumetric sources.");
57

58
  params.AddOptionalParameterArray<std::shared_ptr<PointSource>>(
724✔
59
    "point_sources", {}, "An array of point sources.");
60

61
  params.AddOptionalParameterArray(
724✔
62
    "boundary_conditions", {}, "An array containing tables for each boundary specification.");
63
  params.LinkParameterToBlock("boundary_conditions", "BoundaryOptionsBlock");
724✔
64

65
  params.AddOptionalParameterBlock(
724✔
66
    "options", ParameterBlock(), "Block of options. See <TT>OptionsBlock</TT>.");
724✔
67
  params.LinkParameterToBlock("options", "OptionsBlock");
724✔
68

69
  params.AddOptionalParameter("use_gpus", false, "Offload the sweep computation to GPUs.");
724✔
70

71
  params.AddOptionalParameter(
724✔
72
    "time_dependent", false, "Flag indicating whether the problem is time dependent.");
73
  return params;
362✔
74
}
×
75

76
LBSProblem::LBSProblem(const InputParameters& params)
362✔
77
  : Problem(params),
78
    time_dependent_(params.GetParamValue<bool>("time_dependent")),
362✔
79
    num_groups_(params.GetParamValue<size_t>("num_groups")),
362✔
80
    grid_(params.GetSharedPtrParam<MeshContinuum>("mesh")),
362✔
81
    use_gpus_(params.GetParamValue<bool>("use_gpus"))
1,448✔
82
{
83
  // Check system for GPU acceleration
84
  if (use_gpus_)
362✔
85
  {
86
    if (time_dependent_)
×
87
      throw std::invalid_argument(GetName() +
×
88
                                  ": Time dependent problems are not supported on GPUs.");
×
89
#ifdef __OPENSN_USE_CUDA__
90
    CheckCapableDevices();
91
#else
92
    throw std::invalid_argument(
×
93
      GetName() + ": GPU support was requested, but OpenSn was built without CUDA enabled.");
×
94
#endif // __OPENSN_USE_CUDA__
95
  }
96

97
  // Initialize options
98
  if (params.IsParameterValid("options"))
362✔
99
  {
100
    auto options_params = LBSProblem::GetOptionsBlock();
202✔
101
    options_params.AssignParameters(params.GetParam("options"));
204✔
102
    SetOptions(options_params);
200✔
103
  }
202✔
104

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

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

123
  InitializeGroupsets(params);
360✔
124
  InitializeSources(params);
360✔
125
  InitializeXSmapAndDensities(params);
360✔
126
  InitializeMaterials();
360✔
127
}
380✔
128

129
LBSOptions&
130
LBSProblem::GetOptions()
20,079✔
131
{
132
  return options_;
20,079✔
133
}
134

135
const LBSOptions&
136
LBSProblem::GetOptions() const
576,933,218✔
137
{
138
  return options_;
576,933,218✔
139
}
140

141
double
142
LBSProblem::GetTime() const
110,976✔
143
{
144
  return time_;
110,976✔
145
}
146

147
void
148
LBSProblem::SetTime(double time)
1,720✔
149
{
150
  time_ = time;
1,720✔
151
}
1,720✔
152

153
void
154
LBSProblem::SetTimeStep(double dt)
860✔
155
{
156
  dt_ = dt;
860✔
157
}
860✔
158

159
double
160
LBSProblem::GetTimeStep() const
1,709,898,240✔
161
{
162
  return dt_;
1,709,898,240✔
163
}
164

165
bool
166
LBSProblem::IsTimeDependent() const
37,695✔
167
{
168
  return time_dependent_;
37,695✔
169
}
170

171
void
172
LBSProblem::SetTheta(double theta)
860✔
173
{
174
  theta_ = theta;
860✔
175
}
860✔
176

177
double
178
LBSProblem::GetTheta() const
1,709,898,240✔
179
{
180
  return theta_;
1,709,898,240✔
181
}
182

183
GeometryType
184
LBSProblem::GetGeometryType() const
4✔
185
{
186
  return geometry_type_;
4✔
187
}
188

189
size_t
190
LBSProblem::GetNumMoments() const
187,545✔
191
{
192
  return num_moments_;
187,545✔
193
}
194

195
unsigned int
196
LBSProblem::GetMaxCellDOFCount() const
419✔
197
{
198
  return max_cell_dof_count_;
419✔
199
}
200

201
unsigned int
202
LBSProblem::GetMinCellDOFCount() const
419✔
203
{
204
  return min_cell_dof_count_;
419✔
205
}
206

207
bool
208
LBSProblem::UseGPUs() const
374✔
209
{
210
  return use_gpus_;
374✔
211
}
212

213
size_t
214
LBSProblem::GetNumGroups() const
74,253✔
215
{
216
  return num_groups_;
74,253✔
217
}
218

219
unsigned int
220
LBSProblem::GetScatteringOrder() const
4✔
221
{
222
  return scattering_order_;
4✔
223
}
224

225
size_t
226
LBSProblem::GetNumPrecursors() const
×
227
{
228
  return num_precursors_;
×
229
}
230

231
size_t
232
LBSProblem::GetMaxPrecursorsPerMaterial() const
8✔
233
{
234
  return max_precursors_per_material_;
8✔
235
}
236

237
const std::vector<LBSGroup>&
238
LBSProblem::GetGroups() const
194,809✔
239
{
240
  return groups_;
194,809✔
241
}
242

243
std::vector<LBSGroupset>&
244
LBSProblem::GetGroupsets()
7,312,347✔
245
{
246
  return groupsets_;
7,312,347✔
247
}
248

249
const std::vector<LBSGroupset>&
250
LBSProblem::GetGroupsets() const
×
251
{
252
  return groupsets_;
×
253
}
254

255
void
256
LBSProblem::AddPointSource(std::shared_ptr<PointSource> point_source)
×
257
{
258
  point_sources_.push_back(point_source);
×
259
  if (discretization_)
×
260
    point_sources_.back()->Initialize(*this);
×
261
}
×
262

263
void
264
LBSProblem::ClearPointSources()
×
265
{
266
  point_sources_.clear();
×
267
}
×
268

269
const std::vector<std::shared_ptr<PointSource>>&
270
LBSProblem::GetPointSources() const
8,332✔
271
{
272
  return point_sources_;
8,332✔
273
}
274

275
void
276
LBSProblem::AddVolumetricSource(std::shared_ptr<VolumetricSource> volumetric_source)
16✔
277
{
278
  volumetric_sources_.push_back(volumetric_source);
16✔
279
  if (discretization_)
16✔
280
    volumetric_sources_.back()->Initialize(*this);
16✔
281
}
16✔
282

283
void
284
LBSProblem::ClearVolumetricSources()
4✔
285
{
286
  volumetric_sources_.clear();
4✔
287
}
4✔
288

289
const std::vector<std::shared_ptr<VolumetricSource>>&
290
LBSProblem::GetVolumetricSources() const
8,332✔
291
{
292
  return volumetric_sources_;
8,332✔
293
}
294

295
void
296
LBSProblem::ClearBoundaries()
×
297
{
298
  boundary_preferences_.clear();
×
299
}
×
300

301
const BlockID2XSMap&
302
LBSProblem::GetBlockID2XSMap() const
7,040✔
303
{
304
  return block_id_to_xs_map_;
7,040✔
305
}
306

307
std::shared_ptr<MeshContinuum>
308
LBSProblem::GetGrid() const
255,661✔
309
{
310
  return grid_;
255,661✔
311
}
312

313
const SpatialDiscretization&
314
LBSProblem::GetSpatialDiscretization() const
83,044✔
315
{
316
  return *discretization_;
83,044✔
317
}
318

319
const std::vector<UnitCellMatrices>&
320
LBSProblem::GetUnitCellMatrices() const
7,798✔
321
{
322
  return unit_cell_matrices_;
7,798✔
323
}
324

325
const std::map<uint64_t, UnitCellMatrices>&
326
LBSProblem::GetUnitGhostCellMatrices() const
17✔
327
{
328
  return unit_ghost_cell_matrices_;
17✔
329
}
330

331
std::vector<CellLBSView>&
332
LBSProblem::GetCellTransportViews()
116,534✔
333
{
334
  return cell_transport_views_;
116,534✔
335
}
336

337
const std::vector<CellLBSView>&
338
LBSProblem::GetCellTransportViews() const
166,464✔
339
{
340
  return cell_transport_views_;
166,464✔
341
}
342

343
const UnknownManager&
344
LBSProblem::GetUnknownManager() const
26,927✔
345
{
346
  return flux_moments_uk_man_;
26,927✔
347
}
348

349
size_t
350
LBSProblem::GetLocalNodeCount() const
3,088✔
351
{
352
  return local_node_count_;
3,088✔
353
}
354

355
size_t
356
LBSProblem::GetGlobalNodeCount() const
2,256✔
357
{
358
  return global_node_count_;
2,256✔
359
}
360

361
std::vector<double>&
362
LBSProblem::GetQMomentsLocal()
58,821✔
363
{
364
  return q_moments_local_;
58,821✔
365
}
366

367
const std::vector<double>&
368
LBSProblem::GetQMomentsLocal() const
×
369
{
370
  return q_moments_local_;
×
371
}
372

373
std::vector<double>&
374
LBSProblem::GetExtSrcMomentsLocal()
4✔
375
{
376
  return ext_src_moments_local_;
4✔
377
}
378

379
const std::vector<double>&
380
LBSProblem::GetExtSrcMomentsLocal() const
55,488✔
381
{
382
  return ext_src_moments_local_;
55,488✔
383
}
384

385
std::vector<double>&
386
LBSProblem::GetPhiOldLocal()
94,119✔
387
{
388
  return phi_old_local_;
94,119✔
389
}
390

391
const std::vector<double>&
392
LBSProblem::GetPhiOldLocal() const
×
393
{
394
  return phi_old_local_;
×
395
}
396

397
std::vector<double>&
398
LBSProblem::GetPhiNewLocal()
79,754✔
399
{
400
  return phi_new_local_;
79,754✔
401
}
402

403
const std::vector<double>&
404
LBSProblem::GetPhiNewLocal() const
×
405
{
406
  return phi_new_local_;
×
407
}
408

409
std::vector<double>&
410
LBSProblem::GetPrecursorsNewLocal()
16✔
411
{
412
  return precursor_new_local_;
16✔
413
}
414

415
const std::vector<double>&
416
LBSProblem::GetPrecursorsNewLocal() const
×
417
{
418
  return precursor_new_local_;
×
419
}
420

421
std::vector<double>&
422
LBSProblem::GetDensitiesLocal()
669✔
423
{
424
  return densities_local_;
669✔
425
}
426

427
const std::vector<double>&
428
LBSProblem::GetDensitiesLocal() const
55,488✔
429
{
430
  return densities_local_;
55,488✔
431
}
432

433
SetSourceFunction
434
LBSProblem::GetActiveSetSourceFunction() const
4,170✔
435
{
436
  return active_set_source_function_;
4,170✔
437
}
438

439
std::shared_ptr<AGSLinearSolver>
440
LBSProblem::GetAGSSolver()
344✔
441
{
442
  return ags_solver_;
344✔
443
}
444

445
std::vector<std::shared_ptr<LinearSolver>>&
446
LBSProblem::GetWGSSolvers()
113✔
447
{
448
  return wgs_solvers_;
113✔
449
}
450

451
WGSContext&
452
LBSProblem::GetWGSContext(int groupset_id)
11,556✔
453
{
454
  auto& wgs_solver = wgs_solvers_[groupset_id];
11,556✔
455
  auto raw_context = wgs_solver->GetContext();
11,556✔
456
  auto wgs_context_ptr = std::dynamic_pointer_cast<WGSContext>(raw_context);
11,556✔
457
  OpenSnLogicalErrorIf(not wgs_context_ptr, "Failed to cast WGSContext");
11,556✔
458
  return *wgs_context_ptr;
11,556✔
459
}
23,112✔
460

461
std::map<uint64_t, BoundaryPreference>&
462
LBSProblem::GetBoundaryPreferences()
4✔
463
{
464
  return boundary_preferences_;
4✔
465
}
466

467
std::pair<size_t, size_t>
468
LBSProblem::GetNumPhiIterativeUnknowns()
×
469
{
470
  const auto& sdm = *discretization_;
×
471
  const size_t num_local_phi_dofs = sdm.GetNumLocalDOFs(flux_moments_uk_man_);
×
472
  const size_t num_global_phi_dofs = sdm.GetNumGlobalDOFs(flux_moments_uk_man_);
×
473

474
  return {num_local_phi_dofs, num_global_phi_dofs};
×
475
}
476

477
size_t
478
LBSProblem::MapPhiFieldFunction(size_t g, size_t m) const
27,636✔
479
{
480
  OpenSnLogicalErrorIf(phi_field_functions_local_map_.count({g, m}) == 0,
27,636✔
481
                       std::string("Failure to map phi field function g") + std::to_string(g) +
482
                         " m" + std::to_string(m));
483

484
  return phi_field_functions_local_map_.at({g, m});
27,636✔
485
}
486

487
std::shared_ptr<FieldFunctionGridBased>
488
LBSProblem::GetPowerFieldFunction() const
×
489
{
490
  OpenSnLogicalErrorIf(not options_.power_field_function_on,
×
491
                       "Called when options_.power_field_function_on == false");
492

493
  return field_functions_[power_gen_fieldfunc_local_handle_];
×
494
}
495

496
InputParameters
497
LBSProblem::GetOptionsBlock()
426✔
498
{
499
  InputParameters params;
426✔
500

501
  params.SetGeneralDescription("Set options from a large list of parameters");
852✔
502
  params.AddOptionalParameter("max_mpi_message_size",
852✔
503
                              32768,
504
                              "The maximum MPI message size used during sweep initialization.");
505
  params.AddOptionalParameter(
852✔
506
    "restart_writes_enabled", false, "Flag that controls writing of restart dumps");
507
  params.AddOptionalParameter("write_delayed_psi_to_restart",
852✔
508
                              true,
509
                              "Flag that controls writing of delayed angular fluxes to restarts.");
510
  params.AddOptionalParameter(
852✔
511
    "read_restart_path", "", "Full path for reading restart dumps including file stem.");
512
  params.AddOptionalParameter(
852✔
513
    "write_restart_path", "", "Full path for writing restart dumps including file stem.");
514
  params.AddOptionalParameter("write_restart_time_interval",
852✔
515
                              0,
516
                              "Time interval in seconds at which restart data is to be written.");
517
  params.AddOptionalParameter(
852✔
518
    "use_precursors", false, "Flag for using delayed neutron precursors.");
519
  params.AddOptionalParameter("use_source_moments",
852✔
520
                              false,
521
                              "Flag for ignoring fixed sources and selectively using source "
522
                              "moments obtained elsewhere.");
523
  params.AddOptionalParameter(
852✔
524
    "save_angular_flux", false, "Flag indicating whether angular fluxes are to be stored or not.");
525
  params.AddOptionalParameter(
852✔
526
    "adjoint", false, "Flag for toggling whether the solver is in adjoint mode.");
527
  params.AddOptionalParameter(
852✔
528
    "verbose_inner_iterations", true, "Flag to control verbosity of inner iterations.");
529
  params.AddOptionalParameter(
852✔
530
    "verbose_outer_iterations", true, "Flag to control verbosity of across-groupset iterations.");
531
  params.AddOptionalParameter(
852✔
532
    "max_ags_iterations", 100, "Maximum number of across-groupset iterations.");
533
  params.AddOptionalParameter("ags_tolerance", 1.0e-6, "Across-groupset iterations tolerance.");
852✔
534
  params.AddOptionalParameter("ags_convergence_check",
852✔
535
                              "l2",
536
                              "Type of convergence check for AGS iterations. Valid values are "
537
                              "`\"l2\"` and '\"pointwise\"'");
538
  params.AddOptionalParameter(
852✔
539
    "verbose_ags_iterations", true, "Flag to control verbosity of across-groupset iterations.");
540
  params.AddOptionalParameter("power_field_function_on",
852✔
541
                              false,
542
                              "Flag to control the creation of the power generation field "
543
                              "function. If set to `true` then a field function will be created "
544
                              "with the general name <solver_name>_power_generation`.");
545
  params.AddOptionalParameter("power_default_kappa",
852✔
546
                              3.20435e-11,
547
                              "Default `kappa` value (Energy released per fission) to use for "
548
                              "power generation when cross sections do not have `kappa` values. "
549
                              "Default: 3.20435e-11 Joule (corresponding to 200 MeV per fission).");
550
  params.AddOptionalParameter("power_normalization",
852✔
551
                              -1.0,
552
                              "Power normalization factor to use. Supply a negative or zero number "
553
                              "to turn this off.");
554
  params.AddOptionalParameter("field_function_prefix_option",
852✔
555
                              "prefix",
556
                              "Prefix option on field function names. Default: `\"prefix\"`. Can "
557
                              "be `\"prefix\"` or `\"solver_name\"`. By default this option uses "
558
                              "the value of the `field_function_prefix` parameter. If this "
559
                              "parameter is not set, flux field functions will be exported as "
560
                              "`phi_gXXX_mYYY` where `XXX` is the zero padded 3 digit group number "
561
                              "and `YYY` is the zero padded 3 digit moment.");
562
  params.AddOptionalParameter("field_function_prefix",
852✔
563
                              "",
564
                              "Prefix to use on all field functions. Default: `\"\"`. By default "
565
                              "this option is empty. Ff specified, flux moments will be exported "
566
                              "as `prefix_phi_gXXX_mYYY` where `XXX` is the zero padded 3 digit "
567
                              "group number and `YYY` is the zero padded 3 digit moment. The "
568
                              "underscore after \"prefix\" is added automatically.");
569
  params.ConstrainParameterRange("ags_convergence_check",
1,278✔
570
                                 AllowableRangeList::New({"l2", "pointwise"}));
426✔
571
  params.ConstrainParameterRange("field_function_prefix_option",
1,278✔
572
                                 AllowableRangeList::New({"prefix", "solver_name"}));
426✔
573

574
  return params;
426✔
575
}
×
576

577
InputParameters
578
LBSProblem::GetBoundaryOptionsBlock()
738✔
579
{
580
  InputParameters params;
738✔
581

582
  params.SetGeneralDescription("Set options for boundary conditions.");
1,476✔
583
  params.AddRequiredParameter<std::string>("name",
1,476✔
584
                                           "Boundary name that identifies the specific boundary");
585
  params.AddRequiredParameter<std::string>("type", "Boundary type specification.");
1,476✔
586
  params.AddOptionalParameterArray<double>("group_strength",
1,476✔
587
                                           {},
588
                                           "Required only if \"type\" is \"isotropic\". An array "
589
                                           "of isotropic strength per group");
590
  params.AddOptionalParameter(
1,476✔
591
    "function_name", "", "Text name of the function to be called for this boundary condition.");
592
  params.AddOptionalParameter<std::shared_ptr<AngularFluxFunction>>(
1,476✔
593
    "function",
594
    std::shared_ptr<AngularFluxFunction>{},
1,476✔
595
    "Angular flux function to be used for arbitrary boundary conditions. The function takes an "
596
    "energy group index and a direction index and returns the incoming angular flux value.");
597
  params.ConstrainParameterRange(
2,214✔
598
    "type", AllowableRangeList::New({"vacuum", "isotropic", "reflecting", "arbitrary"}));
738✔
599

600
  return params;
738✔
UNCOV
601
}
×
602

603
InputParameters
604
LBSProblem::GetXSMapEntryBlock()
617✔
605
{
606
  InputParameters params;
617✔
607
  params.SetGeneralDescription("Set the cross-section map for the solver.");
1,234✔
608
  params.AddRequiredParameterArray("block_ids", "Mesh block IDs");
1,234✔
609
  params.AddRequiredParameter<std::shared_ptr<MultiGroupXS>>("xs", "Cross-section object");
1,234✔
610
  return params;
617✔
UNCOV
611
}
×
612

613
void
614
LBSProblem::SetOptions(const InputParameters& input)
212✔
615
{
616
  auto params = LBSProblem::GetOptionsBlock();
212✔
617
  params.AssignParameters(input);
212✔
618

619
  // Handle order insensitive options
620
  for (size_t p = 0; p < params.GetNumParameters(); ++p)
4,664✔
621
  {
622
    const auto& spec = params.GetParam(p);
4,452✔
623

624
    if (spec.GetName() == "max_mpi_message_size")
4,452✔
625
      options_.max_mpi_message_size = spec.GetValue<int>();
212✔
626

627
    else if (spec.GetName() == "restart_writes_enabled")
4,240✔
628
      options_.restart_writes_enabled = spec.GetValue<bool>();
212✔
629

630
    else if (spec.GetName() == "write_delayed_psi_to_restart")
4,028✔
631
      options_.write_delayed_psi_to_restart = spec.GetValue<bool>();
212✔
632

633
    else if (spec.GetName() == "read_restart_path")
3,816✔
634
    {
635
      options_.read_restart_path = spec.GetValue<std::string>();
212✔
636
      if (not options_.read_restart_path.empty())
212✔
637
        options_.read_restart_path += std::to_string(opensn::mpi_comm.rank()) + ".restart.h5";
36✔
638
    }
639

640
    else if (spec.GetName() == "write_restart_path")
3,604✔
641
    {
642
      options_.write_restart_path = spec.GetValue<std::string>();
212✔
643
      if (not options_.write_restart_path.empty())
212✔
UNCOV
644
        options_.write_restart_path += std::to_string(opensn::mpi_comm.rank()) + ".restart.h5";
×
645
    }
646

647
    else if (spec.GetName() == "write_restart_time_interval")
3,392✔
648
      options_.write_restart_time_interval = std::chrono::seconds(spec.GetValue<int>());
212✔
649

650
    else if (spec.GetName() == "use_precursors")
3,180✔
651
      options_.use_precursors = spec.GetValue<bool>();
212✔
652

653
    else if (spec.GetName() == "use_source_moments")
2,968✔
654
      options_.use_src_moments = spec.GetValue<bool>();
212✔
655

656
    else if (spec.GetName() == "save_angular_flux")
2,756✔
657
      options_.save_angular_flux = spec.GetValue<bool>();
212✔
658

659
    else if (spec.GetName() == "verbose_inner_iterations")
2,544✔
660
      options_.verbose_inner_iterations = spec.GetValue<bool>();
212✔
661

662
    else if (spec.GetName() == "max_ags_iterations")
2,332✔
663
      options_.max_ags_iterations = spec.GetValue<int>();
212✔
664

665
    else if (spec.GetName() == "ags_tolerance")
2,120✔
666
      options_.ags_tolerance = spec.GetValue<double>();
212✔
667

668
    else if (spec.GetName() == "ags_convergence_check")
1,908✔
669
    {
670
      auto check = spec.GetValue<std::string>();
212✔
671
      if (check == "pointwise")
212✔
UNCOV
672
        options_.ags_pointwise_convergence = true;
×
673
    }
212✔
674

675
    else if (spec.GetName() == "verbose_ags_iterations")
1,696✔
676
      options_.verbose_ags_iterations = spec.GetValue<bool>();
212✔
677

678
    else if (spec.GetName() == "verbose_outer_iterations")
1,484✔
679
      options_.verbose_outer_iterations = spec.GetValue<bool>();
212✔
680

681
    else if (spec.GetName() == "power_field_function_on")
1,272✔
682
      options_.power_field_function_on = spec.GetValue<bool>();
212✔
683

684
    else if (spec.GetName() == "power_default_kappa")
1,060✔
685
      options_.power_default_kappa = spec.GetValue<double>();
212✔
686

687
    else if (spec.GetName() == "power_normalization")
848✔
688
      options_.power_normalization = spec.GetValue<double>();
212✔
689

690
    else if (spec.GetName() == "field_function_prefix_option")
636✔
691
    {
692
      options_.field_function_prefix_option = spec.GetValue<std::string>();
212✔
693
    }
694

695
    else if (spec.GetName() == "field_function_prefix")
424✔
696
      options_.field_function_prefix = spec.GetValue<std::string>();
212✔
697

698
    else if (spec.GetName() == "adjoint")
212✔
699
      options_.adjoint = spec.GetValue<bool>();
212✔
700

701
  } // for p
702

703
  if (options_.restart_writes_enabled)
212✔
704
  {
705
    // Create restart directory if necessary
706
    auto dir = options_.write_restart_path.parent_path();
×
707
    if (opensn::mpi_comm.rank() == 0)
×
708
    {
UNCOV
709
      if (not std::filesystem::exists(dir))
×
710
      {
711
        if (not std::filesystem::create_directories(dir))
×
712
          throw std::runtime_error(GetName() + ": Failed to create restart directory " +
×
UNCOV
713
                                   dir.string());
×
714
      }
715
      else if (not std::filesystem::is_directory(dir))
×
716
        throw std::runtime_error(GetName() + ": Restart path exists but is not a directory " +
×
UNCOV
717
                                 dir.string());
×
718
    }
UNCOV
719
    opensn::mpi_comm.barrier();
×
UNCOV
720
    UpdateRestartWriteTime();
×
UNCOV
721
  }
×
722
}
212✔
723

724
void
725
LBSProblem::SetBoundaryOptions(const InputParameters& params)
738✔
726
{
727
  const auto boundary_name = params.GetParamValue<std::string>("name");
738✔
728
  const auto bndry_type = params.GetParamValue<std::string>("type");
738✔
729

730
  auto grid = GetGrid();
738✔
731
  const auto bnd_map = grid->GetBoundaryIDMap();
738✔
732
  const auto bnd_name_map = grid->GetBoundaryNameMap();
738✔
733
  auto it = bnd_name_map.find(boundary_name);
738✔
734
  if (it == bnd_name_map.end())
738✔
UNCOV
735
    throw std::runtime_error(std::format("Could not find the specified boundary '{}' - please "
×
736
                                         "check that the 'name' parameter is spelled correctly.",
UNCOV
737
                                         boundary_name));
×
738
  const auto bid = it->second;
738✔
739
  const std::map<std::string, LBSBoundaryType> type_list = {
738✔
740
    {"vacuum", LBSBoundaryType::VACUUM},
738✔
741
    {"isotropic", LBSBoundaryType::ISOTROPIC},
738✔
742
    {"reflecting", LBSBoundaryType::REFLECTING},
738✔
743
    {"arbitrary", LBSBoundaryType::ARBITRARY}};
3,690✔
744

745
  const auto type = type_list.at(bndry_type);
738✔
746
  switch (type)
738✔
747
  {
748
    case LBSBoundaryType::VACUUM:
634✔
749
    case LBSBoundaryType::REFLECTING:
634✔
750
    {
634✔
751
      boundary_preferences_[bid] = {type, {}, "", nullptr};
634✔
752
      break;
634✔
753
    }
754
    case LBSBoundaryType::ISOTROPIC:
100✔
755
    {
100✔
756
      if (not params.Has("group_strength"))
100✔
UNCOV
757
        throw std::runtime_error("Boundary conditions with type=\"isotropic\" require parameter "
×
UNCOV
758
                                 "\"group_strength\"");
×
759
      params.RequireParameterBlockTypeIs("group_strength", ParameterBlockType::ARRAY);
100✔
760
      const auto group_strength = params.GetParamVectorValue<double>("group_strength");
100✔
761
      boundary_preferences_[bid] = {type, group_strength, "", nullptr};
100✔
762
      break;
100✔
763
    }
100✔
764
    case LBSBoundaryType::ARBITRARY:
4✔
765
    {
4✔
766
      if (not params.Has("group_strength"))
4✔
UNCOV
767
        throw std::runtime_error("Boundary conditions with type=\"arbitrary\" require parameter "
×
UNCOV
768
                                 "\"function\"");
×
769
      auto angular_flux_function = params.GetSharedPtrParam<AngularFluxFunction>("function", false);
4✔
770
      if (not angular_flux_function)
4✔
UNCOV
771
        throw std::runtime_error(
×
772
          "Boundary conditions with type=\"arbitrary\" require a non-null "
UNCOV
773
          "AngularFluxFunction object passed via the \"function\" parameter.");
×
774
      boundary_preferences_[bid] = {type, {}, "", angular_flux_function};
4✔
775
      break;
4✔
776
    }
4✔
777
  }
778
}
2,956✔
779

780
void
781
LBSProblem::Initialize()
360✔
782
{
783
  CALI_CXX_MARK_SCOPE("LBSProblem::Initialize");
360✔
784

785
  PrintSimHeader();
360✔
786
  mpi_comm.barrier();
360✔
787

788
  InitializeSpatialDiscretization();
360✔
789
  InitializeParrays();
360✔
790
  InitializeBoundaries();
360✔
791
  InitializeGPUExtras();
360✔
792
  SetAdjoint(options_.adjoint);
360✔
793

794
  // Initialize point sources
795
  for (auto& point_source : point_sources_)
369✔
796
    point_source->Initialize(*this);
9✔
797

798
  // Initialize volumetric sources
799
  for (auto& volumetric_source : volumetric_sources_)
743✔
800
    volumetric_source->Initialize(*this);
383✔
801
}
360✔
802

803
void
804
LBSProblem::PrintSimHeader()
×
805
{
806
  if (opensn::mpi_comm.rank() == 0)
×
807
  {
UNCOV
808
    std::stringstream outstr;
×
UNCOV
809
    outstr << "\n"
×
810
           << "Initializing " << GetName() << "\n\n"
×
UNCOV
811
           << "Scattering order    : " << scattering_order_ << "\n"
×
812
           << "Number of moments   : " << num_moments_ << "\n"
×
813
           << "Number of groups    : " << groups_.size() << "\n"
×
814
           << "Number of groupsets : " << groupsets_.size() << "\n\n";
×
815

816
    for (const auto& groupset : groupsets_)
×
817
    {
UNCOV
818
      outstr << "***** Groupset " << groupset.id << " *****\n"
×
UNCOV
819
             << "Groups:\n";
×
820
      const auto& groups = groupset.groups;
821
      constexpr int groups_per_line = 12;
822
      for (size_t i = 0; i < groups.size(); ++i)
×
823
      {
UNCOV
824
        outstr << std::setw(5) << groups[i].id << ' ';
×
UNCOV
825
        if ((i + 1) % groups_per_line == 0)
×
UNCOV
826
          outstr << '\n';
×
827
      }
UNCOV
828
      if (!groups.empty() && groups.size() % groups_per_line != 0)
×
UNCOV
829
        outstr << '\n';
×
830
    }
831

UNCOV
832
    log.Log() << outstr.str() << '\n';
×
UNCOV
833
  }
×
UNCOV
834
}
×
835

836
void
837
LBSProblem::InitializeSources(const InputParameters& params)
360✔
838
{
839
  if (params.Has("volumetric_sources"))
360✔
840
  {
841
    const auto& vol_srcs = params.GetParam("volumetric_sources");
360✔
842
    vol_srcs.RequireBlockTypeIs(ParameterBlockType::ARRAY);
360✔
843
    for (const auto& src : vol_srcs)
743✔
844
      volumetric_sources_.push_back(src.GetValue<std::shared_ptr<VolumetricSource>>());
766✔
845
  }
846

847
  if (params.Has("point_sources"))
360✔
848
  {
849
    const auto& pt_srcs = params.GetParam("point_sources");
360✔
850
    pt_srcs.RequireBlockTypeIs(ParameterBlockType::ARRAY);
360✔
851
    for (const auto& src : pt_srcs)
369✔
852
      point_sources_.push_back(src.GetValue<std::shared_ptr<PointSource>>());
18✔
853
  }
854
}
360✔
855

856
void
857
LBSProblem::InitializeGroupsets(const InputParameters& params)
360✔
858
{
859
  // Initialize groups
860
  if (num_groups_ == 0)
360✔
UNCOV
861
    throw std::invalid_argument(GetName() + ": Number of groups must be > 0");
×
862
  for (size_t g = 0; g < num_groups_; ++g)
21,553✔
863
    groups_.emplace_back(static_cast<int>(g));
21,193✔
864

865
  // Initialize groupsets
866
  const auto& groupsets_array = params.GetParam("groupsets");
360✔
867
  const size_t num_gs = groupsets_array.GetNumParameters();
360✔
868
  if (num_gs == 0)
360✔
869
    throw std::invalid_argument(GetName() + ": At least one groupset must be specified");
×
870
  for (size_t gs = 0; gs < num_gs; ++gs)
779✔
871
  {
872
    const auto& groupset_params = groupsets_array.GetParam(gs);
419✔
873
    InputParameters gs_input_params = LBSGroupset::GetInputParameters();
419✔
874
    gs_input_params.SetObjectType("LBSProblem:LBSGroupset");
419✔
875
    gs_input_params.AssignParameters(groupset_params);
419✔
876
    groupsets_.emplace_back(gs_input_params, gs, *this);
419✔
877
    if (groupsets_.back().groups.empty())
419✔
878
    {
UNCOV
879
      std::stringstream oss;
×
UNCOV
880
      oss << GetName() << ": No groups added to groupset " << groupsets_.back().id;
×
UNCOV
881
      throw std::runtime_error(oss.str());
×
UNCOV
882
    }
×
883
  }
419✔
884
}
360✔
885

886
void
887
LBSProblem::InitializeXSmapAndDensities(const InputParameters& params)
360✔
888
{
889
  // Build XS map
890
  const auto& xs_array = params.GetParam("xs_map");
360✔
891
  const size_t num_xs = xs_array.GetNumParameters();
360✔
892
  for (size_t i = 0; i < num_xs; ++i)
977✔
893
  {
894
    const auto& item_params = xs_array.GetParam(i);
617✔
895
    InputParameters xs_entry_pars = GetXSMapEntryBlock();
617✔
896
    xs_entry_pars.AssignParameters(item_params);
617✔
897

898
    const auto& block_ids_param = xs_entry_pars.GetParam("block_ids");
617✔
899
    block_ids_param.RequireBlockTypeIs(ParameterBlockType::ARRAY);
617✔
900
    const auto& block_ids = block_ids_param.GetVectorValue<unsigned int>();
617✔
901
    auto xs = xs_entry_pars.GetSharedPtrParam<MultiGroupXS>("xs");
617✔
902
    for (const auto& block_id : block_ids)
1,330✔
903
      block_id_to_xs_map_[block_id] = xs;
713✔
904
  }
617✔
905

906
  // Assign placeholder unit densities
907
  densities_local_.assign(grid_->local_cells.size(), 1.0);
360✔
908
}
360✔
909

910
void
911
LBSProblem::InitializeMaterials()
372✔
912
{
913
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeMaterials");
372✔
914

915
  log.Log0Verbose1() << "Initializing Materials";
744✔
916

917
  // Create set of material ids locally relevant
918
  int invalid_mat_cell_count = 0;
372✔
919
  std::set<unsigned int> unique_block_ids;
372✔
920
  for (auto& cell : grid_->local_cells)
394,952✔
921
  {
922
    unique_block_ids.insert(cell.block_id);
394,580✔
923
    if (cell.block_id == std::numeric_limits<unsigned int>::max() or
394,580✔
924
        (block_id_to_xs_map_.find(cell.block_id) == block_id_to_xs_map_.end()))
394,580✔
UNCOV
925
      ++invalid_mat_cell_count;
×
926
  }
927
  const auto& ghost_cell_ids = grid_->cells.GetGhostGlobalIDs();
372✔
928
  for (uint64_t cell_id : ghost_cell_ids)
71,746✔
929
  {
930
    const auto& cell = grid_->cells[cell_id];
71,374✔
931
    unique_block_ids.insert(cell.block_id);
71,374✔
932
    if (cell.block_id == std::numeric_limits<unsigned int>::max() or
71,374✔
933
        (block_id_to_xs_map_.find(cell.block_id) == block_id_to_xs_map_.end()))
71,374✔
UNCOV
934
      ++invalid_mat_cell_count;
×
935
  }
936
  OpenSnLogicalErrorIf(invalid_mat_cell_count > 0,
372✔
937
                       std::to_string(invalid_mat_cell_count) +
938
                         " cells encountered with an invalid material id.");
939

940
  // Get ready for processing
941
  for (const auto& [blk_id, mat] : block_id_to_xs_map_)
1,113✔
942
  {
943
    mat->SetAdjointMode(options_.adjoint);
741✔
944

945
    OpenSnLogicalErrorIf(mat->GetNumGroups() < groups_.size(),
741✔
946
                         "Cross-sections for block \"" + std::to_string(blk_id) +
947
                           "\" have fewer groups (" + std::to_string(mat->GetNumGroups()) +
948
                           ") than the simulation (" + std::to_string(groups_.size()) + "). " +
949
                           "Cross-sections must have at least as many groups as the simulation.");
950
  }
951

952
  // Initialize precursor properties
953
  num_precursors_ = 0;
372✔
954
  max_precursors_per_material_ = 0;
372✔
955
  for (const auto& mat_id_xs : block_id_to_xs_map_)
1,113✔
956
  {
957
    const auto& xs = mat_id_xs.second;
741✔
958
    num_precursors_ += xs->GetNumPrecursors();
741✔
959
    max_precursors_per_material_ = std::max(xs->GetNumPrecursors(), max_precursors_per_material_);
741✔
960
  }
961

962
  // if no precursors, turn off precursors
963
  if (num_precursors_ == 0)
372✔
964
    options_.use_precursors = false;
364✔
965

966
  // check compatibility when precursors are on
967
  if (options_.use_precursors)
372✔
968
  {
969
    for (const auto& [mat_id, xs] : block_id_to_xs_map_)
16✔
970
    {
971
      OpenSnLogicalErrorIf(xs->IsFissionable() and num_precursors_ == 0,
8✔
972
                           "Incompatible cross-section data encountered for material id " +
973
                             std::to_string(mat_id) + ". When delayed neutron data is present " +
974
                             "for one fissionable matrial, it must be present for all fissionable "
975
                             "materials.");
976
    }
977
  }
978

979
  // Update transport views if available
980
  if (grid_->local_cells.size() == cell_transport_views_.size())
372✔
981
    for (const auto& cell : grid_->local_cells)
10,812✔
982
    {
983
      const auto& xs_ptr = block_id_to_xs_map_[cell.block_id];
10,800✔
984
      auto& transport_view = cell_transport_views_[cell.local_id];
10,800✔
985
      transport_view.ReassignXS(*xs_ptr);
10,800✔
986
    }
987

988
  mpi_comm.barrier();
372✔
989
}
372✔
990

991
void
992
LBSProblem::InitializeSpatialDiscretization()
352✔
993
{
994
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeSpatialDiscretization");
352✔
995

996
  log.Log() << "Initializing spatial discretization.\n";
704✔
997
  discretization_ = PieceWiseLinearDiscontinuous::New(grid_);
352✔
998

999
  ComputeUnitIntegrals();
352✔
1000
}
352✔
1001

1002
void
1003
LBSProblem::ComputeUnitIntegrals()
360✔
1004
{
1005
  CALI_CXX_MARK_SCOPE("LBSProblem::ComputeUnitIntegrals");
360✔
1006

1007
  log.Log() << "Computing unit integrals.\n";
720✔
1008
  const auto& sdm = *discretization_;
360✔
1009

1010
  const size_t num_local_cells = grid_->local_cells.size();
360✔
1011
  unit_cell_matrices_.resize(num_local_cells);
360✔
1012

1013
  for (const auto& cell : grid_->local_cells)
384,140✔
1014
    unit_cell_matrices_[cell.local_id] =
383,780✔
1015
      ComputeUnitCellIntegrals(sdm, cell, grid_->GetCoordinateSystem());
383,780✔
1016

1017
  const auto ghost_ids = grid_->cells.GetGhostGlobalIDs();
360✔
1018
  for (auto ghost_id : ghost_ids)
70,978✔
1019
    unit_ghost_cell_matrices_[ghost_id] =
70,618✔
1020
      ComputeUnitCellIntegrals(sdm, grid_->cells[ghost_id], grid_->GetCoordinateSystem());
141,236✔
1021

1022
  // Assessing global unit cell matrix storage
1023
  std::array<size_t, 2> num_local_ucms = {unit_cell_matrices_.size(),
360✔
1024
                                          unit_ghost_cell_matrices_.size()};
360✔
1025
  std::array<size_t, 2> num_global_ucms = {0, 0};
360✔
1026

1027
  mpi_comm.all_reduce(num_local_ucms.data(), 2, num_global_ucms.data(), mpi::op::sum<size_t>());
360✔
1028

1029
  opensn::mpi_comm.barrier();
360✔
1030
  log.Log() << "Ghost cell unit cell-matrix ratio: "
360✔
1031
            << (double)num_global_ucms[1] * 100 / (double)num_global_ucms[0] << "%";
720✔
1032
  log.Log() << "Cell matrices computed.";
720✔
1033
}
360✔
1034

1035
void
1036
LBSProblem::InitializeParrays()
360✔
1037
{
1038
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeParrays");
360✔
1039

1040
  log.Log() << "Initializing parallel arrays."
720✔
1041
            << " G=" << num_groups_ << " M=" << num_moments_ << std::endl;
360✔
1042

1043
  // Initialize unknown
1044
  // structure
1045
  flux_moments_uk_man_.unknowns.clear();
360✔
1046
  for (size_t m = 0; m < num_moments_; ++m)
1,294✔
1047
  {
1048
    flux_moments_uk_man_.AddUnknown(UnknownType::VECTOR_N, groups_.size());
934✔
1049
    flux_moments_uk_man_.unknowns.back().name = "m" + std::to_string(m);
934✔
1050
  }
1051

1052
  // Compute local # of dof
1053
  local_node_count_ = discretization_->GetNumLocalNodes();
360✔
1054
  global_node_count_ = discretization_->GetNumGlobalNodes();
360✔
1055

1056
  // Compute num of unknowns
1057
  size_t num_grps = groups_.size();
360✔
1058
  size_t local_unknown_count = local_node_count_ * num_grps * num_moments_;
360✔
1059

1060
  log.LogAllVerbose1() << "LBS Number of phi unknowns: " << local_unknown_count;
720✔
1061

1062
  // Size local vectors
1063
  q_moments_local_.assign(local_unknown_count, 0.0);
360✔
1064
  phi_old_local_.assign(local_unknown_count, 0.0);
360✔
1065
  phi_new_local_.assign(local_unknown_count, 0.0);
360✔
1066

1067
  // Setup precursor vector
1068
  if (options_.use_precursors)
360✔
1069
  {
1070
    size_t num_precursor_dofs = grid_->local_cells.size() * max_precursors_per_material_;
8✔
1071
    precursor_new_local_.assign(num_precursor_dofs, 0.0);
8✔
1072
  }
1073

1074
  // Initialize transport views
1075
  // Transport views act as a data structure to store information
1076
  // related to the transport simulation. The most prominent function
1077
  // here is that it holds the means to know where a given cell's
1078
  // transport quantities are located in the unknown vectors (i.e. phi)
1079
  //
1080
  // Also, for a given cell, within a given sweep chunk,
1081
  // we need to solve a matrix which square size is the
1082
  // amount of nodes on the cell. max_cell_dof_count is
1083
  // initialized here.
1084
  //
1085
  size_t block_MG_counter = 0; // Counts the strides of moment and group
360✔
1086

1087
  const Vector3 ihat(1.0, 0.0, 0.0);
360✔
1088
  const Vector3 jhat(0.0, 1.0, 0.0);
360✔
1089
  const Vector3 khat(0.0, 0.0, 1.0);
360✔
1090

1091
  min_cell_dof_count_ = std::numeric_limits<unsigned int>::max();
360✔
1092
  max_cell_dof_count_ = 0;
360✔
1093
  cell_transport_views_.clear();
360✔
1094
  cell_transport_views_.reserve(grid_->local_cells.size());
360✔
1095
  for (auto& cell : grid_->local_cells)
384,140✔
1096
  {
1097
    size_t num_nodes = discretization_->GetCellNumNodes(cell);
383,780✔
1098

1099
    // compute cell volumes
1100
    double cell_volume = 0.0;
383,780✔
1101
    const auto& IntV_shapeI = unit_cell_matrices_[cell.local_id].intV_shapeI;
383,780✔
1102
    for (size_t i = 0; i < num_nodes; ++i)
3,032,868✔
1103
      cell_volume += IntV_shapeI(i);
2,649,088✔
1104

1105
    size_t cell_phi_address = block_MG_counter;
383,780✔
1106

1107
    const size_t num_faces = cell.faces.size();
383,780✔
1108
    std::vector<bool> face_local_flags(num_faces, true);
383,780✔
1109
    std::vector<int> face_locality(num_faces, opensn::mpi_comm.rank());
383,780✔
1110
    std::vector<const Cell*> neighbor_cell_ptrs(num_faces, nullptr);
383,780✔
1111
    bool cell_on_boundary = false;
383,780✔
1112
    int f = 0;
383,780✔
1113
    for (auto& face : cell.faces)
2,467,052✔
1114
    {
1115
      if (not face.has_neighbor)
2,083,272✔
1116
      {
1117
        cell_on_boundary = true;
77,698✔
1118
        face_local_flags[f] = false;
77,698✔
1119
        face_locality[f] = -1;
77,698✔
1120
      } // if bndry
1121
      else
1122
      {
1123
        const int neighbor_partition = face.GetNeighborPartitionID(grid_.get());
2,005,574✔
1124
        face_local_flags[f] = (neighbor_partition == opensn::mpi_comm.rank());
2,005,574✔
1125
        face_locality[f] = neighbor_partition;
2,005,574✔
1126
        neighbor_cell_ptrs[f] = &grid_->cells[face.neighbor_id];
2,005,574✔
1127
      }
1128

1129
      ++f;
2,083,272✔
1130
    } // for f
1131

1132
    max_cell_dof_count_ = std::max(max_cell_dof_count_, static_cast<unsigned int>(num_nodes));
383,780✔
1133
    min_cell_dof_count_ = std::min(min_cell_dof_count_, static_cast<unsigned int>(num_nodes));
383,780✔
1134
    cell_transport_views_.emplace_back(cell_phi_address,
767,560✔
1135
                                       num_nodes,
1136
                                       num_grps,
1137
                                       num_moments_,
383,780✔
1138
                                       num_faces,
1139
                                       *block_id_to_xs_map_[cell.block_id],
383,780✔
1140
                                       cell_volume,
1141
                                       face_local_flags,
1142
                                       face_locality,
1143
                                       neighbor_cell_ptrs,
1144
                                       cell_on_boundary);
1145
    block_MG_counter += num_nodes * num_grps * num_moments_;
383,780✔
1146
  } // for local cell
383,780✔
1147

1148
  // Populate grid nodal mappings
1149
  // This is used in the Flux Data Structures (FLUDS)
1150
  grid_nodal_mappings_.clear();
360✔
1151
  grid_nodal_mappings_.reserve(grid_->local_cells.size());
360✔
1152
  for (auto& cell : grid_->local_cells)
384,140✔
1153
  {
1154
    CellFaceNodalMapping cell_nodal_mapping;
383,780✔
1155
    cell_nodal_mapping.reserve(cell.faces.size());
383,780✔
1156

1157
    for (auto& face : cell.faces)
2,467,052✔
1158
    {
1159
      std::vector<short> face_node_mapping;
2,083,272✔
1160
      std::vector<short> cell_node_mapping;
2,083,272✔
1161
      int adj_face_idx = -1;
2,083,272✔
1162

1163
      if (face.has_neighbor)
2,083,272✔
1164
      {
1165
        grid_->FindAssociatedVertices(face, face_node_mapping);
2,005,574✔
1166
        grid_->FindAssociatedCellVertices(face, cell_node_mapping);
2,005,574✔
1167
        adj_face_idx = face.GetNeighborAdjacentFaceIndex(grid_.get());
2,005,574✔
1168
      }
1169

1170
      cell_nodal_mapping.emplace_back(adj_face_idx, face_node_mapping, cell_node_mapping);
2,083,272✔
1171
    } // for f
2,083,272✔
1172

1173
    grid_nodal_mappings_.push_back(cell_nodal_mapping);
383,780✔
1174
  } // for local cell
383,780✔
1175

1176
  // Get grid localized communicator set
1177
  grid_local_comm_set_ = grid_->MakeMPILocalCommunicatorSet();
360✔
1178

1179
  // Initialize Field Functions
1180
  InitializeFieldFunctions();
360✔
1181

1182
  opensn::mpi_comm.barrier();
360✔
1183
  log.Log() << "Done with parallel arrays." << std::endl;
720✔
1184
}
360✔
1185

1186
void
1187
LBSProblem::InitializeFieldFunctions()
360✔
1188
{
1189
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeFieldFunctions");
360✔
1190

1191
  if (not field_functions_.empty())
360✔
UNCOV
1192
    return;
×
1193

1194
  // Initialize Field Functions for flux moments
1195
  phi_field_functions_local_map_.clear();
360✔
1196

1197
  for (size_t g = 0; g < groups_.size(); ++g)
21,553✔
1198
  {
1199
    for (size_t m = 0; m < num_moments_; ++m)
97,704✔
1200
    {
1201
      std::string prefix;
76,511✔
1202
      if (options_.field_function_prefix_option == "prefix")
76,511✔
1203
      {
1204
        prefix = options_.field_function_prefix;
76,511✔
1205
        if (not prefix.empty())
76,511✔
1206
          prefix += "_";
1✔
1207
      }
1208
      if (options_.field_function_prefix_option == "solver_name")
76,511✔
UNCOV
1209
        prefix = GetName() + "_";
×
1210

1211
      std::ostringstream oss;
76,511✔
1212
      oss << prefix << "phi_g" << std::setw(3) << std::setfill('0') << static_cast<int>(g) << "_m"
76,511✔
1213
          << std::setw(2) << std::setfill('0') << static_cast<int>(m);
76,511✔
1214
      const std::string name = oss.str();
76,511✔
1215

1216
      auto group_ff = std::make_shared<FieldFunctionGridBased>(
76,511✔
1217
        name, discretization_, Unknown(UnknownType::SCALAR));
76,511✔
1218

1219
      field_function_stack.push_back(group_ff);
153,022✔
1220
      field_functions_.push_back(group_ff);
76,511✔
1221

1222
      phi_field_functions_local_map_[{g, m}] = field_functions_.size() - 1;
76,511✔
1223
    } // for m
76,511✔
1224
  } // for g
1225

1226
  // Initialize power generation field function
1227
  if (options_.power_field_function_on)
360✔
1228
  {
1229
    std::string prefix;
4✔
1230
    if (options_.field_function_prefix_option == "prefix")
4✔
1231
    {
1232
      prefix = options_.field_function_prefix;
4✔
1233
      if (not prefix.empty())
4✔
UNCOV
1234
        prefix += "_";
×
1235
    }
1236
    if (options_.field_function_prefix_option == "solver_name")
4✔
UNCOV
1237
      prefix = GetName() + "_";
×
1238

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

1242
    field_function_stack.push_back(power_ff);
8✔
1243
    field_functions_.push_back(power_ff);
4✔
1244

1245
    power_gen_fieldfunc_local_handle_ = field_functions_.size() - 1;
4✔
1246
  }
4✔
1247
}
360✔
1248

1249
void
1250
LBSProblem::InitializeSolverSchemes()
360✔
1251
{
1252
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeSolverSchemes");
360✔
1253

1254
  log.Log() << "Initializing WGS and AGS solvers";
720✔
1255

1256
  InitializeWGSSolvers();
360✔
1257

1258
  ags_solver_ = std::make_shared<AGSLinearSolver>(*this, wgs_solvers_);
360✔
1259
  if (groupsets_.size() == 1)
360✔
1260
  {
1261
    ags_solver_->SetMaxIterations(1);
305✔
1262
    ags_solver_->SetVerbosity(false);
305✔
1263
  }
1264
  else
1265
  {
1266
    ags_solver_->SetMaxIterations(options_.max_ags_iterations);
55✔
1267
    ags_solver_->SetVerbosity(options_.verbose_ags_iterations);
55✔
1268
  }
1269
  ags_solver_->SetTolerance(options_.ags_tolerance);
360✔
1270
}
360✔
1271

1272
#ifndef __OPENSN_USE_CUDA__
1273
void
1274
LBSProblem::InitializeGPUExtras()
360✔
1275
{
1276
}
360✔
1277

1278
void
1279
LBSProblem::ResetGPUCarriers()
352✔
1280
{
1281
}
352✔
1282

1283
void
UNCOV
1284
LBSProblem::CheckCapableDevices()
×
1285
{
UNCOV
1286
}
×
1287
#endif // __OPENSN_USE_CUDA__
1288

1289
std::vector<double>
1290
LBSProblem::MakeSourceMomentsFromPhi()
4✔
1291
{
1292
  CALI_CXX_MARK_SCOPE("LBSProblem::MakeSourceMomentsFromPhi");
4✔
1293

1294
  size_t num_local_dofs = discretization_->GetNumLocalDOFs(flux_moments_uk_man_);
4✔
1295

1296
  std::vector<double> source_moments(num_local_dofs, 0.0);
4✔
1297
  for (auto& groupset : groupsets_)
8✔
1298
  {
1299
    active_set_source_function_(groupset,
4✔
1300
                                source_moments,
1301
                                phi_new_local_,
4✔
1302
                                APPLY_AGS_SCATTER_SOURCES | APPLY_WGS_SCATTER_SOURCES |
1303
                                  APPLY_AGS_FISSION_SOURCES | APPLY_WGS_FISSION_SOURCES);
4✔
1304
  }
1305

1306
  return source_moments;
4✔
1307
}
4✔
1308

1309
void
1310
LBSProblem::UpdateFieldFunctions()
1,200✔
1311
{
1312
  CALI_CXX_MARK_SCOPE("LBSProblem::UpdateFieldFunctions");
1,200✔
1313

1314
  const auto& sdm = *discretization_;
1,200✔
1315
  const auto& phi_uk_man = flux_moments_uk_man_;
1,200✔
1316

1317
  // Update flux moments
1318
  for (const auto& [g_and_m, ff_index] : phi_field_functions_local_map_)
83,239✔
1319
  {
1320
    const size_t g = g_and_m.first;
82,039✔
1321
    const size_t m = g_and_m.second;
82,039✔
1322

1323
    std::vector<double> data_vector_local(local_node_count_, 0.0);
82,039✔
1324

1325
    for (const auto& cell : grid_->local_cells)
21,615,610✔
1326
    {
1327
      const auto& cell_mapping = sdm.GetCellMapping(cell);
21,533,571✔
1328
      const size_t num_nodes = cell_mapping.GetNumNodes();
21,533,571✔
1329

1330
      for (size_t i = 0; i < num_nodes; ++i)
150,504,935✔
1331
      {
1332
        const auto imapA = sdm.MapDOFLocal(cell, i, phi_uk_man, m, g);
128,971,364✔
1333
        const auto imapB = sdm.MapDOFLocal(cell, i);
128,971,364✔
1334

1335
        data_vector_local[imapB] = phi_new_local_[imapA];
128,971,364✔
1336
      } // for node
1337
    } // for cell
1338

1339
    auto& ff_ptr = field_functions_.at(ff_index);
82,039✔
1340
    ff_ptr->UpdateFieldVector(data_vector_local);
82,039✔
1341
  }
82,039✔
1342

1343
  // Update power generation and scalar flux
1344
  if (options_.power_field_function_on)
1,200✔
1345
  {
1346
    std::vector<double> data_vector_power_local(local_node_count_, 0.0);
4✔
1347

1348
    double local_total_power = 0.0;
4✔
1349
    for (const auto& cell : grid_->local_cells)
83,268✔
1350
    {
1351
      const auto& cell_mapping = sdm.GetCellMapping(cell);
83,264✔
1352
      const size_t num_nodes = cell_mapping.GetNumNodes();
83,264✔
1353

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

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

1358
      if (not xs->IsFissionable())
83,264✔
1359
        continue;
56,360✔
1360

1361
      for (size_t i = 0; i < num_nodes; ++i)
134,520✔
1362
      {
1363
        const auto imapA = sdm.MapDOFLocal(cell, i);
107,616✔
1364
        const auto imapB = sdm.MapDOFLocal(cell, i, phi_uk_man, 0, 0);
107,616✔
1365

1366
        double nodal_power = 0.0;
1367
        for (size_t g = 0; g < groups_.size(); ++g)
860,928✔
1368
        {
1369
          const double sigma_fg = xs->GetSigmaFission()[g];
753,312✔
1370
          // const double kappa_g = xs->Kappa()[g];
1371
          const double kappa_g = options_.power_default_kappa;
753,312✔
1372

1373
          nodal_power += kappa_g * sigma_fg * phi_new_local_[imapB + g];
753,312✔
1374
        } // for g
1375

1376
        data_vector_power_local[imapA] = nodal_power;
107,616✔
1377
        local_total_power += nodal_power * Vi(i);
107,616✔
1378
      } // for node
1379
    } // for cell
1380

1381
    double scale_factor = 1.0;
4✔
1382
    if (options_.power_normalization > 0.0)
4✔
1383
    {
1384
      double global_total_power = 0.0;
4✔
1385
      mpi_comm.all_reduce(local_total_power, global_total_power, mpi::op::sum<double>());
4✔
1386
      scale_factor = options_.power_normalization / global_total_power;
4✔
1387
      Scale(data_vector_power_local, scale_factor);
4✔
1388
    }
1389

1390
    const size_t ff_index = power_gen_fieldfunc_local_handle_;
4✔
1391

1392
    auto& ff_ptr = field_functions_.at(ff_index);
4✔
1393
    ff_ptr->UpdateFieldVector(data_vector_power_local);
4✔
1394

1395
    // scale scalar flux if neccessary
1396
    if (scale_factor != 1.0)
4✔
1397
    {
1398
      for (size_t g = 0; g < groups_.size(); ++g)
32✔
1399
      {
1400
        const size_t phi_ff_index = phi_field_functions_local_map_.at({g, size_t{0}});
28✔
1401
        auto& phi_ff_ptr = field_functions_.at(phi_ff_index);
28✔
1402
        const auto& phi_vec = phi_ff_ptr->GetLocalFieldVector();
28✔
1403
        std::vector<double> phi_scaled(phi_vec.begin(), phi_vec.end());
28✔
1404
        Scale(phi_scaled, scale_factor);
28✔
1405
        phi_ff_ptr->UpdateFieldVector(phi_scaled);
28✔
1406
      }
28✔
1407
    }
1408
  } // if power enabled
4✔
1409
}
1,200✔
1410

1411
void
1412
LBSProblem::SetPhiFromFieldFunctions(PhiSTLOption which_phi,
×
1413
                                     const std::vector<size_t>& m_indices,
1414
                                     const std::vector<size_t>& g_indices)
1415
{
1416
  CALI_CXX_MARK_SCOPE("LBSProblem::SetPhiFromFieldFunctions");
×
1417

1418
  std::vector<size_t> m_ids_to_copy = m_indices;
×
UNCOV
1419
  std::vector<size_t> g_ids_to_copy = g_indices;
×
1420
  if (m_indices.empty())
×
UNCOV
1421
    for (size_t m = 0; m < num_moments_; ++m)
×
1422
      m_ids_to_copy.push_back(m);
×
1423
  if (g_ids_to_copy.empty())
×
1424
    for (size_t g = 0; g < num_groups_; ++g)
×
UNCOV
1425
      g_ids_to_copy.push_back(g);
×
1426

UNCOV
1427
  const auto& sdm = *discretization_;
×
1428
  const auto& phi_uk_man = flux_moments_uk_man_;
×
1429

UNCOV
1430
  for (const size_t m : m_ids_to_copy)
×
1431
  {
UNCOV
1432
    for (const size_t g : g_ids_to_copy)
×
1433
    {
1434
      const size_t ff_index = phi_field_functions_local_map_.at({g, m});
×
UNCOV
1435
      const auto& ff_ptr = field_functions_.at(ff_index);
×
1436
      const auto& ff_data = ff_ptr->GetLocalFieldVector();
×
1437

1438
      for (const auto& cell : grid_->local_cells)
×
1439
      {
UNCOV
1440
        const auto& cell_mapping = sdm.GetCellMapping(cell);
×
UNCOV
1441
        const size_t num_nodes = cell_mapping.GetNumNodes();
×
1442

UNCOV
1443
        for (size_t i = 0; i < num_nodes; ++i)
×
1444
        {
UNCOV
1445
          const auto imapA = sdm.MapDOFLocal(cell, i);
×
UNCOV
1446
          const auto imapB = sdm.MapDOFLocal(cell, i, phi_uk_man, m, g);
×
1447

UNCOV
1448
          if (which_phi == PhiSTLOption::PHI_OLD)
×
UNCOV
1449
            phi_old_local_[imapB] = ff_data[imapA];
×
UNCOV
1450
          else if (which_phi == PhiSTLOption::PHI_NEW)
×
UNCOV
1451
            phi_new_local_[imapB] = ff_data[imapA];
×
1452
        } // for node
1453
      } // for cell
1454
    } // for g
1455
  } // for m
UNCOV
1456
}
×
1457

1458
LBSProblem::~LBSProblem()
352✔
1459
{
1460
  ResetGPUCarriers();
1461
}
1,760✔
1462

352✔
1463
void
1464
LBSProblem::SetAdjoint(bool adjoint)
372✔
1465
{
1466
  if (adjoint and time_dependent_)
372✔
UNCOV
1467
    throw std::invalid_argument(GetName() + ": Time-dependent adjoint problems are not supported.");
×
1468

1469
  if (adjoint != options_.adjoint)
372✔
1470
  {
1471
    options_.adjoint = adjoint;
12✔
1472

1473
    // If a discretization exists, the solver has already been initialized.
1474
    // Reinitialize the materials to obtain the appropriate xs and clear the
1475
    // sources to prepare for defining the adjoint problem
1476
    if (discretization_)
12✔
1477
    {
1478
      // The materials are reinitialized here to ensure that the proper cross sections
1479
      // are available to the solver. Because an adjoint solve requires volumetric or
1480
      // point sources, the material-based sources are not set within the initialize routine.
1481
      InitializeMaterials();
12✔
1482

1483
      // Forward and adjoint sources are fundamentally different, so any existing sources
1484
      // should be cleared and reset through options upon changing modes.
1485
      point_sources_.clear();
12✔
1486
      volumetric_sources_.clear();
12✔
1487
      boundary_preferences_.clear();
12✔
1488

1489
      // Set all solutions to zero.
1490
      phi_old_local_.assign(phi_old_local_.size(), 0.0);
12✔
1491
      phi_new_local_.assign(phi_new_local_.size(), 0.0);
12✔
1492
      ZeroSolutions();
12✔
1493
      precursor_new_local_.assign(precursor_new_local_.size(), 0.0);
12✔
1494
    }
1495
  }
1496
}
372✔
1497

1498
} // 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