Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-14 08:50:58

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/InverseSquareDistribution.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Assert.hh"
0010 #include "corecel/Macros.hh"
0011 #include "corecel/Types.hh"
0012 
0013 #include "UniformRealDistribution.hh"
0014 
0015 namespace celeritas
0016 {
0017 //---------------------------------------------------------------------------//
0018 /*!
0019  * Sample \f$ 1/x^2 \f$ over a given domain.
0020  *
0021  * This distribution is defined on a positive range \f$ [a, b) \f$ and has the
0022  * normalized PDF:
0023  * \f[
0024    f(x; a, b) = \frac{a b}{x^2 (b - a)} \quad \mathrm{for} \  a \le x < b
0025    \f]
0026  * which integrated into a CDF and inverted gives a sample:
0027  * \f[
0028   x = \frac{a b}{a + \xi (b - a)}
0029    \f]
0030  */
0031 template<class RealType = ::celeritas::real_type>
0032 class InverseSquareDistribution
0033 {
0034   public:
0035     //!@{
0036     //! \name Type aliases
0037     using real_type = RealType;
0038     using result_type = real_type;
0039     //!@}
0040 
0041   public:
0042     // Construct on an interval
0043     inline CELER_FUNCTION InverseSquareDistribution(real_type a, real_type b);
0044 
0045     // Sample a random number according to the distribution
0046     template<class Generator>
0047     inline CELER_FUNCTION result_type operator()(Generator& rng) const;
0048 
0049   private:
0050     RealType product_;
0051     UniformRealDistribution<RealType> sample_denom_;
0052 };
0053 
0054 //---------------------------------------------------------------------------//
0055 // INLINE DEFINITIONS
0056 //---------------------------------------------------------------------------//
0057 /*!
0058  * Construct on the interval [a, b).
0059  *
0060  * As with UniformRealDistribution, it is allowable for the two bounds to be
0061  * out of order.
0062  */
0063 template<class RealType>
0064 CELER_FUNCTION
0065 InverseSquareDistribution<RealType>::InverseSquareDistribution(real_type a,
0066                                                                real_type b)
0067     : product_{a * b}, sample_denom_{a, b}
0068 {
0069     CELER_EXPECT(a > 0);
0070     CELER_EXPECT(b >= a);
0071 }
0072 
0073 //---------------------------------------------------------------------------//
0074 /*!
0075  * Sample a random number according to the distribution.
0076  */
0077 template<class RealType>
0078 template<class Generator>
0079 CELER_FUNCTION auto
0080 InverseSquareDistribution<RealType>::operator()(Generator& rng) const -> result_type
0081 {
0082     return product_ / sample_denom_(rng);
0083 }
0084 
0085 //---------------------------------------------------------------------------//
0086 }  // namespace celeritas