diff --git a/corsika/detail/stack/WeightStackExtension.inl b/corsika/detail/stack/WeightStackExtension.inl new file mode 100644 index 0000000000000000000000000000000000000000..f3521e1bb2a025e2769de35c997a62bfe6420a8f --- /dev/null +++ b/corsika/detail/stack/WeightStackExtension.inl @@ -0,0 +1,92 @@ +/* + * (c) Copyright 2018 CORSIKA Project, corsika-project@lists.kit.edu + * + * This software is distributed under the terms of the GNU General Public + * Licence version 3 (GPL Version 3). See file LICENSE for a full version of + * the license. + */ + +#pragma once + +#include <corsika/framework/core/Logging.hpp> +#include <corsika/framework/stack/Stack.hpp> + +#include <tuple> +#include <utility> +#include <vector> + +namespace corsika::weights { + + // default version for particle-creation from input data + template <typename TParentStack> + inline void WeightDataInterface<TParentStack>::setParticleData( + std::tuple<double> const v) { + setWeight(std::get<0>(v)); + } + + template <typename TParentStack> + inline void WeightDataInterface<TParentStack>::setParticleData( + WeightDataInterface const& parent, std::tuple<double> const) { + setWeight(parent.getWeight()); // copy Weight from parent particle! + } + + template <typename TParentStack> + inline void WeightDataInterface<TParentStack>::setParticleData() { + setWeight(1); + } // default weight + + template <typename TParentStack> + inline void WeightDataInterface<TParentStack>::setParticleData( + WeightDataInterface const& parent) { + setWeight(parent.getWeight()); // copy Weight from parent particle! + } + + template <typename TParentStack> + inline std::string WeightDataInterface<TParentStack>::asString() const { + return fmt::format("weight={}", fmt::ptr(getWeight())); + } + + template <typename TParentStack> + inline void WeightDataInterface<TParentStack>::setWeight(double const v) { + super_type::getStackData().setWeight(super_type::getIndex(), v); + } + + template <typename TParentStack> + inline double WeightDataInterface<TParentStack>::getWeight() const { + return super_type::getStackData().getWeight(super_type::getIndex()); + } + + // definition of stack-data object to store geometry information + + // these functions are needed for the Stack interface + inline void WeightData::clear() { weight_vector_.clear(); } + + inline unsigned int WeightData::getSize() const { return weight_vector_.size(); } + + inline unsigned int WeightData::getCapacity() const { return weight_vector_.size(); } + + inline void WeightData::copy(int const i1, int const i2) { + weight_vector_[i2] = weight_vector_[i1]; + } + + inline void WeightData::swap(int const i1, int const i2) { + std::swap(weight_vector_[i1], weight_vector_[i2]); + } + + // custom data access function + inline void WeightData::setWeight(int const i, double const v) { + weight_vector_[i] = v; + } + + inline double WeightData::getWeight(int const i) const { return weight_vector_[i]; } + + // these functions are also needed by the Stack interface + inline void WeightData::incrementSize() { + weight_vector_.push_back(1); + } // default weight + + inline void WeightData::decrementSize() { + if (weight_vector_.size() > 0) { weight_vector_.pop_back(); } + } + +} // namespace corsika::weights diff --git a/corsika/stack/WeightStackExtension.hpp b/corsika/stack/WeightStackExtension.hpp index 372d266991bdd5a06089b7a09e8286690c7e3bdb..b2e6081f2638c0fe116e5ad87d18af0502e71605 100644 --- a/corsika/stack/WeightStackExtension.hpp +++ b/corsika/stack/WeightStackExtension.hpp @@ -36,27 +36,16 @@ namespace corsika::weights { public: // default version for particle-creation from input data - void setParticleData(std::tuple<double> const v) { setWeight(std::get<0>(v)); } - void setParticleData(WeightDataInterface& parent, std::tuple<double> const) { - setWeight(parent.getWeight()); // copy Weight from parent particle! - } - void setParticleData() { setWeight(1); } // default weight - void setParticleData(WeightDataInterface& parent) { - setWeight(parent.getWeight()); // copy Weight from parent particle! - } - - std::string asString() const { - return fmt::format("weight={}", fmt::ptr(getWeight())); - } - - void setWeight(double const v) { - - super_type::getStackData().setWeight(super_type::getIndex(), v); - } - - double getWeight() const { - return super_type::getStackData().getWeight(super_type::getIndex()); - } + void setParticleData(std::tuple<double> const v); + void setParticleData(WeightDataInterface const& parent, std::tuple<double> const); + void setParticleData(); + void setParticleData(WeightDataInterface const& parent); + + std::string asString() const; + + void setWeight(double const v); + + double getWeight() const; }; // definition of stack-data object to store geometry information @@ -72,39 +61,31 @@ namespace corsika::weights { typedef std::vector<double> weight_vector_type; WeightData() = default; - WeightData(WeightData const&) = default; - WeightData(WeightData&&) = default; - WeightData& operator=(WeightData const&) = default; - WeightData& operator=(WeightData&&) = default; // these functions are needed for the Stack interface - void clear() { weight_vector_.clear(); } + void clear(); - unsigned int getSize() const { return weight_vector_.size(); } + unsigned int getSize() const; - unsigned int getCapacity() const { return weight_vector_.size(); } + unsigned int getCapacity() const; - void copy(int const i1, int const i2) { weight_vector_[i2] = weight_vector_[i1]; } + void copy(int const i1, int const i2); - void swap(int const i1, int const i2) { - std::swap(weight_vector_[i1], weight_vector_[i2]); - } + void swap(int const i1, int const i2); // custom data access function - void setWeight(int const i, double const v) { weight_vector_[i] = v; } + void setWeight(int const i, double const v); - double getWeight(int const i) const { return weight_vector_[i]; } + double getWeight(int const i) const; // these functions are also needed by the Stack interface - void incrementSize() { weight_vector_.push_back(1); } // default weight + void incrementSize(); - void decrementSize() { - if (weight_vector_.size() > 0) { weight_vector_.pop_back(); } - } + void decrementSize(); // custom private data section private: @@ -117,3 +98,5 @@ namespace corsika::weights { }; } // namespace corsika::weights + +#include <corsika/detail/stack/WeightStackExtension.inl> diff --git a/tests/stack/testWeightStackExtension.cpp b/tests/stack/testWeightStackExtension.cpp index 4fc44e76adf5f0659949a382825c8717a6a4f85e..2754c367913e336325491945f6570e4bd014899b 100644 --- a/tests/stack/testWeightStackExtension.cpp +++ b/tests/stack/testWeightStackExtension.cpp @@ -58,7 +58,7 @@ TEST_CASE("WeightStackExtension", "[stack]") { CHECK(s.getEntries() == 1); const auto pout = s.getNextParticle(); - CHECK(pout.getWeight() == 15); + CHECK(pout.getWeight() == weight); } SECTION("stack fill and cleanup") { @@ -79,7 +79,7 @@ TEST_CASE("WeightStackExtension", "[stack]") { v += p.getWeight(); p.erase(); } - CHECK(v == 99 * weight); + CHECK(v == Approx(99 * weight)); CHECK(s.getEntries() == 0); } }