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

Open-Sn / opensn / 18211469908

02 Oct 2025 06:19PM UTC coverage: 75.017% (-0.004%) from 75.021%
18211469908

push

github

web-flow
Merge pull request #787 from andrsd/n-grp-type

Number of groups is stored as `size_t`

1 of 1 new or added line in 1 file covered. (100.0%)

264 existing lines in 10 files now uncovered.

17812 of 23744 relevant lines covered (75.02%)

47075299.24 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
  static InputParameters GetOptionsBlock();
52

53
  static InputParameters GetBoundaryOptionsBlock();
54

55
  static InputParameters GetXSMapEntryBlock();
56

57
  void SetOptions(const InputParameters& input);
58

59
  void SetBoundaryOptions(const InputParameters& params);
60

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

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

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

70
  /**
71
   * Returns the number of precursors for the solver. This will only be non-zero after
72
   * initialization.
73
   */
74
  size_t GetNumPrecursors() const;
75

76
  /**
77
   * Returns the maximum number of precursors defined on any material. This will only be non-zero
78
   * after initialization.
79
   */
80
  size_t GetMaxPrecursorsPerMaterial() const;
81

82
  /**
83
   * Adds a group to the list of groups. If group id < 0, the id will be logically derived from the
84
   * list size. If >= 0 the id will be set to the id specified.
85
   */
86
  void AddGroup(int id);
87

88
  const std::vector<LBSGroup>& GetGroups() const;
89

90
  /**
91
   * Adds a groupset to the list of groupsets. The groupset id will be logically derived from the
92
   * list size.
93
   */
94
  void AddGroupset();
95

96
  std::vector<LBSGroupset>& GetGroupsets();
97

98
  const std::vector<LBSGroupset>& GetGroupsets() const;
99

100
  /// Adds a point source to the solver.
101
  void AddPointSource(std::shared_ptr<PointSource> point_source);
102

103
  /// Clears all the point sources from the solver.
104
  void ClearPointSources();
105

106
  /// Constant accessor to the list of point sources.
107
  const std::vector<std::shared_ptr<PointSource>>& GetPointSources() const;
108

109
  /// Adds a volumetric source to the solver.
110
  void AddVolumetricSource(std::shared_ptr<VolumetricSource> volumetric_source);
111

112
  /// Clears all the volumetric sources from the solver.
113
  void ClearVolumetricSources();
114

115
  /// Constant accessor to the list of volumetric sources.
116
  const std::vector<std::shared_ptr<VolumetricSource>>& GetVolumetricSources() const;
117

118
  /// Clears all the boundary conditions from the solver.
119
  void ClearBoundaries();
120

121
  size_t& GetLastRestartTime();
122

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

126
  /// Obtains a reference to the grid.
127
  std::shared_ptr<MeshContinuum> GetGrid() const;
128

129
  /// Get pointer to carriers.
130
  void* GetCarrier(std::uint32_t idx) { return carriers_.at(idx); }
131

132
  /// Get pointer to pinners.
133
  void* GetPinner(std::uint32_t idx) { return pinners_.at(idx); }
134

135
  /// Obtains a reference to the spatial discretization.
136
  const SpatialDiscretization& GetSpatialDiscretization() const;
137

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

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

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

147
  /// Read/Write access to the boundary preferences.
148
  std::map<uint64_t, BoundaryPreference>& GetBoundaryPreferences();
149

150
  /// Obtains a reference to the unknown manager for flux-moments.
151
  const UnknownManager& GetUnknownManager() const;
152

153
  /// Returns the local node count for the flux-moments data structures.
154
  size_t GetLocalNodeCount() const;
155

156
  /// Returns the global node count for the flux-moments data structures.
157
  size_t GetGlobalNodeCount() const;
158

159
  /// Read/write access to source moments vector.
160
  std::vector<double>& GetQMomentsLocal();
161

162
  /// Read access to source moments vector.
163
  const std::vector<double>& GetQMomentsLocal() const;
