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

Open-Sn / opensn / #5

02 Apr 2026 01:04PM UTC coverage: 74.858% (-1.2%) from 76.085%
#5

push

web-flow
Merge pull request #1009 from andrsd/unit-tests

Removing `opensn-test`

20988 of 28037 relevant lines covered (74.86%)

65922489.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/framework/math/spatial_discretization/spatial_discretization.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 "framework/math/spatial_discretization/cell_mappings/cell_mapping.h"
7
#include "framework/math/quadratures/spatial/spatial_quadrature.h"
8
#include "framework/math/unknown_manager/unknown_manager.h"
9
#include "framework/mesh/mesh_continuum/mesh_continuum.h"
10
#include "framework/mesh/cell/cell.h"
11
#include "framework/mesh/mesh.h"
12
#include "framework/math/math.h"
13
#include <petscksp.h>
14
#include <cassert>
15
#include <vector>
16
#include <map>
17
#include <set>
18

19
namespace opensn
20
{
21
class SpatialDiscretization
22
{
23
public:
24
  const UnknownManager UNITARY_UNKNOWN_MANAGER;
25

26
  /**
27
   * Utility method for getting node indices seperately for domain internal local nodes, and
28
   * boundary nodes.
29
   */
30
  std::pair<std::set<uint32_t>, std::set<uint32_t>>
31
  MakeCellInternalAndBndryNodeIDs(const Cell& cell) const;
32

33
  const CellMapping& GetCellMapping(const Cell& cell) const
2,303,490✔
34
  {
35
    assert(cell.local_id < cell_mappings_.size());
2,303,490✔
36
    if (GetGrid()->IsCellLocal(cell.global_id))
4,606,980✔
37
      return *cell_mappings_[cell.local_id];
2,303,490✔
38
    return *nb_cell_mappings_.at(cell.global_id);
×
39
  }
40

41
  SpatialDiscretizationType GetType() const;
42

43
  /// Returns the reference grid on which this discretization is based.
44
  std::shared_ptr<MeshContinuum> GetGrid() const;
45

46
  /**
47
   * Builds the sparsity pattern for a local block matrix compatible withthe given unknown manager.
48
   * The modified vectors are: `nodal_nnz_in_diag` which specifies for each row the number of
49
   * non-zeros in the local diagonal block, `nodal_nnz_off_diag` which specifies for each row the
50
   * number of non-zeros in the off-diagonal block.
51
   */
52
  virtual void BuildSparsityPattern(std::vector<int64_t>& nodal_nnz_in_diag,
53
                                    std::vector<int64_t>& nodal_nnz_off_diag,
54
                                    const UnknownManager& unknown_manager) const = 0;
55

56
  /// Maps the global address of a degree of freedom.
57
  virtual uint64_t MapDOF(const Cell& cell,
58
                          unsigned int node,
59
                          const UnknownManager& unknown_manager,
60
                          unsigned int unknown_id,
61
                          unsigned int component) const = 0;
62

63
  /// Maps the local address of a degree of freedom. This can include ghost entries if the specific
64
  /// discretization has any.
65
  virtual uint64_t MapDOFLocal(const Cell& cell,
66
                               unsigned int node,
67
                               const UnknownManager& unknown_manager,
68
                               unsigned int unknown_id,
69
                               unsigned int component) const = 0;
70

71
  /**
72
   * Maps the local address of a degree of freedom. This can include ghost entries if the specific
73
   * discretization has any. Default structure here is a single scalar unknown.
74
   */
75
  virtual uint64_t MapDOF(const Cell& cell, unsigned int node) const = 0;
76

77
  /**
78
   * Maps the local address of a degree of freedom. This can include ghost entries if the specific
79
   * discretization has any. Default structure here is a single scalar unknown.
80
   */
81
  virtual uint64_t MapDOFLocal(const Cell& cell, unsigned int node) const = 0;
82

83
  /// Returns the number of local nodes used in this discretization.
84
  size_t GetNumLocalNodes() const;
85

86
  /// Returns the number of global nodes used in this discretization.
87
  size_t GetNumGlobalNodes() const;
88

89
  /**
90
   * For the unknown structure in the unknown manager, returns the number of local
91
   * degrees-of-freedom.
92
   */
93
  size_t GetNumLocalDOFs(const UnknownManager& unknown_manager) const;
94

95
  /**
96
   * For the unknown structure in the unknown manager, returns the number of global
97
   * degrees-of-freedom.
98
   */
99
  size_t GetNumGlobalDOFs(const UnknownManager& unknown_manager) const;
100

101
  /**
102
   * For the unknown structure in the unknown manager, returns the number of ghost
103
   * degrees-of-freedom.
104
   */
105
  virtual size_t GetNumGhostDOFs(const UnknownManager& unknown_manager) const = 0;
106

107
  /**
108
   *For the unknown structure in the unknown manager, returns the global IDs of all the ghost
109
   * degrees-of-freedom.
110
   */
111
  virtual std::vector<uint64_t> GetGhostDOFIndices(const UnknownManager& unknown_manager) const = 0;
112

113
  /**
114
   * For the unknown structure in the unknown manager, returns the number of local- and ghost
115
   * degrees-of-freedom.
116
   */
117
  size_t GetNumLocalAndGhostDOFs(const UnknownManager& unknown_manager) const;
118

119
  /**
120
   * For the given cell, returns the number of relevant nodes. The same can be achieved by
121
   * retrieving the cell-to-element mapping first.
122
   */
123
  size_t GetCellNumNodes(const Cell& cell) const;
124

125
  /**
126
   * For the given cell, returns a reference to the relevant node locations. The same can be
127
   * achieved by retrieving the cell-to-element mapping first.
128
   */
129
  const std::vector<Vector3>& GetCellNodeLocations(const Cell& cell) const;
130

131
  /**
132
   * For each cell, for each face of that cell, for each node on that face, maps to which local
133
   * node on the adjacent cell that node position corresponds.
134
   *
135
   * \param tolerance double. Tolerance to use to determine if two node locations are
136
   *                            equal. [Default: 1.0e-12]
137
   *
138
   *  For example consider two adjacent quadrilaterals.
139
   *
140
   * \verbatim
141
   * o--------o--------o                       o--------o
142
   * |3      2|3      2|                       |    2   |
143
   * |  101   |   102  | , face ids for both:  |3      1|
144
   * |0      1|0      1|                       |    0   |
145
   * o--------o--------o                       o--------o
146
   * internal face for cell 101 is face-1, ccw orientated
147
   * --o
148
   *  1|
149
   *   |
150
   *  0|
151
   * --o
152
   * internal face for cell 102 is face-3, ccw orientated
153
   * o-
154
   * |0
155
   * |
156
   * |1
157
   * o-
158
   *
159
   * mapping[101][1][0] = 0
160
   * mapping[101][1][1] = 3
161
   *
162
   * mapping[102][3][0] = 2
163
   * mapping[102][3][1] = 1
164
   * \endverbatim
165
   */
166
  std::vector<std::vector<std::vector<int>>>
167
  MakeInternalFaceNodeMappings(double tolerance = 1.0e-12) const;
168

169
  /**
170
   * Copy part of vector A to vector B. Suppose vector A's entries are managed `UnknownManager` A
171
   * (`uk_manA`) and that the entries of the vector B are managed by `UnknownManager` B (`uk_manB`).
172
   * This function copies the entries associated with an unknown with id `uk_id_A` in `uk_manA` from
173
   * vector A to vector B such that the entries in vector B are aligned with the entries of an
174
   * unknown with id `uk_id_B` in `uk_manB`. All the components are copied.
175
   *
176
   * \param from_vector Vector to copy from.
177
   * \param to_vector Vector to copy to.
178
   * \param from_vec_uk_structure Unknown manager for vector A.
179
   * \param from_vec_uk_id Unknown-id in unknown manager A.
180
   * \param to_vec_uk_structure Unknown manager for vector B.
181
   * \param to_vec_uk_id Unknown-id in unknown manager B.
182
   */
183
  void CopyVectorWithUnknownScope(const std::vector<double>& from_vector,
184
                                  std::vector<double>& to_vector,
185
                                  const UnknownManager& from_vec_uk_structure,
186
                                  unsigned int from_vec_uk_id,
187
                                  const UnknownManager& to_vec_uk_structure,
188
                                  unsigned int to_vec_uk_id) const;
189

190
  /**
191
   * Develops a localized view of a petsc vector. Each spatial discretization can have a
192
   * specialization of this method.
193
   */
194
  virtual void LocalizePETScVector(Vec petsc_vector,
195
                                   std::vector<double>& local_vector,
196
                                   const UnknownManager& unknown_manager) const;
197
  /**
198
   * Develops a localized view of a petsc vector. Each spatial discretization can have a
199
   * specialization of this method.
200
   */
201
  virtual void LocalizePETScVectorWithGhosts(Vec petsc_vector,
202
                                             std::vector<double>& local_vector,
203
                                             const UnknownManager& unknown_manager) const;
204

205
  using SpatialWeightFunction = std::function<double(const Vector3&)>;
206

207
  /// Returns the spatial weighting function appropriate to the discretization's coordinate system.
208
  SpatialWeightFunction GetSpatialWeightingFunction() const;
209

210
  virtual ~SpatialDiscretization() = default;
1,420✔
211

212
protected:
213
  SpatialDiscretization(std::shared_ptr<MeshContinuum> grid, SpatialDiscretizationType sdm_type);
214

215
  const std::shared_ptr<MeshContinuum> grid_;
216
  std::vector<std::unique_ptr<CellMapping>> cell_mappings_;
217
  std::map<uint64_t, std::shared_ptr<CellMapping>> nb_cell_mappings_;
218

219
  uint64_t local_block_address_ = 0;
220
  std::vector<uint64_t> locJ_block_address_;
221
  std::vector<uint64_t> locJ_block_size_;
222

223
  uint64_t local_base_block_size_ = 0;
224
  uint64_t global_base_block_size_ = 0;
225

226
private:
227
  const SpatialDiscretizationType type_;
228

229
public:
230
  /// Cartesian coordinate system spatial weighting function.
231
  static double CartesianSpatialWeightFunction(const Vector3& point);
232

233
  /// Cylindrical coordinate system (RZ) spatial weighting function.
234
  static double CylindricalRZSpatialWeightFunction(const Vector3& point);
235

236
  /// Spherical coordinate system (1D Spherical) spatial weighting function.
237
  static double Spherical1DSpatialWeightFunction(const Vector3& point);
238
};
239

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