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

Open-Sn / opensn / 21738357923

06 Feb 2026 02:45AM UTC coverage: 74.553% (+0.6%) from 73.983%
21738357923

push

github

web-flow
Merge pull request #921 from wdhawkins/update_quick_install_doc

Updating quick install instructions.

19363 of 25972 relevant lines covered (74.55%)

59700221.09 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/source_functions/source_flags.h"
10
#include "modules/linear_boltzmann_solvers/lbs_problem/point_source/point_source.h"
11
#include "modules/linear_boltzmann_solvers/lbs_problem/volumetric_source/volumetric_source.h"
12
#include "modules/linear_boltzmann_solvers/lbs_problem/lbs_structs.h"
13
#include "modules/linear_boltzmann_solvers/lbs_problem/lbs_view.h"
14
#include "framework/math/spatial_discretization/spatial_discretization.h"
15
#include "framework/mesh/mesh_continuum/mesh_continuum.h"
16
#include "framework/math/linear_solver/linear_solver.h"
17
#include "framework/math/spatial_discretization/finite_element/unit_cell_matrices.h"
18
#include "framework/math/geometry.h"
19
#include <petscksp.h>
20
#include <any>
21
#include <chrono>
22

23
namespace opensn
24
{
25

26
class MPICommunicatorSet;
27
class GridFaceHistogram;
28
class AGSLinearSolver;
29
class WGSLinearSolver;
30
struct WGSContext;
31

32
/// Base class for all Linear Boltzmann Solvers.
33
class LBSProblem : public Problem
34
{
35
public:
36
  explicit LBSProblem(std::string name, std::shared_ptr<MeshContinuum> grid);
37

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

41
  LBSProblem(const LBSProblem&) = delete;
42

43
  LBSProblem& operator=(const LBSProblem&) = delete;
44

45
  ~LBSProblem() override;
46

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

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

53
  void SetOptions(const InputParameters& input);
54

55
  /// Returns simulation time in seconds for time dependent problems
56
  double GetTime() const;
57

58
  /// Sets simulation time in seconds for time dependent problems
59
  void SetTime(double time);
60

61
  /// Sets dt
62
  void SetTimeStep(double dt);
63

64
  /// Returns dt
65
  double GetTimeStep() const;
66

67
  /// Sets theta for time discretization
68
  void SetTheta(double theta);
69

70
  /// Sets theta for time discretization
71
  double GetTheta() const;
72

73
  /// Is the problem time dependent
74

75
  void SetAdjoint(bool adjoint);
76

77
  GeometryType GetGeometryType() const;
78

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

82
  unsigned int GetMaxCellDOFCount() const;
83

84
  unsigned int GetMinCellDOFCount() const;
85

86
  bool UseGPUs() const;
87

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

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

94
  /**
95
   * Returns the number of precursors for the solver. This will only be non-zero after
96
   * initialization.
97
   */
98
  size_t GetNumPrecursors() const;
99

100
  /**
101
   * Returns the maximum number of precursors defined on any material. This will only be non-zero
102
   * after initialization.
103
   */
104
  size_t GetMaxPrecursorsPerMaterial() const;
105

106
  const std::vector<LBSGroup>& GetGroups() const;
107

108
  std::vector<LBSGroupset>& GetGroupsets();
109

110
  const std::vector<LBSGroupset>& GetGroupsets() const;
111

112
  /// Adds a point source to the solver.
113
  void AddPointSource(std::shared_ptr<PointSource> point_source);
114

115
  /// Clears all the point sources from the solver.
116
  void ClearPointSources();
117

118
  /// Constant accessor to the list of point sources.
119
  const std::vector<std::shared_ptr<PointSource>>& GetPointSources() const;
120

121
  /// Adds a volumetric source to the solver.
122
  void AddVolumetricSource(std::shared_ptr<VolumetricSource> volumetric_source);
123

124
  /// Clears all the volumetric sources from the solver.
125
  void ClearVolumetricSources();
126

127
  /// Constant accessor to the list of volumetric sources.
128
  const std::vector<std::shared_ptr<VolumetricSource>>& GetVolumetricSources() const;
129

130
  /// Clears all the boundary conditions from the solver.
131
  virtual void ClearBoundaries() = 0;
132

133
  size_t& GetLastRestartTime();
134

135
  /// Returns a reference to the map of material ids to XSs.
136
  const BlockID2XSMap& GetBlockID2XSMap() const;
137

138
  /// Replaces the map of block ids to XSs and refreshes material data.
139
  void SetBlockID2XSMap(const BlockID2XSMap& xs_map);
140

141
  /// Obtains a reference to the grid.
142
  std::shared_ptr<MeshContinuum> GetGrid() const;
143

144
  /// Get pointer to carriers.
145
  void* GetCarrier(std::uint32_t idx) { return carriers_.at(idx); }
146

147
  /// Get pointer to pinners.
148
  void* GetPinner(std::uint32_t idx) { return pinners_.at(idx); }
149

150
  /// Obtains a reference to the spatial discretization.
151
  const SpatialDiscretization& GetSpatialDiscretization() const;
152

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

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

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

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

165
  /// Obtains a reference to the unknown manager for flux-moments.
166
  const UnknownManager& GetUnknownManager() const;
167

168
  /// Returns the local node count for the flux-moments data structures.
169
  size_t GetLocalNodeCount() const;
170

171
  /// Returns the global node count for the flux-moments data structures.
172
  size_t GetGlobalNodeCount() const;
173

174
  /// Read/write access to source moments vector.
175
  std::vector<double>& GetQMomentsLocal();
176

177
  /// Read access to source moments vector.
178
  const std::vector<double>& GetQMomentsLocal() const;
179

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

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

186
  /// Read/write access to last updated flux vector.
187
  std::vector<double>& GetPhiOldLocal();
188

189
  /// Read access to last updated flux vector.
190
  const std::vector<double>& GetPhiOldLocal() const;
191

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

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

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

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

204
  /// Read/write access to the cell-wise densities.
205
  std::vector<double>& GetDensitiesLocal();
206

207
  /// Read access to the cell-wise densities.
208
  const std::vector<double>& GetDensitiesLocal() const;
209

210
  SetSourceFunction GetActiveSetSourceFunction() const;
211
  void SetActiveSetSourceFunction(SetSourceFunction source_function);
212

213
  std::shared_ptr<AGSLinearSolver> GetAGSSolver();
214

215
  std::vector<std::shared_ptr<LinearSolver>>& GetWGSSolvers();
216

217
  WGSContext& GetWGSContext(int groupset_id);
218

219
  /**
220
   * Gets the local and global number of iterative unknowns. This normally is only the flux moments,
221
   * however, the sweep based solvers might include delayed angular fluxes in this number.
222
   */
223
  virtual std::pair<size_t, size_t> GetNumPhiIterativeUnknowns();
224

225
  /// Gets the local handle of a flux-moment based field function.
226
  size_t MapPhiFieldFunction(size_t g, size_t m) const;
227

228
  /// Returns the power generation field function, if enabled.
229
  std::shared_ptr<FieldFunctionGridBased> GetPowerFieldFunction() const;
230

231
  void Initialize() override;
232

233
  bool TriggerRestartDump() const
234
  {
235
    if (options_.write_restart_time_interval <= std::chrono::seconds(0))
236
      return false;
237

238
    auto elapsed = std::chrono::system_clock::now() - options_.last_restart_write_time;
239
    return elapsed >= options_.write_restart_time_interval;
240
  }
241

242
  void UpdateRestartWriteTime()
×
243
  {
244
    options_.last_restart_write_time = std::chrono::system_clock::now();
×
245
  }
246

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

250
  /// Copy relevant section of phi_old to the field functions.
251
  void UpdateFieldFunctions();
252

253
  /// Sets the internal phi vector to the value in the associated field function.
254
  void SetPhiFromFieldFunctions(PhiSTLOption which_phi,
255
                                const std::vector<size_t>& m_indices,
256
                                const std::vector<size_t>& g_indices);
257

258
  /**
259
   * A method for post-processing an adjoint solution.
260
   *
261
   * @note This does nothing for diffusion-based solvers.
262
   */
263
  virtual void ReorientAdjointSolution() {};
×
264

265
  virtual void UpdatePsiOld() {};
×
266

267
protected:
268
  virtual void PrintSimHeader();
269

270
  void ComputeUnitIntegrals();
271

272
  virtual void InitializeSpatialDiscretization();
273

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

277
  void InitializeFieldFunctions();
278

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

282
  /// Derived problems handle boundary options.
283
  virtual void SetBoundaryOptions(const InputParameters& params) = 0;
284

285
  void InitializeSolverSchemes();
286

287
  virtual void InitializeWGSSolvers() {};
×
288

×
289
  /// Initializes data carriers to GPUs and memory pinner.
×
290
  void InitializeGPUExtras();
×
291

×
292
  /// Reset data carriers to null and unpin memory.
293
  void ResetGPUCarriers();
294

295
  virtual void ZeroSolutions() = 0;
296

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
  size_t num_moments_ = 0;
303
  size_t num_groups_ = 0;
304
  unsigned int scattering_order_ = 0;
305
  size_t num_precursors_ = 0;
306
  size_t max_precursors_per_material_ = 0;
307

308
  std::vector<LBSGroup> groups_;
309
  std::vector<LBSGroupset> groupsets_;
310

311
  BlockID2XSMap block_id_to_xs_map_;
312

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

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

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

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

326
  UnknownManager flux_moments_uk_man_;
327

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

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

338
  SetSourceFunction active_set_source_function_;
339

340
  std::shared_ptr<AGSLinearSolver> ags_solver_;
341
  std::vector<std::shared_ptr<LinearSolver>> wgs_solvers_;
342

343
  std::map<std::pair<size_t, size_t>, 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

361
private:
362
  /// Initialize groupsets
363
  void InitializeGroupsets(const InputParameters& params);
364

365
  /// Initializes materials
366
  void InitializeXSmapAndDensities(const InputParameters& params);
367
  void InitializeMaterials();
368

369
  /// Initialize sources
370
  void InitializeSources(const InputParameters& params);
371

372
  /// Initialize boundary conditions
373
  void InitializeBoundaryConditions(const InputParameters& params);
374

375
public:
376
  /// Max number of DOFs per cell that the sweep kernel on GPU can handle.
377
  static constexpr std::uint32_t max_dofs_gpu = 10;
378

379
  /// Returns the input parameters for this object.
380
  static InputParameters GetInputParameters();
381

382
  static InputParameters GetOptionsBlock();
383

384
  static InputParameters GetXSMapEntryBlock();
385

386
protected:
387
  /// Checks if the current CPU is associated with any GPU.
388
  static void CheckCapableDevices();
389
};
390

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