Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
derivatives.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
20#include "shambackends/sycl.hpp"
21#include <functional>
22
23namespace shammath {
24
37 template<class T>
38 inline T derivative_upwind(T x, T dx, std::function<T(T)> &&fct) {
39 return (fct(x + dx) - fct(x)) / dx;
40 }
41
54 template<class T>
55 inline T derivative_centered(T x, T dx, std::function<T(T)> &&fct) {
56 return (fct(x + dx) - fct(x - dx)) / (2 * dx);
57 }
58
70 template<class T>
71 inline T derivative_3point_forward(T x, T dx, std::function<T(T)> &&fct) {
72 return (-3 * fct(x) + 4 * fct(x + dx) - fct(x + 2 * dx)) / (2 * dx);
73 }
74
86 template<class T>
87 inline T derivative_3point_backward(T x, T dx, std::function<T(T)> &&fct) {
88 return (3 * fct(x) - 4 * fct(x - dx) + fct(x - 2 * dx)) / (2 * dx);
89 }
90
102 template<class T>
103 inline T derivative_5point_midpoint(T x, T dx, std::function<T(T)> &&fct) {
104 return (-fct(x + 2 * dx) + 8 * fct(x + dx) - 8 * fct(x - dx) + fct(x - 2 * dx)) / (12 * dx);
105 }
106
113 template<class T>
114 inline T estim_deriv_step(u32 order) {
115 return sycl::powr(shambase::get_epsilon<T>(), 1.0 / (order + 1));
116 }
117
118} // namespace shammath
std::uint32_t u32
32 bit unsigned integer
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for math utility
Definition AABB.hpp:26
T estim_deriv_step(u32 order)
Estimate the best step size for numerical differentiation of given order.
T derivative_3point_forward(T x, T dx, std::function< T(T)> &&fct)
Compute the derivative of a function at x using a 3-point forward finite difference.
T derivative_3point_backward(T x, T dx, std::function< T(T)> &&fct)
Compute the derivative of a function at x using a 3-point backward finite difference.
T derivative_5point_midpoint(T x, T dx, std::function< T(T)> &&fct)
Compute the derivative of a function at x using a 5-point centered finite difference.
T derivative_upwind(T x, T dx, std::function< T(T)> &&fct)
Compute the derivative of a function at x using the upwind method.
T derivative_centered(T x, T dx, std::function< T(T)> &&fct)
Compute the derivative of a function at x using the centered difference method.