Newer
Older
* (c) Copyright 2018 CORSIKA Project, corsika-project@lists.kit.edu
*
* See file AUTHORS for a list of contributors.
*
* 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.
*/
#ifndef _include_corsika_cascade_Cascade_h_
#define _include_corsika_cascade_Cascade_h_
#include <corsika/setup/SetupTrajectory.h>
/* see Issue 161, we need to include SetupStack only because we need
to globally define StackView. This is clearly not nice and should
be changed, when possible. It might be that StackView needs to be
templated in Cascade, but this would be even worse... so we don't
do that until it is really needed.
*/
#include <corsika/setup/SetupStack.h>
#include <cmath>
#include <iostream>
#include <boost/type_index.hpp>
using boost::typeindex::type_id_with_cvr;
/**
* The cascade namespace assembles all objects needed to simulate full particles cascades.
*/
* The Cascade class is constructed from template arguments making
* it very versatile. Via the template arguments physics models are
* plugged into the cascade simulation.
*
* <code>auto GetTrack(Particle const& p)</auto>,
* with the return type <code>geometry::Trajectory<corsika::geometry::Line>
* </code>
* <b>Stack</b> is the storage object for particle data, i.e. with
* Particle class type <code>Stack::ParticleType</code>
*
*
*/
template <typename TTracking, typename TProcessList, typename TStack,
/*
TStackView is needed as template parameter because of issue 161 and the
inability of clang to understand "MakeView" so far.
*/
typename TStackView = corsika::setup::StackView>
using VolumeTreeNode =
std::remove_pointer_t<decltype(((Particle*)nullptr)->GetNode())>;
using MediumInterface = typename VolumeTreeNode::IModelProperties;
// we only want fully configured objects
ralfulrich
committed
Cascade() = delete;
/**
* Cascade class cannot be default constructed, but needs a valid
* list of physics processes for configuration at construct time.
*/
Cascade(corsika::environment::Environment<MediumInterface> const& env, TTracking& tr,
TProcessList& pl, TStack& stack)
/**
* The Init function is called before the actual cascade simulations.
* All components of the Cascade simulation must be configured here.
*/
/**
* set the nodes for all particles on the stack according to their numerical
* position
*/
void SetNodes() {
std::for_each(fStack.begin(), fStack.end(), [&](auto& p) {
auto const* numericalNode =
fEnvironment.GetUniverse()->GetContainingNode(p.GetPosition());
/**
* The Run function is the main simulation loop, which processes
* particles from the Stack until the Stack is empty.
*/
while (!fStack.IsEmpty()) {
while (!fStack.IsEmpty()) {
Maximilian Reininghaus
committed
auto pNext = fStack.GetNextParticle();
ralfulrich
committed
Step(pNext);
fProcessSequence.DoStack(fStack);
}
// do cascade equations, which can put new particles on Stack,
// thus, the double loop
/**
* The Step function is executed for each particle from the
* stack. It will calcualte geometric transport of the particles,
* and apply continuous and stochastic processes to it, which may
* lead to energy losses, scattering, absorption, decays and the
* production of secondary particles.
*
* New particles produced in one step are subject to further
* processing, e.g. thinning, etc.
*/
auto [step, geomMaxLength, nextVol] = fTracking.GetTrack(vParticle);
[[maybe_unused]] auto const& dummy_nextVol = nextVol;
// determine combined total interaction length (inverse)
fProcessSequence.GetTotalInverseInteractionLength(vParticle);
corsika::random::ExponentialDistribution expDist(1 / total_inv_lambda);
GrammageType const next_interact = expDist(fRNG);
std::cout << "total_inv_lambda=" << total_inv_lambda
<< ", next_interact=" << next_interact << std::endl;
auto const* currentLogicalNode = vParticle.GetNode();
// assert that particle stays outside void Universe if it has no
// model properties set
assert(currentLogicalNode != &*fEnvironment.GetUniverse() ||
fEnvironment.GetUniverse()->HasModelProperties());
// convert next_step from grammage to length
currentLogicalNode->GetModelProperties().ArclengthFromGrammage(step,
next_interact);
// determine the maximum geometric step length
LengthType const distance_max = fProcessSequence.MaxStepLength(vParticle, step);
std::cout << "distance_max=" << distance_max << std::endl;
// determine combined total inverse decay time
corsika::random::ExponentialDistribution expDistDecay(1 / total_inv_lifetime);
TimeType const next_decay = expDistDecay(fRNG);
std::cout << "total_inv_lifetime=" << total_inv_lifetime
<< ", next_decay=" << next_decay << std::endl;
// convert next_decay from time to length [m]
LengthType const distance_decay = next_decay * vParticle.GetMomentum().norm() /
vParticle.GetEnergy() * units::constants::c;
// take minimum of geometry, interaction, decay for next step
std::min({distance_interact, distance_decay, distance_max, geomMaxLength});
std::cout << " move particle by : " << min_distance << std::endl;
// here the particle is actually moved along the trajectory to new position:
// std::visit(setup::ParticleUpdate<Particle>{vParticle}, step);
vParticle.SetPosition(step.PositionFromArclength(min_distance));
// .... also update time, momentum, direction, ...
vParticle.SetTime(vParticle.GetTime() + min_distance / units::constants::c);
step.LimitEndTo(min_distance);
// apply all continuous processes on particle + track
process::EProcessReturn status = fProcessSequence.DoContinuous(vParticle, step);
if (status == process::EProcessReturn::eParticleAbsorbed) {
std::cout << "Cascade: delete absorbed particle " << vParticle.GetPID() << " "
<< vParticle.GetEnergy() / 1_GeV << "GeV" << std::endl;
vParticle.Delete();
std::cout << "sth. happening before geometric limit ? "
<< ((min_distance < geomMaxLength) ? "yes" : "no") << std::endl;
if (min_distance < geomMaxLength) { // interaction to happen within geometric limit
// check whether decay or interaction limits this step the
// outcome of decay or interaction MAY be a) new particles in
// secondaries, b) the projectile particle deleted (or
// changed)
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
TStackView secondaries(vParticle);
if (min_distance != distance_max) {
/*
Create SecondaryView object on Stack. The data container
remains untouched and identical, and 'projectil' is identical
to 'vParticle' above this line. However,
projectil.AddSecondaries populate the SecondaryView, which can
then be used afterwards for further processing. Thus: it is
important to use projectle (and not vParticle) for Interaction,
and Decay!
*/
[[maybe_unused]] auto projectile = secondaries.GetProjectile();
if (min_distance == distance_interact) {
std::cout << "collide" << std::endl;
InverseGrammageType const current_inv_length =
fProcessSequence.GetTotalInverseInteractionLength(vParticle);
random::UniformRealDistribution<InverseGrammageType> uniDist(
current_inv_length);
const auto sample_process = uniDist(fRNG);
InverseGrammageType inv_lambda_count = 0. * meter * meter / gram;
fProcessSequence.SelectInteraction(vParticle, projectile, sample_process,
inv_lambda_count);
} else {
assert(min_distance == distance_decay);
std::cout << "decay" << std::endl;
InverseTimeType const actual_decay_time =
fProcessSequence.GetTotalInverseLifetime(vParticle);
random::UniformRealDistribution<InverseTimeType> uniDist(actual_decay_time);
const auto sample_process = uniDist(fRNG);
InverseTimeType inv_decay_count = 0 / second;
fProcessSequence.SelectDecay(vParticle, projectile, sample_process,
inv_decay_count);
}
fProcessSequence.DoSecondaries(secondaries);
vParticle.Delete(); // todo: this should be reviewed. Where
// exactly are particles best deleted, and
// where they should NOT be
// deleted... maybe Delete function should
// be "protected" and not accessible to physics
} else { // step-length limitation within volume
std::cout << "step-length limitation" << std::endl;
fProcessSequence.DoSecondaries(secondaries);
auto const assertion = [&] {
auto const* numericalNodeAfterStep =
fEnvironment.GetUniverse()->GetContainingNode(vParticle.GetPosition());
return numericalNodeAfterStep == currentLogicalNode;
};
assert(assertion()); // numerical and logical nodes don't match
std::cout << "boundary crossing! next node = " << nextVol << std::endl;
vParticle.SetNode(nextVol);
// DoBoundary may delete the particle (or not)
fProcessSequence.DoBoundaryCrossing(vParticle, *currentLogicalNode, *nextVol);
corsika::environment::Environment<MediumInterface> const& fEnvironment;
TTracking& fTracking;
TProcessList& fProcessSequence;
TStack& fStack;
corsika::random::RNG& fRNG =
corsika::random::RNGManager::GetInstance().GetRandomStream("cascade");
}; // namespace corsika::cascade
} // namespace corsika::cascade