|
| 1 | +#include "gtest/gtest.h" |
| 2 | +#include "source_estate/module_dm/density_matrix.h" |
| 3 | + |
| 4 | +#include <complex> |
| 5 | +#include <cmath> |
| 6 | + |
| 7 | +/************************************************************************ |
| 8 | + * Regression test for the nspin=4 (non-collinear/SOC) magnetization |
| 9 | + * round-trip through the density-matrix pipeline. |
| 10 | + * |
| 11 | + * Physical invariant (must hold regardless of internal sign conventions): |
| 12 | + * the magnetization <sigma> of the occupied one-electron state that is |
| 13 | + * encoded in the density matrix must be recovered, with the CORRECT SIGN |
| 14 | + * in ALL THREE cartesian components, by func_xyz_to_updown(). |
| 15 | + * |
| 16 | + * Why this test exists (regression for the #7664 nspin=4 m_y sign flip): |
| 17 | + * ABACUS builds the k-space DM as DM_{ab} = sum_n w_n conj(c_{n,a}) c_{n,b} |
| 18 | + * (cal_dm_psi.cpp: the conj() is applied to the FIRST index a). Hence the |
| 19 | + * stored DM block is the complex conjugate of the physical 1-RDM P: |
| 20 | + * DM_{up,dn} = conj(c_up) c_dn = conj(P_{up,dn}). |
| 21 | + * Since m_x, m_z read Re() (conjugation-invariant) but m_y reads Im(), |
| 22 | + * ONLY m_y is sensitive to this conjugation. func_xyz_to_updown() must be |
| 23 | + * consistent with that stored convention. PR #7664 set the m_y extraction |
| 24 | + * to the "bare" textbook formula (valid for P, not for conj(P)), which |
| 25 | + * flips m_y for in-plane moments and quenches non-collinear order |
| 26 | + * (e.g. Mn3Sn 120-degree AFM). This test pins m_y down. |
| 27 | + * |
| 28 | + * The helper build_DM_block_as_cal_dm_psi() MUST mirror cal_dm_psi.cpp. If |
| 29 | + * that convention is ever changed (e.g. the "upstream" fix that makes the DM |
| 30 | + * hold the physical P), update the helper in the SAME commit so this test |
| 31 | + * keeps asserting the physical invariant. |
| 32 | + ************************************************************************/ |
| 33 | + |
| 34 | +namespace |
| 35 | +{ |
| 36 | +using cd = std::complex<double>; |
| 37 | + |
| 38 | +// spinor of the occupied state with <sigma> = mhat (the +1 eigenstate of mhat.sigma) |
| 39 | +void spinor_from_direction(const double mhat[3], cd c[2]) |
| 40 | +{ |
| 41 | + // |+n> = (cos(th/2), sin(th/2) e^{i ph}); n=(sin th cos ph, sin th sin ph, cos th) |
| 42 | + const double th = std::acos(std::max(-1.0, std::min(1.0, mhat[2]))); |
| 43 | + const double ph = std::atan2(mhat[1], mhat[0]); |
| 44 | + c[0] = cd(std::cos(0.5 * th), 0.0); |
| 45 | + c[1] = std::sin(0.5 * th) * cd(std::cos(ph), std::sin(ph)); |
| 46 | +} |
| 47 | + |
| 48 | +// Build the 4 spinor-block DM elements EXACTLY as cal_dm_psi.cpp stores them: |
| 49 | +// DM_{a,b} = sum_occ w * conj(c_a) * c_b (conj on the first index) |
| 50 | +// layout tmp = {uu, ud, du, dd} |
| 51 | +void build_DM_block_as_cal_dm_psi(const cd c[2], double w, cd tmp[4]) |
| 52 | +{ |
| 53 | + tmp[0] = w * std::conj(c[0]) * c[0]; // uu |
| 54 | + tmp[1] = w * std::conj(c[0]) * c[1]; // ud |
| 55 | + tmp[2] = w * std::conj(c[1]) * c[0]; // du |
| 56 | + tmp[3] = w * std::conj(c[1]) * c[1]; // dd |
| 57 | +} |
| 58 | + |
| 59 | +// physical magnetization of a normalized spinor: m_i = <c| sigma_i |c> |
| 60 | +void physical_m(const cd c[2], double m[3]) |
| 61 | +{ |
| 62 | + m[0] = 2.0 * std::real(std::conj(c[0]) * c[1]); |
| 63 | + m[1] = 2.0 * std::imag(std::conj(c[0]) * c[1]); |
| 64 | + m[2] = std::norm(c[0]) - std::norm(c[1]); |
| 65 | +} |
| 66 | +} // namespace |
| 67 | + |
| 68 | +TEST(SocMagnetizationRoundtrip, ExtractRecoversPhysicalMagnetization) |
| 69 | +{ |
| 70 | + // several magnetization directions, all with a nonzero transverse (y) part |
| 71 | + const double dirs[5][3] = { |
| 72 | + {0.0, 1.0, 0.0}, // pure +y (the critical case) |
| 73 | + {0.0, -1.0, 0.0}, // pure -y (like Mn3Sn atom-1) |
| 74 | + {0.6, 0.8, 0.0}, // in-plane 120-deg-like |
| 75 | + {0.36, 0.48, -0.8}, // general 3D |
| 76 | + {-0.5, 0.5, 0.70710678}, // general 3D |
| 77 | + }; |
| 78 | + |
| 79 | + // step_trace for a single 2x2 spinor block written contiguously as a 2x2 (col_size=2) |
| 80 | + const int col_size = 2; |
| 81 | + const int step_trace[4] = {0, 1, col_size, col_size + 1}; |
| 82 | + |
| 83 | + for (const auto& mhat : dirs) |
| 84 | + { |
| 85 | + cd c[2]; |
| 86 | + spinor_from_direction(mhat, c); |
| 87 | + |
| 88 | + double m_ref[3]; |
| 89 | + physical_m(c, m_ref); // the TRUE magnetization encoded in the state |
| 90 | + |
| 91 | + cd tmp[4]; |
| 92 | + build_DM_block_as_cal_dm_psi(c, 1.0, tmp); |
| 93 | + |
| 94 | + // 2x2 output buffer (row-major), func writes rho0/x/y/z into step_trace slots at icol=0 |
| 95 | + double out[4] = {0, 0, 0, 0}; |
| 96 | + elecstate::DensityMatrix_Tools::func_xyz_to_updown<double>(tmp, 0, step_trace, out); |
| 97 | + |
| 98 | + const double mx = out[step_trace[1]]; |
| 99 | + const double my = out[step_trace[2]]; |
| 100 | + const double mz = out[step_trace[3]]; |
| 101 | + |
| 102 | + EXPECT_NEAR(mx, m_ref[0], 1e-10) << "m_x wrong for dir (" << mhat[0] << "," << mhat[1] << "," << mhat[2] << ")"; |
| 103 | + EXPECT_NEAR(my, m_ref[1], 1e-10) << "m_y SIGN/VALUE wrong (transverse channel, #7664 regression) for dir (" |
| 104 | + << mhat[0] << "," << mhat[1] << "," << mhat[2] << ")"; |
| 105 | + EXPECT_NEAR(mz, m_ref[2], 1e-10) << "m_z wrong for dir (" << mhat[0] << "," << mhat[1] << "," << mhat[2] << ")"; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// Same invariant for the <complex> (multi-k) specialization, which is changed identically. |
| 110 | +// For a single occupied state the 2x2 block is Hermitian, so the extracted Pauli components come |
| 111 | +// out real and must equal the physical magnetization; the imaginary parts must vanish. |
| 112 | +TEST(SocMagnetizationRoundtrip, ComplexSpecializationRecoversPhysicalMagnetization) |
| 113 | +{ |
| 114 | + const double dirs[4][3] = { |
| 115 | + {0.0, 1.0, 0.0}, {0.0, -1.0, 0.0}, {0.6, 0.8, 0.0}, {0.36, 0.48, -0.8}, |
| 116 | + }; |
| 117 | + const int col_size = 2; |
| 118 | + const int step_trace[4] = {0, 1, col_size, col_size + 1}; |
| 119 | + |
| 120 | + for (const auto& mhat : dirs) |
| 121 | + { |
| 122 | + cd c[2]; |
| 123 | + spinor_from_direction(mhat, c); |
| 124 | + double m_ref[3]; |
| 125 | + physical_m(c, m_ref); |
| 126 | + |
| 127 | + cd tmp[4]; |
| 128 | + build_DM_block_as_cal_dm_psi(c, 1.0, tmp); |
| 129 | + |
| 130 | + cd out[4] = {cd(0, 0), cd(0, 0), cd(0, 0), cd(0, 0)}; |
| 131 | + elecstate::DensityMatrix_Tools::func_xyz_to_updown<std::complex<double>>(tmp, 0, step_trace, out); |
| 132 | + |
| 133 | + EXPECT_NEAR(out[step_trace[1]].real(), m_ref[0], 1e-10) << "m_x"; |
| 134 | + EXPECT_NEAR(out[step_trace[2]].real(), m_ref[1], 1e-10) << "m_y (complex specialization)"; |
| 135 | + EXPECT_NEAR(out[step_trace[3]].real(), m_ref[2], 1e-10) << "m_z"; |
| 136 | + EXPECT_NEAR(out[step_trace[1]].imag(), 0.0, 1e-10); |
| 137 | + EXPECT_NEAR(out[step_trace[2]].imag(), 0.0, 1e-10); |
| 138 | + EXPECT_NEAR(out[step_trace[3]].imag(), 0.0, 1e-10); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +int main(int argc, char** argv) |
| 143 | +{ |
| 144 | + testing::InitGoogleTest(&argc, argv); |
| 145 | + return RUN_ALL_TESTS(); |
| 146 | +} |
0 commit comments