Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-04 10:10:16

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file accel/SetupOptions.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <functional>
0010 #include <memory>
0011 #include <string>
0012 #include <unordered_set>
0013 #include <vector>
0014 
0015 #include "corecel/sys/Device.hh"
0016 #include "celeritas/Types.hh"
0017 #include "celeritas/global/ActionInterface.hh"
0018 #include "celeritas/inp/Control.hh"
0019 #include "celeritas/inp/Physics.hh"
0020 #include "celeritas/inp/Scoring.hh"
0021 #include "celeritas/inp/Tracking.hh"
0022 
0023 class G4LogicalVolume;
0024 class G4ParticleDefinition;
0025 
0026 namespace celeritas
0027 {
0028 namespace inp
0029 {
0030 struct FrameworkInput;
0031 struct GeantSd;
0032 }  // namespace inp
0033 
0034 class CoreParams;
0035 struct AlongStepFactoryInput;
0036 //---------------------------------------------------------------------------//
0037 /*!
0038  * Control options for initializing Celeritas SD callbacks.
0039  *
0040  * By default, Celeritas connects to Geant4 sensitive detectors so that it
0041  * reconstructs full-fidelity hits with all available step information.
0042  * - If your problem has no SDs, you must set \c enabled to \c false.
0043  * - By default, steps that do not deposit energy do not generate any hits.
0044  * - To improve performance and memory usage, determine what quantities (time,
0045  *   position, direction, touchable, ...) are required by your setup's
0046  *   sensitive detectors and set all other attributes to \c false.
0047  * - Reconstructing the full geometry status using \c locate_touchable is the
0048  *   most expensive detector option. Disable it unless your SDs require (e.g.)
0049  *   the volume's copy number to locate a detector submodule.
0050  *
0051  * Various attributes on the step, track, and pre/post step points may be
0052  * available depending on the selected options.
0053  * - Disabling \c track will leave \c G4Step::GetTrack as \c nullptr .
0054  * - Enabling \c track will set the \c Charge attribute on the
0055  *   pre-step.
0056  * - Requested post-step data including \c GlobalTime, \c Position, \c
0057  *   KineticEnergy, and \c MomentumDirection will be copied to the \c Track
0058  *   when the combination of options is enabled.
0059  * - Some properties (\c Material and \c MaterialCutsCouple) are
0060  *   set for the pre-step and, if the post-step touchable is reconstructed, for
0061  *   post-step as well.
0062  * - Track and Parent IDs will \em never be a valid value since Celeritas track
0063  *   counters are independent from Geant4 track counters. Similarly, special
0064  *   Geant4 user-defined \c UserInformation and \c AuxiliaryTrackInformation
0065  *   are never set.
0066  *
0067  * The \c force_volumes option can be used for unusual cases (i.e., when using
0068  * a custom run manager) that do not define the same SDs on the main thread
0069  * as on worker threads.
0070  * Similarly, the \c skip_volumes option allows optimized GPU-defined SDs to be
0071  * used in place of a Geant4 callback. For both options, the \c
0072  * FindVolumes helper function can be used to determine LV pointers from
0073  * the volume names.
0074  *
0075  * \note These setup options affect only the \c GeantSd construction that is
0076  * responsible for reconstructing CPU hits and sending directly to the Geant4
0077  * detectors. It does not change the underlying physics.
0078  *
0079  * \note This class will be replaced in v1.0
0080  *       by \c celeritas::inp::SensitiveDetector .
0081  */
0082 struct SDSetupOptions
0083 {
0084     struct StepPoint
0085     {
0086         bool global_time{true};
0087         bool position{true};
0088         bool direction{true};  //!< AKA momentum direction
0089         bool kinetic_energy{true};
0090     };
0091 
0092     //! Call back to Geant4 sensitive detectors
0093     bool enabled{true};
0094     //! Skip steps that do not deposit energy locally
0095     bool ignore_zero_deposition{true};
0096     //! Save energy deposition
0097     bool energy_deposition{true};
0098     //! Save physical step length
0099     bool step_length{true};
0100     //! Set TouchableHandle for PreStepPoint
0101     bool locate_touchable{true};
0102     //! Set TouchableHandle for PostStepPoint
0103     bool locate_touchable_post{true};
0104     //! Create a track with the dynamic particle type and post-step data
0105     bool track{true};
0106     //! Options for saving and converting beginning-of-step data
0107     StepPoint pre;
0108     //! Options for saving and converting end-of-step data
0109     StepPoint post;
0110 
0111     //! Manually list LVs that don't have an SD on the master thread
0112     std::unordered_set<G4LogicalVolume const*> force_volumes;
0113     //! List LVs that should *not* have automatic hit mapping
0114     std::unordered_set<G4LogicalVolume const*> skip_volumes;
0115 
0116     //! True if SD is enabled
0117     explicit operator bool() const { return this->enabled; }
0118 };
0119 
0120 //---------------------------------------------------------------------------//
0121 /*!
0122  * Control options for initializing optical photon transport in Celeritas.
0123  */
0124 struct OpticalSetupOptions
0125 {
0126     //! Capacity for storing optical photon state
0127     inp::OpticalStateCapacity capacity;
0128     //! Optical photon generation mechanism
0129     inp::OpticalGenerator generator;
0130     //! Limits for the optical stepping loop
0131     inp::OpticalTrackingLimits limits;
0132     //! Callback for optical photon hits
0133     inp::OpticalDetector detectors;
0134 };
0135 
0136 //---------------------------------------------------------------------------//
0137 /*!
0138  * Control options for initializing Celeritas.
0139  *
0140  * The interface for the "along-step factory" (input parameters and output) is
0141  * described in \c AlongStepFactoryInterface .
0142  *
0143  * If \c offload_particles is false, the default offload particles will be
0144  * used. If it's an empty vector, no particle types will be offloaded to
0145  * Celeritas. This differs from the \c CELER_DISABLE which disables Celeritas
0146  * offloading entirely and from the \c CELER_KILL_OFFLOAD option which both
0147  * disables Celeritas offloading and immediately kills the \c offload_particles
0148  * in Geant4. The only expected use case for an empty \c offload_particles
0149  * vector is when offloading optical distribution data to Celeritas through the
0150  * \c LocalOpticalGenOffload.
0151  *
0152  * Note that the Celeritas core capacity values (\c max_num_tracks, \c
0153  * initializer_capacity and \c auto_flush) are per \em stream while the \c
0154  * capacity values in \c OpticalSetupOptions are per \em process.
0155  *
0156  * \note This class will be replaced in v1.0
0157  *       by \c celeritas::inp::FrameworkInput .
0158  * \todo Improve and clarify the settings for optical distribution offloading.
0159  */
0160 struct SetupOptions
0161 {
0162     //!@{
0163     //! \name Type aliases
0164     using size_type = unsigned int;
0165     using real_type = double;
0166 
0167     using SPConstAction = std::shared_ptr<CoreStepActionInterface const>;
0168     using AlongStepFactory
0169         = std::function<SPConstAction(AlongStepFactoryInput const&)>;
0170     using IntAccessor = std::function<int()>;
0171     using VecString = std::vector<std::string>;
0172     using VecG4PD = std::vector<G4ParticleDefinition*>;
0173     //!@}
0174 
0175     //! Don't limit the number of steps
0176     static constexpr size_type no_max_steps()
0177     {
0178         return static_cast<size_type>(-1);
0179     }
0180 
0181     //!@{
0182     //! \name I/O
0183 
0184     //! GDML filename (optional: defaults to exporting existing Geant4)
0185     std::string geometry_file;
0186     //! Filename for JSON diagnostic output, empty to disable
0187     std::string output_file{"celeritas.out.json"};
0188     //! Filename for ROOT dump of physics data
0189     std::string physics_output_file;
0190     //! Filename to dump a ROOT/HepMC3/JSON copy of offloaded tracks as events
0191     std::string offload_output_file;
0192     //! Filename to dump a GDML file for debugging inside frameworks
0193     std::string geometry_output_file;
0194     //!@}
0195 
0196     //!@{
0197     //! \name Optical photon options
0198 
0199     std::optional<OpticalSetupOptions> optical;
0200     //!@}
0201 
0202     //!@{
0203     //! \name Celeritas stepper options
0204 
0205     //! Number of track "slots" to be transported simultaneously
0206     size_type max_num_tracks{};
0207     //! Limit on number of steps per track before killing
0208     size_type max_steps = no_max_steps();
0209     //! Limit on number of step iterations before aborting
0210     size_type max_step_iters = no_max_steps();
0211     //! Maximum number of track initializers (primaries+secondaries)
0212     size_type initializer_capacity{};
0213     //! At least the average number of secondaries per track slot
0214     real_type secondary_stack_factor{};
0215     //! Number of tracks to buffer before offloading (if unset: max num tracks)
0216     size_type auto_flush{};
0217     //!@}
0218 
0219     //!@{
0220     //! \name Track reordering options
0221     TrackOrder track_order{TrackOrder::size_};
0222     //!@}
0223 
0224     //! Set the number of streams (defaults to run manager # threads)
0225     IntAccessor get_num_streams;
0226 
0227     //!@{
0228     //! \name Stepping actions
0229 
0230     AlongStepFactory make_along_step;
0231     //!@}
0232 
0233     //!@{
0234     //! \name Field options
0235 
0236     size_type max_field_substeps{10};
0237     //!@}
0238 
0239     //! Sensitive detector options
0240     SDSetupOptions sd;
0241 
0242     //!@{
0243     //! \name Physics options
0244 
0245     //! Do not use Celeritas physics for the given Geant4 process names
0246     VecString ignore_processes;
0247     //! Only offload a subset of particles
0248     std::optional<VecG4PD> offload_particles;
0249     //! Physics grid interpolation options
0250     inp::Interpolation interpolation{};
0251     //!@}
0252 
0253     //!@{
0254     //! \name CUDA options
0255 
0256     //! Per-thread stack size (may be needed for VecGeom) [B]
0257     size_type cuda_stack_size{};
0258     //! Dynamic heap size (may be needed for VecGeom) [B]
0259     size_type cuda_heap_size{};
0260     //! Sync the GPU at every kernel for timing
0261     bool action_times{false};
0262     //! Launch all kernels on the default stream for debugging (REMOVED)
0263     bool default_stream{false};
0264     //!@}
0265 
0266     //!@{
0267     //! \name Diagnostic setup
0268 
0269     //! Filename base for slot diagnostics
0270     std::string slot_diagnostic_prefix;
0271 
0272     //! Add additional diagnostic user actions [EXPERIMENTAL]
0273     std::function<void(CoreParams const&)> add_user_actions;
0274     //!@}
0275 
0276     explicit inline operator bool() const
0277     {
0278         return static_cast<bool>(make_along_step);
0279     }
0280 };
0281 
0282 //---------------------------------------------------------------------------//
0283 // FREE FUNCTIONS
0284 //---------------------------------------------------------------------------//
0285 
0286 // Find volumes by name for SDSetupOptions
0287 std::unordered_set<G4LogicalVolume const*> FindVolumes(
0288     std::unordered_set<std::string>);
0289 
0290 // Convert SD options for forward compatibility
0291 inp::GeantSd to_inp(SDSetupOptions const& so);
0292 
0293 // Construct a framework input
0294 inp::FrameworkInput to_inp(SetupOptions const& so);
0295 
0296 //---------------------------------------------------------------------------//
0297 }  // namespace celeritas