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

Open-Sn / opensn / 25093042657

27 Apr 2026 05:34PM UTC coverage: 75.068%. Remained the same
25093042657

push

github

web-flow
Merge pull request #1033 from wdhawkins/iteration_logging

Improvements for iteration logging

364 of 485 new or added lines in 29 files covered. (75.05%)

478 existing lines in 26 files now uncovered.

21700 of 28907 relevant lines covered (75.07%)

65057883.8 hits per line

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

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

30
namespace opensn
31
{
32

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

38
  params.ChangeExistingParamToOptional("name", "LBSProblem");
1,336✔
39

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

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

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

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

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

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

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

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

65
  return params;
668✔
66
}
×
67

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

85
  // Initialize options
86
  if (params.IsParameterValid("options"))
668✔
87
  {
88
    auto options_params = LBSProblem::GetOptionsBlock();
454✔
89
    options_params.AssignParameters(params.GetParam("options"));
456✔
90
    ParseOptions(options_params);
452✔
91
  }
454✔
92

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

98
  InitializeGroupsets(params);
666✔
99
  InitializeSources(params);
666✔
100
  InitializeXSMap(params);
666✔
101
  InitializeMaterials();
666✔
102
}
684✔
103

104
const LBSOptions&
105
LBSProblem::GetOptions() const
1,275,435,118✔
106
{
107
  return options_;
1,275,435,118✔
108
}
109

110
double
111
LBSProblem::GetTime() const
185,220✔
112
{
113
  return time_;
185,220✔
114
}
115

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

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

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

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

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

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

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

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

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

173
unsigned int
174
LBSProblem::GetNumMoments() const
311,039✔
175
{
176
  return num_moments_;
311,039✔
177
}
178

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

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

191
bool
192
LBSProblem::UseGPUs() const
1,695✔
193
{
194
  return use_gpus_;
1,695✔
195
}
196

197
unsigned int
198
LBSProblem::GetNumGroups() const
489,448✔
199
{
200
  return num_groups_;
489,448✔
201
}
202

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

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

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

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

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

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

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

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

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

259
const std::vector<std::shared_ptr<PointSource>>&
260
LBSProblem::GetPointSources() const
23,762✔
261
{
262
  return point_sources_;
23,762✔
263
}
264

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

273
void
274
LBSProblem::ClearVolumetricSources()
12✔
275
{
276
  volumetric_sources_.clear();
12✔
277
}
12✔
278

279
const std::vector<std::shared_ptr<VolumetricSource>>&
280
LBSProblem::GetVolumetricSources() const
23,762✔
281
{
282
  return volumetric_sources_;
23,762✔
283
}
284

285
const BlockID2XSMap&
286
LBSProblem::GetBlockID2XSMap() const
2,064,044✔
287
{
288
  return block_id_to_xs_map_;
2,064,044✔
289
}
290

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

298
  block_id_to_xs_map_ = xs_map;
184✔
299
  InitializeMaterials();
184✔
300

301
  if (initialized_)
184✔
302
  {
303
    if (options_.use_precursors)
184✔
304
    {
305
      const size_t num_cells = grid_->local_cells.size();
136✔
306
      const size_t new_max_precursors_per_material = max_precursors_per_material_;
136✔
307
      const size_t num_precursor_dofs = num_cells * new_max_precursors_per_material;
136✔
308

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

318
          const unsigned int new_num_precursors =
13,656✔
319
            block_id_to_xs_map_.at(cell.block_id)->GetNumPrecursors();
13,656✔
320
          const unsigned int num_precursors_to_copy =
13,656✔
321
            std::min(old_num_precursors, new_num_precursors);
13,656✔
322

323
          const size_t old_base = cell.local_id * old_max_precursors_per_material;
13,656✔
324
          const size_t new_base = cell.local_id * new_max_precursors_per_material;
13,656✔
325
          for (unsigned int j = 0; j < num_precursors_to_copy; ++j)
15,660✔
326
            remapped_precursors[new_base + j] = old_precursor_state[old_base + j];
2,004✔
327
        }
328
      }
329

330
      precursor_new_local_ = std::move(remapped_precursors);
136✔
331
    }
136✔
332
    else
333
      precursor_new_local_.clear();
48✔
334
  }
335

336
  ResetGPUCarriers();
184✔
337
  InitializeGPUExtras();
184✔
338
}
184✔
339

340
std::shared_ptr<MeshContinuum>
341
LBSProblem::GetGrid() const
404,515✔
342
{
343
  return grid_;
404,515✔
344
}
345

346
const SpatialDiscretization&
347
LBSProblem::GetSpatialDiscretization() const
118,759✔
348
{
349
  return *discretization_;
118,759✔
350
}
351

352
const std::vector<UnitCellMatrices>&
353
LBSProblem::GetUnitCellMatrices() const
2,062,138✔
354
{
355
  return unit_cell_matrices_;
2,062,138✔
356
}
357

358
const std::map<uint64_t, UnitCellMatrices>&
359
LBSProblem::GetUnitGhostCellMatrices() const
16✔
360
{
361
  return unit_ghost_cell_matrices_;
16✔
362
}
363

364
std::vector<CellLBSView>&
365
LBSProblem::GetCellTransportViews()
186,151✔
366
{
367
  return cell_transport_views_;
186,151✔
368
}
369

370
const std::vector<CellLBSView>&
371
LBSProblem::GetCellTransportViews() const
285,619✔
372
{
373
  return cell_transport_views_;
285,619✔
374
}
375

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

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

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

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

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

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

412
const std::vector<double>&
413
LBSProblem::GetExtSrcMomentsLocal() const
91,760✔
414
{
415
  return ext_src_moments_local_;
91,760✔
416
}
417

