From e367193c135326783ec2db27ff833813dab9556c Mon Sep 17 00:00:00 2001 From: Maximilian Reininghaus <maximilian.reininghaus@kit.edu> Date: Tue, 24 May 2022 18:21:43 +0200 Subject: [PATCH] const-correctness, changed a few names, renamed file --- examples/environment.cpp | 77 ++++++++++++++++++++++++++++++++ examples/environment_example.cpp | 71 ----------------------------- 2 files changed, 77 insertions(+), 71 deletions(-) create mode 100644 examples/environment.cpp delete mode 100644 examples/environment_example.cpp diff --git a/examples/environment.cpp b/examples/environment.cpp new file mode 100644 index 000000000..406b475ae --- /dev/null +++ b/examples/environment.cpp @@ -0,0 +1,77 @@ +/* + * (c) Copyright 2022 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/media/Environment.hpp> +#include <corsika/media/HomogeneousMedium.hpp> +#include <corsika/media/IMediumModel.hpp> +#include <corsika/media/MediumProperties.hpp> +#include <corsika/media/MediumPropertyModel.hpp> + +using namespace corsika; + +// Example to show how to construct an environment in CORSIKA + +// MyMediumInterface is constructed via "mixin inheritance" +// Here, we construct our base class with IMediumModel and IMediumPropertyModel: +// the former allow us define a density profile and nuclear composite, +// the later is used to determine energy losses parameters. +using MyMediumInterface = IMediumPropertyModel<IMediumModel>; + +using MyEnvType = Environment<MyMediumInterface>; + +int main() { + // create environment and universe, empty so far + MyEnvType env; + + // create a geometry object: a sphere with radius of 1000Â m + CoordinateSystemPtr const& rootCS = env.getCoordinateSystem(); + Point const center{rootCS, 0_m, 0_m, 0_m}; + LengthType const radius = 1000_m; + auto sphere = std::make_unique<Sphere>(center, radius); + + // create node from geometry object + auto node = std::make_unique<VolumeTreeNode<MyMediumInterface>>(std::move(sphere)); + + // set medium properties to our node, say it is water + NuclearComposition const nucl_comp{{Code::Hydrogen, Code::Oxygen}, {0.11, 0.89}}; + MassDensityType const density = 1_g / (1_cm * 1_cm * 1_cm); + + // create concrete implementation of MyMediumInterface by combining parts that + // implement the corresponding interfaces. HomogeneousMedium implements IMediumModel, + // MediumPropertyModel implements IMediumPropertyModel. + auto const medium = + std::make_shared<MediumPropertyModel<HomogeneousMedium<MyMediumInterface>>>( + Medium::WaterLiquid, density, nucl_comp); + node->setModelProperties(medium); + + // put our node into universe + // node: this has to be down after setting node model properties, since + // std::move will make our previous defined node, which is a unique pointer + // un-referencable in the context + VolumeTreeNode<MyMediumInterface>* const universe = env.getUniverse().get(); + universe->addChild(std::move(node)); + + // example to explore the media properties of the node + for (auto h = 0_m; h < radius; h += 100_m) { + Point const ptest{rootCS, 0_m, 0_m, h}; + MyMediumInterface const& media_prop = + env.getUniverse()->getContainingNode(ptest)->getModelProperties(); + MassDensityType const rho = media_prop.getMassDensity(ptest); + NuclearComposition const& nuc_comp = media_prop.getNuclearComposition(); + std::vector<Code> const& nuclei = nuc_comp.getComponents(); + std::vector<double> const& fractions = nuc_comp.getFractions(); + CORSIKA_LOG_INFO( + "radius: {:.2f} m, density: {:.2f} g/cm^3, nuclei: ({:d}, {:d}), fractions: " + "({:.2f}, " + "{:.2f})", + h / 1_m, rho / (1_g / static_pow<3>(1_cm)), get_PDG(nuclei.at(0)), + get_PDG(nuclei.at(1)), fractions.at(0), fractions.at(1)); + } + + return 0; +} diff --git a/examples/environment_example.cpp b/examples/environment_example.cpp deleted file mode 100644 index 99ae9d866..000000000 --- a/examples/environment_example.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * (c) Copyright 2022 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/media/Environment.hpp> -#include <corsika/media/HomogeneousMedium.hpp> -#include <corsika/media/IMediumModel.hpp> -#include <corsika/media/MediumProperties.hpp> -#include <corsika/media/MediumPropertyModel.hpp> - -using namespace corsika; - -// Example to show how to construct an environment in CORSIKA - -// MediumType is constructed via "mixin inheritance" -// Here, we construct our base class with IMediumModel and IMediumPropertyModel: -// the former allow us define a density profile and nuclear composite, -// the later is used to determine energy losses parameters. -using IMediumType = IMediumPropertyModel<IMediumModel>; - -using EnvType = Environment<IMediumType>; - -int main() { - // define the environment and universe - EnvType env; - VolumeTreeNode<IMediumType>* universe = env.getUniverse().get(); - - // create a geometry object: a sphere with radius of 1000m - CoordinateSystemPtr const& rootCS = env.getCoordinateSystem(); - Point const center{rootCS, 0_m, 0_m, 0_m}; - LengthType radius = 1000_m; - auto sphere = std::make_unique<Sphere>(center, radius); - - // create node from geometry object - auto node = std::make_unique<VolumeTreeNode<IMediumType>>(std::move(sphere)); - - // set medium properties to our node, say it is water - auto nuc_comp = NuclearComposition({{Code::Hydrogen, Code::Oxygen}, {0.11, 0.89}}); - MassDensityType density = 1_g / (1_cm * 1_cm * 1_cm); - auto medium = std::make_shared<MediumPropertyModel<HomogeneousMedium<IMediumType>>>( - Medium::WaterLiquid, density, nuc_comp); - node->setModelProperties(medium); - - // put our node into universe - // node: this has to be down after setting node model properties, since - // std::move will make our previous defined node, which is a unique pointer - // un-referencable in the context - universe->addChild(std::move(node)); - - // example to explore the media properties of the node - for (LengthType h = 0_m; h < radius; h += 100_m) { - Point const ptest{rootCS, 0_m, 0_m, h}; - IMediumType const& media_prop_ = - env.getUniverse()->getContainingNode(ptest)->getModelProperties(); - MassDensityType rho_ = media_prop_.getMassDensity(ptest); - NuclearComposition nuc_comp_ = media_prop_.getNuclearComposition(); - std::vector<Code> nucs_ = nuc_comp_.getComponents(); - std::vector<double> fracs_ = nuc_comp_.getFractions(); - CORSIKA_LOG_INFO( - "radius: {:.2f} m, density: {:.2f} g/cm^3, nucs: ({:d}, {:d}), fracs: ({:.2f}, " - "{:.2f})", - h / 1_m, rho_ / 1_g * (1_cm * 1_cm * 1_cm), get_PDG(nucs_[0]), get_PDG(nucs_[1]), - fracs_[0], fracs_[1]); - } - - return 0; -} \ No newline at end of file -- GitLab