|
| 1 | +#include "../math_chebyshev.h" |
| 2 | +#include "mpi.h" |
| 3 | +#include "module_base/parallel_comm.h" |
| 4 | +#include "gmock/gmock.h" |
| 5 | +#include "gtest/gtest.h" |
| 6 | +/************************************************ |
| 7 | + * unit test of class Chebyshev MPI part |
| 8 | + ***********************************************/ |
| 9 | + |
| 10 | + /** |
| 11 | + * - Tested Functions: |
| 12 | + * - checkconverge |
| 13 | + */ |
| 14 | +class toolfunc |
| 15 | +{ |
| 16 | + public: |
| 17 | + double x7(double x) |
| 18 | + { |
| 19 | + return pow(x, 7); |
| 20 | + } |
| 21 | + double x6(double x) |
| 22 | + { |
| 23 | + return pow(x, 6); |
| 24 | + } |
| 25 | + double expr(double x) |
| 26 | + { |
| 27 | + return exp(x); |
| 28 | + } |
| 29 | + std::complex<double> expi(std::complex<double> x) |
| 30 | + { |
| 31 | + const std::complex<double> j(0.0, 1.0); |
| 32 | + return exp(j * x); |
| 33 | + } |
| 34 | + std::complex<double> expi2(std::complex<double> x) |
| 35 | + { |
| 36 | + const std::complex<double> j(0.0, 1.0); |
| 37 | + const double PI = 3.14159265358979323846; |
| 38 | + return exp(j * PI / 2.0 * x); |
| 39 | + } |
| 40 | + // Pauli matrix: [0,-i;i,0] |
| 41 | + int LDA = 2; |
| 42 | + double factor = 1; |
| 43 | + void sigma_y(std::complex<double>* spin_in, std::complex<double>* spin_out, const int m = 1) |
| 44 | + { |
| 45 | + const std::complex<double> j(0.0, 1.0); |
| 46 | + if (this->LDA < 2) { |
| 47 | + this->LDA = 2; |
| 48 | +} |
| 49 | + for (int i = 0; i < m; ++i) |
| 50 | + { |
| 51 | + spin_out[LDA * i] = -factor * j * spin_in[LDA * i + 1]; |
| 52 | + spin_out[LDA * i + 1] = factor * j * spin_in[LDA * i]; |
| 53 | + } |
| 54 | + } |
| 55 | +#ifdef __ENABLE_FLOAT_FFTW |
| 56 | + float x7(float x) |
| 57 | + { |
| 58 | + return pow(x, 7); |
| 59 | + } |
| 60 | + float x6(float x) |
| 61 | + { |
| 62 | + return pow(x, 6); |
| 63 | + } |
| 64 | + float expr(float x) |
| 65 | + { |
| 66 | + return exp(x); |
| 67 | + } |
| 68 | + std::complex<float> expi(std::complex<float> x) |
| 69 | + { |
| 70 | + const std::complex<float> j(0.0, 1.0); |
| 71 | + return exp(j * x); |
| 72 | + } |
| 73 | + std::complex<float> expi2(std::complex<float> x) |
| 74 | + { |
| 75 | + const std::complex<float> j(0.0, 1.0); |
| 76 | + const float PI = 3.14159265358979323846; |
| 77 | + return exp(j * PI / 2.0f * x); |
| 78 | + } |
| 79 | + // Pauli matrix: [0,-i;i,0] |
| 80 | + void sigma_y(std::complex<float>* spin_in, std::complex<float>* spin_out, const int m = 1) |
| 81 | + { |
| 82 | + const std::complex<float> j(0.0, 1.0); |
| 83 | + if (this->LDA < 2) |
| 84 | + this->LDA = 2; |
| 85 | + for (int i = 0; i < m; ++i) |
| 86 | + { |
| 87 | + spin_out[LDA * i] = -j * spin_in[LDA * i + 1]; |
| 88 | + spin_out[LDA * i + 1] = j * spin_in[LDA * i]; |
| 89 | + } |
| 90 | + } |
| 91 | +#endif |
| 92 | +}; |
| 93 | +class MathChebyshevTest : public testing::Test |
| 94 | +{ |
| 95 | + protected: |
| 96 | + ModuleBase::Chebyshev<double>* p_chetest; |
| 97 | + ModuleBase::Chebyshev<float>* p_fchetest; |
| 98 | + toolfunc fun; |
| 99 | + int dsize = 0; |
| 100 | + int my_rank = 0; |
| 101 | + void SetUp() override |
| 102 | + { |
| 103 | + int world_rank; |
| 104 | + MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); |
| 105 | + int world_size; |
| 106 | + MPI_Comm_size(MPI_COMM_WORLD, &world_size); |
| 107 | + |
| 108 | + int color = (world_rank < world_size / 2) ? 0 : 1; |
| 109 | + int key = world_rank; |
| 110 | + |
| 111 | + MPI_Comm_split(MPI_COMM_WORLD, color, key, &POOL_WORLD); |
| 112 | + |
| 113 | + int pool_rank, pool_size; |
| 114 | + MPI_Comm_rank(POOL_WORLD, &pool_rank); |
| 115 | + MPI_Comm_size(POOL_WORLD, &pool_size); |
| 116 | + } |
| 117 | + void TearDown() override |
| 118 | + { |
| 119 | + } |
| 120 | +}; |
| 121 | + |
| 122 | +TEST_F(MathChebyshevTest, checkconverge) |
| 123 | +{ |
| 124 | + const int norder = 100; |
| 125 | + p_chetest = new ModuleBase::Chebyshev<double>(norder); |
| 126 | + auto fun_sigma_y |
| 127 | + = [&](std::complex<double>* in, std::complex<double>* out, const int m = 1) { fun.sigma_y(in, out, m); }; |
| 128 | + |
| 129 | + std::complex<double>* v = new std::complex<double>[4]; |
| 130 | + v[0] = 1.0; |
| 131 | + v[1] = 0.0; |
| 132 | + v[2] = 0.0; |
| 133 | + v[3] = 1.0; //[1 0; 0 1] |
| 134 | + double tmin = -1.1; |
| 135 | + double tmax = 1.1; |
| 136 | + bool converge; |
| 137 | + converge = p_chetest->checkconverge(fun_sigma_y, v, 2, 2, tmax, tmin, 0.2); |
| 138 | + EXPECT_TRUE(converge); |
| 139 | + converge = p_chetest->checkconverge(fun_sigma_y, v + 2, 2, 2, tmax, tmin, 0.2); |
| 140 | + EXPECT_TRUE(converge); |
| 141 | + EXPECT_NEAR(tmin, -1.1, 1e-8); |
| 142 | + EXPECT_NEAR(tmax, 1.1, 1e-8); |
| 143 | + |
| 144 | + tmax = -1.1; |
| 145 | + converge = p_chetest->checkconverge(fun_sigma_y, v, 2, 2, tmax, tmin, 2.2); |
| 146 | + EXPECT_TRUE(converge); |
| 147 | + EXPECT_NEAR(tmin, -1.1, 1e-8); |
| 148 | + EXPECT_NEAR(tmax, 1.1, 1e-8); |
| 149 | + |
| 150 | + // not converge |
| 151 | + v[0] = std::complex<double>(0, 1), v[1] = 1; |
| 152 | + fun.factor = 1.5; |
| 153 | + tmin = -1.1, tmax = 1.1; |
| 154 | + converge = p_chetest->checkconverge(fun_sigma_y, v, 2, 2, tmax, tmin, 0.2); |
| 155 | + EXPECT_FALSE(converge); |
| 156 | + |
| 157 | + fun.factor = -1.5; |
| 158 | + tmin = -1.1, tmax = 1.1; |
| 159 | + converge = p_chetest->checkconverge(fun_sigma_y, v, 2, 2, tmax, tmin, 0.2); |
| 160 | + EXPECT_FALSE(converge); |
| 161 | + fun.factor = 1; |
| 162 | + |
| 163 | + delete[] v; |
| 164 | + delete p_chetest; |
| 165 | +} |
| 166 | + |
| 167 | +#ifdef __ENABLE_FLOAT_FFTW |
| 168 | +TEST_F(MathChebyshevTest, checkconverge_float) |
| 169 | +{ |
| 170 | + const int norder = 100; |
| 171 | + p_fchetest = new ModuleBase::Chebyshev<float>(norder); |
| 172 | + |
| 173 | + std::complex<float>* v = new std::complex<float>[4]; |
| 174 | + v[0] = 1.0; |
| 175 | + v[1] = 0.0; |
| 176 | + v[2] = 0.0; |
| 177 | + v[3] = 1.0; //[1 0; 0 1] |
| 178 | + float tmin = -1.1; |
| 179 | + float tmax = 1.1; |
| 180 | + bool converge; |
| 181 | + |
| 182 | + auto fun_sigma_yf |
| 183 | + = [&](std::complex<float>* in, std::complex<float>* out, const int m = 1) { fun.sigma_y(in, out, m); }; |
| 184 | + converge = p_fchetest->checkconverge(fun_sigma_yf, v, 2, 2, tmax, tmin, 0.2); |
| 185 | + EXPECT_TRUE(converge); |
| 186 | + converge = p_fchetest->checkconverge(fun_sigma_yf, v + 2, 2, 2, tmax, tmin, 0.2); |
| 187 | + EXPECT_TRUE(converge); |
| 188 | + EXPECT_NEAR(tmin, -1.1, 1e-6); |
| 189 | + EXPECT_NEAR(tmax, 1.1, 1e-6); |
| 190 | + |
| 191 | + delete[] v; |
| 192 | + delete p_fchetest; |
| 193 | +} |
| 194 | +#endif |
| 195 | + |
| 196 | +int main(int argc, char** argv) |
| 197 | +{ |
| 198 | +#ifdef __MPI |
| 199 | + MPI_Init(&argc, &argv); |
| 200 | +#endif |
| 201 | + testing::InitGoogleTest(&argc, argv); |
| 202 | + int result = RUN_ALL_TESTS(); |
| 203 | +#ifdef __MPI |
| 204 | + MPI_Finalize(); |
| 205 | +#endif |
| 206 | + return result; |
| 207 | +} |
0 commit comments