418
void
419
LBSProblem::SetExtSrcMomentsFrom(const std::vector<double>& ext_src_moments)
4✔
420
{
421
  if (not phi_old_local_.empty())
4✔
422
    OpenSnLogicalErrorIf(ext_src_moments.size() != phi_old_local_.size(),
4✔
423
                         "SetExtSrcMomentsFrom size mismatch. Provided size=" +
424
                           std::to_string(ext_src_moments.size()) +
425
                           ", expected local DOFs=" + std::to_string(phi_old_local_.size()) + ".");
426

427
  if (ext_src_moments_local_.empty())
4✔
428
  {
429
    ext_src_moments_local_ = ext_src_moments;
4✔
430
    return;
4✔
431
  }
432

433
  assert(ext_src_moments.size() == ext_src_moments_local_.size() &&
×
434
         "SetExtSrcMomentsFrom size mismatch.");
435
  ext_src_moments_local_ = ext_src_moments;
×
436
}
437

438
std::vector<double>&
439
LBSProblem::GetPhiOldLocal()
196,071✔
440
{
441
  return phi_old_local_;
196,071✔
442
}
443

444
const std::vector<double>&
445
LBSProblem::GetPhiOldLocal() const
×
446
{
447
  return phi_old_local_;
×
448
}
449

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

456
const std::vector<double>&
457
LBSProblem::GetPhiNewLocal() const
10,339✔
458
{
459
  return phi_new_local_;
10,339✔
460
}
461

462
std::vector<double>&
463
LBSProblem::GetPrecursorsNewLocal()
5,438✔
464
{
465
  return precursor_new_local_;
5,438✔
466
}
467

468
const std::vector<double>&
469
LBSProblem::GetPrecursorsNewLocal() const
×
470
{
471
  return precursor_new_local_;
×
472
}
473

474
SetSourceFunction
475
LBSProblem::GetActiveSetSourceFunction() const
5,138✔
476
{
477
  return active_set_source_function_;
5,138✔
478
}
479

480
void
481
LBSProblem::SetActiveSetSourceFunction(SetSourceFunction source_function)
112✔
482
{
483
  active_set_source_function_ = std::move(source_function);
112✔
484
}
112✔
485

486
std::pair<size_t, size_t>
487
LBSProblem::GetNumPhiIterativeUnknowns()
×
488
{
489
  const auto& sdm = *discretization_;
×
490
  const size_t num_local_phi_dofs = sdm.GetNumLocalDOFs(flux_moments_uk_man_);
×
491
  const size_t num_global_phi_dofs = sdm.GetNumGlobalDOFs(flux_moments_uk_man_);
×
492

493
  return {num_local_phi_dofs, num_global_phi_dofs};
×
494
}
495

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

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

569
  return params;
906✔
570
}
×
571

572
InputParameters
573
LBSProblem::GetXSMapEntryBlock()
1,111✔
574
{
575
  InputParameters params;
1,111✔
576
  params.SetGeneralDescription("Set the cross-section map for the solver.");
2,222✔
577
  params.AddRequiredParameterArray("block_ids", "Mesh block IDs");
2,222✔
578
  params.AddRequiredParameter<std::shared_ptr<MultiGroupXS>>("xs", "Cross-section object");
2,222✔
579
  return params;
1,111✔
580
}
×
581

