IAP GITLAB

Skip to content
Snippets Groups Projects
Commit df704e19 authored by ralfulrich's avatar ralfulrich Committed by Maximilian Reininghaus
Browse files

added weights to stack

parent 2edb9ba2
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <corsika/framework/stack/CombinedStack.hpp> #include <corsika/framework/stack/CombinedStack.hpp>
#include <corsika/stack/GeometryNodeStackExtension.hpp> #include <corsika/stack/GeometryNodeStackExtension.hpp>
#include <corsika/stack/NuclearStackExtension.hpp> #include <corsika/stack/NuclearStackExtension.hpp>
#include <corsika/stack/WeightStackExtension.hpp>
#include <corsika/stack/history/HistorySecondaryProducer.hpp> #include <corsika/stack/history/HistorySecondaryProducer.hpp>
#include <corsika/stack/history/HistoryStackExtension.hpp> #include <corsika/stack/history/HistoryStackExtension.hpp>
...@@ -27,7 +28,7 @@ namespace corsika { ...@@ -27,7 +28,7 @@ namespace corsika {
// environment: // environment:
template <typename TStackIter> template <typename TStackIter>
using SetupGeometryDataInterface = using SetupGeometryDataInterface =
typename node::MakeGeometryDataInterface<TStackIter, setup::Environment>::type; typename node::make_GeometryDataInterface<TStackIter, setup::Environment>::type;
// combine particle data stack with geometry information for tracking // combine particle data stack with geometry information for tracking
template <typename TStackIter> template <typename TStackIter>
......
...@@ -117,7 +117,7 @@ namespace corsika::node { ...@@ -117,7 +117,7 @@ namespace corsika::node {
}; };
template <typename T, typename TEnv> template <typename T, typename TEnv>
struct MakeGeometryDataInterface { struct make_GeometryDataInterface {
typedef GeometryDataInterface<T, TEnv> type; typedef GeometryDataInterface<T, TEnv> type;
}; };
......
/*
* (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 {
/**
* Describe "particle weights" on a Stack.
*
* Corresponding defintion of a stack-readout object, the iteractor
* dereference operator will deliver access to these function
* defintion of a stack-readout object, the iteractor dereference
* operator will deliver access to these function
*/
/**
* \tparam TParentStack The stack to be exteneded here with weight data
*/
template <typename TParentStack>
struct WeightDataInterface : public TParentStack {
typedef TParentStack super_type;
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());
}
};
// definition of stack-data object to store geometry information
/**
* @class WeightData
*
* definition of stack-data object to store geometry information
*/
class WeightData {
public:
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(); }
unsigned int getSize() const { return weight_vector_.size(); }
unsigned int getCapacity() const { return weight_vector_.size(); }
void copy(int const i1, int const i2) { weight_vector_[i2] = weight_vector_[i1]; }
void swap(int const i1, int const i2) {
std::swap(weight_vector_[i1], weight_vector_[i2]);
}
// custom data access function
void setWeight(int const i, double const v) { weight_vector_[i] = v; }
double getWeight(int const i) const { return weight_vector_[i]; }
// these functions are also needed by the Stack interface
void incrementSize() { weight_vector_.push_back(1); } // default weight
void decrementSize() {
if (weight_vector_.size() > 0) { weight_vector_.pop_back(); }
}
// custom private data section
private:
weight_vector_type weight_vector_;
};
template <typename TParentStack>
struct make_WeightDataInterface {
typedef WeightDataInterface<TParentStack> type;
};
} // namespace corsika::weights
...@@ -3,6 +3,7 @@ set (test_stack_sources ...@@ -3,6 +3,7 @@ set (test_stack_sources
testHistoryStack.cpp testHistoryStack.cpp
testHistoryView.cpp testHistoryView.cpp
testGeometryNodeStackExtension.cpp testGeometryNodeStackExtension.cpp
testWeightStackExtension.cpp
testDummyStack.cpp testDummyStack.cpp
testVectorStack.cpp testVectorStack.cpp
testNuclearStackExtension.cpp testNuclearStackExtension.cpp
......
...@@ -26,7 +26,7 @@ public: ...@@ -26,7 +26,7 @@ public:
// the GeometryNode stack needs to know the type of geometry-nodes from the DummyEnv: // the GeometryNode stack needs to know the type of geometry-nodes from the DummyEnv:
template <typename TStackIter> template <typename TStackIter>
using DummyGeometryDataInterface = using DummyGeometryDataInterface =
typename node::MakeGeometryDataInterface<TStackIter, DummyEnv>::type; typename node::make_GeometryDataInterface<TStackIter, DummyEnv>::type;
// combine dummy stack with geometry information for tracking // combine dummy stack with geometry information for tracking
template <typename TStackIter> template <typename TStackIter>
......
/*
* (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.
*/
#include <corsika/framework/stack/CombinedStack.hpp>
#include <corsika/stack/DummyStack.hpp>
#include <corsika/stack/WeightStackExtension.hpp>
using namespace corsika;
#include <catch2/catch.hpp>
#include <iostream>
using namespace std;
// the Weight stack:
template <typename TStackIter>
using DummyWeightDataInterface =
typename weights::make_WeightDataInterface<TStackIter>::type;
// combine dummy stack with geometry information for tracking
template <typename TStackIter>
using StackWithGeometryInterface =
CombinedParticleInterface<dummy_stack::DummyStack::pi_type, DummyWeightDataInterface,
TStackIter>;
using TestStack =
CombinedStack<typename dummy_stack::DummyStack::stack_implementation_type,
weights::WeightData, StackWithGeometryInterface>;
TEST_CASE("WeightStackExtension", "[stack]") {
logging::set_level(logging::level::info);
corsika_logger->set_pattern("[%n:%^%-8l%$] custom pattern: %v");
dummy_stack::NoData noData;
SECTION("write weights") {
double const weight = 5.1;
TestStack s;
s.addParticle(std::make_tuple(noData), std::tuple<double>{weight});
CHECK(s.getEntries() == 1);
}
SECTION("write/read weights") {
double const weight = 15;
TestStack s;
auto p = s.addParticle(std::make_tuple(noData));
p.setWeight(weight);
CHECK(s.getEntries() == 1);
const auto pout = s.getNextParticle();
CHECK(pout.getWeight() == 15);
}
SECTION("stack fill and cleanup") {
double const weight = 16;
TestStack s;
// add 99 particles, each 10th particle is a nucleus with A=i and Z=A/2!
for (int i = 0; i < 99; ++i) {
auto p = s.addParticle(std::tuple<dummy_stack::NoData>{noData});
p.setWeight(weight);
}
CHECK(s.getEntries() == 99);
double v = 0;
for (int i = 0; i < 99; ++i) {
auto p = s.getNextParticle();
v += p.getWeight();
p.erase();
}
CHECK(v == 99 * weight);
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