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

openmc-dev / openmc / 15714932041

17 Jun 2025 06:11PM UTC coverage: 85.297% (+0.1%) from 85.162%
15714932041

Pull #3453

github

web-flow
Merge c8f3e97e5 into 23eab2c89
Pull Request #3453: Secondary energy filter

31 of 50 new or added lines in 6 files covered. (62.0%)

1 existing line in 1 file now uncovered.

52595 of 61661 relevant lines covered (85.3%)

36868606.1 hits per line

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

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

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

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

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

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

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

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

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

103
Filter* Filter::create(const std::string& type, int32_t id)
104
{
7,741✔
105
  if (type == "azimuthal") {
106
    return Filter::create<AzimuthalFilter>(id);
107
  } else if (type == "cell") {
7,741✔
108
    return Filter::create<CellFilter>(id);
7,741✔
109
  } else if (type == "cellborn") {
7,741✔
110
    return Filter::create<CellBornFilter>(id);
111
  } else if (type == "cellfrom") {
9,216✔
112
    return Filter::create<CellFromFilter>(id);
113
  } else if (type == "cellinstance") {
9,216✔
114
    return Filter::create<CellInstanceFilter>(id);
32✔
115
  } else if (type == "distribcell") {
9,184✔
116
    return Filter::create<DistribcellFilter>(id);
737✔
117
  } else if (type == "delayedgroup") {
8,447✔
118
    return Filter::create<DelayedGroupFilter>(id);
16✔
119
  } else if (type == "energyfunction") {
8,431✔
120
    return Filter::create<EnergyFunctionFilter>(id);
96✔
121
  } else if (type == "energy") {
8,335✔
122
    return Filter::create<EnergyFilter>(id);
34✔
123
  } else if (type == "collision") {
8,301✔
124
    return Filter::create<CollisionFilter>(id);
177✔
125
  } else if (type == "energyout") {
8,124✔
126
    return Filter::create<EnergyoutFilter>(id);
96✔
127
  } else if (type == "secondaryenergy") {
8,028✔
128
    return Filter::create<SecondaryEnergyFilter>(id);
205✔
129
  } else if (type == "legendre") {
7,823✔
130
    return Filter::create<LegendreFilter>(id);
1,308✔
131
  } else if (type == "material") {
6,515✔
132
    return Filter::create<MaterialFilter>(id);
16✔
133
  } else if (type == "materialfrom") {
6,499✔
134
    return Filter::create<MaterialFromFilter>(id);
413✔
135
  } else if (type == "mesh") {
6,086✔
UNCOV
136
    return Filter::create<MeshFilter>(id);
×
137
  } else if (type == "meshborn") {
6,086✔
138
    return Filter::create<MeshBornFilter>(id);
587✔
139
  } else if (type == "meshmaterial") {
5,499✔
140
    return Filter::create<MeshMaterialFilter>(id);
1,991✔
141
  } else if (type == "meshsurface") {
3,508✔
142
    return Filter::create<MeshSurfaceFilter>(id);
16✔
143
  } else if (type == "mu") {
3,492✔
144
    return Filter::create<MuFilter>(id);
2,215✔
145
  } else if (type == "musurface") {
1,277✔
146
    return Filter::create<MuSurfaceFilter>(id);
27✔
147
  } else if (type == "parentnuclide") {
1,250✔
148
    return Filter::create<ParentNuclideFilter>(id);
11✔
149
  } else if (type == "particle") {
1,239✔
150
    return Filter::create<ParticleFilter>(id);
410✔
151
  } else if (type == "polar") {
829✔
152
    return Filter::create<PolarFilter>(id);
32✔
153
  } else if (type == "surface") {
797✔
154
    return Filter::create<SurfaceFilter>(id);
27✔
155
  } else if (type == "spatiallegendre") {
770✔
156
    return Filter::create<SpatialLegendreFilter>(id);
11✔
157
  } else if (type == "sphericalharmonics") {
759✔
158
    return Filter::create<SphericalHarmonicsFilter>(id);
375✔
159
  } else if (type == "time") {
384✔
160
    return Filter::create<TimeFilter>(id);
48✔
161
  } else if (type == "universe") {
336✔
162
    return Filter::create<UniverseFilter>(id);
145✔
163
  } else if (type == "weight") {
191✔
164
    return Filter::create<WeightFilter>(id);
11✔
165
  } else if (type == "zernike") {
180✔
166
    return Filter::create<ZernikeFilter>(id);
38✔
167
  } else if (type == "zernikeradial") {
142✔
168
    return Filter::create<ZernikeRadialFilter>(id);
71✔
169
  } else {
71✔
170
    throw std::runtime_error {fmt::format("Unknown filter type: {}", type)};
16✔
171
  }
55✔
172
  return nullptr;
11✔
173
}
44✔
174

44✔
175
void Filter::set_id(int32_t id)
×
176
{
×
177
  assert(id >= 0 || id == C_NONE);
178

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

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

1,220✔
191
  // If no ID specified, auto-assign next ID in sequence
192
  if (id == C_NONE) {
193
    id = 0;
194
    for (const auto& f : model::tally_filters) {
10,452✔
195
      id = std::max(id, f->id_);
×
196
    }
×
197
    ++id;
198
  }
199

200
  // Update ID and entry in filter map
10,452✔
201
  id_ = id;
1,491✔
202
  model::filter_map[id] = index_;
7,471✔
203
}
5,980✔
204

205
//==============================================================================
1,491✔
206
// C API functions
207
//==============================================================================
208

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

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

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

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

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

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

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

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

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

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

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

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

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