DiFfRG
Loading...
Searching...
No Matches
integrator_angle_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 <int d, typename NT, typename KERNEL> class IntegratorAngleFiniteTq0TBB
15 {
16 static_assert(d >= 3, "dimension must be at least 3");
17
18 public:
19 using ctype = typename get_type::ctype<NT>;
20
22 const ctype x_extent, const JSONValue &json)
23 : IntegratorAngleFiniteTq0TBB(quadrature_provider, grid_sizes, x_extent, json.get_double("/physical/T"),
24 json.get_uint("/integration/cudathreadsperblock"))
25 {
26 }
27
29 const ctype x_extent, const ctype T, const uint max_block_size = 0)
30 : quadrature_provider(quadrature_provider), grid_sizes{{_grid_sizes[0], _grid_sizes[1], 0}}, x_extent(x_extent)
31 {
32 ptr_x_quadrature_p = quadrature_provider.get_points<ctype>(grid_sizes[0]).data();
33 ptr_x_quadrature_w = quadrature_provider.get_weights<ctype>(grid_sizes[0]).data();
34 ptr_ang_quadrature_p = quadrature_provider.get_points<ctype>(grid_sizes[1]).data();
35 ptr_ang_quadrature_w = quadrature_provider.get_weights<ctype>(grid_sizes[1]).data();
36
37 set_T(T);
38
39 (void)max_block_size;
40 }
41
49 void set_T(const ctype T, const ctype E = 0)
50 {
51 if (is_close(T, m_T) && (std::abs(E - m_E) / std::max(E, m_E) < 2.5e-2)) return;
52
53 std::cout << "SETTING T" << std::endl;
54
55 m_T = T;
56 // the default typical energy scale will default the matsubara size to 11.
57 m_E = is_close(E, 0.) ? 10 * m_T : E;
58 manual_E = !is_close(E, 0.);
59
60 grid_sizes[2] = quadrature_provider.get_matsubara_points<ctype>(m_T, m_E).size();
61 ptr_matsubara_quadrature_p = quadrature_provider.get_matsubara_points<ctype>(m_T, m_E).data();
62 ptr_matsubara_quadrature_w = quadrature_provider.get_matsubara_weights<ctype>(m_T, m_E).data();
63 }
64
70 void set_E(const ctype E) { set_T(m_T, E); }
71
73 : quadrature_provider(other.quadrature_provider), grid_sizes(other.grid_sizes),
74 ptr_x_quadrature_p(other.ptr_x_quadrature_p), ptr_x_quadrature_w(other.ptr_x_quadrature_w),
75 ptr_ang_quadrature_p(other.ptr_ang_quadrature_p), ptr_ang_quadrature_w(other.ptr_ang_quadrature_w),
76 ptr_matsubara_quadrature_p(other.ptr_matsubara_quadrature_p),
77 ptr_matsubara_quadrature_w(other.ptr_matsubara_quadrature_w), x_extent(other.x_extent), m_T(other.m_T),
78 m_E(other.m_E), manual_E(other.manual_E)
79 {
80 }
81
82 template <typename... T> NT get(const ctype k, const T &...t)
83 {
84 if (!manual_E && (std::abs(k - m_E) / std::max(k, m_E) > 2.5e-2)) {
85 set_T(m_T, k);
86 manual_E = false;
87 }
88
89 constexpr ctype S_dm1 = S_d_prec<ctype>(d - 1);
90 using std::sqrt, std::exp, std::log;
91
92 const auto constant = KERNEL::constant(k, t...);
93 return constant +
94 tbb::parallel_reduce(
95 tbb::blocked_range3d<uint, uint, uint>(0, grid_sizes[0], 0, grid_sizes[1], 0, grid_sizes[2]), NT(0.),
96 [&](const tbb::blocked_range3d<uint, uint, uint> &r, NT value) -> NT {
97 for (uint idx_x = r.pages().begin(); idx_x != r.pages().end(); ++idx_x) {
98 const ctype q = k * sqrt(ptr_x_quadrature_p[idx_x] * x_extent);
99
100 const ctype int_element = S_dm1 // solid nd angle
101 * (powr<d - 3>(q) / (ctype)2 * powr<2>(k)) // x = p^2 / k^2 integral
102 * (1 / (ctype)2) // divide the cos integral out
103 / powr<d - 1>(2 * (ctype)M_PI); // fourier factor
104
105 for (uint idx_y = r.rows().begin(); idx_y != r.rows().end(); ++idx_y) {
106 const ctype cos = 2 * (ptr_ang_quadrature_p[idx_y] - (ctype)0.5);
107
108 const ctype x_weight = 2 * ptr_ang_quadrature_w[idx_y] * ptr_x_quadrature_w[idx_x] * x_extent;
109
110 for (uint idx_z = r.cols().begin(); idx_z != r.cols().end(); ++idx_z) {
111 const ctype q0 = ptr_matsubara_quadrature_p[idx_z];
112 const ctype weight = x_weight * ptr_matsubara_quadrature_w[idx_z];
113 value += int_element * weight *
114 (KERNEL::kernel(q, cos, q0, k, t...) + KERNEL::kernel(q, cos, -q0, k, t...));
115 if (m_T > 0 && idx_z == 0)
116 value += int_element * x_weight * m_T * KERNEL::kernel(q, cos, (ctype)0, k, t...);
117 }
118 }
119 }
120 return value;
121 },
122 [&](NT x, NT y) -> NT { return x + y; });
123 }
124
125 template <typename... T> std::future<NT> request(const ctype k, const T &...t)
126 {
127 return std::async(std::launch::deferred, [=, this]() { return get(k, t...); });
128 }
129
130 private:
132
133 std::array<uint, 3> grid_sizes;
134
136 ctype m_T, m_E;
138
145 };
146} // namespace DiFfRG
Definition integrator_angle_finiteTq0_cpu.hh:15
NT get(const ctype k, const T &...t)
Definition integrator_angle_finiteTq0_cpu.hh:82
std::future< NT > request(const ctype k, const T &...t)
Definition integrator_angle_finiteTq0_cpu.hh:125
ctype m_E
Definition integrator_angle_finiteTq0_cpu.hh:136
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_angle_finiteTq0_cpu.hh:49
QuadratureProvider & quadrature_provider
Definition integrator_angle_finiteTq0_cpu.hh:131
const ctype * ptr_ang_quadrature_w
Definition integrator_angle_finiteTq0_cpu.hh:142
const ctype * ptr_matsubara_quadrature_p
Definition integrator_angle_finiteTq0_cpu.hh:143
std::array< uint, 3 > grid_sizes
Definition integrator_angle_finiteTq0_cpu.hh:133
typename get_type::ctype< NT > ctype
Definition integrator_angle_finiteTq0_cpu.hh:19
const ctype * ptr_ang_quadrature_p
Definition integrator_angle_finiteTq0_cpu.hh:141
bool manual_E
Definition integrator_angle_finiteTq0_cpu.hh:137
const ctype x_extent
Definition integrator_angle_finiteTq0_cpu.hh:135
void set_E(const ctype E)
Set the typical energy scale of the integrator and recompute the Matsubara quadrature rule.
Definition integrator_angle_finiteTq0_cpu.hh:70
const ctype * ptr_x_quadrature_w
Definition integrator_angle_finiteTq0_cpu.hh:140
IntegratorAngleFiniteTq0TBB(QuadratureProvider &quadrature_provider, const std::array< uint, 2 > grid_sizes, const ctype x_extent, const JSONValue &json)
Definition integrator_angle_finiteTq0_cpu.hh:21
IntegratorAngleFiniteTq0TBB(const IntegratorAngleFiniteTq0TBB &other)
Definition integrator_angle_finiteTq0_cpu.hh:72
const ctype * ptr_matsubara_quadrature_w
Definition integrator_angle_finiteTq0_cpu.hh:144
IntegratorAngleFiniteTq0TBB(QuadratureProvider &quadrature_provider, std::array< uint, 2 > _grid_sizes, const ctype x_extent, const ctype T, const uint max_block_size=0)
Definition integrator_angle_finiteTq0_cpu.hh:28
const ctype * ptr_x_quadrature_p
Definition integrator_angle_finiteTq0_cpu.hh:139
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
consteval NT S_d_prec(uint d)
Surface of a d-dimensional sphere (precompiled)
Definition math.hh:104
unsigned int uint
Definition utils.hh:22