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

Open-Sn / opensn / 17874334706

19 Sep 2025 06:40PM UTC coverage: 74.578% (-0.2%) from 74.728%
17874334706

push

github

web-flow
Merge pull request #763 from andrsd/cmake-lib-names

cmake: correcting file names for the opensn library files

17734 of 23779 relevant lines covered (74.58%)

44077127.1 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
  size_t 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 newest updated angular flux vector.
190
  std::vector<std::vector<double>>& GetPsiNewLocal();
191

192
  /// Read access to newest updated angular flux vector.
193
  const std::vector<std::vector<double>>& GetPsiNewLocal() const;
194

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

198
  /// Read access to the cell-wise densities.
199
  const std::vector<double>& GetDensitiesLocal() const;
200

201
  SetSourceFunction GetActiveSetSourceFunction() const;
202

203
  std::shared_ptr<AGSLinearSolver> GetAGSSolver();
204

205
  std::vector<std::shared_ptr<LinearSolver>>& GetWGSSolvers();
206

207
  WGSContext& GetWGSContext(int groupset_id);
208

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

215
  /// Gets the local handle of a flux-moment based field function.
216
  size_t MapPhiFieldFunction(size_t g, size_t m) const;
217

218
  /// Returns the power generation field function, if enabled.
219
  std::shared_ptr<FieldFunctionGridBased> GetPowerFieldFunction() const;
220

221
  void Initialize() override;
222

223
  /// Initializes default materials and physics materials.
224
  void InitializeMaterials();
225

226
  bool TriggerRestartDump() const
×
227
  {
228
    if (options_.write_restart_time_interval <= std::chrono::seconds(0))
×
229
      return false;
230

231
    auto elapsed = std::chrono::system_clock::now() - options_.last_restart_write_time;
×
232
    return elapsed >= options_.write_restart_time_interval;
×
233
  }
234

235
  void UpdateRestartWriteTime()
×
236
  {
237
    options_.last_restart_write_time = std::chrono::system_clock::now();
×
238
  }
239

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

243
  /// Copy relevant section of phi_old to the field functions.
244
  void UpdateFieldFunctions();
245

246
  /// Sets the internal phi vector to the value in the associated field function.
247
  void SetPhiFromFieldFunctions(PhiSTLOption which_phi,
248
                                const std::vector<size_t>& m_indices,
249
                                const std::vector<size_t>& g_indices);
250

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

258
protected:
259
  /// Performs general input checks before initialization continues.
260
  virtual void PerformInputChecks();
261

262
  /// Prints header information of simulation.
263
  void PrintSimHeader();
264

265
  virtual void InitializeSpatialDiscretization();
266

267
  void ComputeUnitIntegrals();
268

269
  /// Initializes common groupset items.
270
  void InitializeGroupsets();
271

272
  /// Computes the number of moments for the given mesher types
273
  void ValidateAndComputeScatteringMoments();
274

275
  /// Initializes parallel arrays.
276
  virtual void InitializeParrays();
277

278
  void InitializeFieldFunctions();
279

280
  /// Initializes boundaries.
281
  virtual void InitializeBoundaries() {}
×
282

283
  virtual void InitializeSolverSchemes();
284

285
  virtual void InitializeWGSSolvers() {};
×
286

287
  /// Initializes data carriers to GPUs and memory pinner.
288
  void InitializeGPUExtras();
289

290
  /// Reset data carriers to null and unpin memory.
291
  void ResetGPUCarriers();
292

293
  LBSOptions options_;
294
  size_t num_moments_ = 0;
295
  size_t num_groups_ = 0;
296
  size_t scattering_order_ = 0;
297
  size_t num_precursors_ = 0;
298
  size_t max_precursors_per_material_ = 0;
299

300
  std::vector<LBSGroup> groups_;
301
  std::vector<LBSGroupset> groupsets_;
302

303
  std::map<int, std::shared_ptr<MultiGroupXS>> block_id_to_xs_map_;
304

305
  std::vector<std::shared_ptr<PointSource>> point_sources_;
306
  std::vector<std::shared_ptr<VolumetricSource>> volumetric_sources_;
307

308
  std::shared_ptr<MeshContinuum> grid_;
309
  std::shared_ptr<SpatialDiscretization> discretization_ = nullptr;
310

311
  std::vector<CellFaceNodalMapping> grid_nodal_mappings_;
312
  std::shared_ptr<MPICommunicatorSet> grid_local_comm_set_ = nullptr;
313

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

318
  std::map<uint64_t, BoundaryPreference> boundary_preferences_;
319

320
  UnknownManager flux_moments_uk_man_;
321

322
  size_t max_cell_dof_count_ = 0;
323
  uint64_t local_node_count_ = 0;
324
  uint64_t global_node_count_ = 0;
325

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

332
  SetSourceFunction active_set_source_function_;
333

334
  std::shared_ptr<AGSLinearSolver> ags_solver_;
335
  std::vector<std::shared_ptr<LinearSolver>> wgs_solvers_;
336

337
  std::map<std::pair<size_t, size_t>, size_t> phi_field_functions_local_map_;
338
  size_t power_gen_fieldfunc_local_handle_ = 0;
339

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

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

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

355
  /// Flag indicating if GPU acceleration is enabled.
356
  bool use_gpus_;
357

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

361
public:
362
  static std::map<std::string, uint64_t> supported_boundary_names;
363
  static std::map<uint64_t, std::string> supported_boundary_ids;
364

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

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