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

Open-Sn / opensn / 19558587334

20 Nov 2025 04:45PM UTC coverage: 74.139% (+0.1%) from 74.012%
19558587334

push

github

web-flow
Merge pull request #843 from wdhawkins/tidy_fixes

Fixes for moving from clang-tidy 19.x to clang-tidy 21.x

18365 of 24771 relevant lines covered (74.14%)

53857292.47 hits per line

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

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

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

27
namespace opensn
28
{
29

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

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

40
  params.ChangeExistingParamToOptional("name", "LBSProblem");
628✔
41

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

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

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

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

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

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

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

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

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

70
  return params;
314✔
71
}
×
72

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

90
  // Initialize options
91
  if (params.IsParameterValid("options"))
314✔
92
  {
93
    auto options_params = LBSProblem::GetOptionsBlock();
178✔
94
    options_params.AssignParameters(params.GetParam("options"));
180✔
95
    SetOptions(options_params);
176✔
96
  }
178✔
97

98
  // Set geometry type
99
  geometry_type_ = grid_->GetGeometryType();
312✔
100
  if (geometry_type_ == GeometryType::INVALID)
312✔
101
    throw std::runtime_error(GetName() + ": Invalid geometry type.");
×
102

103
  // Set boundary conditions
104
  if (params.Has("boundary_conditions"))
312✔
105
  {
106
    const auto& bcs = params.GetParam("boundary_conditions");
312✔
107
    bcs.RequireBlockTypeIs(ParameterBlockType::ARRAY);
312✔
108
    for (size_t b = 0; b < bcs.GetNumParameters(); ++b)
838✔
109
    {
110
      auto bndry_params = GetBoundaryOptionsBlock();
526✔
111
      bndry_params.AssignParameters(bcs.GetParam(b));
526✔
112
      SetBoundaryOptions(bndry_params);
526✔
113
    }
526✔
114
  }
115

116
  InitializeGroupsets(params);
312✔
117
  InitializeSources(params);
312✔
118
  InitializeXSmapAndDensities(params);
312✔
119
  InitializeMaterials();
312✔
120
}
332✔
121

122
LBSOptions&
123
LBSProblem::GetOptions()
17,310✔
124
{
125
  return options_;
17,310✔
126
}
127

128
const LBSOptions&
129
LBSProblem::GetOptions() const
473,157,114✔
130
{
131
  return options_;
473,157,114✔
132
}
133

134
GeometryType
135
LBSProblem::GetGeometryType() const
4✔
136
{
137
  return geometry_type_;
4✔
138
}
139

140
size_t
141
LBSProblem::GetNumMoments() const
129,215✔
142
{
143
  return num_moments_;
129,215✔
144
}
145

146
size_t
147
LBSProblem::GetNumGroups() const
53,088✔
148
{
149
  return num_groups_;
53,088✔
150
}
151

152
unsigned int
153
LBSProblem::GetScatteringOrder() const
4✔
154
{
155
  return scattering_order_;
4✔
156
}
157

158
size_t
159
LBSProblem::GetNumPrecursors() const
×
160
{
161
  return num_precursors_;
×
162
}
163

164
size_t
165
LBSProblem::GetMaxPrecursorsPerMaterial() const
8✔
166
{
167
  return max_precursors_per_material_;
8✔
168
}
169

170
const std::vector<LBSGroup>&
171
LBSProblem::GetGroups() const
139,190✔
172
{
173
  return groups_;
139,190✔
174
}
175

176
std::vector<LBSGroupset>&
177
LBSProblem::GetGroupsets()
8,170,715✔
178
{
179
  return groupsets_;
8,170,715✔
180
}
181

182
const std::vector<LBSGroupset>&
183
LBSProblem::GetGroupsets() const
×
184
{
185
  return groupsets_;
×
186
}
187

188
void
189
LBSProblem::AddPointSource(std::shared_ptr<PointSource> point_source)
×
190
{
191
  point_sources_.push_back(point_source);
×
192
  if (discretization_)
×
193
    point_sources_.back()->Initialize(*this);
×
194
}
×
195

196
void
197
LBSProblem::ClearPointSources()
×
198
{
199
  point_sources_.clear();
×
200
}
×
201

202
const std::vector<std::shared_ptr<PointSource>>&
203
LBSProblem::GetPointSources() const
7,452✔
204
{
205
  return point_sources_;
7,452✔
206
}
207

208
void
209
LBSProblem::AddVolumetricSource(std::shared_ptr<VolumetricSource> volumetric_source)
16✔
210
{
211
  volumetric_sources_.push_back(volumetric_source);
16✔
212
  if (discretization_)
16✔
213
    volumetric_sources_.back()->Initialize(*this);
16✔
214
}
16✔
215

216
void
217
LBSProblem::ClearVolumetricSources()
4✔
218
{
219
  volumetric_sources_.clear();
4✔
220
}
4✔
221

222
const std::vector<std::shared_ptr<VolumetricSource>>&
223
LBSProblem::GetVolumetricSources() const
7,452✔
224
{
225
  return volumetric_sources_;
7,452✔
226
}
227

228
void
229
LBSProblem::ClearBoundaries()
×
230
{
231
  boundary_preferences_.clear();
×
232
}
×
233

234
const std::map<int, std::shared_ptr<MultiGroupXS>>&
235
LBSProblem::GetMatID2XSMap() const
6,681✔
236
{
237
  return block_id_to_xs_map_;
6,681✔
238
}
239

240
std::shared_ptr<MeshContinuum>
241
LBSProblem::GetGrid() const
181,274✔
242
{
243
  return grid_;
181,274✔
244
}
245

246
const SpatialDiscretization&
247
LBSProblem::GetSpatialDiscretization() const
64,184✔
248
{
249
  return *discretization_;
64,184✔
250
}
251

