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

openmc-dev / openmc / 12776996362

14 Jan 2025 09:49PM UTC coverage: 84.938% (+0.2%) from 84.729%
12776996362

Pull #3133

github

web-flow
Merge 0495246d9 into 549cc0973
Pull Request #3133: Kinetics parameters using Iterated Fission Probability

318 of 330 new or added lines in 10 files covered. (96.36%)

1658 existing lines in 66 files now uncovered.

50402 of 59340 relevant lines covered (84.94%)

33987813.96 hits per line

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

85.51
/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()
1,917✔
59
{
60
  return model::tally_filters.size();
1,917✔
61
}
62

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

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

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

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

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

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

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

99
Filter* Filter::create(const std::string& type, int32_t id)
100
{
7,304✔
101
  if (type == "azimuthal") {
102
    return Filter::create<AzimuthalFilter>(id);
103
  } else if (type == "cell") {
7,304✔
104
    return Filter::create<CellFilter>(id);
7,304✔
105
  } else if (type == "cellborn") {
7,304✔
106
    return Filter::create<CellBornFilter>(id);
107
  } else if (type == "cellfrom") {
9,318✔
108
    return Filter::create<CellFromFilter>(id);
109
  } else if (type == "cellinstance") {
9,318✔
110
    return Filter::create<CellInstanceFilter>(id);
34✔
111
  } else if (type == "distribcell") {
9,284✔
112
    return Filter::create<DistribcellFilter>(id);
686✔
113
  } else if (type == "delayedgroup") {
8,598✔
114
    return Filter::create<DelayedGroupFilter>(id);
17✔
115
  } else if (type == "energyfunction") {
8,581✔
116
    return Filter::create<EnergyFunctionFilter>(id);
86✔
117
  } else if (type == "energy") {
8,495✔
118
    return Filter::create<EnergyFilter>(id);
36✔
119
  } else if (type == "collision") {
8,459✔
120
    return Filter::create<CollisionFilter>(id);
189✔
121
  } else if (type == "energyout") {
8,270✔
122
    return Filter::create<EnergyoutFilter>(id);
102✔
123
  } else if (type == "legendre") {
8,168✔
124
    return Filter::create<LegendreFilter>(id);
220✔
125
  } else if (type == "material") {
7,948✔
126
    return Filter::create<MaterialFilter>(id);
1,164✔
127
  } else if (type == "materialfrom") {
6,784✔
128
    return Filter::create<MaterialFromFilter>(id);
17✔
129
  } else if (type == "mesh") {
6,767✔
130
    return Filter::create<MeshFilter>(id);
392✔
131
  } else if (type == "meshborn") {
6,375✔
132
    return Filter::create<MeshBornFilter>(id);
710✔
133
  } else if (type == "meshsurface") {
5,665✔
134
    return Filter::create<MeshSurfaceFilter>(id);
1,810✔
135
  } else if (type == "mu") {
3,855✔
136
    return Filter::create<MuFilter>(id);
17✔
137
  } else if (type == "musurface") {
3,838✔
138
    return Filter::create<MuSurfaceFilter>(id);
2,478✔
139
  } else if (type == "particle") {
1,360✔
140
    return Filter::create<ParticleFilter>(id);
29✔
141
  } else if (type == "polar") {
1,331✔
142
    return Filter::create<PolarFilter>(id);
593✔
143
  } else if (type == "surface") {
738✔
144
    return Filter::create<SurfaceFilter>(id);
34✔
145
  } else if (type == "spatiallegendre") {
704✔
146
    return Filter::create<SpatialLegendreFilter>(id);
29✔
147
  } else if (type == "sphericalharmonics") {
675✔
148
    return Filter::create<SphericalHarmonicsFilter>(id);
306✔
149
  } else if (type == "time") {
369✔
150
    return Filter::create<TimeFilter>(id);
35✔
151
  } else if (type == "universe") {
334✔
152
    return Filter::create<UniverseFilter>(id);
139✔
153
  } else if (type == "zernike") {
195✔
154
    return Filter::create<ZernikeFilter>(id);
12✔
155
  } else if (type == "zernikeradial") {
183✔
156
    return Filter::create<ZernikeRadialFilter>(id);
41✔
157
  } else {
142✔
158
    throw std::runtime_error {fmt::format("Unknown filter type: {}", type)};
77✔
159
  }
65✔
160
  return nullptr;
17✔
161
}
48✔
162

48✔
163
void Filter::set_id(int32_t id)
×
UNCOV
164
{
×
165
  Expects(id >= 0 || id == C_NONE);
UNCOV
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
  }
11,252✔
172

173
  // Make sure no other filter has same ID
11,252✔
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)};
11,252✔
177
  }
1,917✔
178

1,917✔
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) {
11,252✔
UNCOV
183
      id = std::max(id, f->id_);
×
UNCOV
184
    }
×
185
    ++id;
186
  }
187

188
  // Update ID and entry in filter map
11,252✔
189
  id_ = id;
2,031✔
190
  model::filter_map[id] = index_;
9,439✔
191
}
7,408✔
192

193
//==============================================================================
2,031✔
194
// C API functions
195
//==============================================================================
196

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

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

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

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

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

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

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

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

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

144✔
242
extern "C" int openmc_get_filter_index(int32_t id, int32_t* index)
243
{
144✔
UNCOV
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) + ".");
144✔
247
    return OPENMC_E_INVALID_ID;
144✔
248
  }
249

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

12✔
UNCOV
254
extern "C" void openmc_get_filter_next_id(int32_t* id)
×
UNCOV
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());
12✔
259
  }
12✔
260
  *id = largest_filter_id + 1;
261
}
262

×
263
extern "C" int openmc_new_filter(const char* type, int32_t* index)
UNCOV
264
{
×
265
  *index = model::tally_filters.size();
×
UNCOV
266
  Filter::create(type);
×
267
  return 0;
UNCOV
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