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

Open-Sn / opensn / 20388244010

20 Dec 2025 12:27AM UTC coverage: 74.437% (+0.02%) from 74.415%
20388244010

push

github

web-flow
Merge pull request #882 from andrsd/issue/90-local-cell-idxs

Unifying integer types for local cell face/side indices

13 of 15 new or added lines in 2 files covered. (86.67%)

43 existing lines in 8 files now uncovered.

18677 of 25091 relevant lines covered (74.44%)

66914505.04 hits per line

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

66.25
/framework/math/unknown_manager/unknown_manager.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 <vector>
7
#include <string>
8
#include <stdexcept>
9
#include <sstream>
10
#include <iomanip>
11

12
namespace opensn
13
{
14

15
/// Different types of variables.
16
enum class UnknownType
17
{
18
  SCALAR = 1,
19
  VECTOR_2 = 2,
20
  VECTOR_3 = 3,
21
  VECTOR_N = 4,
22
  TENSOR = 5
23
};
24

25
/// Nodal variable storage format.
26
enum class UnknownStorageType
27
{
28
  NODAL = 1,
29
  BLOCK = 2
30
};
31

32
/// Basic class for an variable.
33
class Unknown
34
{
35
public:
36
  explicit Unknown(UnknownType type, unsigned int num_components = 1, unsigned int map_begin = 0)
230,992✔
37
    : type(type),
230,992✔
38
      num_components((type == UnknownType::SCALAR)     ? 1
230,992✔
39
                     : (type == UnknownType::VECTOR_2) ? 2
77,515✔
40
                     : (type == UnknownType::VECTOR_3) ? 3
77,515✔
41
                                                       : num_components),
42
      map_begin(map_begin)
230,992✔
43
  {
44
    component_names.resize(num_components, std::string());
230,992✔
45
    for (unsigned int c = 0; c < num_components; ++c)
1,661,735✔
46
    {
47
      std::ostringstream oss;
1,430,743✔
48
      oss << ' ' << std::setw(3) << std::setfill('0') << c;
1,430,743✔
49
      component_names[c] = oss.str();
1,430,743✔
50
    }
1,430,743✔
51
    num_off_block_connections.resize(num_components, 0);
230,992✔
52
  }
230,992✔
53

54
  unsigned int GetMap(unsigned int component_number = 0) const
2,147,483,647✔
55
  {
56
    unsigned int map_value = 0;
2,147,483,647✔
57
    switch (type)
2,147,483,647✔
58
    {
59
      case UnknownType::SCALAR:
196,766,658✔
60
        if (component_number >= num_components)
196,766,658✔
61
          throw std::out_of_range("Attempting to access component " +
×
UNCOV
62
                                  std::to_string(component_number) +
×
63
                                  ">=1"
UNCOV
64
                                  " for a SCALAR unknown.");
×
65
        map_value = 0;
66
        break;
67
      case UnknownType::VECTOR_2:
×
68
        if (component_number >= num_components)
×
69
          throw std::out_of_range("Attempting to access component " +
×
UNCOV
70
                                  std::to_string(component_number) +
×
71
                                  ">=2"
72
                                  " for a VECTOR_2 unknown.");
×
73
        map_value = map_begin + component_number;
×
74
        break;
×
75
      case UnknownType::VECTOR_3:
×
76
        if (component_number >= num_components)
×
77
          throw std::out_of_range("Attempting to access component " +
×
UNCOV
78
                                  std::to_string(component_number) +
×
79
                                  ">=3"
80
                                  " for a VECTOR_3 unknown.");
×
81
        map_value = map_begin + component_number;
×
UNCOV
82
        break;
×
83
      case UnknownType::VECTOR_N:
2,147,483,647✔
84
        if (component_number >= num_components)
2,147,483,647✔
85
          throw std::out_of_range(
×
86
            "Attempting to access component " + std::to_string(component_number) +
×
UNCOV
87
            ">=" + std::to_string(num_components) + " for a VECTOR_N unknown.");
×
88
        map_value = map_begin + component_number;
2,147,483,647✔
89
        break;
2,147,483,647✔
90
      case UnknownType::TENSOR:
×
91
        if (component_number >= num_components)
×
92
          throw std::out_of_range("Attempting to access component " +
×
93
                                  std::to_string(component_number) +
×
94
                                  ">=" + std::to_string(num_components) + " for a TENSOR unknown.");
×
95
        map_value = map_begin + component_number;
×
UNCOV
96
        break;
×
97
      default:
98
        break;
99
    }
100

101
    return map_value;
2,147,483,647✔
102
  }
103
  unsigned int GetMapEnd() const { return map_begin + num_components - 1; }
2,147,483,647✔
104

105
  unsigned int GetNumComponents() const { return num_components; }
106

107
  const UnknownType type;
108
  const unsigned int num_components;
109
  const unsigned int map_begin;
110
  std::string name;
111
  std::vector<std::string> component_names;
112
  std::vector<int> num_off_block_connections;
113
};
114

115
/// General object for the management of unknowns in mesh-based mathematical model.
116
class UnknownManager
117
{
118
public:
119
  // Constructors
120
  explicit UnknownManager(UnknownStorageType storage_type = UnknownStorageType::NODAL) noexcept
121
    : dof_storage_type(storage_type)
122
  {
123
  }
124

125
  UnknownManager(std::initializer_list<std::pair<UnknownType, unsigned int>> unknown_info_list,
126
                 UnknownStorageType storage_type = UnknownStorageType::NODAL)
127
    : dof_storage_type(storage_type)
128
  {
129
    for (const auto& uk_info : unknown_info_list)
130
      AddUnknown(uk_info.first, uk_info.second);
131
  }
132

133
  explicit UnknownManager(const std::vector<Unknown>& unknown_info_list,
134
                          UnknownStorageType storage_type = UnknownStorageType::NODAL)
135
    : dof_storage_type(storage_type)
136
  {
137
    for (const auto& uk : unknown_info_list)
138
      AddUnknown(uk.type, uk.num_components);
139
  }
140

141
  UnknownManager(std::initializer_list<Unknown> unknowns,
76,531✔
142
                 UnknownStorageType storage_type = UnknownStorageType::NODAL)
143
    : dof_storage_type(storage_type)
76,531✔
144
  {
145
    std::size_t uk_id = 0;
76,531✔
146
    for (const auto& uk : unknowns)
153,062✔
147
    {
148
      AddUnknown(uk.type, uk.num_components);
76,531✔
149
      SetUnknownName(uk_id, uk.name);
76,531✔
150
      size_t comp_id = 0;
76,531✔
151
      for (const auto& comp_name : uk.component_names)
153,195✔
152
      {
153
        SetUnknownComponentName(uk_id, comp_id, comp_name);
76,664✔
154
        ++comp_id;
76,664✔
155
      }
156

157
      ++uk_id;
76,531✔
158
    }
159
  }
76,531✔
160

161
  UnknownManager(const UnknownManager& other) = default;
162
  UnknownManager& operator=(const UnknownManager& other) = default;
163

164
  std::size_t GetNumberOfUnknowns() const { return unknowns.size(); }
165
  const Unknown& GetUnknown(std::size_t id) const { return unknowns[id]; }
166

167
  void SetDOFStorageType(const UnknownStorageType storage_type) { dof_storage_type = storage_type; }
168

169
  UnknownStorageType GetDOFStorageType() const { return dof_storage_type; }
170

171
  void Clear() { unknowns.clear(); }
172

173
  /**
174
   * Adds an unknown to the manager. This method will figure out where the last unknown ends and
175
   * where to begin the next one.
176
   */
177
  std::size_t AddUnknown(UnknownType unk_type, unsigned int dimension = 0);
178

179
  /// Maps the unknown's component within the storage of a node.
180
  std::size_t MapUnknown(std::size_t unknown_id, unsigned int component = 0) const;
181

182
  /// Determines the total number of components over all unknowns.
183
  std::size_t GetTotalUnknownStructureSize() const;
184

185
  /**
186
   * Sets the number of off block connections for the given unknown. All the components will be set
187
   * to the same amount.
188
   */
189
  void SetUnknownNumOffBlockConnections(std::size_t unknown_id, int num_conn);
190

191
  /// Sets the number of off block connections for the given unknown-component pair.
192
  void SetUnknownComponentNumOffBlockConnections(std::size_t unknown_id,
193
                                                 unsigned int component,
194
                                                 int num_conn);
195

196
  /// Sets a unk_name for the indicated unknown
197
  void SetUnknownName(std::size_t unknown_id, const std::string& unk_name);
198

199
  /// Sets the component_name to be associated with each component of the unknown.
200
  void SetUnknownComponentName(std::size_t unknown_id,
201
                               unsigned int component,
202
                               const std::string& component_name);
203

204
  ~UnknownManager() = default;
77,908✔
205

1,007,234✔
206
  std::vector<Unknown> unknowns;
435✔
207
  UnknownStorageType dof_storage_type;
435✔
208

870✔
209
public:
435✔
210
  // Utilities
435✔
211
  static UnknownManager GetUnitaryUnknownManager()
105✔
212
  {
836✔
213
    return UnknownManager({std::make_pair(UnknownType::SCALAR, 0)});
836✔
214
  }
102,091✔
215
};
4✔
216

4✔
217
} // namespace opensn
522✔
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