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

openmc-dev / openmc / 13183515203

06 Feb 2025 04:36PM UTC coverage: 82.601% (-2.3%) from 84.867%
13183515203

Pull #3087

github

web-flow
Merge d68c72d5e into 6e0f156d3
Pull Request #3087: wheel building with scikit build core

107123 of 129687 relevant lines covered (82.6%)

12608333.34 hits per line

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

64.49
/src/tallies/filter.cpp
1
#include "openmc/tallies/filter.h"
2

3
#include <algorithm> // for max
4
#include <cstring>   // for strcpy
5
#include <string>
6

7
#include <fmt/core.h>
8

9
#include "openmc/capi.h"
10
#include "openmc/constants.h" // for MAX_LINE_LEN;
11
#include "openmc/error.h"
12
#include "openmc/tallies/filter_azimuthal.h"
13
#include "openmc/tallies/filter_cell.h"
14
#include "openmc/tallies/filter_cell_instance.h"
15
#include "openmc/tallies/filter_cellborn.h"
16
#include "openmc/tallies/filter_cellfrom.h"
17
#include "openmc/tallies/filter_collision.h"
18
#include "openmc/tallies/filter_delayedgroup.h"
19
#include "openmc/tallies/filter_distribcell.h"
20
#include "openmc/tallies/filter_energy.h"
21
#include "openmc/tallies/filter_energyfunc.h"
22
#include "openmc/tallies/filter_legendre.h"
23
#include "openmc/tallies/filter_material.h"
24
#include "openmc/tallies/filter_materialfrom.h"
25
#include "openmc/tallies/filter_mesh.h"
26
#include "openmc/tallies/filter_meshborn.h"
27
#include "openmc/tallies/filter_meshsurface.h"
28
#include "openmc/tallies/filter_mu.h"
29
#include "openmc/tallies/filter_musurface.h"
30
#include "openmc/tallies/filter_particle.h"
31
#include "openmc/tallies/filter_polar.h"
32
#include "openmc/tallies/filter_sph_harm.h"
33
#include "openmc/tallies/filter_sptl_legendre.h"
34
#include "openmc/tallies/filter_surface.h"
35
#include "openmc/tallies/filter_time.h"
36
#include "openmc/tallies/filter_universe.h"
37
#include "openmc/tallies/filter_zernike.h"
38
#include "openmc/xml_interface.h"
39

40
// explicit template instantiation definition
41
template class openmc::vector<openmc::FilterMatch>;
42

43
namespace openmc {
44

45
//==============================================================================
46
// Global variables
47
//==============================================================================
48

49
namespace model {
50
std::unordered_map<int, int> filter_map;
51
vector<unique_ptr<Filter>> tally_filters;
52
} // namespace model
53

54
//==============================================================================
55
// Non-member functions
56
//==============================================================================
57

58
extern "C" size_t tally_filters_size()
×
59
{
60
  return model::tally_filters.size();
×
61
}
62

63
//==============================================================================
64
// Filter implementation
65
//==============================================================================
66

67
Filter::Filter()
6,496✔
68
{
69
  index_ = model::tally_filters.size(); // Avoids warning about narrowing
6,496✔
70
}
6,496✔
71

72
Filter::~Filter()
6,496✔
73
{
74
  model::filter_map.erase(id_);
6,496✔
75
}
6,496✔
76

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

85
  // Convert filter type to lower case
6,307✔
86
  std::string s;
87
  if (check_for_node(node, "type")) {
88
    s = get_node_value(node, "type", true);
6,307✔
89
  }
×
90

91
  // Allocate according to the filter type
6,307✔
92
  auto f = Filter::create(s, filter_id);
93

94
  // Read filter data from XML
6,307✔
95
  f->from_xml(node);
6,307✔
96
  return f;
6,307✔
97
}
98

