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

Open-Sn / opensn / 22701806567

04 Mar 2026 02:49PM UTC coverage: 74.311% (+0.04%) from 74.268%
22701806567

push

github

web-flow
Merge pull request #957 from wdhawkins/lbs_problem_api

Refactor LBSProblem for const-correct API, reduced visibility, and unified error reporting

66 of 93 new or added lines in 17 files covered. (70.97%)

3 existing lines in 2 files now uncovered.

20009 of 26926 relevant lines covered (74.31%)

67212610.44 hits per line

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

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

4
#pragma once
5

6
#include "modules/problem.h"
7
#include "modules/linear_boltzmann_solvers/lbs_problem/groupset/lbs_groupset.h"
8
#include "modules/linear_boltzmann_solvers/lbs_problem/source_functions/source_flags.h"
9
#include "modules/linear_boltzmann_solvers/lbs_problem/point_source/point_source.h"
10
#include "modules/linear_boltzmann_solvers/lbs_problem/volumetric_source/volumetric_source.h"
11
#include "modules/linear_boltzmann_solvers/lbs_problem/lbs_structs.h"
12
#include "modules/linear_boltzmann_solvers/lbs_problem/lbs_view.h"
13
#include "framework/math/spatial_discretization/spatial_discretization.h"
14
#include "framework/mesh/mesh_continuum/mesh_continuum.h"
15
#include "framework/math/linear_solver/linear_solver.h"
16
#include "framework/math/spatial_discretization/finite_element/unit_cell_matrices.h"
17
#include "framework/math/geometry.h"
18
#include <petscksp.h>
19
#include <chrono>
20

