Back to home page

EIC code displayed by LXR

 
 

    


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

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/OpticalPrimaryGenerator.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <cmath>
0010 
0011 #include "corecel/Assert.hh"
0012 #include "corecel/Macros.hh"
0013 #include "corecel/Types.hh"
0014 #include "geocel/random/IsotropicDistribution.hh"
0015 
0016 #include "../TrackInitializer.hh"
0017 
0018 namespace celeritas
0019 {
0020 //---------------------------------------------------------------------------//
0021 /*!
0022  * Sample optical photons from user-configurable distributions.
0023  *
0024  * \todo Support runtime-configurable distributions instead of hardcoding
0025  */
0026 class OpticalPrimaryGenerator
0027 {
0028   public:
0029     // Construct from distribution data
0030     inline CELER_FUNCTION
0031     OpticalPrimaryGenerator(PrimaryDistributionData const& data);
0032 
0033     // Sample an optical photon from the distributions
0034     template<class Generator>
0035     inline CELER_FUNCTION optical::TrackInitializer operator()(Generator& rng);
0036 
0037   private:
0038     //// DATA ////
0039 
0040     PrimaryDistributionData const& data_;
0041     IsotropicDistribution<real_type> sample_angle_;
0042 };
0043 
0044 //---------------------------------------------------------------------------//
0045 // INLINE DEFINITIONS
0046 //---------------------------------------------------------------------------//
0047 /*!
0048  * Construct from optical materials and distribution parameters.
0049  */
0050 CELER_FUNCTION
0051 OpticalPrimaryGenerator::OpticalPrimaryGenerator(
0052     PrimaryDistributionData const& data)
0053     : data_(data)
0054 {
0055     CELER_EXPECT(data_);
0056 }
0057 
0058 //---------------------------------------------------------------------------//
0059 /*!
0060  * Sample an optical photon from the distributions.
0061  *
0062  * \todo There are a couple places in the code where we resample the
0063  * polarization if orthogonality fails: possibly add a helper function to
0064  * reduce duplication
0065  */
0066 template<class Generator>
0067 CELER_FUNCTION optical::TrackInitializer
0068 OpticalPrimaryGenerator::operator()(Generator& rng)
0069 {
0070     optical::TrackInitializer result;
0071     result.energy = data_.energy;
0072     result.position = data_.position;
0073     result.direction = sample_angle_(rng);
0074     do
0075     {
0076         result.polarization = make_unit_vector(
0077             make_orthogonal(sample_angle_(rng), result.direction));
0078     } while (CELER_UNLIKELY(
0079         !soft_zero(dot_product(result.polarization, result.direction))));
0080 
0081     return result;
0082 }
0083 
0084 //---------------------------------------------------------------------------//
0085 }  // namespace celeritas