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

Open-Sn / opensn / 22512725730

27 Feb 2026 08:22PM UTC coverage: 74.152% (+0.003%) from 74.149%
22512725730

push

github

web-flow
Merge pull request #950 from wdhawkins/solvers_refactor

Update steady-state <--> time-dependent mode transitions

79 of 113 new or added lines in 7 files covered. (69.91%)

285 existing lines in 10 files now uncovered.

20096 of 27101 relevant lines covered (74.15%)

66856917.7 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/lbs_problem/groupset/lbs_groupset.h"
8
#include "modules/linear_boltzmann_solvers/lbs_problem/source_functions/source_flags.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 "modules/linear_boltzmann_solvers/lbs_problem/lbs_view.h"
13
#include "framework/math/spatial_discretization/spatial_discretization.h"
14
#include "framework/mesh/mesh_continuum/mesh_continuum.h"
15
#include "framework/math/linear_solver/linear_solver.h"
16
#include "framework/math/spatial_discretization/finite_element/unit_cell_matrices.h"
17
#include "framework/math/geometry.h"
18
#include <petscksp.h>
19
#include <chrono>
20

21
namespace opensn
22
{
23

24
class MPICommunicatorSet;
25
class GridFaceHistogram;
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(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
  /// Returns simulation time in seconds for time dependent problems
54
  double GetTime() const;
55

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

59
  /// Sets dt
60
  void SetTimeStep(double dt);
61

62
  /// Returns dt
63
  double GetTimeStep() const;
64

65
  /// Sets theta for time discretization
66
  void SetTheta(double theta);
67

68
  /// Sets theta for time discretization
69
  double GetTheta() const;
70

71
  /// Is the problem time dependent
72

73
  /**
74
   * Toggle forward/adjoint transport mode.
75
   *
76
   * If called after initialization, this performs a mode-transition reset:
77
   * materials are reinitialized in the selected mode, sources and boundaries
78
   * are cleared, and solution vectors are zeroed.
79
   */
80
  void SetAdjoint(bool adjoint);
81
  bool IsAdjoint() const;
82
  virtual void SetSaveAngularFlux(bool save);
83
  void ApplyOptions();
84
  void ZeroPhi();
85
  virtual void ZeroPsi() = 0;
86

87
  GeometryType GetGeometryType() const;
88

89
  /// Returns the number of moments for the solver. This will only be non-zero after initialization.
90
  unsigned int GetNumMoments() const;
91

92
  unsigned int GetMaxCellDOFCount() const;
93

94
  unsigned int GetMinCellDOFCount() const;
95

96
  bool UseGPUs() const;
97

98
  /// Returns the number of groups for the solver. This will only be non-zero after initialization.
99
  unsigned int GetNumGroups() const;
100

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

104
  /**
105
   * Returns the number of precursors for the solver. This will only be non-zero after
106
   * initialization.
107
   */
108
  unsigned int GetNumPrecursors() const;
109

110
  /**
111
   * Returns the maximum number of precursors defined on any material. This will only be non-zero
112
   * after initialization.
113
   */
114
  unsigned int GetMaxPrecursorsPerMaterial() const;
115

116
  std::vector<LBSGroupset>& GetGroupsets();
117

118
  const std::vector<LBSGroupset>& GetGroupsets() const;
119

120
  /// Adds a point source to the solver.
121
  void AddPointSource(std::shared_ptr<PointSource> point_source);
122

123
  /// Clears all the point sources from the solver.
124
  void ClearPointSources();
125

126
  /// Constant accessor to the list of point sources.
127
  const std::vector<std::shared_ptr<PointSource>>& GetPointSources() const;
128

129
  /// Adds a volumetric source to the solver.
130
  void AddVolumetricSource(std::shared_ptr<VolumetricSource> volumetric_source);
131

132
  /// Clears all the volumetric sources from the solver.
133
  void ClearVolumetricSources();
134

135
  /// Constant accessor to the list of volumetric sources.
136
  const std::vector<std::shared_ptr<VolumetricSource>>& GetVolumetricSources() const;
137

138
  /// Clears all the boundary conditions from the solver.
139
  virtual void ClearBoundaries() = 0;
140

141
  size_t& GetLastRestartTime();
142

143
  /// Returns a reference to the map of material ids to XSs.
144
  const BlockID2XSMap& GetBlockID2XSMap() const;
145

146
  /// Replaces the map of block ids to XSs and refreshes material data.
147
  void SetBlockID2XSMap(const BlockID2XSMap& xs_map);
148

149
  /// Obtains a reference to the grid.
150
  std::shared_ptr<MeshContinuum> GetGrid() const;
151

152
  /// Get pointer to carriers.
153
  void* GetCarrier(std::uint32_t idx) { return carriers_.at(idx); }
154

155
  /// Get pointer to pinners.
156
  void* GetPinner(std::uint32_t idx) { return pinners_.at(idx); }
157

158
  /// Obtains a reference to the spatial discretization.
159
  const SpatialDiscretization& GetSpatialDiscretization() const;
160

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

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

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

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

173
  /// Obtains a reference to the unknown manager for flux-moments.
174
  const UnknownManager& GetUnknownManager() const;
175

176
  /// Returns the local node count for the flux-moments data structures.
177
  size_t GetLocalNodeCount() const;
178

179
  /// Returns the global node count for the flux-moments data structures.
180
  size_t GetGlobalNodeCount() const;
181

182
  /// Read/write access to source moments vector.
183
  std::vector<double>& GetQMomentsLocal();
184

185
  /// Read access to source moments vector.
186
  const std::vector<double>& GetQMomentsLocal() const;
187

188
  /// Read/write access to exterior src moments vector.
189
  std::vector<double>& GetExtSrcMomentsLocal();
190

191
  /// Read access to exterior src moments vector.
192
  const std::vector<double>& GetExtSrcMomentsLocal() const;
193

194
  /// Read/write access to last updated flux vector.
195
  std::vector<double>& GetPhiOldLocal();
196

197
  /// Read access to last updated flux vector.
198
  const std::vector<double>& GetPhiOldLocal() const;
199

200
  /// Read/write access to newest updated flux vector.
201
  std::vector<double>& GetPhiNewLocal();
202

203
  /// Read access to newest updated flux vector.
204
  const std::vector<double>& GetPhiNewLocal() const;
205

206
  /// Read/write access to newest updated precursors vector.
207
  std::vector<double>& GetPrecursorsNewLocal();
208

209
  /// Read access to newest updated precursors vector.
210
  const std::vector<double>& GetPrecursorsNewLocal() const;
211

212
  /// Read/write access to the cell-wise densities.
213
  std::vector<double>& GetDensitiesLocal();
214

215
  /// Read access to the cell-wise densities.
216
  const std::vector<double>& GetDensitiesLocal() const;
217

218
  SetSourceFunction GetActiveSetSourceFunction() const;
219
  void SetActiveSetSourceFunction(SetSourceFunction source_function);
220

221
  std::shared_ptr<AGSLinearSolver> GetAGSSolver();
222

223
  std::vector<std::shared_ptr<LinearSolver>>& GetWGSSolvers();
224

225
  WGSContext& GetWGSContext(int groupset_id);
226

227
  /**
228
   * Gets the local and global number of iterative unknowns. This normally is only the flux moments,
229
   * however, the sweep based solvers might include delayed angular fluxes in this number.
230
   */
231
  virtual std::pair<size_t, size_t> GetNumPhiIterativeUnknowns();
232

233
  /// Gets the local handle of a flux-moment based field function.
234
  size_t MapPhiFieldFunction(unsigned int g, unsigned int m) const;
235

236
  /// Returns the power generation field function, if enabled.
237
  std::shared_ptr<FieldFunctionGridBased> GetPowerFieldFunction() const;
238

239
  void Initialize() override;
240

241
  bool TriggerRestartDump() const
242
  {
243
    if (options_.write_restart_time_interval <= std::chrono::seconds(0))
244
      return false;
245

246
    auto elapsed = std::chrono::system_clock::now() - options_.last_restart_write_time;
247
    return elapsed >= options_.write_restart_time_interval;
248
  }
249

UNCOV
250
  void UpdateRestartWriteTime()
×
251
  {
UNCOV
252
    options_.last_restart_write_time = std::chrono::system_clock::now();
×
253
  }
254

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

258
  /// Copy relevant section of phi_old to the field functions.
259
  void UpdateFieldFunctions();
260

261
  /// Sets the internal phi vector to the value in the associated field function.
262
  void SetPhiFromFieldFunctions(PhiSTLOption which_phi,
263
                                const std::vector<unsigned int>& m_indices,
264
                                const std::vector<unsigned int>& g_indices);
265

266
  /**
267
   * A method for post-processing an adjoint solution.
268
   *
269
   * @note This does nothing for diffusion-based solvers.
270
   */
UNCOV
271
  virtual void ReorientAdjointSolution() {};
×
272

UNCOV
273
  virtual void UpdatePsiOld() {};
×
274

275
protected:
276
  virtual void PrintSimHeader();
277

278
  void ComputeUnitIntegrals();
279

280
  virtual void InitializeSpatialDiscretization();
281

282
  /// Initializes parallel arrays.
283
  void InitializeParrays();
284

285
  void InitializeFieldFunctions();
286

287
  /// Initializes boundaries.
UNCOV
288
  virtual void InitializeBoundaries() {}
×
289

290
  /// Derived problems handle boundary options.
291
  virtual void SetBoundaryOptions(const InputParameters& params) = 0;
292

293
  void InitializeSolverSchemes();
294

UNCOV
295
  virtual void InitializeWGSSolvers() {};
×
UNCOV
296

×
UNCOV
297
  /// Initializes data carriers to GPUs and memory pinner.
×
UNCOV
298
  void InitializeGPUExtras();
×
UNCOV
299

×
300
  /// Reset data carriers to null and unpin memory.
301
  void ResetGPUCarriers();
302

303
  LBSOptions options_;
304
  double time_ = 0.0;
305
  double theta_ = 1.0;
306
  double dt_ = 1.0;
307
  GeometryType geometry_type_ = GeometryType::INVALID;
308
  unsigned int num_moments_ = 0;
309
  unsigned int num_groups_ = 0;
310
  unsigned int scattering_order_ = 0;
311
  unsigned int num_precursors_ = 0;
312
  unsigned int max_precursors_per_material_ = 0;
313

314
  std::vector<LBSGroupset> groupsets_;
315

316
  BlockID2XSMap block_id_to_xs_map_;
317

318
  std::vector<std::shared_ptr<PointSource>> point_sources_;
319
  std::vector<std::shared_ptr<VolumetricSource>> volumetric_sources_;
320

321
  std::shared_ptr<MeshContinuum> grid_;
322
  std::shared_ptr<SpatialDiscretization> discretization_ = nullptr;
323

324
  std::vector<CellFaceNodalMapping> grid_nodal_mappings_;
325
  std::shared_ptr<MPICommunicatorSet> grid_local_comm_set_ = nullptr;
326

327
  std::vector<UnitCellMatrices> unit_cell_matrices_;
328
  std::map<uint64_t, UnitCellMatrices> unit_ghost_cell_matrices_;
329
  std::vector<CellLBSView> cell_transport_views_;
330

331
  UnknownManager flux_moments_uk_man_;
332

333
  unsigned int max_cell_dof_count_ = 0;
334
  unsigned int min_cell_dof_count_ = 0;
335
  uint64_t local_node_count_ = 0;
336
  uint64_t global_node_count_ = 0;
337

338
  std::vector<double> q_moments_local_, ext_src_moments_local_;
339
  std::vector<double> phi_new_local_, phi_old_local_;
340
  std::vector<double> precursor_new_local_;
341
  std::vector<double> densities_local_;
342

343
  SetSourceFunction active_set_source_function_;
344

345
  std::shared_ptr<AGSLinearSolver> ags_solver_;
346
  std::vector<std::shared_ptr<LinearSolver>> wgs_solvers_;
347

348
  std::map<std::pair<unsigned int, unsigned int>, size_t> phi_field_functions_local_map_;
349
  size_t power_gen_fieldfunc_local_handle_ = 0;
350

351
  /**
352
   * \brief Data carriers for necessary data to run the sweep on GPU.
353
   * \details These objects manage GPU memory allocation automatically, organize cross-section,
354
   * outflow, and mesh data into contiguous memory on the CPU, and handle copying it to the GPU.
355
   *
356
   * There are 3 carriers, respectively for cross sections, outflow and mesh.
357
   */
358
  std::array<void*, 3> carriers_ = {nullptr, nullptr, nullptr};
359

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

363
  /// Flag indicating if GPU acceleration is enabled.
364
  bool use_gpus_;
365
  bool applied_adjoint_ = false;
366
  bool applied_save_angular_flux_ = false;
367

368
private:
369
  /// Initialize groupsets
370
  void InitializeGroupsets(const InputParameters& params);
371

372
  /// Initializes materials
373
  void InitializeXSmapAndDensities(const InputParameters& params);
374
  void InitializeMaterials();
375

376
  /// Initialize sources
377
  void InitializeSources(const InputParameters& params);
378

379
  /// Parse and validate options without applying runtime side-effects.
380
  void ParseOptions(const InputParameters& input);
381

382
public:
383
  /// Max number of DOFs per cell that the sweep kernel on GPU can handle.
384
  static constexpr std::uint32_t max_dofs_gpu = 10;
385

386
  /// Returns the input parameters for this object.
387
  static InputParameters GetInputParameters();
388

389
  static InputParameters GetOptionsBlock();
390

391
  static InputParameters GetXSMapEntryBlock();
392

393
protected:
394
  /// Checks if the current CPU is associated with any GPU.
395
  static void CheckCapableDevices();
396
};
397

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