Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions source/source_pw/module_dfpt/unittests/test_dfpt_phon_elec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,75 @@ class DFPTPhonSerialTest : public ::testing::Test
#include <complex>
#include <vector>

TEST_F(DFPTPhonSerialTest, EqualOccupationSubspaceRotationPreservesTheBilinear)
{
const int npwk = pw_wfc_.npwk[0];
psi::Psi<std::complex<double>> states(1, 2, npwk, npwk, true);
ModuleBase::matrix weights(1, 2);
weights(0, 0) = 1.0;
weights(0, 1) = 1.0;
ModuleDFPT::DFPT_KQ_Basis kq;
kq.init(&pw_wfc_, &pw_rho_, q_cart_, 0);
std::vector<std::vector<std::complex<double>>> responses(2);
for (int band = 0; band < 2; ++band)
{
for (int ig = 0; ig < npwk; ++ig)
{
const double real_argument = 0.31 * ig + band;
const double imaginary_argument = 0.17 * ig - band;
const double real_part = std::sin(real_argument);
const double imaginary_part = std::cos(imaginary_argument);
states(0, band, ig) = std::complex<double>(real_part, imaginary_part);
}
responses[band].resize(kq.get_npwk());
for (int ig = 0; ig < kq.get_npwk(); ++ig)
{
const double real_argument = 0.13 * ig + band;
const double imaginary_argument = 0.37 * ig - band;
const double real_part = std::cos(real_argument);
const double imaginary_part = std::sin(imaginary_argument);
responses[band][ig] = std::complex<double>(real_part, imaginary_part);
}
data_.set_dpsi(0, 0, band, responses[band]);
}
const std::vector<std::complex<double>> no_core;
phon_.accumulate_electron(0, 0, 1, states, weights, data_, no_core);
const ModuleBase::ComplexMatrix reference = phon_.dynmat_accum();
const psi::Psi<std::complex<double>> original = states;
const double inverse_root_two = 1.0 / std::sqrt(2.0);
const std::complex<double> imaginary(0.0, 1.0);
for (int ig = 0; ig < npwk; ++ig)
{
states(0, 0, ig) = inverse_root_two * (original(0, 0, ig) + imaginary * original(0, 1, ig));
states(0, 1, ig) = inverse_root_two * (imaginary * original(0, 0, ig) + original(0, 1, ig));
}
for (int band = 0; band < 2; ++band)
{
std::vector<std::complex<double>> rotated(responses[band].size());
for (size_t ig = 0; ig < rotated.size(); ++ig)
{
rotated[ig] = band == 0 ? inverse_root_two * (responses[0][ig] + imaginary * responses[1][ig])
: inverse_root_two * (imaginary * responses[0][ig] + responses[1][ig]);
}
data_.set_dpsi(0, 0, band, rotated);
}
ModuleDFPT::DFPT_Phon rotated_phon;
rotated_phon.init(ucell_, &pw_rho_, &pert_);
rotated_phon.accumulate_electron(0, 0, 1, states, weights, data_, no_core);
double scale = 0.0;
double error = 0.0;
for (int row = 0; row < 3; ++row)
{
for (int column = 0; column < 3; ++column)
{
scale += std::norm(reference(row, column));
error += std::norm(rotated_phon.dynmat_accum()(row, column) - reference(row, column));
}
}
ASSERT_GT(scale, 1.0e-12);
EXPECT_LT(error / scale, 1.0e-24);
}

TEST_F(DFPTPhonSerialTest, AccumulateElectronAnalyticContraction)
{
// psi: band 0 = single plane wave at G'=0 (c=1, occupied, wg=2),
Expand Down
45 changes: 45 additions & 0 deletions source/source_pw/module_dfpt/unittests/test_dfpt_rho.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,51 @@ class DFPTRhoSerialTest : public ::testing::Test
}
};

