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

Open-Sn / opensn / 21892714514

10 Feb 2026 11:58PM UTC coverage: 74.806% (-0.02%) from 74.828%
21892714514

push

github

web-flow
Merge pull request #929 from wdhawkins/iteration_log

Fixing iteration status messages

4 of 4 new or added lines in 1 file covered. (100.0%)

211 existing lines in 16 files now uncovered.

19638 of 26252 relevant lines covered (74.81%)

69654996.54 hits per line

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

84.21
/python/lib/source.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 "modules/linear_boltzmann_solvers/lbs_problem/volumetric_source/volumetric_source.h"
6
#include "modules/linear_boltzmann_solvers/lbs_problem/point_source/point_source.h"
7
#include "framework/math/functions/function.h"
8
#include <memory>
9

10
namespace opensn
11
{
12

13
namespace
14
{
15
std::shared_ptr<GroupTimeFunction>
16
MakeGroupTimeFunction(const py::function& func)
4✔
17
{
18
  auto wrapper = [func](unsigned int group, double time)
16✔
19
  {
20
    py::gil_scoped_acquire gil;
4,704,000✔
21
    try
4,704,000✔
22
    {
23
      return func(group, time).cast<double>();
4,704,000✔
24
    }
UNCOV
25
    catch (const py::error_already_set& err)
×
26
    {
UNCOV
27
      if (err.matches(PyExc_TypeError))
×
28
      {
UNCOV
29
        PyErr_Clear();
×
UNCOV
30
        return func(group).cast<double>();
×
31
      }
UNCOV
32
      throw;
×
UNCOV
33
    }
×
34
  };
4,704,000✔
35
  return std::make_shared<GroupTimeFunction>(wrapper);
4✔
36
}
4✔
37
} // namespace
38

39
// Wrap point source
40
void
41
WrapPointSource(py::module& src)
671✔
42
{
43
  // clang-format off
44
  // point source
45
  auto point_source = py::class_<PointSource, std::shared_ptr<PointSource>>(
671✔
46
    src,
47
    "PointSource",
48
    R"(
49
    Point sources, defined by its location and a group-wise strength vector.
50

51
    Wrapper of :cpp:class:`opensn::PointSource`.
52
    )"
53
  );
671✔
54
  point_source.def(
1,342✔
55
    py::init(
671✔
56
      [](py::kwargs& params)
9✔
57
      {
58
        ParameterBlock main;
9✔
59
        for (auto [key, value] : params)
54✔
60
        {
61
          if (key.cast<std::string>().empty())
18✔
UNCOV
62
            continue;
×
63

64
          const auto name = key.cast<std::string>();
18✔
65
          if (name == "strength_function" and py::isinstance<py::function>(value))
18✔
66
          {
UNCOV
67
            main.AddParameter(name, MakeGroupTimeFunction(value.cast<py::function>()));
×
UNCOV
68
            continue;
×
69
          }
70
          main.AddParameter(pyobj_to_param_block(name, value.cast<py::object>()));
18✔
71
        }
18✔
72
        return PointSource::Create(main);
18✔
73
      }
9✔
74
    ),
75
    R"(
76
    Construct a point source from its location and strength.
77

78
    Parameters
79
    ----------
80
    location: Tuple[float, float, float]
81
        Coordinates of the point source.
82
    strength: List[float]
83
        Group-wise point source strength.
84
    strength_function: Callable
85
        Callable that returns the source strength for a given group (and optionally time). It must
86
        accept either `(group, time)` for transient problems or just `(group)` for steady-state.
87
        If strength_function is provided, do not specify start_time/end_time; implement any time
88
        dependence inside the callback instead.
89
    )"
90
  );
91
  // clang-format on
92
}
671✔
93

94
// Wrap volumetric source
95
void
96
WrapVolumetricSource(py::module& src)
671✔
97
{
98
  // clang-format off
99
  // volumetric source
100
  auto volumetric_source = py::class_<VolumetricSource, std::shared_ptr<VolumetricSource>>(
671✔
101
    src,
102
    "VolumetricSource",
103
    R"(
104
      Multi-group isotropic volumetric sources.
105

106
      Wrapper of :cpp:class:`opensn::VolumetricSource`.
107
    )"
108
  );
671✔
109
  volumetric_source.def(
1,342✔
110
    py::init(
671✔
111
      [](py::kwargs& params)
483✔
112
      {
113
        ParameterBlock main;
483✔
114
        for (auto [key, value] : params)
3,170✔
115
        {
116
          if (key.cast<std::string>().empty())
1,102✔
117
            continue;
4✔
118

119
          const auto name = key.cast<std::string>();
1,102✔
120
          if (name == "strength_function" and py::isinstance<py::function>(value))
1,102✔
121
          {
122
            main.AddParameter(name, MakeGroupTimeFunction(value.cast<py::function>()));
8✔
123
            continue;
4✔
124
          }
125
          main.AddParameter(pyobj_to_param_block(name, value.cast<py::object>()));
1,098✔
126
        }
1,102✔
127
        return VolumetricSource::Create(main);
966✔
128
      }
483✔
129
    ),
130
    R"(
131
    Construct a multi-group isotropic volumetric sources.
132

133
    Parameters
134
    ----------
135
    block_ids: List[int]
136
        An array of block IDs the volumetric source is present within.
137
    logical_volume: pyopensn.logvol.LogicalVolume
138
        Logical volume that the volumetric source is defined within.
139
    group_strength: List[float]
140
        An array of multi-group source strength values. Note that this is only used when a function
141
        is not provided.
142
    func: pyopensn.math.VectorSpatialFunction
143
        Function to be used to define the source.
144
    strength_function: Callable
145
        Callable that returns the source strength for a given group (and optionally time). It must
146
        accept either `(group, time)` for transient problems or just `(group)` for steady-state.
147
        If strength_function is provided, do not specify start_time/end_time; implement any time
148
        dependence inside the callback instead.
149
    )"
150
  );
151
  // clang-format on
152
}
671✔
153

154
// Wrap the source components of OpenSn
155
void
156
py_source(py::module& pyopensn)
63✔
157
{
158
  py::module src = pyopensn.def_submodule("source", "Source module.");
63✔
159
  WrapPointSource(src);
63✔
160
  WrapVolumetricSource(src);
63✔
161
}
63✔
162

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