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

openmc-dev / openmc / 17707962129

14 Sep 2025 07:15AM UTC coverage: 85.074% (-0.1%) from 85.218%
17707962129

Pull #3547

github

web-flow
Merge 07e1f2aef into afd9d0607
Pull Request #3547: Tally spectrum of secondary particles

23 of 58 new or added lines in 8 files covered. (39.66%)

31 existing lines in 2 files now uncovered.

52790 of 62052 relevant lines covered (85.07%)

38251650.85 hits per line

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

85.62
/src/tallies/filter.cpp
1
#include "openmc/tallies/filter.h"
2

3
#include <algorithm> // for max
4
#include <cassert>
5
#include <cstring> // for strcpy
6
#include <string>
7

8
#include <fmt/core.h>
9

10
#include "openmc/capi.h"
11
#include "openmc/constants.h" // for MAX_LINE_LEN;
12
#include "openmc/error.h"
13
#include "openmc/tallies/filter_azimuthal.h"
14
#include "openmc/tallies/filter_cell.h"
15
#include "openmc/tallies/filter_cell_instance.h"
16
#include "openmc/tallies/filter_cellborn.h"
17
#include "openmc/tallies/filter_cellfrom.h"
18
#include "openmc/tallies/filter_collision.h"
19
#include "openmc/tallies/filter_delayedgroup.h"
20
#include "openmc/tallies/filter_distribcell.h"
21
#include "openmc/tallies/filter_energy.h"
22
#include "openmc/tallies/filter_energyfunc.h"
23
#include "openmc/tallies/filter_legendre.h"
24
#include "openmc/tallies/filter_material.h"
25
#include "openmc/tallies/filter_materialfrom.h"
26
#include "openmc/tallies/filter_mesh.h"
27
#include "openmc/tallies/filter_meshborn.h"
28
#include "openmc/tallies/filter_meshmaterial.h"
29
#include "openmc/tallies/filter_meshsurface.h"
30
#include "openmc/tallies/filter_mu.h"
31
#include "openmc/tallies/filter_musurface.h"
32
#include "openmc/tallies/filter_parent_nuclide.h"
33
#include "openmc/tallies/filter_particle.h"
34
#include "openmc/tallies/filter_polar.h"
35
#include "openmc/tallies/filter_sph_harm.h"
36
#include "openmc/tallies/filter_sptl_legendre.h"
37
#include "openmc/tallies/filter_surface.h"
38
#include "openmc/tallies/filter_time.h"
39
#include "openmc/tallies/filter_universe.h"
40
#include "openmc/tallies/filter_weight.h"
41
#include "openmc/tallies/filter_zernike.h"
42
#include "openmc/xml_interface.h"
43

44
// explicit template instantiation definition
45
template class openmc::vector<openmc::FilterMatch>;
46