TEST_F(DFPTRhoSerialTest, EqualOccupationSubspaceRotationPreservesTheDensity)
{
// Rotate both occupied legs by a complex unitary matrix. Independent
// per-band phases are a weaker test: they cannot detect a loss of the
// cross-band cancellations required in a degenerate occupied space.
psi::Psi<std::complex<double>> states;
std::vector<std::vector<std::vector<std::complex<double>>>> responses;
FillRandomStates(states, responses);
ModuleBase::matrix weights(1, nbands_);
weights(0, 0) = 1.0;
weights(0, 1) = 1.0;
rho_.compute_drho(states, weights, 0, data_);
const std::vector<std::complex<double>> reference = data_.get_drho_g(0, 0);
const psi::Psi<std::complex<double>> original = states;
const double inverse_root_two = 1.0 / std::sqrt(2.0);
const std::complex<double> imaginary(0.0, 1.0);
for (int ig = 0; ig < pw_wfc_.npwk[0]; ++ig)
{
states(0, 0, ig) = inverse_root_two * (original(0, 0, ig) + imaginary * original(0, 1, ig));
states(0, 1, ig) = inverse_root_two * (imaginary * original(0, 0, ig) + original(0, 1, ig));
}
for (int band = 0; band < nbands_; ++band)
{
std::vector<std::complex<double>> rotated(responses[0][band].size());
for (size_t ig = 0; ig < rotated.size(); ++ig)
{
rotated[ig] = band == 0 ? inverse_root_two * (responses[0][0][ig] + imaginary * responses[0][1][ig])
: inverse_root_two * (imaginary * responses[0][0][ig] + responses[0][1][ig]);
}
data_.set_dpsi(0, 0, band, rotated);
}
rho_.compute_drho(states, weights, 0, data_);
const std::vector<std::complex<double>> actual = data_.get_drho_g(0, 0);
ASSERT_EQ(actual.size(), reference.size());
double scale = 0.0;
double error = 0.0;
for (size_t ig = 0; ig < actual.size(); ++ig)
{
scale += std::norm(reference[ig]);
error += std::norm(actual[ig] - reference[ig]);
}
ASSERT_GT(scale, 1.0e-12);
EXPECT_LT(error / scale, 1.0e-25);
}

