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

Open-Sn / opensn / 21892714514

10 Feb 2026 11:58PM UTC coverage: 74.806% (-0.02%) from 74.828%
21892714514

push

github

web-flow
Merge pull request #929 from wdhawkins/iteration_log

Fixing iteration status messages

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

211 existing lines in 16 files now uncovered.

19638 of 26252 relevant lines covered (74.81%)

69654996.54 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
  unsigned int 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
  std::vector<LBSGroupset>& GetGroupsets();
107

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

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

113
  /// Clears all the point sources from the solver.
114
  void ClearPointSources();
115

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

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

122
  /// Clears all the volumetric sources from the solver.
123
  void ClearVolumetricSources();
124

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

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

131
  size_t& GetLastRestartTime();
132

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

208
  SetSourceFunction GetActiveSetSourceFunction() const;
209
  void SetActiveSetSourceFunction(SetSourceFunction source_function);
210

211
  std::shared_ptr<AGSLinearSolver> GetAGSSolver();
212

213
  std::vector<std::shared_ptr<LinearSolver>>& GetWGSSolvers();
214

215
  WGSContext& GetWGSContext(int groupset_id);
216

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

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

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

229
  void Initialize() override;
230

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

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

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

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

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

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

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

263
  virtual void UpdatePsiOld() {};
×
264

265
protected:
266
  virtual void PrintSimHeader();
267

268
  void ComputeUnitIntegrals();
269

270
  virtual void InitializeSpatialDiscretization();
271

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

275
  void InitializeFieldFunctions();
276

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

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

283
  void InitializeSolverSchemes();
284

UNCOV
285
  virtual void InitializeWGSSolvers() {};
×
UNCOV
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
  virtual void ZeroSolutions() = 0;
294

295
  LBSOptions options_;
296
  double time_ = 0.0;
297
  double theta_ = 1.0;
298
  double dt_ = 1.0;
299
  GeometryType geometry_type_ = GeometryType::INVALID;
300
  size_t num_moments_ = 0;
301
  unsigned int num_groups_ = 0;
302
  unsigned int scattering_order_ = 0;
303
  size_t num_precursors_ = 0;
304
  size_t max_precursors_per_material_ = 0;
305

306
  std::vector<LBSGroupset> groupsets_;
307

308
  BlockID2XSMap block_id_to_xs_map_;
309

310
  std::vector<std::shared_ptr<PointSource>> point_sources_;
311
  std::vector<std::shared_ptr<VolumetricSource>> volumetric_sources_;
312

313
  std::shared_ptr<MeshContinuum> grid_;
314
  std::shared_ptr<SpatialDiscretization> discretization_ = nullptr;
315

316
  std::vector<CellFaceNodalMapping> grid_nodal_mappings_;
317
  std::shared_ptr<MPICommunicatorSet> grid_local_comm_set_ = nullptr;
318

319
  std::vector<UnitCellMatrices> unit_cell_matrices_;
320
  std::map<uint64_t, UnitCellMatrices> unit_ghost_cell_matrices_;
321
  std::vector<CellLBSView> cell_transport_views_;
322

323
  UnknownManager flux_moments_uk_man_;
324

325
  unsigned int max_cell_dof_count_ = 0;
326
  unsigned int min_cell_dof_count_ = 0;
327
  uint64_t local_node_count_ = 0;
328
  uint64_t global_node_count_ = 0;
329

330
  std::vector<double> q_moments_local_, ext_src_moments_local_;
331
  std::vector<double> phi_new_local_, phi_old_local_;
332
  std::vector<double> precursor_new_local_;
333
  std::vector<double> densities_local_;
334

335
  SetSourceFunction active_set_source_function_;
336

337
  std::shared_ptr<AGSLinearSolver> ags_solver_;
338
  std::vector<std::shared_ptr<LinearSolver>> wgs_solvers_;
339

340
  std::map<std::pair<unsigned int, size_t>, size_t> phi_field_functions_local_map_;
341
  size_t power_gen_fieldfunc_local_handle_ = 0;
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
private:
359
  /// Initialize groupsets
360
  void InitializeGroupsets(const InputParameters& params);
361

362
  /// Initializes materials
363
  void InitializeXSmapAndDensities(const InputParameters& params);
364
  void InitializeMaterials();
365

366
  /// Initialize sources
367
  void InitializeSources(const InputParameters& params);
368

369
  /// Initialize boundary conditions
370
  void InitializeBoundaryConditions(const InputParameters& params);
371

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

376
  /// Returns the input parameters for this object.
377
  static InputParameters GetInputParameters();
378

379
  static InputParameters GetOptionsBlock();
380

381
  static InputParameters GetXSMapEntryBlock();
382

383
protected:
384
  /// Checks if the current CPU is associated with any GPU.
385
  static void CheckCapableDevices();
386
};
387

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