Back to home page

EIC code displayed by LXR

 
 

    


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

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