• 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

80.95
/python/lib/fieldfunc.cc
1
// SPDX-FileCopyrightText: 2025 The OpenSn Authors <https://open-sn.github.io/opensn/>
2
// SPDX-License-Identifier: MIT
3

4
#include "python/lib/py_wrappers.h"
5
#include "framework/runtime.h"
6
#include "framework/field_functions/field_function.h"
7
#include "framework/field_functions/field_function_grid_based.h"
8
#include "framework/field_functions/interpolation/ffinterpolation.h"
9
#include "framework/field_functions/interpolation/ffinter_point.h"
10
#include "framework/field_functions/interpolation/ffinter_line.h"
11
#include "framework/field_functions/interpolation/ffinter_volume.h"
12
#include <pybind11/functional.h>
13
#include <map>
14
#include <memory>
15
#include <string>
16
#include <vector>
17

18
namespace opensn
19
{
20

21
// dictionary to field function interpolation operation type
22
static std::map<std::string, FieldFunctionInterpolationOperation> ff_op_type_map{
23
  {"sum", FieldFunctionInterpolationOperation::OP_SUM},
24
  {"avg", FieldFunctionInterpolationOperation::OP_AVG},
25
  {"max", FieldFunctionInterpolationOperation::OP_MAX},
26
  {"sum_func", FieldFunctionInterpolationOperation::OP_SUM_FUNC},
27
  {"avg_func", FieldFunctionInterpolationOperation::OP_AVG_FUNC},
28
  {"max_func", FieldFunctionInterpolationOperation::OP_MAX_FUNC}};
29

30
// Wrap field functions
31
void
32
WrapFieldFunction(py::module& ffunc)
416✔
33
{
34
  // clang-format off
35
  // field function
36
  auto field_func = py::class_<FieldFunction, std::shared_ptr<FieldFunction>>(
37
    ffunc,
38
    "FieldFunction",
39
    R"(
40
    Field function.
41

42
    Wrapper of :cpp:class:`opensn::FieldFunction`.
43
    )"
44
  );
416✔
45
  // clang-format on
46
}
416✔
47

48
// Wrap field functions grid based
49
void
50
WrapFieldFunctionGridBased(py::module& ffunc)
416✔
51
{
52
  // clang-format off
53
  // field function grid based
54
  auto field_func_grid_based = py::class_<FieldFunctionGridBased,
55
                                          std::shared_ptr<FieldFunctionGridBased>,
56
                                          FieldFunction>(
57
    ffunc,
58
    "FieldFunctionGridBased",
59
    R"(
60
    Field function grid based.
61

62
    Wrapper of :cpp:class:`opensn::FieldFunctionGridBased`.
63
    )"
64
  );
416✔
65
  field_func_grid_based.def_static(
416✔
66
    "ExportMultipleToPVTU",
UNCOV
67
    [](py::list& ff_list, const std::string& base_name)
×
68
    {
69
      std::vector<std::shared_ptr<const FieldFunctionGridBased>> cpp_ff_list;
53✔
70
      cpp_ff_list.reserve(ff_list.size());
53✔
71
      for (py::handle item : ff_list)
5,986✔
72
      {
73
        cpp_ff_list.push_back(item.cast<std::shared_ptr<const FieldFunctionGridBased>>());
5,933✔
74
      }
75
      FieldFunctionGridBased::ExportMultipleToPVTU(base_name, cpp_ff_list);
53✔
76
    },
53✔
77
    R"(
78
      Export a list of "field function grid based" to parallel VTU format.
79

80
      Parameters
81
      ----------
82
      ff_list: List[opensn.FieldFunctionGridBased]
83
          List of "field function grid based" to export.
84
      base_name: str
85
          Base name.
86
    )",
87
    py::arg("ff_list"), py::arg("base_name")
416✔
88
  );
89
  // clang-format on
90
}
416✔
91

92
// Wrap field function interpolation
93
void
94
WrapFieldFunctionInterpolation(py::module& ffunc)
416✔
95
{
96
  // clang-format off
97
  // field function interpolation
98
  auto field_func_interp = py::class_<FieldFunctionInterpolation,
99
                                      std::shared_ptr<FieldFunctionInterpolation>>(
100
    ffunc,
101
    "FieldFunctionInterpolation",
102
    R"(
103
    Base class for field-function interpolation objects.
104

105
    Wrapper of :cpp:class:`opensn::FieldFunctionInterpolation`.
106
    )"
107
  );
