|
| 1 | +#include "deepks_test_runner.h" |
| 2 | + |
| 3 | +#include "source_lcao/module_deepks/deepks_iterate.h" |
| 4 | +#include "source_lcao/module_rt/snap_phialpha_half_tddft.h" |
| 5 | +#include "source_lcao/module_rt/snap_projector_half_tddft.h" |
| 6 | + |
| 7 | +#include <algorithm> |
| 8 | +#include <cmath> |
| 9 | +#include <complex> |
| 10 | +#include <gtest/gtest.h> |
| 11 | +#include <iomanip> |
| 12 | +#include <iostream> |
| 13 | +#include <type_traits> |
| 14 | + |
| 15 | +template <typename T> |
| 16 | +void test_deepks<T>::check_phialpha_grid_zero_field() |
| 17 | +{ |
| 18 | + struct ComparisonStats |
| 19 | + { |
| 20 | + double max_real_diff = 0.0; |
| 21 | + double max_imag_abs = 0.0; |
| 22 | + double max_reference_abs = 0.0; |
| 23 | + double max_relative_diff = 0.0; |
| 24 | + double reference_at_max_diff = 0.0; |
| 25 | + int compared = 0; |
| 26 | + bool shape_mismatch = false; |
| 27 | + }; |
| 28 | + |
| 29 | + const ModuleBase::Vector3<double> zero_A(0.0, 0.0, 0.0); |
| 30 | + const auto compare_grid = [&](const int radial_grid_num, const int lebedev_grid_points) { |
| 31 | + ComparisonStats stats; |
| 32 | + module_rt::SnapIntegrationOptions options; |
| 33 | + options.radial_grid_num = radial_grid_num; |
| 34 | + options.lebedev_grid_points = lebedev_grid_points; |
| 35 | + |
| 36 | + DeePKS_domain::iterate_ad1( |
| 37 | + ucell, |
| 38 | + Test_Deepks::GridD, |
| 39 | + ORB, |
| 40 | + false, |
| 41 | + [&](const int iat, |
| 42 | + const ModuleBase::Vector3<double>& tau0, |
| 43 | + const int ibt, |
| 44 | + const ModuleBase::Vector3<double>& tau1, |
| 45 | + const int start, |
| 46 | + const int nw_tot, |
| 47 | + ModuleBase::Vector3<int> dR) { |
| 48 | + const int T1 = ucell.iat2it[ibt]; |
| 49 | + const Atom* atom1 = &ucell.atoms[T1]; |
| 50 | + |
| 51 | + auto all_indexes = ParaO.get_indexes_row(ibt); |
| 52 | + auto col_indexes = ParaO.get_indexes_col(ibt); |
| 53 | + all_indexes.insert(all_indexes.end(), col_indexes.begin(), col_indexes.end()); |
| 54 | + std::sort(all_indexes.begin(), all_indexes.end()); |
| 55 | + all_indexes.erase(std::unique(all_indexes.begin(), all_indexes.end()), all_indexes.end()); |
| 56 | + |
| 57 | + for (size_t iw1l = 0; iw1l < all_indexes.size(); iw1l += this->npol) |
| 58 | + { |
| 59 | + const int iw1 = all_indexes[iw1l] / this->npol; |
| 60 | + const int L1 = atom1->iw2l[iw1]; |
| 61 | + const int N1 = atom1->iw2n[iw1]; |
| 62 | + const int m1 = atom1->iw2m[iw1]; |
| 63 | + const int M1 = (m1 % 2 == 0) ? -m1 / 2 : (m1 + 1) / 2; |
| 64 | + |
| 65 | + std::vector<std::vector<std::complex<double>>> grid_nlm; |
| 66 | + module_rt::snap_phialpha_half_tddft(ORB, |
| 67 | + grid_nlm, |
| 68 | + tau1 * ucell.lat0, |
| 69 | + T1, |
| 70 | + L1, |
| 71 | + m1, |
| 72 | + N1, |
| 73 | + tau0 * ucell.lat0, |
| 74 | + zero_A, |
| 75 | + false, |
| 76 | + options); |
| 77 | + |
| 78 | + std::vector<std::vector<double>> tci_nlm; |
| 79 | + const int T0_fixed = 0; |
| 80 | + overlap_orb_alpha_.snap(T1, |
| 81 | + L1, |
| 82 | + N1, |
| 83 | + M1, |
| 84 | + T0_fixed, |
| 85 | + (tau0 - tau1) * ucell.lat0, |
| 86 | + false, |
| 87 | + tci_nlm); |
| 88 | + |
| 89 | + if (grid_nlm.empty() || tci_nlm.empty() || grid_nlm[0].size() != tci_nlm[0].size()) |
| 90 | + { |
| 91 | + stats.shape_mismatch = true; |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + for (size_t i = 0; i < grid_nlm[0].size(); ++i) |
| 96 | + { |
| 97 | + const double reference_abs = std::abs(tci_nlm[0][i]); |
| 98 | + const double real_diff = std::abs(grid_nlm[0][i].real() - tci_nlm[0][i]); |
| 99 | + if (real_diff > stats.max_real_diff) |
| 100 | + { |
| 101 | + stats.max_real_diff = real_diff; |
| 102 | + stats.reference_at_max_diff = tci_nlm[0][i]; |
| 103 | + } |
| 104 | + stats.max_imag_abs = std::max(stats.max_imag_abs, std::abs(grid_nlm[0][i].imag())); |
| 105 | + stats.max_reference_abs = std::max(stats.max_reference_abs, reference_abs); |
| 106 | + if (reference_abs > 1.0e-8) |
| 107 | + { |
| 108 | + stats.max_relative_diff = std::max(stats.max_relative_diff, real_diff / reference_abs); |
| 109 | + } |
| 110 | + ++stats.compared; |
| 111 | + } |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + const char* instance = std::is_same<T, double>::value ? "gamma" : "multik"; |
| 116 | + std::cout << std::scientific << std::setprecision(12) << "phialpha " << instance << " grid " |
| 117 | + << radial_grid_num << "x" << lebedev_grid_points |
| 118 | + << ": max abs error = " << stats.max_real_diff |
| 119 | + << " (reference = " << stats.reference_at_max_diff << ")" |
| 120 | + << ", max reference = " << stats.max_reference_abs |
| 121 | + << ", max imaginary magnitude = " << stats.max_imag_abs |
| 122 | + << ", max relative error (|reference| > 1e-8) = " << stats.max_relative_diff |
| 123 | + << std::defaultfloat << std::endl; |
| 124 | + |
| 125 | + EXPECT_FALSE(stats.shape_mismatch) << "phialpha grid and two-center integration output shapes differ"; |
| 126 | + EXPECT_GT(stats.compared, 0) << "No phialpha grid entries were compared"; |
| 127 | + EXPECT_LE(stats.max_imag_abs, 1.0e-14) << "max reference abs = " << stats.max_reference_abs; |
| 128 | + return stats; |
| 129 | + }; |
| 130 | + |
| 131 | + const ComparisonStats default_grid = compare_grid(140, 110); |
| 132 | + const ComparisonStats dense_radial_grid = compare_grid(280, 110); |
| 133 | + const ComparisonStats dense_angular_grid = compare_grid(140, 590); |
| 134 | + |
| 135 | + const bool is_gamma = std::is_same<T, double>::value; |
| 136 | + const double default_grid_tolerance = is_gamma ? 6.0e-5 : 1.0e-5; |
| 137 | + const double dense_angular_grid_tolerance = is_gamma ? 5.0e-6 : 4.0e-6; |
| 138 | + |
| 139 | + // The 110-point angular rule limits both radial-grid cases. The 590-point |
| 140 | + // rule exposes the lower error reached by the corrected interpolation. |
| 141 | + EXPECT_LE(default_grid.max_real_diff, default_grid_tolerance); |
| 142 | + EXPECT_LE(dense_radial_grid.max_real_diff, default_grid_tolerance); |
| 143 | + EXPECT_NEAR(dense_radial_grid.max_real_diff, default_grid.max_real_diff, 2.0e-10); |
| 144 | + EXPECT_LE(dense_angular_grid.max_real_diff, dense_angular_grid_tolerance); |
| 145 | + EXPECT_LE(dense_angular_grid.max_real_diff, 0.5 * default_grid.max_real_diff); |
| 146 | +} |
| 147 | + |
| 148 | +template void test_deepks<double>::check_phialpha_grid_zero_field(); |
| 149 | +template void test_deepks<std::complex<double>>::check_phialpha_grid_zero_field(); |
| 150 | + |
| 151 | +template <typename T> |
| 152 | +void run_deepks_unit_phialpha_grid_zero_field(test_deepks<T>& test) |
| 153 | +{ |
| 154 | + test.check_phialpha_grid_zero_field(); |
| 155 | +} |
| 156 | + |
| 157 | +template void run_deepks_unit_phialpha_grid_zero_field<double>(test_deepks<double>& test); |
| 158 | +template void run_deepks_unit_phialpha_grid_zero_field<std::complex<double>>(test_deepks<std::complex<double>>& test); |
0 commit comments