164

165
  /// Read/write access to exterior src moments vector.
166
  std::vector<double>& GetExtSrcMomentsLocal();
167

168
  /// Read access to exterior src moments vector.
169
  const std::vector<double>& GetExtSrcMomentsLocal() const;
170

171
  /// Read/write access to last updated flux vector.
172
  std::vector<double>& GetPhiOldLocal();
173

174
  /// Read access to last updated flux vector.
175
  const std::vector<double>& GetPhiOldLocal() const;
176

177
  /// Read/write access to newest updated flux vector.
178
  std::vector<double>& GetPhiNewLocal();
179

180
  /// Read access to newest updated flux vector.
181
  const std::vector<double>& GetPhiNewLocal() const;
182

183
  /// Read/write access to newest updated precursors vector.
184
  std::vector<double>& GetPrecursorsNewLocal();
185

186
  /// Read access to newest updated precursors vector.
187
  const std::vector<double>& GetPrecursorsNewLocal() const;
188

189
  /// Read/write access to the cell-wise densities.
190
  std::vector<double>& GetDensitiesLocal();
191

192
  /// Read access to the cell-wise densities.
193
  const std::vector<double>& GetDensitiesLocal() const;
194

195
  SetSourceFunction GetActiveSetSourceFunction() const;
196

197
  std::shared_ptr<AGSLinearSolver> GetAGSSolver();
198

199
  std::vector<std::shared_ptr<LinearSolver>>& GetWGSSolvers();
200

201
  WGSContext& GetWGSContext(int groupset_id);
202

203
  /**
204
   * Gets the local and global number of iterative unknowns. This normally is only the flux moments,
205
   * however, the sweep based solvers might include delayed angular fluxes in this number.
206
   */
207
  virtual std::pair<size_t, size_t> GetNumPhiIterativeUnknowns();
208

209
  /// Gets the local handle of a flux-moment based field function.
210
  size_t MapPhiFieldFunction(size_t g, size_t m) const;
211

212
  /// Returns the power generation field function, if enabled.
213
  std::shared_ptr<FieldFunctionGridBased> GetPowerFieldFunction() const;
214

215
  void Initialize() override;
216

217
  /// Initializes default materials and physics materials.
218
  void InitializeMaterials();
219

UNCOV
220
  bool TriggerRestartDump() const
×
221
  {
UNCOV
222
    if (options_.write_restart_time_interval <= std::chrono::seconds(0))
×
223
      return false;
224

UNCOV
225
    auto elapsed = std::chrono::system_clock::now() - options_.last_restart_write_time;
×
226
    return elapsed >= options_.write_restart_time_interval;
×
227
  }
228

UNCOV
229
  void UpdateRestartWriteTime()
×
230
  {
231
    options_.last_restart_write_time = std::chrono::system_clock::now();
×
232
  }
233

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

237
  /// Copy relevant section of phi_old to the field functions.
238
  void UpdateFieldFunctions();
239

240
  /// Sets the internal phi vector to the value in the associated field function.
241
  void SetPhiFromFieldFunctions(PhiSTLOption which_phi,
242
                                const std::vector<size_t>& m_indices,
243
                                const std::vector<size_t>& g_indices);
244

245
  /**
246
   * A method for post-processing an adjoint solution.
247
   *
248
   * @note This does nothing for diffusion-based solvers.
249
   */
UNCOV
250
  virtual void ReorientAdjointSolution() {};
×
251

252
protected:
253
  /// Performs general input checks before initialization continues.
254
  virtual void PerformInputChecks();
255

256
  /// Prints header information of simulation.
257
  void PrintSimHeader();
258

259
  virtual void InitializeSpatialDiscretization();
260

261
  void ComputeUnitIntegrals();
262

263
  /// Initializes common groupset items.
264
  void InitializeGroupsets();
265

266
  /// Computes the number of moments for the given mesher types
267
  void ValidateAndComputeScatteringMoments();
268

