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

openmc-dev / openmc / 25472760865

07 May 2026 02:32AM UTC coverage: 80.952% (-0.4%) from 81.374%
25472760865

Pull #3757

github

web-flow
Merge 4928ac803 into e542b2f03
Pull Request #3757: Testing point detectors

17726 of 25786 branches covered (68.74%)

Branch coverage included in aggregate %.

44 of 386 new or added lines in 25 files covered. (11.4%)

81 existing lines in 3 files now uncovered.

58569 of 68461 relevant lines covered (85.55%)

47138839.61 hits per line

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

80.22
/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_particle_production.h"
35
#include "openmc/tallies/filter_point.h"
36
#include "openmc/tallies/filter_polar.h"
37
#include "openmc/tallies/filter_reaction.h"
38
#include "openmc/tallies/filter_sph_harm.h"
39
#include "openmc/tallies/filter_sptl_legendre.h"
40
#include "openmc/tallies/filter_surface.h"
41
#include "openmc/tallies/filter_time.h"
42
#include "openmc/tallies/filter_universe.h"
43
#include "openmc/tallies/filter_weight.h"
44
#include "openmc/tallies/filter_zernike.h"
45
#include "openmc/xml_interface.h"
46

47
// explicit template instantiation definition
48
template class openmc::vector<openmc::FilterMatch>;
49

