Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:59:57

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