Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:54:06

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/grid/VectorUtils.hh
0006 //! \brief Grid creation helpers
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <algorithm>
0011 #include <cmath>
0012 #include <vector>
0013 
0014 #include "corecel/Types.hh"
0015 #include "corecel/cont/Range.hh"
0016 #include "corecel/cont/Span.hh"
0017 #include "corecel/math/Algorithms.hh"
0018 #include "corecel/math/SoftEqual.hh"
0019 
0020 namespace celeritas
0021 {
0022 //---------------------------------------------------------------------------//
0023 // Return evenly spaced numbers over a specific interval
0024 std::vector<double> linspace(double start, double stop, size_type n);
0025 
0026 //---------------------------------------------------------------------------//
0027 // Return logarithmically spaced numbers over a specific interval
0028 std::vector<double> logspace(double start, double stop, size_type n);
0029 
0030 //---------------------------------------------------------------------------//
0031 /*!
0032  * True if the grid values are monotonically nondecreasing.
0033  */
0034 template<class T>
0035 inline bool is_monotonic_nondecreasing(Span<T> grid)
0036 {
0037     return all_adjacent(grid.begin(), grid.end(), [](T& left, T& right) {
0038         return left <= right;
0039     });
0040 }
0041 
0042 //---------------------------------------------------------------------------//
0043 /*!
0044  * True if the grid values are monotonically increasing.
0045  */
0046 template<class T>
0047 inline bool is_monotonic_increasing(Span<T> grid)
0048 {
0049     return all_adjacent(grid.begin(), grid.end(), [](T& left, T& right) {
0050         return left < right;
0051     });
0052 }
0053 
0054 //---------------------------------------------------------------------------//
0055 /*!
0056  * Calculate (geometric) ratio of successive grid points in a uniform log grid.
0057  */
0058 template<class T>
0059 T calc_log_delta(Span<T const> grid)
0060 {
0061     CELER_EXPECT(grid.size() > 1);
0062     return fastpow(grid.back() / grid.front(), T(1) / (grid.size() - 1));
0063 }
0064 
0065 //---------------------------------------------------------------------------//
0066 /*!
0067  * True if the grid has logarithmic spacing.
0068  */
0069 template<class T>
0070 inline bool has_log_spacing(Span<T const> grid)
0071 {
0072     T delta = calc_log_delta(grid);
0073     for (auto i : range(grid.size() - 1))
0074     {
0075         if (!soft_equal(delta, grid[i + 1] / grid[i]))
0076             return false;
0077     }
0078     return true;
0079 }
0080 
0081 //---------------------------------------------------------------------------//
0082 }  // namespace celeritas