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

openmc-dev / openmc / 22231051182

20 Feb 2026 03:57PM UTC coverage: 81.849% (-0.2%) from 82.058%
22231051182

Pull #2693

github

web-flow
Merge c2c0a6ecd into 53ce1910f
Pull Request #2693: Add reactivity control to coupled transport-depletion analyses

17270 of 24310 branches covered (71.04%)

Branch coverage included in aggregate %.

77 of 82 new or added lines in 4 files covered. (93.9%)

3461 existing lines in 80 files now uncovered.

57624 of 67193 relevant lines covered (85.76%)

49135727.7 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_polar.h"
36
#include "openmc/tallies/filter_sph_harm.h"
37
#include "openmc/tallies/filter_sptl_legendre.h"
38
#include "openmc/tallies/filter_surface.h"
39
#include "openmc/tallies/filter_time.h"
40
#include "openmc/tallies/filter_universe.h"
41
#include "openmc/tallies/filter_weight.h"
42
#include "openmc/tallies/filter_zernike.h"
43
#include "openmc/xml_interface.h"
44

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

48
namespace openmc {
49

50
//==============================================================================
51
// Global variables
52
//==============================================================================
53

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

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

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

68
//==============================================================================
69
// Filter implementation
70
//==============================================================================
71

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

77
Filter::~Filter()
9,924✔
78
{
79
  model::filter_map.erase(id_);
9,924✔
80
}
9,924✔
81

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

90
  // Convert filter type to lower case
91
  std::string s;
8,604✔
92
  if (check_for_node(node, "type")) {
8,604!
93
    s = get_node_value(node, "type", true);
8,604✔
94
  }
95

96
  // Allocate according to the filter type
97
  auto f = Filter::create(s, filter_id);
8,604✔
98

99
  // Read filter data from XML
100
  f->from_xml(node);
8,604✔
101
  return f;
8,604✔
102
}
8,604✔
103

104
Filter* Filter::create(const std::string& type, int32_t id)
9,910✔
105
{
106
  if (type == "azimuthal") {
9,910✔
107
    return Filter::create<AzimuthalFilter>(id);
28✔
108
  } else if (type == "cell") {
9,882✔
109
    return Filter::create<CellFilter>(id);
697✔
110
  } else if (type == "cellborn") {
9,185✔
111
    return Filter::create<CellBornFilter>(id);
14✔
112
  } else if (type == "cellfrom") {
9,171✔
113
    return Filter::create<CellFromFilter>(id);
84✔
114
  } else if (type == "cellinstance") {
9,087✔
115
    return Filter::create<CellInstanceFilter>(id);
30✔
116
  } else if (type == "distribcell") {
9,057✔
117
    return Filter::create<DistribcellFilter>(id);
166✔
118
  } else if (type == "delayedgroup") {
8,891✔
119
    return Filter::create<DelayedGroupFilter>(id);
118✔
120
  } else if (type == "energyfunction") {
8,773✔
121
    return Filter::create<EnergyFunctionFilter>(id);
182✔
122
  } else if (type == "energy") {
8,591✔
123
    return Filter::create<EnergyFilter>(id);
1,733✔
124
  } else if (type == "collision") {
6,858✔
125
    return Filter::create<CollisionFilter>(id);
14✔
126
  } else if (type == "energyout") {
6,844✔
127
    return Filter::create<EnergyoutFilter>(id);
594✔
128
  } else if (type == "legendre") {
6,250✔
129
    return Filter::create<LegendreFilter>(id);
728✔
130
  } else if (type == "material") {
5,522✔
131
    return Filter::create<MaterialFilter>(id);
2,176✔
132
  } else if (type == "materialfrom") {
3,346✔
133
    return Filter::create<MaterialFromFilter>(id);
14✔
134
  } else if (type == "mesh") {
3,332✔
135
    return Filter::create<MeshFilter>(id);
2,113✔
136
  } else if (type == "meshborn") {
1,219✔
137
    return Filter::create<MeshBornFilter>(id);
24✔
138
  } else if (type == "meshmaterial") {
1,195✔
139
    return Filter::create<MeshMaterialFilter>(id);
20✔
140
  } else if (type == "meshsurface") {
1,175✔
141
    return Filter::create<MeshSurfaceFilter>(id);
340✔
142
  } else if (type == "mu") {
835✔
143
    return Filter::create<MuFilter>(id);
28✔
144
  } else if (type == "musurface") {
807✔
145
    return Filter::create<MuSurfaceFilter>(id);
24✔
146
  } else if (type == "parentnuclide") {
783✔
147
    return Filter::create<ParentNuclideFilter>(id);
10✔
148
  } else if (type == "particle") {
773✔
149
    return Filter::create<ParticleFilter>(id);
365✔
150
  } else if (type == "particleproduction") {
408✔
151
    return Filter::create<ParticleProductionFilter>(id);
28✔
152
  } else if (type == "polar") {
380✔
153
    return Filter::create<PolarFilter>(id);
42✔
154
  } else if (type == "surface") {
338✔
155
    return Filter::create<SurfaceFilter>(id);
156✔
156
  } else if (type == "spatiallegendre") {
182✔
157
    return Filter::create<SpatialLegendreFilter>(id);
10✔
158
  } else if (type == "sphericalharmonics") {
172✔
159
    return Filter::create<SphericalHarmonicsFilter>(id);
34✔
160
  } else if (type == "time") {
138✔
161
    return Filter::create<TimeFilter>(id);
74✔
162
  } else if (type == "universe") {
64✔
163
    return Filter::create<UniverseFilter>(id);
14✔
164
  } else if (type == "weight") {
50✔
165
    return Filter::create<WeightFilter>(id);
10✔
166
  } else if (type == "zernike") {
40!
167
    return Filter::create<ZernikeFilter>(id);
40✔
168
  } else if (type == "zernikeradial") {
×
UNCOV
169
    return Filter::create<ZernikeRadialFilter>(id);
×
170
  } else {
UNCOV
171
    throw std::runtime_error {fmt::format("Unknown filter type: {}", type)};
×
172
  }
173
  return nullptr;
174
}
175