99
Filter* Filter::create(const std::string& type, int32_t id)
100
{
6,307✔
101
  if (type == "azimuthal") {
102
    return Filter::create<AzimuthalFilter>(id);
103
  } else if (type == "cell") {
6,307✔
104
    return Filter::create<CellFilter>(id);
6,307✔
105
  } else if (type == "cellborn") {
6,307✔
106
    return Filter::create<CellBornFilter>(id);
107
  } else if (type == "cellfrom") {
6,479✔
108
    return Filter::create<CellFromFilter>(id);
109
  } else if (type == "cellinstance") {
6,479✔
110
    return Filter::create<CellInstanceFilter>(id);
34✔
111
  } else if (type == "distribcell") {
6,445✔
112
    return Filter::create<DistribcellFilter>(id);
649✔
113
  } else if (type == "delayedgroup") {
5,796✔
114
    return Filter::create<DelayedGroupFilter>(id);
17✔
115
  } else if (type == "energyfunction") {
5,779✔
116
    return Filter::create<EnergyFunctionFilter>(id);
86✔
117
  } else if (type == "energy") {
5,693✔
118
    return Filter::create<EnergyFilter>(id);
36✔
119
  } else if (type == "collision") {
5,657✔
120
    return Filter::create<CollisionFilter>(id);
165✔
121
  } else if (type == "energyout") {
5,492✔
122
    return Filter::create<EnergyoutFilter>(id);
102✔
123
  } else if (type == "legendre") {
5,390✔
124
    return Filter::create<LegendreFilter>(id);
136✔
125
  } else if (type == "material") {
5,254✔
126
    return Filter::create<MaterialFilter>(id);
867✔
127
  } else if (type == "materialfrom") {
4,387✔
128
    return Filter::create<MaterialFromFilter>(id);
17✔
129
  } else if (type == "mesh") {
4,370✔
130
    return Filter::create<MeshFilter>(id);
342✔
131
  } else if (type == "meshborn") {
4,028✔
132
    return Filter::create<MeshBornFilter>(id);
335✔
133
  } else if (type == "meshsurface") {
3,693✔
134
    return Filter::create<MeshSurfaceFilter>(id);
1,313✔
135
  } else if (type == "mu") {
2,380✔
136
    return Filter::create<MuFilter>(id);
17✔
137
  } else if (type == "musurface") {
2,363✔
138
    return Filter::create<MuSurfaceFilter>(id);
1,553✔
139
  } else if (type == "particle") {
810✔
140
    return Filter::create<ParticleFilter>(id);
29✔
141
  } else if (type == "polar") {
781✔
142
    return Filter::create<PolarFilter>(id);
170✔
143
  } else if (type == "surface") {
611✔
144
    return Filter::create<SurfaceFilter>(id);
34✔
145
  } else if (type == "spatiallegendre") {
577✔
146
    return Filter::create<SpatialLegendreFilter>(id);
29✔
147
  } else if (type == "sphericalharmonics") {
548✔
148
    return Filter::create<SphericalHarmonicsFilter>(id);
215✔
149
  } else if (type == "time") {
333✔
150
    return Filter::create<TimeFilter>(id);
35✔
151
  } else if (type == "universe") {
298✔
152
    return Filter::create<UniverseFilter>(id);
139✔
153
  } else if (type == "zernike") {
159✔
154
    return Filter::create<ZernikeFilter>(id);
12✔
155
  } else if (type == "zernikeradial") {
147✔
156
    return Filter::create<ZernikeRadialFilter>(id);
41✔
157
  } else {
106✔
158
    throw std::runtime_error {fmt::format("Unknown filter type: {}", type)};
77✔
159
  }
29✔
160
  return nullptr;
17✔
161
}
12✔
162

12✔
163
void Filter::set_id(int32_t id)
×
164
{
×
165
  Expects(id >= 0 || id == C_NONE);
166

×
167
  // Clear entry in filter map if an ID was already assigned before
168
  if (id_ != C_NONE) {
169
    model::filter_map.erase(id_);
170
    id_ = C_NONE;
171
  }
6,496✔
172

173
  // Make sure no other filter has same ID
6,496✔
174
  if (model::filter_map.find(id) != model::filter_map.end()) {
175
    throw std::runtime_error {
176
      "Two filters have the same ID: " + std::to_string(id)};
6,496✔
177
  }
×
178

×
179
  // If no ID specified, auto-assign next ID in sequence
180
  if (id == C_NONE) {
181
    id = 0;
182
    for (const auto& f : model::tally_filters) {
6,496✔
183
      id = std::max(id, f->id_);
×
184
    }
×
185
    ++id;
186
  }
187

188
  // Update ID and entry in filter map
6,496✔
189
  id_ = id;
189✔
190
  model::filter_map[id] = index_;
799✔
191
}
610✔
192

193
//==============================================================================
189✔
194
// C API functions
195
//==============================================================================
196

197
int verify_filter(int32_t index)
6,496✔
198
{
6,496✔
199
  if (index < 0 || index >= model::tally_filters.size()) {
6,496✔
200
    set_errmsg("Filter index is out of bounds.");
201
    return OPENMC_E_OUT_OF_BOUNDS;
202
  }
203
  return 0;
204
}
205

94✔
206
extern "C" int openmc_filter_get_id(int32_t index, int32_t* id)
207
{
94✔
208
  if (int err = verify_filter(index))
×
209
    return err;
×
210

211
  *id = model::tally_filters[index]->id();
94✔
212
  return 0;
213
}
214

×
215
extern "C" int openmc_filter_set_id(int32_t index, int32_t id)
216
{
×
217
  if (int err = verify_filter(index))
×
218
    return err;
219

×
220
  model::tally_filters[index]->set_id(id);
×
221
  return 0;
222
}
223

×
224
extern "C" int openmc_filter_get_type(int32_t index, char* type)
225
{
×
226
  if (int err = verify_filter(index))
×
227
    return err;
228

×
229
  std::strcpy(type, model::tally_filters[index]->type_str().c_str());
×
230
  return 0;
231
}
232

×
233
extern "C" int openmc_filter_get_num_bins(int32_t index, int* n_bins)
234
{
×
235
  if (int err = verify_filter(index))
×
236
    return err;
237

×
238
  *n_bins = model::tally_filters[index]->n_bins();
×
239
  return 0;
240
}
241

×
242
extern "C" int openmc_get_filter_index(int32_t id, int32_t* index)
243
{
×
244
  auto it = model::filter_map.find(id);
×
245
  if (it == model::filter_map.end()) {
246
    set_errmsg("No filter exists with ID=" + std::to_string(id) + ".");
×
247
    return OPENMC_E_INVALID_ID;
×
248
  }
249

250
  *index = it->second;
×
251
  return 0;
252
}
×
253

×
254
extern "C" void openmc_get_filter_next_id(int32_t* id)
×
255
{
×
256
  int32_t largest_filter_id = 0;
257
  for (const auto& t : model::tally_filters) {
258
    largest_filter_id = std::max(largest_filter_id, t->id());
×
259
  }
×
260
  *id = largest_filter_id + 1;
261
}
262

×
263
extern "C" int openmc_new_filter(const char* type, int32_t* index)
264
{
×
265
  *index = model::tally_filters.size();
×
266
  Filter::create(type);
×
267
  return 0;
268
}
×
269

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