IAP GITLAB

Skip to content
Snippets Groups Projects
corsika.cpp 15.4 KiB
Newer Older
ralfulrich's avatar
ralfulrich committed
/*
 * (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.
 */

/* clang-format off */
// InteractionCounter used boost/histogram, which
// fails if boost/type_traits have been included before. Thus, we have
// to include it first...
#include <corsika/framework/process/InteractionCounter.hpp>
/* clang-format on */
#include <corsika/framework/process/ProcessSequence.hpp>
#include <corsika/framework/process/SwitchProcessSequence.hpp>
#include <corsika/framework/process/InteractionCounter.hpp>
ralfulrich's avatar
ralfulrich committed
#include <corsika/framework/geometry/Plane.hpp>
#include <corsika/framework/geometry/Sphere.hpp>
#include <corsika/framework/geometry/PhysicalGeometry.hpp>
#include <corsika/framework/core/Logging.hpp>
ralfulrich's avatar
ralfulrich committed
#include <corsika/framework/core/PhysicalUnits.hpp>
#include <corsika/framework/core/Cascade.hpp>
ralfulrich's avatar
ralfulrich committed
#include <corsika/framework/core/EnergyMomentumOperations.hpp>
#include <corsika/framework/utility/SaveBoostHistogram.hpp>
#include <corsika/framework/utility/CorsikaFenv.hpp>
#include <corsika/framework/random/RNGManager.hpp>
ralfulrich's avatar
ralfulrich committed

#include <corsika/output/OutputManager.hpp>
ralfulrich's avatar
ralfulrich committed
#include <corsika/modules/writers/SubWriter.hpp>
#include <corsika/modules/writers/EnergyLossWriter.hpp>
ralfulrich's avatar
ralfulrich committed
#include <corsika/modules/writers/LongitudinalWriter.hpp>
ralfulrich's avatar
ralfulrich committed

#include <corsika/media/Environment.hpp>
#include <corsika/media/FlatExponential.hpp>
#include <corsika/media/HomogeneousMedium.hpp>
#include <corsika/media/IMagneticFieldModel.hpp>
#include <corsika/media/LayeredSphericalAtmosphereBuilder.hpp>
#include <corsika/media/NuclearComposition.hpp>
#include <corsika/media/MediumPropertyModel.hpp>
#include <corsika/media/UniformMagneticField.hpp>
#include <corsika/media/ShowerAxis.hpp>
ralfulrich's avatar
ralfulrich committed
#include <corsika/media/CORSIKA7Atmospheres.hpp>
ralfulrich's avatar
ralfulrich committed

#include <corsika/modules/BetheBlochPDG.hpp>
#include <corsika/modules/LongitudinalProfile.hpp>
#include <corsika/modules/ObservationPlane.hpp>
#include <corsika/modules/OnShellCheck.hpp>
#include <corsika/modules/StackInspector.hpp>
#include <corsika/modules/TrackWriter.hpp>
#include <corsika/modules/ParticleCut.hpp>
#include <corsika/modules/Pythia8.hpp>
#include <corsika/modules/Sibyll.hpp>
ralfulrich's avatar
ralfulrich committed
#include <corsika/modules/Epos.hpp>
ralfulrich's avatar
ralfulrich committed
#include <corsika/modules/UrQMD.hpp>
#include <corsika/modules/PROPOSAL.hpp>
#include <corsika/modules/QGSJetII.hpp>

#include <corsika/setup/SetupStack.hpp>
#include <corsika/setup/SetupTrajectory.hpp>

#include <CLI/App.hpp>
#include <CLI/Formatter.hpp>
#include <CLI/Config.hpp>

ralfulrich's avatar
ralfulrich committed
#include <iomanip>
#include <limits>
#include <string>

/*
  NOTE, WARNING, ATTENTION

  The .../Random.hpppp implement the hooks of external modules to the C8 random
  number generator. It has to occur excatly ONCE per linked
  executable. If you include the header below multiple times and
  link this togehter, it will fail.
 */
ralfulrich's avatar
ralfulrich committed
#include <corsika/modules/Random.hpp>
ralfulrich's avatar
ralfulrich committed

using namespace corsika;
using namespace std;

using Particle = setup::Stack::particle_type;

