Skip to content

Commit

Permalink
abacus: fix bug in reading nspin2 results
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Nov 25, 2024
1 parent 5916b4f commit ba4cb93
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
10 changes: 8 additions & 2 deletions dpdata/abacus/scf.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,14 +478,20 @@ def get_mag_force(outlines):
j = i + 2
mag = []
while "-------------------------" not in outlines[j]:
mag.append([float(ii) for ii in outlines[j].split()[1:]])
imag = [float(ii) for ii in outlines[j].split()[1:]]
if len(imag) == 1:
imag = [0, 0, imag[0]]
mag.append(imag)
j += 1
mags.append(mag)
if "Magnetic force (eV/uB)" in line:
j = i + 2
magforce = []
while "-------------------------" not in outlines[j]:
magforce.append([float(ii) for ii in outlines[j].split()[1:]])
imagforce = [float(ii) for ii in outlines[j].split()[1:]]
if len(imagforce) == 1:
imagforce = [0, 0, imagforce[0]]
magforce.append(imagforce)
j += 1
magforces.append(magforce)
return np.array(mags), np.array(magforces)
Expand Down
4 changes: 2 additions & 2 deletions tests/abacus.spin/INPUT
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
INPUT_PARAMETERS
suffix ABACUS
suffix ABACUS-nspin2
calculation scf
ecutwfc 100
scf_thr 1e-07
Expand All @@ -14,7 +14,7 @@ basis_type lcao
symmetry 0
noncolin 1
lspinorb 0
nspin 4
nspin 2
out_mul true
sc_mag_switch 1
decay_grad_switch 1
Expand Down
27 changes: 27 additions & 0 deletions tests/abacus.spin/INPUT.scf.nspin2
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
INPUT_PARAMETERS
suffix ABACUS-nspin2
calculation scf
ecutwfc 100
scf_thr 1e-07
scf_nmax 100
out_chg 0
smearing_method gauss
smearing_sigma 0.01
mixing_type broyden
mixing_ndim 10
ks_solver genelpa
basis_type lcao
symmetry 0
noncolin 1
lspinorb 0
nspin 2
out_mul true
sc_mag_switch 1
decay_grad_switch 1
sc_thr 1e-07
sc_scf_thr 1e-09
nsc 100
nsc_min 2
alpha_trial 0.01
sccut 3
cal_force 1
34 changes: 34 additions & 0 deletions tests/test_abacus_spin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,40 @@ def test_scf(self):
np.testing.assert_almost_equal(
data["force_mags"], sys2.data["force_mags"], decimal=8
)

def test_scf_nspin2(self):
os.system("cp abacus.spin/INPUT.scf.nspin2 abacus.spin/INPUT")
mysys = dpdata.LabeledSystem("abacus.spin", fmt="abacus/scf")
data = mysys.data
self.assertAlmostEqual(data["energies"][0], -6818.719409466637)
np.testing.assert_almost_equal(
data["spins"][0],
[
[0,0,2.4000001004],
[0,0,2.3999994597],
],
decimal=8,
)
np.testing.assert_almost_equal(
data["force_mags"][0],
[
[0,0, -0.3669618965],
[0,0, -0.3669821632],
],
decimal=8,
)

# dump to deepmd-npy
mysys.to(file_name=self.dump_path, fmt="deepmd/npy")
self.assertTrue(os.path.isfile(f"{self.dump_path}/set.000/spin.npy"))
self.assertTrue(os.path.isfile(f"{self.dump_path}/set.000/force_mag.npy"))

sys2 = dpdata.LabeledSystem(self.dump_path, fmt="deepmd/npy")
np.testing.assert_almost_equal(data["spins"], sys2.data["spins"], decimal=8)
np.testing.assert_almost_equal(
data["force_mags"], sys2.data["force_mags"], decimal=8
)


def test_relax(self):
os.system("cp abacus.spin/INPUT.relax abacus.spin/INPUT")
Expand Down

0 comments on commit ba4cb93

Please sign in to comment.