582
void
583
LBSProblem::ParseOptions(const InputParameters& input)
452✔
584
{
585
  auto params = LBSProblem::GetOptionsBlock();
452✔
586
  params.AssignParameters(input);
452✔
587
  const auto& params_at_assignment = input.GetParametersAtAssignment();
452✔
588
  const auto& specified_params = params_at_assignment.GetNumParameters() > 0
452✔
589
                                   ? params_at_assignment
452✔
590
                                   : static_cast<const ParameterBlock&>(input);
452✔
591

592
  using OptionSetter = std::function<void(const ParameterBlock&)>;
452✔
593
  const std::unordered_map<std::string, OptionSetter> option_setters = {
452✔
594
    {"max_mpi_message_size",
595
     [this](const ParameterBlock& spec) { options_.max_mpi_message_size = spec.GetValue<int>(); }},
×
596
    {"restart_writes_enabled",
597
     [this](const ParameterBlock& spec)
908✔
598
     { options_.restart_writes_enabled = spec.GetValue<bool>(); }},
4✔
599
    {"write_delayed_psi_to_restart",
600
     [this](const ParameterBlock& spec)
904✔
601
     { options_.write_delayed_psi_to_restart = spec.GetValue<bool>(); }},
×
602
    {"read_restart_path",
603
     [this](const ParameterBlock& spec)
920✔
604
     { options_.read_restart_path = BuildRestartPath(spec.GetValue<std::string>()); }},
32✔
605
    {"write_restart_path",
606
     [this](const ParameterBlock& spec)
908✔
607
     { options_.write_restart_path = BuildRestartPath(spec.GetValue<std::string>()); }},
8✔
608
    {"write_restart_time_interval",
609
     [this](const ParameterBlock& spec)
904✔
610
     { options_.write_restart_time_interval = std::chrono::seconds(spec.GetValue<int>()); }},
×
611
    {"use_precursors",
612
     [this](const ParameterBlock& spec) { options_.use_precursors = spec.GetValue<bool>(); }},
144✔
613
    {"use_source_moments",
614
     [this](const ParameterBlock& spec) { options_.use_src_moments = spec.GetValue<bool>(); }},
4✔
615
    {"save_angular_flux",
616
     [this](const ParameterBlock& spec) { options_.save_angular_flux = spec.GetValue<bool>(); }},
278✔
617
    {"verbose_inner_iterations",
618
     [this](const ParameterBlock& spec)
1,154✔
619
     { options_.verbose_inner_iterations = spec.GetValue<bool>(); }},
250✔
620
    {"max_ags_iterations",
621
     [this](const ParameterBlock& spec) { options_.max_ags_iterations = spec.GetValue<int>(); }},
72✔
622
    {"ags_tolerance",
623
     [this](const ParameterBlock& spec) { options_.ags_tolerance = spec.GetValue<double>(); }},
8✔
624
    {"ags_convergence_check",
625
     [this](const ParameterBlock& spec)
904✔
626
     { options_.ags_pointwise_convergence = (spec.GetValue<std::string>() == "pointwise"); }},
×
627
    {"verbose_outer_iterations",
628
     [this](const ParameterBlock& spec)
1,126✔
629
     { options_.verbose_outer_iterations = spec.GetValue<bool>(); }},
222✔
630
    {"power_default_kappa",
631
     [this](const ParameterBlock& spec)
917✔
632
     { options_.power_default_kappa = spec.GetValue<double>(); }},
13✔
633
    {"field_function_prefix_option",
634
     [this](const ParameterBlock& spec)
904✔
635
     { options_.field_function_prefix_option = spec.GetValue<std::string>(); }},
×
636
    {"field_function_prefix",
637
     [this](const ParameterBlock& spec)
904✔
UNCOV
638
     { options_.field_function_prefix = spec.GetValue<std::string>(); }},
×
639
    {"adjoint", [this](const ParameterBlock& spec) { options_.adjoint = spec.GetValue<bool>(); }},
16✔
640
  };
9,040✔
641

642
  for (const auto& spec : specified_params.GetParameters())
1,483✔
643
  {
644
    const auto setter_it = option_setters.find(spec.GetName());
2,062✔
645
    if (setter_it != option_setters.end())
1,031✔
646
      setter_it->second(spec);
1,031✔
647
  }
648

649
  OpenSnInvalidArgumentIf(options_.write_restart_time_interval > std::chrono::seconds(0) and
452✔
650
                            not options_.restart_writes_enabled,
651
                          GetName() + ": `write_restart_time_interval>0` requires "
652
                                      "`restart_writes_enabled=true`.");
653

654
  OpenSnInvalidArgumentIf(options_.write_restart_time_interval > std::chrono::seconds(0) and
452✔
655
                            options_.write_restart_time_interval < std::chrono::seconds(30),
656
                          GetName() + ": `write_restart_time_interval` must be 0 (disabled) "
657
                                      "or at least 30 seconds.");
658

659
  OpenSnInvalidArgumentIf(options_.restart_writes_enabled and options_.write_restart_path.empty(),
452✔
660
                          GetName() + ": `restart_writes_enabled=true` requires a non-empty "
661
                                      "`write_restart_path`.");
662

663
  OpenSnInvalidArgumentIf(not options_.field_function_prefix.empty() and
452✔
664
                            options_.field_function_prefix_option != "prefix",
665
                          GetName() + ": non-empty `field_function_prefix` requires "
666
                                      "`field_function_prefix_option=\"prefix\"`.");
667

668
  if (options_.restart_writes_enabled)
452✔
669
  {
670
    const auto dir = options_.write_restart_path.parent_path();
4✔
671

672
    // Create restart directory if necessary.
673
    // If dir is empty, write path resolves relative to the working directory.
674
    if ((not dir.empty()) and opensn::mpi_comm.rank() == 0)
8✔
675
    {
676
      if (not std::filesystem::exists(dir))
1✔
677
      {
UNCOV
678
        OpenSnLogicalErrorIf(not std::filesystem::create_directories(dir),
×
679
                             GetName() + ": Failed to create restart directory " + dir.string());
680
      }
681
      else
682
        OpenSnLogicalErrorIf(not std::filesystem::is_directory(dir),
1✔
683
                             GetName() + ": Restart path exists but is not a directory " +
684
                               dir.string());
685
    }
686
    opensn::mpi_comm.barrier();
4✔
687
    UpdateRestartWriteTime();
4✔
688
  }
4✔
689
}
904✔
690

691
std::filesystem::path
692
LBSProblem::BuildRestartPath(const std::string& path_stem)
20✔
693
{
694
  if (path_stem.empty())
20✔
UNCOV
695
    return {};
×
696

697
  auto path = std::filesystem::path(path_stem);
20✔
698
  path += std::to_string(opensn::mpi_comm.rank()) + ".restart.h5";
60✔
699
  return path;
20✔
700
}
20✔
701

702
bool
703
LBSProblem::ReadRestartData(const RestartDataHook& extra_reader)
16✔
704
{
705
  return LBSSolverIO::ReadRestartData(*this, extra_reader);
16✔
706
}
707

708
bool
709
LBSProblem::WriteRestartData(const RestartDataHook& extra_writer)
4✔
710
{
711
  return LBSSolverIO::WriteRestartData(*this, extra_writer);
4✔
712
}
713

714
bool
UNCOV
715
LBSProblem::ReadProblemRestartData(hid_t /*file_id*/)
×
716
{
UNCOV
717
  return true;
×
718
}
719

720
bool
UNCOV
721
LBSProblem::WriteProblemRestartData(hid_t /*file_id*/) const
×
722
{
UNCOV
723
  return true;
×
724
}
725

726
void
727
LBSProblem::BuildRuntime()
666✔
728
{
729
  CALI_CXX_MARK_SCOPE("LBSProblem::BuildRuntime");
666✔
730
  if (initialized_)
666✔
UNCOV
731
    return;
×
732

733
  PrintSimHeader();
666✔
734
  mpi_comm.barrier();
666✔
735

736
  InitializeRuntimeCore();
666✔
737
  ValidateRuntimeModeConfiguration();
666✔
738
  InitializeSources();
666✔
739

740
  initialized_ = true;
666✔
741
}
666✔
742

743
void
744
LBSProblem::InitializeRuntimeCore()
666✔
745
{
746
  InitializeSpatialDiscretization();
666✔
747
  InitializeParrays();
666✔
748
  InitializeBoundaries();
666✔
749
  InitializeGPUExtras();
666✔
750
}
666✔
751

