DiFfRG
Loading...
Searching...
No Matches
integrator_4D_finiteTq0_cpu.hh
Go to the documentation of this file.
1#pragma once
2
3// standard library
4#include <future>
5
6// external libraries
7#include <tbb/tbb.h>
8
9// DiFfRG
11
12namespace DiFfRG
13{
14 template <typename NT, typename KERNEL> class Integrator4DFiniteTq0TBB
15 {
16 public:
17 using ctype = typename get_type::ctype<NT>;
18
20 const ctype x_extent, const ctype q0_extent, const uint q0_summands, const JSONValue &json)
21 : Integrator4DFiniteTq0TBB(quadrature_provider, grid_sizes, x_extent, json.get_double("/physical/T"),
22 json.get_uint("/integration/cudathreadsperblock"))
23 {
24 }
25
27 const ctype x_extent, const ctype T, const uint max_block_size = 0)
28 : quadrature_provider(quadrature_provider), grid_sizes{{_grid_sizes[0], _grid_sizes[1], _grid_sizes[2], 0}},
29 x_extent(x_extent), manual_E(false)
30 {
31 ptr_x_quadrature_p = quadrature_provider.get_points(grid_sizes[0]).data();
32 ptr_x_quadrature_w = quadrature_provider.get_weights(grid_sizes[0]).data();
33 ptr_ang_quadrature_p = quadrature_provider.get_points(grid_sizes[1]).data();
34 ptr_ang_quadrature_w = quadrature_provider.get_weights(grid_sizes[1]).data();
35
36 set_T(T);
37
38 (void)max_block_size;
39 }
40
48 void set_T(const ctype T, const ctype E = 0)
49 {
50 if (is_close(T, m_T) && (std::abs(E - m_E) / std::max(E, m_E) < 2.5e-2)) return;
51
52 m_T = T;
53 // the default typical energy scale will default the matsubara size to 11.
54 m_E = is_close(E, 0.) ? 10 * m_T : E;
55 manual_E = !is_close(E, 0.);
56
57 grid_sizes[3] = quadrature_provider.get_matsubara_points<ctype>(m_T, m_E).size();
58 ptr_matsubara_quadrature_p = quadrature_provider.get_matsubara_points<ctype>(m_T, m_E).data();
59 ptr_matsubara_quadrature_w = quadrature_provider.get_matsubara_weights<ctype>(m_T, m_E).data();
60 }
61
67 void set_E(const ctype E) { set_T(m_T, E); }
68
70 : quadrature_provider(other.quadrature_provider), grid_sizes(other.grid_sizes),
71 ptr_x_quadrature_p(other.ptr_x_quadrature_p), ptr_x_quadrature_w(other.ptr_x_quadrature_w),
72 ptr_ang_quadrature_p(other.ptr_ang_quadrature_p), ptr_ang_quadrature_w(other.ptr_ang_quadrature_w),
73 ptr_matsubara_quadrature_p(other.ptr_matsubara_quadrature_p),
74 ptr_matsubara_quadrature_w(other.ptr_matsubara_quadrature_w), x_extent(other.x_extent), m_T(other.m_T),
75 m_E(other.m_E), manual_E(other.manual_E)
76 {
77 }
78
79 template <typename... T> NT get(const ctype k, const T &...t)
80 {
81 if (!manual_E && (std::abs(k - m_E) / std::max(k, m_E) > 2.5e-2)) {
82 set_T(m_T, k);
83 manual_E = false;
84 }
85
86 constexpr int d = 4;
87 using std::sqrt, std::exp, std::log;
88
89 const auto constant = KERNEL::constant(k, t...);
90 return constant +
91 tbb::parallel_reduce(
92 tbb::blocked_range3d<uint, uint, uint>(0, grid_sizes[0], 0, grid_sizes[1], 0, grid_sizes[2]), NT(0.),
93 [&](const tbb::blocked_range3d<uint, uint, uint> &r, NT value) -> NT {
94 for (uint idx_x = r.pages().begin(); idx_x != r.pages().end(); ++idx_x) {
95 const ctype q = k * sqrt(ptr_x_quadrature_p[idx_x] * x_extent);
96
97 const ctype int_element = (powr<d - 3>(q) / (ctype)2 * powr<2>(k)) // x = p^2 / k^2 integral
98 / powr<d - 1>(2 * (ctype)M_PI); // fourier factor
99
100 for (uint idx_y = r.rows().begin(); idx_y != r.rows().end(); ++idx_y) {
101 const ctype cos = 2 * (ptr_ang_quadrature_p[idx_y] - (ctype)0.5);
102 for (uint idx_z = r.cols().begin(); idx_z != r.cols().end(); ++idx_z) {
103 const ctype phi = 2 * (ctype)M_PI * ptr_ang_quadrature_p[idx_z];
104
105 const ctype x_weight = 2 * (ctype)M_PI * ptr_ang_quadrature_w[idx_z] * 2 *
106 ptr_ang_quadrature_w[idx_y] * ptr_x_quadrature_w[idx_x] * x_extent;
107
108 // summation
109 for (uint idx_0 = 0; idx_0 < grid_sizes[3]; ++idx_0) {
110 const ctype q0 = ptr_matsubara_quadrature_p[idx_0];
111 const ctype weight = x_weight * ptr_matsubara_quadrature_w[idx_0];
112 value +=
113 int_element * weight *
114 (KERNEL::kernel(q, cos, phi, q0, k, t...) + KERNEL::kernel(q, cos, phi, -q0, k, t...));
115 if (m_T > 0 && idx_0 == 0)
116 value += int_element * x_weight * m_T * KERNEL::kernel(q, cos, phi, (ctype)0, k, t...);
117 }
118 }
119 }
120 }
121 return value;
122 },
123 [&](NT x, NT y) -> NT { return x + y; });
124 }
125
126 template <typename... T> std::future<NT> request(const ctype k, const T &...t)
127 {
128 return std::async(std::launch::deferred, [=, this]() { return get(k, t...); });
129 }
130
131 private:
133
134 std::array<uint, 4> grid_sizes;
135
137 ctype m_T, m_E;
139
146 };
147} // namespace DiFfRG
Definition integrator_4D_finiteTq0_cpu.hh:15
ctype m_E
Definition integrator_4D_finiteTq0_cpu.hh:137
Integrator4DFiniteTq0TBB(const Integrator4DFiniteTq0TBB &other)
Definition integrator_4D_finiteTq0_cpu.hh:69
Integrator4DFiniteTq0TBB(QuadratureProvider &quadrature_provider, const std::array< uint, 4 > grid_sizes, const ctype x_extent, const ctype q0_extent, const uint q0_summands, const JSONValue &json)
Definition integrator_4D_finiteTq0_cpu.hh:19
bool manual_E
Definition integrator_4D_finiteTq0_cpu.hh:138
std::array< uint, 4 > grid_sizes
Definition integrator_4D_finiteTq0_cpu.hh:134
QuadratureProvider & quadrature_provider
Definition integrator_4D_finiteTq0_cpu.hh:132
const ctype * ptr_x_quadrature_p
Definition integrator_4D_finiteTq0_cpu.hh:140
const ctype * ptr_matsubara_quadrature_w
Definition integrator_4D_finiteTq0_cpu.hh:145
NT get(const ctype k, const T &...t)
Definition integrator_4D_finiteTq0_cpu.hh:79
typename get_type::ctype< NT > ctype
Definition integrator_4D_finiteTq0_cpu.hh:17
Integrator4DFiniteTq0TBB(QuadratureProvider &quadrature_provider, std::array< uint, 4 > _grid_sizes, const ctype x_extent, const ctype T, const uint max_block_size=0)
Definition integrator_4D_finiteTq0_cpu.hh:26
std::future< NT > request(const ctype k, const T &...t)
Definition integrator_4D_finiteTq0_cpu.hh:126
void set_E(const ctype E)
Set the typical energy scale of the integrator and recompute the Matsubara quadrature rule.
Definition integrator_4D_finiteTq0_cpu.hh:67
void set_T(const ctype T, const ctype E=0)
Set the temperature and typical energy scale of the integrator and recompute the Matsubara quadrature...
Definition integrator_4D_finiteTq0_cpu.hh:48
const ctype * ptr_x_quadrature_w
Definition integrator_4D_finiteTq0_cpu.hh:141
const ctype * ptr_ang_quadrature_p
Definition integrator_4D_finiteTq0_cpu.hh:142
const ctype x_extent
Definition integrator_4D_finiteTq0_cpu.hh:136
const ctype * ptr_matsubara_quadrature_p
Definition integrator_4D_finiteTq0_cpu.hh:144
const ctype * ptr_ang_quadrature_w
Definition integrator_4D_finiteTq0_cpu.hh:143
A wrapper around the boost json value class.
Definition json.hh:19
A class that provides quadrature points and weights, in host and device memory. The quadrature points...
Definition quadrature_provider.hh:139
const std::vector< NT > & get_points(const size_t order, const QuadratureType type=QuadratureType::legendre)
Get the quadrature points for a quadrature of size quadrature_size.
Definition quadrature_provider.hh:151
typename internal::_ctype< CT >::value ctype
Definition types.hh:106
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
constexpr auto & get(named_tuple< tuple_type, strs... > &ob)
get a reference to the element with the given name
Definition tuples.hh:82
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
unsigned int uint
Definition utils.hh:22