252
const std::vector<UnitCellMatrices>&
253
LBSProblem::GetUnitCellMatrices() const
7,271✔
254
{
255
  return unit_cell_matrices_;
7,271✔
256
}
257

258
const std::map<uint64_t, UnitCellMatrices>&
259
LBSProblem::GetUnitGhostCellMatrices() const
17✔
260
{
261
  return unit_ghost_cell_matrices_;
17✔
262
}
263

264
const std::vector<CellLBSView>&
265
LBSProblem::GetCellTransportViews() const
190,823✔
266
{
267
  return cell_transport_views_;
190,823✔
268
}
269

270
const UnknownManager&
271
LBSProblem::GetUnknownManager() const
26,703✔
272
{
273
  return flux_moments_uk_man_;
26,703✔
274
}
275

276
size_t
277
LBSProblem::GetLocalNodeCount() const
3,068✔
278
{
279
  return local_node_count_;
3,068✔
280
}
281

282
size_t
283
LBSProblem::GetGlobalNodeCount() const
1,524✔
284
{
285
  return global_node_count_;
1,524✔
286
}
287

288
std::vector<double>&
289
LBSProblem::GetQMomentsLocal()
37,657✔
290
{
291
  return q_moments_local_;
37,657✔
292
}
293

294
const std::vector<double>&
295
LBSProblem::GetQMomentsLocal() const
×
296
{
297
  return q_moments_local_;
×
298
}
299

300
std::vector<double>&
301
LBSProblem::GetExtSrcMomentsLocal()
4✔
302
{
303
  return ext_src_moments_local_;
4✔
304
}
305

306
const std::vector<double>&
307
LBSProblem::GetExtSrcMomentsLocal() const
37,267✔
308
{
309
  return ext_src_moments_local_;
37,267✔
310
}
311

312
std::vector<double>&
313
LBSProblem::GetPhiOldLocal()
57,109✔
314
{
315
  return phi_old_local_;
57,109✔
316
}
317

318
const std::vector<double>&
319
LBSProblem::GetPhiOldLocal() const
×
320
{
321
  return phi_old_local_;
×
322
}
323

324
std::vector<double>&
325
LBSProblem::GetPhiNewLocal()
59,502✔
326
{
327
  return phi_new_local_;
59,502✔
328
}
329

330
const std::vector<double>&
331
LBSProblem::GetPhiNewLocal() const
×
332
{
333
  return phi_new_local_;
×
334
}
335

336
std::vector<double>&
337
LBSProblem::GetPrecursorsNewLocal()
16✔
338
{
339
  return precursor_new_local_;
16✔
340
}
341

342
const std::vector<double>&
343
LBSProblem::GetPrecursorsNewLocal() const
×
344
{
345
  return precursor_new_local_;
×
346
}
347

348
std::vector<double>&
349
LBSProblem::GetDensitiesLocal()
250✔
350
{
351
  return densities_local_;
250✔
352
}
353

354
const std::vector<double>&
355
LBSProblem::GetDensitiesLocal() const
37,267✔
356
{
357
  return densities_local_;
37,267✔
358
}
359

360
SetSourceFunction
361
LBSProblem::GetActiveSetSourceFunction() const
4,142✔
362
{
363
  return active_set_source_function_;
4,142✔
364
}
365

366
std::shared_ptr<AGSLinearSolver>
367
LBSProblem::GetAGSSolver()
296✔
368
{
369
  return ags_solver_;
296✔
370
}
371

372
std::vector<std::shared_ptr<LinearSolver>>&
373
LBSProblem::GetWGSSolvers()
113✔
374
{
375
  return wgs_solvers_;
113✔
376
}
377

378
WGSContext&
379
LBSProblem::GetWGSContext(int groupset_id)
11,472✔
380
{
381
  auto& wgs_solver = wgs_solvers_[groupset_id];
11,472✔
382
  auto raw_context = wgs_solver->GetContext();
11,472✔
383
  auto wgs_context_ptr = std::dynamic_pointer_cast<WGSContext>(raw_context);
11,472✔
384
  OpenSnLogicalErrorIf(not wgs_context_ptr, "Failed to cast WGSContext");
11,472✔
385
  return *wgs_context_ptr;
11,472✔
386
}
22,944✔
387

388
std::map<uint64_t, BoundaryPreference>&
389
LBSProblem::GetBoundaryPreferences()
4✔
390
{
391
  return boundary_preferences_;
4✔
392
}
393

394
std::pair<size_t, size_t>
395
LBSProblem::GetNumPhiIterativeUnknowns()
×
396
{
397
  const auto& sdm = *discretization_;
×
398
  const size_t num_local_phi_dofs = sdm.GetNumLocalDOFs(flux_moments_uk_man_);
×
399
  const size_t num_global_phi_dofs = sdm.GetNumGlobalDOFs(flux_moments_uk_man_);
×
400

401
  return {num_local_phi_dofs, num_global_phi_dofs};
×
402
}
403

404
size_t
405
LBSProblem::MapPhiFieldFunction(size_t g, size_t m) const
23,444✔
406
{
407
  OpenSnLogicalErrorIf(phi_field_functions_local_map_.count({g, m}) == 0,
23,444✔
408
                       std::string("Failure to map phi field function g") + std::to_string(g) +
409
                         " m" + std::to_string(m));
410

411
  return phi_field_functions_local_map_.at({g, m});
23,444✔
412
}
413

414
std::shared_ptr<FieldFunctionGridBased>
415
LBSProblem::GetPowerFieldFunction() const
×
416
{
417
  OpenSnLogicalErrorIf(not options_.power_field_function_on,
×
418
                       "Called when options_.power_field_function_on == false");
419

420
  return field_functions_[power_gen_fieldfunc_local_handle_];
×
421
}
422