752
void
753
LBSProblem::ValidateRuntimeModeConfiguration() const
666✔
754
{
755
  if (options_.adjoint)
666✔
756
    if (IsTimeDependent())
16✔
UNCOV
757
      OpenSnInvalidArgument(GetName() + ": Time-dependent adjoint problems are not supported.");
×
758
}
666✔
759

760
void
761
LBSProblem::InitializeSources()
666✔
762
{
763
  // Initialize point sources
764
  for (auto& point_source : point_sources_)
674✔
765
    point_source->Initialize(*this);
8✔
766

767
  // Initialize volumetric sources
768
  for (auto& volumetric_source : volumetric_sources_)
1,241✔
769
    volumetric_source->Initialize(*this);
575✔
770
}
666✔
771

772
void
UNCOV
773
LBSProblem::PrintSimHeader()
×
774
{
775
  if (opensn::mpi_comm.rank() == 0)
×
776
  {
777
    std::stringstream outstr;
×
778
    outstr << "\n"
×
779
           << "Initializing " << GetName() << "\n\n"
×
780
           << "Scattering order    : " << scattering_order_ << "\n"
×
UNCOV
781
           << "Number of moments   : " << num_moments_ << "\n"
×
782
           << "Number of groups    : " << num_groups_ << "\n"
×
UNCOV
783
           << "Number of groupsets : " << groupsets_.size() << "\n\n";
×
784

785
    for (const auto& groupset : groupsets_)
×
786
    {
UNCOV
787
      outstr << "***** Groupset " << groupset.id << " *****\n"
×
788
             << "Groups:\n";
×
UNCOV
789
      const auto n_gs_groups = groupset.GetNumGroups();
×
790
      constexpr int groups_per_line = 12;
791
      for (size_t i = 0; i < n_gs_groups; ++i)
×
792
      {
UNCOV
793
        outstr << std::setw(5) << groupset.first_group + i << ' ';
×
794
        if ((i + 1) % groups_per_line == 0)
×
795
          outstr << '\n';
×
796
      }
UNCOV
797
      if (n_gs_groups > 0 && n_gs_groups % groups_per_line != 0)
×
798
        outstr << '\n';
×
799
    }
800

UNCOV
801
    log.Log() << outstr.str() << '\n';
×
UNCOV
802
  }
×
UNCOV
803
}
×
804

805
void
806
LBSProblem::InitializeSources(const InputParameters& params)
666✔
807
{
808
  if (params.Has("volumetric_sources"))
666✔
809
  {
810
    const auto& vol_srcs = params.GetParam("volumetric_sources");
666✔
811
    vol_srcs.RequireBlockTypeIs(ParameterBlockType::ARRAY);
666✔
812
    for (const auto& src : vol_srcs)
1,241✔
813
      volumetric_sources_.push_back(src.GetValue<std::shared_ptr<VolumetricSource>>());
1,150✔
814
  }
815

816
  if (params.Has("point_sources"))
666✔
817
  {
818
    const auto& pt_srcs = params.GetParam("point_sources");
666✔
819
    pt_srcs.RequireBlockTypeIs(ParameterBlockType::ARRAY);
666✔
820
    for (const auto& src : pt_srcs)
674✔
821
      point_sources_.push_back(src.GetValue<std::shared_ptr<PointSource>>());
16✔
822
  }
823
}
666✔
824

825
void
826
LBSProblem::InitializeGroupsets(const InputParameters& params)
666✔
827
{
828
  // Initialize groups
829
  OpenSnInvalidArgumentIf(num_groups_ == 0, GetName() + ": Number of groups must be > 0.");
666✔
830

831
  // Initialize groupsets
832
  const auto& groupsets_array = params.GetParam("groupsets");
666✔
833
  const size_t num_gs = groupsets_array.GetNumParameters();
666✔
834
  OpenSnInvalidArgumentIf(num_gs == 0, GetName() + ": At least one groupset must be specified.");
666✔
835
  for (size_t gs = 0; gs < num_gs; ++gs)
1,417✔
836
  {
837
    const auto& groupset_params = groupsets_array.GetParam(gs);
751✔
838
    InputParameters gs_input_params = LBSGroupset::GetInputParameters();
751✔
839
    gs_input_params.SetObjectType("LBSProblem:LBSGroupset");
751✔
840
    gs_input_params.AssignParameters(groupset_params);
751✔
841
    groupsets_.emplace_back(gs_input_params, gs, *this);
751✔
842
    if (groupsets_.back().GetNumGroups() == 0)
751✔
843
    {
844
      std::stringstream oss;
×
UNCOV
845
      oss << GetName() << ": No groups added to groupset " << groupsets_.back().id;
×
UNCOV
846
      OpenSnInvalidArgument(oss.str());
×
UNCOV
847
    }
×
848
  }
751✔
849
}
666✔
850

851
void
852
LBSProblem::InitializeXSMap(const InputParameters& params)
666✔
853
{
854
  // Build XS map
855
  const auto& xs_array = params.GetParam("xs_map");
666✔
856
  const size_t num_xs = xs_array.GetNumParameters();
666✔
857
  for (size_t i = 0; i < num_xs; ++i)
1,593✔
858
  {
859
    const auto& item_params = xs_array.GetParam(i);
927✔
860
    InputParameters xs_entry_pars = GetXSMapEntryBlock();
927✔
861
    xs_entry_pars.AssignParameters(item_params);
927✔
862

863
    const auto& block_ids_param = xs_entry_pars.GetParam("block_ids");
927✔
864
    block_ids_param.RequireBlockTypeIs(ParameterBlockType::ARRAY);
927✔
865
    const auto& block_ids = block_ids_param.GetVectorValue<unsigned int>();
927✔
866
    auto xs = xs_entry_pars.GetSharedPtrParam<MultiGroupXS>("xs");
927✔
867
    for (const auto& block_id : block_ids)
1,998✔
868
      block_id_to_xs_map_[block_id] = xs;
1,071✔
869
  }
927✔
870
}
666✔
871

