Skip to content
Open
Show file tree
Hide file tree
Changes from 78 commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
06ae298
Implement get_pdf functionality
Aug 27, 2025
78d841e
Update include/openmc/distribution.h
itay-space Aug 28, 2025
33a27a9
Update include/openmc/particle_data.h
itay-space Aug 28, 2025
d968875
Update include/openmc/particle_data.h
itay-space Aug 28, 2025
3b896c0
Update src/physics.cpp
itay-space Aug 28, 2025
c034bff
Update src/physics.cpp
itay-space Aug 28, 2025
a5d2f4c
Update src/physics.cpp
itay-space Aug 28, 2025
ca5b44e
Cleaning some code
Aug 31, 2025
eed8b1c
Resolving conversations
Aug 31, 2025
f2ecdd4
cosmetic fixes
GuySten Aug 31, 2025
71d5fa2
Merge branch 'develop' into get_pdf
GuySten Aug 31, 2025
c3b4d82
simplified code significantly
GuySten Aug 31, 2025
78a33cc
Update src/distribution_angle.cpp
itay-space Aug 31, 2025
6d70d39
ran clang format
GuySten Aug 31, 2025
acfd402
fixes
GuySten Aug 31, 2025
98ca32e
cosmetics
GuySten Aug 31, 2025
26d2a56
using openmc's fatal_error
Sep 1, 2025
7abe69c
Use accessors in powerlaw
Sep 1, 2025
d183cce
clausing error for different distributions
Sep 1, 2025
37b41c4
fixing function signature
Sep 1, 2025
007dcf5
Update include/openmc/angle_energy.h
Eliezer214 Sep 2, 2025
ebebb7a
Update include/openmc/distribution_angle.h
Eliezer214 Sep 2, 2025
0239c1f
Update include/openmc/secondary_thermal.h
Eliezer214 Sep 2, 2025
225f837
Update include/openmc/angle_energy.h
Eliezer214 Sep 2, 2025
fa22e14
Update include/openmc/reaction_product.h
Eliezer214 Sep 2, 2025
a0678c9
Update src/reaction_product.cpp
itay-space Sep 2, 2025
e590d82
Update src/thermal.cpp
itay-space Sep 2, 2025
1b1134e
Update include/openmc/secondary_thermal.h
itay-space Sep 2, 2025
5c348f1
Update src/secondary_uncorrelated.cpp
itay-space Sep 2, 2025
203a191
Update src/secondary_thermal.cpp
itay-space Sep 2, 2025
c4d8aa6
Update include/openmc/secondary_thermal.h
itay-space Sep 2, 2025
e9b6860
Update src/reaction_product.cpp
itay-space Sep 2, 2025
ee1f2c5
Apply suggestion from @GuySten
itay-space Sep 2, 2025
e07e312
Apply suggestion from @GuySten
itay-space Sep 2, 2025
1ab0da1
Update src/thermal.cpp
itay-space Sep 2, 2025
c98566a
Update src/thermal.cpp
itay-space Sep 2, 2025
de3fc79
Update include/openmc/reaction_product.h
itay-space Sep 2, 2025
978bf74
remove gsl-lite
GuySten Sep 2, 2025
eb8389d
few fixes to signature
Sep 2, 2025
220f099
more signature fixes
Sep 2, 2025
d9a2b53
more signature fixes
Sep 2, 2025
8271e15
ran clang format
GuySten Sep 2, 2025
d0d8494
Restore PDF calculation block in secondary_kalbach.cpp after accident…
Sep 2, 2025
f2418d2
cleaning
Sep 2, 2025
9a5d701
remove gsl-lite
GuySten Sep 2, 2025
3f91b0d
clean
Sep 2, 2025
e1d069c
Update src/secondary_correlated.cpp
itay-space Sep 2, 2025
f9e4693
Update include/openmc/secondary_thermal.h
GuySten Sep 2, 2025
b45dcfd
ran clang format
GuySten Sep 2, 2025
a85365c
Update src/secondary_kalbach.cpp
GuySten Sep 2, 2025
d782013
returning pdf assuming data in lab
Sep 2, 2025
c72bb02
cleaning
Sep 2, 2025
3cb6821
remove gsl-lite
GuySten Sep 2, 2025
47e96a6
Apply suggestions from code review
GuySten Sep 2, 2025
f97f4ad
Update secondary_nbody.cpp
GuySten Sep 2, 2025
1e4b763
ran clang format
GuySten Sep 2, 2025
3bc4698
Update src/secondary_kalbach.cpp
itay-space Sep 3, 2025
4e85033
returning pdf
Sep 3, 2025
6727a03
Update src/secondary_nbody.cpp
itay-space Sep 3, 2025
c5a737b
isotropic and no lab yet
Sep 3, 2025
186ad34
Update src/distribution_angle.cpp
itay-space Sep 3, 2025
108fa52
remove gsl-lite
GuySten Sep 2, 2025
0f40022
ran clang format
GuySten Sep 3, 2025
4bf8f23
Update include/openmc/distribution_angle.h
GuySten Sep 3, 2025
ca7cd85
returning pdf in uncorrelated
Sep 3, 2025
d7b1621
fix access to get_pdf
GuySten Sep 3, 2025
7cb99e8
ran clang format
GuySten Sep 3, 2025
e2de1c4
cosmetics
GuySten Sep 3, 2025
d5ea1c0
WIP: trying new approach until reaction_product.cpp
GuySten Sep 4, 2025
dc87a7f
fix additional signatures
GuySten Sep 4, 2025
ad86823
wip
GuySten Sep 4, 2025
4500491
reverted most of the changes in the new approach
GuySten Sep 4, 2025
7a1b5cc
bugfix
GuySten Sep 4, 2025
ae2a0fb
bugfix
GuySten Sep 4, 2025
f3aa6dc
bugfix
GuySten Sep 4, 2025
52feef7
another fix for mixed elastic
GuySten Sep 4, 2025
12d5372
guarantee every AngleEnergy has sample_energy_and_pdf
GuySten Sep 4, 2025
ff83fc4
fix for decay photons
GuySten Sep 4, 2025
f859882
replace pdf->evaluate
GuySten Sep 6, 2025
b0613dc
Merge branch 'develop' into get_pdf
GuySten Nov 3, 2025
8bb4356
Merge branch 'develop' into get_pdf
GuySten Nov 3, 2025
cc7c3a5
added evaluate for SphericalUnitDistribution
GuySten Nov 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/openmc/angle_energy.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class AngleEnergy {
public:
virtual void sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const = 0;
virtual double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const = 0;
virtual ~AngleEnergy() = default;
};

