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

Open-Sn / opensn / 18300593117

06 Oct 2025 10:47PM UTC coverage: 74.862% (-0.2%) from 75.031%
18300593117

push

github

web-flow
Merge pull request #759 from wdhawkins/performance

Sweep performance optimizations

294 of 302 new or added lines in 15 files covered. (97.35%)

334 existing lines in 80 files now uncovered.

17788 of 23761 relevant lines covered (74.86%)

61852783.95 hits per line

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

46.61
/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";
2✔
20
    case ParameterBlockType::FLOAT:
1✔
21
      return "FLOAT";
2✔
22
    case ParameterBlockType::STRING:
1✔
23
      return "STRING";
2✔
24
    case ParameterBlockType::INTEGER:
1✔
25
      return "INTEGER";
2✔
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)
379✔
39
{
40
  name_ = name;
379✔
41
}
379✔
42

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

48
ParameterBlock::ParameterBlock(const ParameterBlock& other)
179,444✔
49
  : type_(other.type_),
179,444✔
50
    name_(other.name_),
179,444✔
51
    parameters_(other.parameters_),
179,444✔
52
    error_origin_scope_(other.error_origin_scope_)
358,888✔
53
{
54
  if (other.value_ptr_)
179,444✔
55
    value_ptr_ = std::make_unique<Varying>(*other.value_ptr_);
165,929✔
56
}
179,444✔
57

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

71
  return *this;
20,609✔
72
}
73

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

86
  return *this;
21,897,067✔
87
}
88

89
// Accessors
90
ParameterBlockType
91
ParameterBlock::GetType() const
365,690✔
92
{
93
  return type_;
365,690✔
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
204,317,185✔
108
{
109
  return name_;
204,317,185✔
110
}
111

112
const Varying&
113
ParameterBlock::GetValue() const
88,736✔
114
{
115
  switch (this->GetType())
88,736✔
116
  {
117
    case ParameterBlockType::BOOLEAN:
88,736✔
118
    case ParameterBlockType::FLOAT:
119
    case ParameterBlockType::STRING:
120
    case ParameterBlockType::INTEGER:
121
    case ParameterBlockType::USER_DATA:
122
    {
123
      if (value_ptr_ == nullptr)
88,736✔
124
        throw std::runtime_error(error_origin_scope_ + std::string(__PRETTY_FUNCTION__) +
×
125
                                 ": Uninitialized Varying value for block " + this->GetName());
×
126
      return *value_ptr_;
88,736✔
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
70,244✔
139
{
140
  return parameters_.size();
70,244✔
141
}
142

143
const std::vector<ParameterBlock>&
144
ParameterBlock::GetParameters() const
18,341✔
145
{
146
  return parameters_;
18,341✔
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,977✔
159
{
160
  const std::string fname = __PRETTY_FUNCTION__;
5,977✔
161
  if (parameters_.empty())
5,977✔
162
  {
163
    type_ = ParameterBlockType::ARRAY;
5,963✔
164
    return;
5,963✔
165
  }
166

167
  const auto& first_param = parameters_.front();
14✔
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,977✔
177

178
// NOLINTBEGIN(misc-no-recursion)
179
void
180
ParameterBlock::SetErrorOriginScope(const std::string& scope)
17,201✔
181
{
182
  error_origin_scope_ = scope;
17,201✔
183
  for (auto& param : parameters_)
31,896✔
184
    param.SetErrorOriginScope(scope);
14,695✔
185
}
17,201✔
186
// NOLINTEND(misc-no-recursion)
187

188
void
189
ParameterBlock::RequireBlockTypeIs(ParameterBlockType type) const
1,817✔
190
{
191
  if (GetType() != type)
1,817✔
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,817✔
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)
110,758✔
207
{
208
  for (const auto& param : parameters_)
10,538,562✔
209
    if (param.GetName() == block.GetName())
10,427,804✔
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));
110,758✔
216

217
  SortParameters();
110,758✔
218
}
110,758✔
219

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

231
  struct AlphabeticNumericFunctor
232
  {
233
    bool operator()(const ParameterBlock& paramA, const ParameterBlock& paramB)
90,905,468✔
234
    {
235
      return std::stoi(paramA.GetName()) < std::stoi(paramB.GetName());
90,905,468✔
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)
110,758✔
244
    std::sort(parameters_.begin(), parameters_.end(), AlphabeticFunctor());
46,463✔
245
  else
246
    std::sort(parameters_.begin(), parameters_.end(), AlphabeticNumericFunctor());
64,295✔
247
}
110,758✔
248

249
bool
250
ParameterBlock::Has(const std::string& param_name) const
61,388✔
251
{
252
  return std::any_of(parameters_.begin(),
61,388✔
253
                     parameters_.end(),
254
                     [&param_name](const ParameterBlock& param)
365,073✔
255
                     { return param.name_ == param_name; });
426,461✔
256
}
257

258
ParameterBlock&
259
ParameterBlock::GetParam(const std::string& param_name)
28,146✔
260
{
261
  for (auto& param : parameters_)
157,571✔
262
    if (param.GetName() == param_name)
157,571✔
263
      return param;
28,146✔
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)
4,179✔
271
{
272
  try
273
  {
274
    return parameters_.at(index);
4,179✔
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
23,461✔
286
{
287
  for (const auto& param : parameters_)
143,672✔
288
    if (param.GetName() == param_name)
143,672✔
289
      return param;
23,461✔
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
63,305✔
297
{
298
  try
299
  {
300
    return parameters_.at(index);
63,305✔
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
      }
UNCOV
359
      default:
×
UNCOV
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);
×
UNCOV
416
        break;
×
417
      }
UNCOV
418
      default:
×
UNCOV
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