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

Open-Sn / opensn / 18380674863

09 Oct 2025 02:45PM UTC coverage: 75.202% (+0.3%) from 74.862%
18380674863

push

github

web-flow
Merge pull request #793 from wdhawkins/remove_cmake_debug_flags

Removing forced use of -O0 for Debug config

18014 of 23954 relevant lines covered (75.2%)

56527302.01 hits per line

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

65.82
/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

10
namespace opensn
11
{
12

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

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

30
/// Basic class for an variable.
31
class Unknown
32
{
33
public:
34
  const UnknownType type;
35
  const unsigned int num_components;
36
  const unsigned int map_begin;
37
  std::string name;
38
  std::vector<std::string> component_names;
39
  std::vector<int> num_off_block_connections;
40

41
public:
42
  explicit Unknown(UnknownType type, unsigned int num_components = 1, unsigned int map_begin = 0)
197,618✔
43
    : type(type),
197,618✔
44
      num_components((type == UnknownType::SCALAR)     ? 1
197,618✔
45
                     : (type == UnknownType::VECTOR_2) ? 2
51,716✔
46
                     : (type == UnknownType::VECTOR_3) ? 3
51,716✔
47
                                                       : num_components),
48
      map_begin(map_begin)
197,618✔
49
  {
50
    component_names.resize(num_components, std::string());
197,618✔
51
    for (unsigned int c = 0; c < num_components; ++c)
1,413,062✔
52
    {
53

54
      char buffer[100];
1,215,444✔
55
      snprintf(buffer, 100, " %03d", c);
1,215,444✔
56
      component_names[c] = buffer;
1,215,444✔
57
    }
58
    num_off_block_connections.resize(num_components, 0);
197,618✔
59
  }
197,618✔
60

61
  unsigned int GetMap(unsigned int component_number = 0) const
1,833,906,838✔
62
  {
63
    unsigned int map_value = 0;
1,833,906,838✔
64
    switch (type)
1,833,906,838✔
65
    {
66
      case UnknownType::SCALAR:
171,189,122✔
67
        if (component_number >= num_components)
171,189,122✔
68
          throw std::out_of_range("Attempting to access component " +
×
69
                                  std::to_string(component_number) +
×
70
                                  ">=1"
71
                                  " for a SCALAR unknown.");
×
72
        map_value = 0;
73
        break;
74
      case UnknownType::VECTOR_2:
×
75
        if (component_number >= num_components)
×
76
          throw std::out_of_range("Attempting to access component " +
×
77
                                  std::to_string(component_number) +
×
78
                                  ">=2"
79
                                  " for a VECTOR_2 unknown.");
×
80
        map_value = map_begin + component_number;
×
81
        break;
×
82
      case UnknownType::VECTOR_3:
×
83
        if (component_number >= num_components)
×
84
          throw std::out_of_range("Attempting to access component " +
×
85
                                  std::to_string(component_number) +
×
86
                                  ">=3"
87
                                  " for a VECTOR_3 unknown.");
×
88
        map_value = map_begin + component_number;
×
89
        break;
×
90
      case UnknownType::VECTOR_N:
1,662,717,716✔
91
        if (component_number >= num_components)
1,662,717,716✔
92
          throw std::out_of_range(
×
93
            "Attempting to access component " + std::to_string(component_number) +
×
94
            ">=" + std::to_string(num_components) + " for a VECTOR_N unknown.");
×
95
        map_value = map_begin + component_number;
1,662,717,716✔
96
        break;
1,662,717,716✔
97
      case UnknownType::TENSOR:
×
98
        if (component_number >= num_components)
×
99
          throw std::out_of_range("Attempting to access component " +
×
100
                                  std::to_string(component_number) +
×
101
                                  ">=" + std::to_string(num_components) + " for a TENSOR unknown.");
×
102
        map_value = map_begin + component_number;
×
103
        break;
×
104
      default:
105
        break;
106
    }
107

108
    return map_value;
1,833,906,838✔
109
  }
110
  unsigned int GetMapEnd() const { return map_begin + num_components - 1; }
1,834,139,215✔
111

112
  unsigned int GetNumComponents() const { return num_components; }
113
};
114

115
/// General object for the management of unknowns in mesh-based mathematical model.
116
class UnknownManager
117
{
118
private:
119
public:
120
  std::vector<Unknown> unknowns;
121
  UnknownStorageType dof_storage_type;
122

123
  // Constructors
124
  explicit UnknownManager(UnknownStorageType storage_type = UnknownStorageType::NODAL) noexcept
125
    : dof_storage_type(storage_type)
126
  {
127
  }
128

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

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

145
  UnknownManager(std::initializer_list<Unknown> unknowns,
72,775✔
146
                 UnknownStorageType storage_type = UnknownStorageType::NODAL)
147
    : dof_storage_type(storage_type)
72,775✔
148
  {
149
    size_t uk_id = 0;
72,775✔
150
    for (const auto& uk : unknowns)
145,550✔
151
    {
152
      AddUnknown(uk.type, uk.num_components);
72,775✔
153
      SetUnknownName(uk_id, uk.name);
72,775✔
154
      size_t comp_id = 0;
72,775✔
155
      for (const auto& comp_name : uk.component_names)
145,683✔
156
      {
157
        SetUnknownComponentName(uk_id, comp_id, comp_name);
72,908✔
158
        ++comp_id;
72,908✔
159
      }
160

161
      ++uk_id;
72,775✔
162
    }
163
  }
72,775✔
164

165
  UnknownManager(const UnknownManager& other) = default;
166
  UnknownManager& operator=(const UnknownManager& other) = default;
167

168
  // Utilities
169
  static UnknownManager GetUnitaryUnknownManager()
170
  {
171
    return UnknownManager({std::make_pair(UnknownType::SCALAR, 0)});
172
  }
173

174
  size_t GetNumberOfUnknowns() const { return unknowns.size(); }
175
  const Unknown& GetUnknown(size_t id) const { return unknowns[id]; }
176

177
  void SetDOFStorageType(const UnknownStorageType storage_type) { dof_storage_type = storage_type; }
178

179
  UnknownStorageType GetDOFStorageType() const { return dof_storage_type; }
180

181
  void Clear() { unknowns.clear(); }
182

183
  /**
184
   * Adds an unknown to the manager. This method will figure out where the last unknown ends and
185
   * where to begin the next one.
186
   */
187
  unsigned int AddUnknown(UnknownType unk_type, unsigned int dimension = 0);
188

189
  /// Maps the unknown's component within the storage of a node.
190
  unsigned int MapUnknown(int unknown_id, unsigned int component = 0) const;
191

192
  /// Determines the total number of components over all unknowns.
193
  unsigned int GetTotalUnknownStructureSize() const;
194

195
  /**
196
   * Sets the number of off block connections for the given unknown. All the components will be set
197
   * to the same amount.
198
   */
199
  void SetUnknownNumOffBlockConnections(int unknown_id, int num_conn);
200

201
  /// Sets the number of off block connections for the given unknown-component pair.
202
  void
203
  SetUnknownComponentNumOffBlockConnections(int unknown_id, unsigned int component, int num_conn);
204

205
  /// Sets a unk_name for the indicated unknown
206
  void SetUnknownName(int unknown_id, const std::string& unk_name);
207

208
  /// Sets the component_name to be associated with each component of the unknown.
209
  void SetUnknownComponentName(int unknown_id,
210
                               unsigned int component,
211
                               const std::string& component_name);
212

213
  ~UnknownManager() = default;
73,953✔
214
};
1,007,230✔
215

372✔
216
} // namespace opensn
372✔
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