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

Open-Sn / opensn / 16821314466

07 Aug 2025 04:57PM UTC coverage: 74.527% (+1.0%) from 73.488%
16821314466

push

github

web-flow
Merge pull request #708 from wdhawkins/curvilinear_warning

Adding experimental warning to curvilinear solver

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

250 existing lines in 17 files now uncovered.

17543 of 23539 relevant lines covered (74.53%)

44793362.81 hits per line

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

44.44
/framework/parameters/parameter_block.cc
1
// SPDX-FileCopyrightText: 2024 The OpenSn Authors <https://open-sn.github.io/opensn/>
2
// SPDX-License-Identifier: MIT
3

4
#include "framework/parameters/parameter_block.h"
5
#include <algorithm>
6
#include <memory>
7
#include <sstream>
8
#include <iostream>
9

10
namespace opensn
11
{
12

13
std::string
14
ParameterBlockTypeName(ParameterBlockType type)
×
15
{
16
  switch (type)
×
17
  {
18
    case ParameterBlockType::BOOLEAN:
×
19
      return "BOOLEAN";
×
20
    case ParameterBlockType::FLOAT:
×
21
      return "FLOAT";
×
22
    case ParameterBlockType::STRING:
×
23
      return "STRING";
×
24
    case ParameterBlockType::INTEGER:
×
25
      return "INTEGER";
×
26
    case ParameterBlockType::ARRAY:
×
27
      return "ARRAY";
×
28
    case ParameterBlockType::BLOCK:
×
29
      return "BLOCK";
×
30
    case ParameterBlockType::USER_DATA:
×
31
      return "USER_DATA";
×
32
    default:
×
33
      throw std::logic_error(std::string(__PRETTY_FUNCTION__) + ": No name associated with type");
×
34
  }
35
}
36

37
void
38
ParameterBlock::SetBlockName(const std::string& name)
312✔
39
{
40
  name_ = name;
312✔
41
}
312✔
42

43
ParameterBlock::ParameterBlock(const std::string& name)
18,321✔
44
  : type_(ParameterBlockType::BLOCK), name_(name), value_ptr_(nullptr)
18,321✔
45
{
46
}
18,321✔
47

48
ParameterBlock::ParameterBlock(const ParameterBlock& other)
183,530✔
49
{
50
  type_ = other.type_;
183,530✔
51
  name_ = other.name_;
183,530✔
52
  if (other.value_ptr_)
183,530✔
53
    value_ptr_ = std::make_unique<Varying>(*other.value_ptr_);
337,228✔
54
  parameters_ = other.parameters_;
183,530✔
55
  error_origin_scope_ = other.error_origin_scope_;
183,530✔
56
}
183,530✔
57

58
ParameterBlock&
59
ParameterBlock::operator=(const ParameterBlock& other)
21,663✔
60
{
61
  if (this != &other)
21,663✔
62
  {
63
    type_ = other.type_;
21,663✔
64
    name_ = other.name_;
21,663✔
65
    if (other.value_ptr_)
21,663✔
66
      value_ptr_ = std::make_unique<Varying>(*other.value_ptr_);
27,366✔
67
    parameters_ = other.parameters_;
21,663✔
68
    error_origin_scope_ = other.error_origin_scope_;
21,663✔
69
  }
70

71
  return *this;
21,663✔
72
}
73

74
ParameterBlock::ParameterBlock(ParameterBlock&& other) noexcept
8,925,811✔
75
{
76
  std::swap(type_, other.type_);
8,925,811✔
77
  std::swap(name_, other.name_);
8,925,811✔
78
  std::swap(value_ptr_, other.value_ptr_);
8,925,811✔
79
  std::swap(parameters_, other.parameters_);
8,925,811✔
80
  std::swap(error_origin_scope_, other.error_origin_scope_);
8,925,811✔
81
}
8,925,811✔
82

83
ParameterBlock&
84
ParameterBlock::operator=(ParameterBlock&& other) noexcept
15,918,596✔
85
{
86
  if (this != &other)
15,918,596✔
87
  {
88
    std::swap(type_, other.type_);
15,918,596✔
89
    std::swap(name_, other.name_);
15,918,596✔
90
    std::swap(value_ptr_, other.value_ptr_);
15,918,596✔
91
    std::swap(parameters_, other.parameters_);
15,918,596✔
92
    std::swap(error_origin_scope_, other.error_origin_scope_);
15,918,596✔
93
  }
94

95
  return *this;
15,918,596✔
96
}
97

98
// Accessors
99
ParameterBlockType
100
ParameterBlock::GetType() const
329,138✔
101
{
102
  return type_;
329,138✔
103
}
104

105
bool
106
ParameterBlock::IsScalar() const
×
107
{
108
  return (type_ >= ParameterBlockType::BOOLEAN and type_ <= ParameterBlockType::INTEGER);
×
109
}
110
std::string
111
ParameterBlock::GetTypeName() const
×
112
{
113
  return ParameterBlockTypeName(type_);
×
114
}
115
std::string
116
ParameterBlock::GetName() const
144,849,819✔
117
{
118
  return name_;
144,849,819✔
119
}
120

121
const Varying&
122
ParameterBlock::GetValue() const
78,882✔
123
{
124
  switch (this->GetType())
78,882✔
125
  {
126
    case ParameterBlockType::BOOLEAN:
78,882✔
127
    case ParameterBlockType::FLOAT:
78,882✔
128
    case ParameterBlockType::STRING:
78,882✔
129
    case ParameterBlockType::INTEGER:
78,882✔
130
    case ParameterBlockType::USER_DATA:
78,882✔
131
    {
78,882✔
132
      if (value_ptr_ == nullptr)
78,882✔
133
        throw std::runtime_error(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) +
×
134
                                 ": Uninitialized Varying value for block " + this->GetName());
×
135
      return *value_ptr_;
78,882✔
136
    }
137
    default:
×
138
      throw std::logic_error(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) + ":\"" +
×
139
                             this->GetName() +
×
140
                             "\""
141
                             " Called for block of type " +
×
142
                             ParameterBlockTypeName(this->GetType()) + " which has no value.");
×
143
  }
