Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-26 09:34:37

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 corecel/random/distribution/IsotropicDistribution.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Constants.hh"
0010 #include "corecel/Types.hh"
0011 #include "corecel/cont/Array.hh"
0012 #include "corecel/math/ArrayUtils.hh"
0013 #include "corecel/random/data/DistributionData.hh"
0014 #include "corecel/random/distribution/UniformRealDistribution.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Sample points uniformly on the surface of a unit sphere.
0021  */
0022 template<class RealType = ::celeritas::real_type>
0023 class IsotropicDistribution
0024 {
0025   public:
0026     //!@{
0027     //! \name Type aliases
0028     using real_type = RealType;
0029     using result_type = Array<real_type, 3>;
0030     using RecordT = IsotropicDistributionRecord;
0031     //!@}
0032 
0033   public:
0034     // Constructor
0035     inline CELER_FUNCTION IsotropicDistribution();
0036 
0037     // Construct from record
0038     explicit CELER_FUNCTION IsotropicDistribution(RecordT const&)
0039         : IsotropicDistribution()
0040     {
0041     }
0042 
0043     // Sample a random unit vector
0044     template<class Generator>
0045     inline CELER_FUNCTION result_type operator()(Generator& rng);
0046 
0047   private:
0048     UniformRealDistribution<real_type> sample_costheta_;
0049     UniformRealDistribution<real_type> sample_phi_;
0050 };
0051 
0052 //---------------------------------------------------------------------------//
0053 // INLINE DEFINITIONS
0054 //---------------------------------------------------------------------------//
0055 /*!
0056  * Construct with defaults.
0057  */
0058 template<class RealType>
0059 CELER_FUNCTION IsotropicDistribution<RealType>::IsotropicDistribution()
0060     : sample_costheta_(-1, 1), sample_phi_(0, real_type(2 * constants::pi))
0061 {
0062 }
0063 
0064 //---------------------------------------------------------------------------//
0065 /*!
0066  * Sample an isotropic unit vector.
0067  */
0068 template<class RealType>
0069 template<class Generator>
0070 CELER_FUNCTION auto IsotropicDistribution<RealType>::operator()(Generator& rng)
0071     -> result_type
0072 {
0073     real_type const costheta = sample_costheta_(rng);
0074     real_type const phi = sample_phi_(rng);
0075     return from_spherical(costheta, phi);
0076 }
0077 
0078 //---------------------------------------------------------------------------//
0079 }  // namespace celeritas