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

openmc-dev / openmc / 17271335175

27 Aug 2025 03:30PM UTC coverage: 85.158% (-0.05%) from 85.204%
17271335175

Pull #3547

github

web-flow
Merge 4e47f041b into d1df80a21
Pull Request #3547: Tally spectrum of secondary particles

23 of 58 new or added lines in 8 files covered. (39.66%)

1 existing line in 1 file now uncovered.

52954 of 62183 relevant lines covered (85.16%)

37874602.56 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,311✔
72
{
73
  index_ = model::tally_filters.size(); // Avoids warning about narrowing
9,311✔
74
}
9,311✔
75

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

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

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

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

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

103
Filter* Filter::create(const std::string& type, int32_t id)
104
{
7,817✔
105
  if (type == "azimuthal") {
106
    return Filter::create<AzimuthalFilter>(id);
107
  } else if (type == "cell") {
7,817✔
108
    return Filter::create<CellFilter>(id);
7,817✔
109
  } else if (type == "cellborn") {
7,817✔
110
    return Filter::create<CellBornFilter>(id);
111
  } else if (type == "cellfrom") {
9,295✔
112
    return Filter::create<CellFromFilter>(id);
113
  } else if (type == "cellinstance") {
9,295✔
114
    return Filter::create<CellInstanceFilter>(id);
32✔
115
  } else if (type == "distribcell") {
9,263✔
116
    return Filter::create<DistribcellFilter>(id);
764✔
117
  } else if (type == "delayedgroup") {
8,499✔
118
    return Filter::create<DelayedGroupFilter>(id);
16✔
119
  } else if (type == "energyfunction") {
8,483✔
120
    return Filter::create<EnergyFunctionFilter>(id);
96✔
121
  } else if (type == "energy") {
8,387✔
122
    return Filter::create<EnergyFilter>(id);
34✔
123
  } else if (type == "collision") {
8,353✔
124
    return Filter::create<CollisionFilter>(id);
177✔
125
  } else if (type == "energyout") {
8,176✔
126
    return Filter::create<EnergyoutFilter>(id);
96✔
127
  } else if (type == "legendre") {
8,080✔
128
    return Filter::create<LegendreFilter>(id);
205✔
129
  } else if (type == "material") {
7,875✔
130
    return Filter::create<MaterialFilter>(id);
1,333✔
131
  } else if (type == "materialfrom") {
6,542✔
132
    return Filter::create<MaterialFromFilter>(id);
16✔
133
  } else if (type == "mesh") {
6,526✔
134
    return Filter::create<MeshFilter>(id);
414✔
135
  } else if (type == "meshborn") {
6,112✔
136
    return Filter::create<MeshBornFilter>(id);
588✔
137
  } else if (type == "meshmaterial") {
5,524✔
138
    return Filter::create<MeshMaterialFilter>(id);
2,003✔
139
  } else if (type == "meshsurface") {
3,521✔
140
    return Filter::create<MeshSurfaceFilter>(id);
16✔
141
  } else if (type == "mu") {
3,505✔
142
    return Filter::create<MuFilter>(id);
2,227✔
143
  } else if (type == "musurface") {
1,278✔
144
    return Filter::create<MuSurfaceFilter>(id);
27✔
145
  } else if (type == "parentnuclide") {
1,251✔
146
    return Filter::create<ParentNuclideFilter>(id);
11✔
147
  } else if (type == "particle") {
1,240✔
148
    return Filter::create<ParticleFilter>(id);
410✔
149
  } else if (type == "particleout") {
830✔
150
    return Filter::create<ParticleOutFilter>(id);
32✔
151
  } else if (type == "polar") {
798✔
152
    return Filter::create<PolarFilter>(id);
27✔
153
  } else if (type == "surface") {
771✔
154
    return Filter::create<SurfaceFilter>(id);
11✔
155
  } else if (type == "spatiallegendre") {
760✔
156
    return Filter::create<SpatialLegendreFilter>(id);
376✔
157
  } else if (type == "sphericalharmonics") {
384✔
UNCOV
158
    return Filter::create<SphericalHarmonicsFilter>(id);
×
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,531✔
184

185
  // Make sure no other filter has same ID
8,486✔
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,531✔
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,531✔
195
      id = std::max(id, f->id_);
×
196
    }
×
197
    ++id;
198
  }
199

200
  // Update ID and entry in filter map
10,531✔
201
  id_ = id;
1,494✔
202
  model::filter_map[id] = index_;
7,480✔
203
}
5,986✔
204

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

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

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

223
  *id = model::tally_filters[index]->id();
20,958✔
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