IAP GITLAB

Skip to content
Snippets Groups Projects
Commit cbd9131c authored by ralfulrich's avatar ralfulrich
Browse files

Max comments

parent f7fd58fb
No related branches found
No related tags found
No related merge requests found
/*
* (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
...@@ -36,27 +36,16 @@ namespace corsika::weights { ...@@ -36,27 +36,16 @@ namespace corsika::weights {
public: public:
// default version for particle-creation from input data // default version for particle-creation from input data
void setParticleData(std::tuple<double> const v) { setWeight(std::get<0>(v)); } void setParticleData(std::tuple<double> const v);
void setParticleData(WeightDataInterface& parent, std::tuple<double> const) { void setParticleData(WeightDataInterface const& parent, std::tuple<double> const);
setWeight(parent.getWeight()); // copy Weight from parent particle! void setParticleData();
} void setParticleData(WeightDataInterface const& parent);
void setParticleData() { setWeight(1); } // default weight
void setParticleData(WeightDataInterface& parent) { std::string asString() const;
setWeight(parent.getWeight()); // copy Weight from parent particle!
} void setWeight(double const v);
std::string asString() const { double getWeight() 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());
}
}; };
// definition of stack-data object to store geometry information // definition of stack-data object to store geometry information
...@@ -72,39 +61,31 @@ namespace corsika::weights { ...@@ -72,39 +61,31 @@ namespace corsika::weights {
typedef std::vector<double> weight_vector_type; typedef std::vector<double> weight_vector_type;
WeightData() = default; WeightData() = default;
WeightData(WeightData const&) = default; WeightData(WeightData const&) = default;
WeightData(WeightData&&) = default; WeightData(WeightData&&) = default;
WeightData& operator=(WeightData const&) = default; WeightData& operator=(WeightData const&) = default;
WeightData& operator=(WeightData&&) = default; WeightData& operator=(WeightData&&) = default;
// these functions are needed for the Stack interface // 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) { void swap(int const i1, int const i2);
std::swap(weight_vector_[i1], weight_vector_[i2]);
}
// custom data access function // 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 // these functions are also needed by the Stack interface
void incrementSize() { weight_vector_.push_back(1); } // default weight void incrementSize();
void decrementSize() { void decrementSize();
if (weight_vector_.size() > 0) { weight_vector_.pop_back(); }
}
// custom private data section // custom private data section
private: private:
...@@ -117,3 +98,5 @@ namespace corsika::weights { ...@@ -117,3 +98,5 @@ namespace corsika::weights {
}; };
} // namespace corsika::weights } // namespace corsika::weights
#include <corsika/detail/stack/WeightStackExtension.inl>
...@@ -58,7 +58,7 @@ TEST_CASE("WeightStackExtension", "[stack]") { ...@@ -58,7 +58,7 @@ TEST_CASE("WeightStackExtension", "[stack]") {
CHECK(s.getEntries() == 1); CHECK(s.getEntries() == 1);
const auto pout = s.getNextParticle(); const auto pout = s.getNextParticle();
CHECK(pout.getWeight() == 15); CHECK(pout.getWeight() == weight);
} }
SECTION("stack fill and cleanup") { SECTION("stack fill and cleanup") {
...@@ -79,7 +79,7 @@ TEST_CASE("WeightStackExtension", "[stack]") { ...@@ -79,7 +79,7 @@ TEST_CASE("WeightStackExtension", "[stack]") {
v += p.getWeight(); v += p.getWeight();
p.erase(); p.erase();
} }
CHECK(v == 99 * weight); CHECK(v == Approx(99 * weight));
CHECK(s.getEntries() == 0); CHECK(s.getEntries() == 0);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment