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

openmc-dev / openmc / 21498346812

29 Jan 2026 11:22PM UTC coverage: 81.303% (-0.7%) from 81.953%
21498346812

Pull #3757

github

web-flow
Merge 00995505f into f7a734189
Pull Request #3757: Testing point detectors

17285 of 24400 branches covered (70.84%)

Branch coverage included in aggregate %.

103 of 504 new or added lines in 26 files covered. (20.44%)

3 existing lines in 2 files now uncovered.

55851 of 65555 relevant lines covered (85.2%)

43756726.79 hits per line

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

79.48
/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_point.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,150✔
64
{
65
  return model::tally_filters.size();
1,150✔
66
}
67

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

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

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

82
Filter* Filter::create(pugi::xml_node node)
8,951✔
83
{
84
  // Copy filter id
85
  if (!check_for_node(node, "id")) {
8,951!
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,951✔
89

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

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

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

104
Filter* Filter::create(const std::string& type, int32_t id)
10,359✔
105
{
106
  if (type == "azimuthal") {
10,359✔
107
    return Filter::create<AzimuthalFilter>(id);
32✔
108
  } else if (type == "cell") {
10,327✔
109
    return Filter::create<CellFilter>(id);
786✔
110
  } else if (type == "cellborn") {
9,541✔
111
    return Filter::create<CellBornFilter>(id);
16✔
112
  } else if (type == "cellfrom") {
9,525✔
113
    return Filter::create<CellFromFilter>(id);
96✔
114
  } else if (type == "cellinstance") {
9,429✔
115
    return Filter::create<CellInstanceFilter>(id);
34✔
116
  } else if (type == "distribcell") {
9,395✔
117
    return Filter::create<DistribcellFilter>(id);
188✔
118
  } else if (type == "delayedgroup") {
9,207✔
119
    return Filter::create<DelayedGroupFilter>(id);
134✔
120
  } else if (type == "energyfunction") {
9,073✔
121
    return Filter::create<EnergyFunctionFilter>(id);
205✔
122
  } else if (type == "energy") {
8,868✔
123
    return Filter::create<EnergyFilter>(id);
1,664✔
124
  } else if (type == "collision") {
7,204✔
125
    return Filter::create<CollisionFilter>(id);
16✔
126
  } else if (type == "energyout") {
7,188✔
127
    return Filter::create<EnergyoutFilter>(id);
553✔
128
  } else if (type == "legendre") {
6,635✔
129
    return Filter::create<LegendreFilter>(id);
701✔
130
  } else if (type == "material") {
5,934✔
131
    return Filter::create<MaterialFilter>(id);
2,307✔
132
  } else if (type == "materialfrom") {
3,627✔
133
    return Filter::create<MaterialFromFilter>(id);
16✔
134
  } else if (type == "mesh") {
3,611✔
135
    return Filter::create<MeshFilter>(id);
2,309✔
136
  } else if (type == "meshborn") {
1,302✔
137
    return Filter::create<MeshBornFilter>(id);
27✔
138
  } else if (type == "meshmaterial") {
1,275✔
139
    return Filter::create<MeshMaterialFilter>(id);
22✔
140
  } else if (type == "meshsurface") {
1,253✔
141
    return Filter::create<MeshSurfaceFilter>(id);
380✔
142
  } else if (type == "mu") {
873✔
143
    return Filter::create<MuFilter>(id);
32✔
144
  } else if (type == "musurface") {
841✔
145
    return Filter::create<MuSurfaceFilter>(id);
27✔
146
  } else if (type == "parentnuclide") {
814✔
147
    return Filter::create<ParentNuclideFilter>(id);
11✔
148
  } else if (type == "particle") {
803✔
149
    return Filter::create<ParticleFilter>(id);
376✔
150
  } else if (type == "point") {
427!
NEW
151
    return Filter::create<PointFilter>(id);
×
152
  } else if (type == "polar") {
427✔
153
    return Filter::create<PolarFilter>(id);
48✔
154
  } else if (type == "surface") {
379✔
155
    return Filter::create<SurfaceFilter>(id);
177✔
156
  } else if (type == "spatiallegendre") {
202✔
157
    return Filter::create<SpatialLegendreFilter>(id);
11✔
158
  } else if (type == "sphericalharmonics") {
191✔
159
    return Filter::create<SphericalHarmonicsFilter>(id);
38✔
160
  } else if (type == "time") {
153✔
161
    return Filter::create<TimeFilter>(id);
82✔
162
  } else if (type == "universe") {
71✔
163
    return Filter::create<UniverseFilter>(id);
16✔
164
  } else if (type == "weight") {
55✔
165
    return Filter::create<WeightFilter>(id);
11✔
166
  } else if (type == "zernike") {
44!
167
    return Filter::create<ZernikeFilter>(id);
44✔
168
  } else if (type == "zernikeradial") {
×
169
    return Filter::create<ZernikeRadialFilter>(id);
×
170
  } else {
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,525✔
177
{
178
  assert(id >= 0 || id == C_NONE);
9,386!
179

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

186
  // Make sure no other filter has same ID
187
  if (model::filter_map.find(id) != model::filter_map.end()) {
11,525!
188
    throw std::runtime_error {
×
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,525✔
194
    id = 0;
1,424✔
195
    for (const auto& f : model::tally_filters) {
7,289✔
196
      id = std::max(id, f->id_);
5,865✔
197
    }
198
    ++id;
1,424✔
199
  }
200

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

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

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

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

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

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

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

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

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

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

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

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

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

267
extern "C" void openmc_get_filter_next_id(int32_t* id)
×
268
{
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
  }
273
  *id = largest_filter_id + 1;
×
274
}
×
275

276
extern "C" int openmc_new_filter(const char* type, int32_t* index)
1,150✔
277
{
278
  *index = model::tally_filters.size();
1,150✔
279
  Filter::create(type);
1,150✔
280
  return 0;
1,150✔
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