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

Open-Sn / opensn / 17934392714

23 Sep 2025 01:57AM UTC coverage: 74.703% (+0.08%) from 74.627%
17934392714

push

github

web-flow
Merge pull request #766 from wdhawkins/python_doc_updates

Updating Python interface documentation

17718 of 23718 relevant lines covered (74.7%)

44392337.32 hits per line

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

47.03
/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)
4✔
15
{
16
  switch (type)
4✔
17
  {
18
    case ParameterBlockType::BOOLEAN:
1✔
19
      return "BOOLEAN";
1✔
20
    case ParameterBlockType::FLOAT:
1✔
21
      return "FLOAT";
1✔
22
    case ParameterBlockType::STRING:
1✔
23
      return "STRING";
1✔
24
    case ParameterBlockType::INTEGER:
1✔
25
      return "INTEGER";
1✔
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)
319✔
39
{
40
  name_ = name;
319✔
41
}
319✔
42

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

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

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

71
  return *this;
17,545✔
72
}
73

74
ParameterBlock&
75
ParameterBlock::operator=(ParameterBlock&& other) noexcept
15,652,959✔
76
{
77
  if (this != &other)
15,652,959✔
78
  {
79
    std::swap(type_, other.type_);
15,652,959✔
80
    std::swap(name_, other.name_);
15,652,959✔
81
    std::swap(value_ptr_, other.value_ptr_);
15,652,959✔
82
    std::swap(parameters_, other.parameters_);
15,652,959✔
83
    std::swap(error_origin_scope_, other.error_origin_scope_);
15,652,959✔
84
  }
85

86
  return *this;
15,652,959✔
87
}
88

89
// Accessors
90
ParameterBlockType
91
ParameterBlock::GetType() const
311,750✔
92
{
93
  return type_;
311,750✔
94
}
95

96
bool
97
ParameterBlock::IsScalar() const
×
98
{
99
  return (type_ >= ParameterBlockType::BOOLEAN and type_ <= ParameterBlockType::INTEGER);
×
100
}
101
std::string
102
ParameterBlock::GetTypeName() const
×
103
{
104
  return ParameterBlockTypeName(type_);
×
105
}
106
std::string
107
ParameterBlock::GetName() const
143,547,045✔
108
{
109
  return name_;
143,547,045✔
110
}
111

112
const Varying&
113
ParameterBlock::GetValue() const
75,896✔
114
{
115
  switch (this->GetType())
75,896✔
116
  {
117
    case ParameterBlockType::BOOLEAN:
75,896✔
118
    case ParameterBlockType::FLOAT:
75,896✔
119
    case ParameterBlockType::STRING:
75,896✔
120
    case ParameterBlockType::INTEGER:
75,896✔
121
    case ParameterBlockType::USER_DATA:
75,896✔
122
    {
75,896✔
123
      if (value_ptr_ == nullptr)
75,896✔
124
        throw std::runtime_error(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) +
×
125
                                 ": Uninitialized Varying value for block " + this->GetName());
×
126
      return *value_ptr_;
75,896✔
127
    }
128
    default:
×
129
      throw std::logic_error(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) + ":\"" +
×
130
                             this->GetName() +
×
131
                             "\""
132
                             " Called for block of type " +
×
133
                             ParameterBlockTypeName(this->GetType()) + " which has no value.");
×
134
  }
135
}
136

137
size_t
138
ParameterBlock::GetNumParameters() const
59,300✔
139
{
140
  return parameters_.size();
59,300✔
141
}
142

143
const std::vector<ParameterBlock>&
144
ParameterBlock::GetParameters() const
15,461✔
145
{
146
  return parameters_;
15,461✔
147
}
148

149
bool
150
ParameterBlock::HasValue() const
×
151
{
152
  return value_ptr_ != nullptr;
×
153
}
154

155
// Mutators
156

157
void
158
ParameterBlock::ChangeToArray()
5,245✔
159
{
160
  const std::string fname = __PRETTY_FUNCTION__;
5,245✔
161
  if (parameters_.empty())
5,245✔
162
  {
163
    type_ = ParameterBlockType::ARRAY;
5,231✔
164
    return;
5,231✔
165
  }
166

167
  const auto& first_param = parameters_.front();
168
  for (const auto& param : parameters_)
40✔
169
    if (param.GetType() != first_param.GetType())
26✔
170
      throw std::logic_error(error_origin_scope_ + fname +
×
171
                             ": Cannot change ParameterBlock to "
172
                             "array. It has existing parameters and they are not of the same"
173
                             "type.");
×
174

175
  type_ = ParameterBlockType::ARRAY;
14✔
176
}
5,245✔
177

178
// NOLINTBEGIN(misc-no-recursion)
179
void
180
ParameterBlock::SetErrorOriginScope(const std::string& scope)
15,497✔
181
{
182
  error_origin_scope_ = scope;
15,497✔
183
  for (auto& param : parameters_)
28,728✔
184
    param.SetErrorOriginScope(scope);
13,231✔
185
}
15,497✔
186
// NOLINTEND(misc-no-recursion)
187

188
void
189
ParameterBlock::RequireBlockTypeIs(ParameterBlockType type) const
1,557✔
190
{
191
  if (GetType() != type)
1,557✔
192
    throw std::logic_error(error_origin_scope_ + ":" + GetName() + " Is required to be of type " +
×
193
                           ParameterBlockTypeName(type) + " but is " +
×
194
                           ParameterBlockTypeName(GetType()));
×
195
}
1,557✔
196

