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

Open-Sn / opensn / 23994768430

02 Apr 2026 07:54PM UTC coverage: 75.04%. Remained the same
23994768430

push

github

web-flow
Merge pull request #1010 from wdhawkins/field_function_refactor

Refactoring field function implementation

107 of 158 new or added lines in 3 files covered. (67.72%)

258 existing lines in 7 files now uncovered.

20988 of 27969 relevant lines covered (75.04%)

65963021.64 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 <memory>
19
#include <petscksp.h>
20
#include <chrono>
21

22
namespace opensn
23
{
24

25
class MPICommunicatorSet;
26
class GridFaceHistogram;
27
class AGSLinearSolver;
28
class WGSLinearSolver;
29
class LinearSolver;
30
struct WGSContext;
31
class TotalXSCarrier;
32
class OutflowCarrier;
33
class MeshCarrier;
34
template <typename T>
35
class MemoryPinner;
36

37
/// Base class for all Linear Boltzmann Solvers.
38
class LBSProblem : public Problem
39
{
40
public:
41
  LBSProblem(const LBSProblem&) = delete;
42

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

45
  ~LBSProblem() override;
46

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

50
  /// Returns simulation time in seconds for time dependent problems.
51
  double GetTime() const;
52

53
  /// Sets simulation time in seconds for time dependent problems.
54
  void SetTime(double time);
55

56
  /// Sets dt.
57
  void SetTimeStep(double dt);
58

59
  /// Returns dt.
60
  double GetTimeStep() const;
61

62
  /// Sets theta for time discretization.
63
  void SetTheta(double theta);
64

65
  /// Returns theta for time discretization.
66
  double GetTheta() const;
67

68
  /// Returns true if the problem is currently in time-dependent mode.
69
  virtual bool IsTimeDependent() const;
70

71
  /// Set the problem to time-dependent mode.
72
  virtual void SetTimeDependentMode();
73

74
  /// Set the problem to steady-state mode.
75
  virtual void SetSteadyStateMode();
76

77
  /**
78
   * Toggle forward/adjoint transport mode.
79
   *
80
   * If the requested mode differs from the current mode, this performs a
81
   * mode-transition reset. Materials are reinitialized in the selected mode,
82
   * sources and boundaries are cleared, and solution vectors are zeroed.
83
   */
84
  void SetAdjoint(bool adjoint = true);
85

86
  /// Set the problem to forward mode.
87
  void SetForward();
88

89
  /// Returns true if the problem is in adjoint mode.
90
  bool IsAdjoint() const;
91

92
  virtual void SetSaveAngularFlux(bool save);
93

94
  void ZeroPhi();
95
  void CopyPhiNewToOld();
96
  void SetPhiOldFrom(const std::vector<double>& phi_old);
97
  void SetPhiNewFrom(const std::vector<double>& phi_new);
98
  void ScalePhiOld(double factor);
99
  void ScalePhiNew(double factor);
100
  void ZeroQMoments();
101
  void ScaleQMoments(double factor);
102
  void SetQMomentsFrom(const std::vector<double>& q_moments);
103
  void ScalePrecursors(double factor);
104
  void ZeroPrecursors();
105
  void ZeroExtSrcMoments();
106
  void ScaleExtSrcMoments(double factor);
107
  virtual void ZeroPsi() = 0;
108

109
  GeometryType GetGeometryType() const;
110

111
  /// Returns the number of moments for the solver.
112
  unsigned int GetNumMoments() const;
113

114
  unsigned int GetMaxCellDOFCount() const;
115

116
  unsigned int GetMinCellDOFCount() const;
117

118
  bool UseGPUs() const;
119

120
  /// Returns the number of groups for the solver.
121
  unsigned int GetNumGroups() const;
122

123
  /// Returns the scattering order for the solver.
124
  unsigned int GetScatteringOrder() const;
125

126
  /// Returns the number of precursors for the solver.
127
  unsigned int GetNumPrecursors() const;
128

129
  /// Returns the maximum number of precursors defined on any material.
130
  unsigned int GetMaxPrecursorsPerMaterial() const;
131

132
  const std::vector<LBSGroupset>& GetGroupsets() const;
133
  LBSGroupset& GetGroupset(size_t groupset_id);
134
  const LBSGroupset& GetGroupset(size_t groupset_id) const;
135
  size_t GetNumGroupsets() const;
136

137
  /// Adds a point source to the solver.
138
  void AddPointSource(std::shared_ptr<PointSource> point_source);
139

140
  /// Clears all the point sources from the solver.
141
  void ClearPointSources();
142

143
  /// Constant accessor to the list of point sources.
144
  const std::vector<std::shared_ptr<PointSource>>& GetPointSources() const;
145

146
  /// Adds a volumetric source to the solver.
147
  void AddVolumetricSource(std::shared_ptr<VolumetricSource> volumetric_source);
148

149
  /// Clears all the volumetric sources from the solver.
150
  void ClearVolumetricSources();
151

152
  /// Constant accessor to the list of volumetric sources.
153
  const std::vector<std::shared_ptr<VolumetricSource>>& GetVolumetricSources() const;
154

155
  /// Clears all the boundary conditions from the solver.
156
  virtual void ClearBoundaries() = 0;
157

158
  /// Returns a reference to the map of material ids to XSs.
159
  const BlockID2XSMap& GetBlockID2XSMap() const;
160

161
  /// Replaces the map of block ids to XSs and refreshes material data.
162
  void SetBlockID2XSMap(const BlockID2XSMap& xs_map);
163

164
  /// Obtains a reference to the grid.
165
  std::shared_ptr<MeshContinuum> GetGrid() const;
166

167
  TotalXSCarrier* GetTotalXSCarrier() { return total_xs_carrier_.get(); }
168
  const TotalXSCarrier* GetTotalXSCarrier() const { return total_xs_carrier_.get(); }
169

170
  OutflowCarrier* GetOutflowCarrier() { return outflow_carrier_.get(); }
171
  const OutflowCarrier* GetOutflowCarrier() const { return outflow_carrier_.get(); }
172

173
  MeshCarrier* GetMeshCarrier() { return mesh_carrier_.get(); }
174
  const MeshCarrier* GetMeshCarrier() const { return mesh_carrier_.get(); }
175

176
  MemoryPinner<double>* GetSourceMomentsPinner() { return source_pinner_.get(); }
177
  const MemoryPinner<double>* GetSourceMomentsPinner() const { return source_pinner_.get(); }
178

179
  MemoryPinner<double>* GetPhiPinner() { return phi_pinner_.get(); }
180
  const MemoryPinner<double>* GetPhiPinner() const { return phi_pinner_.get(); }
181

182
  /// Obtains a reference to the spatial discretization.
183
  const SpatialDiscretization& GetSpatialDiscretization() const;
184

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

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

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

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

197
  /// Obtains a reference to the unknown manager for flux-moments.
198
  const UnknownManager& GetUnknownManager() const;
199

200
  /// Returns the local node count for the flux-moments data structures.
201
  size_t GetLocalNodeCount() const;
202

203
  /// Returns the global node count for the flux-moments data structures.
204
  size_t GetGlobalNodeCount() const;
205

206
  /// Read/write access to source moments vector.
207
  std::vector<double>& GetQMomentsLocal();
208

209
  /// Read access to source moments vector.
210
  const std::vector<double>& GetQMomentsLocal() const;
211

212
  /// Read/write access to exterior src moments vector.
213
  std::vector<double>& GetExtSrcMomentsLocal();
214

215
  /// Read access to exterior src moments vector.
216
  const std::vector<double>& GetExtSrcMomentsLocal() const;
217

218
  /// Replaces exterior source moments vector.
219
  void SetExtSrcMomentsFrom(const std::vector<double>& ext_src_moments);
220

221
  /// Read/write access to last updated flux vector.
222
  std::vector<double>& GetPhiOldLocal();
223

224
  /// Read access to last updated flux vector.
225
  const std::vector<double>& GetPhiOldLocal() const;
226

227
  /// Read/write access to newest updated flux vector.
228
  std::vector<double>& GetPhiNewLocal();
229

230
  /// Read access to newest updated flux vector.
231
  const std::vector<double>& GetPhiNewLocal() const;
232

233
  /// Read/write access to newest updated precursors vector.
234
  std::vector<double>& GetPrecursorsNewLocal();
235

236
  /// Read access to newest updated precursors vector.
237
  const std::vector<double>& GetPrecursorsNewLocal() const;
238

239
  SetSourceFunction GetActiveSetSourceFunction() const;
240

241
  std::shared_ptr<AGSLinearSolver> GetAGSSolver();
242

243
  std::shared_ptr<LinearSolver> GetWGSSolver(size_t groupset_id);
244
  size_t GetNumWGSSolvers();
245

246
  WGSContext& GetWGSContext(int groupset_id);
247

248
  /**
249
   * Gets the local and global number of iterative unknowns. This normally is only the flux moments,
250
   * however, the sweep based solvers might include delayed angular fluxes in this number.
251
   */
252
  virtual std::pair<size_t, size_t> GetNumPhiIterativeUnknowns();
253

254
  /**
255
   * Returns a flux-moment field function for a given group and moment.
256
   *
257
   * The returned field function is created on demand from the current \c phi_new state.
258
   */
259
  std::shared_ptr<FieldFunctionGridBased> CreateScalarFluxFieldFunction(unsigned int g,
260
                                                                        unsigned int m = 0);
261

262
  /// Creates a named field function from a 1D XS or the special case `power`.
263
  std::shared_ptr<FieldFunctionGridBased> CreateFieldFunction(
264
    const std::string& name, const std::string& xs_name, double power_normalization_target = -1.0);
265

266
  bool TriggerRestartDump() const
267
  {
268
    if (options_.write_restart_time_interval <= std::chrono::seconds(0))
269
      return false;
270

271
    auto elapsed = std::chrono::system_clock::now() - options_.last_restart_write_time;
272
    return elapsed >= options_.write_restart_time_interval;
273
  }
274

275
  void UpdateRestartWriteTime()
×
276
  {
277
    options_.last_restart_write_time = std::chrono::system_clock::now();
×
278
  }
279

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

283
  /**
284
   * A method for post-processing an adjoint solution.
285
   *
286
   * @note This does nothing for diffusion-based solvers.
287
   */
UNCOV
288
  virtual void ReorientAdjointSolution() {};
×
289

UNCOV
290
  virtual void UpdatePsiOld() {};
×
291

292
protected:
293
  /// Input parameters based construction.
294
  explicit LBSProblem(const InputParameters& params);
295

296
  /// Build runtime data structures once all constructor-time configuration is complete.
297
  void BuildRuntime();
298

299
  virtual void PrintSimHeader();
300

301
  void ComputeUnitIntegrals();
302

303
  virtual void InitializeSpatialDiscretization();
304

305
  /// Initializes boundaries.
UNCOV
306
  virtual void InitializeBoundaries() {}
×
307

308
  /// Derived problems handle boundary options.
309
  virtual void SetBoundaryOptions(const InputParameters& params) = 0;
310

311
  void InitializeSolverSchemes();
312

UNCOV
313
  virtual void InitializeWGSContexts() {};
×
UNCOV
314
  virtual void InitializeWGSSolvers() {};
×
UNCOV
315
  void CheckWGSContextsInitialized();
×
UNCOV
316
  void CheckWGSSolversInitialized();
×
UNCOV
317
  void CheckAGSSolverInitialized();
×
UNCOV
318

×
319
  void SetActiveSetSourceFunction(SetSourceFunction source_function);
320

321
  std::shared_ptr<FieldFunctionGridBased> CreateEmptyFieldFunction(const std::string& name) const;
322
  std::string MakeFieldFunctionName(const std::string& base_name) const;
323

324
  LBSOptions options_;
325
  double time_ = 0.0;
326
  double theta_ = 1.0;
327
  double dt_ = 1.0;
328
  GeometryType geometry_type_ = GeometryType::INVALID;
329
  unsigned int num_moments_ = 0;
330
  unsigned int num_groups_ = 0;
331
  unsigned int scattering_order_ = 0;
332
  unsigned int num_precursors_ = 0;
333
  unsigned int max_precursors_per_material_ = 0;
334

335
  std::vector<LBSGroupset> groupsets_;
336

337
  BlockID2XSMap block_id_to_xs_map_;
338

339
  std::vector<std::shared_ptr<PointSource>> point_sources_;
340
  std::vector<std::shared_ptr<VolumetricSource>> volumetric_sources_;
341

342
  std::shared_ptr<MeshContinuum> grid_;
343
  std::shared_ptr<SpatialDiscretization> discretization_ = nullptr;
344

345
  std::vector<CellFaceNodalMapping> grid_nodal_mappings_;
346
  std::shared_ptr<MPICommunicatorSet> grid_local_comm_set_ = nullptr;
347

348
  std::vector<UnitCellMatrices> unit_cell_matrices_;
349
  std::map<uint64_t, UnitCellMatrices> unit_ghost_cell_matrices_;
350
  std::vector<CellLBSView> cell_transport_views_;
351

352
  UnknownManager flux_moments_uk_man_;
353

354
  unsigned int max_cell_dof_count_ = 0;
355
  unsigned int min_cell_dof_count_ = 0;
356
  uint64_t local_node_count_ = 0;
357
  uint64_t global_node_count_ = 0;
358

359
  std::vector<double> q_moments_local_, ext_src_moments_local_;
360
  std::vector<double> phi_new_local_, phi_old_local_;
361
  std::vector<double> precursor_new_local_;
362

363
  SetSourceFunction active_set_source_function_;
364

365
  std::shared_ptr<AGSLinearSolver> ags_solver_;
366
  std::vector<std::shared_ptr<WGSContext>> wgs_contexts_;
367
  std::vector<std::shared_ptr<LinearSolver>> wgs_solvers_;
368
  bool initialized_ = false;
369

370
  /// Data carriers needed to run the sweep on GPU.
371
  std::shared_ptr<TotalXSCarrier> total_xs_carrier_ = nullptr;
372
  std::shared_ptr<OutflowCarrier> outflow_carrier_ = nullptr;
373
  std::shared_ptr<MeshCarrier> mesh_carrier_ = nullptr;
374

375
  /// Memory pinners for source moments and destination phi.
376
  std::shared_ptr<MemoryPinner<double>> source_pinner_ = nullptr;
377
  std::shared_ptr<MemoryPinner<double>> phi_pinner_ = nullptr;
378

379
  /// Flag indicating if GPU acceleration is enabled.
380
  bool use_gpus_;
381

382
private:
383
  void InitializeRuntimeCore();
384
  void ValidateRuntimeModeConfiguration() const;
385
  void InitializeSources();
386
  /// Initializes parallel arrays.
387
  void InitializeParrays();
388
  std::string MakeScalarFluxFieldFunctionName(unsigned int g, unsigned int m) const;
389
  std::vector<double> ComputeScalarFluxFieldFunctionData(unsigned int g, unsigned int m) const;
390
  double ComputeFieldFunctionPowerScaleFactor(double power_normalization_target) const;
391
  const std::vector<double>* GetFieldFunctionCoefficients(const MultiGroupXS& xs,
392
                                                          const std::string& xs_name) const;
393
  std::vector<double> ComputeXSFieldFunctionData(const std::string& xs_name) const;
394
  std::vector<double> ComputePowerFieldFunctionData(double& local_total_power) const;
395
  /// Initializes data carriers to GPUs and memory pinner.
396
  void InitializeGPUExtras();
397
  /// Reset data carriers to null and unpin memory.
398
  void ResetGPUCarriers();
399

400
  /// Initialize groupsets
401
  void InitializeGroupsets(const InputParameters& params);
402

403
  /// Initializes materials
404
  void InitializeXSMap(const InputParameters& params);
405
  void InitializeMaterials();
406

407
  /// Initialize sources
408
  void InitializeSources(const InputParameters& params);
409

410
  void ParseOptions(const InputParameters& input);
411

412
  static std::filesystem::path BuildRestartPath(const std::string& path_stem);
413

414
  /// Checks if the current CPU is associated with any GPU.
415
  static void CheckCapableDevices();
416

417
public:
418
  /// Max number of DOFs per cell that the sweep kernel on GPU can handle.
419
  static constexpr std::uint32_t max_dofs_gpu = 10;
420

421
  /// Returns the input parameters for this object.
422
  static InputParameters GetInputParameters();
423

424
  static InputParameters GetOptionsBlock();
425

426
  static InputParameters GetXSMapEntryBlock();
427
};
428

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