DiFfRG
Loading...
Searching...
No Matches
integrator_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 <int d, typename NT, typename KERNEL> class IntegratorFiniteTx0TBB
15 {
16 static_assert(d >= 2, "dimension must be at least 2");
17
18 public:
19 using ctype = typename get_type::ctype<NT>;
20
22 const ctype x_extent, const ctype x0_extent, const uint x0_summands, const JSONValue &json)
24 json.get_double("/physical/T"))
25 {
26 }
27
28 IntegratorFiniteTx0TBB(QuadratureProvider &quadrature_provider, const std::array<uint, 2> _grid_sizes,
29 const ctype x_extent, const ctype x0_extent, const uint _x0_summands, const ctype T,
30 const uint max_block_size = 0)
31 : quadrature_provider(quadrature_provider), grid_sizes{{_grid_sizes[0], _grid_sizes[1] + _x0_summands}},
32 x_extent(x_extent), x0_extent(x0_extent), original_x0_summands(_x0_summands)
33 {
34 ptr_x_quadrature_p = quadrature_provider.get_points<ctype>(grid_sizes[0]).data();
35 ptr_x_quadrature_w = quadrature_provider.get_weights<ctype>(grid_sizes[0]).data();
36
37 set_T(T);
38
39 (void)max_block_size;
40 }
41
42 void set_T(const ctype T)
43 {
44 m_T = T;
45 if (is_close(T, 0.))
46 x0_summands = 0;
47 else
48 x0_summands = original_x0_summands;
49 ptr_x0_quadrature_p = quadrature_provider.get_points<ctype>(grid_sizes[1] - x0_summands).data();
50 ptr_x0_quadrature_w = quadrature_provider.get_weights<ctype>(grid_sizes[1] - x0_summands).data();
51 }
52
53 void set_x0_extent(const ctype val) { x0_extent = val; }
54
56 : quadrature_provider(other.quadrature_provider), grid_sizes(other.grid_sizes),
57 ptr_x_quadrature_p(other.ptr_x_quadrature_p), ptr_x_quadrature_w(other.ptr_x_quadrature_w),
58 ptr_x0_quadrature_p(other.ptr_x0_quadrature_p), ptr_x0_quadrature_w(other.ptr_x0_quadrature_w),
59 x_extent(other.x_extent), x0_extent(other.x0_extent), original_x0_summands(other.original_x0_summands),
60 x0_summands(other.x0_summands), m_T(other.m_T)
61 {
62 }
63
64 template <typename... T> NT get(const ctype k, const T &...t) const
65 {
66 constexpr ctype S_dm1 = S_d_prec<ctype>(d - 1);
67 using std::sqrt, std::exp, std::log;
68
69 const ctype integral_start = (2 * x0_summands * (ctype)M_PI * m_T) / k;
70 const ctype log_start = log(integral_start + (m_T == 0) * ctype(1e-3));
71 const ctype log_ext = log(x0_extent / (integral_start + (m_T == 0) * ctype(1e-3)));
72
73 const auto constant = KERNEL::constant(k, t...);
74 return constant +
75 tbb::parallel_reduce(
76 tbb::blocked_range2d<uint, uint>(0, grid_sizes[0], 0, grid_sizes[1]), NT(0),
77 [&](const tbb::blocked_range2d<uint, uint> &r, NT value) -> NT {
78 for (uint idx_x = r.rows().begin(); idx_x != r.rows().end(); ++idx_x) {
79 const ctype q = k * sqrt(ptr_x_quadrature_p[idx_x] * x_extent);
80 for (uint idx_y = r.cols().begin(); idx_y != r.cols().end(); ++idx_y) {
81 if (idx_y >= x0_summands) {
82 const ctype q0 = k * (exp(log_start + log_ext * ptr_x0_quadrature_p[idx_y - x0_summands]) -
83 (m_T == 0) * ctype(1e-3));
84
85 const ctype int_element = S_dm1 // solid nd angle
86 * (powr<d - 3>(q) / (ctype)2 * powr<2>(k)) // x = p^2 / k^2 integral
87 * (k) // x0 = q0 / k integral
88 / powr<d>(2 * (ctype)M_PI); // fourier factor
89 const ctype weight = ptr_x_quadrature_w[idx_x] * x_extent *
90 (ptr_x0_quadrature_w[idx_y - x0_summands] * log_ext * q0 / k);
91
92 value +=
93 int_element * weight * (KERNEL::kernel(q, q0, k, t...) + KERNEL::kernel(q, -q0, k, t...));
94 } else {
95 const ctype q0 = 2 * (ctype)M_PI * m_T * idx_y;
96 const ctype int_element = m_T * S_dm1 // temp * solid nd angle
97 * (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 const ctype weight = ptr_x_quadrature_w[idx_x] * x_extent;
100 value += int_element * weight *
101 (idx_y == 0 ? KERNEL::kernel(q, (ctype)0, k, t...)
102 : KERNEL::kernel(q, q0, k, t...) + KERNEL::kernel(q, -q0, k, t...));
103 }
104 }
105 }
106 return value;
107 },
108 [&](NT x, NT y) -> NT { return x + y; });
109 }
110
111 template <typename... T> std::future<NT> request(const ctype k, const T &...t) const
112 {
113 return std::async(std::launch::deferred, [=, this]() { return get(k, t...); });
114 }
115
116 private:
118
119 const std::array<uint, 2> grid_sizes;
120
126
131 };
132} // namespace DiFfRG
Definition integrator_finiteTx0_cpu.hh:15
const uint original_x0_summands
Definition integrator_finiteTx0_cpu.hh:123
const ctype * ptr_x_quadrature_p
Definition integrator_finiteTx0_cpu.hh:127
QuadratureProvider & quadrature_provider
Definition integrator_finiteTx0_cpu.hh:117
NT get(const ctype k, const T &...t) const
Definition integrator_finiteTx0_cpu.hh:64
uint x0_summands
Definition integrator_finiteTx0_cpu.hh:124
ctype m_T
Definition integrator_finiteTx0_cpu.hh:125
void set_x0_extent(const ctype val)
Definition integrator_finiteTx0_cpu.hh:53
const ctype x_extent
Definition integrator_finiteTx0_cpu.hh:121
std::future< NT > request(const ctype k, const T &...t) const
Definition integrator_finiteTx0_cpu.hh:111
const ctype * ptr_x0_quadrature_p
Definition integrator_finiteTx0_cpu.hh:129
IntegratorFiniteTx0TBB(const IntegratorFiniteTx0TBB &other)
Definition integrator_finiteTx0_cpu.hh:55
typename get_type::ctype< NT > ctype
Definition integrator_finiteTx0_cpu.hh:19
void set_T(const ctype T)
Definition integrator_finiteTx0_cpu.hh:42
IntegratorFiniteTx0TBB(QuadratureProvider &quadrature_provider, const std::array< uint, 2 > _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_finiteTx0_cpu.hh:28
const std::array< uint, 2 > grid_sizes
Definition integrator_finiteTx0_cpu.hh:119
const ctype * ptr_x_quadrature_w
Definition integrator_finiteTx0_cpu.hh:128
const ctype * ptr_x0_quadrature_w
Definition integrator_finiteTx0_cpu.hh:130
ctype x0_extent
Definition integrator_finiteTx0_cpu.hh:122
IntegratorFiniteTx0TBB(QuadratureProvider &quadrature_provider, const std::array< uint, 2 > grid_sizes, const ctype x_extent, const ctype x0_extent, const uint x0_summands, const JSONValue &json)
Definition integrator_finiteTx0_cpu.hh:21
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