TEST_F(DFPTRhoSerialTest, ComputeDrhoMatchesBruteForceGSpace)
{
psi::Psi<std::complex<double>> psi;
Expand Down
4 changes: 4 additions & 0 deletions tests/01_PW/CASES_CPU.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,9 @@ scf_sccs_full
relax_sccs
scf_sccs_sa
scf_sccs_sa_pcc_2d_lowpass
dfpt_si_g_aniso
dfpt_si_g_aniso_full
dfpt_bn_g_nlcc
dfpt_bn_g_nlcc_full
dfpt_si_g
dfpt_si_g_symm
34 changes: 34 additions & 0 deletions tests/01_PW/dfpt_bn_g_nlcc/INPUT
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
INPUT_PARAMETERS
suffix autotest
calculation scf
esolver_type dfpt
basis_type pw
ntype 2
nbands 8
symmetry 1
pseudo_dir ../../PP_ORB
ecutwfc 20
ecutrho 80.0
scf_thr 1e-14
scf_nmax 100
smearing_method fixed
dfpt_qfile QFILE
dfpt_compute_q0 true
dfpt_loto true
dfpt_conv_thr 1e-9
dfpt_max_iter 100
dfpt_mix_beta 0.4

dfpt_asr no
dfpt_out_dyn true

pw_diag_thr 1e-14
dfpt_lin_conv_thr 1e-11

nx 20
ny 20
nz 20

ks_solver dav

pseudo_rcut 10
4 changes: 4 additions & 0 deletions tests/01_PW/dfpt_bn_g_nlcc/KPT
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
K_POINTS
0
Gamma
2 2 2 0 0 0
4 changes: 4 additions & 0 deletions tests/01_PW/dfpt_bn_g_nlcc/QFILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Q_POINTS
1
Direct
0 0 0 1.0
1 change: 1 addition & 0 deletions tests/01_PW/dfpt_bn_g_nlcc/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Polar cubic BN Gamma regression with B NLCC, 2x2x2 k mesh, 20/80 Ry and 20^3 FFT. Paired symmetry 1/-1 inputs differ only in symmetry. Autotest checks all frequencies and compares the complete pre-ASR complex force constants, exported dielectric tensor and E-U Born charges after both cases finish. Matrix atol=2e-7 Ry/bohr^2, rtol=1e-7; tensor atol=2e-6, rtol=1e-6, fixed before running. Low-cutoff regression, not converged material properties.
23 changes: 23 additions & 0 deletions tests/01_PW/dfpt_bn_g_nlcc/STRU
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ATOMIC_SPECIES
B 10.811 B.PD04.PBE.UPF
N 14.0067 N_ONCV_PBE-1.0.upf

LATTICE_CONSTANT
1.8897261255

LATTICE_VECTORS
0.0 1.8075 1.8075
1.8075 0.0 1.8075
1.8075 1.8075 0.0

ATOMIC_POSITIONS
Direct

B
0.0
1
0.00000000 0.00000000 0.00000000 1 1 1
N
0.0
1
0.25000000 0.25000000 0.25000000 1 1 1
9 changes: 9 additions & 0 deletions tests/01_PW/dfpt_bn_g_nlcc/dfpt_pair.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"partner": "dfpt_bn_g_nlcc_full",
"dyn": "OUT.autotest/dyn.q0",
"atol": 2e-07,
"rtol": 1e-07,
"tensors": true,
"tensor_atol": 2e-06,
"tensor_rtol": 1e-06
}
18 changes: 18 additions & 0 deletions tests/01_PW/dfpt_bn_g_nlcc/result.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
etotref -339.4710336200116
etotperatomref -169.7355168100
dfpt_freq_q0_m0 49.796097
dfpt_freq_q0_m1 49.796097
dfpt_freq_q0_m2 49.796097
dfpt_freq_q0_m3 1517.847302
dfpt_freq_q0_m4 1517.847302
dfpt_freq_q0_m5 1517.847302
dfpt_loto_q0_m0 49.796097
dfpt_loto_q0_m1 49.796097
dfpt_loto_q0_m2 505.550597
dfpt_loto_q0_m3 1517.847302
dfpt_loto_q0_m4 1517.847302
dfpt_loto_q0_m5 1688.488583
pointgroupref T_d
spacegroupref T_d
nksibzref 3
totaltimeref 20.49
34 changes: 34 additions & 0 deletions tests/01_PW/dfpt_bn_g_nlcc_full/INPUT
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
INPUT_PARAMETERS
suffix autotest
calculation scf
esolver_type dfpt
basis_type pw
ntype 2
nbands 8
symmetry -1
pseudo_dir ../../PP_ORB
ecutwfc 20
ecutrho 80.0
scf_thr 1e-14
scf_nmax 100
smearing_method fixed
dfpt_qfile QFILE
dfpt_compute_q0 true
dfpt_loto true
dfpt_conv_thr 1e-9
dfpt_max_iter 100
dfpt_mix_beta 0.4

dfpt_asr no
dfpt_out_dyn true

pw_diag_thr 1e-14
dfpt_lin_conv_thr 1e-11

nx 20
ny 20
nz 20

ks_solver dav

pseudo_rcut 10
4 changes: 4 additions & 0 deletions tests/01_PW/dfpt_bn_g_nlcc_full/KPT
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
K_POINTS
0
Gamma
2 2 2 0 0 0
4 changes: 4 additions & 0 deletions tests/01_PW/dfpt_bn_g_nlcc_full/QFILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Q_POINTS
1
Direct
0 0 0 1.0
1 change: 1 addition & 0 deletions tests/01_PW/dfpt_bn_g_nlcc_full/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Polar cubic BN Gamma regression with B NLCC, 2x2x2 k mesh, 20/80 Ry and 20^3 FFT. Paired symmetry 1/-1 inputs differ only in symmetry. Autotest checks all frequencies and compares the complete pre-ASR complex force constants, exported dielectric tensor and E-U Born charges after both cases finish. Matrix atol=2e-7 Ry/bohr^2, rtol=1e-7; tensor atol=2e-6, rtol=1e-6, fixed before running. Low-cutoff regression, not converged material properties.
23 changes: 23 additions & 0 deletions tests/01_PW/dfpt_bn_g_nlcc_full/STRU
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ATOMIC_SPECIES
B 10.811 B.PD04.PBE.UPF
N 14.0067 N_ONCV_PBE-1.0.upf

