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

Open-Sn / opensn / 18581001536

16 Oct 2025 03:16PM UTC coverage: 75.124% (-0.06%) from 75.188%
18581001536

push

github

web-flow
Merge pull request #797 from wdhawkins/problem_updates

Minor refactoring of problem classes

140 of 199 new or added lines in 4 files covered. (70.35%)

2 existing lines in 2 files now uncovered.

17996 of 23955 relevant lines covered (75.12%)

54568204.05 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/discrete_ordinates_problem/sweep/sweep.h"
8
#include "modules/linear_boltzmann_solvers/lbs_problem/groupset/lbs_groupset.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 "framework/math/spatial_discretization/spatial_discretization.h"
13
#include "framework/mesh/mesh_continuum/mesh_continuum.h"
14
#include "framework/math/linear_solver/linear_solver.h"
15
#include "framework/math/spatial_discretization/finite_element/unit_cell_matrices.h"
16
#include <petscksp.h>
17
#include <any>
18
#include <chrono>
19

20
namespace opensn
21
{
22

23
class MPICommunicatorSet;
24
class GridFaceHistogram;
25
class TimeIntegration;
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
  explicit LBSProblem(const std::string& name, std::shared_ptr<MeshContinuum> grid);
35

36
  /// Input parameters based construction.
37
  explicit LBSProblem(const InputParameters& params);
38

39
  LBSProblem(const LBSProblem&) = delete;
40

41
  LBSProblem& operator=(const LBSProblem&) = delete;
42

43
  ~LBSProblem() override;
44

45
  /// Returns a reference to the solver options.
46
  LBSOptions& GetOptions();
47

48
  /// Returns a constant reference to the solver options.
49
  const LBSOptions& GetOptions() const;
50

51
  void SetOptions(const InputParameters& input);
52

53
  void SetBoundaryOptions(const InputParameters& params);
54

55
  void SetAdjoint(bool adjoint);
56

57
  /// Returns the number of moments for the solver. This will only be non-zero after initialization.
58
  size_t GetNumMoments() const;
59

60
  /// Returns the number of groups for the solver. This will only be non-zero after initialization.
61
  size_t GetNumGroups() const;
62

63
  /// Returns the scattering order for the solver. This will only be non-zero after initialization.
64
  unsigned int GetScatteringOrder() const;
65

66
  /**
67
   * Returns the number of precursors for the solver. This will only be non-zero after
68
   * initialization.
69
   */
70
  size_t GetNumPrecursors() const;
71

72
  /**
73
   * Returns the maximum number of precursors defined on any material. This will only be non-zero
74
   * after initialization.
75
   */
76
  size_t GetMaxPrecursorsPerMaterial() const;
77

78
  const std::vector<LBSGroup>& GetGroups() const;
79

80
  std::vector<LBSGroupset>& GetGroupsets();
81

82
  const std::vector<LBSGroupset>& GetGroupsets() const;
83

84
  /// Adds a point source to the solver.
85
  void AddPointSource(std::shared_ptr<PointSource> point_source);
86

87
  /// Clears all the point sources from the solver.
88
  void ClearPointSources();
89

90
  /// Constant accessor to the list of point sources.
91
  const std::vector<std::shared_ptr<PointSource>>& GetPointSources() const;
92

93
  /// Adds a volumetric source to the solver.
94
  void AddVolumetricSource(std::shared_ptr<VolumetricSource> volumetric_source);
95

96
  /// Clears all the volumetric sources from the solver.
97
  void ClearVolumetricSources();
98

99
  /// Constant accessor to the list of volumetric sources.
100
  const std::vector<std::shared_ptr<VolumetricSource>>& GetVolumetricSources() const;
101

102
  /// Clears all the boundary conditions from the solver.
103
  void ClearBoundaries();
104

105
  size_t& GetLastRestartTime();
106

107
  /// Returns a reference to the map of material ids to XSs.
108
  const std::map<int, std::shared_ptr<MultiGroupXS>>& GetMatID2XSMap() const;
109

110
  /// Obtains a reference to the grid.
111
  std::shared_ptr<MeshContinuum> GetGrid() const;
112

113
  /// Get pointer to carriers.
114
  void* GetCarrier(std::uint32_t idx) { return carriers_.at(idx); }
115

116
  /// Get pointer to pinners.
117
  void* GetPinner(std::uint32_t idx) { return pinners_.at(idx); }
118

119
  /// Obtains a reference to the spatial discretization.
120
  const SpatialDiscretization& GetSpatialDiscretization() const;
121

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

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

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

131
  /// Read/Write access to the boundary preferences.
132
  std::map<uint64_t, BoundaryPreference>& GetBoundaryPreferences();
133

134
  /// Obtains a reference to the unknown manager for flux-moments.
135
  const UnknownManager& GetUnknownManager() const;
136

137
  /// Returns the local node count for the flux-moments data structures.
138
  size_t GetLocalNodeCount() const;
139

140
  /// Returns the global node count for the flux-moments data structures.
141
  size_t GetGlobalNodeCount() const;
142

143
  /// Read/write access to source moments vector.
144
  std::vector<double>& GetQMomentsLocal();
145

146
  /// Read access to source moments vector.
147
  const std::vector<double>& GetQMomentsLocal() const;
148

149
  /// Read/write access to exterior src moments vector.
150
  std::vector<double>& GetExtSrcMomentsLocal();
151

152
  /// Read access to exterior src moments vector.
153
  const std::vector<double>& GetExtSrcMomentsLocal() const;
154

155
  /// Read/write access to last updated flux vector.
156
  std::vector<double>& GetPhiOldLocal();
157

158
  /// Read access to last updated flux vector.
159
  const std::vector<double>& GetPhiOldLocal() const;
160

161
  /// Read/write access to newest updated flux vector.
162
  std::vector<double>& GetPhiNewLocal();
163

164
  /// Read access to newest updated flux vector.
165
  const std::vector<double>& GetPhiNewLocal() const;
166

167
  /// Read/write access to newest updated precursors vector.
168
  std::vector<double>& GetPrecursorsNewLocal();
169

170
  /// Read access to newest updated precursors vector.
171
  const std::vector<double>& GetPrecursorsNewLocal() const;
172

173
  /// Read/write access to the cell-wise densities.
174
  std::vector<double>& GetDensitiesLocal();
175

176
  /// Read access to the cell-wise densities.
177
  const std::vector<double>& GetDensitiesLocal() const;
178

179
  SetSourceFunction GetActiveSetSourceFunction() const;
180

181
  std::shared_ptr<AGSLinearSolver> GetAGSSolver();
182

183
  std::vector<std::shared_ptr<LinearSolver>>& GetWGSSolvers();
184

185
  WGSContext& GetWGSContext(int groupset_id);
186

187
  /**
188
   * Gets the local and global number of iterative unknowns. This normally is only the flux moments,
189
   * however, the sweep based solvers might include delayed angular fluxes in this number.
190
   */
191
  virtual std::pair<size_t, size_t> GetNumPhiIterativeUnknowns();
192

193
  /// Gets the local handle of a flux-moment based field function.
194
  size_t MapPhiFieldFunction(size_t g, size_t m) const;
195

196
  /// Returns the power generation field function, if enabled.
197
  std::shared_ptr<FieldFunctionGridBased> GetPowerFieldFunction() const;
198

199
  void Initialize() override;
200

UNCOV
201
  bool TriggerRestartDump() const
×
202
  {
203
    if (options_.write_restart_time_interval <= std::chrono::seconds(0))
×
204
      return false;
205

206
    auto elapsed = std::chrono::system_clock::now() - options_.last_restart_write_time;
×
207
    return elapsed >= options_.write_restart_time_interval;
×
208
  }
209

210
  void UpdateRestartWriteTime()
×
211
  {
212
    options_.last_restart_write_time = std::chrono::system_clock::now();
×
213
  }
214

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

218
  /// Copy relevant section of phi_old to the field functions.
219
  void UpdateFieldFunctions();
220

221
  /// Sets the internal phi vector to the value in the associated field function.
222
  void SetPhiFromFieldFunctions(PhiSTLOption which_phi,
223
                                const std::vector<size_t>& m_indices,
224
                                const std::vector<size_t>& g_indices);
225

226
  /**
227
   * A method for post-processing an adjoint solution.
228
   *
229
   * @note This does nothing for diffusion-based solvers.
230
   */
231
  virtual void ReorientAdjointSolution() {};
×
232

233
private:
234
  /// Initialize groupsets
235
  void InitializeGroupsets(const InputParameters& params);
236

237
  /// Initializes materials
238
  void InitializeXSmapAndDensities(const InputParameters& params);
239
  void InitializeMaterials();
240

241
  /// Initialize sources
242
  void InitializeSources(const InputParameters& params);
243

244
  /// Initialize boundary conditions
245
  void InitializeBoundaryConditions(const InputParameters& params);
246

247
protected:
248
  virtual void PrintSimHeader();
249

250
  void ComputeUnitIntegrals();
251

252
  virtual void InitializeSpatialDiscretization();
253

254
  /// Initializes parallel arrays.
255
  void InitializeParrays();
256

257
  void InitializeFieldFunctions();
258

259
  /// Initializes boundaries.
260
  virtual void InitializeBoundaries() {}
×
261

262
  void InitializeSolverSchemes();
263

264
  virtual void InitializeWGSSolvers() {};
×
265

266
  /// Initializes data carriers to GPUs and memory pinner.
267
  void InitializeGPUExtras();
268

269
  /// Reset data carriers to null and unpin memory.
270
  void ResetGPUCarriers();
271

272
  virtual void ZeroSolutions() = 0;
273

274
  LBSOptions options_;
275
  size_t num_moments_ = 0;
276
  size_t num_groups_ = 0;
277
  unsigned int scattering_order_ = 0;
278
  size_t num_precursors_ = 0;
279
  size_t max_precursors_per_material_ = 0;
280

281
  std::vector<LBSGroup> groups_;
282
  std::vector<LBSGroupset> groupsets_;
283

284
  std::map<int, std::shared_ptr<MultiGroupXS>> block_id_to_xs_map_;
285

286
  std::vector<std::shared_ptr<PointSource>> point_sources_;
287
  std::vector<std::shared_ptr<VolumetricSource>> volumetric_sources_;
288

289
  std::shared_ptr<MeshContinuum> grid_;
290
  std::shared_ptr<SpatialDiscretization> discretization_ = nullptr;
291

292
  std::vector<CellFaceNodalMapping> grid_nodal_mappings_;
293
  std::shared_ptr<MPICommunicatorSet> grid_local_comm_set_ = nullptr;
294

295
  std::vector<UnitCellMatrices> unit_cell_matrices_;
296
  std::map<uint64_t, UnitCellMatrices> unit_ghost_cell_matrices_;
297
  std::vector<CellLBSView> cell_transport_views_;
298

299
  std::map<uint64_t, BoundaryPreference> boundary_preferences_;
300

301
  UnknownManager flux_moments_uk_man_;
302

303
  size_t max_cell_dof_count_ = 0;
304
  size_t min_cell_dof_count_ = 0;
305
  uint64_t local_node_count_ = 0;
306
  uint64_t global_node_count_ = 0;
307

308
  std::vector<double> q_moments_local_, ext_src_moments_local_;
309
  std::vector<double> phi_new_local_, phi_old_local_;
310
  std::vector<double> precursor_new_local_;
311
  std::vector<double> densities_local_;
312

313
  SetSourceFunction active_set_source_function_;
314

315
  std::shared_ptr<AGSLinearSolver> ags_solver_;
316
  std::vector<std::shared_ptr<LinearSolver>> wgs_solvers_;
317

318
  std::map<std::pair<size_t, size_t>, size_t> phi_field_functions_local_map_;
319
  size_t power_gen_fieldfunc_local_handle_ = 0;
320

321
  /// Time integration parameter meant to be set by an executor
322
  std::shared_ptr<const TimeIntegration> time_integration_ = nullptr;
323

324
  /**
325
   * @brief Data carriers for necessary data to run the sweep on GPU.
326
   * @details These objects manage GPU memory allocation automatically, organize cross-section,
327
   * outflow, and mesh data into contiguous memory on the CPU, and handle copying it to the GPU.
328
   *
329
   * There are 3 carriers, respectively for cross sections, outflow and mesh.
330
   */
331
  std::array<void*, 3> carriers_ = {nullptr, nullptr, nullptr};
332

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

336
  /// Flag indicating if GPU acceleration is enabled.
337
  bool use_gpus_;
338

339
  /// Checks if the current CPU is associated with any GPU.
340
  static void CheckCapableDevices();
341

342
public:
343
  static std::map<std::string, uint64_t> supported_boundary_names;
344
  static std::map<uint64_t, std::string> supported_boundary_ids;
345

346
  /// Returns the input parameters for this object.
347
  static InputParameters GetInputParameters();
348

349
  static InputParameters GetOptionsBlock();
350

351
  static InputParameters GetBoundaryOptionsBlock();
352

353
  static InputParameters GetXSMapEntryBlock();
354
};
355

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