872
void
873
LBSProblem::InitializeMaterials()
874✔
874
{
875
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeMaterials");
874✔
876

877
  log.Log0Verbose1() << "Initializing Materials";
1,748✔
878

879
  // Create set of material ids locally relevant
880
  int invalid_mat_cell_count = 0;
874✔
881
  std::set<unsigned int> unique_block_ids;
874✔
882
  for (auto& cell : grid_->local_cells)
677,851✔
883
  {
884
    unique_block_ids.insert(cell.block_id);
676,977✔
885
    if (cell.block_id == std::numeric_limits<unsigned int>::max() or
676,977✔
886
        (block_id_to_xs_map_.find(cell.block_id) == block_id_to_xs_map_.end()))
676,977✔
UNCOV
887
      ++invalid_mat_cell_count;
×
888
  }
889
  const auto& ghost_cell_ids = grid_->cells.GetGhostGlobalIDs();
874✔
890
  for (uint64_t cell_id : ghost_cell_ids)
109,869✔
891
  {
892
    const auto& cell = grid_->cells[cell_id];
108,995✔
893
    unique_block_ids.insert(cell.block_id);
108,995✔
894
    if (cell.block_id == std::numeric_limits<unsigned int>::max() or
108,995✔
895
        (block_id_to_xs_map_.find(cell.block_id) == block_id_to_xs_map_.end()))
108,995✔
UNCOV
896
      ++invalid_mat_cell_count;
×
897
  }
898
  OpenSnLogicalErrorIf(invalid_mat_cell_count > 0,
874✔
899
                       std::to_string(invalid_mat_cell_count) +
900
                         " cells encountered with an invalid material id.");
901

902
  // Get ready for processing
903
  for (const auto& [blk_id, mat] : block_id_to_xs_map_)
2,177✔
904
  {
905
    mat->SetAdjointMode(options_.adjoint);
1,303✔
906

907
    OpenSnLogicalErrorIf(mat->GetNumGroups() < num_groups_,
1,303✔
908
                         "Cross-sections for block \"" + std::to_string(blk_id) +
909
                           "\" have fewer groups (" + std::to_string(mat->GetNumGroups()) +
910
                           ") than the simulation (" + std::to_string(num_groups_) + "). " +
911
                           "Cross-sections must have at least as many groups as the simulation.");
912
  }
913

914
  // Initialize precursor properties
915
  num_precursors_ = 0;
874✔
916
  max_precursors_per_material_ = 0;
874✔
917
  for (const auto& mat_id_xs : block_id_to_xs_map_)
2,177✔
918
  {
919
    const auto& xs = mat_id_xs.second;
1,303✔
920
    num_precursors_ += xs->GetNumPrecursors();
1,303✔
921
    max_precursors_per_material_ = std::max(xs->GetNumPrecursors(), max_precursors_per_material_);
1,303✔
922
  }
923

924
  const bool has_fissionable_precursors =
874✔
925
    std::any_of(block_id_to_xs_map_.begin(),
874✔
926
                block_id_to_xs_map_.end(),
927
                [](const auto& mat_id_xs)
1,303✔
928
                {
929
                  const auto& xs = mat_id_xs.second;
1,303✔
930
                  return xs->IsFissionable() and xs->GetNumPrecursors() > 0;
1,303✔
931
                });
932
  const bool has_fissionable_material =
874✔
933
    std::any_of(block_id_to_xs_map_.begin(),
874✔
934
                block_id_to_xs_map_.end(),
935
                [](const auto& mat_id_xs) { return mat_id_xs.second->IsFissionable(); });
1,259✔
936

937
  const bool has_any_precursor_data =
874✔
938
    std::any_of(block_id_to_xs_map_.begin(),
874✔
939
                block_id_to_xs_map_.end(),
940
                [](const auto& mat_id_xs) { return mat_id_xs.second->GetNumPrecursors() > 0; });
1,303✔
941

942
  if (options_.use_precursors and has_fissionable_material and not has_any_precursor_data)
874✔
943
  {
944
    log.Log0Warning() << GetName()
255✔
945
                      << ": options.use_precursors is enabled, but no precursor data was found "
946
                         "in the active cross-section map. Running without delayed-neutron "
947
                         "precursor coupling.";
85✔
948
  }
949

950
  // check compatibility when at least one fissionable material has delayed-neutron data
951
  if (options_.use_precursors and has_fissionable_precursors)
874✔
952
  {
953
    for (const auto& [mat_id, xs] : block_id_to_xs_map_)
378✔
954
    {
955
      OpenSnInvalidArgumentIf(xs->IsFissionable() and xs->GetNumPrecursors() == 0,
189✔
956
                              GetName() + ": incompatible cross-section data for material id " +
957
                                std::to_string(mat_id) +
958
                                ". When options.use_precursors=true and "
959
                                "delayed-neutron precursor data is present for one fissionable "
960
                                "material, it must be present for all fissionable materials.");
961
    }
962
  }
963

964
  // Update transport views if available
965
  if (grid_->local_cells.size() == cell_transport_views_.size())
874✔
966
    for (const auto& cell : grid_->local_cells)
28,108✔
967
    {
968
      const auto& xs_ptr = block_id_to_xs_map_[cell.block_id];
27,900✔
969
      auto& transport_view = cell_transport_views_[cell.local_id];
27,900✔
970
      transport_view.ReassignXS(*xs_ptr);
27,900✔
971
    }
972

973
  mpi_comm.barrier();
874✔
974
}
874✔
975

