Skip to content

Commit

Permalink
Add option to specify max substeps in field propagator (#1236)
Browse files Browse the repository at this point in the history
  • Loading branch information
amandalund committed May 15, 2024
1 parent 9e06741 commit 567576d
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 24 deletions.
1 change: 1 addition & 0 deletions app/celer-g4/GlobalSetup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ void GlobalSetup::ReadInput(std::string const& filename)
options_->action_times = input_.action_times;
options_->default_stream = input_.default_stream;
options_->auto_flush = input_.auto_flush;
options_->max_field_substeps = input_.field_options.max_substeps;
}
else if (ends_with(filename, ".mac"))
{
Expand Down
3 changes: 2 additions & 1 deletion app/celer-sim/Runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ void Runner::build_core_params(RunnerInput const& inp,
params.rng = std::make_shared<RngParams>(inp.seed);

// Construct simulation params
params.sim = SimParams::from_import(imported, params.particle);
params.sim = SimParams::from_import(
imported, params.particle, inp.field_options.max_substeps);

// Get the total number of events
auto num_events = this->build_events(inp, params.particle);
Expand Down
5 changes: 5 additions & 0 deletions src/accel/SetupOptions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ struct SetupOptions
AlongStepFactory make_along_step;
//!@}

//!@{
//! \name Field options
short int max_field_substeps{100};
//!@}

//!@{
//! \name Sensitive detector options
SDSetupOptions sd;
Expand Down
3 changes: 3 additions & 0 deletions src/accel/SetupOptionsMessenger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ SetupOptionsMessenger::SetupOptionsMessenger(SetupOptions* options)
add_cmd(&options->auto_flush,
"autoFlush",
"Number of tracks to buffer before offloading");
add_cmd(&options->max_field_substeps,
"maxFieldSubsteps",
"Limit on substeps in the field propagator");

directories_.emplace_back(new CelerDirectory(
"/celer/detector/", "Celeritas sensitive detector setup options"));
Expand Down
1 change: 1 addition & 0 deletions src/accel/SetupOptionsMessenger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct SetupOptions;
maxInitializers | Maximum number of track initializers
secondaryStackFactor | At least the average number of secondaries per track
autoFlush | Number of tracks to buffer before offloading
maxFieldSubsteps | Limit on substeps in field propagator
* The following option is exposed in the \c /celer/detector/ command
* "directory":
Expand Down
3 changes: 2 additions & 1 deletion src/accel/SharedParams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@ void SharedParams::initialize_core(SetupOptions const& options)
params.rng = std::make_shared<RngParams>(CLHEP::HepRandom::getTheSeed());

// Construct simulation params
params.sim = SimParams::from_import(*imported, params.particle);
params.sim = SimParams::from_import(
*imported, params.particle, options.max_field_substeps);

// Construct track initialization params
params.init = [&options] {
Expand Down
5 changes: 5 additions & 0 deletions src/celeritas/field/FieldDriver.hh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class FieldDriver

//// ACCESSORS ////

CELER_FUNCTION short int max_substeps() const
{
return options_.max_substeps;
}

CELER_FUNCTION real_type minimum_step() const
{
return options_.minimum_step;
Expand Down
2 changes: 2 additions & 0 deletions src/celeritas/field/FieldDriverOptions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ void validate_input(FieldDriverOptions const& opts)
<< "invalid max_stepping_decrease " << opts.max_stepping_decrease);
CELER_VALIDATE(opts.max_nsteps > 0,
<< "invalid max_nsteps " << opts.max_nsteps);
CELER_VALIDATE(opts.max_substeps > 0,
<< "invalid max_substeps " << opts.max_substeps);
CELER_ENSURE(opts);
}

Expand Down
6 changes: 5 additions & 1 deletion src/celeritas/field/FieldDriverOptions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ struct FieldDriverOptions
//! Maximum number of integrations (or trials)
short int max_nsteps = 100;

//! Maximum number of substeps in the field propagator
short int max_substeps = 100;

//! Initial step tolerance
static constexpr inline real_type initial_step_tol = 1e-6;

Expand All @@ -84,7 +87,7 @@ struct FieldDriverOptions
&& (safety > 0 && safety < 1)
&& (max_stepping_increase > 1)
&& (max_stepping_decrease > 0 && max_stepping_decrease < 1)
&& (max_nsteps > 0);
&& (max_nsteps > 0) && (max_substeps > 0);
// clang-format on
}
};
Expand All @@ -107,6 +110,7 @@ operator==(FieldDriverOptions const& a, FieldDriverOptions const& b)
&& a.max_stepping_increase == b.max_stepping_increase
&& a.max_stepping_decrease == b.max_stepping_decrease
&& a.max_nsteps == b.max_nsteps
&& a.max_substeps == b.max_substeps
&& a.initial_step_tol == b.initial_step_tol
&& a.dchord_tol == b.dchord_tol
&& a.min_chord_shrink == b.min_chord_shrink;
Expand Down
2 changes: 2 additions & 0 deletions src/celeritas/field/FieldDriverOptionsIO.json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void from_json(nlohmann::json const& j, FieldDriverOptions& opts)
FDO_INPUT(max_stepping_increase);
FDO_INPUT(max_stepping_decrease);
FDO_INPUT(max_nsteps);
FDO_INPUT(max_substeps);

