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

openmc-dev / openmc / 14260484407

04 Apr 2025 07:41AM UTC coverage: 84.589% (-0.3%) from 84.851%
14260484407

Pull #3279

github

web-flow
Merge 33e03305e into 07f533461
Pull Request #3279: Hexagonal mesh model

2 of 332 new or added lines in 3 files covered. (0.6%)

2798 existing lines in 84 files now uncovered.

51799 of 61236 relevant lines covered (84.59%)

38650400.15 hits per line

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

85.71
/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_zernike.h"
40
#include "openmc/xml_interface.h"
41

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

45
namespace openmc {
46

47
//==============================================================================
48
// Global variables
49
//==============================================================================
50

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

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

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

65
//==============================================================================
66
// Filter implementation
67
//==============================================================================
68

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

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

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

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

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

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

101
Filter* Filter::create(const std::string& type, int32_t id)
102
{
7,664✔
103
  if (type == "azimuthal") {
104
    return Filter::create<AzimuthalFilter>(id);
105
  } else if (type == "cell") {
7,664✔
106
    return Filter::create<CellFilter>(id);
7,664✔
107
  } else if (type == "cellborn") {
7,664✔
108
    return Filter::create<CellBornFilter>(id);
109
  } else if (type == "cellfrom") {
9,104✔
110
    return Filter::create<CellFromFilter>(id);
111
  } else if (type == "cellinstance") {
9,104✔
112
    return Filter::create<CellInstanceFilter>(id);
32✔
113
  } else if (type == "distribcell") {
9,072✔
114
    return Filter::create<DistribcellFilter>(id);
737✔
115
  } else if (type == "delayedgroup") {
8,335✔
116
    return Filter::create<DelayedGroupFilter>(id);
16✔
117
  } else if (type == "energyfunction") {
8,319✔
118
    return Filter::create<EnergyFunctionFilter>(id);
96✔
119
  } else if (type == "energy") {
8,223✔
120
    return Filter::create<EnergyFilter>(id);
34✔
121
  } else if (type == "collision") {
8,189✔
122
    return Filter::create<CollisionFilter>(id);
177✔
123
  } else if (type == "energyout") {
8,012✔
124
    return Filter::create<EnergyoutFilter>(id);
96✔
125
  } else if (type == "legendre") {
7,916✔
126
    return Filter::create<LegendreFilter>(id);
205✔
127
  } else if (type == "material") {
7,711✔
128
    return Filter::create<MaterialFilter>(id);
1,308✔
129
  } else if (type == "materialfrom") {
6,403✔
130
    return Filter::create<MaterialFromFilter>(id);
16✔
131
  } else if (type == "mesh") {
6,387✔
132
    return Filter::create<MeshFilter>(id);
413✔
133
  } else if (type == "meshborn") {
5,974✔
134
    return Filter::create<MeshBornFilter>(id);
587✔
135
  } else if (type == "meshsurface") {
5,387✔
136
    return Filter::create<MeshSurfaceFilter>(id);
1,956✔
137
  } else if (type == "mu") {
3,431✔
138
    return Filter::create<MuFilter>(id);
16✔
139
  } else if (type == "musurface") {
3,415✔
140
    return Filter::create<MuSurfaceFilter>(id);
2,182✔
141
  } else if (type == "parentnuclide") {
1,233✔
142
    return Filter::create<ParentNuclideFilter>(id);
27✔
143
  } else if (type == "particle") {
1,206✔
144
    return Filter::create<ParticleFilter>(id);
399✔
145
  } else if (type == "polar") {
807✔
146
    return Filter::create<PolarFilter>(id);
32✔
147
  } else if (type == "surface") {
775✔
148
    return Filter::create<SurfaceFilter>(id);
27✔
149
  } else if (type == "spatiallegendre") {
748✔
150
    return Filter::create<SpatialLegendreFilter>(id);
11✔
151
  } else if (type == "sphericalharmonics") {
737✔
152
    return Filter::create<SphericalHarmonicsFilter>(id);
364✔
153
  } else if (type == "time") {
373✔
154
    return Filter::create<TimeFilter>(id);
48✔
155
  } else if (type == "universe") {
325✔
156
    return Filter::create<UniverseFilter>(id);
145✔
157
  } else if (type == "zernike") {
180✔
158
    return Filter::create<ZernikeFilter>(id);
11✔
159
  } else if (type == "zernikeradial") {
169✔
160
    return Filter::create<ZernikeRadialFilter>(id);
38✔
161
  } else {
131✔
162
    throw std::runtime_error {fmt::format("Unknown filter type: {}", type)};
71✔
163
  }
60✔
164
  return nullptr;
16✔
165
}
44✔
166

44✔
UNCOV
167
void Filter::set_id(int32_t id)
×
UNCOV
168
{
×
169
  assert(id >= 0 || id == C_NONE);
UNCOV
170

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

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

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

192
  // Update ID and entry in filter map
10,305✔
193
  id_ = id;
1,456✔
194
  model::filter_map[id] = index_;
7,401✔
195
}
5,945✔
196

197
//==============================================================================
1,456✔
198
// C API functions
199
//==============================================================================
200

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

20,818✔
210
extern "C" int openmc_filter_get_id(int32_t index, int32_t* id)
211
{
20,818✔
UNCOV
212
  if (int err = verify_filter(index))
×
UNCOV
213
    return err;
×
214

215
  *id = model::tally_filters[index]->id();
20,818✔
216
  return 0;
217
}
218

10,736✔
219
extern "C" int openmc_filter_set_id(int32_t index, int32_t id)
220
{
10,736✔
UNCOV
221
  if (int err = verify_filter(index))
×
222
    return err;
223

10,736✔
224
  model::tally_filters[index]->set_id(id);
10,736✔
225
  return 0;
226
}
227

1,185✔
228
extern "C" int openmc_filter_get_type(int32_t index, char* type)
229
{
1,185✔
UNCOV
230
  if (int err = verify_filter(index))
×
231
    return err;
232

1,185✔
233
  std::strcpy(type, model::tally_filters[index]->type_str().c_str());
1,185✔
234
  return 0;
235
}
236

5,837✔
237
extern "C" int openmc_filter_get_num_bins(int32_t index, int* n_bins)
238
{
5,837✔
UNCOV
239
  if (int err = verify_filter(index))
×
240
    return err;
241

5,837✔
242
  *n_bins = model::tally_filters[index]->n_bins();
5,837✔
243
  return 0;
244
}
245

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

254
  *index = it->second;
11✔
255
  return 0;
256
}
11✔
257

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

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

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