Skip to content

Commit 3f06a42

Browse files
authored
Refactor get_energy_index to prevent repetition (#3686)
1 parent a2fd6cc commit 3f06a42

File tree

6 files changed

+36
-55
lines changed

6 files changed

+36
-55
lines changed

include/openmc/math_functions.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <cstdlib>
1010

1111
#include "openmc/position.h"
12+
#include "openmc/search.h"
1213

1314
namespace openmc {
1415

@@ -200,5 +201,15 @@ std::complex<double> faddeeva(std::complex<double> z);
200201
//! \return Derivative of Faddeeva function evaluated at z
201202
std::complex<double> w_derivative(std::complex<double> z, int order);
202203

204+
//! Helper function to get index and interpolation function on an incident
205+
//! energy grid
206+
//!
207+
//! \param energies energy grid
208+
//! \param E incident energy
209+
//! \param i grid index
210+
//! \param f interpolation factor
211+
void get_energy_index(
212+
const vector<double>& energies, double E, int& i, double& f);
213+
203214
} // namespace openmc
204215
#endif // OPENMC_MATH_FUNCTIONS_H

src/distribution_angle.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "openmc/endf.h"
99
#include "openmc/hdf5_interface.h"
10+
#include "openmc/math_functions.h"
1011
#include "openmc/random_lcg.h"
1112
#include "openmc/search.h"
1213
#include "openmc/vector.h" // for vector
@@ -64,23 +65,10 @@ AngleDistribution::AngleDistribution(hid_t group)
6465

6566
double AngleDistribution::sample(double E, uint64_t* seed) const
6667
{
67-
// Determine number of incoming energies
68-
auto n = energy_.size();
69-
70-
// Find energy bin and calculate interpolation factor -- if the energy is
71-
// outside the range of the tabulated energies, choose the first or last bins
68+
// Find energy bin and calculate interpolation factor
7269
int i;
7370
double r;
74-
if (E < energy_[0]) {
75-
i = 0;
76-
r = 0.0;
77-
} else if (E > energy_[n - 1]) {
78-
i = n - 2;
79-
r = 1.0;
80-
} else {
81-
i = lower_bound_index(energy_.begin(), energy_.end(), E);
82-
r = (E - energy_[i]) / (energy_[i + 1] - energy_[i]);
83-
}
71+
get_energy_index(energy_, E, i, r);
8472

8573
// Sample between the ith and (i+1)th bin
8674
if (r > prn(seed))

src/math_functions.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,4 +919,19 @@ std::complex<double> w_derivative(std::complex<double> z, int order)
919919
}
920920
}
921921

922+
// Helper function to get index and interpolation function on an incident energy
923+
// grid
924+
void get_energy_index(
925+
const vector<double>& energies, double E, int& i, double& f)
926+
{
927+
// Get index and interpolation factor for linear-linear energy grid
928+
i = 0;
929+
f = 0.0;
930+
if (E >= energies.front()) {
931+
i = lower_bound_index(energies.begin(), energies.end(), E);
932+
if (i + 1 < energies.size())
933+
f = (E - energies[i]) / (energies[i + 1] - energies[i]);
934+
}
935+
}
936+
922937
} // namespace openmc

src/secondary_correlated.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "openmc/endf.h"
1212
#include "openmc/hdf5_interface.h"
13+
#include "openmc/math_functions.h"
1314
#include "openmc/random_lcg.h"
1415
#include "openmc/search.h"
1516

@@ -156,21 +157,10 @@ CorrelatedAngleEnergy::CorrelatedAngleEnergy(hid_t group)
156157
void CorrelatedAngleEnergy::sample(
157158
double E_in, double& E_out, double& mu, uint64_t* seed) const
158159
{
159-
// Find energy bin and calculate interpolation factor -- if the energy is
160-
// outside the range of the tabulated energies, choose the first or last bins
161-
auto n_energy_in = energy_.size();
160+
// Find energy bin and calculate interpolation factor
162161
int i;
163162
double r;
164-
if (E_in < energy_[0]) {
165-
i = 0;
166-
r = 0.0;
167-
} else if (E_in > energy_[n_energy_in - 1]) {
168-
i = n_energy_in - 2;
169-
r = 1.0;
170-
} else {
171-
i = lower_bound_index(energy_.begin(), energy_.end(), E_in);
172-
r = (E_in - energy_[i]) / (energy_[i + 1] - energy_[i]);
173-
}
163+
get_energy_index(energy_, E_in, i, r);
174164

175165
// Sample between the ith and [i+1]th bin
176166
int l = r > prn(seed) ? i + 1 : i;

src/secondary_kalbach.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "xtensor/xview.hpp"
1010

1111
#include "openmc/hdf5_interface.h"
12+
#include "openmc/math_functions.h"
1213
#include "openmc/random_dist.h"
1314
#include "openmc/random_lcg.h"
1415
#include "openmc/search.h"
@@ -117,21 +118,10 @@ KalbachMann::KalbachMann(hid_t group)
117118
void KalbachMann::sample(
118119
double E_in, double& E_out, double& mu, uint64_t* seed) const
119120
{
120-
// Find energy bin and calculate interpolation factor -- if the energy is
121-
// outside the range of the tabulated energies, choose the first or last bins
122-
auto n_energy_in = energy_.size();
121+
// Find energy bin and calculate interpolation factor
123122
int i;
124123
double r;
125-
if (E_in < energy_[0]) {
126-
i = 0;
127-
r = 0.0;
128-
} else if (E_in > energy_[n_energy_in - 1]) {
129-
i = n_energy_in - 2;
130-
r = 1.0;
131-
} else {
132-
i = lower_bound_index(energy_.begin(), energy_.end(), E_in);
133-
r = (E_in - energy_[i]) / (energy_[i + 1] - energy_[i]);
134-
}
124+
get_energy_index(energy_, E_in, i, r);
135125

136126
// Sample between the ith and [i+1]th bin
137127
int l = r > prn(seed) ? i + 1 : i;

src/secondary_thermal.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "openmc/secondary_thermal.h"
22

33
#include "openmc/hdf5_interface.h"
4+
#include "openmc/math_functions.h"
45
#include "openmc/random_lcg.h"
56
#include "openmc/search.h"
67

@@ -11,20 +12,6 @@
1112

1213
namespace openmc {
1314

14-
// Helper function to get index on incident energy grid
15-
void get_energy_index(
16-
const vector<double>& energies, double E, int& i, double& f)
17-
{
18-
// Get index and interpolation factor for elastic grid
19-
i = 0;
20-
f = 0.0;
21-
if (E >= energies.front()) {
22-
i = lower_bound_index(energies.begin(), energies.end(), E);
23-
if (i + 1 < energies.size())
24-
f = (E - energies[i]) / (energies[i + 1] - energies[i]);
25-
}
26-
}
27-
2815
//==============================================================================
2916
// CoherentElasticAE implementation
3017
//==============================================================================

0 commit comments

Comments
 (0)