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

Open-Sn / opensn / 16766938693

05 Aug 2025 03:26PM UTC coverage: 73.386% (+0.1%) from 73.282%
16766938693

push

github

web-flow
Merge pull request #693 from andrsd/move-bnds

Moving sweep boundaries into `DiscreteOrdinatesProblem`

247 of 311 new or added lines in 19 files covered. (79.42%)

625 existing lines in 49 files now uncovered.

18320 of 24964 relevant lines covered (73.39%)

43106214.05 hits per line

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

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

4
#include "framework/post_processors/post_processor.h"
5
#include "framework/event_system/physics_event_publisher.h"
6
#include "framework/event_system/event_subscriber.h"
7
#include "framework/event_system/event.h"
8
#include "framework/logging/log.h"
9
#include "framework/runtime.h"
10
#include <inttypes.h>
11

12
namespace opensn
13
{
14

15
InputParameters
16
PostProcessor::GetInputParameters()
32✔
17
{
18
  InputParameters params;
32✔
19

20
  params.SetGeneralDescription("Base class for Post-Processors. For more general"
64✔
21
                               "information see \\ref doc_PostProcessors");
22
  params.SetDocGroup("doc_PostProcessors");
64✔
23

24
  params.AddRequiredParameter<std::string>(
64✔
25
    "name",
26
    "Name of the post processor. This name will be used in many places so make "
27
    "sure it's a useful name.");
28
  params.AddOptionalParameterArray(
64✔
29
    "execute_on",
30
    std::vector<std::string>{
32✔
31
      "SolverInitialized", "SolverAdvanced", "SolverExecuted", "ProgramExecuted"},
320✔
32
    "List of events at which the post-processor will execute.");
33

34
  params.AddOptionalParameterArray(
64✔
35
    "print_on",
36
    std::vector<std::string>{
32✔
37
      "SolverInitialized", "SolverAdvanced", "SolverExecuted", "ProgramExecuted"},
320✔
38
    "List of events at which the post-processor will print. Make sure that "
39
    "these "
40
    "events are also set for the `PostProcessorPrinter` otherwise it wont "
41
    "print.");
42

43
  params.AddOptionalParameterBlock("initial_value", ParameterBlock{}, "An initial value.");
64✔
44

45
  params.AddOptionalParameter("print_numeric_format", "general", "Numeric format to use.");
64✔
46

47
  params.ConstrainParameterRange(
96✔
48
    "print_numeric_format",
49
    AllowableRangeList::New({"fixed", "floating_point", "scientific", "general"}));
32✔
50

51
  params.AddOptionalParameter(
64✔
52
    "print_precision", 6, "Number of digits to display after decimal point");
53

54
  params.AddOptionalParameter("solvername_filter",
64✔
55
                              "",
56
                              "Controls update events to only execute on the relevant solver's"
57
                              "event calls.");
58

59
  return params;
32✔
60
}
×
61

62
PostProcessor::PostProcessor(const InputParameters& params, PPType type)
32✔
63
  : name_(params.GetParamValue<std::string>("name")),
32✔
64
    subscribed_events_for_execution_(params.GetParamVectorValue<std::string>("execute_on")),
32✔
65
    subscribed_events_for_printing_(params.GetParamVectorValue<std::string>("print_on")),
32✔
66
    type_(type),
32✔
67
    print_numeric_format_(
32✔
68
      ConstructNumericFormat(params.GetParamValue<std::string>("print_numeric_format"))),
64✔
69
    print_precision_(params.GetParamValue<size_t>("print_precision")),
32✔
70
    solvername_filter_(params.GetParamValue<std::string>("solvername_filter"))
128✔
71
{
72
  if (params.IsParameterValid("initial_value"))
32✔
73
  {
UNCOV
74
    value_ = params.GetParam("initial_value");
×
75
    SetType(FigureTypeFromValue(value_));
×
76
  }
77
}
32✔
78

79
PPNumericFormat
80
PostProcessor::ConstructNumericFormat(const std::string& format_string)
32✔
81
{
82
  if (format_string == "fixed")
32✔
83
    return PPNumericFormat::FIXED;
84
  else if (format_string == "floating_point")
32✔
85
    return PPNumericFormat::FLOATING_POINT;
86
  else if (format_string == "scientific")
32✔
87
    return PPNumericFormat::SCIENTIFIC;
UNCOV
88
  else if (format_string == "general")
×
89
    return PPNumericFormat::GENERAL;
90
  else
UNCOV
91
    OpenSnLogicalError("Invalid numeric format string \"" + format_string + "\"");
×
92
}
93

94
const std::string&
95
PostProcessor::GetName() const
64✔
96
{
97
  return name_;
64✔
98
}
99
PPType
100
PostProcessor::GetType() const
64✔
101
{
102
  return type_;
64✔
103
}
104

105
PPNumericFormat
106
PostProcessor::GetNumericFormat() const
64✔
107
{
108
  return print_numeric_format_;
64✔
109
}
110

111
size_t
112
PostProcessor::GetNumericPrecision() const
64✔
113
{
114
  return print_precision_;
64✔
115
}
116

117
void
UNCOV
118
PostProcessor::ReceiveEventUpdate(const Event& event)
×
119
{
UNCOV
120
  auto it = std::find(subscribed_events_for_execution_.begin(),
×
121
                      subscribed_events_for_execution_.end(),
UNCOV
122
                      event.GetName());
×
123

UNCOV
124
  if (it != subscribed_events_for_execution_.end())
×
125
  {
UNCOV
126
    if (event.IsSolverEvent() and not solvername_filter_.empty())
×
127
    {
UNCOV
128
      if (event.Parameters().GetParamValue<std::string>("solver_name") != solvername_filter_)
×
UNCOV
129
        return;
×
130
    }
131

UNCOV
132
    Execute(event);
×
UNCOV
133
    if (log.GetVerbosity() >= 1)
×
UNCOV
134
      log.Log0Verbose1() << "Post processor \"" << GetName()
×
135
                         << "\" executed on "
UNCOV
136
                            "event \""
×
137
                         << event.GetName() << "\".";
×
138
  }
139
}
140

141
const ParameterBlock&
142
PostProcessor::GetValue() const
64✔
143
{
144
  return value_;
64✔
145
}
146

147
const std::vector<PostProcessor::TimeHistoryEntry>&
148
PostProcessor::GetTimeHistory() const
80✔
149
{
150
  return time_history_;
80✔
151
}
152

153
const std::vector<std::string>&
154
PostProcessor::PrintScope() const
64✔
155
{
156
  return subscribed_events_for_printing_;
64✔
157
}
158

159
std::string
160
PostProcessor::ConvertScalarValueToString(const ParameterBlock& value) const
64✔
161
{
162
  std::string value_string;
64✔
163
  if (value.GetType() == ParameterBlockType::BOOLEAN)
64✔
164
  {
UNCOV
165
    value_string = value.GetValue<bool>() ? "true" : "false";
×
166
  }
167
  else if (value.GetType() == ParameterBlockType::FLOAT)
64✔
168
  {
169
    const auto dblval = value.GetValue<double>();
64✔
170
    char buffer[30];
64✔
171
    const auto numeric_format = GetNumericFormat();
64✔
172
    const size_t precision = GetNumericPrecision();
64✔
173
    if (numeric_format == PPNumericFormat::SCIENTIFIC)
64✔
174
    {
175
      const std::string format_spec = "%." + std::to_string(precision) + "e";
128✔
176
      snprintf(buffer, 30, format_spec.c_str(), dblval);
64✔
177
    }
64✔
UNCOV
178
    else if (numeric_format == PPNumericFormat::FLOATING_POINT)
×
179
    {
UNCOV
180
      const std::string format_spec = "%." + std::to_string(precision) + "f";
×
UNCOV
181
      snprintf(buffer, 30, format_spec.c_str(), dblval);
×
182
    }
183
    else // GENERAL
184
    {
UNCOV
185
      if (dblval < 1.0e-4)
×
186
      {
UNCOV
187
        const std::string format_spec = "%." + std::to_string(precision) + "e";
×
UNCOV
188
        snprintf(buffer, 30, format_spec.c_str(), dblval);
×
UNCOV
189
      }
×
UNCOV
190
      else if (dblval >= 1.0e-4 and dblval < 1.0e6)
×
191
      {
UNCOV
192
        const std::string format_spec = "%." + std::to_string(precision) + "f";
×
UNCOV
193
        snprintf(buffer, 30, format_spec.c_str(), dblval);
×
UNCOV
194
      }
×
195
      else
196
      {
197
        const std::string format_spec = "%." + std::to_string(precision) + "e";
×
UNCOV
198
        snprintf(buffer, 30, format_spec.c_str(), dblval);
×
199
      }
×
200
    } // if num_format
201

202
    value_string = buffer;
64✔
203
  }
204
  else if (value.GetType() == ParameterBlockType::STRING)
×
205
  {
206
    value_string = value.GetValue<std::string>();
×
207
  }
208
  else if (value.GetType() == ParameterBlockType::INTEGER)
×
209
  {
UNCOV
210
    const auto intval = value.GetValue<int64_t>();
×
211
    char buffer[30];
×
212
    snprintf(buffer, 30, "%" PRId64, intval);
×
213
    value_string = buffer;
×
214
  }
215

216
  return value_string;
64✔
217
}
×
218

219
std::string
220
PostProcessor::ConvertValueToString(const ParameterBlock& value) const
64✔
221
{
222
  const PPType type = FigureTypeFromValue(value);
64✔
223
  if (type == PPType::SCALAR)
64✔
224
    return ConvertScalarValueToString(value);
64✔
225
  else if (type == PPType::VECTOR)
×
226
  {
227
    if (value.GetNumParameters() == 0)
×
UNCOV
228
      return "";
×
229
    const auto& first_entry = value.GetParam(0);
×
230
    const auto first_entry_type = first_entry.GetType();
×
231

232
    OpenSnLogicalErrorIf(FigureTypeFromValue(first_entry) != PPType::SCALAR,
×
233
                         "The entries of the vector value of post-processor \"" + GetName() +
234
                           "\" must all be SCALAR.");
235

236
    std::string output;
×
UNCOV
237
    for (const auto& entry : value)
×
238
    {
UNCOV
239
      OpenSnLogicalErrorIf(entry.GetType() != first_entry_type,
×
240
                           "Mixed typed encountered in the vector values of post-processor \"" +
241
                             GetName() + "\"");
UNCOV
242
      output.append(ConvertScalarValueToString(entry) + " ");
×
243
    }
244

UNCOV
245
    return output;
×
246
  }
×
247
  else
248
  {
249
    std::string outstr;
×
UNCOV
250
    value.RecursiveDumpToString(outstr);
×
251
    std::replace(outstr.begin(), outstr.end(), '\n', ' ');
×
UNCOV
252
    return outstr;
×
UNCOV
253
  }
×
254
}
255

256
PPType
257
PostProcessor::FigureTypeFromValue(const ParameterBlock& value)
64✔
258
{
259
  const std::vector<ParameterBlockType> scalar_types = {ParameterBlockType::BOOLEAN,
64✔
260
                                                        ParameterBlockType::FLOAT,
261
                                                        ParameterBlockType::STRING,
262
                                                        ParameterBlockType::INTEGER};
64✔
263

264
  /**Lambda to check if this is a scalar*/
265
  auto IsScalar = [&scalar_types](const ParameterBlockType& block_type)
128✔
266
  { return std::find(scalar_types.begin(), scalar_types.end(), block_type) != scalar_types.end(); };
128✔
267

268
  if (not value.HasValue() and value.GetNumParameters() == 0)
64✔
269
    return PPType::NO_VALUE;
270
  else if (IsScalar(value.GetType()))
64✔
271
    return PPType::SCALAR;
272
  else if (value.GetType() == ParameterBlockType::ARRAY)
×
273
  {
UNCOV
274
    if (value.GetNumParameters() == 0)
×
275
      return PPType::NO_VALUE;
276
    else
277
    {
UNCOV
278
      if (IsScalar(value.GetParam(0).GetType()))
×
279
        return PPType::VECTOR;
280
      else
UNCOV
281
        return PPType::ARBITRARY;
×
282
    }
283
  }
UNCOV
284
  else if (value.GetType() == ParameterBlockType::BLOCK)
×
285
    return PPType::ARBITRARY;
286
  else
UNCOV
287
    OpenSnLogicalError("Unsupported type");
×
288
}
64✔
289

290
void
291
PostProcessor::SetType(PPType type)
×
292
{
293
  type_ = type;
×
UNCOV
294
}
×
295

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