144
}
145

146
size_t
147
ParameterBlock::GetNumParameters() const
63,022✔
148
{
149
  return parameters_.size();
63,022✔
150
}
151

152
const std::vector<ParameterBlock>&
153
ParameterBlock::GetParameters() const
16,024✔
154
{
155
  return parameters_;
16,024✔
156
}
157

158
bool
UNCOV
159
ParameterBlock::HasValue() const
×
160
{
UNCOV
161
  return value_ptr_ != nullptr;
×
162
}
163

164
// Mutators
165

166
void
167
ParameterBlock::ChangeToArray()
5,437✔
168
{
169
  const std::string fname = __PRETTY_FUNCTION__;
5,437✔
170
  if (parameters_.empty())
5,437✔
171
  {
172
    type_ = ParameterBlockType::ARRAY;
5,423✔
173
    return;
5,423✔
174
  }
175

176
  const auto& first_param = parameters_.front();
177
  for (const auto& param : parameters_)
40✔
178
    if (param.GetType() != first_param.GetType())
26✔
179
      throw std::logic_error(error_origin_scope_ + fname +
×
180
                             ": Cannot change ParameterBlock to "
181
                             "array. It has existing parameters and they are not of the same"
182
                             "type.");
×
183

184
  type_ = ParameterBlockType::ARRAY;
14✔
185
}
5,437✔
186

187
// NOLINTBEGIN(misc-no-recursion)
188
void
189
ParameterBlock::SetErrorOriginScope(const std::string& scope)
14,434✔
190
{
191
  error_origin_scope_ = scope;
14,434✔
192
  for (auto& param : parameters_)
26,640✔
193
    param.SetErrorOriginScope(scope);
12,206✔
194
}
14,434✔
195
// NOLINTEND(misc-no-recursion)
196

197
void
198
ParameterBlock::RequireBlockTypeIs(ParameterBlockType type) const
1,559✔
199
{
200
  if (GetType() != type)
1,559✔
201
    throw std::logic_error(error_origin_scope_ + ":" + GetName() + " Is required to be of type " +
×
202
                           ParameterBlockTypeName(type) + " but is " +
×
203
                           ParameterBlockTypeName(GetType()));
×
204
}
1,559✔
205

206
void
207
ParameterBlock::RequireParameter(const std::string& param_name) const
×
208
{
209
  if (not Has(param_name))
×
210
    throw std::logic_error(error_origin_scope_ + ":" + GetName() +
×
211
                           " Is required to have parameter " + param_name);
×
212
}
×
213