269
  /// Initializes parallel arrays.
270
  virtual void InitializeParrays();
271

272
  void InitializeFieldFunctions();
273

274
  /// Initializes boundaries.
UNCOV
275
  virtual void InitializeBoundaries() {}
×
276

277
  virtual void InitializeSolverSchemes();
278

UNCOV
279
  virtual void InitializeWGSSolvers() {};
×
280

281
  /// Initializes data carriers to GPUs and memory pinner.
282
  void InitializeGPUExtras();
283

284
  /// Reset data carriers to null and unpin memory.
285
  void ResetGPUCarriers();
286

287
  virtual void ZeroSolutions() = 0;
288

289
  LBSOptions options_;
290
  size_t num_moments_ = 0;
291
  size_t num_groups_ = 0;
292
  unsigned int scattering_order_ = 0;
293
  size_t num_precursors_ = 0;
294
  size_t max_precursors_per_material_ = 0;
295

296
  std::vector<LBSGroup> groups_;
297
  std::vector<LBSGroupset> groupsets_;
298

299
  std::map<int, std::shared_ptr<MultiGroupXS>> block_id_to_xs_map_;
300

301
  std::vector<std::shared_ptr<PointSource>> point_sources_;
302
  std::vector<std::shared_ptr<VolumetricSource>> volumetric_sources_;
303

304
  std::shared_ptr<MeshContinuum> grid_;
305
  std::shared_ptr<SpatialDiscretization> discretization_ = nullptr;
306

307
  std::vector<CellFaceNodalMapping> grid_nodal_mappings_;
308
  std::shared_ptr<MPICommunicatorSet> grid_local_comm_set_ = nullptr;
309

310
  std::vector<UnitCellMatrices> unit_cell_matrices_;
311
  std::map<uint64_t, UnitCellMatrices> unit_ghost_cell_matrices_;
312
  std::vector<CellLBSView> cell_transport_views_;
313

314
  std::map<uint64_t, BoundaryPreference> boundary_preferences_;
315

316
  UnknownManager flux_moments_uk_man_;
317

318
  size_t max_cell_dof_count_ = 0;
319
  uint64_t local_node_count_ = 0;
320
  uint64_t global_node_count_ = 0;
321

322
  std::vector<double> q_moments_local_, ext_src_moments_local_;
323
  std::vector<double> phi_new_local_, phi_old_local_;
324
  std::vector<double> precursor_new_local_;
325
  std::vector<double> densities_local_;
326

327
  SetSourceFunction active_set_source_function_;
328

329
  std::shared_ptr<AGSLinearSolver> ags_solver_;
330
  std::vector<std::shared_ptr<LinearSolver>> wgs_solvers_;
331

332
  std::map<std::pair<size_t, size_t>, size_t> phi_field_functions_local_map_;
333
  size_t power_gen_fieldfunc_local_handle_ = 0;
334

335
  /// Time integration parameter meant to be set by an executor
336
  std::shared_ptr<const TimeIntegration> time_integration_ = nullptr;
337

338
  /**
339
   * @brief Data carriers for necessary data to run the sweep on GPU.
340
   * @details These objects manage GPU memory allocation automatically, organize cross-section,
341
   * outflow, and mesh data into contiguous memory on the CPU, and handle copying it to the GPU.
342
   *
343
   * There are 3 carriers, respectively for cross sections, outflow and mesh.
344
   */
345
  std::array<void*, 3> carriers_ = {nullptr, nullptr, nullptr};
346

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

350
  /// Flag indicating if GPU acceleration is enabled.
351
  bool use_gpus_;
352

353
  /// Checks if the current CPU is associated with any GPU.
354
  static void CheckCapableDevices();
355

356
public:
357
  static std::map<std::string, uint64_t> supported_boundary_names;
358
  static std::map<uint64_t, std::string> supported_boundary_ids;
359

360
  /// Returns the input parameters for this object.
361
  static InputParameters GetInputParameters();
362
};
363

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