423
InputParameters
424
LBSProblem::GetOptionsBlock()
378✔
425
{
426
  InputParameters params;
378✔
427

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

501
  return params;
378✔
502
}
×
503

504
InputParameters
505
LBSProblem::GetBoundaryOptionsBlock()
526✔
506
{
507
  InputParameters params;
526✔
508

509
  params.SetGeneralDescription("Set options for boundary conditions.");
1,052✔
510
  params.AddRequiredParameter<std::string>("name",
1,052✔
511
                                           "Boundary name that identifies the specific boundary");
512
  params.AddRequiredParameter<std::string>("type", "Boundary type specification.");
1,052✔
513
  params.AddOptionalParameterArray<double>("group_strength",
1,052✔
514
                                           {},
515
                                           "Required only if \"type\" is \"isotropic\". An array "
516
                                           "of isotropic strength per group");
517
  params.AddOptionalParameter(
1,052✔
518
    "function_name", "", "Text name of the function to be called for this boundary condition.");
519
  params.ConstrainParameterRange("type",
1,578✔
520
                                 AllowableRangeList::New({"vacuum", "isotropic", "reflecting"}));
526✔
521

522
  return params;
526✔
523
}
×
524

525
InputParameters
526
LBSProblem::GetXSMapEntryBlock()
569✔
527
{
528
  InputParameters params;
569✔
529
  params.SetGeneralDescription("Set the cross-section map for the solver.");
1,138✔
530
  params.AddRequiredParameterArray("block_ids", "Mesh block IDs");
1,138✔
531
  params.AddRequiredParameter<std::shared_ptr<MultiGroupXS>>("xs", "Cross-section object");
1,138✔
532
  return params;
569✔
533
}
×
534

535
void
536
LBSProblem::SetOptions(const InputParameters& input)
188✔
537
{
538
  auto params = LBSProblem::GetOptionsBlock();
188✔
539
  params.AssignParameters(input);
188✔
540

541
  // Handle order insensitive options
542
  for (size_t p = 0; p < params.GetNumParameters(); ++p)
4,136✔
543
  {
544
    const auto& spec = params.GetParam(p);
3,948✔
545

546
    if (spec.GetName() == "max_mpi_message_size")
3,948✔
547
      options_.max_mpi_message_size = spec.GetValue<int>();
188✔
548

549
    else if (spec.GetName() == "restart_writes_enabled")
3,760✔
550
      options_.restart_writes_enabled = spec.GetValue<bool>();
188✔
551

552
    else if (spec.GetName() == "write_delayed_psi_to_restart")
3,572✔
553
      options_.write_delayed_psi_to_restart = spec.GetValue<bool>();
188✔
554

555
    else if (spec.GetName() == "read_restart_path")
3,384✔
556
    {
557
      options_.read_restart_path = spec.GetValue<std::string>();
188✔
558
      if (not options_.read_restart_path.empty())
188✔
559
        options_.read_restart_path += std::to_string(opensn::mpi_comm.rank()) + ".restart.h5";
36✔
560
    }
561

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

569
    else if (spec.GetName() == "write_restart_time_interval")
3,008✔
570
      options_.write_restart_time_interval = std::chrono::seconds(spec.GetValue<int>());
188✔
571

572
    else if (spec.GetName() == "use_precursors")
2,820✔
573
      options_.use_precursors = spec.GetValue<bool>();
188✔
574

575
    else if (spec.GetName() == "use_source_moments")
2,632✔
576
      options_.use_src_moments = spec.GetValue<bool>();
188✔
577

578
    else if (spec.GetName() == "save_angular_flux")
2,444✔
579
      options_.save_angular_flux = spec.GetValue<bool>();
188✔
580

581
    else if (spec.GetName() == "verbose_inner_iterations")
2,256✔
582
      options_.verbose_inner_iterations = spec.GetValue<bool>();
188✔
583

584
    else if (spec.GetName() == "max_ags_iterations")
2,068✔
585
      options_.max_ags_iterations = spec.GetValue<int>();
188✔
586

587
    else if (spec.GetName() == "ags_tolerance")
1,880✔
588
      options_.ags_tolerance = spec.GetValue<double>();
188✔
589

590
    else if (spec.GetName() == "ags_convergence_check")
1,692✔
591
    {
592
      auto check = spec.GetValue<std::string>();
188✔
593
      if (check == "pointwise")
188✔
594
        options_.ags_pointwise_convergence = true;
×
595
    }
188✔
596

597
    else if (spec.GetName() == "verbose_ags_iterations")
1,504✔
598
      options_.verbose_ags_iterations = spec.GetValue<bool>();
188✔
599

600
    else if (spec.GetName() == "verbose_outer_iterations")
1,316✔
601
      options_.verbose_outer_iterations = spec.GetValue<bool>();
188✔
602

603
    else if (spec.GetName() == "power_field_function_on")
1,128✔
604
      options_.power_field_function_on = spec.GetValue<bool>();
188✔
605

606
    else if (spec.GetName() == "power_default_kappa")
940✔
607
      options_.power_default_kappa = spec.GetValue<double>();
188✔
608

609
    else if (spec.GetName() == "power_normalization")
752✔
610
      options_.power_normalization = spec.GetValue<double>();
188✔
611

612
    else if (spec.GetName() == "field_function_prefix_option")
564✔
613
    {
614
      options_.field_function_prefix_option = spec.GetValue<std::string>();
188✔
615
    }
616

617
    else if (spec.GetName() == "field_function_prefix")
376✔
618
      options_.field_function_prefix = spec.GetValue<std::string>();
188✔
619

620
    else if (spec.GetName() == "adjoint")
188✔
621
      options_.adjoint = spec.GetValue<bool>();
188✔
622

623
  } // for p
624

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

646
void
647
LBSProblem::SetBoundaryOptions(const InputParameters& params)
526✔
648
{
649
  const auto boundary_name = params.GetParamValue<std::string>("name");
526✔
650
  const auto bndry_type = params.GetParamValue<std::string>("type");
526✔
651

652
  auto grid = GetGrid();
526✔
653
  const auto bnd_map = grid->GetBoundaryIDMap();
526✔
654
  const auto bnd_name_map = grid->GetBoundaryNameMap();
526✔
655
  auto it = bnd_name_map.find(boundary_name);
526✔
656
  if (it == bnd_name_map.end())
526✔
657
    throw std::runtime_error(std::format("Could not find the specified boundary '{}' - please "
×
658
                                         "check that the 'name' parameter is spelled correctly.",
659
                                         boundary_name));
×
660
  const auto bid = it->second;
526✔
661
  const std::map<std::string, LBSBoundaryType> type_list = {
526✔
662
    {"vacuum", LBSBoundaryType::VACUUM},
526✔
663
    {"isotropic", LBSBoundaryType::ISOTROPIC},
526✔
664
    {"reflecting", LBSBoundaryType::REFLECTING}};
2,104✔
665

666
  const auto type = type_list.at(bndry_type);
526✔
667
  switch (type)
526✔
668
  {
669
    case LBSBoundaryType::VACUUM:
430✔
670
    case LBSBoundaryType::REFLECTING:
430✔
671
    {
430✔
672
      boundary_preferences_[bid] = {type};
430✔
673
      break;
430✔
674
    }
675
    case LBSBoundaryType::ISOTROPIC:
96✔
676
    {
96✔
677
      OpenSnInvalidArgumentIf(not params.Has("group_strength"),
96✔
678
                              "Boundary conditions with type=\"isotropic\" require parameter "
679
                              "\"group_strength\"");
680

681
      params.RequireParameterBlockTypeIs("group_strength", ParameterBlockType::ARRAY);
96✔
682
      const auto group_strength = params.GetParamVectorValue<double>("group_strength");
96✔
683
      boundary_preferences_[bid] = {type, group_strength};
96✔
684
      break;
96✔
685
    }
96✔
686
    case LBSBoundaryType::ARBITRARY:
×
687
    {
×
688
      throw std::runtime_error(GetName() +
×
689
                               ": Arbitrary boundary conditions are not currently supported");
×
690
      break;
526✔
691
    }
692
  }
693
}
2,104✔
694

695
void
696
LBSProblem::Initialize()
312✔
697
{
698
  CALI_CXX_MARK_SCOPE("LBSProblem::Initialize");
312✔
699

700
  PrintSimHeader();
312✔
701
  mpi_comm.barrier();
312✔
702

703
  InitializeSpatialDiscretization();
312✔
704
  InitializeParrays();
312✔
705
  InitializeBoundaries();
312✔
706
  InitializeGPUExtras();
312✔
707
  SetAdjoint(options_.adjoint);
312✔
708

709
  // Initialize point sources
710
  for (auto& point_source : point_sources_)
321✔
711
    point_source->Initialize(*this);
9✔
712

713
  // Initialize volumetric sources
714
  for (auto& volumetric_source : volumetric_sources_)
643✔
715
    volumetric_source->Initialize(*this);
331✔
716
}
312✔
717

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

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

747
    log.Log() << outstr.str() << '\n';
×
748
  }