#undef FDO_INPUT
}
Expand All @@ -64,6 +65,7 @@ void to_json(nlohmann::json& j, FieldDriverOptions const& opts)
CELER_JSON_PAIR(opts, max_stepping_increase),
CELER_JSON_PAIR(opts, max_stepping_decrease),
CELER_JSON_PAIR(opts, max_nsteps),
CELER_JSON_PAIR(opts, max_substeps),
};
}

Expand Down
25 changes: 11 additions & 14 deletions src/celeritas/field/FieldPropagator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@

namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Configuration options for the field propagator.
*/
struct FieldPropagatorOptions
{
//! Limit on substeps
static constexpr short int max_substeps = 100;
};

//---------------------------------------------------------------------------//
/*!
* Propagate a charged particle in a field.
Expand Down Expand Up @@ -74,10 +64,7 @@ class FieldPropagator
static CELER_CONSTEXPR_FUNCTION bool tracks_can_loop() { return true; }

//! Limit on substeps
static CELER_CONSTEXPR_FUNCTION short int max_substeps()
{
return FieldPropagatorOptions::max_substeps;
}
inline CELER_FUNCTION short int max_substeps() const;

// Intersection tolerance
inline CELER_FUNCTION real_type delta_intersection() const;
Expand Down Expand Up @@ -353,6 +340,16 @@ CELER_FUNCTION real_type FieldPropagator<DriverT, GTV>::delta_intersection() con
return driver_.delta_intersection();
}

//---------------------------------------------------------------------------//
/*!
* Maximum number of substeps.
*/
template<class DriverT, class GTV>
CELER_FUNCTION short int FieldPropagator<DriverT, GTV>::max_substeps() const
{
return driver_.max_substeps();
}

//---------------------------------------------------------------------------//
/*!
* Distance to bump or to consider a "zero" movement.
Expand Down
25 changes: 19 additions & 6 deletions src/celeritas/track/SimParams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "corecel/Assert.hh"
#include "corecel/data/CollectionBuilder.hh"
#include "corecel/math/Algorithms.hh"
#include "celeritas/field/FieldPropagator.hh"
#include "celeritas/field/FieldDriverOptions.hh"
#include "celeritas/io/ImportData.hh"
#include "celeritas/phys/ParticleParams.hh"

Expand All @@ -23,9 +23,12 @@ namespace celeritas
* Construct with imported data.
*/
std::shared_ptr<SimParams>
SimParams::from_import(ImportData const& data, SPConstParticles particle_params)
SimParams::from_import(ImportData const& data,
SPConstParticles particle_params,
short int max_field_substeps)
{
CELER_EXPECT(particle_params);
CELER_EXPECT(max_field_substeps > 0);
CELER_EXPECT(data.trans_params);
CELER_EXPECT(data.trans_params.looping.size() == particle_params->size());

Expand All @@ -35,10 +38,9 @@ SimParams::from_import(ImportData const& data, SPConstParticles particle_params)
// Calculate the maximum number of steps a track below the threshold energy
// can take while looping (ceil(max Geant4 field propagator substeps / max
// Celeritas field propagator substeps))
size_type max_substeps = FieldPropagatorOptions::max_substeps;
size_type imported_max_substeps = data.trans_params.max_substeps;
CELER_ASSERT(imported_max_substeps >= max_substeps);
auto max_subthreshold_steps = ceil_div(imported_max_substeps, max_substeps);
CELER_ASSERT(data.trans_params.max_substeps >= max_field_substeps);
auto max_subthreshold_steps = ceil_div<size_type>(
data.trans_params.max_substeps, max_field_substeps);

for (auto pid : range(ParticleId{input.particles->size()}))
{
Expand All @@ -58,6 +60,17 @@ SimParams::from_import(ImportData const& data, SPConstParticles particle_params)
return std::make_shared<SimParams>(input);
}

//---------------------------------------------------------------------------//
/*!
* Construct with imported data and default max field substeps.
*/
std::shared_ptr<SimParams>
SimParams::from_import(ImportData const& data, SPConstParticles particle_params)
{
return SimParams::from_import(
data, particle_params, FieldDriverOptions{}.max_substeps);
}

//---------------------------------------------------------------------------//
/*!
* Construct with simulation options.
Expand Down
7 changes: 6 additions & 1 deletion src/celeritas/track/SimParams.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@ class SimParams final : public ParamsDataInterface<SimParamsData>
};

public:
// Construct with imported data
// Construct with imported data and default max field substeps
static std::shared_ptr<SimParams>
from_import(ImportData const&, SPConstParticles);

// Construct with imported data
static std::shared_ptr<SimParams> from_import(ImportData const&,
SPConstParticles,
short int max_field_substeps);

// Construct with simulation input data
explicit SimParams(Input const&);

Expand Down

0 comments on commit 567576d

Please sign in to comment.