From 2d20cd31f25c3949218dbb9a3a6350be5c961319 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Thu, 16 Mar 2023 14:37:05 -0500 Subject: [PATCH 01/17] initialize Discrete distribution with sequence of integers --- include/openmc/distribution.h | 1 + src/distribution.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/include/openmc/distribution.h b/include/openmc/distribution.h index 815ffa21a5..86aa4a3819 100644 --- a/include/openmc/distribution.h +++ b/include/openmc/distribution.h @@ -39,6 +39,7 @@ class Discrete : public Distribution { public: explicit Discrete(pugi::xml_node node); Discrete(const double* x, const double* p, int n); + Discrete(const double* p, int n); //! Sample a value from the distribution //! \param seed Pseudorandom number seed pointer diff --git a/src/distribution.cpp b/src/distribution.cpp index 4b4ebd69d7..ce2a85705c 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -41,6 +41,19 @@ Discrete::Discrete(const double* x, const double* p, int n) this->init_alias(x_vec, p_vec); } +Discrete::Discrete(const double* p, int n) +{ + std::vector p_vec(p, p + n); + std::vector x_vec(n); + + for (int i=0; iinit_alias(x_vec, p_vec); +} + void Discrete::init_alias(vector& x, vector& p) { x_ = x; From ee6975767d98f84133a0e4df3e443800a068fc32 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Thu, 16 Mar 2023 17:34:44 -0500 Subject: [PATCH 02/17] Use Discrete distribution for mesh sampling This version builds the independent variable by converting int to double and then converts back when sampling. --- include/openmc/distribution_spatial.h | 6 +----- src/distribution_spatial.cpp | 27 ++------------------------- 2 files changed, 3 insertions(+), 30 deletions(-) diff --git a/include/openmc/distribution_spatial.h b/include/openmc/distribution_spatial.h index a8cc7c58e0..63cbbb956c 100644 --- a/include/openmc/distribution_spatial.h +++ b/include/openmc/distribution_spatial.h @@ -114,11 +114,7 @@ public: private: int32_t mesh_idx_ {C_NONE}; - double total_strength_ {0.0}; - // TODO: move to an independent class in the future that's similar - // to a discrete distribution without outcomes - std::vector mesh_CDF_; - std::vector mesh_strengths_; + UPtrDist elem_idx_; //!< Distribution of mesh element indices }; //============================================================================== diff --git a/src/distribution_spatial.cpp b/src/distribution_spatial.cpp index fb1fbf099e..206f0b597c 100644 --- a/src/distribution_spatial.cpp +++ b/src/distribution_spatial.cpp @@ -212,10 +212,6 @@ MeshSpatial::MeshSpatial(pugi::xml_node node) int32_t n_bins = this->n_sources(); std::vector strengths(n_bins, 0.0); - mesh_CDF_.resize(n_bins + 1); - mesh_CDF_[0] = {0.0}; - total_strength_ = 0.0; - // Create cdfs for sampling for an element over a mesh // Volume scheme is weighted by the volume of each tet // File scheme is weighted by an array given in the xml file @@ -228,28 +224,9 @@ MeshSpatial::MeshSpatial(pugi::xml_node node) "not match the number of entities in mesh {} ({}).", strengths.size(), mesh_id, n_bins)); } - mesh_strengths_ = std::move(strengths); + elem_idx_ = UPtrDist {new Discrete {strengths, n_bins}}; } - if (get_node_value_bool(node, "volume_normalized")) { - for (int i = 0; i < n_bins; i++) { - mesh_strengths_[i] *= mesh()->volume(i); - } - } - - total_strength_ = - std::accumulate(mesh_strengths_.begin(), mesh_strengths_.end(), 0.0); - - for (int i = 0; i < n_bins; i++) { - mesh_CDF_[i + 1] = mesh_CDF_[i] + mesh_strengths_[i] / total_strength_; - } - - if (fabs(mesh_CDF_.back() - 1.0) > FP_COINCIDENT) { - fatal_error( - fmt::format("Mesh sampling CDF is incorrectly formed. Final value is: {}", - mesh_CDF_.back())); - } - mesh_CDF_.back() = 1.0; } Position MeshSpatial::sample(uint64_t* seed) const @@ -257,7 +234,7 @@ Position MeshSpatial::sample(uint64_t* seed) const // Create random variable for sampling element from mesh double eta = prn(seed); // Sample over the CDF defined in initialization above - int32_t elem_idx = lower_bound_index(mesh_CDF_.begin(), mesh_CDF_.end(), eta); + int32_t elem_idx = elem_idx_->sample(eta); return mesh()->sample(seed, elem_idx); } From db92a64ec984b647fc71fcabb5c75288570d80a4 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Thu, 16 Mar 2023 17:51:41 -0500 Subject: [PATCH 03/17] rename variable for clarity; re-add volume normalization --- include/openmc/distribution_spatial.h | 2 +- src/distribution_spatial.cpp | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/include/openmc/distribution_spatial.h b/include/openmc/distribution_spatial.h index 63cbbb956c..dc1d898fe5 100644 --- a/include/openmc/distribution_spatial.h +++ b/include/openmc/distribution_spatial.h @@ -114,7 +114,7 @@ public: private: int32_t mesh_idx_ {C_NONE}; - UPtrDist elem_idx_; //!< Distribution of mesh element indices + UPtrDist elem_idx_dist_; //!< Distribution of mesh element indices }; //============================================================================== diff --git a/src/distribution_spatial.cpp b/src/distribution_spatial.cpp index 206f0b597c..544734d3b3 100644 --- a/src/distribution_spatial.cpp +++ b/src/distribution_spatial.cpp @@ -224,8 +224,15 @@ MeshSpatial::MeshSpatial(pugi::xml_node node) "not match the number of entities in mesh {} ({}).", strengths.size(), mesh_id, n_bins)); } - elem_idx_ = UPtrDist {new Discrete {strengths, n_bins}}; } + + if (get_node_value_bool(node, "volume_normalized")) { + for (int i = 0; i < n_bins; i++) { + strengths[i] *= mesh()->volume(i); + } + } + + elem_idx_dist_ = UPtrDist {new Discrete {strengths, n_bins}}; } @@ -234,7 +241,7 @@ Position MeshSpatial::sample(uint64_t* seed) const // Create random variable for sampling element from mesh double eta = prn(seed); // Sample over the CDF defined in initialization above - int32_t elem_idx = elem_idx_->sample(eta); + int32_t elem_idx = elem_idx_dist_->sample(eta); return mesh()->sample(seed, elem_idx); } From 09d068fa81abeebd44994422437f44caa34331d5 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Thu, 16 Mar 2023 21:16:59 -0500 Subject: [PATCH 04/17] syntax fixes --- src/distribution_spatial.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/distribution_spatial.cpp b/src/distribution_spatial.cpp index 544734d3b3..7c5b2ed646 100644 --- a/src/distribution_spatial.cpp +++ b/src/distribution_spatial.cpp @@ -215,7 +215,6 @@ MeshSpatial::MeshSpatial(pugi::xml_node node) // Create cdfs for sampling for an element over a mesh // Volume scheme is weighted by the volume of each tet // File scheme is weighted by an array given in the xml file - mesh_strengths_ = std::vector(n_bins, 1.0); if (check_for_node(node, "strengths")) { strengths = get_node_array(node, "strengths"); if (strengths.size() != n_bins) { @@ -232,16 +231,14 @@ MeshSpatial::MeshSpatial(pugi::xml_node node) } } - elem_idx_dist_ = UPtrDist {new Discrete {strengths, n_bins}}; + elem_idx_dist_ = UPtrDist {new Discrete {strengths.data(), n_bins}}; } Position MeshSpatial::sample(uint64_t* seed) const { - // Create random variable for sampling element from mesh - double eta = prn(seed); // Sample over the CDF defined in initialization above - int32_t elem_idx = elem_idx_dist_->sample(eta); + int32_t elem_idx = elem_idx_dist_->sample(seed); return mesh()->sample(seed, elem_idx); } From 29b57486871f39fc28a9e336ffc98e64afacc561 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Fri, 17 Mar 2023 08:27:09 -0500 Subject: [PATCH 05/17] reduce temporary memory use for potentially large data --- include/openmc/distribution.h | 2 +- src/distribution.cpp | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/openmc/distribution.h b/include/openmc/distribution.h index 86aa4a3819..34744b3e3d 100644 --- a/include/openmc/distribution.h +++ b/include/openmc/distribution.h @@ -61,7 +61,7 @@ private: void normalize(); //! Initialize alias tables for distribution - void init_alias(vector& x, vector& p); + void init_alias(); }; //============================================================================== diff --git a/src/distribution.cpp b/src/distribution.cpp index ce2a85705c..e2e1138265 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -26,38 +26,38 @@ Discrete::Discrete(pugi::xml_node node) { auto params = get_node_array(node, "parameters"); - std::size_t n = params.size(); - std::vector x_vec(params.begin(), params.begin() + n / 2); - std::vector p_vec(params.begin() + n / 2, params.end()); + std::size_t n = params.size()/2; - this->init_alias(x_vec, p_vec); + x_.assign(params.begin(), params.begin() + n); + prob_.assign(params.begin() + n, params.end()); + + this->init_alias(); } Discrete::Discrete(const double* x, const double* p, int n) { - std::vector x_vec(x, x + n); - std::vector p_vec(p, p + n); + + x_.assign(x, x + n); + prob_.assign(p, p + n); - this->init_alias(x_vec, p_vec); + this->init_alias(); } Discrete::Discrete(const double* p, int n) { - std::vector p_vec(p, p + n); - std::vector x_vec(n); + prob_.assign(p, p + n); + x_.resize(n); for (int i=0; iinit_alias(x_vec, p_vec); + this->init_alias(); } -void Discrete::init_alias(vector& x, vector& p) +void Discrete::init_alias() { - x_ = x; - prob_ = p; normalize(); // The initialization and sampling method is based on Vose From b20dda710b36798d078559fac4eee16c9cdfe6cd Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Fri, 17 Mar 2023 12:58:53 -0500 Subject: [PATCH 06/17] change initialization of strengths --- src/distribution_spatial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/distribution_spatial.cpp b/src/distribution_spatial.cpp index 7c5b2ed646..8060527a81 100644 --- a/src/distribution_spatial.cpp +++ b/src/distribution_spatial.cpp @@ -210,7 +210,7 @@ MeshSpatial::MeshSpatial(pugi::xml_node node) } int32_t n_bins = this->n_sources(); - std::vector strengths(n_bins, 0.0); + std::vector strengths(n_bins, 1.0); // Create cdfs for sampling for an element over a mesh // Volume scheme is weighted by the volume of each tet From 38ca9b2f7cfdc71091d02b6bfe47ca19db33964d Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Tue, 21 Mar 2023 15:43:43 -0400 Subject: [PATCH 07/17] clang format fixes --- src/distribution.cpp | 7 +++---- src/distribution_spatial.cpp | 5 ++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/distribution.cpp b/src/distribution.cpp index e2e1138265..0dac7af367 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -26,7 +26,7 @@ Discrete::Discrete(pugi::xml_node node) { auto params = get_node_array(node, "parameters"); - std::size_t n = params.size()/2; + std::size_t n = params.size() / 2; x_.assign(params.begin(), params.begin() + n); prob_.assign(params.begin() + n, params.end()); @@ -36,7 +36,7 @@ Discrete::Discrete(pugi::xml_node node) Discrete::Discrete(const double* x, const double* p, int n) { - + x_.assign(x, x + n); prob_.assign(p, p + n); @@ -48,8 +48,7 @@ Discrete::Discrete(const double* p, int n) prob_.assign(p, p + n); x_.resize(n); - for (int i=0; ivolume(i); } } elem_idx_dist_ = UPtrDist {new Discrete {strengths.data(), n_bins}}; - } Position MeshSpatial::sample(uint64_t* seed) const From 1d9446a065f6cc5be06c614624c6c157fba44a9e Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Tue, 21 Mar 2023 16:14:38 -0400 Subject: [PATCH 08/17] introduce method to sample discrete index and use in various places --- include/openmc/distribution.h | 34 ++++++++++--- include/openmc/distribution_spatial.h | 3 +- src/distribution.cpp | 72 +++++++++++++++------------ src/distribution_spatial.cpp | 2 +- 4 files changed, 71 insertions(+), 40 deletions(-) diff --git a/include/openmc/distribution.h b/include/openmc/distribution.h index 34744b3e3d..2e210b7abb 100644 --- a/include/openmc/distribution.h +++ b/include/openmc/distribution.h @@ -32,27 +32,24 @@ using UPtrDist = unique_ptr; UPtrDist distribution_from_xml(pugi::xml_node node); //============================================================================== -//! A discrete distribution (probability mass function) +//! A discrete distribution index (probability mass function) //============================================================================== -class Discrete : public Distribution { +class DiscreteIndex { public: explicit Discrete(pugi::xml_node node); - Discrete(const double* x, const double* p, int n); Discrete(const double* p, int n); //! Sample a value from the distribution //! \param seed Pseudorandom number seed pointer //! \return Sampled value - double sample(uint64_t* seed) const override; + size_t sample(uint64_t* seed) const override; // Properties - const vector& x() const { return x_; } const vector& prob() const { return prob_; } const vector& alias() const { return alias_; } private: - vector x_; //!< Possible outcomes vector prob_; //!< Probability of accepting the uniformly sampled bin, //!< mapped to alias method table vector alias_; //!< Alias table @@ -64,6 +61,31 @@ private: void init_alias(); }; +//============================================================================== +//! A discrete distribution (probability mass function) +//============================================================================== + +class Discrete : public Distribution { +public: + explicit Discrete(pugi::xml_node node); + Discrete(const double* x, const double* p, int n); + + //! Sample a value from the distribution + //! \param seed Pseudorandom number seed pointer + //! \return Sampled value + double sample(uint64_t* seed) const override; + + // Properties + const vector& x() const { return x_; } + const vector& prob() const { return di_->prob(); } + const vector& alias() const { return di_->alias(); } + +private: + vector x_; //!< Possible outcomes + unique_ptr di_; //!< discrete probability distribution of + //!< outcome indices +}; + //============================================================================== //! Uniform distribution over the interval [a,b] //============================================================================== diff --git a/include/openmc/distribution_spatial.h b/include/openmc/distribution_spatial.h index dc1d898fe5..9ca9c0684b 100644 --- a/include/openmc/distribution_spatial.h +++ b/include/openmc/distribution_spatial.h @@ -114,7 +114,8 @@ public: private: int32_t mesh_idx_ {C_NONE}; - UPtrDist elem_idx_dist_; //!< Distribution of mesh element indices + unique_ptr elem_idx_dist_; //!< Distribution of + //!< mesh element indices }; //============================================================================== diff --git a/src/distribution.cpp b/src/distribution.cpp index 0dac7af367..2378cf1179 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -19,43 +19,26 @@ namespace openmc { //============================================================================== -// Discrete implementation +// DiscreteIndex implementation //============================================================================== -Discrete::Discrete(pugi::xml_node node) +DiscreteIndex::DiscreteIndex(pugi::xml_node node) { auto params = get_node_array(node, "parameters"); - std::size_t n = params.size() / 2; - - x_.assign(params.begin(), params.begin() + n); - prob_.assign(params.begin() + n, params.end()); + prob_.assign(params.begin(), params.end()); this->init_alias(); } -Discrete::Discrete(const double* x, const double* p, int n) -{ - - x_.assign(x, x + n); - prob_.assign(p, p + n); - - this->init_alias(); -} - -Discrete::Discrete(const double* p, int n) +DiscreteIndex::DiscreteIndex(const double* p, int n) { prob_.assign(p, p + n); - x_.resize(n); - - for (int i = 0; i < n; i++) { - x_[i] = i; - } this->init_alias(); } -void Discrete::init_alias() +void DiscreteIndex::init_alias() { normalize(); @@ -66,12 +49,11 @@ void Discrete::init_alias() vector small; // Set and allocate memory - vector alias(x_.size(), 0); - alias_ = alias; + alias_.assign(prob_.size(), 0); // Fill large and small vectors based on 1/n - for (size_t i = 0; i < x_.size(); i++) { - prob_[i] *= x_.size(); + for (size_t i = 0; i < prob_.size(); i++) { + prob_[i] *= prob_.size(); if (prob_[i] > 1.0) { large.push_back(i); } else { @@ -98,23 +80,23 @@ void Discrete::init_alias() } } -double Discrete::sample(uint64_t* seed) const +size_t DiscreteIndex::sample(uint64_t* seed) const { // Alias sampling of discrete distribution - int n = x_.size(); + int n = prob_.size(); if (n > 1) { int u = prn(seed) * n; if (prn(seed) < prob_[u]) { - return x_[u]; + return u; } else { - return x_[alias_[u]]; + return alias_[u]; } } else { - return x_[0]; + return 0; } } -void Discrete::normalize() +void DiscreteIndex::normalize() { // Renormalize density function so that it sums to unity double norm = std::accumulate(prob_.begin(), prob_.end(), 0.0); @@ -123,6 +105,32 @@ void Discrete::normalize() } } +//============================================================================== +// Discrete implementation +//============================================================================== + +Discrete::Discrete(pugi::xml_node node) +{ + auto params = get_node_array(node, "parameters"); + + std::size_t n = params.size() / 2; + + x_.assign(params.begin(), params.begin() + n); + di_ = new DiscreteIndex(params.begin() + n, n) +} + +Discrete::Discrete(const double* x, const double* p, int n) +{ + + x_.assign(x, x + n); + di_ = new DiscreteIndex(params.begin() + n, n) +} + +double Discrete::sample(uint64_t* seed) const +{ + return x_[di_->sample(seed)]; +} + //============================================================================== // Uniform implementation //============================================================================== diff --git a/src/distribution_spatial.cpp b/src/distribution_spatial.cpp index 272f9d941b..c2d370b17a 100644 --- a/src/distribution_spatial.cpp +++ b/src/distribution_spatial.cpp @@ -231,7 +231,7 @@ MeshSpatial::MeshSpatial(pugi::xml_node node) } } - elem_idx_dist_ = UPtrDist {new Discrete {strengths.data(), n_bins}}; + elem_idx_dist_ = make_unique(strengths.data(), n_bins); } Position MeshSpatial::sample(uint64_t* seed) const From 60c3f2a9d56137ad1348768ad81126346b3555e9 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Tue, 21 Mar 2023 16:36:33 -0400 Subject: [PATCH 09/17] type consistency and storing size as local --- src/distribution.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/distribution.cpp b/src/distribution.cpp index 2378cf1179..2066e5a8c1 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -48,12 +48,14 @@ void DiscreteIndex::init_alias() vector large; vector small; + size_t n = prob_.size(); + // Set and allocate memory - alias_.assign(prob_.size(), 0); + alias_.assign(n, 0); // Fill large and small vectors based on 1/n - for (size_t i = 0; i < prob_.size(); i++) { - prob_[i] *= prob_.size(); + for (size_t i = 0; i < n; i++) { + prob_[i] *= n; if (prob_[i] > 1.0) { large.push_back(i); } else { @@ -83,9 +85,9 @@ void DiscreteIndex::init_alias() size_t DiscreteIndex::sample(uint64_t* seed) const { // Alias sampling of discrete distribution - int n = prob_.size(); + size_t n = prob_.size(); if (n > 1) { - int u = prn(seed) * n; + size_t u = prn(seed) * n; if (prn(seed) < prob_[u]) { return u; } else { From 035e6dc45aee946976f30216be69371fd02aee21 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Wed, 22 Mar 2023 00:25:04 -0400 Subject: [PATCH 10/17] change to instance from pointer and cleanup lazy mistakes --- include/openmc/distribution.h | 14 ++++++++------ include/openmc/distribution_spatial.h | 4 ++-- src/distribution.cpp | 16 +++++++++++----- src/distribution_spatial.cpp | 2 +- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/include/openmc/distribution.h b/include/openmc/distribution.h index 2e210b7abb..b7d0e47210 100644 --- a/include/openmc/distribution.h +++ b/include/openmc/distribution.h @@ -37,13 +37,15 @@ UPtrDist distribution_from_xml(pugi::xml_node node); class DiscreteIndex { public: - explicit Discrete(pugi::xml_node node); - Discrete(const double* p, int n); + explicit DiscreteIndex(pugi::xml_node node); + DiscreteIndex(const double* p, int n); + + void assign(const double* p, int n); //! Sample a value from the distribution //! \param seed Pseudorandom number seed pointer //! \return Sampled value - size_t sample(uint64_t* seed) const override; + size_t sample(uint64_t* seed) const; // Properties const vector& prob() const { return prob_; } @@ -81,9 +83,9 @@ public: const vector& alias() const { return di_->alias(); } private: - vector x_; //!< Possible outcomes - unique_ptr di_; //!< discrete probability distribution of - //!< outcome indices + vector x_; //!< Possible outcomes + DiscreteIndex di_; //!< discrete probability distribution of + //!< outcome indices }; //============================================================================== diff --git a/include/openmc/distribution_spatial.h b/include/openmc/distribution_spatial.h index 9ca9c0684b..9fba950602 100644 --- a/include/openmc/distribution_spatial.h +++ b/include/openmc/distribution_spatial.h @@ -114,8 +114,8 @@ public: private: int32_t mesh_idx_ {C_NONE}; - unique_ptr elem_idx_dist_; //!< Distribution of - //!< mesh element indices + DiscreteIndex elem_idx_dist_; //!< Distribution of + //!< mesh element indices }; //============================================================================== diff --git a/src/distribution.cpp b/src/distribution.cpp index 2066e5a8c1..18fa414490 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -25,8 +25,9 @@ namespace openmc { DiscreteIndex::DiscreteIndex(pugi::xml_node node) { auto params = get_node_array(node, "parameters"); + std::size_t n = params.size() / 2; - prob_.assign(params.begin(), params.end()); + prob_.assign(params.begin() + n, params.end()); this->init_alias(); } @@ -38,6 +39,13 @@ DiscreteIndex::DiscreteIndex(const double* p, int n) this->init_alias(); } +void DiscreteIndex::assign(const double* p, int n) +{ + prob_.assign(p, p + n); + + this->init_alias(); +} + void DiscreteIndex::init_alias() { normalize(); @@ -111,21 +119,19 @@ void DiscreteIndex::normalize() // Discrete implementation //============================================================================== -Discrete::Discrete(pugi::xml_node node) +Discrete::Discrete(pugi::xml_node node) : di_(node) { auto params = get_node_array(node, "parameters"); std::size_t n = params.size() / 2; x_.assign(params.begin(), params.begin() + n); - di_ = new DiscreteIndex(params.begin() + n, n) } -Discrete::Discrete(const double* x, const double* p, int n) +Discrete::Discrete(const double* x, const double* p, int n) : di_(p, n) { x_.assign(x, x + n); - di_ = new DiscreteIndex(params.begin() + n, n) } double Discrete::sample(uint64_t* seed) const diff --git a/src/distribution_spatial.cpp b/src/distribution_spatial.cpp index c2d370b17a..694617ac5b 100644 --- a/src/distribution_spatial.cpp +++ b/src/distribution_spatial.cpp @@ -231,7 +231,7 @@ MeshSpatial::MeshSpatial(pugi::xml_node node) } } - elem_idx_dist_ = make_unique(strengths.data(), n_bins); + elem_idx_dist_.assign(strengths.data(), n_bins); } Position MeshSpatial::sample(uint64_t* seed) const From df4cfad60c08e62463a748dac6413787692396c2 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Wed, 22 Mar 2023 00:29:28 -0400 Subject: [PATCH 11/17] more pointer to instance changes --- include/openmc/distribution.h | 4 ++-- src/distribution.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/openmc/distribution.h b/include/openmc/distribution.h index b7d0e47210..12a6f6f5c9 100644 --- a/include/openmc/distribution.h +++ b/include/openmc/distribution.h @@ -79,8 +79,8 @@ public: // Properties const vector& x() const { return x_; } - const vector& prob() const { return di_->prob(); } - const vector& alias() const { return di_->alias(); } + const vector& prob() const { return di_.prob(); } + const vector& alias() const { return di_.alias(); } private: vector x_; //!< Possible outcomes diff --git a/src/distribution.cpp b/src/distribution.cpp index 18fa414490..deea035dee 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -136,7 +136,7 @@ Discrete::Discrete(const double* x, const double* p, int n) : di_(p, n) double Discrete::sample(uint64_t* seed) const { - return x_[di_->sample(seed)]; + return x_[di_.sample(seed)]; } //============================================================================== From c272ed4c4e11d8f3e35e877fb6246c65102c7ee8 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Wed, 22 Mar 2023 00:37:12 -0400 Subject: [PATCH 12/17] more pointer->instance repair --- src/distribution_spatial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/distribution_spatial.cpp b/src/distribution_spatial.cpp index 694617ac5b..e1a4c20c21 100644 --- a/src/distribution_spatial.cpp +++ b/src/distribution_spatial.cpp @@ -237,7 +237,7 @@ MeshSpatial::MeshSpatial(pugi::xml_node node) Position MeshSpatial::sample(uint64_t* seed) const { // Sample over the CDF defined in initialization above - int32_t elem_idx = elem_idx_dist_->sample(seed); + int32_t elem_idx = elem_idx_dist_.sample(seed); return mesh()->sample(seed, elem_idx); } From fb4845494f19b6c0dfde82f1ecb90d5fa8a879a7 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Wed, 22 Mar 2023 00:37:21 -0400 Subject: [PATCH 13/17] add default empty consructor --- include/openmc/distribution.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/openmc/distribution.h b/include/openmc/distribution.h index 12a6f6f5c9..03b046bbbe 100644 --- a/include/openmc/distribution.h +++ b/include/openmc/distribution.h @@ -37,7 +37,8 @@ UPtrDist distribution_from_xml(pugi::xml_node node); class DiscreteIndex { public: - explicit DiscreteIndex(pugi::xml_node node); + DiscreteIndex() {}; + DiscreteIndex(pugi::xml_node node); DiscreteIndex(const double* p, int n); void assign(const double* p, int n); From 967cd43fa13f9360437e130a76086abb08abf106 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Wed, 22 Mar 2023 08:45:19 -0400 Subject: [PATCH 14/17] reuse assign --- src/distribution.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/distribution.cpp b/src/distribution.cpp index deea035dee..599f57ad45 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -27,16 +27,12 @@ DiscreteIndex::DiscreteIndex(pugi::xml_node node) auto params = get_node_array(node, "parameters"); std::size_t n = params.size() / 2; - prob_.assign(params.begin() + n, params.end()); - - this->init_alias(); + assign(params.begin(), n); } DiscreteIndex::DiscreteIndex(const double* p, int n) { - prob_.assign(p, p + n); - - this->init_alias(); + assign(p, n); } void DiscreteIndex::assign(const double* p, int n) From 6fc0c3e545627f67bece40950b3426b7001a0b56 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Wed, 22 Mar 2023 09:27:50 -0400 Subject: [PATCH 15/17] manually convert iterator to pointer --- src/distribution.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/distribution.cpp b/src/distribution.cpp index 599f57ad45..aa0e8e6843 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -27,7 +27,7 @@ DiscreteIndex::DiscreteIndex(pugi::xml_node node) auto params = get_node_array(node, "parameters"); std::size_t n = params.size() / 2; - assign(params.begin(), n); + assign(&(*params.begin()), n); } DiscreteIndex::DiscreteIndex(const double* p, int n) From 72a6f77b0c57cc1db62041409662a08f43112862 Mon Sep 17 00:00:00 2001 From: "Paul P.H. Wilson" Date: Wed, 22 Mar 2023 09:59:00 -0400 Subject: [PATCH 16/17] use correct part of parameters --- src/distribution.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/distribution.cpp b/src/distribution.cpp index aa0e8e6843..32b47093cd 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -27,7 +27,7 @@ DiscreteIndex::DiscreteIndex(pugi::xml_node node) auto params = get_node_array(node, "parameters"); std::size_t n = params.size() / 2; - assign(&(*params.begin()), n); + assign(&(*params.begin()) + n, n); } DiscreteIndex::DiscreteIndex(const double* p, int n) From 9bb1ef4369871444f3342ee144a5854a34479a87 Mon Sep 17 00:00:00 2001 From: Paul Wilson Date: Wed, 22 Mar 2023 21:42:06 -0400 Subject: [PATCH 17/17] Update src/distribution.cpp Co-authored-by: Paul Romano --- src/distribution.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/distribution.cpp b/src/distribution.cpp index 32b47093cd..10d404c10f 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -27,7 +27,7 @@ DiscreteIndex::DiscreteIndex(pugi::xml_node node) auto params = get_node_array(node, "parameters"); std::size_t n = params.size() / 2; - assign(&(*params.begin()) + n, n); + assign(params.data() + n, n); } DiscreteIndex::DiscreteIndex(const double* p, int n)