![]() |
|
|||
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/GenerateCanonical.hh 0007 //---------------------------------------------------------------------------// 0008 #pragma once 0009 0010 #include <limits> 0011 #include <random> 0012 #include <type_traits> 0013 0014 #include "corecel/Macros.hh" 0015 #include "corecel/Types.hh" 0016 0017 namespace celeritas 0018 { 0019 //---------------------------------------------------------------------------// 0020 //! Helper function to generate a random uniform number 0021 template<class RealType, class Generator> 0022 inline CELER_FUNCTION RealType generate_canonical(Generator& g); 0023 0024 //---------------------------------------------------------------------------// 0025 //! Sample a real_type on [0, 1). 0026 template<class Generator> 0027 inline CELER_FUNCTION real_type generate_canonical(Generator& g); 0028 0029 //---------------------------------------------------------------------------// 0030 /*! 0031 * Generate random numbers in [0, 1). 0032 * 0033 * This is essentially an implementation detail; partial specialization can be 0034 * used to sample using special functions with a given generator. 0035 */ 0036 template<class Generator, class RealType = ::celeritas::real_type> 0037 class GenerateCanonical 0038 { 0039 static_assert(std::is_floating_point<RealType>::value, 0040 "RealType must be float or double"); 0041 0042 public: 0043 //!@{ 0044 //! \name Type aliases 0045 using real_type = RealType; 0046 using result_type = real_type; 0047 //!@} 0048 0049 public: 0050 // Sample a random number 0051 result_type operator()(Generator& rng); 0052 }; 0053 0054 //---------------------------------------------------------------------------// 0055 // INLINE DEFINITIONS 0056 //---------------------------------------------------------------------------// 0057 /*! 0058 * Generate random numbers in [0, 1). 0059 * 0060 * This is the default implementation, for CPU-only code. 0061 */ 0062 template<class Generator, class RealType> 0063 auto GenerateCanonical<Generator, RealType>::operator()(Generator& rng) 0064 -> result_type 0065 { 0066 using limits_t = std::numeric_limits<result_type>; 0067 return std::generate_canonical<result_type, limits_t::digits>(rng); 0068 } 0069 0070 //---------------------------------------------------------------------------// 0071 /*! 0072 * Helper function to generate a random real number in [0, 1). 0073 */ 0074 template<class RealType, class Generator> 0075 CELER_FUNCTION RealType generate_canonical(Generator& g) 0076 { 0077 return GenerateCanonical<Generator, RealType>()(g); 0078 } 0079 0080 //---------------------------------------------------------------------------// 0081 /*! 0082 * Helper function to generate a random real number in [0, 1). 0083 */ 0084 template<class Generator> 0085 CELER_FUNCTION real_type generate_canonical(Generator& g) 0086 { 0087 return GenerateCanonical<Generator, real_type>()(g); 0088 } 0089 0090 //---------------------------------------------------------------------------// 0091 } // namespace celeritas
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |