Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TestEm3 Stepper and default RNG to XORWOW #427

Merged
merged 5 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ if(DEFINED CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE)
endif()

# RNG selection
set(CELERITAS_RNG_OPTIONS)
set(CELERITAS_RNG_OPTIONS XORWOW)
if(CELERITAS_USE_CUDA)
list(APPEND CELERITAS_RNG_OPTIONS CURAND)
elseif(CELERITAS_USE_HIP)
Expand All @@ -107,7 +107,6 @@ elseif(NOT (CELERITAS_USE_CUDA OR CELERITAS_USE_HIP))
# TODO: allow wrapper to standard library RNG when not building for device?
# TODO: maybe even add wrapper to Geant4 RNG??
endif()
list(APPEND CELERITAS_RNG_OPTIONS XORWOW)
list(GET CELERITAS_RNG_OPTIONS 0 _DEFAULT_RNG)
set(CELERITAS_RNG "${_DEFAULT_RNG}" CACHE STRING
"Celeritas runtime random number generator")
Expand Down
2 changes: 1 addition & 1 deletion app/demo-loop/LDemoIO.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ TransporterInput load_input(const LDemoArgs& args)
// the geometry (possibly using a ROOT/GDML input with an ORANGE
// geometry): try to let the GeoMaterialParams remap them
CELER_LOG(warning) << "Volume/material mapping is inconsistent "
"between ROOT file and geometry file: "
"between Geant4 data and geometry file: "
"attempting to remap";
input.volume_names.resize(imported_data.volumes.size());
for (auto volume_idx : range(imported_data.volumes.size()))
Expand Down
10 changes: 6 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ configure_file("celeritas_config.h.in" "celeritas_config.h" @ONLY)
#----------------------------------------------------------------------------#

# TODO: add build flags
set(CELERITAS_CMAKE_STRINGS)
set(CELERITAS_BUILD_TYPE ${CMAKE_BUILD_TYPE})
foreach(_var CELERITAS_RNG CELERITAS_BUILD_TYPE)
set(CELERITAS_CMAKE_STRINGS
"static const char celeritas_rng[] = \"${CELERITAS_RNG}\";\n"
)
set(BUILD_TYPE ${CMAKE_BUILD_TYPE})
foreach(_var BUILD_TYPE CLHEP_VERSION Geant4_VERSION)
string(TOLOWER "${_var}" _lower)
string(APPEND CELERITAS_CMAKE_STRINGS
"static const char ${_lower}[] = \"${${_var}}\";\n"
"static const char celeritas_${_lower}[] = \"${${_var}}\";\n"
)
endforeach()
configure_file("celeritas_cmake_strings.h.in" "celeritas_cmake_strings.h" @ONLY)
Expand Down
6 changes: 6 additions & 0 deletions src/celeritas/ext/GeantSetup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <FTFP_BERT.hh>
#include <G4EmParameters.hh>
#include <G4GenericPhysicsList.hh>
#include <G4ParticleTable.hh>
#include <G4UImanager.hh>
#include <G4VModularPhysicsList.hh>

Expand All @@ -41,6 +42,11 @@ GeantSetup::GeantSetup(const std::string& gdml_filename, Options options)
{
// Run manager writes output that cannot be redirected...
ScopedTimeAndRedirect scoped_time("G4RunManager");
detail::GeantExceptionHandler scoped_exception_handler;
// Access the particle table before creating the run manager, so that
// missing environment variables like G4ENSDFSTATEDATA get caught
// cleanly rather than segfaulting
G4ParticleTable::GetParticleTable();
#if CELERITAS_G4_V10
run_manager_.reset(new G4RunManager);
#else
Expand Down
2 changes: 2 additions & 0 deletions src/celeritas/global/Stepper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Stepper<M>::Stepper(Input input)
CELER_EXPECT(params_);
CELER_VALIDATE(input.num_track_slots > 0,
<< "number of track slots has not been set");
CELER_VALIDATE(input.num_initializers > 0,
<< "number of initializers has not been set");
states_ = CollectionStateStore<CoreStateData, M>(*params_,
input.num_track_slots);

Expand Down
5 changes: 5 additions & 0 deletions src/corecel/io/BuildOutput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ void BuildOutput::output(JsonPimpl* j) const
# undef CO_SAVE_CFG
cfg["CELERITAS_BUILD_TYPE"] = celeritas_build_type;
cfg["CELERITAS_RNG"] = celeritas_rng;
if (CELERITAS_USE_GEANT4)
{
cfg["CLHEP_VERSION"] = celeritas_clhep_version;
cfg["Geant4_VERSION"] = celeritas_geant4_version;
}

obj["config"] = std::move(cfg);
}
Expand Down
16 changes: 11 additions & 5 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ if(NOT CELERITAS_USE_Geant4)
set(_needs_geant4 DISABLE)
else()
# Optional dependence on low-energy EM data
set(_ds G4EMLOW)
set(_optional_geant4_env
ENVIRONMENT "${Geant4_DATASET_${_ds}_ENVVAR}=${Geant4_DATASET_${_ds}_PATH}"
)
set(_geant4_test_env)
foreach(_ds G4EMLOW G4ENSDFSTATE)
list(APPEND _geant4_test_env
"${Geant4_DATASET_${_ds}_ENVVAR}=${Geant4_DATASET_${_ds}_PATH}"
)
endforeach()
set(_optional_geant4_env ENVIRONMENT "${_geant4_test_env}")
set(_optional_geant4_link
LINK_LIBRARIES ${Geant4_LIBRARIES}
)
Expand Down Expand Up @@ -205,9 +208,10 @@ add_library(CeleritasTest
celeritas/GlobalTestBase.cc
celeritas/MockTestBase.cc
celeritas/SimpleTestBase.cc
celeritas/TestEm3Base.cc
)
celeritas_target_link_libraries(CeleritasTest
PRIVATE celeritas CelerTest)
PRIVATE celeritas celeritas_io CelerTest)