976
void
977
LBSProblem::InitializeSpatialDiscretization()
586✔
978
{
979
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeSpatialDiscretization");
586✔
980

981
  OpenSnLogicalErrorIf(not discretization_,
586✔
982
                       GetName() + ": Missing spatial discretization. Construct the problem "
983
                                   "through its factory Create(...) entry point.");
984
  log.Log() << "Initializing spatial discretization metadata.\n";
1,172✔
985

986
  ComputeUnitIntegrals();
586✔
987
}
586✔
988

989
void
990
LBSProblem::ComputeUnitIntegrals()
666✔
991
{
992
  CALI_CXX_MARK_SCOPE("LBSProblem::ComputeUnitIntegrals");
666✔
993

994
  log.Log() << "Computing unit integrals.\n";
1,332✔
995
  const auto& sdm = *discretization_;
666✔
996

997
  const size_t num_local_cells = grid_->local_cells.size();
666✔
998
  unit_cell_matrices_.resize(num_local_cells);
666✔
999

1000
  for (const auto& cell : grid_->local_cells)
649,743✔
1001
    unit_cell_matrices_[cell.local_id] =
649,077✔
1002
      ComputeUnitCellIntegrals(sdm, cell, grid_->GetCoordinateSystem());
649,077✔
1003

1004
  const auto ghost_ids = grid_->cells.GetGhostGlobalIDs();
666✔
1005
  for (auto ghost_id : ghost_ids)
100,801✔
1006
    unit_ghost_cell_matrices_[ghost_id] =
100,135✔
1007
      ComputeUnitCellIntegrals(sdm, grid_->cells[ghost_id], grid_->GetCoordinateSystem());
200,270✔
1008

1009
  // Assessing global unit cell matrix storage
1010
  std::array<size_t, 2> num_local_ucms = {unit_cell_matrices_.size(),
666✔
1011
                                          unit_ghost_cell_matrices_.size()};
666✔
1012
  std::array<size_t, 2> num_global_ucms = {0, 0};
666✔
1013

1014
  mpi_comm.all_reduce(num_local_ucms.data(), 2, num_global_ucms.data(), mpi::op::sum<size_t>());
666✔
1015

1016
  opensn::mpi_comm.barrier();
666✔
1017
  log.Log() << "Ghost cell unit cell-matrix ratio: "
666✔
1018
            << (double)num_global_ucms[1] * 100 / (double)num_global_ucms[0] << "%";
1,332✔
1019
  log.Log() << "Cell matrices computed.";
1,332✔
1020
}
666✔
1021

