IAP GITLAB

Skip to content
Snippets Groups Projects
Cascade.h 11.7 KiB
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_
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
#include <corsika/environment/Environment.h>
ralfulrich's avatar
ralfulrich committed
#include <corsika/process/ProcessReturn.h>
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
#include <corsika/random/ExponentialDistribution.h>
ralfulrich's avatar
ralfulrich committed
#include <corsika/random/RNGManager.h>
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
#include <corsika/random/UniformRealDistribution.h>
ralfulrich's avatar
ralfulrich committed
#include <corsika/stack/SecondaryView.h>
#include <corsika/units/PhysicalUnits.h>
ralfulrich's avatar
ralfulrich committed
#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 <cassert>
#include <type_traits>
#include <boost/type_index.hpp>
using boost::typeindex::type_id_with_cvr;

ralfulrich's avatar
ralfulrich committed
/**
 * The cascade namespace assembles all objects needed to simulate full particles cascades.
 */

namespace corsika::cascade {

ralfulrich's avatar
ralfulrich committed
  /**
   * \class Cascade
   *
ralfulrich's avatar
ralfulrich committed
   * The Cascade class is constructed from template arguments making
   * it very versatile. Via the template arguments physics models are
   * plugged into the cascade simulation.
   *
ralfulrich's avatar
ralfulrich committed
   * <b>TTracking</b> must be a class according to the
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
   * TrackingInterface providing the functions:
   * <code>auto GetTrack(Particle const& p)</auto>,
   * with the return type <code>geometry::Trajectory<corsika::geometry::Line>
   * </code>
ralfulrich's avatar
ralfulrich committed
   *
ralfulrich's avatar
ralfulrich committed
   * <b>TProcessList</b> must be a ProcessSequence.   *
ralfulrich's avatar
ralfulrich committed
   * <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>
  class Cascade {
ralfulrich's avatar
ralfulrich committed
    using Particle = typename TStack::ParticleType;
    using VolumeTreeNode =
        std::remove_pointer_t<decltype(((Particle*)nullptr)->GetNode())>;
    using MediumInterface = typename VolumeTreeNode::IModelProperties;
    // we only want fully configured objects
ralfulrich's avatar
ralfulrich committed
    /**
     * Cascade class cannot be default constructed, but needs a valid
     * list of physics processes for configuration at construct time.
     */
ralfulrich's avatar
ralfulrich committed
    Cascade(corsika::environment::Environment<MediumInterface> const& env, TTracking& tr,
            TProcessList& pl, TStack& stack)
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
        : fEnvironment(env)
        , fTracking(tr)
        , fProcessSequence(pl)
Ralf Ulrich's avatar
Ralf Ulrich committed
        , fStack(stack) {}
ralfulrich's avatar
ralfulrich committed
    /**
     * The Init function is called before the actual cascade simulations.
     * All components of the Cascade simulation must be configured here.
     */
      fProcessSequence.Init();
      fStack.Init();
    /**
     * set the nodes for all particles on the stack according to their numerical
     * position
     */
    void SetNodes() {
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
      std::for_each(fStack.begin(), fStack.end(), [&](auto& p) {
        auto const* numericalNode =
            fEnvironment.GetUniverse()->GetContainingNode(p.GetPosition());
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
        p.SetNode(numericalNode);
      });
ralfulrich's avatar
ralfulrich committed
    /**
     * 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()) {
        }
        // do cascade equations, which can put new particles on Stack,
        // thus, the double loop
ralfulrich's avatar
ralfulrich committed
        // DoCascadeEquations();
ralfulrich's avatar
ralfulrich committed
  private:
ralfulrich's avatar
ralfulrich committed
    /**
     * 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.
     */
ralfulrich's avatar
ralfulrich committed
    void Step(Particle& vParticle) {
      using namespace corsika;
ralfulrich's avatar
ralfulrich committed
      using namespace corsika::units::si;
      // determine geometric tracking
ralfulrich's avatar
ralfulrich committed
      auto [step, geomMaxLength, nextVol] = fTracking.GetTrack(vParticle);
      [[maybe_unused]] auto const& dummy_nextVol = nextVol;

      // determine combined total interaction length (inverse)
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
      InverseGrammageType const total_inv_lambda =
          fProcessSequence.GetTotalInverseInteractionLength(vParticle);
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
      // sample random exponential step length in grammage
      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
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
      LengthType const distance_interact =
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
          currentLogicalNode->GetModelProperties().ArclengthFromGrammage(step,
                                                                         next_interact);
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed

      // determine the maximum geometric step length
ralfulrich's avatar
ralfulrich committed
      LengthType const distance_max = fProcessSequence.MaxStepLength(vParticle, step);
      std::cout << "distance_max=" << distance_max << std::endl;

      // determine combined total inverse decay time
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
      InverseTimeType const total_inv_lifetime =
ralfulrich's avatar
ralfulrich committed
          fProcessSequence.GetTotalInverseLifetime(vParticle);
      // sample random exponential 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]
ralfulrich's avatar
ralfulrich committed
      LengthType const distance_decay = next_decay * vParticle.GetMomentum().norm() /
                                        vParticle.GetEnergy() * units::constants::c;

      // take minimum of geometry, interaction, decay for next step
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
      auto const min_distance =
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
          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:
ralfulrich's avatar
ralfulrich committed
      // std::visit(setup::ParticleUpdate<Particle>{vParticle}, step);
      vParticle.SetPosition(step.PositionFromArclength(min_distance));
      // .... also update time, momentum, direction, ...
ralfulrich's avatar
ralfulrich committed
      vParticle.SetTime(vParticle.GetTime() + min_distance / units::constants::c);

      // apply all continuous processes on particle + track
ralfulrich's avatar
ralfulrich committed
      process::EProcessReturn status = fProcessSequence.DoContinuous(vParticle, step);
ralfulrich's avatar
ralfulrich committed
      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 ? "
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
                << ((min_distance < geomMaxLength) ? "yes" : "no") << std::endl;
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
      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)

        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

Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
        } 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
ralfulrich's avatar
ralfulrich committed
      } else {               // boundary crossing, step is limited by volume boundary
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
        std::cout << "boundary crossing! next node = " << nextVol << std::endl;
        // DoBoundary may delete the particle (or not)
        fProcessSequence.DoBoundaryCrossing(vParticle, *currentLogicalNode, *nextVol);
ralfulrich's avatar
ralfulrich committed

    corsika::environment::Environment<MediumInterface> const& fEnvironment;
ralfulrich's avatar
ralfulrich committed
    TTracking& fTracking;
    TProcessList& fProcessSequence;
    TStack& fStack;
    corsika::random::RNG& fRNG =
Maximilian Reininghaus's avatar
Maximilian Reininghaus committed
        corsika::random::RNGManager::GetInstance().GetRandomStream("cascade");
  }; // namespace corsika::cascade
} // namespace corsika::cascade