21
namespace opensn
22
{
23

24
class MPICommunicatorSet;
25
class GridFaceHistogram;
26
class AGSLinearSolver;
27
class WGSLinearSolver;
28
struct WGSContext;
29

30
/// Base class for all Linear Boltzmann Solvers.
31
class LBSProblem : public Problem
32
{
33
public:
34
  LBSProblem(const LBSProblem&) = delete;
35

36
  LBSProblem& operator=(const LBSProblem&) = delete;
37

38
  ~LBSProblem() override;
39

40
  /// Returns a constant reference to the solver options.
41
  const LBSOptions& GetOptions() const;
42

43
  void SetOptions(const InputParameters& input);
44

45
  /// Returns simulation time in seconds for time dependent problems.
46
  double GetTime() const;
47

48
  /// Sets simulation time in seconds for time dependent problems.
49
  void SetTime(double time);
50

51
  /// Sets dt.
52
  void SetTimeStep(double dt);
53

54
  /// Returns dt.
55
  double GetTimeStep() const;
56

57
  /// Sets theta for time discretization.
58
  void SetTheta(double theta);
59

60
  /// Returns theta for time discretization.
61
  double GetTheta() const;
62

63
  /// Returns true if the problem is currently in time-dependent mode.
64
  virtual bool IsTimeDependent() const;
65

66
  /// Set the problem to time-dependent mode.
67
  virtual void SetTimeDependentMode();
68

69
  /// Set the problem to steady-state mode.
70
  virtual void SetSteadyStateMode();
71

72
  /**
73
   * Toggle forward/adjoint transport mode.
74
   *
75
   * If the requested mode differs from the current mode, this performs a
76
   * mode-transition reset. Materials are reinitialized in the selected mode,
77
   * sources and boundaries are cleared, and solution vectors are zeroed.
78
   */
79
  void SetAdjoint(bool adjoint);
80

81
  /// Returns true if the problem is in adjoint mode.
82
  bool IsAdjoint() const;
83

84
  virtual void SetSaveAngularFlux(bool save);
85

86
  void ApplyOptions();
87

88
  void ZeroPhi();
89

90
  virtual void ZeroPsi() = 0;
91

92
  GeometryType GetGeometryType() const;
93

94
  /// Returns the number of moments for the solver.
95
  unsigned int GetNumMoments() const;
96

97
  unsigned int GetMaxCellDOFCount() const;
98

99
  unsigned int GetMinCellDOFCount() const;
100

101
  bool UseGPUs() const;
102

103
  /// Returns the number of groups for the solver.
104
  unsigned int GetNumGroups() const;
105

106
  /// Returns the scattering order for the solver.
107
  unsigned int GetScatteringOrder() const;
108

109
  /// Returns the number of precursors for the solver.
110
  unsigned int GetNumPrecursors() const;
111

112
  /// Returns the maximum number of precursors defined on any material.
113
  unsigned int GetMaxPrecursorsPerMaterial() const;
114

115
  const std::vector<LBSGroupset>& GetGroupsets() const;
116
  LBSGroupset& GetGroupset(size_t groupset_id);
117
  const LBSGroupset& GetGroupset(size_t groupset_id) const;
118
  size_t GetNumGroupsets() const;
119

120
  /// Adds a point source to the solver.
121
  void AddPointSource(std::shared_ptr<PointSource> point_source);
122

123
  /// Clears all the point sources from the solver.
124
  void ClearPointSources();
125

126
  /// Constant accessor to the list of point sources.
127
  const std::vector<std::shared_ptr<PointSource>>& GetPointSources() const;
128

129
  /// Adds a volumetric source to the solver.
130
  void AddVolumetricSource(std::shared_ptr<VolumetricSource> volumetric_source);
131

132
  /// Clears all the volumetric sources from the solver.
133
  void ClearVolumetricSources();
134

135
  /// Constant accessor to the list of volumetric sources.
136
  const std::vector<std::shared_ptr<VolumetricSource>>& GetVolumetricSources() const;
137

138
  /// Clears all the boundary conditions from the solver.
139
  virtual void ClearBoundaries() = 0;
140

141
  /// Returns a reference to the map of material ids to XSs.
142
  const BlockID2XSMap& GetBlockID2XSMap() const;
143

144
  /// Replaces the map of block ids to XSs and refreshes material data.
145
  void SetBlockID2XSMap(const BlockID2XSMap& xs_map);
146

147
  /// Obtains a reference to the grid.
148
  std::shared_ptr<MeshContinuum> GetGrid() const;
149

150
  /// Get pointer to carriers.
151
  void* GetCarrier(std::uint32_t idx) { return carriers_.at(idx); }
152

153
  /// Get pointer to pinners.
154
  void* GetPinner(std::uint32_t idx) { return pinners_.at(idx); }
155

156
  /// Obtains a reference to the spatial discretization.
157
  const SpatialDiscretization& GetSpatialDiscretization() const;
158

159
  /// Returns read-only access to the unit cell matrices.
160
  const std::vector<UnitCellMatrices>& GetUnitCellMatrices() const;
161

162
  /// Returns read-only access to the unit ghost cell matrices.
163
  const std::map<uint64_t, UnitCellMatrices>& GetUnitGhostCellMatrices() const;
164

165
  /// Returns a reference to the list of local cell transport views.
166
  std::vector<CellLBSView>& GetCellTransportViews();
167

168
  /// Returns a const reference to the list of local cell transport views.
169
  const std::vector<CellLBSView>& GetCellTransportViews() const;
170

171
  /// Obtains a reference to the unknown manager for flux-moments.
172
  const UnknownManager& GetUnknownManager() const;
173

174
  /// Returns the local node count for the flux-moments data structures.
175
  size_t GetLocalNodeCount() const;
176

177
  /// Returns the global node count for the flux-moments data structures.
178
  size_t GetGlobalNodeCount() const;
179

180
  /// Read/write access to source moments vector.
181
  std::vector<double>& GetQMomentsLocal();
182

183
  /// Read access to source moments vector.
184
  const std::vector<double>& GetQMomentsLocal() const;
185

186
  /// Read/write access to exterior src moments vector.
187
  std::vector<double>& GetExtSrcMomentsLocal();
188

189
  /// Read access to exterior src moments vector.
190
  const std::vector<double>& GetExtSrcMomentsLocal() const;
191

192
  /// Read/write access to last updated flux vector.
193
  std::vector<double>& GetPhiOldLocal();
194

195
  /// Read access to last updated flux vector.
196
  const std::vector<double>& GetPhiOldLocal() const;
197

198
  /// Read/write access to newest updated flux vector.
199
  std::vector<double>& GetPhiNewLocal();
200

201
  /// Read access to newest updated flux vector.
202
  const std::vector<double>& GetPhiNewLocal() const;
203

204
  /// Read/write access to newest updated precursors vector.
205
  std::vector<double>& GetPrecursorsNewLocal();
206

207
  /// Read access to newest updated precursors vector.
208
  const std::vector<double>& GetPrecursorsNewLocal() const;
209

210
  /// Read/write access to the cell-wise densities.
211
  std::vector<double>& GetDensitiesLocal();
212

213
  /// Read access to the cell-wise densities.
214
  const std::vector<double>& GetDensitiesLocal() const;
215

216
  SetSourceFunction GetActiveSetSourceFunction() const;
217

218
  std::shared_ptr<AGSLinearSolver> GetAGSSolver() const;
219

220
  std::shared_ptr<LinearSolver> GetWGSSolver(size_t groupset_id) const;
221
  size_t GetNumWGSSolvers() const;
222

223
  WGSContext& GetWGSContext(int groupset_id);
224

225
  /**
226
   * Gets the local and global number of iterative unknowns. This normally is only the flux moments,
227
   * however, the sweep based solvers might include delayed angular fluxes in this number.
228
   */
229
  virtual std::pair<size_t, size_t> GetNumPhiIterativeUnknowns();
230

231
  /// Returns a flux-moment field function for a given group and moment.
232
  std::shared_ptr<FieldFunctionGridBased> GetScalarFluxFieldFunction(unsigned int g,
233
                                                                     unsigned int m = 0) const;
234

235
  /// Returns the power generation field function, if enabled.
236
  std::shared_ptr<FieldFunctionGridBased> GetPowerFieldFunction() const;
237

238
  bool TriggerRestartDump() const
239
  {
240
    if (options_.write_restart_time_interval <= std::chrono::seconds(0))
241
      return false;
242

243
    auto elapsed = std::chrono::system_clock::now() - options_.last_restart_write_time;
244
    return elapsed >= options_.write_restart_time_interval;
245
  }
246

247
  void UpdateRestartWriteTime()
×
248
  {
249
    options_.last_restart_write_time = std::chrono::system_clock::now();
×
250
  }
251

252
  /// Makes a source-moments vector from scattering and fission based on the latest phi-solution.
253
  std::vector<double> MakeSourceMomentsFromPhi();
254

255
  /// Copy relevant section of phi_old to the field functions.
256
  void UpdateFieldFunctions();
257

258
  /// Sets the internal phi vector to the value in the associated field function.
259
  void SetPhiFromFieldFunctions(PhiSTLOption which_phi,
260
                                const std::vector<unsigned int>& m_indices,
261
                                const std::vector<unsigned int>& g_indices);
262

263
  /**
264
   * A method for post-processing an adjoint solution.
265
   *
266
   * @note This does nothing for diffusion-based solvers.
267
   */
268
  virtual void ReorientAdjointSolution() {};
×
269

270
  virtual void UpdatePsiOld() {};
×
271

272
protected:
273
  /// Input parameters based construction.
274
  explicit LBSProblem(const InputParameters& params);
275

276
  /// Build runtime data structures once all constructor-time configuration is complete.
277
  void BuildRuntime();
278

279
  virtual void PrintSimHeader();
280

281
  void ComputeUnitIntegrals();
282

283
  virtual void InitializeSpatialDiscretization();
284

285
  /// Initializes boundaries.
UNCOV
286
  virtual void InitializeBoundaries() {}
×
287

288
  /// Derived problems handle boundary options.
289
  virtual void SetBoundaryOptions(const InputParameters& params) = 0;
290

291
  void InitializeSolverSchemes();
292

293
  virtual void InitializeWGSSolvers() {};
×
294

×
NEW
295
  void SetActiveSetSourceFunction(SetSourceFunction source_function);
×
296

×
UNCOV
297
  LBSOptions options_;
×
298
  double time_ = 0.0;
299
  double theta_ = 1.0;
300
  double dt_ = 1.0;
301
  GeometryType geometry_type_ = GeometryType::INVALID;
302
  unsigned int num_moments_ = 0;
303
  unsigned int num_groups_ = 0;
304
  unsigned int scattering_order_ = 0;
305
  unsigned int num_precursors_ = 0;
306
  unsigned int max_precursors_per_material_ = 0;
307

308
  std::vector<LBSGroupset> groupsets_;
309

310
  BlockID2XSMap block_id_to_xs_map_;
311

312
  std::vector<std::shared_ptr<PointSource>> point_sources_;
313
  std::vector<std::shared_ptr<VolumetricSource>> volumetric_sources_;
314

315
  std::shared_ptr<MeshContinuum> grid_;
316
  std::shared_ptr<SpatialDiscretization> discretization_ = nullptr;
317

318
  std::vector<CellFaceNodalMapping> grid_nodal_mappings_;
319
  std::shared_ptr<MPICommunicatorSet> grid_local_comm_set_ = nullptr;
320

321
  std::vector<UnitCellMatrices> unit_cell_matrices_;
322
  std::map<uint64_t, UnitCellMatrices> unit_ghost_cell_matrices_;
323
  std::vector<CellLBSView> cell_transport_views_;
324

325
  UnknownManager flux_moments_uk_man_;
326

327
  unsigned int max_cell_dof_count_ = 0;
328
  unsigned int min_cell_dof_count_ = 0;
329
  uint64_t local_node_count_ = 0;
330
  uint64_t global_node_count_ = 0;
331

332
  std::vector<double> q_moments_local_, ext_src_moments_local_;
333
  std::vector<double> phi_new_local_, phi_old_local_;
334
  std::vector<double> precursor_new_local_;
335
  std::vector<double> densities_local_;
336

337
  SetSourceFunction active_set_source_function_;
338

339
  std::shared_ptr<AGSLinearSolver> ags_solver_;
340
  std::vector<std::shared_ptr<LinearSolver>> wgs_solvers_;
341
  bool initialized_ = false;
342

343
  std::map<std::pair<unsigned int, unsigned int>, size_t> phi_field_functions_local_map_;
344
  size_t power_gen_fieldfunc_local_handle_ = 0;
345

346
  /**
347
   * \brief Data carriers for necessary data to run the sweep on GPU.
348
   * \details These objects manage GPU memory allocation automatically, organize cross-section,
349
   * outflow, and mesh data into contiguous memory on the CPU, and handle copying it to the GPU.
350
   *
351
   * There are 3 carriers, respectively for cross sections, outflow and mesh.
352
   */
353
  std::array<void*, 3> carriers_ = {nullptr, nullptr, nullptr};
354

355
  /// Memory pinner for source moments and destination phi.
356
  std::array<void*, 2> pinners_ = {nullptr, nullptr};
357

358
  /// Flag indicating if GPU acceleration is enabled.
359
  bool use_gpus_;
360
  bool applied_adjoint_ = false;
361
  bool applied_save_angular_flux_ = false;
362

363
private:
364
  void InitializeRuntimeCore();
365
  void ValidateRuntimeModeConfiguration() const;
366
  void InitializeSources();
367
  /// Initializes parallel arrays.
368
  void InitializeParrays();
369
  void InitializeFieldFunctions();
370
  /// Initializes data carriers to GPUs and memory pinner.
371
  void InitializeGPUExtras();
372
  /// Reset data carriers to null and unpin memory.
373
  void ResetGPUCarriers();
374

375
  /// Initialize groupsets
376
  void InitializeGroupsets(const InputParameters& params);
377

378
  /// Initializes materials
379
  void InitializeXSmapAndDensities(const InputParameters& params);
380
  void InitializeMaterials();
381

382
  /// Initialize sources
383
  void InitializeSources(const InputParameters& params);
384

385
  /// Parse and validate options without applying runtime side-effects.
386
  void ParseOptions(const InputParameters& input);
387
  /// Checks if the current CPU is associated with any GPU.
388
  static void CheckCapableDevices();
389

390
public:
391
  /// Max number of DOFs per cell that the sweep kernel on GPU can handle.
392
  static constexpr std::uint32_t max_dofs_gpu = 10;
393

394
  /// Returns the input parameters for this object.
395
  static InputParameters GetInputParameters();
396

397
  static InputParameters GetOptionsBlock();
398

399
  static InputParameters GetXSMapEntryBlock();
400
};
401

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