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

openmc-dev / openmc / 13443476914

20 Feb 2025 07:49PM UTC coverage: 85.022% (+0.05%) from 84.969%
13443476914

Pull #3140

github

web-flow
Merge 8ddcb065a into 7638661fa
Pull Request #3140: Add Versioning Support from `version.txt`

8 of 8 new or added lines in 2 files covered. (100.0%)

1242 existing lines in 42 files now uncovered.

50634 of 59554 relevant lines covered (85.02%)

35434881.25 hits per line

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

85.96
/src/tallies/filter_material.cpp
1
#include "openmc/tallies/filter_material.h"
2

3
#include <cassert>
4

5
#include <fmt/core.h>
6

7
#include "openmc/capi.h"
8
#include "openmc/material.h"
9
#include "openmc/xml_interface.h"
10

11
namespace openmc {
12

13
void MaterialFilter::from_xml(pugi::xml_node node)
1,450✔
14
{
15
  // Get material IDs and convert to indices in the global materials vector
16
  auto mats = get_node_array<int32_t>(node, "bins");
1,450✔
17
  for (auto& m : mats) {
3,163✔
18
    auto search = model::material_map.find(m);
1,713✔
19
    if (search == model::material_map.end()) {
1,713✔
UNCOV
20
      throw std::runtime_error {fmt::format(
×
UNCOV
21
        "Could not find material {} specified on tally filter.", m)};
×
22
    }
23
    m = search->second;
1,713✔
24
  }
25

26
  this->set_materials(mats);
1,450✔
27
}
1,450✔
28

29
void MaterialFilter::set_materials(span<const int32_t> materials)
1,879✔
30
{
31
  // Clear existing materials
32
  materials_.clear();
1,879✔
33
  materials_.reserve(materials.size());
1,879✔
34
  map_.clear();
1,879✔
35

36
  // Update materials and mapping
37
  for (auto& index : materials) {
6,906✔
38
    assert(index >= 0);
4,275✔
39
    assert(index < model::materials.size());
4,275✔
40
    materials_.push_back(index);
5,027✔
41
    map_[index] = materials_.size() - 1;
5,027✔
42
  }
43

44
  n_bins_ = materials_.size();
1,879✔
45
}
1,879✔
46

47
void MaterialFilter::get_all_bins(
1,188,896,431✔
48
  const Particle& p, TallyEstimator estimator, FilterMatch& match) const
49
{
50
  auto search = map_.find(p.material());
1,188,896,431✔
51
  if (search != map_.end()) {
1,188,896,431✔
52
    match.bins_.push_back(search->second);
382,175,581✔
53
    match.weights_.push_back(1.0);
382,175,581✔
54
  }
55
}
1,188,896,431✔
56

57
void MaterialFilter::to_statepoint(hid_t filter_group) const
2,668✔
58
{
59
  Filter::to_statepoint(filter_group);
2,668✔
60
  vector<int32_t> material_ids;
2,668✔
61
  for (auto c : materials_)
23,830✔
62
    material_ids.push_back(model::materials[c]->id_);
21,162✔
63
  write_dataset(filter_group, "bins", material_ids);
2,668✔
64
}
2,668✔
65

66
std::string MaterialFilter::text_label(int bin) const
10,884✔
67
{
68
  return fmt::format("Material {}", model::materials[materials_[bin]]->id_);
21,768✔
69
}
70

71
//==============================================================================
72
// C-API functions
73
//==============================================================================
74

75
extern "C" int openmc_material_filter_get_bins(
348✔
76
  int32_t index, const int32_t** bins, size_t* n)
77
{
78
  // Make sure this is a valid index to an allocated filter.
79
  if (int err = verify_filter(index))
348✔
UNCOV
80
    return err;
×
81

82
  // Get a pointer to the filter and downcast.
83
  const auto& filt_base = model::tally_filters[index].get();
348✔
84
  auto* filt = dynamic_cast<MaterialFilter*>(filt_base);
348✔
85

86
  // Check the filter type.
87
  if (!filt) {
348✔
UNCOV
88
    set_errmsg("Tried to get material filter bins on a non-material filter.");
×
UNCOV
89
    return OPENMC_E_INVALID_TYPE;
×
90
  }
91

92
  // Output the bins.
93
  *bins = filt->materials().data();
348✔
94
  *n = filt->materials().size();
348✔
95
  return 0;
348✔
96
}
97

98
extern "C" int openmc_material_filter_set_bins(
429✔
99
  int32_t index, size_t n, const int32_t* bins)
100
{
101
  // Make sure this is a valid index to an allocated filter.
102
  if (int err = verify_filter(index))
429✔
UNCOV
103
    return err;
×
104

105
  // Get a pointer to the filter and downcast.
106
  const auto& filt_base = model::tally_filters[index].get();
429✔
107
  auto* filt = dynamic_cast<MaterialFilter*>(filt_base);
429✔
108

109
  // Check the filter type.
110
  if (!filt) {
429✔
UNCOV
111
    set_errmsg("Tried to set material filter bins on a non-material filter.");
×
UNCOV
112
    return OPENMC_E_INVALID_TYPE;
×
113
  }
114

115
  // Update the filter.
116
  filt->set_materials({bins, n});
429✔
117
  return 0;
429✔
118
}
119

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