Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:30

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file celeritas/random/distribution/IsotropicDistribution.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <cmath>
0011 
0012 #include "corecel/Types.hh"
0013 #include "corecel/cont/Array.hh"
0014 #include "corecel/math/ArrayUtils.hh"
0015 #include "celeritas/Constants.hh"
0016 
0017 #include "UniformRealDistribution.hh"
0018 
0019 namespace celeritas
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Sample points uniformly on the surface of a unit sphere.
0024  */
0025 template<class RealType = ::celeritas::real_type>
0026 class IsotropicDistribution
0027 {
0028   public:
0029     //!@{
0030     //! \name Type aliases
0031     using real_type = RealType;
0032     using result_type = Array<real_type, 3>;
0033     //!@}
0034 
0035   public:
0036     // Constructor
0037     inline CELER_FUNCTION IsotropicDistribution();
0038 
0039     // Sample a random unit vector
0040     template<class Generator>
0041     inline CELER_FUNCTION result_type operator()(Generator& rng);
0042 
0043   private:
0044     UniformRealDistribution<real_type> sample_costheta_;
0045     UniformRealDistribution<real_type> sample_phi_;
0046 };
0047 
0048 //---------------------------------------------------------------------------//
0049 // INLINE DEFINITIONS
0050 //---------------------------------------------------------------------------//
0051 /*!
0052  * Construct with defaults.
0053  */
0054 template<class RealType>
0055 CELER_FUNCTION IsotropicDistribution<RealType>::IsotropicDistribution()
0056     : sample_costheta_(-1, 1), sample_phi_(0, 2 * constants::pi)
0057 {
0058 }
0059 
0060 //---------------------------------------------------------------------------//
0061 /*!
0062  * Sample an isotropic unit vector.
0063  */
0064 template<class RealType>
0065 template<class Generator>
0066 CELER_FUNCTION auto
0067 IsotropicDistribution<RealType>::operator()(Generator& rng) -> result_type
0068 {
0069     real_type const costheta = sample_costheta_(rng);
0070     real_type const phi = sample_phi_(rng);
0071     return from_spherical(costheta, phi);
0072 }
0073 
0074 //---------------------------------------------------------------------------//
0075 }  // namespace celeritas