Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
ScalarKernels.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
12
13#include <cmath>
14
16
17inline void computeDsqBlock(float ref_x, float ref_y, float ref_z,
18 const PositionBlockT<float> &block,
19 float *CORRELATION_RESTRICT out_dsq) noexcept {
20 for (std::size_t idx = 0; idx < block.count; ++idx) {
21 out_dsq[idx] = distSqScalar<float>(
22 {
23 .x = ref_x,
24 .y = ref_y,
25 .z = ref_z,
26 },
27 {
28 .x = block.x[idx],
29 .y = block.y[idx],
30 .z = block.z[idx],
31 });
32 }
33}
34
35inline void computeDsqBlock(double ref_x, double ref_y, double ref_z,
36 const PositionBlockT<double> &block,
37 double *CORRELATION_RESTRICT out_dsq) noexcept {
38 for (std::size_t idx = 0; idx < block.count; ++idx) {
39 out_dsq[idx] = distSqScalar<double>(
40 {
41 .x = ref_x,
42 .y = ref_y,
43 .z = ref_z,
44 },
45 {
46 .x = block.x[idx],
47 .y = block.y[idx],
48 .z = block.z[idx],
49 });
50 }
51}
52
53inline double simdDot(const double *CORRELATION_RESTRICT input_a,
54 const double *CORRELATION_RESTRICT input_b, std::size_t count) noexcept {
55 double acc = 0.0;
56 double carry = 0.0;
57 for (std::size_t idx = 0; idx < count; ++idx) {
58 const double val = input_a[idx] * input_b[idx];
59 const double y_val = val - carry;
60 const double temp_sum = acc + y_val;
61 carry = (temp_sum - acc) - y_val;
62 acc = temp_sum;
63 }
64 return acc;
65}
66
67inline float simdDot(const float *CORRELATION_RESTRICT input_a,
68 const float *CORRELATION_RESTRICT input_b, std::size_t count) noexcept {
69 float acc = 0.0F;
70 for (std::size_t idx = 0; idx < count; ++idx) {
71 acc += input_a[idx] * input_b[idx];
72 }
73 return acc;
74}
75
76inline void dotBlock(double v1x, double v1y, double v1z, const double *CORRELATION_RESTRICT v2x,
77 const double *CORRELATION_RESTRICT v2y, const double *CORRELATION_RESTRICT v2z,
78 double *CORRELATION_RESTRICT out_dot, std::size_t count) noexcept {
79 for (std::size_t idx = 0; idx < count; ++idx) {
80 out_dot[idx] = v1x * v2x[idx] + v1y * v2y[idx] + v1z * v2z[idx];
81 }
82}
83
84inline void dotBlock(float v1x, float v1y, float v1z, const float *CORRELATION_RESTRICT v2x,
85 const float *CORRELATION_RESTRICT v2y, const float *CORRELATION_RESTRICT v2z,
86 float *CORRELATION_RESTRICT out_dot, std::size_t count) noexcept {
87 for (std::size_t idx = 0; idx < count; ++idx) {
88 out_dot[idx] = v1x * v2x[idx] + v1y * v2y[idx] + v1z * v2z[idx];
89 }
90}
91
92inline double sincIntegral(const SincIntegralParams<double> &params) noexcept {
93 for (std::size_t idx = 0; idx < params.count; ++idx) {
94 params.sinqr_scratch[idx] = std::sin(params.q_magnitude * params.radial_bins[idx]);
95 }
96 return simdDot(params.integrand, params.sinqr_scratch, params.count);
97}
98
99inline float sincIntegral(const SincIntegralParams<float> &params) noexcept {
100 for (std::size_t idx = 0; idx < params.count; ++idx) {
101 params.sinqr_scratch[idx] = std::sin(params.q_magnitude * params.radial_bins[idx]);
102 }
103 return simdDot(params.integrand, params.sinqr_scratch, params.count);
104}
105
106inline double debyeSum(double q_magnitude, const double *CORRELATION_RESTRICT distances,
107 double *CORRELATION_RESTRICT scratch, std::size_t count) noexcept {
108 if (q_magnitude < 1.0e-9) {
109 return static_cast<double>(count);
110 }
111
112 double acc = 0.0;
113 double carry = 0.0;
114 for (std::size_t idx = 0; idx < count; ++idx) {
115 const double val_x = q_magnitude * distances[idx];
116 const double val = (val_x < 1.0e-4) ? (1.0 - (val_x * val_x) / 6.0) : (std::sin(val_x) / val_x);
117 scratch[idx] = val;
118 const double y_val = val - carry;
119 const double temp_sum = acc + y_val;
120 carry = (temp_sum - acc) - y_val;
121 acc = temp_sum;
122 }
123 return acc;
124}
125
126inline float debyeSum(float q_magnitude, const float *CORRELATION_RESTRICT distances,
127 float *CORRELATION_RESTRICT scratch, std::size_t count) noexcept {
128 if (q_magnitude < 1.0e-9F) {
129 return static_cast<float>(count);
130 }
131
132 float acc = 0.0F;
133 for (std::size_t idx = 0; idx < count; ++idx) {
134 const float val_x = q_magnitude * distances[idx];
135 const float val =
136 (val_x < 1.0e-4F) ? (1.0F - (val_x * val_x) / 6.0F) : (std::sin(val_x) / val_x);
137 scratch[idx] = val;
138 acc += val;
139 }
140 return acc;
141}
142
143inline void normalizeRdfBins(const RDFNormalizationParams<double> &params) noexcept {
144 if (params.count > 0) {
145 params.g_out[0] = 0.0;
146 params.G_out[0] = 0.0;
147 params.J_out[0] = 0.0;
148 params.Jinv_out[0] = 0.0;
149 }
150 for (std::size_t idx = 1; idx < params.count; ++idx) {
151 const double r_val = params.radial_bins[idx];
152 if (r_val < 1.0e-9) {
153 params.g_out[idx] = 0.0;
154 params.G_out[idx] = 0.0;
155 params.J_out[idx] = 0.0;
156 params.Jinv_out[idx] = 0.0;
157 continue;
158 }
159 const double g_val = params.hist_data[idx] * params.g_norm / (r_val * r_val);
160 params.g_out[idx] = g_val;
161 params.G_out[idx] = params.pi4_rho_j * r_val * (g_val - 1.0);
162 params.J_out[idx] = params.hist_data[idx] * params.inv_Ni_dr;
163 params.Jinv_out[idx] = params.hist_data[idx] * params.inv_Nj_dr;
164 }
165}
166
167inline void normalizeRdfBins(const RDFNormalizationParams<float> &params) noexcept {
168 if (params.count > 0) {
169 params.g_out[0] = 0.0F;
170 params.G_out[0] = 0.0F;
171 params.J_out[0] = 0.0F;
172 params.Jinv_out[0] = 0.0F;
173 }
174 for (std::size_t idx = 1; idx < params.count; ++idx) {
175 const float r_val = params.radial_bins[idx];
176 if (r_val < 1.0e-9F) {
177 params.g_out[idx] = 0.0F;
178 params.G_out[idx] = 0.0F;
179 params.J_out[idx] = 0.0F;
180 params.Jinv_out[idx] = 0.0F;
181 continue;
182 }
183 const float g_val = params.hist_data[idx] * params.g_norm / (r_val * r_val);
184 params.g_out[idx] = g_val;
185 params.G_out[idx] = params.pi4_rho_j * r_val * (g_val - 1.0F);
186 params.J_out[idx] = params.hist_data[idx] * params.inv_Ni_dr;
187 params.Jinv_out[idx] = params.hist_data[idx] * params.inv_Nj_dr;
188 }
189}
190
191inline void scaleBins(const ScaleBinsParams<double> &params) noexcept {
192 for (std::size_t idx = 0; idx < params.count; ++idx) {
193 params.arr[idx] *= params.scale_factor;
194 }
195}
196
197inline void scaleBins(const ScaleBinsParams<float> &params) noexcept {
198 for (std::size_t idx = 0; idx < params.count; ++idx) {
199 params.arr[idx] *= params.scale_factor;
200 }
201}
202
204 MillerPhaseSumResult<double> &result) noexcept {
205 result.cos_sum = 0.0;
206 result.sin_sum = 0.0;
207 for (std::size_t idx = 0; idx < params.count; ++idx) {
208 const double c12 = params.cos1[idx] * params.cos2[idx] - params.sin1[idx] * params.sin2[idx];
209 const double s12 = params.sin1[idx] * params.cos2[idx] + params.cos1[idx] * params.sin2[idx];
210 result.cos_sum += c12 * params.cos3[idx] - s12 * params.sin3[idx];
211 result.sin_sum += s12 * params.cos3[idx] + c12 * params.sin3[idx];
212 }
213}
214
216 MillerPhaseSumResult<float> &result) noexcept {
217 result.cos_sum = 0.0F;
218 result.sin_sum = 0.0F;
219 for (std::size_t idx = 0; idx < params.count; ++idx) {
220 const float c12 = params.cos1[idx] * params.cos2[idx] - params.sin1[idx] * params.sin2[idx];
221 const float s12 = params.sin1[idx] * params.cos2[idx] + params.cos1[idx] * params.sin2[idx];
222 result.cos_sum += c12 * params.cos3[idx] - s12 * params.sin3[idx];
223 result.sin_sum += s12 * params.cos3[idx] + c12 * params.sin3[idx];
224 }
225}
226
227} // namespace correlation::math::detail::scalar
Common parameter containers, POD types, and scalar helpers for SIMD operations.
Definition ScalarKernels.hpp:15
double debyeSum(double q_magnitude, const double *distances, double *scratch, std::size_t count) noexcept
Definition ScalarKernels.hpp:106
double sincIntegral(const SincIntegralParams< double > &params) noexcept
Definition ScalarKernels.hpp:92
void normalizeRdfBins(const RDFNormalizationParams< double > &params) noexcept
Definition ScalarKernels.hpp:143
void scaleBins(const ScaleBinsParams< double > &params) noexcept
Definition ScalarKernels.hpp:191
void dotBlock(double v1x, double v1y, double v1z, const double *v2x, const double *v2y, const double *v2z, double *out_dot, std::size_t count) noexcept
Definition ScalarKernels.hpp:76
void computeDsqBlock(float ref_x, float ref_y, float ref_z, const PositionBlockT< float > &block, float *out_dsq) noexcept
Definition ScalarKernels.hpp:17
double simdDot(const double *input_a, const double *input_b, std::size_t count) noexcept
Definition ScalarKernels.hpp:53
void millerPhaseSum(const MillerPhaseSumParams< double > &params, MillerPhaseSumResult< double > &result) noexcept
Definition ScalarKernels.hpp:203
T distSqScalar(Point3T< T > point_a, Point3T< T > point_b) noexcept
Computes the squared distance between two 3D points.
Definition SIMDTypes.hpp:162
ComplexExpSumResult< T > MillerPhaseSumResult
Alias for MillerPhaseSumResult used with millerPhaseSum kernel invocations.
Definition SIMDTypes.hpp:138
Parameter container for millerPhaseSum kernel invocations.
Definition SIMDTypes.hpp:98
Represents a Structure of Arrays (SoA) block of atom positions for SIMD processing.
Definition SIMDTypes.hpp:27
Parameter container for normalizeRdfBins kernel invocations.
Definition SIMDTypes.hpp:70
Parameter container for scaleBins kernel invocations.
Definition SIMDTypes.hpp:88
Parameter container for sincIntegral kernel invocations.
Definition SIMDTypes.hpp:58