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

Open-Sn / opensn / 25478472129

07 May 2026 04:23AM UTC coverage: 75.685% (+0.3%) from 75.416%
25478472129

push

github

web-flow
Merge pull request #1057 from wdhawkins/c5g7_fixes

Updating tests.json for c5g7 tests and new iterative output.

22081 of 29175 relevant lines covered (75.68%)

65079358.06 hits per line

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

85.21
/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/point_source/point_source.h"
6
#include "modules/linear_boltzmann_solvers/lbs_problem/groupset/lbs_groupset.h"
7
#include "framework/field_functions/field_function_grid_based.h"
8
#include "framework/materials/multi_group_xs/multi_group_xs.h"
9
#include "framework/mesh/mesh_continuum/mesh_continuum.h"
10
#include "framework/utils/hdf_utils.h"
11
#include "framework/object_factory.h"
12
#include "framework/logging/log.h"
13
#include "framework/runtime.h"
14
#include "framework/data_types/allowable_range.h"
15
#include "framework/utils/error.h"
16
#include "caliper/cali.h"
17
#include <algorithm>
18
#include <iomanip>
19
#include <fstream>
20
#include <cstring>
21
#include <cassert>
22
#include <memory>
23
#include <stdexcept>
24
#include <sys/stat.h>
25
#include <unordered_map>
26
#include <functional>
27
#include <utility>
28

