DiFfRG
Loading...
Searching...
No Matches
integrator_4D_finiteTx0_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 Integrator4DFiniteTx0TBB
15 {
16 public:
17 using ctype = typename get_type::ctype<NT>;
18
20 const ctype x_extent, const ctype x0_extent, const uint x0_summands, const JSONValue &json)
22 json.get_double("/physical/T"), json.get_uint("/integration/cudathreadsperblock"))
23 {
24 }
25
27 const ctype x_extent, const ctype x0_extent, const uint _x0_summands, const ctype T,
28 const uint max_block_size = 0)
30 grid_sizes{{_grid_sizes[0], _grid_sizes[1], _grid_sizes[2], _grid_sizes[3] + _x0_summands}},
31 x_extent(x_extent), x0_extent(x0_extent), original_x0_summands(_x0_summands), m_T(T)
32 {
33 ptr_x_quadrature_p = quadrature_provider.get_points(grid_sizes[0]).data();
34 ptr_x_quadrature_w = quadrature_provider.get_weights(grid_sizes[0]).data();
35 ptr_ang_quadrature_p = quadrature_provider.get_points(grid_sizes[1]).data();
36 ptr_ang_quadrature_w = quadrature_provider.get_weights(grid_sizes[1]).data();
37
38 set_T(T);
39
40 (void)max_block_size;
41 }
42
43 void set_T(const ctype T)
44 {
45 m_T = T;
46 if (is_close(T, 0.))
47 x0_summands = 0;
48 else
49 x0_summands = original_x0_summands;
50 ptr_x0_quadrature_p = quadrature_provider.get_points<ctype>(grid_sizes[3] - x0_summands).data();
51 ptr_x0_quadrature_w = quadrature_provider.get_weights<ctype>(grid_sizes[3] - x0_summands).data();
52 }
53
54 void set_x0_extent(const ctype val) { x0_extent = val; }
55
57 : quadrature_provider(other.quadrature_provider), grid_sizes(other.grid_sizes),
58 ptr_x_quadrature_p(other.ptr_x_quadrature_p), ptr_x_quadrature_w(other.ptr_x_quadrature_w),
59 ptr_ang_quadrature_p(other.ptr_ang_quadrature_p), ptr_ang_quadrature_w(other.ptr_ang_quadrature_w),
60 ptr_x0_quadrature_p(other.ptr_x0_quadrature_p), ptr_x0_quadrature_w(other.ptr_x0_quadrature_w),
61 x_extent(other.x_extent), x0_extent(other.x0_extent), original_x0_summands(other.x0_summands),
62 x0_summands(other.x0_summands), m_T(other.m_T)
63 {
64 }
65
66 template <typename... T> NT get(const ctype k, const T &...t) const
67 {
68 constexpr int d = 4;
69 using std::sqrt, std::exp, std::log;
70
71 const ctype integral_start = (2 * x0_summands * (ctype)M_PI * m_T) / k;
72 const ctype log_start = log(integral_start + (m_T == 0) * ctype(1e-3));
73 const ctype log_ext = log(x0_extent / (integral_start + (m_T == 0) * ctype(1e-3)));
74
75 const auto constant = KERNEL::constant(k, t...);
76 return constant +
77 tbb::parallel_reduce(
78 tbb::blocked_range3d<uint, uint, uint>(0, grid_sizes[0], 0, grid_sizes[1], 0, grid_sizes[2]), NT(0),
79 [&](const tbb::blocked_range3d<uint, uint, uint> &r, NT value) -> NT {
80 for (uint idx_x = r.pages().begin(); idx_x != r.pages().end(); ++idx_x) {
81 const ctype q = k * sqrt(ptr_x_quadrature_p[idx_x] * x_extent);
82 for (uint idx_y = r.rows().begin(); idx_y != r.rows().end(); ++idx_y) {
83 const ctype cos = 2 * (ptr_ang_quadrature_p[idx_y] - (ctype)0.5);
84 for (uint idx_z = r.cols().begin(); idx_z != r.cols().end(); ++idx_z) {
85 const ctype phi = 2 * (ctype)M_PI * ptr_ang_quadrature_p[idx_z];
86
87 const ctype weight = 2 * (ctype)M_PI * ptr_ang_quadrature_w[idx_z] * 2 *
88 ptr_ang_quadrature_w[idx_y] * ptr_x_quadrature_w[idx_x] * x_extent;
89
90 // integral
91 const ctype int_element_int =
92 (powr<d - 3>(q) / (ctype)2 * powr<2>(k)) // x = p^2 / k^2 integral
93 * (k) // x0 = q0 / k integral
94 / powr<d>(2 * (ctype)M_PI); // fourier factor
95 for (uint idx_0 = 0; idx_0 < grid_sizes[3] - x0_summands; ++idx_0) {
96 const ctype q0 =
97 k * (exp(log_start + log_ext * ptr_x0_quadrature_p[idx_0]) - (m_T == 0) * ctype(1e-3));
98 const ctype m_weight = weight * (ptr_x0_quadrature_w[idx_0] * log_ext * q0 / k);
99
100 value +=
101 int_element_int * m_weight *
102 (KERNEL::kernel(q, cos, phi, q0, k, t...) + KERNEL::kernel(q, cos, phi, -q0, k, t...));
103 }
104
105 // sum
106 const ctype int_element_sum =
107 m_T // solid nd angle
108 * (powr<d - 3>(q) / (ctype)2 * powr<2>(k)) // x = p^2 / k^2 integral
109 / powr<d - 1>(2 * (ctype)M_PI); // fourier factor
110
111 for (uint idx_0 = 0; idx_0 < x0_summands; ++idx_0) {
112 const ctype q0 = 2 * (ctype)M_PI * m_T * idx_0;
113 value += int_element_sum * weight *
114 (idx_0 == 0 ? KERNEL::kernel(q, cos, phi, (ctype)0, k, t...)
115 : KERNEL::kernel(q, cos, phi, q0, k, t...) +
116 KERNEL::kernel(q, cos, phi, -q0, 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) const
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
141
148 };
149} // namespace DiFfRG
Definition integrator_4D_finiteTx0_cpu.hh:15
uint x0_summands
Definition integrator_4D_finiteTx0_cpu.hh:139
Integrator4DFiniteTx0TBB(const Integrator4DFiniteTx0TBB &other)
Definition integrator_4D_finiteTx0_cpu.hh:56
ctype m_T
Definition integrator_4D_finiteTx0_cpu.hh:140
NT get(const ctype k, const T &...t) const
Definition integrator_4D_finiteTx0_cpu.hh:66
const ctype * ptr_x_quadrature_p
Definition integrator_4D_finiteTx0_cpu.hh:142
const ctype x_extent
Definition integrator_4D_finiteTx0_cpu.hh:136
ctype x0_extent
Definition integrator_4D_finiteTx0_cpu.hh:137
const ctype * ptr_ang_quadrature_p
Definition integrator_4D_finiteTx0_cpu.hh:144
void set_T(const ctype T)
Definition integrator_4D_finiteTx0_cpu.hh:43
std::future< NT > request(const ctype k, const T &...t) const
Definition integrator_4D_finiteTx0_cpu.hh:126
const ctype * ptr_x0_quadrature_w
Definition integrator_4D_finiteTx0_cpu.hh:147
const ctype * ptr_x0_quadrature_p
Definition integrator_4D_finiteTx0_cpu.hh:146
void set_x0_extent(const ctype val)
Definition integrator_4D_finiteTx0_cpu.hh:54
const uint original_x0_summands
Definition integrator_4D_finiteTx0_cpu.hh:138
Integrator4DFiniteTx0TBB(QuadratureProvider &quadrature_provider, const std::array< uint, 4 > grid_sizes, const ctype x_extent, const ctype x0_extent, const uint x0_summands, const JSONValue &json)
Definition integrator_4D_finiteTx0_cpu.hh:19
typename get_type::ctype< NT > ctype
Definition integrator_4D_finiteTx0_cpu.hh:17
QuadratureProvider & quadrature_provider
Definition integrator_4D_finiteTx0_cpu.hh:132
const ctype * ptr_ang_quadrature_w
Definition integrator_4D_finiteTx0_cpu.hh:145
const ctype * ptr_x_quadrature_w
Definition integrator_4D_finiteTx0_cpu.hh:143
Integrator4DFiniteTx0TBB(QuadratureProvider &quadrature_provider, std::array< uint, 4 > _grid_sizes, const ctype x_extent, const ctype x0_extent, const uint _x0_summands, const ctype T, const uint max_block_size=0)
Definition integrator_4D_finiteTx0_cpu.hh:26
std::array< uint, 4 > grid_sizes
Definition integrator_4D_finiteTx0_cpu.hh:134
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