×
749
}
×
750

751
void
752
LBSProblem::InitializeSources(const InputParameters& params)
312✔
753
{
754
  if (params.Has("volumetric_sources"))
312✔
755
  {
756
    const auto& vol_srcs = params.GetParam("volumetric_sources");
312✔
757
    vol_srcs.RequireBlockTypeIs(ParameterBlockType::ARRAY);
312✔
758
    for (const auto& src : vol_srcs)
643✔
759
      volumetric_sources_.push_back(src.GetValue<std::shared_ptr<VolumetricSource>>());
662✔
760
  }
761

762
  if (params.Has("point_sources"))
312✔
763
  {
764
    const auto& pt_srcs = params.GetParam("point_sources");
312✔
765
    pt_srcs.RequireBlockTypeIs(ParameterBlockType::ARRAY);
312✔
766
    for (const auto& src : pt_srcs)
321✔
767
      point_sources_.push_back(src.GetValue<std::shared_ptr<PointSource>>());
18✔
768
  }
769
}
312✔
770

771
void
772
LBSProblem::InitializeGroupsets(const InputParameters& params)
312✔
773
{
774
  // Initialize groups
775
  if (num_groups_ == 0)
312✔
776
    throw std::invalid_argument(GetName() + ": Number of groups must be > 0");
×
777
  for (size_t g = 0; g < num_groups_; ++g)
20,661✔
778
    groups_.emplace_back(static_cast<int>(g));
20,349✔
779

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

801
void
802
LBSProblem::InitializeXSmapAndDensities(const InputParameters& params)
312✔
803
{
804
  // Build XS map
805
  const auto& xs_array = params.GetParam("xs_map");
312✔
806
  const size_t num_xs = xs_array.GetNumParameters();
312✔
807
  for (size_t i = 0; i < num_xs; ++i)
881✔
808
  {
809
    const auto& item_params = xs_array.GetParam(i);
569✔
810
    InputParameters xs_entry_pars = GetXSMapEntryBlock();
569✔
811
    xs_entry_pars.AssignParameters(item_params);
569✔
812

813
    const auto& block_ids_param = xs_entry_pars.GetParam("block_ids");
569✔
814
    block_ids_param.RequireBlockTypeIs(ParameterBlockType::ARRAY);
569✔
815
    const auto& block_ids = block_ids_param.GetVectorValue<int>();
569✔
816
    auto xs = xs_entry_pars.GetSharedPtrParam<MultiGroupXS>("xs");
569✔
817
    for (const auto& block_id : block_ids)
1,234✔
818
      block_id_to_xs_map_[block_id] = xs;
665✔
819
  }
569✔
820

821
  // Assign placeholder unit densities
822
  densities_local_.assign(grid_->local_cells.size(), 1.0);
312✔
823
}
312✔
824

825
void
826
LBSProblem::InitializeMaterials()
324✔
827
{
828
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeMaterials");
324✔
829

830
  log.Log0Verbose1() << "Initializing Materials";
648✔
831

832
  // Create set of material ids locally relevant
833
  int invalid_mat_cell_count = 0;
324✔
834
  std::set<int> unique_block_ids;
324✔
835
  for (auto& cell : grid_->local_cells)
334,208✔
836
  {
837
    unique_block_ids.insert(cell.block_id);
333,884✔
838
    if (cell.block_id < 0 or (block_id_to_xs_map_.find(cell.block_id) == block_id_to_xs_map_.end()))
333,884✔
839
      ++invalid_mat_cell_count;
×
840
  }
841
  const auto& ghost_cell_ids = grid_->cells.GetGhostGlobalIDs();
324✔
842
  for (uint64_t cell_id : ghost_cell_ids)
48,415✔
843
  {
844
    const auto& cell = grid_->cells[cell_id];
48,091✔
845
    unique_block_ids.insert(cell.block_id);
48,091✔
846
    if (cell.block_id < 0 or (block_id_to_xs_map_.find(cell.block_id) == block_id_to_xs_map_.end()))
48,091✔
847
      ++invalid_mat_cell_count;
×
848
  }
849
  OpenSnLogicalErrorIf(invalid_mat_cell_count > 0,
324✔
850
                       std::to_string(invalid_mat_cell_count) +
851
                         " cells encountered with an invalid material id.");
852

853
  // Get ready for processing
854
  for (const auto& [blk_id, mat] : block_id_to_xs_map_)
1,017✔
855
  {
856
    mat->SetAdjointMode(options_.adjoint);
693✔
857

858
    OpenSnLogicalErrorIf(mat->GetNumGroups() < groups_.size(),
693✔
859
                         "Cross-sections for block \"" + std::to_string(blk_id) +
860
                           "\" have fewer groups (" + std::to_string(mat->GetNumGroups()) +
861
                           ") than the simulation (" + std::to_string(groups_.size()) + "). " +
862
                           "Cross-sections must have at least as many groups as the simulation.");
863
  }
864

865
  // Initialize precursor properties
866
  num_precursors_ = 0;
324✔
867
  max_precursors_per_material_ = 0;
324✔
868
  for (const auto& mat_id_xs : block_id_to_xs_map_)
1,017✔
869
  {
870
    const auto& xs = mat_id_xs.second;
693✔
871
    num_precursors_ += xs->GetNumPrecursors();
693✔
872
    if (xs->GetNumPrecursors() > max_precursors_per_material_)
693✔
873
      max_precursors_per_material_ = xs->GetNumPrecursors();
8✔
874
  }
875

876
  // if no precursors, turn off precursors
877
  if (num_precursors_ == 0)
324✔
878
    options_.use_precursors = false;
316✔
879

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

893
  // Update transport views if available
894
  if (grid_->local_cells.size() == cell_transport_views_.size())
324✔
895
    for (const auto& cell : grid_->local_cells)
10,812✔
896
    {
897
      const auto& xs_ptr = block_id_to_xs_map_[cell.block_id];
10,800✔
898
      auto& transport_view = cell_transport_views_[cell.local_id];
10,800✔
899
      transport_view.ReassignXS(*xs_ptr);
10,800✔
900
    }
901

902
  mpi_comm.barrier();
324✔
903
}
324✔
904

905
void
906
LBSProblem::InitializeSpatialDiscretization()
304✔
907
{
908
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeSpatialDiscretization");
304✔
909

910
  log.Log() << "Initializing spatial discretization.\n";
608✔
911
  discretization_ = PieceWiseLinearDiscontinuous::New(grid_);
304✔
912

913
  ComputeUnitIntegrals();
304✔
914
}
304✔
915

916
void
917
LBSProblem::ComputeUnitIntegrals()
312✔
918
{
919
  CALI_CXX_MARK_SCOPE("LBSProblem::ComputeUnitIntegrals");
312✔
920

921
  log.Log() << "Computing unit integrals.\n";
624✔
922
  const auto& sdm = *discretization_;
312✔
923

924
  const size_t num_local_cells = grid_->local_cells.size();
312✔
925
  unit_cell_matrices_.resize(num_local_cells);
312✔
926

927
  for (const auto& cell : grid_->local_cells)
323,396✔
928
    unit_cell_matrices_[cell.local_id] =
323,084✔
929
      ComputeUnitCellIntegrals(sdm, cell, grid_->GetCoordinateSystem());
323,084✔
930

931
  const auto ghost_ids = grid_->cells.GetGhostGlobalIDs();
312✔
932
  for (auto ghost_id : ghost_ids)
47,647✔
933
    unit_ghost_cell_matrices_[ghost_id] =
47,335✔
934
      ComputeUnitCellIntegrals(sdm, grid_->cells[ghost_id], grid_->GetCoordinateSystem());
94,670✔
935

936
  // Assessing global unit cell matrix storage
937
  std::array<size_t, 2> num_local_ucms = {unit_cell_matrices_.size(),
312✔
938
                                          unit_ghost_cell_matrices_.size()};
312✔
939
  std::array<size_t, 2> num_global_ucms = {0, 0};
312✔
940

941
  mpi_comm.all_reduce(num_local_ucms.data(), 2, num_global_ucms.data(), mpi::op::sum<size_t>());
312✔
942

943
  opensn::mpi_comm.barrier();
312✔
944
  log.Log() << "Ghost cell unit cell-matrix ratio: "
312✔
945
            << (double)num_global_ucms[1] * 100 / (double)num_global_ucms[0] << "%";
624✔
946
  log.Log() << "Cell matrices computed.";
624✔
947
}
312✔
948

949
void
950
LBSProblem::InitializeParrays()
312✔
951
{
952
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeParrays");
312✔
953

954
  log.Log() << "Initializing parallel arrays."
624✔
955
            << " G=" << num_groups_ << " M=" << num_moments_ << std::endl;
312✔
956

957
  // Initialize unknown
958
  // structure
959
  flux_moments_uk_man_.unknowns.clear();
312✔
960
  for (size_t m = 0; m < num_moments_; ++m)
1,178✔
961
  {
962
    flux_moments_uk_man_.AddUnknown(UnknownType::VECTOR_N, groups_.size());
866✔
963
    flux_moments_uk_man_.unknowns.back().name = "m" + std::to_string(m);
866✔
964
  }
965

966
  // Compute local # of dof
967
  local_node_count_ = discretization_->GetNumLocalNodes();
312✔
968
  global_node_count_ = discretization_->GetNumGlobalNodes();
312✔
969

970
  // Compute num of unknowns
971
  size_t num_grps = groups_.size();
312✔
972
  size_t local_unknown_count = local_node_count_ * num_grps * num_moments_;
312✔
973

974
  log.LogAllVerbose1() << "LBS Number of phi unknowns: " << local_unknown_count;
624✔
975

976
  // Size local vectors
977
  q_moments_local_.assign(local_unknown_count, 0.0);
312✔
978
  phi_old_local_.assign(local_unknown_count, 0.0);
312✔
979
  phi_new_local_.assign(local_unknown_count, 0.0);
312✔
980

981
  // Setup precursor vector
982
  if (options_.use_precursors)
312✔
983
  {
984
    size_t num_precursor_dofs = grid_->local_cells.size() * max_precursors_per_material_;
8✔
985
    precursor_new_local_.assign(num_precursor_dofs, 0.0);
8✔
986
  }
987

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

1001
  const Vector3 ihat(1.0, 0.0, 0.0);
312✔
1002
  const Vector3 jhat(0.0, 1.0, 0.0);
312✔
1003
  const Vector3 khat(0.0, 0.0, 1.0);
312✔
1004

1005
  min_cell_dof_count_ = static_cast<size_t>(-1);
312✔
1006
  max_cell_dof_count_ = 0;
312✔
1007
  cell_transport_views_.clear();
312✔
1008
  cell_transport_views_.reserve(grid_->local_cells.size());
312✔
1009
  for (auto& cell : grid_->local_cells)
323,396✔
1010
  {
1011
    size_t num_nodes = discretization_->GetCellNumNodes(cell);
323,084✔
1012

1013
    // compute cell volumes
1014
    double cell_volume = 0.0;
323,084✔
1015
    const auto& IntV_shapeI = unit_cell_matrices_[cell.local_id].intV_shapeI;
323,084✔
1016
    for (size_t i = 0; i < num_nodes; ++i)
2,726,018✔
1017
      cell_volume += IntV_shapeI(i);
2,402,934✔
1018

1019
    size_t cell_phi_address = block_MG_counter;
323,084✔
1020

1021
    const size_t num_faces = cell.faces.size();
323,084✔
1022
    std::vector<bool> face_local_flags(num_faces, true);
323,084✔
1023
    std::vector<int> face_locality(num_faces, opensn::mpi_comm.rank());
323,084✔
1024
    std::vector<const Cell*> neighbor_cell_ptrs(num_faces, nullptr);
323,084✔
1025
    bool cell_on_boundary = false;
323,084✔
1026
    int f = 0;
323,084✔
1027
    for (auto& face : cell.faces)
2,162,202✔
1028
    {
1029
      if (not face.has_neighbor)
1,839,118✔
1030
      {
1031
        cell_on_boundary = true;
61,096✔
1032
        face_local_flags[f] = false;
61,096✔
1033
        face_locality[f] = -1;
61,096✔
1034
      } // if bndry
1035
      else
1036
      {
1037
        const int neighbor_partition = face.GetNeighborPartitionID(grid_.get());
1,778,022✔
1038
        face_local_flags[f] = (neighbor_partition == opensn::mpi_comm.rank());
1,778,022✔
1039
        face_locality[f] = neighbor_partition;
1,778,022✔
1040
        neighbor_cell_ptrs[f] = &grid_->cells[face.neighbor_id];
1,778,022✔
1041
      }
1042

1043
      ++f;
1,839,118✔
1044
    } // for f
1045

1046
    max_cell_dof_count_ = std::max(max_cell_dof_count_, num_nodes);
323,084✔
1047
    min_cell_dof_count_ = std::min(min_cell_dof_count_, num_nodes);
323,084✔
1048
    cell_transport_views_.emplace_back(cell_phi_address,
646,168✔
1049
                                       num_nodes,
1050
                                       num_grps,
1051
                                       num_moments_,
323,084✔
1052
                                       num_faces,
1053
                                       *block_id_to_xs_map_[cell.block_id],
323,084✔
1054
                                       cell_volume,
1055
                                       face_local_flags,
1056
                                       face_locality,
1057
                                       neighbor_cell_ptrs,
1058
                                       cell_on_boundary);
1059
    block_MG_counter += num_nodes * num_grps * num_moments_;
323,084✔
1060
  } // for local cell
323,084✔
1061

1062
  // Populate grid nodal mappings
1063
  // This is used in the Flux Data Structures (FLUDS)
1064
  grid_nodal_mappings_.clear();
312✔
1065
  grid_nodal_mappings_.reserve(grid_->local_cells.size());
312✔
1066
  for (auto& cell : grid_->local_cells)
323,396✔
1067
  {
1068
    CellFaceNodalMapping cell_nodal_mapping;
323,084✔
1069
    cell_nodal_mapping.reserve(cell.faces.size());
323,084✔
1070

1071
    for (auto& face : cell.faces)
2,162,202✔
1072
    {
1073
      std::vector<short> face_node_mapping;
1,839,118✔
1074
      std::vector<short> cell_node_mapping;
1,839,118✔
1075
      int adj_face_idx = -1;
1,839,118✔
1076

1077
      if (face.has_neighbor)
1,839,118✔
1078
      {
1079
        grid_->FindAssociatedVertices(face, face_node_mapping);
1,778,022✔
1080
        grid_->FindAssociatedCellVertices(face, cell_node_mapping);
1,778,022✔
1081
        adj_face_idx = face.GetNeighborAdjacentFaceIndex(grid_.get());
1,778,022✔
1082
      }
1083

1084
      cell_nodal_mapping.emplace_back(adj_face_idx, face_node_mapping, cell_node_mapping);
1,839,118✔
1085
    } // for f
1,839,118✔
1086

1087
    grid_nodal_mappings_.push_back(cell_nodal_mapping);
323,084✔
1088
  } // for local cell
323,084✔
1089

1090
  // Get grid localized communicator set
1091
  grid_local_comm_set_ = grid_->MakeMPILocalCommunicatorSet();
312✔
1092

1093
  // Initialize Field Functions
1094
  InitializeFieldFunctions();
312✔
1095

1096
  opensn::mpi_comm.barrier();
312✔
1097
  log.Log() << "Done with parallel arrays." << std::endl;
624✔
1098
}
312✔
1099

1100
void
1101
LBSProblem::InitializeFieldFunctions()
312✔
1102
{
1103
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeFieldFunctions");
312✔
1104

1105
  if (not field_functions_.empty())
312✔
1106
    return;
×
1107

1108
  // Initialize Field Functions for flux moments
1109
  phi_field_functions_local_map_.clear();
312✔
1110

1111
  for (size_t g = 0; g < groups_.size(); ++g)
20,661✔
1112
  {
1113
    for (size_t m = 0; m < num_moments_; ++m)
94,312✔
1114
    {
1115
      std::string prefix;
73,963✔
1116
      if (options_.field_function_prefix_option == "prefix")
73,963✔
1117
      {
1118
        prefix = options_.field_function_prefix;
73,963✔
1119
        if (not prefix.empty())
73,963✔
1120
          prefix += "_";
1✔
1121
      }
1122
      if (options_.field_function_prefix_option == "solver_name")
73,963✔
1123
        prefix = GetName() + "_";
×
1124

1125
      char buff[100];
73,963✔
1126
      snprintf(
73,963✔
1127
        buff, 99, "%sphi_g%03d_m%02d", prefix.c_str(), static_cast<int>(g), static_cast<int>(m));
1128
      const std::string name = std::string(buff);
73,963✔
1129

1130
      auto group_ff = std::make_shared<FieldFunctionGridBased>(
73,963✔
1131
        name, discretization_, Unknown(UnknownType::SCALAR));
73,963✔
1132

1133
      field_function_stack.push_back(group_ff);
147,926✔
1134
      field_functions_.push_back(group_ff);
73,963✔
1135

1136
      phi_field_functions_local_map_[{g, m}] = field_functions_.size() - 1;
73,963✔
1137
    } // for m
73,963✔
1138
  } // for g
1139

1140
  // Initialize power generation field function
1141
  if (options_.power_field_function_on)
312✔
1142
  {
1143
    std::string prefix;
4✔
1144
    if (options_.field_function_prefix_option == "prefix")
4✔
1145
    {
1146
      prefix = options_.field_function_prefix;
4✔
1147
      if (not prefix.empty())
4✔
1148
        prefix += "_";
×
1149
    }
1150
    if (options_.field_function_prefix_option == "solver_name")
4✔
1151
      prefix = GetName() + "_";
×
1152

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

1156
    field_function_stack.push_back(power_ff);
8✔
1157
    field_functions_.push_back(power_ff);
4✔
1158

1159
    power_gen_fieldfunc_local_handle_ = field_functions_.size() - 1;
4✔
1160
  }
4✔
1161
}
312✔
1162

1163
void
1164
LBSProblem::InitializeSolverSchemes()
312✔
1165
{
1166
  CALI_CXX_MARK_SCOPE("LBSProblem::InitializeSolverSchemes");
312✔
1167

1168
  log.Log() << "Initializing WGS and AGS solvers";
624✔
1169

1170
  InitializeWGSSolvers();
312✔
1171

1172
  ags_solver_ = std::make_shared<AGSLinearSolver>(*this, wgs_solvers_);
312✔
1173
  if (groupsets_.size() == 1)
312✔
1174
  {
1175
    ags_solver_->SetMaxIterations(1);
261✔
1176
    ags_solver_->SetVerbosity(false);
261✔
1177
  }
1178
  else
1179
  {
1180
    ags_solver_->SetMaxIterations(options_.max_ags_iterations);
51✔
1181
    ags_solver_->SetVerbosity(options_.verbose_ags_iterations);
51✔
1182
  }
1183
  ags_solver_->SetTolerance(options_.ags_tolerance);
312✔
1184
}
312✔
1185

1186
#ifndef __OPENSN_USE_CUDA__
1187
void
1188
LBSProblem::InitializeGPUExtras()
312✔
1189
{
1190
}
312✔
1191

1192
void
1193
LBSProblem::ResetGPUCarriers()
308✔
1194
{
1195
}
308✔
1196

1197
void
1198
LBSProblem::CheckCapableDevices()
×
1199
{
1200
}
×
1201
#endif // __OPENSN_USE_CUDA__
1202

1203
std::vector<double>
1204
LBSProblem::MakeSourceMomentsFromPhi()
4✔
1205
{
1206
  CALI_CXX_MARK_SCOPE("LBSProblem::MakeSourceMomentsFromPhi");
4✔
1207

1208
  size_t num_local_dofs = discretization_->GetNumLocalDOFs(flux_moments_uk_man_);
4✔
1209

1210
  std::vector<double> source_moments(num_local_dofs, 0.0);
4✔
1211
  for (auto& groupset : groupsets_)
8✔
1212
  {
1213
    active_set_source_function_(groupset,
4✔
1214
                                source_moments,
1215
                                phi_new_local_,
4✔
1216
                                APPLY_AGS_SCATTER_SOURCES | APPLY_WGS_SCATTER_SOURCES |
1217
                                  APPLY_AGS_FISSION_SOURCES | APPLY_WGS_FISSION_SOURCES);
4✔
1218
  }
1219

1220
  return source_moments;
4✔
1221
}
4✔
1222

1223
void
1224
LBSProblem::UpdateFieldFunctions()
324✔
1225
{
1226
  CALI_CXX_MARK_SCOPE("LBSProblem::UpdateFieldFunctions");
324✔
1227

1228
  const auto& sdm = *discretization_;
324✔
1229
  const auto& phi_uk_man = flux_moments_uk_man_;
324✔
1230

1231
  // Update flux moments
1232
  for (const auto& [g_and_m, ff_index] : phi_field_functions_local_map_)
74,335✔
1233
  {
1234
    const size_t g = g_and_m.first;
74,011✔
1235
    const size_t m = g_and_m.second;
74,011✔
1236

1237
    std::vector<double> data_vector_local(local_node_count_, 0.0);
74,011✔
1238

1239
    for (const auto& cell : grid_->local_cells)
18,279,988✔
1240
    {
1241
      const auto& cell_mapping = sdm.GetCellMapping(cell);
18,205,977✔
1242
      const size_t num_nodes = cell_mapping.GetNumNodes();
18,205,977✔
1243

1244
      for (size_t i = 0; i < num_nodes; ++i)
129,384,485✔
1245
      {
1246
        const auto imapA = sdm.MapDOFLocal(cell, i, phi_uk_man, m, g);
111,178,508✔
1247
        const auto imapB = sdm.MapDOFLocal(cell, i);
111,178,508✔
1248

1249
        data_vector_local[imapB] = phi_new_local_[imapA];
111,178,508✔
1250
      } // for node
1251
    } // for cell
1252

1253
    auto& ff_ptr = field_functions_.at(ff_index);
74,011✔
1254
    ff_ptr->UpdateFieldVector(data_vector_local);
74,011✔
1255
  }
74,011✔
1256

1257
  // Update power generation and scalar flux
1258
  if (options_.power_field_function_on)
324✔
1259
  {
1260
    std::vector<double> data_vector_power_local(local_node_count_, 0.0);
4✔
1261

1262
    double local_total_power = 0.0;
4✔
1263
    for (const auto& cell : grid_->local_cells)
83,268✔
1264
    {
1265
      const auto& cell_mapping = sdm.GetCellMapping(cell);
83,264✔
1266
      const size_t num_nodes = cell_mapping.GetNumNodes();
83,264✔
1267

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

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

1272
      if (not xs->IsFissionable())
83,264✔
1273
        continue;
56,360✔
1274

1275
      for (size_t i = 0; i < num_nodes; ++i)
134,520✔
1276
      {
1277
        const auto imapA = sdm.MapDOFLocal(cell, i);
107,616✔
1278
        const auto imapB = sdm.MapDOFLocal(cell, i, phi_uk_man, 0, 0);
107,616✔
1279

1280
        double nodal_power = 0.0;
1281
        for (size_t g = 0; g < groups_.size(); ++g)
860,928✔
1282
        {
1283
          const double sigma_fg = xs->GetSigmaFission()[g];
753,312✔
1284
          // const double kappa_g = xs->Kappa()[g];
1285
          const double kappa_g = options_.power_default_kappa;
753,312✔
1286

1287
          nodal_power += kappa_g * sigma_fg * phi_new_local_[imapB + g];
753,312✔
1288
        } // for g
1289

1290
        data_vector_power_local[imapA] = nodal_power;
107,616✔
1291
        local_total_power += nodal_power * Vi(i);
107,616✔
1292
      } // for node
1293
    } // for cell
1294

1295
    double scale_factor = 1.0;
4✔
1296
    if (options_.power_normalization > 0.0)
4✔
1297
    {
1298
      double global_total_power = 0.0;
4✔
1299
      mpi_comm.all_reduce(local_total_power, global_total_power, mpi::op::sum<double>());
4✔
1300
      scale_factor = options_.power_normalization / global_total_power;
4✔
1301
      Scale(data_vector_power_local, scale_factor);
4✔
1302
    }
1303

1304
    const size_t ff_index = power_gen_fieldfunc_local_handle_;
4✔
1305

1306
    auto& ff_ptr = field_functions_.at(ff_index);
4✔
1307
    ff_ptr->UpdateFieldVector(data_vector_power_local);
4✔
1308

1309
    // scale scalar flux if neccessary
1310
    if (scale_factor != 1.0)
4✔
1311
    {
1312
      for (size_t g = 0; g < groups_.size(); ++g)
32✔
1313
      {
1314
        const size_t phi_ff_index = phi_field_functions_local_map_.at({g, size_t{0}});
28✔
1315
        auto& phi_ff_ptr = field_functions_.at(phi_ff_index);
28✔
1316
        const auto& phi_vec = phi_ff_ptr->GetLocalFieldVector();
28✔
1317
        std::vector<double> phi_scaled(phi_vec.begin(), phi_vec.end());
28✔
1318
        Scale(phi_scaled, scale_factor);
28✔
1319
        phi_ff_ptr->UpdateFieldVector(phi_scaled);
28✔
1320
      }
28✔
1321
    }
1322
  } // if power enabled
4✔
1323
}
324✔
1324

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc