1#ifndef AMREX_ALGORITHM_H_
2#define AMREX_ALGORITHM_H_
3#include <AMReX_Config.H>
41 template <
class T,
class ... Ts>
64 template <
class T,
class ... Ts>
81 template <
class T,
class ... Ts>
83 T
elemwiseMin (
const T& a,
const T& b,
const Ts& ... c)
noexcept
98 template <
class T,
class ... Ts>
110 void Swap (T& t1, T& t2)
noexcept
112 T temp = std::move(t1);
114 t2 = std::move(temp);
121 template <
typename T>
123 constexpr T
Clamp (
const T& v,
const T& lo,
const T& hi)
137 template <std::
floating_po
int T>
145 return std::abs(
x-
y) <= std::numeric_limits<T>::epsilon() * std::abs(
x+
y) * T(ulp)
147 || std::abs(
x-
y) < std::numeric_limits<T>::min();
168 template <std::
floating_po
int T,
class F>
170 T
bisect (T lo, T hi,
F f, T tol=1e-12,
int max_iter=100)
173 "Error - calling bisect but lo and hi don't describe a reasonable interval.");
178 if (flo == T(0)) {
return lo; }
179 if (fhi == T(0)) {
return hi; }
182 "Error - calling bisect but lo and hi don't bracket a root.");
184 T mi = (lo + hi) / T(2);
187 while (n <= max_iter)
189 if (hi - lo < tol ||
almostEqual(lo,hi)) {
break; }
190 mi = (lo + hi) / T(2);
192 if (fmi == T(0)) {
break; }
193 ((fmi < T(0)) != (flo < T(0))) ? hi = mi : lo = mi;
200 "Error - maximum number of iterations reached in bisect.");
227 template <
typename T, std::
integral Index>
229 Index
bisect (T
const* d, Index lo, Index hi, T
const& v)
231 while (hi - lo > 1) {
232 Index mid = lo + (hi - lo) / 2;
257 template<
typename ItType,
typename ValType>
259 ItType
upper_bound (ItType first, ItType last,
const ValType& val)
262 std::ptrdiff_t count = last-first;
265 const auto step = count/2;
278 return std::upper_bound(first, last, val);
297 template<
typename ItType,
typename ValType>
299 ItType
lower_bound (ItType first, ItType last,
const ValType& val)
302 std::ptrdiff_t count = last-first;
306 const auto step = count/2;
320 return std::lower_bound(first, last, val);
342 template<
typename ItType, std::
floating_po
int ValType>
343 requires (std::floating_point<typename std::iterator_traits<ItType>::value_type>)
345 void linspace (ItType first,
const ItType& last,
const ValType& start,
const ValType& stop)
347 const std::ptrdiff_t count = last-first;
349 const auto delta = (stop - start)/(count - 1);
350 for (std::ptrdiff_t i = 0; i < count-1; ++i){
351 *(first++) = start + i*delta;
376 template<
typename ItType, std::
floating_po
int ValType>
377 requires (std::floating_point<typename std::iterator_traits<ItType>::value_type>)
380 const ValType& start,
const ValType& stop,
const ValType& base)
382 const std::ptrdiff_t count = last-first;
384 const auto delta = (stop - start)/(count - 1);
385 for (std::ptrdiff_t i = 0; i < count-1; ++i){
386 *(first++) = std::pow(base, start + i*delta);
388 *first = std::pow(base, stop);
Assertion macros used across AMReX for runtime consistency checks.
#define AMREX_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:37
Simple structs for 3-component integer and real-valued tuples.
Compiler- and backend-specific extension macros (e.g., restrict, SIMD, inline).
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:124
#define AMREX_IF_ON_DEVICE(CODE)
Definition AMReX_GpuQualifiers.H:56
#define AMREX_IF_ON_HOST(CODE)
Definition AMReX_GpuQualifiers.H:58
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
__host__ __device__ ItType upper_bound(ItType first, ItType last, const ValType &val)
Return an iterator to the first element greater than a given value.
Definition AMReX_Algorithm.H:259
__host__ __device__ void logspace(ItType first, const ItType &last, const ValType &start, const ValType &stop, const ValType &base)
Fill a range with logarithmically spaced values over a closed interval.
Definition AMReX_Algorithm.H:379
__host__ __device__ void Swap(T &t1, T &t2) noexcept
Definition AMReX_Algorithm.H:110
__host__ __device__ T bisect(T lo, T hi, F f, T tol=1e-12, int max_iter=100)
Find a root of a scalar function on a bracketing interval using bisection.
Definition AMReX_Algorithm.H:170
__host__ __device__ constexpr T elemwiseMax(T const &a, T const &b) noexcept
Definition AMReX_Algorithm.H:92
__host__ __device__ constexpr const T & min(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:31
__host__ __device__ ItType lower_bound(ItType first, ItType last, const ValType &val)
Return an iterator to the first element not less than a given value.
Definition AMReX_Algorithm.H:299
__host__ __device__ void linspace(ItType first, const ItType &last, const ValType &start, const ValType &stop)
Fill a range with linearly spaced values over a closed interval.
Definition AMReX_Algorithm.H:345
__host__ __device__ constexpr const T & max(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:54
__host__ __device__ bool almostEqual(T x, T y, int ulp=2)
Definition AMReX_Algorithm.H:140
__host__ __device__ constexpr T Clamp(const T &v, const T &lo, const T &hi)
Definition AMReX_Algorithm.H:123
__host__ __device__ constexpr T elemwiseMin(T const &a, T const &b) noexcept
Definition AMReX_Algorithm.H:75
Definition AMReX_Amr.cpp:50
@ F
Definition AMReX_Order.H:7
@ y
Definition AMReX_Orientation.H:14
@ x
Definition AMReX_Orientation.H:14