• 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

74.32
/python/lib/xs.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/materials/multi_group_xs/multi_group_xs.h"
7
#include "framework/materials/multi_group_xs/xsfile.h"
8
#include <pybind11/stl.h>
9
#include <memory>
10
#include <string>
11
#include <vector>
12

13
#define XS_GETTER(method_name) [](MultiGroupXS& self) { return convert_vector(self.method_name()); }
14

15
namespace opensn
16
{
17

18
// Wrap multi-group cross section
19
void
20
WrapMultiGroupXS(py::module& xs)
416✔
21
{
22
  // clang-format off
23
  // multi-group cross section
24
  auto multigroup_xs = py::class_<MultiGroupXS, std::shared_ptr<MultiGroupXS>>(
25
    xs,
26
    "MultiGroupXS",
27
    R"(
28
    Multi-group cross section.
29

30
    Wrapper of :cpp:class:`opensn::MultiGroupXS`.
31
    )"
32
  );
416✔
33
  multigroup_xs.def(
416✔
34
    py::init(
416✔
UNCOV
35
      []()
×
36
      {
37
        std::shared_ptr<MultiGroupXS> xs = std::make_shared<MultiGroupXS>();
532✔
38
        multigroup_xs_stack.push_back(xs);
532✔
39
        return xs;
532✔
40
      }),
×
41
    "Create an empty multi-group cross section."
42
  );
43
  multigroup_xs.def(
416✔
44
    "CreateSimpleOneGroup",
UNCOV
45
    [](MultiGroupXS& self, double sigma_t, double c) {
×
46
      self = MultiGroupXS::CreateSimpleOneGroup(sigma_t, c);
92✔
47
    },
92✔
48
    R"(
49
    Create a one-group cross section.
50

51
    Parameters
52
    ----------
53
    sigma_t: float
54
        Total cross section.
55
    c: float
56
        Scattering ratio.
57
    )",
58
    py::arg("sigma_t"),
416✔
59
    py::arg("c")
416✔
60
  );
61
  multigroup_xs.def(
416✔
62
    "LoadFromOpenSn",
UNCOV
63
    [](MultiGroupXS& self, const std::string& file_name)
×
64
    {
65
      self = MultiGroupXS::LoadFromOpenSn(file_name);
375✔
66
    },
375✔
67
    py::arg("file_name"),
416✔
68
    R"(
69
    Load multi-group cross sections from an OpenSn cross section input file.
70

71
    Format is as follows (for transfers, gprime denotes the departing group and g is the arrival
72
    group).
73

74
    .. code-block:: none
75

76
       # Add comment lines, as needed
77
       NUM_GROUPS ng
78
       NUM_MOMENTS nmom
79

80
       SIGMA_T_BEGIN
81
       0 value
82
       .
83
       .
84
       ng-1 value
85
       SIGMA_T_END
86

87
       SIGMA_A_BEGIN
88
       0 value
89
       .
90
       .
91
       ng-1 value
92
       SIGMA_A_END
93

94
       TRANSFER_MOMENTS_BEGIN
95
       M_GPRIME_G_VAL 0 0 0 value
96
       .
97
       M_GPRIME_G_VAL moment gprime g value
98
       .
99
       M_GPRIME_G_VAL nmom-1 ng-1 ng-1 value
100
       TRANSFER_MOMENTS_END
101
    )"
102
  );
103
  multigroup_xs.def(
416✔
104
    "Combine",
UNCOV
105
    [](MultiGroupXS& self, const std::vector<std::pair<std::shared_ptr<MultiGroupXS>, double>>& combinations)
×
106
    {
107
      self = MultiGroupXS::Combine(combinations);
1✔
108
    },
1✔
109
    R"(
110
    Combine cross-section
111

112
    Parameters
113
    ----------
114

115
    combinations: List[Tuple[pyopensn.xs.MultiGroupXS, float]]
116
        List of combinations (cross section, factor)
117

118
    Examples
119
    --------
120

121
    >>> xs_1 = MultiGroupXS()
122
    >>> xs_1.CreateSimpleOneGroup(sigma_t=1, c=0.5)
123
    >>> xs_2 = MultiGroupXS()
124
    >>> xs_2.CreateSimpleOneGroup(sigma_t=2, c=1./3.)
125
    >>> xs_combined = MultiGroupXS()
126
    >>> combo = [
127
    ...     ( xs_1, 0.5 ),
128
    ...     ( xs_2, 3.0 )
129
    ... ]
130
    >>> xs_combined.Combine(combo)
131
    )",