214
void
215
ParameterBlock::AddParameter(ParameterBlock block)
101,088✔
216
{
217
  for (const auto& param : parameters_)
7,706,304✔
218
    if (param.GetName() == block.GetName())
7,605,216✔
219
      throw std::invalid_argument(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) +
×
220
                                  ": Attempting to add duplicate parameter " + param.GetName() +
×
221
                                  " to "
222
                                  "block " +
×
223
                                  this->GetName());
×
224
  parameters_.push_back(std::move(block));
101,088✔
225

226
  SortParameters();
101,088✔
227
}
101,088✔
228

229
void
230
ParameterBlock::SortParameters()
101,088✔
231
{
232
  struct AlphabeticFunctor
101,088✔
233
  {
234
    bool operator()(const ParameterBlock& paramA, const ParameterBlock& paramB)
1,051,965✔
235
    {
236
      return paramA.GetName() < paramB.GetName();
1,051,965✔
237
    }
238
  };
239

240
  struct AlphabeticNumericFunctor
101,088✔
241
  {
242
    bool operator()(const ParameterBlock& paramA, const ParameterBlock& paramB)
63,493,406✔
243
    {
244
      return std::stoi(paramA.GetName()) < std::stoi(paramB.GetName());
190,480,218✔
245
    }
246
  };
247

248
  // The different functor here is necessary when the parameters are guaranteed
249
  // to have integer names that were converted to strings. It never showed up
250
  // because we were not testing with enough values. Essentially "11" < "2" in
251
  // the realm of strings but not in integer world.
252
  if (this->GetType() != ParameterBlockType::ARRAY)
101,088✔
253
    std::sort(parameters_.begin(), parameters_.end(), AlphabeticFunctor());
47,146✔
254
  else
255
    std::sort(parameters_.begin(), parameters_.end(), AlphabeticNumericFunctor());
53,942✔
256
}
101,088✔
257

258
bool
259
ParameterBlock::Has(const std::string& param_name) const
64,521✔
260
{
261
  return std::any_of(parameters_.begin(),
64,521✔
262
                     parameters_.end(),
263
                     [&param_name](const ParameterBlock& param)
546,833✔
264
                     { return param.name_ == param_name; });
168,788✔
265
}
266

267
ParameterBlock&
268
ParameterBlock::GetParam(const std::string& param_name)
28,607✔
269
{
270
  for (auto& param : parameters_)
220,968✔
271
    if (param.GetName() == param_name)
220,968✔
272
      return param;
28,607✔
273

274
  throw std::out_of_range(error_origin_scope_ + ":" + std::string(__PRETTY_FUNCTION__) +
×
275
                          ": Parameter \"" + param_name + "\" not present in block");
×
276
}
277

278
ParameterBlock&
279
ParameterBlock::GetParam(size_t index)
7,616✔
280
{
281
  try
7,616✔
282
  {
283
    return parameters_.at(index);
7,616✔
284
  }
285
  catch (const std::out_of_range& oor)
×
286
  {
287
    throw std::out_of_range(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) +
×
288
                            ": Parameter with index " + std::to_string(index) +
×
289
                            " not present in block");
×
290
  }
×
291
}
292

293
const ParameterBlock&
294
ParameterBlock::GetParam(const std::string& param_name) const
20,762✔
295
{
296
  for (const auto& param : parameters_)
123,719✔
297
    if (param.GetName() == param_name)
123,719✔
298
      return param;
20,762✔
299

300
  throw std::out_of_range(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) +
×
301
                          ": Parameter \"" + param_name + "\" not present in block");
×
302
}
303

304
const ParameterBlock&
305
ParameterBlock::GetParam(size_t index) const
52,998✔
306
{
307
  try
52,998✔
308
  {
309
    return parameters_.at(index);
52,998✔
310
  }
311
  catch (const std::out_of_range& oor)
×
312
  {
313
    throw std::out_of_range(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) +
×
314
                            ": Parameter with index " + std::to_string(index) +
×
315
                            " not present in block");
×
316
  }
×
317
}
318