176
void Filter::set_id(int32_t id)
11,000✔
177
{
178
  assert(id >= 0 || id == C_NONE);
8,722!
179

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

186
  // Make sure no other filter has same ID
187
  if (model::filter_map.find(id) != model::filter_map.end()) {
11,000!
UNCOV
188
    throw std::runtime_error {
×
UNCOV
189
      "Two filters have the same ID: " + std::to_string(id)};
×
190
  }
191

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

201
  // Update ID and entry in filter map
202
  id_ = id;
11,000✔
203
  model::filter_map[id] = index_;
11,000✔
204
}
11,000✔
205

206
//==============================================================================
207
// C API functions
208
//==============================================================================
209

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

219
extern "C" int openmc_filter_get_id(int32_t index, int32_t* id)
9,706✔
220
{
221
  if (int err = verify_filter(index))
9,706!
UNCOV
222
    return err;
×
223

224
  *id = model::tally_filters[index]->id();
9,706✔
225
  return 0;
9,706✔
226
}
227

228
extern "C" int openmc_filter_set_id(int32_t index, int32_t id)
1,076✔
229
{
230
  if (int err = verify_filter(index))
1,076!
UNCOV
231
    return err;
×
232

233
  model::tally_filters[index]->set_id(id);
1,076✔
234
  return 0;
1,076✔
235
}
236

237
extern "C" int openmc_filter_get_type(int32_t index, char* type)
5,300✔
238
{
239
  if (int err = verify_filter(index))
5,300!
UNCOV
240
    return err;
×
241

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

246
extern "C" int openmc_filter_get_num_bins(int32_t index, int* n_bins)
120✔
247
{
248
  if (int err = verify_filter(index))
120!
UNCOV
249
    return err;
×
250

251
  *n_bins = model::tally_filters[index]->n_bins();
120✔
252
  return 0;
120✔
253
}
254

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

263
  *index = it->second;
10✔
264
  return 0;
10✔
265
}
266

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

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

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