47
namespace openmc {
48

49
//==============================================================================
50
// Global variables
51
//==============================================================================
52

53
namespace model {
54
std::unordered_map<int, int> filter_map;
55
vector<unique_ptr<Filter>> tally_filters;
56
} // namespace model
57

58
//==============================================================================
59
// Non-member functions
60
//==============================================================================
61

62
extern "C" size_t tally_filters_size()
1,793✔
63
{
64
  return model::tally_filters.size();
1,793✔
65
}
66

67
//==============================================================================
68
// Filter implementation
69
//==============================================================================
70

71
Filter::Filter()
10,049✔
72
{
73
  index_ = model::tally_filters.size(); // Avoids warning about narrowing
10,049✔
74
}
10,049✔
75

76
Filter::~Filter()
10,049✔
77
{
78
  model::filter_map.erase(id_);
10,049✔
79
}
10,049✔
80

×
81
Filter* Filter::create(pugi::xml_node node)
82
{
83
  // Copy filter id
84
  if (!check_for_node(node, "id")) {
10,049✔
85
    fatal_error("Must specify id for filter in tally XML file.");
86
  }
10,049✔
87
  int filter_id = std::stoi(get_node_value(node, "id"));
10,049✔
88

89
  // Convert filter type to lower case
7,982✔
90
  std::string s;
91
  if (check_for_node(node, "type")) {
92
    s = get_node_value(node, "type", true);
7,982✔
93
  }
×
94

95
  // Allocate according to the filter type
7,982✔
96
  auto f = Filter::create(s, filter_id);
97

98
  // Read filter data from XML
7,982✔
99
  f->from_xml(node);
7,982✔
100
  return f;
7,982✔
101
}
102

103
Filter* Filter::create(const std::string& type, int32_t id)
104
{
7,982✔
105
  if (type == "azimuthal") {
106
    return Filter::create<AzimuthalFilter>(id);
107
  } else if (type == "cell") {
7,982✔
108
    return Filter::create<CellFilter>(id);
7,982✔
109
  } else if (type == "cellborn") {
7,982✔
110
    return Filter::create<CellBornFilter>(id);
111
  } else if (type == "cellfrom") {
10,033✔
112
    return Filter::create<CellFromFilter>(id);
113
  } else if (type == "cellinstance") {
10,033✔
114
    return Filter::create<CellInstanceFilter>(id);
32✔
115
  } else if (type == "distribcell") {
10,001✔
116
    return Filter::create<DistribcellFilter>(id);
772✔
117
  } else if (type == "delayedgroup") {
9,229✔
118
    return Filter::create<DelayedGroupFilter>(id);
16✔
119
  } else if (type == "energyfunction") {
9,213✔
120
    return Filter::create<EnergyFunctionFilter>(id);
96✔
121
  } else if (type == "energy") {
9,117✔
122
    return Filter::create<EnergyFilter>(id);
34✔
123
  } else if (type == "collision") {
9,083✔
124
    return Filter::create<CollisionFilter>(id);
223✔
125
  } else if (type == "energyout") {
8,860✔
126
    return Filter::create<EnergyoutFilter>(id);
96✔
127
  } else if (type == "legendre") {
8,764✔
128
    return Filter::create<LegendreFilter>(id);
276✔
129
  } else if (type == "material") {
8,488✔
130
    return Filter::create<MaterialFilter>(id);
1,564✔
131
  } else if (type == "materialfrom") {
6,924✔
132
    return Filter::create<MaterialFromFilter>(id);
16✔
133
  } else if (type == "mesh") {
6,908✔
134
    return Filter::create<MeshFilter>(id);
412✔
135
  } else if (type == "meshborn") {
6,496✔
136
    return Filter::create<MeshBornFilter>(id);
573✔
137
  } else if (type == "meshmaterial") {
5,923✔
138
    return Filter::create<MeshMaterialFilter>(id);
2,348✔
139
  } else if (type == "meshsurface") {
3,575✔
140
    return Filter::create<MeshSurfaceFilter>(id);
16✔
141
  } else if (type == "mu") {
3,559✔
142
    return Filter::create<MuFilter>(id);
2,260✔
143
  } else if (type == "musurface") {
1,299✔
144
    return Filter::create<MuSurfaceFilter>(id);
27✔
145
  } else if (type == "parentnuclide") {
1,272✔
146
    return Filter::create<ParentNuclideFilter>(id);
11✔
147
  } else if (type == "particle") {
1,261✔
148
    return Filter::create<ParticleFilter>(id);
403✔
149
  } else if (type == "particleout") {
858✔
150
    return Filter::create<ParticleOutFilter>(id);
32✔
151
  } else if (type == "polar") {
826✔
152
    return Filter::create<PolarFilter>(id);
27✔
153
  } else if (type == "surface") {
799✔
154
    return Filter::create<SurfaceFilter>(id);
11✔
155
  } else if (type == "spatiallegendre") {
788✔
156
    return Filter::create<SpatialLegendreFilter>(id);
387✔
157
  } else if (type == "sphericalharmonics") {
401✔
UNCOV
158
    return Filter::create<SphericalHarmonicsFilter>(id);
×
159
  } else if (type == "time") {
401✔
160
    return Filter::create<TimeFilter>(id);
48✔
161
  } else if (type == "universe") {
353✔
162
    return Filter::create<UniverseFilter>(id);
145✔
163
  } else if (type == "weight") {
208✔
164
    return Filter::create<WeightFilter>(id);
11✔
165
  } else if (type == "zernike") {
197✔
166
    return Filter::create<ZernikeFilter>(id);
38✔
167
  } else if (type == "zernikeradial") {
159✔
168
    return Filter::create<ZernikeRadialFilter>(id);
82✔
169
  } else {
77✔
170
    throw std::runtime_error {fmt::format("Unknown filter type: {}", type)};
16✔
171
  }
61✔
172
  return nullptr;
11✔
173
}
50✔
174

50✔
175
void Filter::set_id(int32_t id)
×
176
{
×
177
  assert(id >= 0 || id == C_NONE);
178

×
179
  // Clear entry in filter map if an ID was already assigned before
180
  if (id_ != C_NONE) {
181
    model::filter_map.erase(id_);
182
    id_ = C_NONE;
183
  }
11,842✔
184

185
  // Make sure no other filter has same ID
9,168✔
186
  if (model::filter_map.find(id) != model::filter_map.end()) {
187
    throw std::runtime_error {
188
      "Two filters have the same ID: " + std::to_string(id)};
11,842✔
189
  }
1,793✔
190

1,793✔
191
  // If no ID specified, auto-assign next ID in sequence
192
  if (id == C_NONE) {
193
    id = 0;
194
    for (const auto& f : model::tally_filters) {
11,842✔
195
      id = std::max(id, f->id_);
×
196
    }
×
197
    ++id;
198
  }
199

200
  // Update ID and entry in filter map
11,842✔
201
  id_ = id;
2,067✔
202
  model::filter_map[id] = index_;
11,097✔
203
}
9,030✔
204

205
//==============================================================================
2,067✔
206
// C API functions
207
//==============================================================================
208

209
int verify_filter(int32_t index)
11,842✔
210
{
11,842✔
211
  if (index < 0 || index >= model::tally_filters.size()) {
11,842✔
212
    set_errmsg("Filter index is out of bounds.");
213
    return OPENMC_E_OUT_OF_BOUNDS;
214
  }
215
  return 0;
216
}
217

33,437✔
218
extern "C" int openmc_filter_get_id(int32_t index, int32_t* id)
219
{
33,437✔
220
  if (int err = verify_filter(index))
×
221
    return err;
×
222

223
  *id = model::tally_filters[index]->id();
33,437✔
224
  return 0;
225
}
226

17,373✔
227
extern "C" int openmc_filter_set_id(int32_t index, int32_t id)
228
{
17,373✔
229
  if (int err = verify_filter(index))
×
230
    return err;
231

17,373✔
232
  model::tally_filters[index]->set_id(id);
17,373✔
233
  return 0;
234
}
235

1,793✔
236
extern "C" int openmc_filter_get_type(int32_t index, char* type)
237
{
1,793✔
238
  if (int err = verify_filter(index))
×
239
    return err;
240

1,793✔
241
  std::strcpy(type, model::tally_filters[index]->type_str().c_str());
1,793✔
242
  return 0;
243
}
244

9,395✔
245
extern "C" int openmc_filter_get_num_bins(int32_t index, int* n_bins)
246
{
9,395✔
247
  if (int err = verify_filter(index))
×
248
    return err;
249

9,395✔
250
  *n_bins = model::tally_filters[index]->n_bins();
9,395✔
251
  return 0;
252
}
253

144✔
254
extern "C" int openmc_get_filter_index(int32_t id, int32_t* index)
255
{
144✔
256
  auto it = model::filter_map.find(id);
×
257
  if (it == model::filter_map.end()) {
258
    set_errmsg("No filter exists with ID=" + std::to_string(id) + ".");
144✔
259
    return OPENMC_E_INVALID_ID;
144✔
260
  }
261

262
  *index = it->second;
13✔
263
  return 0;
264
}
13✔
265

13✔
266
extern "C" void openmc_get_filter_next_id(int32_t* id)
×
267
{
×
268
  int32_t largest_filter_id = 0;
269
  for (const auto& t : model::tally_filters) {
270
    largest_filter_id = std::max(largest_filter_id, t->id());
13✔
271
  }
13✔
272
  *id = largest_filter_id + 1;
273
}
274

×
275
extern "C" int openmc_new_filter(const char* type, int32_t* index)
276
{
×
277
  *index = model::tally_filters.size();
×
278
  Filter::create(type);
×
279
  return 0;
280
}
×
281

282
} // namespace openmc
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