25#include <boost/archive/binary_iarchive.hpp>
26#include <boost/archive/binary_oarchive.hpp>
27#include <boost/iostreams/device/array.hpp>
28#include <boost/iostreams/stream.hpp>
29#include <boost/serialization/string.hpp>
30#include <boost/serialization/vector.hpp>
45int min(
int i,
unsigned int j) {
return std::min(i,
static_cast<int>(j)); }
51 std::vector<double>
const &A2) {
52 assert(A1.size() == A2.size());
53 std::vector<double> A_compressed(A1.size());
55 std::ranges::transform(A1, A2, A_compressed.begin(),
56 [](
double a,
double b) { return 0.5 * (a + b); });
63 std::vector<double>
const &A2) {
64 assert(A1.size() == A2.size());
65 std::vector<double> A_compressed(A2);
71 std::vector<double>
const &A2) {
72 assert(A1.size() == A2.size());
73 std::vector<double> A_compressed(A1);
78 std::vector<double>
const &B,
80 if (A.size() != B.size()) {
81 throw std::runtime_error(
82 "Error in scalar product: The vector sizes do not match");
85 auto const result = std::inner_product(A.begin(), A.end(), B.begin(), 0.0);
90 std::vector<double>
const &B,
92 std::vector<double> C(A.size());
93 if (A.size() != B.size()) {
94 throw std::runtime_error(
95 "Error in componentwise product: The vector sizes do not match");
98 std::ranges::transform(A, B, C.begin(), std::multiplies<>());
104 std::vector<double>
const &B,
106 std::vector<double> C(A.size() * B.size());
107 auto C_it = C.begin();
119 std::vector<double>
const &B,
121 if (A.size() != B.size()) {
122 throw std::runtime_error(
123 "Error in square distance componentwise: The vector sizes do not "
127 std::vector<double> C(A.size());
129 std::ranges::transform(A, B, C.begin(), [](
double a,
double b) ->
double {
130 return Utils::sqr(a - b);
138std::vector<double>
fcs_acf(std::vector<double>
const &A,
139 std::vector<double>
const &B,
141 if (A.size() != B.size()) {
142 throw std::runtime_error(
143 "Error in fcs_acf: The vector sizes do not match.");
146 auto const C_size = A.size() / 3u;
147 assert(3u * C_size == A.size());
149 std::vector<double> C{};
152 for (std::size_t i = 0u; i < C_size; i++) {
154 for (std::size_t j = 0u; j < 3u; j++) {
155 auto const a = A[3u * i + j];
156 auto const b = B[3u * i + j];
159 C.emplace_back(std::exp(acc));
165void Correlator::initialize_operations() {
168 if (m_tau_lin == 1) {
169 m_tau_lin =
static_cast<int>(std::ceil(m_tau_max / m_dt));
170 m_tau_lin += m_tau_lin % 2;
174 throw std::runtime_error(
"tau_lin must be >= 2");
178 throw std::runtime_error(
"tau_lin must be divisible by 2");
181 if (m_tau_max <= m_dt) {
182 throw std::runtime_error(
"tau_max must be >= delta_t (delta_N too large)");
185 if ((m_tau_max / m_dt) < m_tau_lin) {
186 m_hierarchy_depth = 1;
188 auto const operand = (m_tau_max / m_dt) /
double(m_tau_lin - 1);
189 assert(operand > 0.);
190 m_hierarchy_depth =
static_cast<int>(std::ceil(1. + std::log2(operand)));
195 dim_A = A_obs->n_values();
196 dim_B = B_obs->n_values();
199 throw std::runtime_error(
"dimension of first observable has to be >= 1");
202 throw std::runtime_error(
"dimension of second observable has to be >= 1");
206 if (corr_operation_name ==
"componentwise_product") {
208 m_shape = A_obs->shape();
211 }
else if (corr_operation_name ==
"tensor_product") {
212 m_dim_corr = dim_A * dim_B;
214 m_shape.emplace_back(dim_A);
215 m_shape.emplace_back(dim_B);
218 }
else if (corr_operation_name ==
"square_distance_componentwise") {
220 m_shape = A_obs->shape();
223 }
else if (corr_operation_name ==
"fcs_acf") {
227 throw std::runtime_error(
"missing parameter for fcs_acf: w_x w_y w_z");
230 m_correlation_args_input);
232 throw std::runtime_error(
"dimA must be divisible by 3 for fcs_acf");
233 m_dim_corr = dim_A / 3u;
234 m_shape = A_obs->shape();
235 if (m_shape.back() != 3u)
236 throw std::runtime_error(
237 "the last dimension of dimA must be 3 for fcs_acf");
240 }
else if (corr_operation_name ==
"scalar_product") {
243 m_shape.emplace_back(1u);
247 throw std::invalid_argument(
"correlation operation '" +
248 corr_operation_name +
"' not implemented");
252 if (compressA_name ==
"discard2") {
254 }
else if (compressA_name ==
"discard1") {
256 }
else if (compressA_name ==
"linear") {
259 throw std::invalid_argument(
"unknown compression method '" +
260 compressA_name +
"' for first observable");
263 if (compressB_name ==
"discard2") {
265 }
else if (compressB_name ==
"discard1") {
267 }
else if (compressB_name ==
"linear") {
270 throw std::invalid_argument(
"unknown compression method '" +
271 compressB_name +
"' for second observable");
275void Correlator::initialize_buffers() {
276 using index_type =
decltype(result)::index;
278 A.resize(std::array<int, 2>{{m_hierarchy_depth, m_tau_lin + 1}});
279 std::fill_n(A.data(), A.num_elements(), std::vector<double>(dim_A, 0));
280 B.resize(std::array<int, 2>{{m_hierarchy_depth, m_tau_lin + 1}});
281 std::fill_n(B.data(), B.num_elements(), std::vector<double>(dim_B, 0));
284 A_accumulated_average = std::vector<double>(dim_A, 0);
285 B_accumulated_average = std::vector<double>(dim_B, 0);
288 n_sweeps = std::vector<std::size_t>(n_result, 0);
289 n_vals = std::vector<long>(m_hierarchy_depth, 0);
291 result.resize(std::array<std::size_t, 2>{{n_result, m_dim_corr}});
292 for (index_type i = 0; i < static_cast<index_type>(n_result); i++) {
293 for (index_type j = 0; j < static_cast<index_type>(m_dim_corr); j++) {
298 newest = std::vector<long>(m_hierarchy_depth, m_tau_lin);
300 tau.resize(n_result);
301 for (
int i = 0; i < m_tau_lin + 1; i++) {
305 for (
int j = 1; j < m_hierarchy_depth; j++) {
306 for (
int k = 0; k < m_tau_lin / 2; k++) {
307 tau[m_tau_lin + 1 + (j - 1) * m_tau_lin / 2 + k] =
308 (k + (m_tau_lin / 2) + 1) * (1 << j);
315 throw std::runtime_error(
316 "No data can be added after finalize() was called.");
319 if (comm.rank() != 0) {
321 A_obs->operator()(comm);
322 if (A_obs != B_obs) {
323 B_obs->operator()(comm);
332 int highest_level_to_compress = -1;
339 auto const max_depth = m_hierarchy_depth - 1;
342 if (i >= max_depth or n_vals[i] <= m_tau_lin) {
345 auto const modulo = 1 << (i + 1);
346 auto const remainder = (t - (m_tau_lin + 1) * (modulo - 1) - 1) % modulo;
347 if (remainder != 0) {
350 highest_level_to_compress += 1;
358 for (
int i = highest_level_to_compress; i >= 0; i--) {
361 newest[i + 1] = (newest[i + 1] + 1) % (m_tau_lin + 1);
363 A[i + 1][newest[i + 1]] =
364 (*compressA)(A[i][(newest[i] + 1) % (m_tau_lin + 1)],
365 A[i][(newest[i] + 2) % (m_tau_lin + 1)]);
366 B[i + 1][newest[i + 1]] =
367 (*compressB)(B[i][(newest[i] + 1) % (m_tau_lin + 1)],
368 B[i][(newest[i] + 2) % (m_tau_lin + 1)]);
371 newest[0] = (newest[0] + 1) % (m_tau_lin + 1);
374 A[0][newest[0]] = A_obs->operator()(comm);
375 if (A_obs != B_obs) {
376 B[0][newest[0]] = B_obs->operator()(comm);
378 B[0][newest[0]] = A[0][newest[0]];
383 for (std::size_t k = 0; k < dim_A; k++) {
384 A_accumulated_average[k] += A[0][newest[0]][k];
387 for (std::size_t k = 0; k < dim_B; k++) {
388 B_accumulated_average[k] += B[0][newest[0]][k];
391 using index_type =
decltype(result)::index;
393 for (
long j = 0; j < min(m_tau_lin + 1, n_vals[0]); j++) {
394 auto const index_new = newest[0];
395 auto const index_old = (newest[0] - j + m_tau_lin + 1) % (m_tau_lin + 1);
397 (corr_operation)(A[0][index_old], B[0][index_new], m_correlation_args);
398 assert(temp.size() == m_dim_corr);
401 for (index_type k = 0; k < static_cast<index_type>(m_dim_corr); k++) {
402 result[j][k] += temp[k];
406 for (
int i = 1; i < highest_level_to_compress + 2; i++) {
407 for (
long j = (m_tau_lin + 1) / 2 + 1; j < min(m_tau_lin + 1, n_vals[i]);
409 auto const index_new = newest[i];
410 auto const index_old = (newest[i] - j + m_tau_lin + 1) % (m_tau_lin + 1);
411 auto const index_res =
412 m_tau_lin + (i - 1) * m_tau_lin / 2 + (j - m_tau_lin / 2 + 1) - 1;
413 auto const temp = (corr_operation)(A[i][index_old], B[i][index_new],
415 assert(temp.size() == m_dim_corr);
417 n_sweeps[index_res]++;
418 for (index_type k = 0; k < static_cast<index_type>(m_dim_corr); k++) {
419 result[index_res][k] += temp[k];
426 using index_type =
decltype(result)::index;
428 throw std::runtime_error(
"Correlator::finalize() can only be called once.");
438 if (comm.rank() != 0) {
442 for (
int ll = 0; ll < m_hierarchy_depth - 1; ll++) {
444 if (n_vals[ll] > m_tau_lin + 1)
445 vals_ll = m_tau_lin + n_vals[ll] % 2;
447 vals_ll = n_vals[ll];
451 auto highest_level_to_compress = (vals_ll % 2) ? ll : -1;
456 auto const max_depth = m_hierarchy_depth - 1;
458 while (highest_level_to_compress > -1) {
459 if (i >= max_depth or n_vals[i] % 2 == 0 or n_vals[i] <= m_tau_lin) {
462 highest_level_to_compress += 1;
472 for (
int i = highest_level_to_compress; i >= ll; i--) {
475 newest[i + 1] = (newest[i + 1] + 1) % (m_tau_lin + 1);
478 (*compressA)(A[i][(newest[i] + 1) % (m_tau_lin + 1)],
479 A[i][(newest[i] + 2) % (m_tau_lin + 1)]);
480 (*compressB)(B[i][(newest[i] + 1) % (m_tau_lin + 1)],
481 B[i][(newest[i] + 2) % (m_tau_lin + 1)]);
483 newest[ll] = (newest[ll] + 1) % (m_tau_lin + 1);
486 for (
int i = ll + 1; i < highest_level_to_compress + 2; i++) {
487 for (
long j = (m_tau_lin + 1) / 2 + 1;
488 j < min(m_tau_lin + 1, n_vals[i]); j++) {
489 auto const index_new = newest[i];
490 auto const index_old =
491 (newest[i] - j + m_tau_lin + 1) % (m_tau_lin + 1);
492 auto const index_res =
493 m_tau_lin + (i - 1) * m_tau_lin / 2 + (j - m_tau_lin / 2 + 1) - 1;
495 auto const temp = (corr_operation)(A[i][index_old], B[i][index_new],
497 assert(temp.size() == m_dim_corr);
499 n_sweeps[index_res]++;
500 for (index_type k = 0; k < static_cast<index_type>(m_dim_corr); k++) {
501 result[index_res][k] += temp[k];
511 using index_type =
decltype(result)::index;
513 std::vector<double> res(n_result * m_dim_corr);
515 for (std::size_t i = 0; i < n_result; i++) {
516 auto const index =
static_cast<index_type
>(m_dim_corr * i);
517 for (index_type k = 0; k < static_cast<index_type>(m_dim_corr); k++) {
519 res[index + k] = result[
static_cast<index_type
>(i)][k] /
520 static_cast<double>(n_sweeps[i]);
528 std::vector<double> res(
n_values());
529 std::ranges::transform(tau, res.begin(),
530 [
dt = m_dt](
auto const &a) { return a * dt; });
535 std::stringstream ss;
536 boost::archive::binary_oarchive oa(ss);
541 oa << m_correlation_args_input;
548 oa << A_accumulated_average;
549 oa << B_accumulated_average;
556 namespace iostreams = boost::iostreams;
557 iostreams::array_source src(state.data(), state.size());
558 iostreams::stream<iostreams::array_source> ss(src);
559 boost::archive::binary_iarchive ia(ss);
564 ia >> m_correlation_args_input;
571 ia >> A_accumulated_average;
572 ia >> B_accumulated_average;
574 initialize_operations();
Vector implementation and trait types for boost qvm interoperability.
void const * m_system
for bookkeeping purposes
std::size_t n_values() const
std::string get_internal_state() const final
void set_internal_state(std::string const &) final
std::vector< double > get_lag_times() const
int finalize(boost::mpi::communicator const &comm)
At the end of data collection, go through the whole hierarchy and correlate data left there.
std::vector< double > get_correlation()
Return correlation result.
void update(boost::mpi::communicator const &comm) override
The function to process a new datapoint of A and B.
static DEVICE_QUALIFIER constexpr Vector< T, N > broadcast(typename Base::value_type const &value) noexcept
Create a vector that has all entries set to the same value.
std::vector< double > componentwise_product(std::vector< double > const &A, std::vector< double > const &B, Utils::Vector3d const &)
std::vector< double > tensor_product(std::vector< double > const &A, std::vector< double > const &B, Utils::Vector3d const &)
std::vector< double > compress_linear(std::vector< double > const &A1, std::vector< double > const &A2)
Compress computing arithmetic mean: A_compressed=(A1+A2)/2.
std::vector< double > scalar_product(std::vector< double > const &A, std::vector< double > const &B, Utils::Vector3d const &)
std::vector< double > compress_discard1(std::vector< double > const &A1, std::vector< double > const &A2)
Compress discarding the 1st argument and return the 2nd.
std::vector< double > compress_discard2(std::vector< double > const &A1, std::vector< double > const &A2)
Compress discarding the 2nd argument and return the 1st.
std::vector< double > fcs_acf(std::vector< double > const &A, std::vector< double > const &B, Utils::Vector3d const &wsquare)
std::vector< double > square_distance_componentwise(std::vector< double > const &A, std::vector< double > const &B, Utils::Vector3d const &)
DEVICE_QUALIFIER constexpr T sqr(T x)
Calculates the SQuaRe of x.
auto hadamard_product(Vector< T, N > const &a, Vector< U, N > const &b)
int min(int i, unsigned int j)