Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:59: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 geocel/random/UniformBoxDistribution.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <cmath>
0010 
0011 #include "corecel/Types.hh"
0012 #include "corecel/cont/Array.hh"
0013 #include "corecel/random/distribution/UniformRealDistribution.hh"
0014 
0015 namespace celeritas
0016 {
0017 //---------------------------------------------------------------------------//
0018 /*!
0019  * Sample a point uniformly in a box.
0020  */
0021 template<class RealType = ::celeritas::real_type>
0022 class UniformBoxDistribution
0023 {
0024   public:
0025     //!@{
0026     //! \name Type aliases
0027     using real_type = RealType;
0028     using result_type = Array<real_type, 3>;
0029     //!@}
0030 
0031   public:
0032     // Constructor
0033     inline CELER_FUNCTION
0034     UniformBoxDistribution(result_type lower, result_type upper);
0035 
0036     // Sample a random point in the box
0037     template<class Generator>
0038     inline CELER_FUNCTION result_type operator()(Generator& rng);
0039 
0040   private:
0041     using UniformRealDist = UniformRealDistribution<real_type>;
0042 
0043     Array<UniformRealDist, 3> sample_pos_;
0044 };
0045 
0046 //---------------------------------------------------------------------------//
0047 // INLINE DEFINITIONS
0048 //---------------------------------------------------------------------------//
0049 /*!
0050  * Construct from upper and lower coordinates.
0051  */
0052 template<class RealType>
0053 CELER_FUNCTION
0054 UniformBoxDistribution<RealType>::UniformBoxDistribution(result_type lower,
0055                                                          result_type upper)
0056     : sample_pos_{UniformRealDist{lower[0], upper[0]},
0057                   UniformRealDist{lower[1], upper[1]},
0058                   UniformRealDist{lower[2], upper[2]}}
0059 {
0060     CELER_EXPECT(lower[0] <= upper[0]);
0061     CELER_EXPECT(lower[1] <= upper[1]);
0062     CELER_EXPECT(lower[2] <= upper[2]);
0063 }
0064 
0065 //---------------------------------------------------------------------------//
0066 /*!
0067  * Sample uniformly in the box.
0068  */
0069 template<class RealType>
0070 template<class Generator>
0071 CELER_FUNCTION auto
0072 UniformBoxDistribution<RealType>::operator()(Generator& rng) -> result_type
0073 {
0074     result_type result;
0075     for (int i = 0; i < 3; ++i)
0076     {
0077         result[i] = sample_pos_[i](rng);
0078     }
0079     return result;
0080 }
0081 
0082 //---------------------------------------------------------------------------//
0083 }  // namespace celeritas