mptensor v0.6.0
Parallel Library for Tensor Network Methods
Loading...
Searching...
No Matches
test_functions.hpp
Go to the documentation of this file.
1#ifndef MPTENSOR_TEST_TEST_FUNCTIONS_HPP_
2#define MPTENSOR_TEST_TEST_FUNCTIONS_HPP_
3
4#include <algorithm>
5#include <cmath>
6#include <cstddef>
7#include <type_traits>
8
10
11#include "mpi_helpers.hpp"
12
13namespace mptensor_test {
14
15using namespace mptensor;
16
18constexpr int kL = 10;
20constexpr double kEps = 1.0e-10;
21
22namespace detail {
23inline double coord(const Index& idx, const Shape& shape, std::size_t k) {
24 return double(idx[k]) / double(shape[k]);
25}
26} // namespace detail
27
29template <typename T>
30T func2_1(const Index& idx, const Shape& shape) {
31 const double x0 = detail::coord(idx, shape, 0);
32 const double x1 = detail::coord(idx, shape, 1);
33 if constexpr (std::is_same_v<T, double>) {
34 return 1.0 / (1.0 + std::abs(x0 - std::sqrt(x1)));
35 } else {
36 return 1.0 / (1.0 + complex(x0, std::sqrt(x1)));
37 }
38}
39
41template <typename T>
42T func4_1(const Index& idx, const Shape& shape) {
43 const double x0 = detail::coord(idx, shape, 0);
44 const double x1 = detail::coord(idx, shape, 1);
45 const double x2 = detail::coord(idx, shape, 2);
46 const double x3 = detail::coord(idx, shape, 3);
47 if constexpr (std::is_same_v<T, double>) {
48 return (x0 + x1) / (1.0 + std::abs(x2 - x3));
49 } else {
50 return complex(x0, x1) / complex(1.0 + x2, -x3);
51 }
52}
53
55template <typename T>
56T func4_2(const Index& idx, const Shape& shape) {
57 const double x0 = detail::coord(idx, shape, 0);
58 const double x1 = detail::coord(idx, shape, 1);
59 const double x2 = detail::coord(idx, shape, 2);
60 const double x3 = detail::coord(idx, shape, 3);
61 if constexpr (std::is_same_v<T, double>) {
62 return std::cos(x0 * x3) + std::sin(x1 / (x2 + 1.0));
63 } else {
64 return complex(std::cos(x0 * x3), std::sin(x1 / (x2 + 1.0)));
65 }
66}
67
69inline double conj_value(double x) { return x; }
70inline complex conj_value(const complex& x) { return std::conj(x); }
71
73template <typename TensorType, typename F>
75 const Shape shape = t.shape();
76 for (std::size_t i = 0; i < t.local_size(); ++i) {
77 t[i] = f(t.global_index(i), shape);
78 }
79}
80
82template <typename TensorType, typename F>
83double max_error(const TensorType& t, F exact) {
84 double error = 0.0;
85 for (std::size_t i = 0; i < t.local_size(); ++i) {
86 error = std::max(error, std::abs(t[i] - exact(t.global_index(i))));
87 }
88 return max_over_ranks(error, t.get_comm());
89}
90
91} // namespace mptensor_test
92
93#endif // MPTENSOR_TEST_TEST_FUNCTIONS_HPP_
Definition index.hpp:102
std::complex< double > complex
Definition complex.hpp:38
Top header file of mptensor.
double coord(const Index &idx, const Shape &shape, std::size_t k)
Definition test_functions.hpp:23
Definition mpi_helpers.hpp:10
T func4_2(const Index &idx, const Shape &shape)
Analytic rank-4 element (legacy func4_2 / cfunc4_2).
Definition test_functions.hpp:56
constexpr double kEps
Default tolerance.
Definition test_functions.hpp:20
double max_over_ranks(double local, MPI_Comm comm)
Maximum of local over all ranks of comm. Collective.
Definition mpi_helpers.hpp:19
void fill_tensor(TensorType &t, F f)
Set every local element to f(global_index, shape).
Definition test_functions.hpp:74
constexpr int kL
Base size of test tensors (legacy default N).
Definition test_functions.hpp:18
T func2_1(const Index &idx, const Shape &shape)
Analytic matrix element (legacy func2_1 / cfunc2_1).
Definition test_functions.hpp:30
double max_error(const TensorType &t, F exact)
Maximum over all ranks of |t[idx] - exact(idx)|. Collective.
Definition test_functions.hpp:83
double conj_value(double x)
Complex conjugate that keeps double as double.
Definition test_functions.hpp:69
T func4_1(const Index &idx, const Shape &shape)
Analytic rank-4 element (legacy func4_1 / cfunc4_1).
Definition test_functions.hpp:42
Definition complex.hpp:34