Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-26 09:34:37

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