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

openmc-dev / openmc / 18554903056

16 Oct 2025 08:18AM UTC coverage: 81.836% (-0.08%) from 81.917%
18554903056

Pull #3547

github

web-flow
Merge 192e6b2c2 into b94b49611
Pull Request #3547: Tally spectrum of secondary particles

16598 of 23152 branches covered (71.69%)

Branch coverage included in aggregate %.

24 of 67 new or added lines in 10 files covered. (35.82%)

30 existing lines in 3 files now uncovered.

53729 of 62784 relevant lines covered (85.58%)

44550947.96 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_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,995✔
63
{
64
  return model::tally_filters.size();
1,995✔
65
}
66

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

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

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

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

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

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

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

103
Filter* Filter::create(const std::string& type, int32_t id)
10,716✔
104
{
105
  if (type == "azimuthal") {
10,716✔
106
    return Filter::create<AzimuthalFilter>(id);
32✔
107
  } else if (type == "cell") {
10,684✔
108
    return Filter::create<CellFilter>(id);
800✔
109
  } else if (type == "cellborn") {
9,884✔
110
    return Filter::create<CellBornFilter>(id);
16✔
111
  } else if (type == "cellfrom") {
9,868✔
112
    return Filter::create<CellFromFilter>(id);
96✔
113
  } else if (type == "cellinstance") {
9,772✔
114
    return Filter::create<CellInstanceFilter>(id);
34✔
115
  } else if (type == "distribcell") {
9,738✔
116
    return Filter::create<DistribcellFilter>(id);
213✔
117
  } else if (type == "delayedgroup") {
9,525✔
118
    return Filter::create<DelayedGroupFilter>(id);
134✔
119
  } else if (type == "energyfunction") {
9,391✔
120
    return Filter::create<EnergyFunctionFilter>(id);
280✔
121
  } else if (type == "energy") {
9,111✔
122
    return Filter::create<EnergyFilter>(id);
1,599✔
123
  } else if (type == "collision") {
7,512✔
124
    return Filter::create<CollisionFilter>(id);
16✔
125
  } else if (type == "energyout") {
7,496✔
126
    return Filter::create<EnergyoutFilter>(id);
422✔
127
  } else if (type == "legendre") {
7,074✔
128
    return Filter::create<LegendreFilter>(id);
648✔
129
  } else if (type == "material") {
6,426✔
130
    return Filter::create<MaterialFilter>(id);
2,411✔
131
  } else if (type == "materialfrom") {
4,015✔
132
    return Filter::create<MaterialFromFilter>(id);
16✔
133
  } else if (type == "mesh") {
3,999✔
134
    return Filter::create<MeshFilter>(id);
2,542✔
135
  } else if (type == "meshborn") {
1,457✔
136
    return Filter::create<MeshBornFilter>(id);
27✔
137
  } else if (type == "meshmaterial") {
1,430✔
138
    return Filter::create<MeshMaterialFilter>(id);
11✔
139
  } else if (type == "meshsurface") {
1,419✔
140
    return Filter::create<MeshSurfaceFilter>(id);
494✔
141
  } else if (type == "mu") {
925✔
142
    return Filter::create<MuFilter>(id);
32✔
143
  } else if (type == "musurface") {
893✔
144
    return Filter::create<MuSurfaceFilter>(id);
27✔
145
  } else if (type == "parentnuclide") {
866✔
146
    return Filter::create<ParentNuclideFilter>(id);
11✔
147
  } else if (type == "particle") {
855✔
148
    return Filter::create<ParticleFilter>(id);
442✔
149
  } else if (type == "particleout") {
413!
NEW
150
    return Filter::create<ParticleOutFilter>(id);
×
151
  } else if (type == "polar") {
413✔
152
    return Filter::create<PolarFilter>(id);
48✔
153
  } else if (type == "surface") {
365✔
154
    return Filter::create<SurfaceFilter>(id);
145✔
155
  } else if (type == "spatiallegendre") {
220✔
156
    return Filter::create<SpatialLegendreFilter>(id);
11✔
157
  } else if (type == "sphericalharmonics") {
209✔
158
    return Filter::create<SphericalHarmonicsFilter>(id);
38✔
159
  } else if (type == "time") {
171✔
160
    return Filter::create<TimeFilter>(id);
82✔
161
  } else if (type == "universe") {
89✔
162
    return Filter::create<UniverseFilter>(id);
16✔
163
  } else if (type == "weight") {
73✔
164
    return Filter::create<WeightFilter>(id);
11✔
165
  } else if (type == "zernike") {
62!
166
    return Filter::create<ZernikeFilter>(id);
62✔
167
  } else if (type == "zernikeradial") {
×
168
    return Filter::create<ZernikeRadialFilter>(id);
×
169
  } else {
170
    throw std::runtime_error {fmt::format("Unknown filter type: {}", type)};
×
171
  }
172
  return nullptr;
173
}
174

175
void Filter::set_id(int32_t id)
12,727✔
176
{
177
  assert(id >= 0 || id == C_NONE);
10,705!
178

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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());
×
271
  }
272
  *id = largest_filter_id + 1;
×
273
}
×
274

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