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

openmc-dev / openmc / 14971664494

12 May 2025 12:01PM UTC coverage: 85.218% (+0.09%) from 85.126%
14971664494

Pull #3088

github

web-flow
Merge 0901c645e into f615441f0
Pull Request #3088: External transfer rates source term

153 of 163 new or added lines in 5 files covered. (93.87%)

1782 existing lines in 54 files now uncovered.

52333 of 61411 relevant lines covered (85.22%)

37392080.21 hits per line

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

85.92
/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_meshsurface.h"
29
#include "openmc/tallies/filter_mu.h"
30
#include "openmc/tallies/filter_musurface.h"
31
#include "openmc/tallies/filter_parent_nuclide.h"
32
#include "openmc/tallies/filter_particle.h"
33
#include "openmc/tallies/filter_polar.h"
34
#include "openmc/tallies/filter_sph_harm.h"
35
#include "openmc/tallies/filter_sptl_legendre.h"
36
#include "openmc/tallies/filter_surface.h"
37
#include "openmc/tallies/filter_time.h"
38
#include "openmc/tallies/filter_universe.h"
39
#include "openmc/tallies/filter_weight.h"
40
#include "openmc/tallies/filter_zernike.h"
41
#include "openmc/xml_interface.h"
42

43
// explicit template instantiation definition
44
template class openmc::vector<openmc::FilterMatch>;
45