Expand Down
2 changes: 2 additions & 0 deletions include/openmc/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class DecayPhotonAngleEnergy : public AngleEnergy {
//! \param[inout] seed Pseudorandom seed pointer
void sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const override;
double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const override;

private:
const Distribution* photon_energy_;
Expand Down
9 changes: 9 additions & 0 deletions include/openmc/distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "pugixml.hpp"

#include "openmc/constants.h"
#include "openmc/error.h"
#include "openmc/memory.h" // for unique_ptr
#include "openmc/span.h"
#include "openmc/vector.h" // for vector
Expand All @@ -23,6 +24,10 @@ class Distribution {
public:
virtual ~Distribution() = default;
virtual double sample(uint64_t* seed) const = 0;
virtual double pdf(double x) const
{
fatal_error("pdf not available for this Distribution type");
}

//! Return integral of distribution
//! \return Integral of distribution
Expand Down Expand Up @@ -111,6 +116,7 @@ class Uniform : public Distribution {
//! \param seed Pseudorandom number seed pointer
//! \return Sampled value
double sample(uint64_t* seed) const override;
double pdf(double x) const override;

double a() const { return a_; }
double b() const { return b_; }
Expand All @@ -135,6 +141,7 @@ class PowerLaw : public Distribution {
//! \param seed Pseudorandom number seed pointer
//! \return Sampled value
double sample(uint64_t* seed) const override;
double pdf(double x) const override;

double a() const { return std::pow(offset_, ninv_); }
double b() const { return std::pow(offset_ + span_, ninv_); }
Expand Down Expand Up @@ -204,6 +211,7 @@ class Normal : public Distribution {
//! \param seed Pseudorandom number seed pointer
//! \return Sampled value
double sample(uint64_t* seed) const override;
double pdf(double x) const override;

double mean_value() const { return mean_value_; }
double std_dev() const { return std_dev_; }
Expand All @@ -227,6 +235,7 @@ class Tabular : public Distribution {
//! \param seed Pseudorandom number seed pointer
//! \return Sampled value
double sample(uint64_t* seed) const override;
double pdf(double x) const override;

// properties
vector<double>& x() { return x_; }
Expand Down
1 change: 1 addition & 0 deletions include/openmc/distribution_angle.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class AngleDistribution {
//! \param[inout] seed pseudorandom number seed pointer
//! \return Cosine of the angle in the range [-1,1]
double sample(double E, uint64_t* seed) const;
double pdf(double E, double mu) const;

//! Determine whether angle distribution is empty
//! \return Whether distribution is empty
Expand Down
3 changes: 3 additions & 0 deletions include/openmc/reaction_product.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class ReactionProduct {
//! \param[inout] seed Pseudorandom seed pointer
void sample(double E_in, double& E_out, double& mu, uint64_t* seed) const;

double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const;

ParticleType particle_; //!< Particle type
EmissionMode emission_mode_; //!< Emission mode
double decay_rate_; //!< Decay rate (for delayed neutron precursors) in [1/s]
Expand Down
2 changes: 2 additions & 0 deletions include/openmc/secondary_correlated.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class CorrelatedAngleEnergy : public AngleEnergy {
//! \param[inout] seed Pseudorandom seed pointer
void sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const override;
double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const override;

// energy property
vector<double>& energy() { return energy_; }
Expand Down
2 changes: 2 additions & 0 deletions include/openmc/secondary_kalbach.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class KalbachMann : public AngleEnergy {
//! \param[inout] seed Pseudorandom seed pointer
void sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const override;
double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const override;

private:
//! Outgoing energy/angle at a single incoming energy
Expand Down
2 changes: 2 additions & 0 deletions include/openmc/secondary_nbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class NBodyPhaseSpace : public AngleEnergy {
//! \param[inout] seed Pseudorandom seed pointer
void sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const override;
double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const override;

private:
int n_bodies_; //!< Number of particles distributed
Expand Down
12 changes: 12 additions & 0 deletions include/openmc/secondary_thermal.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class CoherentElasticAE : public AngleEnergy {
//! \param[inout] seed Pseudorandom seed pointer
void sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const override;
double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const override;

private:
const CoherentElasticXS& xs_; //!< Coherent elastic scattering cross section
Expand All @@ -55,6 +57,8 @@ class IncoherentElasticAE : public AngleEnergy {
//! \param[inout] seed Pseudorandom number seed pointer
void sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const override;
double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const override;

private:
double debye_waller_;
Expand All @@ -80,6 +84,8 @@ class IncoherentElasticAEDiscrete : public AngleEnergy {
//! \param[inout] seed Pseudorandom number seed pointer
void sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const override;
double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const override;

private:
const vector<double>& energy_; //!< Energies at which cosines are tabulated
Expand All @@ -106,6 +112,8 @@ class IncoherentInelasticAEDiscrete : public AngleEnergy {
//! \param[inout] seed Pseudorandom number seed pointer
void sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const override;
double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const override;

private:
const vector<double>& energy_; //!< Incident energies
Expand Down Expand Up @@ -134,6 +142,8 @@ class IncoherentInelasticAE : public AngleEnergy {
//! \param[inout] seed Pseudorandom number seed pointer
void sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const override;
double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const override;

private:
//! Secondary energy/angle distribution
Expand Down Expand Up @@ -169,6 +179,8 @@ class MixedElasticAE : public AngleEnergy {
//! \param[inout] seed Pseudorandom number seed pointer
void sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const override;
double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const override;

private:
CoherentElasticAE coherent_dist_; //!< Coherent distribution
Expand Down
2 changes: 2 additions & 0 deletions include/openmc/secondary_uncorrelated.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class UncorrelatedAngleEnergy : public AngleEnergy {
//! \param[inout] seed Pseudorandom seed pointer
void sample(
double E_in, double& E_out, double& mu, uint64_t* seed) const override;
double sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const override;

// Accessors
AngleDistribution& angle() { return angle_; }
Expand Down
2 changes: 2 additions & 0 deletions include/openmc/thermal.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class ThermalData {
//! \param[inout] seed Pseudorandom seed pointer
void sample(const NuclideMicroXS& micro_xs, double E_in, double* E_out,
double* mu, uint64_t* seed);
double sample_energy_and_pdf(const NuclideMicroXS& micro_xs, double E_in,
double mu, double& E_out, uint64_t* seed) const;

private:
struct Reaction {
Expand Down
7 changes: 7 additions & 0 deletions src/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ void DecayPhotonAngleEnergy::sample(
mu = Uniform(-1., 1.).sample(seed);
}

double DecayPhotonAngleEnergy::sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const
{
E_out = photon_energy_->sample(seed);
return 0.5;
}

//==============================================================================
// Global variables
//==============================================================================
Expand Down
68 changes: 68 additions & 0 deletions src/distribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "openmc/math_functions.h"
#include "openmc/random_dist.h"
#include "openmc/random_lcg.h"
#include "openmc/search.h"
#include "openmc/xml_interface.h"

namespace openmc {
Expand Down Expand Up @@ -156,6 +157,14 @@ double Uniform::sample(uint64_t* seed) const
return a_ + prn(seed) * (b_ - a_);
}

double Uniform::pdf(double x) const
{
if (x <= b_ && x >= a_)
return 1 / (b_ - a_);
else
return 0;
}

//==============================================================================
// PowerLaw implementation
//==============================================================================
Expand All @@ -182,6 +191,21 @@ double PowerLaw::sample(uint64_t* seed) const
return std::pow(offset_ + prn(seed) * span_, ninv_);
}

double PowerLaw::pdf(double x) const
{
// Use accessors
double a_val = this->a();
double b_val = this->b();
double n_val = this->n();

if (x < a_val || x > b_val) {
return 0.0; // outside support
}

return (n_val + 1.0) * std::pow(x, n_val) /
(std::pow(b_val, n_val + 1.0) - std::pow(a_val, n_val + 1.0));
}

//==============================================================================
// Maxwell implementation
//==============================================================================
Expand Down Expand Up @@ -236,6 +260,13 @@ double Normal::sample(uint64_t* seed) const
return normal_variate(mean_value_, std_dev_, seed);
}

double Normal::pdf(double x) const
{
double exponent = -0.5 * std::pow((x - mean_value_) / std_dev_, 2);
double coefficient = 1 / (std_dev_ * std::sqrt(2 * PI));
return coefficient * std::exp(exponent);
}

//==============================================================================
// Tabular implementation
//==============================================================================
Expand Down Expand Up @@ -356,6 +387,43 @@ double Tabular::sample(uint64_t* seed) const
}
}

double Tabular::pdf(double x) const
{
// get PDF value at x

int i;
std::size_t n = x_.size();
if (x < x_[0]) {
return 0;
} else if (x > x_[n - 1]) {
return 0;
} else {
i = lower_bound_index(x_.begin(), x_.end(), x);
}

// Determine bounding PDF values
double x_i = x_[i];
double p_i = p_[i];

switch (interp_) {
case Interpolation::histogram:
// Histogram interpolation
return p_i;

case Interpolation::lin_lin: {
// Linear-linear interpolation
double x_i1 = x_[i + 1];
double p_i1 = p_[i + 1];

double m = (p_i1 - p_i) / (x_i1 - x_i);
return (m == 0.0) ? p_i : p_i + (x - x_i) * m;
}

default:
fatal_error("Unsupported interpolation type in PDF evaluation.");
}
}

//==============================================================================
// Equiprobable implementation
//==============================================================================
Expand Down
28 changes: 28 additions & 0 deletions src/distribution_angle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,32 @@ double AngleDistribution::sample(double E, uint64_t* seed) const
return mu;
}

double AngleDistribution::pdf(double E, double mu) const
{
// Determine number of incoming energies
auto n = energy_.size();

// Find energy bin and calculate interpolation factor -- if the energy is
// outside the range of the tabulated energies, choose the first or last bins
int i;
double r;
if (E < energy_[0]) {
i = 0;
r = 0.0;
} else if (E > energy_[n - 1]) {
i = n - 2;
r = 1.0;
} else {
i = lower_bound_index(energy_.begin(), energy_.end(), E);
r = (E - energy_[i]) / (energy_[i + 1] - energy_[i]);
}

double pdf = 0.0;
if (r > 0.0)
pdf += r * distribution_[i + 1]->pdf(mu);
if (r < 1.0)
pdf += (1.0 - r) * distribution_[i]->pdf(mu);
return pdf;
}

} // namespace openmc
29 changes: 29 additions & 0 deletions src/reaction_product.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,33 @@ void ReactionProduct::sample(
}
}

double ReactionProduct::sample_energy_and_pdf(
double E_in, double mu, double& E_out, uint64_t* seed) const
{

int distribution_index;
auto n = applicability_.size();
if (n > 1) {
double prob = 0.0;
double c = prn(seed);
for (int i = 0; i < n; ++i) {
// Determine probability that i-th energy distribution is sampled
prob += applicability_[i](E_in);

// If i-th distribution is sampled, sample energy from the distribution
if (c <= prob) {
// distribution_[i]->sample(E_in, E_out, mu, seed);
distribution_index = i;
break;
}
}
} else {
distribution_index = 0;
}
// now extract pdf

AngleEnergy* angleEnergyPtr = distribution_[distribution_index].get();
return angleEnergyPtr->sample_energy_and_pdf(E_in, mu, E_out, seed);
}

} // namespace openmc
Loading