#-----------------------------------------------------------------------------#
celeritas_setup_tests(SERIAL PREFIX celeritas LINK_LIBRARIES CeleritasTest)
Expand Down Expand Up @@ -282,6 +286,8 @@ celeritas_add_test(celeritas/geo/GeoMaterial.test.cc
# Global
set(CELERITASTEST_PREFIX celeritas/global)
celeritas_add_test(celeritas/global/ActionManager.test.cc)
celeritas_add_test(celeritas/global/Stepper.test.cc
GPU ${_needs_geant4})

#-------------------------------------#
# Grid
Expand Down
3 changes: 3 additions & 0 deletions test/celeritas/GlobalTestBase.hh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ namespace celeritas_test
//---------------------------------------------------------------------------//
/*!
* Lazily construct core parameters, individually or together.
*
* \note Inherit from this class using \c virtual \c public so that tests can
* create mixins (see e.g. \c SimpleStepperTest).
*/
class GlobalTestBase : public celeritas_test::Test
{
Expand Down
2 changes: 1 addition & 1 deletion test/celeritas/MockTestBase.hh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace celeritas_test
*
* Cutoff values are all zero.
*/
class MockTestBase : public GlobalTestBase
class MockTestBase : virtual public GlobalTestBase
{
public:
//!@{
Expand Down
2 changes: 1 addition & 1 deletion test/celeritas/SimpleTestBase.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace celeritas_test
/*!
* Compton scattering with gammas in mock aluminum in a box in hard vacuum.
*/
class SimpleTestBase : public GlobalTestBase
class SimpleTestBase : virtual public GlobalTestBase
{
public:
//!@{
Expand Down
156 changes: 156 additions & 0 deletions test/celeritas/TestEm3Base.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2022 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/TestEm3Base.cc
//---------------------------------------------------------------------------//
#include "TestEm3Base.hh"

#include <string>

#include "celeritas/em/process/BremsstrahlungProcess.hh"
#include "celeritas/em/process/ComptonProcess.hh"
#include "celeritas/em/process/EIonizationProcess.hh"
#include "celeritas/em/process/EPlusAnnihilationProcess.hh"
#include "celeritas/em/process/GammaConversionProcess.hh"
#include "celeritas/em/process/MultipleScatteringProcess.hh"
#include "celeritas/em/process/PhotoelectricProcess.hh"
#include "celeritas/ext/GeantImporter.hh"
#include "celeritas/ext/GeantSetup.hh"
#include "celeritas/geo/GeoMaterialParams.hh"
#include "celeritas/geo/GeoParams.hh"
#include "celeritas/global/ActionManager.hh"
#include "celeritas/io/ImportData.hh"
#include "celeritas/mat/MaterialParams.hh"
#include "celeritas/phys/CutoffParams.hh"
#include "celeritas/phys/ImportedProcessAdapter.hh"
#include "celeritas/phys/ParticleParams.hh"
#include "celeritas/phys/PhysicsParams.hh"
#include "celeritas/random/RngParams.hh"

using namespace celeritas;

namespace celeritas_test
{
//---------------------------------------------------------------------------//
namespace
{
//---------------------------------------------------------------------------//
std::string gdml_filename(const char* basename)
{
return std::string(basename) + std::string(".gdml");
}

//---------------------------------------------------------------------------//
ImportData load_import_data(std::string filename)
{
GeantSetupOptions options;
options.physics = GeantSetupPhysicsList::em_basic;
options.em_bins_per_decade = 14;
GeantImporter import(GeantSetup(filename, options));
return import();
}

//---------------------------------------------------------------------------//
} // namespace

//---------------------------------------------------------------------------//
// PROTECTED MEMBER FUNCTIONS
//---------------------------------------------------------------------------//
auto TestEm3Base::build_material() -> SPConstMaterial
{
return MaterialParams::from_import(this->imported_data());
}

//---------------------------------------------------------------------------//
auto TestEm3Base::build_geomaterial() -> SPConstGeoMaterial
{
GeoMaterialParams::Input input;
input.geometry = this->geometry();
input.materials = this->material();
const auto& imported = this->imported_data();

input.volume_to_mat.resize(imported.volumes.size());
input.volume_names.resize(imported.volumes.size());
for (auto volume_idx :
range<VolumeId::size_type>(input.volume_to_mat.size()))
{
input.volume_to_mat[volume_idx]
= MaterialId{imported.volumes[volume_idx].material_id};
input.volume_names[volume_idx] = imported.volumes[volume_idx].name;
}
return std::make_shared<GeoMaterialParams>(std::move(input));
}

//---------------------------------------------------------------------------//
auto TestEm3Base::build_particle() -> SPConstParticle
{
return ParticleParams::from_import(this->imported_data());
}

//---------------------------------------------------------------------------//
auto TestEm3Base::build_cutoff() -> SPConstCutoff
{
return CutoffParams::from_import(
this->imported_data(), this->particle(), this->material());
}

//---------------------------------------------------------------------------//
auto TestEm3Base::build_physics() -> SPConstPhysics
{
PhysicsParams::Input input;
input.materials = this->material();
input.particles = this->particle();
input.options = this->build_physics_options();
input.action_manager = this->action_mgr().get();

BremsstrahlungProcess::Options brem_options;
brem_options.combined_model = true;
brem_options.enable_lpm = true;

GammaConversionProcess::Options conv_options;
conv_options.enable_lpm = true;

auto process_data
= std::make_shared<ImportedProcesses>(this->imported_data().processes);
input.processes.push_back(
std::make_shared<ComptonProcess>(input.particles, process_data));
input.processes.push_back(std::make_shared<PhotoelectricProcess>(
input.particles, input.materials, process_data));
input.processes.push_back(std::make_shared<GammaConversionProcess>(
input.particles, process_data, conv_options));
input.processes.push_back(
std::make_shared<EPlusAnnihilationProcess>(input.particles));
input.processes.push_back(
std::make_shared<EIonizationProcess>(input.particles, process_data));
input.processes.push_back(std::make_shared<BremsstrahlungProcess>(
input.particles, input.materials, process_data, brem_options));
if (this->enable_msc())
{
input.processes.push_back(std::make_shared<MultipleScatteringProcess>(
input.particles, input.materials, process_data));
}
return std::make_shared<PhysicsParams>(std::move(input));
}

//---------------------------------------------------------------------------//
auto TestEm3Base::build_physics_options() const -> PhysicsOptions
{
PhysicsOptions options;
options.enable_fluctuation = false;
options.secondary_stack_factor = this->secondary_stack_factor();
return options;
}

//---------------------------------------------------------------------------//
// Lazily set up and load geant4
auto TestEm3Base::imported_data() const -> const ImportData&
{
static ImportData imported = load_import_data(this->test_data_path(
"celeritas", gdml_filename(this->geometry_basename()).c_str()));
return imported;
}

//---------------------------------------------------------------------------//
} // namespace celeritas_test
57 changes: 57 additions & 0 deletions test/celeritas/TestEm3Base.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2022 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/TestEm3Base.hh
//---------------------------------------------------------------------------//
#pragma once

#include "celeritas/Types.hh"

#include "GlobalTestBase.hh"

namespace celeritas
{
struct ImportData;
struct PhysicsParamsOptions;
} // namespace celeritas

namespace celeritas_test
{
//---------------------------------------------------------------------------//
/*!
* Test harness for replicating the AdePT TestEm3 input.
*
* This class requires Geant4 to import the data.
*/
class TestEm3Base : virtual public GlobalTestBase
{
public:
//!@{
//! Type aliases
using real_type = celeritas::real_type;
using ImportData = celeritas::ImportData;
using PhysicsOptions = celeritas::PhysicsParamsOptions;
//!@}

protected:
const char* geometry_basename() const override { return "testem3-flat"; }

virtual bool enable_msc() const { return false; }
virtual real_type secondary_stack_factor() const { return 3.0; }

SPConstMaterial build_material() override;
SPConstGeoMaterial build_geomaterial() override;
SPConstParticle build_particle() override;
SPConstCutoff build_cutoff() override;
SPConstPhysics build_physics() override;

virtual PhysicsOptions build_physics_options() const;

// Access lazily loaded static geant4 data
const celeritas::ImportData& imported_data() const;
};

//---------------------------------------------------------------------------//
} // namespace celeritas_test
Loading