46
namespace openmc {
47

48
//==============================================================================
49
// Global variables
50
//==============================================================================
51

52
namespace model {
53
std::unordered_map<int, int> filter_map;
54
vector<unique_ptr<Filter>> tally_filters;
55
} // namespace model
56

57
//==============================================================================
58
// Non-member functions
59
//==============================================================================
60

61
extern "C" size_t tally_filters_size()
1,338✔
62
{
63
  return model::tally_filters.size();
1,338✔
64
}
65

66
//==============================================================================
67
// Filter implementation
68
//==============================================================================
69

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

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

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

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

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

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

102
Filter* Filter::create(const std::string& type, int32_t id)
103
{
7,716✔
104
  if (type == "azimuthal") {
105
    return Filter::create<AzimuthalFilter>(id);
106
  } else if (type == "cell") {
7,716✔
107
    return Filter::create<CellFilter>(id);
7,716✔
108
  } else if (type == "cellborn") {
7,716✔
109
    return Filter::create<CellBornFilter>(id);
110
  } else if (type == "cellfrom") {
9,309✔
111
    return Filter::create<CellFromFilter>(id);
112
  } else if (type == "cellinstance") {
9,309✔
113
    return Filter::create<CellInstanceFilter>(id);
32✔
114
  } else if (type == "distribcell") {
9,277✔
115
    return Filter::create<DistribcellFilter>(id);
737✔
116
  } else if (type == "delayedgroup") {
8,540✔
117
    return Filter::create<DelayedGroupFilter>(id);
16✔
118
  } else if (type == "energyfunction") {
8,524✔
119
    return Filter::create<EnergyFunctionFilter>(id);
96✔
120
  } else if (type == "energy") {
8,428✔
121
    return Filter::create<EnergyFilter>(id);
34✔
122
  } else if (type == "collision") {
8,394✔
123
    return Filter::create<CollisionFilter>(id);
177✔
124
  } else if (type == "energyout") {
8,217✔
125
    return Filter::create<EnergyoutFilter>(id);
96✔
126
  } else if (type == "legendre") {
8,121✔
127
    return Filter::create<LegendreFilter>(id);
205✔
128
  } else if (type == "material") {
7,916✔
129
    return Filter::create<MaterialFilter>(id);
1,312✔
130
  } else if (type == "materialfrom") {
6,604✔
131
    return Filter::create<MaterialFromFilter>(id);
16✔
132
  } else if (type == "mesh") {
6,588✔
133
    return Filter::create<MeshFilter>(id);
417✔
134
  } else if (type == "meshborn") {
6,171✔
135
    return Filter::create<MeshBornFilter>(id);
617✔
136
  } else if (type == "meshsurface") {
5,554✔
137
    return Filter::create<MeshSurfaceFilter>(id);
2,011✔
138
  } else if (type == "mu") {
3,543✔
139
    return Filter::create<MuFilter>(id);
16✔
140
  } else if (type == "musurface") {
3,527✔
141
    return Filter::create<MuSurfaceFilter>(id);
2,253✔
142
  } else if (type == "parentnuclide") {
1,274✔
143
    return Filter::create<ParentNuclideFilter>(id);
27✔
144
  } else if (type == "particle") {
1,247✔
145
    return Filter::create<ParticleFilter>(id);
429✔
146
  } else if (type == "polar") {
818✔
147
    return Filter::create<PolarFilter>(id);
32✔
148
  } else if (type == "surface") {
786✔
149
    return Filter::create<SurfaceFilter>(id);
27✔
150
  } else if (type == "spatiallegendre") {
759✔
151
    return Filter::create<SpatialLegendreFilter>(id);
11✔
152
  } else if (type == "sphericalharmonics") {
748✔
153
    return Filter::create<SphericalHarmonicsFilter>(id);
364✔
154
  } else if (type == "time") {
384✔
155
    return Filter::create<TimeFilter>(id);
48✔
156
  } else if (type == "universe") {
336✔
157
    return Filter::create<UniverseFilter>(id);
145✔
158
  } else if (type == "weight") {
191✔
159
    return Filter::create<WeightFilter>(id);
11✔
160
  } else if (type == "zernike") {
180✔
161
    return Filter::create<ZernikeFilter>(id);
38✔
162
  } else if (type == "zernikeradial") {
142✔
163
    return Filter::create<ZernikeRadialFilter>(id);
71✔
164
  } else {
71✔
165
    throw std::runtime_error {fmt::format("Unknown filter type: {}", type)};
16✔
166
  }
55✔
167
  return nullptr;
11✔
168
}
44✔
169

44✔
170
void Filter::set_id(int32_t id)
×
UNCOV
171
{
×
172
  assert(id >= 0 || id == C_NONE);
UNCOV
173

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

180
  // Make sure no other filter has same ID
8,774✔
181
  if (model::filter_map.find(id) != model::filter_map.end()) {
182
    throw std::runtime_error {
183
      "Two filters have the same ID: " + std::to_string(id)};
10,663✔
184
  }
1,338✔
185

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

195
  // Update ID and entry in filter map
10,663✔
196
  id_ = id;
1,609✔
197
  model::filter_map[id] = index_;
7,923✔
198
}
6,314✔
199

200
//==============================================================================
1,609✔
201
// C API functions
202
//==============================================================================
203

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

21,955✔
213
extern "C" int openmc_filter_get_id(int32_t index, int32_t* id)
214
{
21,955✔
UNCOV
215
  if (int err = verify_filter(index))
×
UNCOV
216
    return err;
×
217

218
  *id = model::tally_filters[index]->id();
21,955✔
219
  return 0;
220
}
221

11,321✔
222
extern "C" int openmc_filter_set_id(int32_t index, int32_t id)
223
{
11,321✔
UNCOV
224
  if (int err = verify_filter(index))
×
225
    return err;
226

11,321✔
227
  model::tally_filters[index]->set_id(id);
11,321✔
228
  return 0;
229
}
230

1,338✔
231
extern "C" int openmc_filter_get_type(int32_t index, char* type)
232
{
1,338✔
UNCOV
233
  if (int err = verify_filter(index))
×
234
    return err;
235

1,338✔
236
  std::strcpy(type, model::tally_filters[index]->type_str().c_str());
1,338✔
237
  return 0;
238
}
239

6,053✔
240
extern "C" int openmc_filter_get_num_bins(int32_t index, int* n_bins)
241
{
6,053✔
UNCOV
242
  if (int err = verify_filter(index))
×
243
    return err;
244

6,053✔
245
  *n_bins = model::tally_filters[index]->n_bins();
6,053✔
246
  return 0;
247
}
248

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

257
  *index = it->second;
11✔
258
  return 0;
259
}
11✔
260

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

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

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