319
//  NOLINTBEGIN(misc-no-recursion)
320
void
321
ParameterBlock::RecursiveDumpToString(std::string& outstr, const std::string& offset) const
×
322
{
323
  outstr += offset + this->GetName() + " = \n";
×
324
  outstr += offset + "{\n";
×
325

326
  if (HasValue())
×
327
    outstr += value_ptr_->PrintStr();
×
328

329
  for (const auto& param : parameters_)
×
330
  {
331

332
    switch (param.GetType())
×
333
    {
334
      case ParameterBlockType::BOOLEAN:
×
335
      {
×
336
        outstr += offset + "  " + param.GetName() + " = ";
×
337
        const bool value = param.GetValue().GetBoolValue();
×
338
        outstr += std::string(value ? "true" : "false") + ",\n";
×
339
        break;
×
340
      }
341
      case ParameterBlockType::FLOAT:
×
342
      {
×
343
        outstr += offset + "  " + param.GetName() + " = ";
×
344
        const double value = param.GetValue().GetFloatValue();
×
345
        outstr += std::to_string(value) + ",\n";
×
346
        break;
×
347
      }
348
      case ParameterBlockType::STRING:
×
349
      {
×
350
        outstr += offset + "  " + param.GetName() + " = ";
×
351
        const auto& value = param.GetValue().GetStringValue();
×
352
        outstr += "\"" + value + "\",\n";
×
353
        break;
×
354
      }
×
355
      case ParameterBlockType::INTEGER:
×
356
      {
×
357
        outstr += offset + "  " + param.GetName() + " = ";
×
358
        const int64_t value = param.GetValue().GetIntegerValue();
×
359
        outstr += std::to_string(value) + ",\n";
×
360
        break;
×
361
      }
362
      case ParameterBlockType::ARRAY:
×
363
      case ParameterBlockType::BLOCK:
×
364
      {
×
365
        param.RecursiveDumpToString(outstr, offset + "  ");
×
366
        break;
×
367
      }
368
      default:
369
        break;
370
    }
371
  } // for parameter
372

373
  outstr += offset + "}\n";
×
374
}
×
375
// NOLINTEND(misc-no-recursion)
376

377
//  NOLINTBEGIN(misc-no-recursion)
378
void
379
ParameterBlock::RecursiveDumpToJSON(std::string& outstr) const
×
380
{
381
  if (HasValue())
×
382
  {
383
    outstr += value_ptr_->PrintStr(false);
×
384
    return;
×
385
  }
386

387
  outstr += (this->GetType() == ParameterBlockType::ARRAY ? "[" : "{");
×
388
  for (const auto& param : parameters_)
×
389
  {
390

391
    switch (param.GetType())
×
392
    {
393
      case ParameterBlockType::BOOLEAN:
×
394
      {
×
395
        outstr += "\"" + param.GetName() + "\" = ";
×
396
        const bool value = param.GetValue().GetBoolValue();
×
397
        outstr += std::string(value ? "true" : "false") + ",\n";
×
398
        break;
×
399
      }
400
      case ParameterBlockType::FLOAT:
×
401
      {
×
402
        outstr += "\"" + param.GetName() + "\" = ";
×
403
        const double value = param.GetValue().GetFloatValue();
×
404
        outstr += std::to_string(value) + ",\n";
×
405
        break;
×
406
      }
407
      case ParameterBlockType::STRING:
×
408
      {
×
409
        outstr += "\"" + param.GetName() + "\" = ";
×
410
        const auto& value = param.GetValue().GetStringValue();
×
411
        outstr += "\"" + value + "\",\n";
×
412
        break;
×
413
      }
×
414
      case ParameterBlockType::INTEGER:
×
415
      {
×
416
        outstr += "\"" + param.GetName() + "\" = ";
×
417
        const int64_t value = param.GetValue().GetIntegerValue();
×
418
        outstr += std::to_string(value) + ",\n";
×
419
        break;
×
420
      }
421
      case ParameterBlockType::ARRAY:
×
422
      case ParameterBlockType::BLOCK:
×
423
      {
×
424
        param.RecursiveDumpToJSON(outstr);
×
425
        break;
426
      }
427
      default:
428
        break;
429
    }
430
  } // for parameter
431
  outstr += (this->GetType() == ParameterBlockType::ARRAY ? "]" : "}");
×
432
}
433
// NOLINTEND(misc-no-recursion)
434

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