void registerRandomStreams(int seed) {
  RNGManager<>::getInstance().registerRandomStream("cascade");
  RNGManager<>::getInstance().registerRandomStream("qgsjet");
  RNGManager<>::getInstance().registerRandomStream("sibyll");
ralfulrich's avatar
ralfulrich committed
  RNGManager<>::getInstance().registerRandomStream("epos");
  RNGManager<>::getInstance().registerRandomStream("pythia");
  RNGManager<>::getInstance().registerRandomStream("urqmd");
  RNGManager<>::getInstance().registerRandomStream("proposal");
  if (seed == 0) {
    std::random_device rd;
    seed = rd();
ralfulrich's avatar
ralfulrich committed
    CORSIKA_LOG_INFO("random seed (auto) {} ", seed);
  } else {
    CORSIKA_LOG_INFO("random seed {} ", seed);
  }
  RNGManager<>::getInstance().setSeed(seed);
ralfulrich's avatar
ralfulrich committed
}

template <typename T>
using MyExtraEnv = MediumPropertyModel<UniformMagneticField<T>>;

int main(int argc, char** argv) {

  // the main command line description
  CLI::App app{"Simulate standard (downgoing) showers with CORSIKA 8."};

  // some options that we want to fill in
  int A, Z, nevent = 0;

  // the following section adds the options to the parser

  // we start by definining a sub-group for the primary ID
  auto opt_Z = app.add_option("-Z", Z, "Atomic number for primary")
                   ->check(CLI::Range(0, 26))
                   ->group("Primary");
  auto opt_A = app.add_option("-A", A, "Atomic mass number for primary")
                   ->needs(opt_Z)
                   ->check(CLI::Range(1, 58))
                   ->group("Primary");
  app.add_option("-p,--pdg", "PDG code for primary.")
      ->excludes(opt_A)
      ->excludes(opt_Z)
      ->group("Primary");
  // the remainding options
  app.add_option("-E,--energy", "Primary energy in GeV")
      ->required()
      ->check(CLI::PositiveNumber)
      ->group("Primary");
  app.add_option("-z,--zenith", "Primary zenith angle (deg)")
      ->required()
      ->default_val(0.)
      ->check(CLI::Range(0, 90))
      ->group("Primary");
  app.add_option("-a,--azimuth", "Primary azimuth angle (deg)")
      ->default_val(0.)
      ->check(CLI::Range(0, 360))
      ->group("Primary");
  app.add_option("-N,--nevent", nevent, "The number of events/showers to run.")
      ->required()
      ->check(CLI::PositiveNumber)
      ->group("Library/Output");
  app.add_option("-f,--filename", "Filename for output library.")
      ->required()
      ->default_val("corsika_library")
      ->check(CLI::NonexistentPath)
      ->group("Library/Output");
  app.add_option("-s,--seed", "The random number seed.")
ralfulrich's avatar
ralfulrich committed
      ->default_val(0)
      ->check(CLI::NonNegativeNumber)
      ->group("Misc.");
ralfulrich's avatar
ralfulrich committed
  bool force_interaction = false;
  app.add_flag("--force-interaction", force_interaction,
               "Force the location of the first interaction.")
      ->group("Misc.");
ralfulrich's avatar
ralfulrich committed
  app.add_option("-v,--verbosity", "Verbosity level: warn, info, debug, trace.")
      ->default_val("info")
      ->check(CLI::IsMember({"warn", "info", "debug", "trace"}))
      ->group("Misc.");

  // parse the command line options into the variables
  CLI11_PARSE(app, argc, argv);

ralfulrich's avatar
ralfulrich committed
  if (app.count("--verbosity")) {
    string const loglevel = app["verbosity"]->as<string>();
    if (loglevel == "warn") {
      logging::set_level(logging::level::warn);
    } else if (loglevel == "info") {
      logging::set_level(logging::level::info);
    } else if (loglevel == "debug") {
      logging::set_level(logging::level::debug);
    } else if (loglevel == "trace") {
ralfulrich's avatar
ralfulrich committed
#ifndef DEBUG
ralfulrich's avatar
ralfulrich committed
      CORSIKA_LOG_ERROR("trace log level requires a Debug build.");
      return 1;
ralfulrich's avatar
ralfulrich committed
#endif
ralfulrich's avatar
ralfulrich committed
      logging::set_level(logging::level::trace);
    }
ralfulrich's avatar
ralfulrich committed
  }

  // check that we got either PDG or A/Z
  // this can be done with option_groups but the ordering
  // gets all messed up
  if (app.count("--pdg") == 0) {
    if ((app.count("-A") == 0) || (app.count("-Z") == 0)) {
ralfulrich's avatar
ralfulrich committed
      CORSIKA_LOG_ERROR("If --pdg is not provided, then both -A and -Z are required.");
ralfulrich's avatar
ralfulrich committed
  }

  // initialize random number sequence(s)
  registerRandomStreams(app["--seed"]->as<int>());
  /* === START: SETUP ENVIRONMENT AND ROOT COORDINATE SYSTEM === */
ralfulrich's avatar
ralfulrich committed
  using EnvType = setup::Environment;
  EnvType env;
  CoordinateSystemPtr const& rootCS = env.getCoordinateSystem();
  Point const center{rootCS, 0_m, 0_m, 0_m};

ralfulrich's avatar
ralfulrich committed
  // build a Linsley US Standard atmosphere into `env`
  create_5layer_atmosphere<setup::EnvironmentInterface, MyExtraEnv>(
      env, AtmosphereId::LinsleyUSStd, center, Medium::AirDry1Atm,
      MagneticFieldVector{rootCS, 0_T, 50_uT, 0_T});

  /* === END: SETUP ENVIRONMENT AND ROOT COORDINATE SYSTEM === */

ralfulrich's avatar
ralfulrich committed
  ofstream atmout("earth.dat");
ralfulrich's avatar
ralfulrich committed
  for (LengthType h = 0_m; h < 110_km; h += 100_m) {
ralfulrich's avatar
ralfulrich committed
    Point const ptest{rootCS, 0_m, 0_m, constants::EarthRadius::Mean + h};
ralfulrich's avatar
ralfulrich committed
    auto rho =
        env.getUniverse()->getContainingNode(ptest)->getModelProperties().getMassDensity(
            ptest);
    atmout << h / 1_m << " " << rho / 1_kg * cube(1_m) << "\n";
  }
  atmout.close();

  /* === START: CONSTRUCT PRIMARY PARTICLE === */

  // parse the primary ID as a PDG or A/Z code
ralfulrich's avatar
ralfulrich committed
  Code beamCode;

  // check if we want to use a PDG code instead
  if (app.count("--pdg") > 0) {
    beamCode = convert_from_PDG(PDGCode(app["--pdg"]->as<int>()));
  } else {
    // check manually for proton and neutrons
ralfulrich's avatar
ralfulrich committed
    if ((A == 1) && (Z == 1))
ralfulrich's avatar
ralfulrich committed
      beamCode = Code::Proton;
ralfulrich's avatar
ralfulrich committed
    else if ((A == 1) && (Z == 0))
ralfulrich's avatar
ralfulrich committed
      beamCode = Code::Neutron;
    else
      beamCode = get_nucleus_code(A, Z);
ralfulrich's avatar
ralfulrich committed
  HEPEnergyType mass = get_mass(beamCode);
  // particle energy
ralfulrich's avatar
ralfulrich committed
  HEPEnergyType const E0 = 1_GeV * app["--energy"]->as<double>();

  // direction of the shower in (theta, phi) space
ralfulrich's avatar
ralfulrich committed
  auto const thetaRad = app["--zenith"]->as<double>() / 180. * M_PI;
  auto const phiRad = app["--azimuth"]->as<double>() / 180. * M_PI;

  // convert Elab to Plab
ralfulrich's avatar
ralfulrich committed
  HEPMomentumType P0 = calculate_momentum(E0, mass);

  // convert the momentum to the zenith and azimuth angle of the primary
  auto const [px, py, pz] =
      std::make_tuple(P0 * sin(thetaRad) * cos(phiRad), P0 * sin(thetaRad) * sin(phiRad),
                      -P0 * cos(thetaRad));
ralfulrich's avatar
ralfulrich committed
  auto plab = MomentumVector(rootCS, {px, py, pz});
  /* === END: CONSTRUCT PRIMARY PARTICLE === */
  /* === START: CONSTRUCT GEOMETRY === */
ralfulrich's avatar
ralfulrich committed
  auto const observationHeight = 0_km + constants::EarthRadius::Mean;
  auto const injectionHeight = 111.75_km + constants::EarthRadius::Mean;
ralfulrich's avatar
ralfulrich committed
  auto const t = -observationHeight * cos(thetaRad) +
                 sqrt(-static_pow<2>(sin(thetaRad) * observationHeight) +
                      static_pow<2>(injectionHeight));
  Point const showerCore{rootCS, 0_m, 0_m, observationHeight};
  Point const injectionPos =
      showerCore + DirectionVector{rootCS,
                                   {-sin(thetaRad) * cos(phiRad),
                                    -sin(thetaRad) * sin(phiRad), cos(thetaRad)}} *
                       t;

  // we make the axis much longer than the inj-core distance since the
  // profile will go beyond the core, depending on zenith angle
  ShowerAxis const showerAxis{injectionPos, (showerCore - injectionPos) * 1.2, env};
  /* === END: CONSTRUCT GEOMETRY === */
ralfulrich's avatar
ralfulrich committed

  // create the output manager that we then register outputs with
  OutputManager output(app["--filename"]->as<std::string>());
ralfulrich's avatar
ralfulrich committed
  // register energy losses as output
ralfulrich's avatar
ralfulrich committed
  EnergyLossWriter dEdX{showerAxis, 10_g / square(1_cm), 200};
ralfulrich's avatar
ralfulrich committed
  output.add("energyloss", dEdX);

  // create a track writer and register it with the output manager
ralfulrich's avatar
ralfulrich committed
  TrackWriter tracks;
ralfulrich's avatar
ralfulrich committed
  output.add("tracks", tracks);
ralfulrich's avatar
ralfulrich committed
  corsika::sibyll::Interaction sibyll;
  InteractionCounter sibyllCounted(sibyll);
  corsika::sibyll::NuclearInteraction sibyllNuc(sibyll, env);
  InteractionCounter sibyllNucCounted(sibyllNuc);
ralfulrich's avatar
ralfulrich committed
  auto heModelCounted = make_sequence(sibyllNucCounted, sibyllCounted);
ralfulrich's avatar
ralfulrich committed

  corsika::pythia8::Decay decayPythia;

  // use sibyll decay routine for decays of particles unknown to pythia
  corsika::sibyll::Decay decaySibyll{{
      Code::N1440Plus,
      Code::N1440MinusBar,
      Code::N1440_0,
      Code::N1440_0Bar,
      Code::N1710Plus,
      Code::N1710MinusBar,
      Code::N1710_0,
      Code::N1710_0Bar,

      Code::Pi1300Plus,
      Code::Pi1300Minus,
      Code::Pi1300_0,

      Code::KStar0_1430_0,
      Code::KStar0_1430_0Bar,
      Code::KStar0_1430_Plus,
      Code::KStar0_1430_MinusBar,
  }};

ralfulrich's avatar
ralfulrich committed
  // decaySibyll.printDecayConfig();
ralfulrich's avatar
ralfulrich committed
  HEPEnergyType const emcut = 50_GeV;
  HEPEnergyType const hadcut = 50_GeV;
ralfulrich's avatar
ralfulrich committed
  ParticleCut<SubWriter<decltype(dEdX)>> cut(emcut, emcut, hadcut, hadcut, true, dEdX);
ralfulrich's avatar
ralfulrich committed

ralfulrich's avatar
ralfulrich committed
  corsika::proposal::Interaction emCascade(env);
ralfulrich's avatar
ralfulrich committed
  // NOT available for PROPOSAL due to interface trouble:
ralfulrich's avatar
ralfulrich committed
  // InteractionCounter emCascadeCounted(emCascade);
ralfulrich's avatar
ralfulrich committed
  // corsika::proposal::ContinuousProcess<SubWriter<decltype(dEdX)>> emContinuous(env);
  BetheBlochPDG<SubWriter<decltype(dEdX)>> emContinuous{dEdX};
ralfulrich's avatar
ralfulrich committed
  LongitudinalWriter profile{showerAxis, 10_g / square(1_cm), 200};
  output.add("profile", profile);
  LongitudinalProfile<SubWriter<decltype(profile)>> longprof{profile};
ralfulrich's avatar
ralfulrich committed

  corsika::urqmd::UrQMD urqmd;
ralfulrich's avatar
ralfulrich committed
  InteractionCounter urqmdCounted(urqmd);
ralfulrich's avatar
ralfulrich committed
  StackInspector<setup::Stack> stackInspect(10000, false, E0);
ralfulrich's avatar
ralfulrich committed

  // assemble all processes into an ordered process list
  struct EnergySwitch {
    HEPEnergyType cutE_;
    EnergySwitch(HEPEnergyType cutE)
        : cutE_(cutE) {}
ralfulrich's avatar
ralfulrich committed
    bool operator()(const Particle& p) const { return (p.getKineticEnergy() < cutE_); }
ralfulrich's avatar
ralfulrich committed
  auto hadronSequence = make_select(EnergySwitch(63.1_GeV), urqmdCounted, heModelCounted);
ralfulrich's avatar
ralfulrich committed
  auto decaySequence = make_sequence(decayPythia, decaySibyll);

ralfulrich's avatar
ralfulrich committed
  TrackWriter trackWriter{tracks};
  // observation plane
ralfulrich's avatar
ralfulrich committed
  Plane const obsPlane(showerCore, DirectionVector(rootCS, {0., 0., 1.}));
ralfulrich's avatar
ralfulrich committed
  ObservationPlane<setup::Tracking, ParticleWriterParquet> observationLevel{
      obsPlane, DirectionVector(rootCS, {1., 0., 0.})};
  // register ground particle output
  output.add("particles", observationLevel);
  // assemble the final process sequence
ralfulrich's avatar
ralfulrich committed
  auto sequence = make_sequence(stackInspect, hadronSequence, decaySequence, cut,
                                emCascade, emContinuous, // trackWriter,
ralfulrich's avatar
ralfulrich committed
                                observationLevel, longprof);
  /* === END: SETUP PROCESS LIST === */
  // create the cascade object using the default stack and tracking implementation
ralfulrich's avatar
ralfulrich committed
  setup::Tracking tracking;
  setup::Stack stack;
  Cascade EAS(env, tracking, sequence, output, stack);

  // print our primary parameters all in one place
  if (app["--pdg"]->count() > 0) {
    CORSIKA_LOG_INFO("Primary PDG ID: {}", app["--pdg"]->as<int>());
  } else {
    CORSIKA_LOG_INFO("Primary Z/A: {}/{}", Z, A);
  }
  CORSIKA_LOG_INFO("Primary Energy: {}", E0);
  CORSIKA_LOG_INFO("Primary Momentum: {}", P0);
  CORSIKA_LOG_INFO("Point of Injection: {}", injectionPos.getCoordinates());
  CORSIKA_LOG_INFO("Shower Axis Length: {}", (showerCore - injectionPos).getNorm() * 1.2);

  // trigger the output manager to open the library for writing
ralfulrich's avatar
ralfulrich committed
  output.startOfLibrary();

  // loop over each shower
  for (int i_shower = 1; i_shower < nevent + 1; i_shower++) {

    CORSIKA_LOG_INFO("Shower {} / {} ", i_shower, nevent);
ralfulrich's avatar
ralfulrich committed

    // directory for outputs
ralfulrich's avatar
ralfulrich committed
    string const outdir(app["--filename"]->as<std::string>());
    string const labHist_file = outdir + "/inthist_lab_" + to_string(i_shower) + ".npz";
    string const cMSHist_file = outdir + "/inthist_cms_" + to_string(i_shower) + ".npz";
ralfulrich's avatar
ralfulrich committed

    // setup particle stack, and add primary particle
    stack.clear();

    // add the desired particle to the stack
ralfulrich's avatar
ralfulrich committed
    stack.addParticle(std::make_tuple(
        beamCode, calculate_kinetic_energy(plab.getNorm(), get_mass(beamCode)),
        plab.normalized(), injectionPos, 0_ns));
    // if we want to fix the first location of the shower
ralfulrich's avatar
ralfulrich committed
    if (force_interaction) {
      CORSIKA_LOG_INFO("Fixing first interaction at injection point.");
      EAS.forceInteraction();
    }
    // run the shower
ralfulrich's avatar
ralfulrich committed
    EAS.run();

ralfulrich's avatar
ralfulrich committed
    HEPEnergyType const Efinal =
        dEdX.getEnergyLost() + observationLevel.getEnergyGround();

    CORSIKA_LOG_INFO(
        "total energy budget (GeV): {} (dEdX={} ground={}), "
        "relative difference (%): {}",
        Efinal / 1_GeV, dEdX.getEnergyLost() / 1_GeV,
        observationLevel.getEnergyGround() / 1_GeV, (Efinal / E0 - 1) * 100);
ralfulrich's avatar
ralfulrich committed
    // auto const hists = heModelCounted.getHistogram() + urqmdCounted.getHistogram();
ralfulrich's avatar
ralfulrich committed
    auto const hists = sibyllCounted.getHistogram() + sibyllNucCounted.getHistogram() +
                       urqmdCounted.getHistogram();

    save_hist(hists.labHist(), labHist_file, true);
    save_hist(hists.CMSHist(), cMSHist_file, true);
  }

  // and finalize the output on disk
ralfulrich's avatar
ralfulrich committed
  output.endOfLibrary();
}