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

openmc-dev / openmc / 22337978178

24 Feb 2026 05:28AM UTC coverage: 81.812% (+0.1%) from 81.688%
22337978178

Pull #3817

github

web-flow
Merge 7b55f5551 into 83a30f686
Pull Request #3817: Fix MeshFilter.get_pandas_dataframe to handle all mesh types

17352 of 24448 branches covered (70.98%)

Branch coverage included in aggregate %.

23 of 25 new or added lines in 4 files covered. (92.0%)

736 existing lines in 15 files now uncovered.

57709 of 67300 relevant lines covered (85.75%)

45474941.11 hits per line

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

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

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

49
namespace openmc {
50

51
//==============================================================================
52
// Global variables
53
//==============================================================================
54

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

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

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

69
//==============================================================================
70
// Filter implementation
71
//==============================================================================
72

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

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

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

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

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

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

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

179
void Filter::set_id(int32_t id)
10,954✔
180
{
181
  assert(id >= 0 || id == C_NONE);
8,685!
182

183
  // Clear entry in filter map if an ID was already assigned before
184
  if (id_ != C_NONE) {
10,954✔
185
    model::filter_map.erase(id_);
1,046✔
186
    id_ = C_NONE;
1,046✔
187
  }
188

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

195
  // If no ID specified, auto-assign next ID in sequence
196
  if (id == C_NONE) {
10,954✔
197
    id = 0;
1,290✔
198
    for (const auto& f : model::tally_filters) {
6,598✔
199
      id = std::max(id, f->id_);
5,308✔
200
    }
201
    ++id;
1,290✔
202
  }
203

204
  // Update ID and entry in filter map
205
  id_ = id;
10,954✔
206
  model::filter_map[id] = index_;
10,954✔
207
}
10,954✔
208

209
//==============================================================================
210
// C API functions
211
//==============================================================================
212

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

222
extern "C" int openmc_filter_get_id(int32_t index, int32_t* id)
9,676✔
223
{
224
  if (int err = verify_filter(index))
9,676!
UNCOV
225
    return err;
×
226

227
  *id = model::tally_filters[index]->id();
9,676✔
228
  return 0;
9,676✔
229
}
230

231
extern "C" int openmc_filter_set_id(int32_t index, int32_t id)
1,046✔
232
{
233
  if (int err = verify_filter(index))
1,046!
UNCOV
234
    return err;
×
235

236
  model::tally_filters[index]->set_id(id);
1,046✔
237
  return 0;
1,046✔
238
}
239

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

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

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

254
  *n_bins = model::tally_filters[index]->n_bins();
120✔
255
  return 0;
120✔
256
}
257

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

266
  *index = it->second;
10✔
267
  return 0;
10✔
268
}
269

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

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

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