416✔
108
  field_func_interp.def(
416✔
109
    "AddFieldFunction",
110
    &FieldFunctionInterpolation::AddFieldFunction,
416✔
111
    R"(
112
    Add a field function to the list.
113
    )",
114
    py::arg("ff")
416✔
115
  );
116
  field_func_interp.def(
416✔
117
    "Initialize",
118
    &FieldFunctionInterpolation::Initialize,
416✔
119
    R"(
120
    Initialize field function interpolator.
121
    )"
122
  );
123
  field_func_interp.def(
416✔
124
    "Execute",
125
    &FieldFunctionInterpolation::Execute,
416✔
126
    R"(
127
    Execute field function interpolator.
128
    )"
129
  );
130
  field_func_interp.def(
416✔
131
    "ExportToCSV",
132
    &FieldFunctionInterpolation::ExportToCSV,
416✔
133
    R"(
134
    Export field function interpolation to CSV files.
135

136
    Parameters
137
    ----------
138
    base_name: str
139
        Base name of the exported CSVs.
140
    )",
141
    py::arg("base_name")
416✔
142
  );
143
  field_func_interp.def_static(
416✔
144
    "GetFieldFunctionByName",
UNCOV
145
    [](const std::string& ff_name)
×
146
    {
147
      // get list of suitable field functions
148
      py::list matched_ff;
×
149
      for (std::shared_ptr<FieldFunction>& ff_ptr : field_function_stack)
×
150
      {
151
        if (ff_ptr->GetName() == ff_name)
×
152
        {
153
          matched_ff.append(ff_ptr);
×
154
        }
155
      }
156
      return matched_ff;
×
157
    },
×
158
    R"(
159
    Get the list of field functions matching a given name.
160

161
    This function returns a list of field functions whose names match the given argument. The list
162
    may be empty or contain multiple elements.
163

164
    Parameters
165
    ----------
166
    ff_name: str
167
        Field function name
168
    )",
169
    py::arg("ff_name")
416✔
170
  );
171

172
  // field function interpolation point
173
  auto field_func_interp_point = py::class_<FieldFunctionInterpolationPoint,
174
                                            std::shared_ptr<FieldFunctionInterpolationPoint>,
175
                                            FieldFunctionInterpolation>(
176
    ffunc,
177
    "FieldFunctionInterpolationPoint",
178
    R"(
179
    Interpolate the field function at a point.
180

181
    Wrapper of :cpp:class:`opensn::FieldFunctionInterpolationPoint`.
182
    )"
183
  );
416✔
184
  field_func_interp_point.def(
416✔
185
    py::init(
416✔
186
      []()
×
187
      {
188
        return FieldFunctionInterpolationPoint::Create();
×
189
      }
190
    ),
191
    "Default constructor."
192
  );
193
  field_func_interp_point.def(
416✔
194
    "GetPointValue",
195
    &FieldFunctionInterpolationPoint::GetPointValue,
416✔
196
    R"(
197
    Get the value of the field function interpolation at the specified point.
198
    )"
199
  );
200

201
  // field function interpolation line
202
  auto field_func_interp_line = py::class_<FieldFunctionInterpolationLine,
203
                                           std::shared_ptr<FieldFunctionInterpolationLine>,
204
                                           FieldFunctionInterpolation>(
205
    ffunc,
206
    "FieldFunctionInterpolationLine",
207
    R"(
208
    Line based interpolation function.
209

210
    Wrapper of :cpp:class:`opensn::FieldFunctionInterpolationLine`.
211
    )"
212
  );
416✔
213
  field_func_interp_line.def(
416✔
214
    py::init(
416✔
UNCOV
215
      []()
×
216
      {
217
        return FieldFunctionInterpolationLine::Create();
570✔
218
      }
219
    ),
220
    "Default constructor."
221
  );