29
namespace opensn
30
{
31

32
InputParameters
33
LBSProblem::GetInputParameters()
980✔
34
{
35
  InputParameters params = Problem::GetInputParameters();
980✔
36

37
  params.ChangeExistingParamToOptional("name", "LBSProblem");
1,960✔
38

39
  params.AddRequiredParameter<std::shared_ptr<MeshContinuum>>("mesh", "Mesh");
1,960✔
40

41
  params.AddRequiredParameter<unsigned int>("num_groups",
1,960✔
42
                                            "The total number of groups within the solver");
43

44
  params.AddRequiredParameterArray("groupsets",
1,960✔
45
                                   "An array of blocks each specifying the input parameters for a "
46
                                   "<TT>LBSGroupset</TT>.");
47
  params.LinkParameterToBlock("groupsets", "LBSGroupset");
1,960✔
48

49
  params.AddRequiredParameterArray("xs_map",
1,960✔
50
                                   "Cross-section map from block IDs to cross-section objects.");
51

52
  params.AddOptionalParameterArray<std::shared_ptr<VolumetricSource>>(
1,960✔
53
    "volumetric_sources", {}, "An array of handles to volumetric sources.");
54

55
  params.AddOptionalParameterArray<std::shared_ptr<PointSource>>(
1,960✔
56
    "point_sources", {}, "An array of point sources.");
57

58
  params.AddOptionalParameterBlock(
1,960✔
59
    "options", ParameterBlock(), "Block of options. See <TT>OptionsBlock</TT>.");
1,960✔
60
  params.LinkParameterToBlock("options", "OptionsBlock");
1,960✔
61

62
  params.AddOptionalParameter("use_gpus", false, "Offload the sweep computation to GPUs.");
1,960✔
63

64
  return params;
980✔
65
}
×
66

67
LBSProblem::LBSProblem(const InputParameters& params)
980✔
68
  : Problem(params),
69
    num_groups_(params.GetParamValue<unsigned int>("num_groups")),
980✔
70
    grid_(params.GetSharedPtrParam<MeshContinuum>("mesh")),
980✔
71
    use_gpus_(params.GetParamValue<bool>("use_gpus"))
2,940✔
72
{
73
  // Check system for GPU acceleration
74
  if (use_gpus_)
980✔
75
  {
76
#ifdef __OPENSN_WITH_GPU__
77
    CheckCapableDevices();
156✔
78
#else
79
    OpenSnInvalidArgument(
80
      GetName() + ": GPU support was requested, but OpenSn was built without CUDA enabled.");
81
#endif // __OPENSN_WITH_GPU__
82
  }
83

84
  // Initialize options
85
  if (params.IsParameterValid("options"))
980✔
86
  {
87
    auto options_params = LBSProblem::GetOptionsBlock();
766✔
88
    options_params.AssignParameters(params.GetParam("options"));
768✔
89
    ParseOptions(options_params);
764✔
90
  }
766✔
91

92
  // Set geometry type
93
  geometry_type_ = grid_->GetGeometryType();
978✔
94
  OpenSnInvalidArgumentIf(geometry_type_ == GeometryType::INVALID,
978✔
95
                          GetName() + ": Invalid geometry type.");
96

97
  InitializeGroupsets(params);
978✔
98
  InitializeSources(params);
978✔
99
  InitializeXSMap(params);
978✔
100
  InitializeMaterials();
978✔
101
}
998✔
102

103
const LBSOptions&
104
LBSProblem::GetOptions() const
1,342,880,823✔
105
{
106
  return options_;
1,342,880,823✔
107
}
108

109
double
110
LBSProblem::GetTime() const
221,392✔
111
{
112
  return time_;
221,392✔
113
}
114

115
void
116
LBSProblem::SetTime(double time)
6,660✔
117
{
118
  time_ = time;
6,660✔
119
}
6,660✔
120

121
void
122
LBSProblem::SetTimeStep(double dt)
2,064✔
123
{
124
  OpenSnInvalidArgumentIf(dt <= 0.0, GetName() + ": dt must be greater than zero.");
2,064✔
125
  dt_ = dt;
2,064✔
126
}
2,064✔
127

128
double
129
LBSProblem::GetTimeStep() const
2,147,483,647✔
130
{
131
  return dt_;
2,147,483,647✔
132
}
133

134
void
135
LBSProblem::SetTheta(double theta)
440✔
136
{
137
  OpenSnInvalidArgumentIf(theta <= 0.0 or theta > 1.0,
440✔
138
                          GetName() + ": theta must be in (0.0, 1.0].");
139
  theta_ = theta;
440✔
140
}
440✔
141

142
double
143
LBSProblem::GetTheta() const
2,147,483,647✔
144
{
145
  return theta_;
2,147,483,647✔
146
}
147

148
bool
149
LBSProblem::IsTimeDependent() const
×
150
{
151
  return false;
×
152
}
153

154
void
155
LBSProblem::SetTimeDependentMode()
×
156
{
157
  OpenSnLogicalError(GetName() + ": Time-dependent mode is not supported for this problem type.");
×
158
}
159

160
void
161
LBSProblem::SetSteadyStateMode()
×
162
{
163
  // Steady-state is the default for problem types without time-dependent support.
164
}
×
165

166
GeometryType
167
LBSProblem::GetGeometryType() const
4✔
168
{
169
  return geometry_type_;
4✔
170
}
171

172
unsigned int
173
LBSProblem::GetNumMoments() const
385,962✔
174
{
175
  return num_moments_;
385,962✔
176
}
177

178
unsigned int
179
LBSProblem::GetMaxCellDOFCount() const
1,703✔
180
{
181
  return max_cell_dof_count_;
1,703✔
182
}
183

184
unsigned int
185
LBSProblem::GetMinCellDOFCount() const
1,703✔
186
{
187
  return min_cell_dof_count_;
1,703✔
188
}
189

190
bool
191
LBSProblem::UseGPUs() const
2,415✔
192
{
193
  return use_gpus_;
2,415✔
194
}
195

196
unsigned int
197
LBSProblem::GetNumGroups() const
575,218✔
198
{
199
  return num_groups_;
575,218✔
200
}
201

202
unsigned int
203
LBSProblem::GetScatteringOrder() const
4✔
204
{
205
  return scattering_order_;
4✔
206
}
207

208
unsigned int
209
LBSProblem::GetNumPrecursors() const
×
210
{
211
  return num_precursors_;
×
212
}
213

214
unsigned int
215
LBSProblem::GetMaxPrecursorsPerMaterial() const
11,624✔
216
{
217
  return max_precursors_per_material_;
11,624✔
218
}
219

220
const std::vector<LBSGroupset>&
221
LBSProblem::GetGroupsets() const
28,643✔
222
{
223
  return groupsets_;
28,643✔
224
}
225

226
LBSGroupset&
227
LBSProblem::GetGroupset(size_t groupset_id)
8,746,488✔
228
{
229
  return groupsets_.at(groupset_id);
8,746,488✔
230
}
231

232
const LBSGroupset&
233
LBSProblem::GetGroupset(size_t groupset_id) const
×
234
{
235
  return groupsets_.at(groupset_id);
×
236
}
237

238
size_t
239
LBSProblem::GetNumGroupsets() const
36✔
240
{
241
  return groupsets_.size();
36✔
242
}
243

244
void
245
LBSProblem::AddPointSource(std::shared_ptr<PointSource> point_source)
×
246
{
247
  point_sources_.push_back(point_source);
×
248
  point_sources_.back()->Initialize(*this);
×
249
}
×
250

251
void
252
LBSProblem::ClearPointSources()
×
253
{
254
  point_sources_.clear();
×
255
}
×
256

257
const std::vector<std::shared_ptr<PointSource>>&
258
LBSProblem::GetPointSources() const
27,158✔
259
{
260
  return point_sources_;
27,158✔
261
}
262

263
void
264
LBSProblem::AddVolumetricSource(std::shared_ptr<VolumetricSource> volumetric_source)
24✔
265
{
266
  volumetric_sources_.push_back(volumetric_source);
24✔
267
  volumetric_sources_.back()->Initialize(*this);
24✔
268
}
24✔
269

270
void
271
LBSProblem::ClearVolumetricSources()
12✔
272
{
273
  volumetric_sources_.clear();
12✔
274
}
12✔
275

276
const std::vector<std::shared_ptr<VolumetricSource>>&
277
LBSProblem::GetVolumetricSources() const
27,158✔
278
{
279
  return volumetric_sources_;
27,158✔
280
}
281

282
const BlockID2XSMap&
283
LBSProblem::GetBlockID2XSMap() const
2,078,308✔
284
{
285
  return block_id_to_xs_map_;
2,078,308✔
286
}
287

288
void
289
LBSProblem::SetBlockID2XSMap(const BlockID2XSMap& xs_map)
184✔
290
{
291
  const BlockID2XSMap old_xs_map = block_id_to_xs_map_;
184✔
292
  const size_t old_max_precursors_per_material = max_precursors_per_material_;
184✔
293
  const auto old_precursor_state = precursor_new_local_;
184✔
294

295
  block_id_to_xs_map_ = xs_map;
184✔
296
  InitializeMaterials();
184✔
297

298
  if (options_.use_precursors)
184✔
299
  {
300
    const size_t num_cells = grid_->local_cells.size();
136✔
301
    const size_t new_max_precursors_per_material = max_precursors_per_material_;
136✔
302
    const size_t num_precursor_dofs = num_cells * new_max_precursors_per_material;
136✔
303

304
    std::vector<double> remapped_precursors(num_precursor_dofs, 0.0);
136✔
305
    if (old_precursor_state.size() == num_cells * old_max_precursors_per_material)
136✔
306
    {
307
      for (const auto& cell : grid_->local_cells)
13,792✔
308
      {
309
        unsigned int old_num_precursors = 0;
13,656✔
310
        if (const auto old_xs_it = old_xs_map.find(cell.block_id); old_xs_it != old_xs_map.end())
13,656✔
311
          old_num_precursors = old_xs_it->second->GetNumPrecursors();
13,656✔
312

313
        const unsigned int new_num_precursors =
13,656✔
314
          block_id_to_xs_map_.at(cell.block_id)->GetNumPrecursors();
13,656✔
315
        const unsigned int num_precursors_to_copy =
13,656✔
316
          std::min(old_num_precursors, new_num_precursors);
13,656✔
317

318
        const size_t old_base = cell.local_id * old_max_precursors_per_material;
13,656✔
319
        const size_t new_base = cell.local_id * new_max_precursors_per_material;
13,656✔
320
        for (unsigned int j = 0; j < num_precursors_to_copy; ++j)
15,660✔
321
          remapped_precursors[new_base + j] = old_precursor_state[old_base + j];
2,004✔
322
      }
323
    }
324

325
    precursor_new_local_ = std::move(remapped_precursors);
136✔
326
  }
136✔
327
  else
328
    precursor_new_local_.clear();
48✔
329

330
  ResetGPUCarriers();
184✔
331
  InitializeGPUExtras();
184✔
332
}
184✔
333

334
std::shared_ptr<MeshContinuum>
335
LBSProblem::GetGrid() const
513,349✔
336
{
337
  return grid_;
513,349✔
338
}
339

340
const SpatialDiscretization&
341
LBSProblem::GetSpatialDiscretization() const
163,583✔
342
{
343
  return *discretization_;
163,583✔
344
}
345

346
const std::vector<UnitCellMatrices>&
347
LBSProblem::GetUnitCellMatrices() const
2,065,379✔
348
{
349
  return unit_cell_matrices_;
2,065,379✔
350
}
351

352
const std::map<uint64_t, UnitCellMatrices>&
353
LBSProblem::GetUnitGhostCellMatrices() const
16✔
354
{
355
  return unit_ghost_cell_matrices_;
16✔
356
}
357

358
const std::vector<CellLBSView>&
359
LBSProblem::GetCellTransportViews() const
571,174✔
360
{
361
  return cell_transport_views_;
571,174✔
362
}
363

364
std::vector<CellOutflowView>&
365
LBSProblem::GetCellOutflowViews()
2,172✔
366
{
367
  return cell_outflow_views_;
2,172✔
368
}
369

370
const std::vector<CellOutflowView>&
371
LBSProblem::GetCellOutflowViews() const
×
372
{
373
  return cell_outflow_views_;
×
374
}
375

376
const UnknownManager&
377
LBSProblem::GetUnknownManager() const
49,596✔
378
{
379
  return flux_moments_uk_man_;
49,596✔
380
}
381

382
size_t
383
LBSProblem::GetLocalNodeCount() const
20,665✔
384
{
385
  return local_node_count_;
20,665✔
386
}
387

388
size_t
389
LBSProblem::GetGlobalNodeCount() const
2,778✔
390
{
391
  return global_node_count_;
2,778✔
392
}
393

394
std::vector<double>&
395
LBSProblem::GetQMomentsLocal()
117,179✔
396
{
397
  return q_moments_local_;
117,179✔
398
}
399

400
const std::vector<double>&
401
LBSProblem::GetQMomentsLocal() const
×
402
{
403
  return q_moments_local_;
×
404
}
405

406
const std::vector<double>&
407
LBSProblem::GetExtSrcMomentsLocal() const
109,808✔
408
{
409
  return ext_src_moments_local_;
109,808✔
410
}
411

412
void
413
LBSProblem::SetExtSrcMomentsFrom(const std::vector<double>& ext_src_moments)
4✔
414
{
415
  if (not phi_old_local_.empty())
4✔
416
    OpenSnLogicalErrorIf(ext_src_moments.size() != phi_old_local_.size(),
4✔
417
                         "SetExtSrcMomentsFrom size mismatch. Provided size=" +
418
                           std::to_string(ext_src_moments.size()) +
419
                           ", expected local DOFs=" + std::to_string(phi_old_local_.size()) + ".");
420

421
  if (ext_src_moments_local_.empty())
4✔
422
  {
423
    ext_src_moments_local_ = ext_src_moments;
4✔
424
    return;
4✔
425
  }
426

427
  assert(ext_src_moments.size() == ext_src_moments_local_.size() &&
×
428
         "SetExtSrcMomentsFrom size mismatch.");
429
  ext_src_moments_local_ = ext_src_moments;
×
430
}
431

432
std::vector<double>&
433
LBSProblem::GetPhiOldLocal()
240,127✔
434
{
435
  return phi_old_local_;
240,127✔
436
}
437

438
const std::vector<double>&
439
LBSProblem::GetPhiOldLocal() const
×
440
{
441
  return phi_old_local_;
×
442
}
443

444
std::vector<double>&
445
LBSProblem::GetPhiNewLocal()
166,874✔
446
{
447
  return phi_new_local_;
166,874✔
448
}
449

450
const std::vector<double>&
451
LBSProblem::GetPhiNewLocal() const
11,276✔
452
{
453
  return phi_new_local_;
11,276✔
454
}
455

456
std::vector<double>&
457
LBSProblem::GetPrecursorsNewLocal()
6,114✔
458
{
459
  return precursor_new_local_;
6,114✔
460
}
461

462
const std::vector<double>&
463
LBSProblem::GetPrecursorsNewLocal() const
×
464
{
465
  return precursor_new_local_;
×
466
}
467

468
SetSourceFunction
469
LBSProblem::GetActiveSetSourceFunction() const
5,994✔
470
{
471
  return active_set_source_function_;
5,994✔
472
}
473

474
void
475
LBSProblem::SetActiveSetSourceFunction(SetSourceFunction source_function)
112✔
476
{
477
  active_set_source_function_ = std::move(source_function);
112✔
478
}
112✔
479

480
std::pair<size_t, size_t>
481
LBSProblem::GetNumPhiIterativeUnknowns()
×
482
{
483
  const auto& sdm = *discretization_;
×
484
  const size_t num_local_phi_dofs = sdm.GetNumLocalDOFs(flux_moments_uk_man_);
×
485
  const size_t num_global_phi_dofs = sdm.GetNumGlobalDOFs(flux_moments_uk_man_);
×
486

487
  return {num_local_phi_dofs, num_global_phi_dofs};
×
488
}
489

490
InputParameters
491
LBSProblem::GetOptionsBlock()
1,530✔
492
{
493
  InputParameters params;
1,530✔
494

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

563
  return params;
1,530✔
564
}
×
565

566
InputParameters
567
LBSProblem::GetXSMapEntryBlock()
1,427✔
568
{
569
  InputParameters params;
1,427✔
570
  params.SetGeneralDescription("Set the cross-section map for the solver.");
2,854✔
571
  params.AddRequiredParameterArray("block_ids", "Mesh block IDs");
2,854✔
572
  params.AddRequiredParameter<std::shared_ptr<MultiGroupXS>>("xs", "Cross-section object");
2,854✔
573
  return params;
1,427✔
574
}
×
575

576
void
577
LBSProblem::ParseOptions(const InputParameters& input)
764✔
578
{
579
  auto params = LBSProblem::GetOptionsBlock();
764✔
580
  params.AssignParameters(input);
764✔
581
  const auto& params_at_assignment = input.GetParametersAtAssignment();
764✔
582
  const auto& specified_params = params_at_assignment.GetNumParameters() > 0
764✔
583
                                   ? params_at_assignment
764✔
584
                                   : static_cast<const ParameterBlock&>(input);
764✔
585

586
  using OptionSetter = std::function<void(const ParameterBlock&)>;
764✔
587
  const std::unordered_map<std::string, OptionSetter> option_setters = {
764✔
588
    {"max_mpi_message_size",
589
     [this](const ParameterBlock& spec) { options_.max_mpi_message_size = spec.GetValue<int>(); }},
×
590
    {"restart_writes_enabled",
591
     [this](const ParameterBlock& spec)
1,532✔
592
     { options_.restart.writes_enabled = spec.GetValue<bool>(); }},
4✔
593
    {"write_delayed_psi_to_restart",
594
     [this](const ParameterBlock& spec)
1,528✔
595
     { options_.restart.write_delayed_psi = spec.GetValue<bool>(); }},
×
596
    {"read_restart_path",
597
     [this](const ParameterBlock& spec)
1,544✔
598
     { options_.restart.read_path = BuildRestartPath(spec.GetValue<std::string>()); }},
32✔
599
    {"write_restart_path",
600
     [this](const ParameterBlock& spec)
1,532✔
601
     { options_.restart.write_path = BuildRestartPath(spec.GetValue<std::string>()); }},
8✔
602
    {"write_restart_time_interval",
603
     [this](const ParameterBlock& spec)
1,528✔
604
     { options_.restart.write_time_interval = std::chrono::seconds(spec.GetValue<int>()); }},
×
605
    {"use_precursors",
606
     [this](const ParameterBlock& spec) { options_.use_precursors = spec.GetValue<bool>(); }},
244✔
607
    {"use_source_moments",
608
     [this](const ParameterBlock& spec) { options_.use_src_moments = spec.GetValue<bool>(); }},
4✔
609
    {"save_angular_flux",
610
     [this](const ParameterBlock& spec) { options_.save_angular_flux = spec.GetValue<bool>(); }},
370✔
611
    {"verbose_inner_iterations",
612
     [this](const ParameterBlock& spec)
2,086✔
613
     { options_.verbose_inner_iterations = spec.GetValue<bool>(); }},
558✔
614
    {"max_ags_iterations",
615
     [this](const ParameterBlock& spec) { options_.max_ags_iterations = spec.GetValue<int>(); }},
292✔
616
    {"ags_tolerance",
617
     [this](const ParameterBlock& spec) { options_.ags_tolerance = spec.GetValue<double>(); }},
16✔
618
    {"ags_convergence_check",
619
     [this](const ParameterBlock& spec)
1,528✔
620
     { options_.ags_pointwise_convergence = (spec.GetValue<std::string>() == "pointwise"); }},
×
621
    {"verbose_outer_iterations",
622
     [this](const ParameterBlock& spec)
2,058✔
623
     { options_.verbose_outer_iterations = spec.GetValue<bool>(); }},
530✔
624
    {"power_default_kappa",
625
     [this](const ParameterBlock& spec)
1,541✔
626
     { options_.power_default_kappa = spec.GetValue<double>(); }},
13✔
627
    {"field_function_prefix_option",
628
     [this](const ParameterBlock& spec)
1,528✔
629
     { options_.field_function_prefix_option = spec.GetValue<std::string>(); }},
×
630
    {"field_function_prefix",
631
     [this](const ParameterBlock& spec)
1,528✔
632
     { options_.field_function_prefix = spec.GetValue<std::string>(); }},
×
633
    {"adjoint", [this](const ParameterBlock& spec) { options_.adjoint = spec.GetValue<bool>(); }},
16✔
634
  };
15,280✔
635

636
  for (const auto& spec : specified_params.GetParameters())
2,831✔
637
  {
638
    const auto setter_it = option_setters.find(spec.GetName());
4,134✔
639
    if (setter_it != option_setters.end())
2,067✔
640
      setter_it->second(spec);
2,067✔
641
  }
642

643
  OpenSnInvalidArgumentIf(options_.restart.write_time_interval > std::chrono::seconds(0) and
764✔
644
                            not options_.restart.writes_enabled,
645
                          GetName() + ": `write_restart_time_interval>0` requires "
646
                                      "`restart_writes_enabled=true`.");
647

648
  OpenSnInvalidArgumentIf(options_.restart.write_time_interval > std::chrono::seconds(0) and
764✔
649
                            options_.restart.write_time_interval < std::chrono::seconds(30),
650
                          GetName() + ": `write_restart_time_interval` must be 0 (disabled) "
651
                                      "or at least 30 seconds.");
652

653
  OpenSnInvalidArgumentIf(options_.restart.writes_enabled and options_.restart.write_path.empty(),
764✔
654
                          GetName() + ": `restart_writes_enabled=true` requires a non-empty "
655
                                      "`write_restart_path`.");
656

657
  OpenSnInvalidArgumentIf(not options_.field_function_prefix.empty() and
764✔
658
                            options_.field_function_prefix_option != "prefix",
659
                          GetName() + ": non-empty `field_function_prefix` requires "
660
                                      "`field_function_prefix_option=\"prefix\"`.");
661

662
  if (options_.restart.writes_enabled)
764✔
663
  {
664
    const auto dir = options_.restart.write_path.parent_path();
4✔
665

666
    // Create restart directory if necessary.
667
    // If dir is empty, write path resolves relative to the working directory.
668
    if ((not dir.empty()) and opensn::mpi_comm.rank() == 0)
8✔
669
    {
670
      if (not std::filesystem::exists(dir))
1✔
671
      {
672
        OpenSnLogicalErrorIf(not std::filesystem::create_directories(dir),
×
673
                             GetName() + ": Failed to create restart directory " + dir.string());
674
      }
675
      else
676
        OpenSnLogicalErrorIf(not std::filesystem::is_directory(dir),
1✔
677
                             GetName() + ": Restart path exists but is not a directory " +
678
                               dir.string());
679
    }
680
    opensn::mpi_comm.barrier();
4✔
681
    options_.restart.MarkWriteComplete();
4✔
682
  }
4✔
683
}
1,528✔
684

685
std::filesystem::path
686
LBSProblem::BuildRestartPath(const std::string& path_stem)
20✔
687
{
688
  if (path_stem.empty())
20✔
689
    return {};
×
690

691
  auto path = std::filesystem::path(path_stem);
20✔
692
  path += std::to_string(opensn::mpi_comm.rank()) + ".restart.h5";
60✔
693
  return path;
20✔
694
}
20✔
695

696
bool
697
LBSProblem::ReadProblemRestartData(hid_t /*file_id*/)
×
698
{
699
  return true;
×
700
}
701

702
bool
703
LBSProblem::WriteProblemRestartData(hid_t /*file_id*/) const
×
704
{
705
  return true;
×
706
}
707

708
void
709
LBSProblem::BuildRuntime()
970✔
710
{
711
  CALI_CXX_MARK_SCOPE("LBSProblem::BuildRuntime");
970✔
712

713
  PrintSimHeader();
970✔
714
  mpi_comm.barrier();
970✔
715

716
  InitializeRuntimeCore();
970✔
717
  ValidateRuntimeModeConfiguration();
970✔
718
  InitializeSources();
970✔
719
}
970✔
720

721
void
722
LBSProblem::InitializeRuntimeCore()
970✔
723
{
724
  InitializeSpatialDiscretization();
970✔
725
  InitializeParrays();
970✔
726
  InitializeBoundaries();
970✔
727
  InitializeGPUExtras();
970✔
728
}
970✔
729

730
void
731
LBSProblem::ValidateRuntimeModeConfiguration() const
970✔
732
{
733
  if (options_.adjoint)
970✔
734
    if (IsTimeDependent())
16✔
735
      OpenSnInvalidArgument(GetName() + ": Time-dependent adjoint problems are not supported.");
×
736
}
970✔
737

738
void
739
LBSProblem::InitializeSources()
970✔
740
{
741
  // Initialize point sources
742
  for (auto& point_source : point_sources_)
978✔
743
    point_source->Initialize(*this);
8✔
744

745
  // Initialize volumetric sources
746
  for (auto& volumetric_source : volumetric_sources_)
1,761✔
747
    volumetric_source->Initialize(*this);
791✔
748
}
970✔
749

750
void
751
LBSProblem::PrintSimHeader()
×
752
{
753
  if (opensn::mpi_comm.rank() == 0)
×
754
  {
755
    std::stringstream outstr;
×
756
    outstr << "\n"
×
757
           << "Initializing " << GetName() << "\n\n"
×
758
           << "Scattering order    : " << scattering_order_ << "\n"
×
759
           << "Number of moments   : " << num_moments_ << "\n"
×
760
           << "Number of groups    : " << num_groups_ << "\n"
×
761
           << "Number of groupsets : " << groupsets_.size() << "\n\n";
×
762

763
    for (const auto& groupset : groupsets_)
×
764
    {
765
      outstr << "***** Groupset " << groupset.id << " *****\n"
×
766
             << "Groups:\n";
×
767
      const auto n_gs_groups = groupset.GetNumGroups();
×
768
      constexpr int groups_per_line = 12;
769
      for (size_t i = 0; i < n_gs_groups; ++i)
×
770
      {
771
        outstr << std::setw(5) << groupset.first_group + i << ' ';
×
772
        if ((i + 1) % groups_per_line == 0)
×
773
          outstr << '\n';
×
774
      }
775
      if (n_gs_groups > 0 && n_gs_groups % groups_per_line != 0)
×
776
        outstr << '\n';
×
777
    }
778

779
    log.Log() << outstr.str() << '\n';
×
780
  }
×
781
}
×
782

783
void
784
LBSProblem::InitializeSources(const InputParameters& params)
978✔
785
{
786
  if (params.Has("volumetric_sources"))
978✔
787
  {
788
    const auto& vol_srcs = params.GetParam("volumetric_sources");
978✔
789
    vol_srcs.RequireBlockTypeIs(ParameterBlockType::ARRAY);
978✔
790
    for (const auto& src : vol_srcs)
1,769✔
791
      volumetric_sources_.push_back(src.GetValue<std::shared_ptr<VolumetricSource>>());
1,582✔
792
  }
793

794
  if (params.Has("point_sources"))
978✔
795
  {
796
    const auto& pt_srcs = params.GetParam("point_sources");
978✔
797
    pt_srcs.RequireBlockTypeIs(ParameterBlockType::ARRAY);
978✔
798
    for (const auto& src : pt_srcs)
986✔
799
      point_sources_.push_back(src.GetValue<std::shared_ptr<PointSource>>());
16✔
800
  }
801
}
978✔
802

803
void
804
LBSProblem::InitializeGroupsets(const InputParameters& params)
978✔
805
{
806
  // Initialize groups
807
  OpenSnInvalidArgumentIf(num_groups_ == 0, GetName() + ": Number of groups must be > 0.");
978✔
808

809
  // Initialize groupsets
810
  const auto& groupsets_array = params.GetParam("groupsets");
978✔
811
  const size_t num_gs = groupsets_array.GetNumParameters();
978✔
812
  OpenSnInvalidArgumentIf(num_gs == 0, GetName() + ": At least one groupset must be specified.");
978✔
813
  for (size_t gs = 0; gs < num_gs; ++gs)
2,077✔
814
  {
815
    const auto& groupset_params = groupsets_array.GetParam(gs);
1,099✔
816
    InputParameters gs_input_params = LBSGroupset::GetInputParameters();
1,099✔
817
    gs_input_params.SetObjectType("LBSProblem:LBSGroupset");
1,099✔
818
    gs_input_params.AssignParameters(groupset_params);
1,099✔
819
    groupsets_.emplace_back(gs_input_params, gs, *this);
1,099✔
820
    if (groupsets_.back().GetNumGroups() == 0)
1,099✔
821
    {
822
      std::stringstream oss;
×
823
      oss << GetName() << ": No groups added to groupset " << groupsets_.back().id;
×
824
      OpenSnInvalidArgument(oss.str());
×
825
    }
×
826
  }
1,099✔
827
}
978✔
828

829
void
830
LBSProblem::InitializeXSMap(const InputParameters& params)
978✔
831
{
832
  // Build XS map
833
  const auto& xs_array = params.GetParam("xs_map");
978✔
834
  const size_t num_xs = xs_array.GetNumParameters();
978✔
835
  for (size_t i = 0; i < num_xs; ++i)
2,221✔
836
  {
837
    const auto& item_params = xs_array.GetParam(i);
1,243✔
838
    InputParameters xs_entry_pars = GetXSMapEntryBlock();
1,243✔
839
    xs_entry_pars.AssignParameters(item_params);
1,243✔
840

841
    const auto& block_ids_param = xs_entry_pars.GetParam("block_ids");
1,243✔
842
    block_ids_param.RequireBlockTypeIs(ParameterBlockType::ARRAY);
1,243✔
843
    const auto& block_ids = block_ids_param.GetVectorValue<unsigned int>();
1,243✔
844
    auto xs = xs_entry_pars.GetSharedPtrParam<MultiGroupXS>("xs");
1,243✔
845
    for (const auto& block_id : block_ids)
2,630✔
846
      block_id_to_xs_map_[block_id] = xs;
1,387✔
847
  }
1,243✔
848
}
978✔
849

850
void
851
LBSProblem::InitializeMaterials()
1,186✔
852
{
853
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeMaterials");
1,186✔
854

855
  log.Log0Verbose1() << "Initializing Materials";
2,372✔
856

857
  // Create set of material ids locally relevant
858
  int invalid_mat_cell_count = 0;
1,186✔
859
  std::set<unsigned int> unique_block_ids;
1,186✔
860
  for (auto& cell : grid_->local_cells)
683,293✔
861
  {
862
    unique_block_ids.insert(cell.block_id);
682,107✔
863
    if (cell.block_id == std::numeric_limits<unsigned int>::max() or
682,107✔
864
        (block_id_to_xs_map_.find(cell.block_id) == block_id_to_xs_map_.end()))
682,107✔
865
      ++invalid_mat_cell_count;
×
866
  }
867
  const auto& ghost_cell_ids = grid_->cells.GetGhostGlobalIDs();
1,186✔
868
  for (uint64_t cell_id : ghost_cell_ids)
112,847✔
869
  {
870
    const auto& cell = grid_->cells[cell_id];
111,661✔
871
    unique_block_ids.insert(cell.block_id);
111,661✔
872
    if (cell.block_id == std::numeric_limits<unsigned int>::max() or
111,661✔
873
        (block_id_to_xs_map_.find(cell.block_id) == block_id_to_xs_map_.end()))
111,661✔
874
      ++invalid_mat_cell_count;
×
875
  }
876
  OpenSnLogicalErrorIf(invalid_mat_cell_count > 0,
1,186✔
877
                       std::to_string(invalid_mat_cell_count) +
878
                         " cells encountered with an invalid material id.");
879

880
  // Get ready for processing
881
  for (const auto& [blk_id, mat] : block_id_to_xs_map_)
2,805✔
882
  {
883
    mat->SetAdjointMode(options_.adjoint);
1,619✔
884

885
    OpenSnLogicalErrorIf(mat->GetNumGroups() < num_groups_,
1,619✔
886
                         "Cross-sections for block \"" + std::to_string(blk_id) +
887
                           "\" have fewer groups (" + std::to_string(mat->GetNumGroups()) +
888
                           ") than the simulation (" + std::to_string(num_groups_) + "). " +
889
                           "Cross-sections must have at least as many groups as the simulation.");
890
  }
891

892
  // Initialize precursor properties
893
  num_precursors_ = 0;
1,186✔
894
  max_precursors_per_material_ = 0;
1,186✔
895
  for (const auto& mat_id_xs : block_id_to_xs_map_)
2,805✔
896
  {
897
    const auto& xs = mat_id_xs.second;
1,619✔
898
    num_precursors_ += xs->GetNumPrecursors();
1,619✔
899
    max_precursors_per_material_ = std::max(xs->GetNumPrecursors(), max_precursors_per_material_);
1,619✔
900
  }
901

902
  const bool has_fissionable_precursors =
1,186✔
903
    std::any_of(block_id_to_xs_map_.begin(),
1,186✔
904
                block_id_to_xs_map_.end(),
905
                [](const auto& mat_id_xs)
1,619✔
906
                {
907
                  const auto& xs = mat_id_xs.second;
1,619✔
908
                  return xs->IsFissionable() and xs->GetNumPrecursors() > 0;
1,619✔
909
                });
910
  const bool has_fissionable_material =
1,186✔
911
    std::any_of(block_id_to_xs_map_.begin(),
1,186✔
912
                block_id_to_xs_map_.end(),
913
                [](const auto& mat_id_xs) { return mat_id_xs.second->IsFissionable(); });
1,575✔
914

915
  const bool has_any_precursor_data =
1,186✔
916
    std::any_of(block_id_to_xs_map_.begin(),
1,186✔
917
                block_id_to_xs_map_.end(),
918
                [](const auto& mat_id_xs) { return mat_id_xs.second->GetNumPrecursors() > 0; });
1,619✔
919

920
  if (options_.use_precursors and has_fissionable_material and not has_any_precursor_data)
1,186✔
921
  {
922
    log.Log0Warning() << GetName()
255✔
923
                      << ": options.use_precursors is enabled, but no precursor data was found "
924
                         "in the active cross-section map. Running without delayed-neutron "
925
                         "precursor coupling.";
85✔
926
  }
927

928
  // check compatibility when at least one fissionable material has delayed-neutron data
929
  if (options_.use_precursors and has_fissionable_precursors)
1,186✔
930
  {
931
    for (const auto& [mat_id, xs] : block_id_to_xs_map_)
378✔
932
    {
933
      OpenSnInvalidArgumentIf(xs->IsFissionable() and xs->GetNumPrecursors() == 0,
189✔
934
                              GetName() + ": incompatible cross-section data for material id " +
935
                                std::to_string(mat_id) +
936
                                ". When options.use_precursors=true and "
937
                                "delayed-neutron precursor data is present for one fissionable "
938
                                "material, it must be present for all fissionable materials.");
939
    }
940
  }
941

942
  // Update transport views if available
943
  if (grid_->local_cells.size() == cell_transport_views_.size())
1,186✔
944
    for (const auto& cell : grid_->local_cells)
28,108✔
945
    {
946
      const auto& xs_ptr = block_id_to_xs_map_[cell.block_id];
27,900✔
947
      auto& transport_view = cell_transport_views_[cell.local_id];
27,900✔
948
      transport_view.ReassignXS(*xs_ptr);
27,900✔
949
    }
950

951
  mpi_comm.barrier();
1,186✔
952
}
1,186✔
953

954
void
955
LBSProblem::InitializeSpatialDiscretization()
890✔
956
{
957
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeSpatialDiscretization");
890✔
958

959
  OpenSnLogicalErrorIf(not discretization_,
890✔
960
                       GetName() + ": Missing spatial discretization. Construct the problem "
961
                                   "through its factory Create(...) entry point.");
962
  log.Log() << "Initializing spatial discretization metadata.\n";
1,780✔
963

964
  ComputeUnitIntegrals();
890✔
965
}
890✔
966

967
void
968
LBSProblem::ComputeUnitIntegrals()
970✔
969
{
970
  CALI_CXX_MARK_SCOPE("LBSProblem::ComputeUnitIntegrals");
970✔
971

972
  log.Log() << "Computing unit integrals.\n";
1,940✔
973
  const auto& sdm = *discretization_;
970✔
974

975
  const size_t num_local_cells = grid_->local_cells.size();
970✔
976
  unit_cell_matrices_.resize(num_local_cells);
970✔
977

978
  for (const auto& cell : grid_->local_cells)
655,157✔
979
    unit_cell_matrices_[cell.local_id] =
654,187✔
980
      ComputeUnitCellIntegrals(sdm, cell, grid_->GetCoordinateSystem());
654,187✔
981

982
  const auto ghost_ids = grid_->cells.GetGhostGlobalIDs();
970✔
983
  for (auto ghost_id : ghost_ids)
103,759✔
984
    unit_ghost_cell_matrices_[ghost_id] =
102,789✔
985
      ComputeUnitCellIntegrals(sdm, grid_->cells[ghost_id], grid_->GetCoordinateSystem());
205,578✔
986

987
  // Assessing global unit cell matrix storage
988
  std::array<size_t, 2> num_local_ucms = {unit_cell_matrices_.size(),
970✔
989
                                          unit_ghost_cell_matrices_.size()};
970✔
990
  std::array<size_t, 2> num_global_ucms = {0, 0};
970✔
991

992
  mpi_comm.all_reduce(num_local_ucms.data(), 2, num_global_ucms.data(), mpi::op::sum<size_t>());
970✔
993

994
  opensn::mpi_comm.barrier();
970✔
995
  log.Log() << "Ghost cell unit cell-matrix ratio: "
970✔
996
            << (double)num_global_ucms[1] * 100 / (double)num_global_ucms[0] << "%";
1,940✔
997
  log.Log() << "Cell matrices computed.";
1,940✔
998
}
970✔
999

1000
void
1001
LBSProblem::InitializeParrays()
970✔
1002
{
1003
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeParrays");
970✔
1004

1005
  log.Log() << "Initializing parallel arrays."
1,940✔
1006
            << " G=" << num_groups_ << " M=" << num_moments_ << std::endl;
970✔
1007

1008
  // Initialize unknown
1009
  // structure
1010
  flux_moments_uk_man_.unknowns.clear();
970✔
1011
  for (unsigned int m = 0; m < num_moments_; ++m)
2,873✔
1012
  {
1013
    flux_moments_uk_man_.AddUnknown(UnknownType::VECTOR_N, num_groups_);
1,903✔
1014
    flux_moments_uk_man_.unknowns.back().name = "m" + std::to_string(m);
1,903✔
1015
  }
1016

1017
  // Compute local # of dof
1018
  local_node_count_ = discretization_->GetNumLocalNodes();
970✔
1019
  global_node_count_ = discretization_->GetNumGlobalNodes();
970✔
1020

1021
  // Compute num of unknowns
1022
  size_t local_unknown_count = local_node_count_ * num_groups_ * num_moments_;
970✔
1023
  log.LogAllVerbose1() << "LBS Number of phi unknowns: " << local_unknown_count;
1,940✔
1024

1025
  // Size local vectors
1026
  q_moments_local_.assign(local_unknown_count, 0.0);
970✔
1027
  phi_old_local_.assign(local_unknown_count, 0.0);
970✔
1028
  phi_new_local_.assign(local_unknown_count, 0.0);
970✔
1029

1030
  // Setup precursor vector
1031
  if (options_.use_precursors)
970✔
1032
  {
1033
    size_t num_precursor_dofs = grid_->local_cells.size() * max_precursors_per_material_;
802✔
1034
    precursor_new_local_.assign(num_precursor_dofs, 0.0);
802✔
1035
  }
1036

1037
  // Initialize cell transport metadata and outflow tallies.
1038
  size_t block_MG_counter = 0; // Counts the strides of moment and group
970✔
1039
  min_cell_dof_count_ = std::numeric_limits<unsigned int>::max();
970✔
1040
  max_cell_dof_count_ = 0;
970✔
1041
  cell_transport_views_.clear();
970✔
1042
  cell_transport_views_.reserve(grid_->local_cells.size());
970✔
1043
  cell_outflow_views_.clear();
970✔
1044
  cell_outflow_views_.reserve(grid_->local_cells.size());
970✔
1045
  for (auto& cell : grid_->local_cells)
655,157✔
1046
  {
1047
    size_t num_nodes = discretization_->GetCellNumNodes(cell);
654,187✔
1048

1049
    // compute cell volumes
1050
    double cell_volume = 0.0;
654,187✔
1051
    const auto& IntV_shapeI = unit_cell_matrices_[cell.local_id].intV_shapeI;
654,187✔
1052
    for (size_t i = 0; i < num_nodes; ++i)
4,602,543✔
1053
      cell_volume += IntV_shapeI(i);
3,948,356✔
1054

1055
    size_t cell_phi_address = block_MG_counter;
654,187✔
1056

1057
    const size_t num_faces = cell.faces.size();
654,187✔
1058
    std::vector<bool> face_local_flags(num_faces, true);
654,187✔
1059
    std::vector<int> face_locality(num_faces, opensn::mpi_comm.rank());
654,187✔
1060
    std::vector<const Cell*> neighbor_cell_ptrs(num_faces, nullptr);
654,187✔
1061
    bool cell_on_boundary = false;
654,187✔
1062
    int f = 0;
654,187✔
1063
    for (auto& face : cell.faces)
3,920,291✔
1064
    {
1065
      if (not face.has_neighbor)
3,266,104✔
1066
      {
1067
        cell_on_boundary = true;
118,250✔
1068
        face_local_flags[f] = false;
118,250✔
1069
        face_locality[f] = -1;
118,250✔
1070
      } // if bndry
1071
      else
1072
      {
1073
        const int neighbor_partition = face.GetNeighborPartitionID(grid_.get());
3,147,854✔
1074
        face_local_flags[f] = (neighbor_partition == opensn::mpi_comm.rank());
3,147,854✔
1075
        face_locality[f] = neighbor_partition;
3,147,854✔
1076
        neighbor_cell_ptrs[f] = &grid_->cells[face.neighbor_id];
3,147,854✔
1077
      }
1078

1079
      ++f;
3,266,104✔
1080
    }
1081

1082
    max_cell_dof_count_ = std::max(max_cell_dof_count_, static_cast<unsigned int>(num_nodes));
654,187✔
1083
    min_cell_dof_count_ = std::min(min_cell_dof_count_, static_cast<unsigned int>(num_nodes));
654,187✔
1084
    cell_transport_views_.emplace_back(cell_phi_address,
1,308,374✔
1085
                                       num_nodes,
1086
                                       num_groups_,
654,187✔
1087
                                       num_moments_,
654,187✔
1088
                                       *block_id_to_xs_map_[cell.block_id],
654,187✔
1089
                                       cell_volume,
1090
                                       face_local_flags,
1091
                                       face_locality,
1092
                                       neighbor_cell_ptrs);
1093
    cell_outflow_views_.emplace_back(num_faces, num_groups_, face_locality, cell_on_boundary);
654,187✔
1094
    block_MG_counter += num_nodes * num_groups_ * num_moments_;
654,187✔
1095
  } // for local cell
654,187✔
1096

1097
  // Populate grid nodal mappings
1098
  // This is used in the Flux Data Structure (FLUDS).
1099
  grid_nodal_mappings_.clear();
970✔
1100
  grid_nodal_mappings_.reserve(grid_->local_cells.size());
970✔
1101
  for (auto& cell : grid_->local_cells)
655,157✔
1102
  {
1103
    CellFaceNodalMapping cell_nodal_mapping;
654,187✔
1104
    cell_nodal_mapping.reserve(cell.faces.size());
654,187✔
1105

1106
    for (auto& face : cell.faces)
3,920,291✔
1107
    {
1108
      std::vector<short> face_node_mapping;
3,266,104✔
1109
      std::vector<short> cell_node_mapping;
3,266,104✔
1110
      int adj_face_idx = -1;
3,266,104✔
1111

1112
      if (face.has_neighbor)
3,266,104✔
1113
      {
1114
        grid_->FindAssociatedVertices(face, face_node_mapping);
3,147,854✔
1115
        grid_->FindAssociatedCellVertices(face, cell_node_mapping);
3,147,854✔
1116
        adj_face_idx = face.GetNeighborAdjacentFaceIndex(grid_.get());
3,147,854✔
1117
      }
1118

1119
      cell_nodal_mapping.emplace_back(adj_face_idx, face_node_mapping, cell_node_mapping);
3,266,104✔
1120
    }
3,266,104✔
1121

1122
    grid_nodal_mappings_.push_back(cell_nodal_mapping);
654,187✔
1123
  }
654,187✔
1124

1125
  // Get grid localized communicator set
1126
  grid_local_comm_set_ = grid_->MakeMPILocalCommunicatorSet();
970✔
1127

1128
  opensn::mpi_comm.barrier();
970✔
1129
  log.Log() << "Done with parallel arrays." << std::endl;
1,940✔
1130
}
970✔
1131

1132
#ifndef __OPENSN_WITH_GPU__
1133
void
1134
LBSProblem::InitializeGPUExtras()
1135
{
1136
}
1137

1138
void
1139
LBSProblem::ResetGPUCarriers()
1140
{
1141
}
1142

1143
void
1144
LBSProblem::CheckCapableDevices()
1145
{
1146
}
1147
#endif // __OPENSN_WITH_GPU__
1148

1149
std::vector<double>
1150
LBSProblem::MakeSourceMomentsFromPhi()
4✔
1151
{
1152
  CALI_CXX_MARK_SCOPE("LBSProblem::MakeSourceMomentsFromPhi");
4✔
1153

1154
  size_t num_local_dofs = discretization_->GetNumLocalDOFs(flux_moments_uk_man_);
4✔
1155

1156
  std::vector<double> source_moments(num_local_dofs, 0.0);
4✔
1157
  for (auto& groupset : groupsets_)
8✔
1158
  {
1159
    active_set_source_function_(groupset,
4✔
1160
                                source_moments,
1161
                                phi_new_local_,
4✔
1162
                                APPLY_AGS_SCATTER_SOURCES | APPLY_WGS_SCATTER_SOURCES |
1163
                                  APPLY_AGS_FISSION_SOURCES | APPLY_WGS_FISSION_SOURCES);
4✔
1164
  }
1165

1166
  return source_moments;
4✔
1167
}
4✔
1168

1169
LBSProblem::~LBSProblem()
926✔
1170
{
1171
  ResetGPUCarriers();
1172
}
4,622✔
1173

926✔
1174
void
1175
LBSProblem::ZeroPhi()
360✔
1176
{
1177
  std::fill(phi_old_local_.begin(), phi_old_local_.end(), 0.0);
360✔
1178
  std::fill(phi_new_local_.begin(), phi_new_local_.end(), 0.0);
360✔
1179
}
360✔
1180

1181
void
1182
LBSProblem::CopyPhiNewToOld()
260✔
1183
{
1184
  assert(phi_old_local_.size() == phi_new_local_.size() && "Phi vectors size mismatch.");
260✔
1185
  phi_old_local_ = phi_new_local_;
260✔
1186
}
260✔
1187

1188
void
1189
LBSProblem::SetPhiOldFrom(const std::vector<double>& phi_old)
2,640✔
1190
{
1191
  assert(phi_old.size() == phi_old_local_.size() && "SetPhiOldFrom size mismatch.");
2,640✔
1192
  phi_old_local_ = phi_old;
2,640✔
1193
}
2,640✔
1194

1195
void
1196
LBSProblem::SetPhiNewFrom(const std::vector<double>& phi_new)
×
1197
{
1198
  assert(phi_new.size() == phi_new_local_.size() && "SetPhiNewFrom size mismatch.");
×
1199
  phi_new_local_ = phi_new;
×
1200
}
×
1201

1202
void
1203
LBSProblem::ScalePhiOld(double factor)
41✔
1204
{
1205
  for (auto& value : phi_old_local_)
3,594,261✔
1206
    value *= factor;
3,594,220✔
1207
}
41✔
1208

1209
void
1210
LBSProblem::ScalePhiNew(double factor)
49✔
1211
{
1212
  for (auto& value : phi_new_local_)
3,762,269✔
1213
    value *= factor;
3,762,220✔
1214
}
49✔
1215

1216
void
1217
LBSProblem::ZeroQMoments()
79,309✔
1218
{
1219
  assert(q_moments_local_.size() == phi_old_local_.size() && "Q moments/Phi size mismatch.");
79,309✔
1220
  std::fill(q_moments_local_.begin(), q_moments_local_.end(), 0.0);
79,309✔
1221
}
79,309✔
1222

1223
void
1224
LBSProblem::ScaleQMoments(double factor)
9,585✔
1225
{
1226
  for (auto& value : q_moments_local_)
702,715,941✔
1227
    value *= factor;
702,706,356✔
1228
}
9,585✔
1229

1230
void
1231
LBSProblem::SetQMomentsFrom(const std::vector<double>& q_moments)
27,699✔
1232
{
1233
  assert(q_moments.size() == q_moments_local_.size() && "SetQMomentsFrom size mismatch.");
27,699✔
1234
  q_moments_local_ = q_moments;
27,699✔
1235
}
27,699✔
1236

1237
void
1238
LBSProblem::ScalePrecursors(double factor)
122✔
1239
{
1240
  for (auto& value : precursor_new_local_)
1,240✔
1241
    value *= factor;
1,118✔
1242
}
122✔
1243

1244
void
1245
LBSProblem::ZeroPrecursors()
2,440✔
1246
{
1247
  std::fill(precursor_new_local_.begin(), precursor_new_local_.end(), 0.0);
2,440✔
1248
}
2,440✔
1249

1250
void
1251
LBSProblem::ZeroExtSrcMoments()
×
1252
{
1253
  std::fill(ext_src_moments_local_.begin(), ext_src_moments_local_.end(), 0.0);
×
1254
}
×
1255

1256
void
1257
LBSProblem::ScaleExtSrcMoments(double factor)
×
1258
{
1259
  for (auto& value : ext_src_moments_local_)
×
1260
    value *= factor;
×
1261
}
×
1262

1263
void
1264
LBSProblem::SetAdjoint(bool adjoint)
24✔
1265
{
1266
  if (adjoint)
24✔
1267
    if (IsTimeDependent())
20✔
1268
      OpenSnInvalidArgument(GetName() + ": Time-dependent adjoint problems are not supported.");
×
1269

1270
  const bool mode_changed = (adjoint != options_.adjoint);
24✔
1271
  if (not mode_changed)
24✔
1272
    return;
1273

1274
  options_.adjoint = adjoint;
24✔
1275

1276
  // Reinitialize materials to obtain the proper forward/adjoint cross sections.
1277
  InitializeMaterials();
24✔
1278

1279
  // Forward and adjoint sources are fundamentally different.
1280
  point_sources_.clear();
24✔
1281
  volumetric_sources_.clear();
24✔
1282
  ClearBoundaries();
24✔
1283

1284
  // Reset all solution vectors.
1285
  ZeroPhi();
24✔
1286
  ResetDerivedSolutionVectors();
24✔
1287
  ZeroPrecursors();
24✔
1288
}
1289

1290
void
1291
LBSProblem::SetForward()
×
1292
{
1293
  SetAdjoint(false);
×
1294
}
×
1295

1296
bool
1297
LBSProblem::IsAdjoint() const
×
1298
{
1299
  return options_.adjoint;
×
1300
}
1301

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