1022
void
1023
LBSProblem::InitializeParrays()
666✔
1024
{
1025
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeParrays");
666✔
1026

1027
  log.Log() << "Initializing parallel arrays."
1,332✔
1028
            << " G=" << num_groups_ << " M=" << num_moments_ << std::endl;
666✔
1029

1030
  // Initialize unknown
1031
  // structure
1032
  flux_moments_uk_man_.unknowns.clear();
666✔
1033
  for (unsigned int m = 0; m < num_moments_; ++m)
2,257✔
1034
  {
1035
    flux_moments_uk_man_.AddUnknown(UnknownType::VECTOR_N, num_groups_);
1,591✔
1036
    flux_moments_uk_man_.unknowns.back().name = "m" + std::to_string(m);
1,591✔
1037
  }
1038

1039
  // Compute local # of dof
1040
  local_node_count_ = discretization_->GetNumLocalNodes();
666✔
1041
  global_node_count_ = discretization_->GetNumGlobalNodes();
666✔
1042

1043
  // Compute num of unknowns
1044
  size_t local_unknown_count = local_node_count_ * num_groups_ * num_moments_;
666✔
1045

1046
  log.LogAllVerbose1() << "LBS Number of phi unknowns: " << local_unknown_count;
1,332✔
1047

1048
  // Size local vectors
1049
  q_moments_local_.assign(local_unknown_count, 0.0);
666✔
1050
  phi_old_local_.assign(local_unknown_count, 0.0);
666✔
1051
  phi_new_local_.assign(local_unknown_count, 0.0);
666✔
1052

1053
  // Setup precursor vector
1054
  if (options_.use_precursors)
666✔
1055
  {
1056
    size_t num_precursor_dofs = grid_->local_cells.size() * max_precursors_per_material_;
590✔
1057
    precursor_new_local_.assign(num_precursor_dofs, 0.0);
590✔
1058
  }
1059

1060
  // Initialize transport views
1061
  // Transport views act as a data structure to store information
1062
  // related to the transport simulation. The most prominent function
1063
  // here is that it holds the means to know where a given cell's
1064
  // transport quantities are located in the unknown vectors (i.e. phi)
1065
  //
1066
  // Also, for a given cell, within a given sweep chunk,
1067
  // we need to solve a matrix which square size is the
1068
  // amount of nodes on the cell. max_cell_dof_count is
1069
  // initialized here.
1070
  //
1071
  size_t block_MG_counter = 0; // Counts the strides of moment and group
666✔
1072

1073
  const Vector3 ihat(1.0, 0.0, 0.0);
666✔
1074
  const Vector3 jhat(0.0, 1.0, 0.0);
666✔
1075
  const Vector3 khat(0.0, 0.0, 1.0);
666✔
1076

1077
  min_cell_dof_count_ = std::numeric_limits<unsigned int>::max();
666✔
1078
  max_cell_dof_count_ = 0;
666✔
1079
  cell_transport_views_.clear();
666✔
1080
  cell_transport_views_.reserve(grid_->local_cells.size());
666✔
1081
  for (auto& cell : grid_->local_cells)
649,743✔
1082
  {
1083
    size_t num_nodes = discretization_->GetCellNumNodes(cell);
649,077✔
1084

1085
    // compute cell volumes
1086
    double cell_volume = 0.0;
649,077✔
1087
    const auto& IntV_shapeI = unit_cell_matrices_[cell.local_id].intV_shapeI;
649,077✔
1088
    for (size_t i = 0; i < num_nodes; ++i)
4,576,461✔
1089
      cell_volume += IntV_shapeI(i);
3,927,384✔
1090

1091
    size_t cell_phi_address = block_MG_counter;
649,077✔
1092

1093
    const size_t num_faces = cell.faces.size();
649,077✔
1094
    std::vector<bool> face_local_flags(num_faces, true);
649,077✔
1095
    std::vector<int> face_locality(num_faces, opensn::mpi_comm.rank());
649,077✔
1096
    std::vector<const Cell*> neighbor_cell_ptrs(num_faces, nullptr);
649,077✔
1097
    bool cell_on_boundary = false;
649,077✔
1098
    int f = 0;
649,077✔
1099
    for (auto& face : cell.faces)
3,894,625✔
1100
    {
1101
      if (not face.has_neighbor)
3,245,548✔
1102
      {
1103
        cell_on_boundary = true;
115,836✔
1104
        face_local_flags[f] = false;
115,836✔
1105
        face_locality[f] = -1;
115,836✔
1106
      } // if bndry
1107
      else
1108
      {
1109
        const int neighbor_partition = face.GetNeighborPartitionID(grid_.get());
3,129,712✔
1110
        face_local_flags[f] = (neighbor_partition == opensn::mpi_comm.rank());
3,129,712✔
1111
        face_locality[f] = neighbor_partition;
3,129,712✔
1112
        neighbor_cell_ptrs[f] = &grid_->cells[face.neighbor_id];
3,129,712✔
1113
      }
1114

1115
      ++f;
3,245,548✔
1116
    } // for f
1117

1118
    max_cell_dof_count_ = std::max(max_cell_dof_count_, static_cast<unsigned int>(num_nodes));
649,077✔
1119
    min_cell_dof_count_ = std::min(min_cell_dof_count_, static_cast<unsigned int>(num_nodes));
649,077✔
1120
    cell_transport_views_.emplace_back(cell_phi_address,
1,298,154✔
1121
                                       num_nodes,
1122
                                       num_groups_,
649,077✔
1123
                                       num_moments_,
649,077✔
1124
                                       num_faces,
1125
                                       *block_id_to_xs_map_[cell.block_id],
649,077✔
1126
                                       cell_volume,
1127
                                       face_local_flags,
1128
                                       face_locality,
1129
                                       neighbor_cell_ptrs,
1130
                                       cell_on_boundary);
1131
    block_MG_counter += num_nodes * num_groups_ * num_moments_;
649,077✔
1132
  } // for local cell
649,077✔
1133

1134
  // Populate grid nodal mappings
1135
  // This is used in the Flux Data Structures (FLUDS)
1136
  grid_nodal_mappings_.clear();
666✔
1137
  grid_nodal_mappings_.reserve(grid_->local_cells.size());
666✔
1138
  for (auto& cell : grid_->local_cells)
649,743✔
1139
  {
1140
    CellFaceNodalMapping cell_nodal_mapping;
649,077✔
1141
    cell_nodal_mapping.reserve(cell.faces.size());
649,077✔
1142

1143
    for (auto& face : cell.faces)
3,894,625✔
1144
    {
1145
      std::vector<short> face_node_mapping;
3,245,548✔
1146
      std::vector<short> cell_node_mapping;
3,245,548✔
1147
      int adj_face_idx = -1;
3,245,548✔
1148

1149
      if (face.has_neighbor)
3,245,548✔
1150
      {
1151
        grid_->FindAssociatedVertices(face, face_node_mapping);
3,129,712✔
1152
        grid_->FindAssociatedCellVertices(face, cell_node_mapping);
3,129,712✔
1153
        adj_face_idx = face.GetNeighborAdjacentFaceIndex(grid_.get());
3,129,712✔
1154
      }
1155

1156
      cell_nodal_mapping.emplace_back(adj_face_idx, face_node_mapping, cell_node_mapping);
3,245,548✔
1157
    } // for f
3,245,548✔
1158

1159
    grid_nodal_mappings_.push_back(cell_nodal_mapping);
649,077✔
1160
  } // for local cell
649,077✔
1161

1162
  // Get grid localized communicator set
1163
  grid_local_comm_set_ = grid_->MakeMPILocalCommunicatorSet();
666✔
1164

1165
  opensn::mpi_comm.barrier();
666✔
1166
  log.Log() << "Done with parallel arrays." << std::endl;
1,332✔
1167
}
666✔
1168

1169
#ifndef __OPENSN_WITH_GPU__
1170
void
1171
LBSProblem::InitializeGPUExtras()
1172
{
1173
}
1174

1175
void
1176
LBSProblem::ResetGPUCarriers()
1177
{
1178
}
1179

1180
void
1181
LBSProblem::CheckCapableDevices()
1182
{
1183
}
1184
#endif // __OPENSN_WITH_GPU__
1185

1186
std::vector<double>
1187
LBSProblem::MakeSourceMomentsFromPhi()
4✔
1188
{
1189
  CALI_CXX_MARK_SCOPE("LBSProblem::MakeSourceMomentsFromPhi");
4✔
1190

1191
  size_t num_local_dofs = discretization_->GetNumLocalDOFs(flux_moments_uk_man_);
4✔
1192

1193
  std::vector<double> source_moments(num_local_dofs, 0.0);
4✔
1194
  for (auto& groupset : groupsets_)
8✔
1195
  {
1196
    active_set_source_function_(groupset,
4✔
1197
                                source_moments,
1198
                                phi_new_local_,
4✔
1199
                                APPLY_AGS_SCATTER_SOURCES | APPLY_WGS_SCATTER_SOURCES |
1200
                                  APPLY_AGS_FISSION_SOURCES | APPLY_WGS_FISSION_SOURCES);
4✔
1201
  }
1202

1203
  return source_moments;
4✔
1204
}
4✔
1205

