Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-07 10:01:42

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 celeritas/optical/gen/GeneratorData.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Macros.hh"
0010 #include "corecel/Types.hh"
0011 #include "corecel/cont/EnumArray.hh"
0012 #include "corecel/data/AuxInterface.hh"
0013 #include "corecel/data/CollectionStateStore.hh"
0014 #include "celeritas/Quantities.hh"
0015 #include "celeritas/Types.hh"
0016 #include "celeritas/phys/GeneratorInterface.hh"
0017 
0018 #include "OffloadData.hh"
0019 #include "../Types.hh"
0020 
0021 namespace celeritas
0022 {
0023 //---------------------------------------------------------------------------//
0024 /*!
0025  * Data for sampling optical photons from user-configurable distributions.
0026  *
0027  * \todo For now this is hardcoded to generate a point source of monoenergetic,
0028  * isotropic photons. Make this configurable.
0029  */
0030 struct PrimaryDistributionData
0031 {
0032     size_type num_photons{};
0033     units::MevEnergy energy;
0034     Real3 position{};
0035 
0036     //! Check whether the data are assigned
0037     explicit CELER_FUNCTION operator bool() const
0038     {
0039         return num_photons > 0 && energy > zero_quantity();
0040     }
0041 };
0042 
0043 //---------------------------------------------------------------------------//
0044 /*!
0045  * Pre- and post-step data for sampling optical photons.
0046  */
0047 struct GeneratorStepData
0048 {
0049     units::LightSpeed speed;
0050     Real3 pos{};
0051 
0052     //! Check whether the data are assigned
0053     explicit CELER_FUNCTION operator bool() const
0054     {
0055         return speed > zero_quantity();
0056     }
0057 };
0058 
0059 //---------------------------------------------------------------------------//
0060 /*!
0061  * Input data for sampling optical photons.
0062  *
0063  * This contains *all* the data needed to sample optical photons directly
0064  * generated by tracks in the main stepping loop.
0065  */
0066 struct GeneratorDistributionData
0067 {
0068     size_type num_photons{};  //!< Sampled number of photons to generate
0069     real_type time{};  //!< Pre-step time
0070     real_type step_length{};
0071     units::ElementaryCharge charge;
0072     OptMatId material;
0073     EnumArray<StepPoint, GeneratorStepData> points;
0074 
0075     //! Check whether the data are assigned
0076     explicit CELER_FUNCTION operator bool() const
0077     {
0078         return num_photons > 0 && step_length > 0 && material;
0079     }
0080 };
0081 
0082 //---------------------------------------------------------------------------//
0083 /*!
0084  * Optical photon distribution data.
0085  *
0086  * The distributions are stored in a buffer indexed by the current buffer size
0087  * plus the track slot ID. The data is compacted at the end of each step by
0088  * removing all invalid distributions. The order of the distributions in the
0089  * buffers is guaranteed to be reproducible.
0090  */
0091 template<Ownership W, MemSpace M>
0092 struct GeneratorStateData
0093 {
0094     //// TYPES ////
0095 
0096     template<class T>
0097     using Items = Collection<T, W, M>;
0098 
0099     //// DATA ////
0100 
0101     // Buffer of distribution data for generating optical photons
0102     Items<GeneratorDistributionData> distributions;
0103 
0104     // Determines which distribution a thread will generate a primary from
0105     Items<size_type> offsets;
0106 
0107     //// METHODS ////
0108 
0109     //! Whether all data are assigned and valid
0110     explicit CELER_FUNCTION operator bool() const
0111     {
0112         return !distributions.empty() && !offsets.empty();
0113     }
0114 
0115     //! Assign from another set of data
0116     template<Ownership W2, MemSpace M2>
0117     GeneratorStateData& operator=(GeneratorStateData<W2, M2>& other)
0118     {
0119         CELER_EXPECT(other);
0120         distributions = other.distributions;
0121         offsets = other.offsets;
0122         return *this;
0123     }
0124 };
0125 
0126 //---------------------------------------------------------------------------//
0127 /*!
0128  * Store optical generation states in aux data.
0129  */
0130 template<MemSpace M>
0131 struct GeneratorState : public GeneratorStateBase
0132 {
0133     CollectionStateStore<GeneratorStateData, M> store;
0134 
0135     //! True if states have been allocated
0136     explicit operator bool() const { return static_cast<bool>(store); }
0137 };
0138 
0139 //---------------------------------------------------------------------------//
0140 /*!
0141  * Resize optical buffere.
0142  */
0143 template<template<Ownership, MemSpace> class P, MemSpace M>
0144 void resize(GeneratorStateData<Ownership::value, M>* state,
0145             HostCRef<P> const&,
0146             StreamId,
0147             size_type size)
0148 {
0149     CELER_EXPECT(size > 0);
0150     resize(&state->distributions, size);
0151     resize(&state->offsets, size);
0152     CELER_ENSURE(*state);
0153 }
0154 
0155 //---------------------------------------------------------------------------//
0156 }  // namespace celeritas