50
namespace openmc {
51

52
//==============================================================================
53
// Global variables
54
//==============================================================================
55

56
namespace model {
57
std::unordered_map<int, int> filter_map;
58
vector<unique_ptr<Filter>> tally_filters;
59
} // namespace model
60

61
//==============================================================================
62
// Non-member functions
63
//==============================================================================
64

65
extern "C" size_t tally_filters_size()
1,184✔
66
{
67
  return model::tally_filters.size();
1,184✔
68
}
69

70
//==============================================================================
71
// Filter implementation
72
//==============================================================================
73

74
Filter::Filter()
11,112✔
75
{
76
  index_ = model::tally_filters.size(); // Avoids warning about narrowing
11,112✔
77
}
11,112✔
78

79
Filter::~Filter()
11,112✔
80
{
81
  model::filter_map.erase(id_);
11,112✔
82
}
11,112✔
83

84
Filter* Filter::create(pugi::xml_node node)
9,619✔
85
{
86
  // Copy filter id
87
  if (!check_for_node(node, "id")) {
9,619!
88
    fatal_error("Must specify id for filter in tally XML file.");
×
89
  }
90
  int filter_id = std::stoi(get_node_value(node, "id"));
19,238✔
91

92
  // Convert filter type to lower case
93
  std::string s;
9,619✔
94
  if (check_for_node(node, "type")) {
9,619!
95
    s = get_node_value(node, "type", true);
9,619✔
96
  }
97

98
  // Allocate according to the filter type
99
  auto f = Filter::create(s, filter_id);
9,619✔
100

101
  // Read filter data from XML
102
  f->from_xml(node);
9,619✔
103
  return f;
9,619✔
104
}
9,619✔
105

106
Filter* Filter::create(const std::string& type, int32_t id)
11,097✔
107
{
108
  if (type == "azimuthal") {
11,097✔
109
    return Filter::create<AzimuthalFilter>(id);
30✔
110
  } else if (type == "cell") {
11,067✔
111
    return Filter::create<CellFilter>(id);
812✔
112
  } else if (type == "cellborn") {
10,255✔
113
    return Filter::create<CellBornFilter>(id);
15✔
114
  } else if (type == "cellfrom") {
10,240✔
115
    return Filter::create<CellFromFilter>(id);
101✔
116
  } else if (type == "cellinstance") {
10,139✔
117
    return Filter::create<CellInstanceFilter>(id);
32✔
118
  } else if (type == "distribcell") {
10,107✔
119
    return Filter::create<DistribcellFilter>(id);
179✔
120
  } else if (type == "delayedgroup") {
9,928✔
121
    return Filter::create<DelayedGroupFilter>(id);
127✔
122
  } else if (type == "energyfunction") {
9,801✔
123
    return Filter::create<EnergyFunctionFilter>(id);
197✔
124
  } else if (type == "energy") {
9,604✔
125
    return Filter::create<EnergyFilter>(id);
1,935✔
126
  } else if (type == "collision") {
7,669✔
127
    return Filter::create<CollisionFilter>(id);
15✔
128
  } else if (type == "energyout") {
7,654✔
129
    return Filter::create<EnergyoutFilter>(id);
646✔
130
  } else if (type == "legendre") {
7,008✔
131
    return Filter::create<LegendreFilter>(id);
793✔
132
  } else if (type == "material") {
6,215✔
133
    return Filter::create<MaterialFilter>(id);
2,411✔
134
  } else if (type == "materialfrom") {
3,804✔
135
    return Filter::create<MaterialFromFilter>(id);
15✔
136
  } else if (type == "mesh") {
3,789✔
137
    return Filter::create<MeshFilter>(id);
2,354✔
138
  } else if (type == "meshborn") {
1,435✔
139
    return Filter::create<MeshBornFilter>(id);
26✔
140
  } else if (type == "meshmaterial") {
1,409✔
141
    return Filter::create<MeshMaterialFilter>(id);
44✔
142
  } else if (type == "meshsurface") {
1,365✔
143
    return Filter::create<MeshSurfaceFilter>(id);
370✔
144
  } else if (type == "mu") {
995✔
145
    return Filter::create<MuFilter>(id);
30✔
146
  } else if (type == "musurface") {
965✔
147
    return Filter::create<MuSurfaceFilter>(id);
26✔
148
  } else if (type == "parentnuclide") {
939✔
149
    return Filter::create<ParentNuclideFilter>(id);
11✔
150
  } else if (type == "particle") {
928✔
151
    return Filter::create<ParticleFilter>(id);
427✔
152
  } else if (type == "particleproduction") {
501✔
153
    return Filter::create<ParticleProductionFilter>(id);
30✔
154
  } else if (type == "point") {
471!
NEW
155
    return Filter::create<PointFilter>(id);
×
156
  } else if (type == "polar") {
471✔
157
    return Filter::create<PolarFilter>(id);
45✔
158
  } else if (type == "reaction") {
426✔
159
    return Filter::create<ReactionFilter>(id);
15✔
160
  } else if (type == "surface") {
411✔
161
    return Filter::create<SurfaceFilter>(id);
212✔
162
  } else if (type == "spatiallegendre") {
199✔
163
    return Filter::create<SpatialLegendreFilter>(id);
11✔
164
  } else if (type == "sphericalharmonics") {
188✔
165
    return Filter::create<SphericalHarmonicsFilter>(id);
37✔
166
  } else if (type == "time") {
151✔
167
    return Filter::create<TimeFilter>(id);
81✔
168
  } else if (type == "universe") {
70✔
169
    return Filter::create<UniverseFilter>(id);
15✔
170
  } else if (type == "weight") {
55✔
171
    return Filter::create<WeightFilter>(id);
11✔
172
  } else if (type == "zernike") {
44!
173
    return Filter::create<ZernikeFilter>(id);
44✔
174
  } else if (type == "zernikeradial") {
×
175
    return Filter::create<ZernikeRadialFilter>(id);
×
176
  } else {
177
    throw std::runtime_error {fmt::format("Unknown filter type: {}", type)};
×
178
  }
179
  return nullptr;
180
}
181

182
void Filter::set_id(int32_t id)
12,296✔
183
{
184
  assert(id >= 0 || id == C_NONE);
12,296!
185

186
  // Clear entry in filter map if an ID was already assigned before
187
  if (id_ != C_NONE) {
12,296✔
188
    model::filter_map.erase(id_);
1,184✔
189
    id_ = C_NONE;
1,184✔
190
  }
191

192
  // Make sure no other filter has same ID
193
  if (model::filter_map.find(id) != model::filter_map.end()) {
12,296!
194
    throw std::runtime_error {
×
195
      "Two filters have the same ID: " + std::to_string(id)};
×
196
  }
197

198
  // If no ID specified, auto-assign next ID in sequence
199
  if (id == C_NONE) {
12,296✔
200
    id = 0;
1,493✔
201
    for (const auto& f : model::tally_filters) {
7,526✔
202
      id = std::max(id, f->id_);
10,303✔
203
    }
204
    ++id;
1,493✔
205
  }
206

207
  // Update ID and entry in filter map
208
  id_ = id;
12,296✔
209
  model::filter_map[id] = index_;
12,296✔
210
}
12,296✔
211

212
//==============================================================================
213
// C API functions
214
//==============================================================================
215

216
int verify_filter(int32_t index)
20,889✔
217
{
218
  if (index < 0 || index >= model::tally_filters.size()) {
20,889!
219
    set_errmsg("Filter index is out of bounds.");
×
220
    return OPENMC_E_OUT_OF_BOUNDS;
×
221
  }
222
  return 0;
223
}
224

225
extern "C" int openmc_filter_get_id(int32_t index, int32_t* id)
10,699✔
226
{
227
  if (int err = verify_filter(index))
10,699!
228
    return err;
229

230
  *id = model::tally_filters[index]->id();
10,699✔
231
  return 0;
10,699✔
232
}
233

234
extern "C" int openmc_filter_set_id(int32_t index, int32_t id)
1,184✔
235
{
236
  if (int err = verify_filter(index))
1,184!
237
    return err;
238

239
  model::tally_filters[index]->set_id(id);
1,184✔
240
  return 0;
1,184✔
241
}
242

243
extern "C" int openmc_filter_get_type(int32_t index, char* type)
5,852✔
244
{
245
  if (int err = verify_filter(index))
5,852!
246
    return err;
247

248
  std::strcpy(type, model::tally_filters[index]->type_str().c_str());
5,852✔
249
  return 0;
5,852✔
250
}
251

252
extern "C" int openmc_filter_get_num_bins(int32_t index, int* n_bins)
132✔
253
{
254
  if (int err = verify_filter(index))
132!
255
    return err;
256

257
  *n_bins = model::tally_filters[index]->n_bins();
132✔
258
  return 0;
132✔
259
}
260

261
extern "C" int openmc_get_filter_index(int32_t id, int32_t* index)
11✔
262
{
263
  auto it = model::filter_map.find(id);
11!
264
  if (it == model::filter_map.end()) {
11!
265
    set_errmsg("No filter exists with ID=" + std::to_string(id) + ".");
×
266
    return OPENMC_E_INVALID_ID;
×
267
  }
268

269
  *index = it->second;
11✔
270
  return 0;
11✔
271
}
272

273
extern "C" void openmc_get_filter_next_id(int32_t* id)
×
274
{
275
  int32_t largest_filter_id = 0;
×
276
  for (const auto& t : model::tally_filters) {
×
277
    largest_filter_id = std::max(largest_filter_id, t->id());
×
278
  }
279
  *id = largest_filter_id + 1;
×
280
}
×
281

282
extern "C" int openmc_new_filter(const char* type, int32_t* index)
1,184✔
283
{
284
  *index = model::tally_filters.size();
1,184✔
285
  Filter::create(type);
1,184✔
286
  return 0;
1,184✔
287
}
288

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