• 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

89.77
/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)
380✔
33
{
34
  // clang-format off
35
  // field function
36
  auto field_func = py::class_<FieldFunction, std::shared_ptr<FieldFunction>>(
380✔
37
    ffunc,
38
    "FieldFunction",
39
    R"(
40
    Field function.
41

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

48
// Wrap field functions grid based
49
void
50
WrapFieldFunctionGridBased(py::module& ffunc)
380✔
51
{
52
  // clang-format off
53
  // field function grid based
54
  auto field_func_grid_based = py::class_<FieldFunctionGridBased,
380✔
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
  );
380✔
65
  field_func_grid_based.def_static(
380✔
66
    "ExportMultipleToVTK",
67
    [](py::list& ff_list, const std::string& base_name)
401✔
68
    {
69
      std::vector<std::shared_ptr<const FieldFunctionGridBased>> cpp_ff_list;
21✔
70
      cpp_ff_list.reserve(ff_list.size());
21✔
71
      for (py::handle item : ff_list)
4,311✔
72
      {
73
        cpp_ff_list.push_back(item.cast<std::shared_ptr<const FieldFunctionGridBased>>());
8,538✔
74
      }
75
      FieldFunctionGridBased::ExportMultipleToVTK(base_name, cpp_ff_list);
21✔
76
    },
21✔
77
    R"(
78
      Export a list of "field function grid based" to VTK 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")
380✔
88
  );
89
  // clang-format on
90
}
380✔
91

92
// Wrap field function interpolation
93
void
94
WrapFieldFunctionInterpolation(py::module& ffunc)
380✔
95
{
96
  // clang-format off
97
  // field function interpolation
98
  auto field_func_interp = py::class_<FieldFunctionInterpolation,
380✔
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
  );
380✔
108
  field_func_interp.def(
380✔
109
    "AddFieldFunction",
110
    &FieldFunctionInterpolation::AddFieldFunction,
760✔
111
    R"(
112
    Add a field function to the list.
113
    )",
114
    py::arg("ff")
380✔
115
  );
116
  field_func_interp.def(
380✔
117
    "Initialize",
118
    &FieldFunctionInterpolation::Initialize,
380✔
119
    R"(
120
    ???
121
    )"
122
  );
123
  field_func_interp.def(
380✔
124
    "Execute",
125
    &FieldFunctionInterpolation::Execute,
380✔
126
    R"(
127
    ???
128
    )"
129
  );
130
  field_func_interp.def(
380✔
131
    "ExportToCSV",
132
    &FieldFunctionInterpolation::ExportToCSV,
760✔
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")
380✔
142
  );
143
  field_func_interp.def_static(
380✔
144
    "GetFieldFunctionByName",
145
    [](const std::string& ff_name)
380✔
146
    {
147
      // get list of suitable field functions
148
      py::list matched_ff;
×
UNCOV
149
      for (std::shared_ptr<FieldFunction>& ff_ptr : field_function_stack)
×
150
      {
UNCOV
151
        if (ff_ptr->GetName() == ff_name)
×
152
        {
UNCOV
153
          matched_ff.append(ff_ptr);
×
154
        }
155
      }
156
      return matched_ff;
×
UNCOV
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")
380✔
170
  );
171

172
  // field function interpolation point
173
  auto field_func_interp_point = py::class_<FieldFunctionInterpolationPoint,
380✔
174
                                            std::shared_ptr<FieldFunctionInterpolationPoint>,
175
                                            FieldFunctionInterpolation>(
176
    ffunc,
177
    "FieldFunctionInterpolationPoint",
178
    R"(
179
    Line based interpolation function.
180
    ??? (same docuumentation as FieldFunctionInterpolationLine)
181

182
    Wrapper of :cpp:class:`opensn::FieldFunctionInterpolationPoint`.
183
    )"
184
  );
380✔
185
  field_func_interp_point.def(
760✔
186
    py::init(
380✔
UNCOV
187
      [](void)
×
188
      {
UNCOV
189
        return FieldFunctionInterpolationPoint::Create();
×
190
      }
191
    ),
192
    "Default constructor."
193
  );
194
  field_func_interp_point.def(
380✔
195
    "GetPointValue",
196
    &FieldFunctionInterpolationPoint::GetPointValue,
380✔
197
    R"(
198
    ???
199
    )"
200
  );
201

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

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

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

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

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

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

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

299
    Parameters
300
    ----------
301
    op_type: {'sum', 'avg', 'max', 'sum_func', 'avg_func', 'max_func'}
302
        Operation type.
303
    )",
304
    py::arg("op_type")
380✔
305
  );
306
  field_func_interp_volume.def(
380✔
307
    "SetOperationFunction",
308
    [](FieldFunctionInterpolationVolume& self, const ScalarMaterialFunction& function)
380✔
309
    {
UNCOV
310
      self.SetOperationFunction(function);
×
311
    },
312
    R"(
313
    ???
314

315
    Parameters
316
    ----------
317
    function: Callable[[float, int], float]
318
        ???
319
    )",
320
    py::arg("function")
380✔
321
  );
322
  field_func_interp_volume.def(
380✔
323
    "GetValue",
324
    &FieldFunctionInterpolationVolume::GetValue,
380✔
325
    R"(
326
    ???
327
    )"
328
  );
329
  // clang-format on
330
}
380✔
331

332
// Wrap the field function components of OpenSn
333
void
334
py_ffunc(py::module& pyopensn)
34✔
335
{
336
  py::module ffunc = pyopensn.def_submodule("fieldfunc", "Field function module.");
34✔
337
  WrapFieldFunction(ffunc);
34✔
338
  WrapFieldFunctionGridBased(ffunc);
34✔
339
  WrapFieldFunctionInterpolation(ffunc);
34✔
340
}
34✔
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