Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-08 08:15:04

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