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

Open-Sn / opensn / 18380674863

09 Oct 2025 02:45PM UTC coverage: 75.202% (+0.3%) from 74.862%
18380674863

push

github

web-flow
Merge pull request #793 from wdhawkins/remove_cmake_debug_flags

Removing forced use of -O0 for Debug config

18014 of 23954 relevant lines covered (75.2%)

56527302.01 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
  void SetAdjoint(bool adjoint);
62

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

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

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

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

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

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

90
  const std::vector<LBSGroup>& GetGroups() const;
91

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

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

100
  const std::vector<LBSGroupset>& GetGroupsets() const;
101

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

105
  /// Clears all the point sources from the solver.
106
  void ClearPointSources();
107

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

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

114
  /// Clears all the volumetric sources from the solver.
115
  void ClearVolumetricSources();
116

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

120
  /// Clears all the boundary conditions from the solver.
121
  void ClearBoundaries();
122

123
  size_t& GetLastRestartTime();
124

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

197
  SetSourceFunction GetActiveSetSourceFunction() const;
198

199
  std::shared_ptr<AGSLinearSolver> GetAGSSolver();
200

201
  std::vector<std::shared_ptr<LinearSolver>>& GetWGSSolvers();
202

203
  WGSContext& GetWGSContext(int groupset_id);
204

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

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

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

217
  void Initialize() override;
218

219
  /// Initializes default materials and physics materials.
220
  void InitializeMaterials();
221

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

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

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

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

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

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

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

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

258
  /// Prints header information of simulation.
259
  void PrintSimHeader();
260

261
  virtual void InitializeSpatialDiscretization();
262

263
  void ComputeUnitIntegrals();
264

265
  /// Initializes common groupset items.
266
  void InitializeGroupsets();
267

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

271
  /// Initializes parallel arrays.
272
  virtual void InitializeParrays();
273

274
  void InitializeFieldFunctions();
275

276
  /// Initializes boundaries.
277
  virtual void InitializeBoundaries() {}
×
278

279
  virtual void InitializeSolverSchemes();
280

281
  virtual void InitializeWGSSolvers() {};
×
282

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

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

289
  virtual void ZeroSolutions() = 0;
290

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

298
  std::vector<LBSGroup> groups_;
299
  std::vector<LBSGroupset> groupsets_;
300

301
  std::map<int, std::shared_ptr<MultiGroupXS>> block_id_to_xs_map_;
302

303
  std::vector<std::shared_ptr<PointSource>> point_sources_;
304
  std::vector<std::shared_ptr<VolumetricSource>> volumetric_sources_;
305

306
  std::shared_ptr<MeshContinuum> grid_;
307
  std::shared_ptr<SpatialDiscretization> discretization_ = nullptr;
308

309
  std::vector<CellFaceNodalMapping> grid_nodal_mappings_;
310
  std::shared_ptr<MPICommunicatorSet> grid_local_comm_set_ = nullptr;
311

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

316
  std::map<uint64_t, BoundaryPreference> boundary_preferences_;
317

318
  UnknownManager flux_moments_uk_man_;
319

320
  size_t max_cell_dof_count_ = 0;
321
  size_t min_cell_dof_count_ = 0;
322
  uint64_t local_node_count_ = 0;
323
  uint64_t global_node_count_ = 0;
324

325
  std::vector<double> q_moments_local_, ext_src_moments_local_;
326
  std::vector<double> phi_new_local_, phi_old_local_;
327
  std::vector<double> precursor_new_local_;
328
  std::vector<double> densities_local_;
329

330
  SetSourceFunction active_set_source_function_;
331

332
  std::shared_ptr<AGSLinearSolver> ags_solver_;
333
  std::vector<std::shared_ptr<LinearSolver>> wgs_solvers_;
334

335
  std::map<std::pair<size_t, size_t>, size_t> phi_field_functions_local_map_;
336
  size_t power_gen_fieldfunc_local_handle_ = 0;
337

338
  /// Time integration parameter meant to be set by an executor
339
  std::shared_ptr<const TimeIntegration> time_integration_ = nullptr;
340

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

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

353
  /// Flag indicating if GPU acceleration is enabled.
354
  bool use_gpus_;
355

356
  /// Checks if the current CPU is associated with any GPU.
357
  static void CheckCapableDevices();
358

359
public:
360
  static std::map<std::string, uint64_t> supported_boundary_names;
361
  static std::map<uint64_t, std::string> supported_boundary_ids;
362

363
  /// Returns the input parameters for this object.
364
  static InputParameters GetInputParameters();
365
};
366

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