LATTICE_CONSTANT
1.8897261255

LATTICE_VECTORS
0.0 1.8075 1.8075
1.8075 0.0 1.8075
1.8075 1.8075 0.0

ATOMIC_POSITIONS
Direct

B
0.0
1
0.00000000 0.00000000 0.00000000 1 1 1
N
0.0
1
0.25000000 0.25000000 0.25000000 1 1 1
15 changes: 15 additions & 0 deletions tests/01_PW/dfpt_bn_g_nlcc_full/result.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
etotref -339.4710336200117
etotperatomref -169.7355168100
dfpt_freq_q0_m0 49.796120
dfpt_freq_q0_m1 49.796124
dfpt_freq_q0_m2 49.796130
dfpt_freq_q0_m3 1517.847303
dfpt_freq_q0_m4 1517.847303
dfpt_freq_q0_m5 1517.847303
dfpt_loto_q0_m0 49.796120
dfpt_loto_q0_m1 49.796128
dfpt_loto_q0_m2 505.550599
dfpt_loto_q0_m3 1517.847303
dfpt_loto_q0_m4 1517.847303
dfpt_loto_q0_m5 1688.488583
totaltimeref 35.32
29 changes: 29 additions & 0 deletions tests/01_PW/dfpt_si_g_aniso/INPUT
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
INPUT_PARAMETERS
suffix autotest
calculation scf
esolver_type dfpt
nbands 8
symmetry 1
pseudo_dir ../../PP_ORB
ecutwfc 8
ecutrho 32
scf_thr 1e-16
smearing_method fixed
scf_nmax 100
basis_type pw
dfpt_qfile QFILE
dfpt_conv_thr 1e-10
dfpt_lin_conv_thr 1e-12
dfpt_max_iter 100
dfpt_compute_q0 true
dfpt_asr crystal
dfpt_loto true

pw_diag_thr 1e-14
dfpt_out_dyn true

nx 16
ny 16
nz 16

ks_solver dav
4 changes: 4 additions & 0 deletions tests/01_PW/dfpt_si_g_aniso/KPT
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
K_POINTS
0
Gamma
2 2 2 0 0 0
4 changes: 4 additions & 0 deletions tests/01_PW/dfpt_si_g_aniso/QFILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Q_POINTS
1
Direct
0.0000000000 0.0000000000 0.0000000000 1.0
1 change: 1 addition & 0 deletions tests/01_PW/dfpt_si_g_aniso/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dfpt_si_g_aniso: orthorhombically strained diamond Si (Cartesian strains +3%, -2%, +7%), Gamma response, 2x2x2 k mesh, symmetry 1. Same numerical settings as its paired case; 16^3 FFT preserves quarter translations. Tests anisotropic optical frequencies and non-cubic tensor components. Low-cutoff single-rank regression, not converged material properties. Compare raw pre-ASR dyn.q0 with the paired case using validation_tools/compare_dfpt_dyn.py; atol 2e-7 Ry/bohr^2, rtol 1e-7, fixed before running. Autotest now runs this raw matrix and polar-tensor comparison automatically when both paired cases are selected. Both stdout and OUT.autotest/warning.log must pass the convergence gate.
19 changes: 19 additions & 0 deletions tests/01_PW/dfpt_si_g_aniso/STRU
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ATOMIC_SPECIES
Si 28.0855 Si_ONCV_PBE-1.0.upf

LATTICE_CONSTANT
1.8897261255

LATTICE_VECTORS
0.00000000 2.66070000 2.90505000
2.79645000 0.00000000 2.90505000
2.79645000 2.66070000 0.00000000

ATOMIC_POSITIONS
Direct

Si
0.0
2
0.00000000 0.00000000 0.00000000 1 1 1
0.25000000 0.25000000 0.25000000 1 1 1
9 changes: 9 additions & 0 deletions tests/01_PW/dfpt_si_g_aniso/dfpt_pair.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"partner": "dfpt_si_g_aniso_full",
"dyn": "OUT.autotest/dyn.q0",
"atol": 2e-07,
"rtol": 1e-07,
"tensors": true,
"tensor_atol": 2e-06,
"tensor_rtol": 1e-06
}
Loading
Loading