132
    py::arg("combinations")
416✔
133
  );
134
  multigroup_xs.def(
416✔
135
    "LoadFromOpenMC",
UNCOV
136
    [](MultiGroupXS& self, const std::string& file_name, const std::string& dataset_name,
×
137
       double temperature)
138
    {
139
      self = MultiGroupXS::LoadFromOpenMC(file_name, dataset_name, temperature);
64✔
140
    },
64✔
141
    "Load multi-group cross sections from an OpenMC cross-section file.",
142
    py::arg("file_name"),
416✔
143
    py::arg("dataset_name"),
416✔
144
    py::arg("temperature")
416✔
145
  );
146
  multigroup_xs.def(
416✔
147
    "SetScalingFactor",
148
    &MultiGroupXS::SetScalingFactor,
416✔
149
    "Scale the cross sections by the specified factor.",
150
    py::arg("factor")
416✔
151
  );
152
  multigroup_xs.def_property_readonly(
416✔
153
    "num_groups",
154
    &MultiGroupXS::GetNumGroups,
416✔
155
    "Get number of energy groups."
156
  );
157
  multigroup_xs.def_property_readonly(
416✔
158
    "scattering_order",
159
    &MultiGroupXS::GetScatteringOrder,
416✔
160
    "Get Legendre scattering order."
161
  );
162
  multigroup_xs.def_property_readonly(
416✔
163
    "num_precursors",
164
    &MultiGroupXS::GetNumPrecursors,
416✔
165
    "Get number of precursors."
166
  );
167
  multigroup_xs.def_property_readonly(
416✔
168
    "is_fissionable",
169
    &MultiGroupXS::IsFissionable,
416✔
170
    "Check if the material is fissile."
171
  );
172
  multigroup_xs.def_property_readonly(
416✔
173
    "scaling_factor",
174
    &MultiGroupXS::GetScalingFactor,
416✔
175
    "Get the arbitrary scaling factor."
176
  );
177
  multigroup_xs.def_property_readonly(
416✔
178
    "sigma_t",
179
    XS_GETTER(GetSigmaTotal),
7✔
180
    "Get total cross section.",
UNCOV
181
    py::keep_alive<0, 1>()
×
182
  );
183
  multigroup_xs.def_property_readonly(
416✔
184
    "sigma_a",
185
    XS_GETTER(GetSigmaAbsorption),
2,548✔
186
    "Get absorption cross section.",
UNCOV
187
    py::keep_alive<0, 1>()
×
188
  );
189
  multigroup_xs.def_property_readonly(
416✔
190
    "sigma_f",
191
    XS_GETTER(GetSigmaFission),
×
192
    "Get fission cross section.",
UNCOV
193
    py::keep_alive<0, 1>()
×
194
  );
195
  multigroup_xs.def_property_readonly(
416✔
196
    "chi",
197
    XS_GETTER(GetChi),
2✔
198
    "Get neutron fission spectrum.",
UNCOV
199
    py::keep_alive<0, 1>()
×
200
  );
201
  multigroup_xs.def_property_readonly(
416✔
202
    "nu_sigma_f",
203
    XS_GETTER(GetNuSigmaF),
×
204
    "Get neutron production due to fission.",
UNCOV
205
    py::keep_alive<0, 1>()
×
206
  );
207
  multigroup_xs.def_property_readonly(
416✔
208
    "nu_prompt_sigma_f",
209
    XS_GETTER(GetNuPromptSigmaF),
×
210
    "Get prompt neutron production due to fission.",
UNCOV
211
    py::keep_alive<0, 1>()
×
212
  );
213
  multigroup_xs.def_property_readonly(
416✔
214
    "nu_delayed_sigma_f",
215
    XS_GETTER(GetNuDelayedSigmaF),
×
216
    "Get delayed neutron production due to fission.",
UNCOV
217
    py::keep_alive<0, 1>()
×
218
  );
219
  multigroup_xs.def_property_readonly(
416✔
220
    "inv_velocity",
221
    XS_GETTER(GetInverseVelocity),
×
222
    "Get inverse velocity.",
UNCOV
223
    py::keep_alive<0, 1>()
×
224
  );
225
  // clang-format on
226
}
416✔
227

228
// Wrap the cross section components of OpenSn
229
void
230
py_xs(py::module& pyopensn)
62✔
231
{
232
  py::module xs = pyopensn.def_submodule("xs", "Cross section module.");
62✔
233
  WrapMultiGroupXS(xs);
62✔
234
}
62✔
235

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