222
  field_func_interp_line.def(
416✔
223
    "SetInitialPoint",
224
    &FieldFunctionInterpolationLine::SetInitialPoint,
416✔
225
    R"(
226
    Set initial point.
227

228
    Parameters
229
    ----------
230
    point: pyopensn.math.Vector3
231
        Coordinates of the initial point.
232
    )",
233
    py::arg("point")
416✔
234
  );
235
  field_func_interp_line.def(
416✔
236
    "SetFinalPoint",
237
    &FieldFunctionInterpolationLine::SetFinalPoint,
416✔
238
    R"(
239
    Set final point.
240

241
    Parameters
242
    ----------
243
    point: pyopensn.math.Vector3
244
        Coordinates of the final point.
245
    )",
246
    py::arg("point")
416✔
247
  );
248
  field_func_interp_line.def(
416✔
249
    "SetNumberOfPoints",
250
    &FieldFunctionInterpolationLine::SetNumberOfPoints,
416✔
251
    R"(
252
    Set number of points.
253

254
    Parameters
255
    ----------
256
    number: int
257
        Number of points.
258
    )",
259
    py::arg("number")
416✔
260
  );
261

262
  // field function interpolation volume
263
  auto field_func_interp_volume = py::class_<FieldFunctionInterpolationVolume,
264
                                             std::shared_ptr<FieldFunctionInterpolationVolume>,
265
                                             FieldFunctionInterpolation>(
266
    ffunc,
267
    "FieldFunctionInterpolationVolume",
268
    R"(
269
    A line based interpolation function.
270

271
    Wrapper of :cpp:class:`opensn::FieldFunctionInterpolationVolume`.
272
    )"
273
  );
416✔
274
  field_func_interp_volume.def(
416✔
275
    py::init(
416✔
UNCOV
276
      []()
×
277
      {
278
        return FieldFunctionInterpolationVolume::Create();
5,946✔
279
      }
280
    ),
281
    "Default constructor."
282
  );
283
  field_func_interp_volume.def(
416✔
284
    "SetLogicalVolume",
285
    &FieldFunctionInterpolationVolume::SetLogicalVolume,
416✔
286
    "Set logical volume.",
287
    py::arg("lv")
416✔
288
  );
289
  field_func_interp_volume.def(
416✔
290
    "SetOperationType",
UNCOV
291
    [](FieldFunctionInterpolationVolume& self, const std::string& op_type)
×
292
    {
293
      self.SetOperationType(ff_op_type_map.at(op_type));
5,946✔
294
    },
5,946✔
295
    R"(
296
    Set operation type.
297

298
    Parameters
299
    ----------
300
    op_type: {'sum', 'avg', 'max', 'sum_func', 'avg_func', 'max_func'}
301
        Operation type.
302
    )",
303
    py::arg("op_type")
416✔
304
  );
305
  field_func_interp_volume.def(
416✔
306
    "SetOperationFunction",
UNCOV
307
    [](FieldFunctionInterpolationVolume& self, const ScalarMaterialFunction& function)
×
308
    {
309
      self.SetOperationFunction(function);
×
UNCOV
310
    },
×
311
    R"(
312
    Set the field function operation type to a custom scalar material function.
313

314
    Parameters
315
    ----------
316
    function: Callable[[float, int], float]
317
        A scalar material function that takes the field function value (float) and the
318
        block id (int) as parameters and returns a double.
319
    )",
320
    py::arg("function")
416✔
321
  );
322
  field_func_interp_volume.def(
416✔
323
    "GetValue",
324
    &FieldFunctionInterpolationVolume::GetValue,
416✔
325
    R"(
326
    Returns the value of the field function interpolation.
327
    )"
328
  );
329
  // clang-format on
330
}
416✔
331

332
// Wrap the field function components of OpenSn
333
void
334
py_ffunc(py::module& pyopensn)
62✔
335
{
336
  py::module ffunc = pyopensn.def_submodule("fieldfunc", "Field function module.");
62✔
337
  WrapFieldFunction(ffunc);
62✔
338
  WrapFieldFunctionGridBased(ffunc);
62✔
339
  WrapFieldFunctionInterpolation(ffunc);
62✔
340
}
62✔
341

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