1206
LBSProblem::~LBSProblem()
654✔
1207
{
1208
  ResetGPUCarriers();
1209
}
3,270✔
1210

654✔
1211
void
1212
LBSProblem::ZeroPhi()
84✔
1213
{
1214
  std::fill(phi_old_local_.begin(), phi_old_local_.end(), 0.0);
84✔
1215
  std::fill(phi_new_local_.begin(), phi_new_local_.end(), 0.0);
84✔
1216
}
84✔
1217

1218
void
1219
LBSProblem::CopyPhiNewToOld()
176✔
1220
{
1221
  assert(phi_old_local_.size() == phi_new_local_.size() && "Phi vectors size mismatch.");
176✔
1222
  phi_old_local_ = phi_new_local_;
176✔
1223
}
176✔
1224

1225
void
1226
LBSProblem::SetPhiOldFrom(const std::vector<double>& phi_old)
2,420✔
1227
{
1228
  assert(phi_old.size() == phi_old_local_.size() && "SetPhiOldFrom size mismatch.");
2,420✔
1229
  phi_old_local_ = phi_old;
2,420✔
1230
}
2,420✔
1231

1232
void
1233
LBSProblem::SetPhiNewFrom(const std::vector<double>& phi_new)
×
1234
{
UNCOV
1235
  assert(phi_new.size() == phi_new_local_.size() && "SetPhiNewFrom size mismatch.");
×
UNCOV
1236
  phi_new_local_ = phi_new;
×
1237
}
×
1238

1239
void
1240
LBSProblem::ScalePhiOld(double factor)
×
1241
{
UNCOV
1242
  for (auto& value : phi_old_local_)
×
UNCOV
1243
    value *= factor;
×
UNCOV
1244
}
×
1245

1246
void
1247
LBSProblem::ScalePhiNew(double factor)
8✔
1248
{
1249
  for (auto& value : phi_new_local_)
168,008✔
1250
    value *= factor;
168,000✔
1251
}
8✔
1252

1253
void
1254
LBSProblem::ZeroQMoments()
65,465✔
1255
{
1256
  assert(q_moments_local_.size() == phi_old_local_.size() && "Q moments/Phi size mismatch.");
65,465✔
1257
  std::fill(q_moments_local_.begin(), q_moments_local_.end(), 0.0);
65,465✔
1258
}
65,465✔
1259

1260
void
1261
LBSProblem::ScaleQMoments(double factor)
8,701✔
1262
{
1263
  for (auto& value : q_moments_local_)
571,607,457✔
1264
    value *= factor;
571,598,756✔
1265
}
8,701✔
1266

1267
void
1268
LBSProblem::SetQMomentsFrom(const std::vector<double>& q_moments)
25,231✔
1269
{
1270
  assert(q_moments.size() == q_moments_local_.size() && "SetQMomentsFrom size mismatch.");
25,231✔
1271
  q_moments_local_ = q_moments;
25,231✔
1272
}
25,231✔
1273

1274
void
1275
LBSProblem::ScalePrecursors(double factor)
122✔
1276
{
1277
  for (auto& value : precursor_new_local_)
1,240✔
1278
    value *= factor;
1,118✔
1279
}
122✔
1280

1281
void
1282
LBSProblem::ZeroPrecursors()
2,356✔
1283
{
1284
  std::fill(precursor_new_local_.begin(), precursor_new_local_.end(), 0.0);
2,356✔
1285
}
2,356✔
1286

1287
void
1288
LBSProblem::ZeroExtSrcMoments()
×
1289
{
UNCOV
1290
  std::fill(ext_src_moments_local_.begin(), ext_src_moments_local_.end(), 0.0);
×
1291
}
×
1292

1293
void
1294
LBSProblem::ScaleExtSrcMoments(double factor)
×
1295
{
UNCOV
1296
  for (auto& value : ext_src_moments_local_)
×
UNCOV
1297
    value *= factor;
×
UNCOV
1298
}
×
1299

1300
void
1301
LBSProblem::SetAdjoint(bool adjoint)
24✔
1302
{
1303
  OpenSnLogicalErrorIf(
24✔
1304
    not initialized_, GetName() + ": Problem must be fully constructed before calling SetAdjoint.");
1305

1306
  if (adjoint)
24✔
1307
    if (IsTimeDependent())
20✔
UNCOV
1308
      OpenSnInvalidArgument(GetName() + ": Time-dependent adjoint problems are not supported.");
×
1309

1310
  const bool mode_changed = (adjoint != options_.adjoint);
24✔
1311
  if (not mode_changed)
24✔
1312
    return;
1313

1314
  options_.adjoint = adjoint;
24✔
1315

1316
  // Reinitialize materials to obtain the proper forward/adjoint cross sections.
1317
  InitializeMaterials();
24✔
1318

1319
  // Forward and adjoint sources are fundamentally different.
1320
  point_sources_.clear();
24✔
1321
  volumetric_sources_.clear();
24✔
1322
  ClearBoundaries();
24✔
1323

1324
  // Reset all solution vectors.
1325
  ZeroPhi();
24✔
1326
  ResetDerivedSolutionVectors();
24✔
1327
  ZeroPrecursors();
24✔
1328
}
1329

1330
void
1331
LBSProblem::SetForward()
×
1332
{
UNCOV
1333
  SetAdjoint(false);
×
1334
}
×
1335

1336
bool
UNCOV
1337
LBSProblem::IsAdjoint() const
×
1338
{
UNCOV
1339
  return options_.adjoint;
×
1340
}
1341

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