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

openmc-dev / openmc / 15648508499

14 Jun 2025 04:41AM UTC coverage: 85.162% (+0.04%) from 85.126%
15648508499

Pull #3346

github

web-flow
Merge 95507c5a3 into b11eb0265
Pull Request #3346: Allow specifying number of equiprobable angles for thermal scattering data generation

52373 of 61498 relevant lines covered (85.16%)

36636961.06 hits per line

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

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

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

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

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

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

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

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

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

103
Filter* Filter::create(const std::string& type, int32_t id)
104
{
7,726✔
105
  if (type == "azimuthal") {
106
    return Filter::create<AzimuthalFilter>(id);
107
  } else if (type == "cell") {
7,726✔
108
    return Filter::create<CellFilter>(id);
7,726✔
109
  } else if (type == "cellborn") {
7,726✔
110
    return Filter::create<CellBornFilter>(id);
111
  } else if (type == "cellfrom") {
9,142✔
112
    return Filter::create<CellFromFilter>(id);
113
  } else if (type == "cellinstance") {
9,142✔
114
    return Filter::create<CellInstanceFilter>(id);
32✔
115
  } else if (type == "distribcell") {
9,110✔
116
    return Filter::create<DistribcellFilter>(id);
737✔
117
  } else if (type == "delayedgroup") {
8,373✔
118
    return Filter::create<DelayedGroupFilter>(id);
16✔
119
  } else if (type == "energyfunction") {
8,357✔
120
    return Filter::create<EnergyFunctionFilter>(id);
96✔
121
  } else if (type == "energy") {
8,261✔
122
    return Filter::create<EnergyFilter>(id);
34✔
123
  } else if (type == "collision") {
8,227✔
124
    return Filter::create<CollisionFilter>(id);
177✔
125
  } else if (type == "energyout") {
8,050✔
126
    return Filter::create<EnergyoutFilter>(id);
96✔
127
  } else if (type == "legendre") {
7,954✔
128
    return Filter::create<LegendreFilter>(id);
205✔
129
  } else if (type == "material") {
7,749✔
130
    return Filter::create<MaterialFilter>(id);
1,306✔
131
  } else if (type == "materialfrom") {
6,443✔
132
    return Filter::create<MaterialFromFilter>(id);
16✔
133
  } else if (type == "mesh") {
6,427✔
134
    return Filter::create<MeshFilter>(id);
411✔
135
  } else if (type == "meshborn") {
6,016✔
136
    return Filter::create<MeshBornFilter>(id);
572✔
137
  } else if (type == "meshmaterial") {
5,444✔
138
    return Filter::create<MeshMaterialFilter>(id);
1,981✔
139
  } else if (type == "meshsurface") {
3,463✔
140
    return Filter::create<MeshSurfaceFilter>(id);
16✔
141
  } else if (type == "mu") {
3,447✔
142
    return Filter::create<MuFilter>(id);
2,185✔
143
  } else if (type == "musurface") {
1,262✔
144
    return Filter::create<MuSurfaceFilter>(id);
27✔
145
  } else if (type == "parentnuclide") {
1,235✔
146
    return Filter::create<ParentNuclideFilter>(id);
11✔
147
  } else if (type == "particle") {
1,224✔
148
    return Filter::create<ParticleFilter>(id);
395✔
149
  } else if (type == "polar") {
829✔
150
    return Filter::create<PolarFilter>(id);
32✔
151
  } else if (type == "surface") {
797✔
152
    return Filter::create<SurfaceFilter>(id);
27✔
153
  } else if (type == "spatiallegendre") {
770✔
154
    return Filter::create<SpatialLegendreFilter>(id);
11✔
155
  } else if (type == "sphericalharmonics") {
759✔
156
    return Filter::create<SphericalHarmonicsFilter>(id);
375✔
157
  } else if (type == "time") {
384✔
158
    return Filter::create<TimeFilter>(id);
48✔
159
  } else if (type == "universe") {
336✔
160
    return Filter::create<UniverseFilter>(id);
145✔
161
  } else if (type == "weight") {
191✔
162
    return Filter::create<WeightFilter>(id);
11✔
163
  } else if (type == "zernike") {
180✔
164
    return Filter::create<ZernikeFilter>(id);
38✔
165
  } else if (type == "zernikeradial") {
142✔
166
    return Filter::create<ZernikeRadialFilter>(id);
71✔
167
  } else {
71✔
168
    throw std::runtime_error {fmt::format("Unknown filter type: {}", type)};
16✔
169
  }
55✔
170
  return nullptr;
11✔
171
}
44✔
172

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

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

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

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

198
  // Update ID and entry in filter map
10,319✔
199
  id_ = id;
1,432✔
200
  model::filter_map[id] = index_;
7,245✔
201
}
5,813✔
202

203
//==============================================================================
1,432✔
204
// C API functions
205
//==============================================================================
206

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

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

221
  *id = model::tally_filters[index]->id();
20,440✔
222
  return 0;
223
}
224

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

10,507✔
230
  model::tally_filters[index]->set_id(id);
10,507✔
231
  return 0;
232
}
233

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

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

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

5,740✔
248
  *n_bins = model::tally_filters[index]->n_bins();
5,740✔
249
  return 0;
250
}
251

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

260
  *index = it->second;
11✔
261
  return 0;
262
}
11✔
263

11✔
264
extern "C" void openmc_get_filter_next_id(int32_t* id)
×
265
{
×
266
  int32_t largest_filter_id = 0;
267
  for (const auto& t : model::tally_filters) {
268
    largest_filter_id = std::max(largest_filter_id, t->id());
11✔
269
  }
11✔
270
  *id = largest_filter_id + 1;
271
}
272

×
273
extern "C" int openmc_new_filter(const char* type, int32_t* index)
274
{
×
275
  *index = model::tally_filters.size();
×
276
  Filter::create(type);
×
277
  return 0;
278
}
×
279

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