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

openmc-dev / openmc / 17361657497

31 Aug 2025 07:54PM UTC coverage: 84.729%. First build
17361657497

Pull #3550

github

web-flow
Merge 98ca32e81 into 00edc7769
Pull Request #3550: [Point Detector] Add distribution get_pdf functionality

7 of 356 new or added lines in 10 files covered. (1.97%)

52947 of 62490 relevant lines covered (84.73%)

38803853.85 hits per line

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

65.56
/src/reaction_product.cpp
1
#include "openmc/reaction_product.h"
2

3
#include <string> // for string
4

5
#include <fmt/core.h>
6

7
#include "openmc/endf.h"
8
#include "openmc/error.h"
9
#include "openmc/hdf5_interface.h"
10
#include "openmc/memory.h"
11
#include "openmc/particle.h"
12
#include "openmc/random_lcg.h"
13
#include "openmc/secondary_correlated.h"
14
#include "openmc/secondary_kalbach.h"
15
#include "openmc/secondary_nbody.h"
16
#include "openmc/secondary_thermal.h"
17
#include "openmc/secondary_uncorrelated.h"
18
#include "openmc/tallies/tally_scoring.h"
19

20
namespace openmc {
21

22
//==============================================================================
23
// ReactionProduct implementation
24
//==============================================================================
25

26
ReactionProduct::ReactionProduct(hid_t group)
3,444,588✔
27
{
28
  // Read particle type
29
  std::string temp;
3,444,588✔
30
  read_attribute(group, "particle", temp);
3,444,588✔
31
  particle_ = str_to_particle_type(temp);
3,444,588✔
32

33
  // Read emission mode and decay rate
34
  read_attribute(group, "emission_mode", temp);
3,444,588✔
35
  if (temp == "prompt") {
3,444,588✔
36
    emission_mode_ = EmissionMode::prompt;
3,366,432✔
37
  } else if (temp == "delayed") {
78,156✔
38
    emission_mode_ = EmissionMode::delayed;
78,156✔
39
  } else if (temp == "total") {
×
40
    emission_mode_ = EmissionMode::total;
×
41
  }
42

43
  // Read decay rate for delayed emission
44
  if (emission_mode_ == EmissionMode::delayed) {
3,444,588✔
45
    if (attribute_exists(group, "decay_rate")) {
78,156✔
46
      read_attribute(group, "decay_rate", decay_rate_);
78,156✔
47
    } else if (particle_ == ParticleType::neutron) {
×
48
      warning(fmt::format("Decay rate doesn't exist for delayed neutron "
×
49
                          "emission ({}).",
50
        object_name(group)));
×
51
    }
52
  }
53

54
  // Read secondary particle yield
55
  yield_ = read_function(group, "yield");
3,444,588✔
56

57
  int n;
58
  read_attribute(group, "n_distribution", n);
3,444,588✔
59

60
  for (int i = 0; i < n; ++i) {
6,890,226✔
61
    std::string s {"distribution_"};
3,445,638✔
62
    s.append(std::to_string(i));
3,445,638✔
63
    hid_t dgroup = open_group(group, s.c_str());
3,445,638✔
64

65
    // Read applicability
66
    if (n > 1) {
3,445,638✔
67
      hid_t app = open_dataset(dgroup, "applicability");
2,004✔
68
      applicability_.emplace_back(app);
2,004✔
69
      close_dataset(app);
2,004✔
70
    }
71

72
    // Determine distribution type and read data
73
    read_attribute(dgroup, "type", temp);
3,445,638✔
74
    if (temp == "uncorrelated") {
3,445,638✔
75
      distribution_.push_back(make_unique<UncorrelatedAngleEnergy>(dgroup));
3,327,349✔
76
    } else if (temp == "correlated") {
118,289✔
77
      distribution_.push_back(make_unique<CorrelatedAngleEnergy>(dgroup));
49,750✔
78
    } else if (temp == "nbody") {
68,539✔
79
      distribution_.push_back(make_unique<NBodyPhaseSpace>(dgroup));
767✔
80
    } else if (temp == "kalbach-mann") {
67,772✔
81
      distribution_.push_back(make_unique<KalbachMann>(dgroup));
67,772✔
82
    }
83

84
    close_group(dgroup);
3,445,638✔
85
  }
3,445,638✔
86
}
3,444,588✔
87

88
ReactionProduct::ReactionProduct(const ChainNuclide::Product& product)
132✔
89
{
90
  particle_ = ParticleType::photon;
132✔
91
  emission_mode_ = EmissionMode::delayed;
132✔
92

93
  // Get chain nuclide object for radionuclide
94
  parent_nuclide_ = data::chain_nuclide_map.at(product.name);
132✔
95
  const auto& chain_nuc = data::chain_nuclides[parent_nuclide_].get();
132✔
96

97
  // Determine decay constant in [s^-1]
98
  decay_rate_ = chain_nuc->decay_constant();
132✔
99

100
  // Determine number of photons per decay and set yield
101
  double photon_per_sec = chain_nuc->photon_energy()->integral();
132✔
102
  double photon_per_decay = photon_per_sec / decay_rate_;
132✔
103
  vector<double> coef = {product.branching_ratio * photon_per_decay};
132✔
104
  yield_ = make_unique<Polynomial>(coef);
132✔
105

106
  // Set decay photon angle-energy distribution
107
  distribution_.push_back(
132✔
108
    make_unique<DecayPhotonAngleEnergy>(chain_nuc->photon_energy()));
264✔
109
}
132✔
110

111
void ReactionProduct::sample(
41,802,550✔
112
  double E_in, double& E_out, double& mu, uint64_t* seed) const
113
{
114
  auto n = applicability_.size();
41,802,550✔
115
  if (n > 1) {
41,802,550✔
116
    double prob = 0.0;
1,078✔
117
    double c = prn(seed);
1,078✔
118
    for (int i = 0; i < n; ++i) {
1,881✔
119
      // Determine probability that i-th energy distribution is sampled
120
      prob += applicability_[i](E_in);
1,881✔
121

122
      // If i-th distribution is sampled, sample energy from the distribution
123
      if (c <= prob) {
1,881✔
124
        distribution_[i]->sample(E_in, E_out, mu, seed);
1,078✔
125
        break;
1,078✔
126
      }
127
    }
128
  } else {
129
    // If only one distribution is present, go ahead and sample it
130
    distribution_[0]->sample(E_in, E_out, mu, seed);
41,801,472✔
131
  }
132
}
41,802,550✔
133

NEW
134
void ReactionProduct::get_pdf(int i_tally, double E_in, double& E_out,
×
135
  uint64_t* seed, Particle& p, std::vector<double>& mu_cm,
136
  std::vector<double>& Js, std::vector<Particle>& ghost_particles,
137
  std::vector<double>& pdfs_lab) const
138
{
139
  /* function to get detector position from user - Future implementation
140
   double det_pos[4];
141
   get_det_pos(det_pos, i_tally);
142
  */
NEW
143
  double det_pos[4] = {0.0, 0.0, 0.0, 1.0}; // Placeholder for detector position
×
144

145
  int distribution_index;
NEW
146
  auto n = applicability_.size();
×
NEW
147
  if (n > 1) {
×
NEW
148
    double prob = 0.0;
×
NEW
149
    double c = prn(seed);
×
NEW
150
    for (int i = 0; i < n; ++i) {
×
151
      // Determine probability that i-th energy distribution is sampled
NEW
152
      prob += applicability_[i](E_in);
×
153

154
      // If i-th distribution is sampled, sample energy from the distribution
NEW
155
      if (c <= prob) {
×
156
        // distribution_[i]->sample(E_in, E_out, mu, seed);
NEW
157
        distribution_index = i;
×
NEW
158
        break;
×
159
      }
160
    }
161
  } else {
162
    // If only one distribution is present, go ahead and sample it
163
    // distribution_[0]->sample(E_in, E_out, mu, seed);
NEW
164
    distribution_index = 0;
×
165
  }
166
  // now extract pdf
167

168
  AngleEnergy* angleEnergyPtr = distribution_[distribution_index].get();
×
169

170
  if (CorrelatedAngleEnergy* correlatedAE =
×
171
        dynamic_cast<CorrelatedAngleEnergy*>(angleEnergyPtr)) {
×
172

173
    (*correlatedAE)
174
      .get_pdf(
×
175
        det_pos, E_in, E_out, seed, p, mu_cm, Js, ghost_particles, pdfs_lab);
176
    // Handle CorrelatedAngleEnergy
177
  } else if (KalbachMann* kalbachMann =
×
178
               dynamic_cast<KalbachMann*>(angleEnergyPtr)) {
×
179

180
    (*kalbachMann)
181
      .get_pdf(
×
182
        det_pos, E_in, E_out, seed, p, mu_cm, Js, ghost_particles, pdfs_lab);
183

184
    // Handle KalbachMann
185
  } else if (NBodyPhaseSpace* nBodyPS =
×
186
               dynamic_cast<NBodyPhaseSpace*>(angleEnergyPtr)) {
×
187

188
    (*nBodyPS).get_pdf(
×
189
      det_pos, E_in, E_out, seed, p, mu_cm, Js, ghost_particles, pdfs_lab);
190
    // Handle NBodyPhaseSpace
191
  } else if (UncorrelatedAngleEnergy* uncorrelatedAE =
×
192
               dynamic_cast<UncorrelatedAngleEnergy*>(angleEnergyPtr)) {
×
193

194
    (*uncorrelatedAE)
195
      .get_pdf(
×
196
        det_pos, E_in, E_out, seed, p, mu_cm, Js, ghost_particles, pdfs_lab);
197
    // Handle UncorrelatedAngleEnergy
198
  } else {
199
    std::cout << "Unknown derived type." << std::endl;
×
200
  }
201
}
202

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

© 2025 Coveralls, Inc