DiFfRG
Loading...
Searching...
No Matches
thermodynamics.hh
Go to the documentation of this file.
1#pragma once
2
3// standard library
4#include <cmath>
5
6// DiFfRG
9
10namespace DiFfRG
11{
12 using std::cosh, std::sinh, std::tanh, std::exp, std::expm1;
13
14 // ----------------------------------------------------------------------------------------------------
15 // For convenience, all hyperbolic functions
16 // ----------------------------------------------------------------------------------------------------
17 template <typename T> auto __forceinline__ __device__ __host__ Cosh(const T x) { return cosh(x); }
18 template <typename T> auto __forceinline__ __device__ __host__ Sinh(const T x) { return sinh(x); }
19 template <typename T> auto __forceinline__ __device__ __host__ Tanh(const T x) { return tanh(x); }
20 template <typename T> auto __forceinline__ __device__ __host__ Coth(const T x) { return 1. / tanh(x); }
21 template <typename T> auto __forceinline__ __device__ __host__ Sech(const T x) { return 1. / cosh(x); }
22 template <typename T> auto __forceinline__ __device__ __host__ Csch(const T x) { return 1. / sinh(x); }
23
24 // ----------------------------------------------------------------------------------------------------
25 // Finite temperature hyperbolic functions
26 // ----------------------------------------------------------------------------------------------------
27
28 template <typename T1, typename T2>
29 requires(std::is_arithmetic_v<T2>)
30 auto __forceinline__ __device__ __host__ CothFiniteT(const T1 x, const T2 T)
31 {
32 using R = decltype(Coth(x / (2 * T)));
33 return is_close(T / x, T2(0)) ? (R)(real(x) < 0 ? -1 : 1) : Coth(x / (2 * T));
34 }
35 template <typename T1, typename T2>
36 requires(std::is_arithmetic_v<T2>)
37 auto __forceinline__ __device__ __host__ TanhFiniteT(const T1 x, const T2 T)
38 {
39 using R = decltype(Tanh(x / (2 * T)));
40 return is_close(T / x, T2(0)) ? (R)(real(x) < 0 ? -1 : 1) : Tanh(x / (2 * T));
41 }
42 template <typename T1, typename T2>
43 requires(std::is_arithmetic_v<T2>)
44 auto __forceinline__ __device__ __host__ SechFiniteT(const T1 x, const T2 T)
45 {
46 using R = decltype(Cosh(x / (2 * T)));
47 const auto res = Cosh(x / (2 * T));
48 return !isfinite(res) ? (R)0 : (R)(1) / res;
49 }
50 template <typename T1, typename T2>
51 requires(std::is_arithmetic_v<T2>)
52 auto __forceinline__ __device__ __host__ CschFiniteT(const T1 x, const T2 T)
53 {
54 using R = decltype(Sinh(x / (2 * T)));
55 const auto res = Sinh(x / (2 * T));
56 return !isfinite(res) ? (R)0 : (R)(1) / res;
57 }
58
59 // ----------------------------------------------------------------------------------------------------
60 // Thermodynamic hyperbolic functions
61 // ----------------------------------------------------------------------------------------------------
62 // coth(e/2T)
63 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ cothS(const T1 e, const T2 T)
64 {
65 return 1. / tanh(e / (T * 2.));
66 }
67 // d/de cothS
68 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ dcothS(const T1 e, const T2 T)
69 {
70 return -1. / powr<2>(sinh(e / (T * 2.))) / (2. * T);
71 }
72 // d^2/de^2 cothS
73 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ ddcothS(const T1 e, const T2 T)
74 {
75 return -dcothS(e, T) * cothS(e, T) / T;
76 }
77 // d^3/de^3 cothS
78 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ dddcothS(const T1 e, const T2 T)
79 {
80 return -(ddcothS(e, T) * cothS(e, T) + powr<2>(dcothS(e, T)));
81 }
82 // tanh(e/2T)
83 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ tanhS(const T1 e, const T2 T)
84 {
85 return tanh(e / (T * 2.));
86 }
87 // d/de tanhS
88 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ dtanhS(const T1 e, const T2 T)
89 {
90 return 1. / powr<2>(cosh(e / (T * 2.))) / (2. * T);
91 }
92 // d^2/de^2 tanhS
93 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ ddtanhS(const T1 e, const T2 T)
94 {
95 return -dtanhS(e, T) * tanhS(e, T) / T;
96 }
97 // sech(e/2T)
98 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ sechS(const T1 e, const T2 T)
99 {
100 return 1. / cosh(e / (T * 2.));
101 }
102 // csch(e/2T)
103 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ cschS(const T1 e, const T2 T)
104 {
105 return 1. / sinh(e / (T * 2.));
106 }
107
108 // ----------------------------------------------------------------------------------------------------
109 // Distribution Functions nB, nF and their Derivatives
110 // ----------------------------------------------------------------------------------------------------
111 // Bosonic distribution
112 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ nB(const T1 e, const T2 T)
113 {
114 return 1. / expm1(e / T);
115 }
116 // Derivative d/de of the bosonic distribution
117 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ dnB(const T1 e, const T2 T)
118 {
119 return -exp(e / T) / powr<2>(expm1(e / T)) / T;
120 }
121 // Derivative d²/de² of the bosonic distribution
122 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ ddnB(const T1 e, const T2 T)
123 {
124 return exp(e / T) * (1. + exp(e / T)) / powr<3>(expm1(e / T)) / powr<2>(T);
125 }
126 // Fermionic distribution
127 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ nF(const T1 e, const T2 T)
128 {
129 return 1. / (exp(e / T) + 1.);
130 }
131 // Derivative d/de of the fermionic distribution
132 template <typename T1, typename T2> auto __forceinline__ __device__ __host__ dnF(const T1 e, const T2 T)
133 {
134 return -exp(e / T) / powr<2>(exp(e / T) + 1.) / T;
135 }
136} // namespace DiFfRG
Definition complex_math.hh:14
constexpr __forceinline__ __host__ __device__ NumberType powr(const NumberType x)
A compile-time evaluatable power function for whole number exponents.
Definition math.hh:45
auto __forceinline__ __device__ __host__ tanhS(const T1 e, const T2 T)
Definition thermodynamics.hh:83
auto __forceinline__ __device__ __host__ cothS(const T1 e, const T2 T)
Definition thermodynamics.hh:63
auto __forceinline__ __device__ __host__ CothFiniteT(const T1 x, const T2 T)
Definition thermodynamics.hh:30
auto __forceinline__ __device__ __host__ Cosh(const T x)
Definition thermodynamics.hh:17
auto __forceinline__ __device__ __host__ Sech(const T x)
Definition thermodynamics.hh:21
bool __forceinline__ __host__ __device__ is_close(T1 a, T2 b, T3 eps_)
Function to evaluate whether two floats are equal to numerical precision. Tests for both relative and...
Definition math.hh:160
auto __forceinline__ __device__ __host__ Coth(const T x)
Definition thermodynamics.hh:20
auto __forceinline__ __device__ __host__ TanhFiniteT(const T1 x, const T2 T)
Definition thermodynamics.hh:37
AUTODIFF_DEVICE_FUNC auto real(const autodiff::Real< N, T > &a)
Definition complex_math.hh:148
auto __forceinline__ __device__ __host__ dtanhS(const T1 e, const T2 T)
Definition thermodynamics.hh:88
auto __forceinline__ __device__ __host__ CschFiniteT(const T1 x, const T2 T)
Definition thermodynamics.hh:52
auto __forceinline__ __device__ __host__ nB(const T1 e, const T2 T)
Definition thermodynamics.hh:112
auto __forceinline__ __device__ __host__ Csch(const T x)
Definition thermodynamics.hh:22
auto __forceinline__ __device__ __host__ dnF(const T1 e, const T2 T)
Definition thermodynamics.hh:132
auto __forceinline__ __device__ __host__ dddcothS(const T1 e, const T2 T)
Definition thermodynamics.hh:78
auto __forceinline__ __device__ __host__ nF(const T1 e, const T2 T)
Definition thermodynamics.hh:127
auto __forceinline__ __device__ __host__ ddcothS(const T1 e, const T2 T)
Definition thermodynamics.hh:73
auto __forceinline__ __device__ __host__ ddtanhS(const T1 e, const T2 T)
Definition thermodynamics.hh:93
auto __forceinline__ __device__ __host__ SechFiniteT(const T1 x, const T2 T)
Definition thermodynamics.hh:44
auto __forceinline__ __device__ __host__ cschS(const T1 e, const T2 T)
Definition thermodynamics.hh:103
auto __forceinline__ __device__ __host__ Sinh(const T x)
Definition thermodynamics.hh:18
auto __forceinline__ __device__ __host__ dcothS(const T1 e, const T2 T)
Definition thermodynamics.hh:68
auto __forceinline__ __device__ __host__ ddnB(const T1 e, const T2 T)
Definition thermodynamics.hh:122
auto __forceinline__ __device__ __host__ sechS(const T1 e, const T2 T)
Definition thermodynamics.hh:98
auto __forceinline__ __device__ __host__ Tanh(const T x)
Definition thermodynamics.hh:19
bool isfinite(const autodiff::Real< N, T > &x)
Finite-ness check for autodiff::real.
Definition math.hh:26
auto __forceinline__ __device__ __host__ dnB(const T1 e, const T2 T)
Definition thermodynamics.hh:117