197
void
198
ParameterBlock::RequireParameter(const std::string& param_name) const
×
199
{
200
  if (not Has(param_name))
×
201
    throw std::logic_error(error_origin_scope_ + ":" + GetName() +
×
202
                           " Is required to have parameter " + param_name);
×
203
}
×
204

205
void
206
ParameterBlock::AddParameter(ParameterBlock block)
94,726✔
207
{
208
  for (const auto& param : parameters_)
7,589,670✔
209
    if (param.GetName() == block.GetName())
7,494,944✔
210
      throw std::invalid_argument(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) +
×
211
                                  ": Attempting to add duplicate parameter " + param.GetName() +
×
212
                                  " to "
213
                                  "block " +
×
214
                                  this->GetName());
×
215
  parameters_.push_back(std::move(block));
94,726✔
216

217
  SortParameters();
94,726✔
218
}
94,726✔
219

220
void
221
ParameterBlock::SortParameters()
94,726✔
222
{
223
  struct AlphabeticFunctor
94,726✔
224
  {
225
    bool operator()(const ParameterBlock& paramA, const ParameterBlock& paramB)
537,840✔
226
    {
227
      return paramA.GetName() < paramB.GetName();
537,840✔
228
    }
229
  };
230

231
  struct AlphabeticNumericFunctor
94,726✔
232
  {
233
    bool operator()(const ParameterBlock& paramA, const ParameterBlock& paramB)
63,549,628✔
234
    {
235
      return std::stoi(paramA.GetName()) < std::stoi(paramB.GetName());
190,648,884✔
236
    }
237
  };
238

239
  // The different functor here is necessary when the parameters are guaranteed
240
  // to have integer names that were converted to strings. It never showed up
241
  // because we were not testing with enough values. Essentially "11" < "2" in
242
  // the realm of strings but not in integer world.
243
  if (this->GetType() != ParameterBlockType::ARRAY)
94,726✔
244
    std::sort(parameters_.begin(), parameters_.end(), AlphabeticFunctor());
40,395✔
245
  else
246
    std::sort(parameters_.begin(), parameters_.end(), AlphabeticNumericFunctor());
54,331✔
247
}
94,726✔
248

249
bool
250
ParameterBlock::Has(const std::string& param_name) const
52,344✔
251
{
252
  return std::any_of(parameters_.begin(),
52,344✔
253
                     parameters_.end(),
254
                     [&param_name](const ParameterBlock& param)
317,541✔
255
                     { return param.name_ == param_name; });
106,617✔
256
}
257

258
ParameterBlock&
259
ParameterBlock::GetParam(const std::string& param_name)
23,870✔
260
{
261
  for (auto& param : parameters_)
136,979✔
262
    if (param.GetName() == param_name)
136,979✔
263
      return param;
23,870✔
264

265
  throw std::out_of_range(error_origin_scope_ + ":" + std::string(__PRETTY_FUNCTION__) +
×
266
                          ": Parameter \"" + param_name + "\" not present in block");
×
267
}
268

269
ParameterBlock&
270
ParameterBlock::GetParam(size_t index)
3,591✔
271
{
272
  try
3,591✔
273
  {
274
    return parameters_.at(index);
3,591✔
275
  }
276
  catch (const std::out_of_range& oor)
×
277
  {
278
    throw std::out_of_range(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) +
×
279
                            ": Parameter with index " + std::to_string(index) +
×
280
                            " not present in block");
×
281
  }
×
282
}
283

284
const ParameterBlock&
285
ParameterBlock::GetParam(const std::string& param_name) const
20,905✔
286
{
287
  for (const auto& param : parameters_)
129,904✔
288
    if (param.GetName() == param_name)
129,904✔
289
      return param;
20,905✔
290

291
  throw std::out_of_range(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) +
×
292
                          ": Parameter \"" + param_name + "\" not present in block");
×
293
}
294

295
const ParameterBlock&
296
ParameterBlock::GetParam(size_t index) const
53,381✔
297
{
298
  try
53,381✔
299
  {
300
    return parameters_.at(index);
53,381✔
301
  }
302
  catch (const std::out_of_range& oor)
×
303
  {
304
    throw std::out_of_range(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) +
×
305
                            ": Parameter with index " + std::to_string(index) +
×
306
                            " not present in block");
×
307
  }
×
308
}
309

310
//  NOLINTBEGIN(misc-no-recursion)
311
void
312
ParameterBlock::RecursiveDumpToString(std::string& outstr, const std::string& offset) const
×
313
{
314
  outstr += offset + this->GetName() + " = \n";
×
315
  outstr += offset + "{\n";
×
316

317
  if (HasValue())
×
318
    outstr += value_ptr_->PrintStr();
×
319

320
  for (const auto& param : parameters_)
×
321
  {
322

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

364
  outstr += offset + "}\n";
×
365
}
×
366
// NOLINTEND(misc-no-recursion)
367

368
//  NOLINTBEGIN(misc-no-recursion)
369
void
370
ParameterBlock::RecursiveDumpToJSON(std::string& outstr) const
×
371
{
372
  if (HasValue())
×
373
  {
374
    outstr += value_ptr_->PrintStr(false);
×
375
    return;
×
376
  }
377

378
  outstr += (this->GetType() == ParameterBlockType::ARRAY ? "[" : "{");
×
379
  for (const auto& param : parameters_)
×
380
  {
381

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

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