Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-25 09:45:13

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> geomspace(double start, double stop, size_type n);
0029 
0030 //---------------------------------------------------------------------------//
0031 //! Return logarithmically spaced numbers over a specific interval
0032 //! \deprecated Remove in v1.0; replaced by geomspace
0033 [[deprecated]] inline std::vector<double>
0034 logspace(double start, double stop, size_type n)
0035 {
0036     return geomspace(start, stop, n);
0037 }
0038 
0039 //---------------------------------------------------------------------------//
0040 /*!
0041  * True if the grid values are monotonically nondecreasing.
0042  */
0043 template<class T>
0044 inline bool is_monotonic_nondecreasing(Span<T> grid)
0045 {
0046     return all_adjacent(grid.begin(), grid.end(), [](T& left, T& right) {
0047         return left <= right;
0048     });
0049 }
0050 
0051 //---------------------------------------------------------------------------//
0052 /*!
0053  * True if the grid values are monotonically increasing.
0054  */
0055 template<class T>
0056 inline bool is_monotonic_increasing(Span<T> grid)
0057 {
0058     return all_adjacent(grid.begin(), grid.end(), [](T& left, T& right) {
0059         return left < right;
0060     });
0061 }
0062 
0063 //---------------------------------------------------------------------------//
0064 /*!
0065  * Calculate (geometric) ratio of successive grid points in a uniform log grid.
0066  */
0067 template<class T>
0068 T calc_log_delta(Span<T const> grid)
0069 {
0070     CELER_EXPECT(grid.size() > 1);
0071     return fastpow(grid.back() / grid.front(), T(1) / (grid.size() - 1));
0072 }
0073 
0074 //---------------------------------------------------------------------------//
0075 /*!
0076  * True if the grid has logarithmic spacing.
0077  */
0078 template<class T>
0079 inline bool has_log_spacing(Span<T const> grid)
0080 {
0081     T delta = calc_log_delta(grid);
0082     for (auto i : range(grid.size() - 1))
0083     {
0084         if (!soft_equal(delta, grid[i + 1] / grid[i]))
0085             return false;
0086     }
0087     return true;
0088 }
0089 
0090 //---------------------------------------------------------------------------//
0091 }  // namespace celeritas