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
70 changes: 61 additions & 9 deletions dpdata/abacus/relax.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import glob
import os

import numpy as np
Expand Down Expand Up @@ -31,7 +32,23 @@ def get_log_file(fname, inlines):
return logf


def get_coords_from_log(loglines, natoms):
def get_relax_stru_files(output_dir):
"""Find the STRU files in the output directory.

Args:
output_dir (str): output directory

Returns
-------
strus: list of STRU files

example:
["STRU_ION1_D", "STRU_ION2_D"]
"""
return glob.glob(os.path.join(output_dir, "STRU_ION*_D"))


def get_coords_from_log(loglines, natoms, stru_files=None):
"""NOTICE: unit of coords and cells is Angstrom
order:
coordinate
Expand Down Expand Up @@ -72,7 +89,12 @@ def get_coords_from_log(loglines, natoms):
coord_direct.append(False)
for k in range(2, 2 + natoms):
coords[-1].append(
list(map(lambda x: float(x) * a0, loglines[i + k].split()[1:4]))
list(
map(
lambda x: float(x) * a0 * bohr2ang,
loglines[i + k].split()[1:4],
)
)
)
else:
assert False, "Unrecongnized coordinate type, %s, line:%d" % ( # noqa: UP031
Expand All @@ -91,7 +113,12 @@ def get_coords_from_log(loglines, natoms):
cells.append([])
for k in range(1, 4):
cells[-1].append(
list(map(lambda x: float(x) * a0, loglines[i + k].split()[0:3]))
list(
map(
lambda x: float(x) * a0 * bohr2ang,
loglines[i + k].split()[0:3],
)
)
)

elif line[1:14] == "final etot is" or "#TOTAL ENERGY#" in line:
Expand All @@ -101,6 +128,27 @@ def get_coords_from_log(loglines, natoms):
# get the energy of current structure
energy.append(float(line.split()[-2]))

# in some relax method (like: bfgs_trad), the coordinate is not outputed in running_relax.log
# but if out_stru is true, then STRU_ION*_D will be outputed in OUT.ABACUS
# we should read cell and coord from STRU_ION*_D files
if len(energy) > 1 and len(coords) == 1:
# the energies of all structrues are collected, but coords have only the first structure
if (
stru_files is not None and len(stru_files) > 1
): # if stru_files are not only STRU_ION_D
stru_file_name = [os.path.basename(i) for i in stru_files]
coords = coords[:1] + [np.nan for i in range(len(energy) - 1)]
coord_direct = coord_direct[:1] + [False for i in range(len(energy) - 1)]
cells = cells[:1] + [np.nan for i in range(len(energy) - 1)]
for iframe in range(1, len(energy)):
if f"STRU_ION{iframe}_D" in stru_file_name:
# read the structure from STRU_ION*_D
stru_data = get_frame_from_stru(
stru_files[stru_file_name.index(f"STRU_ION{iframe}_D")]
)
coords[iframe] = stru_data["coords"][0]
cells[iframe] = stru_data["cells"][0]

force = collect_force(loglines)
stress = collect_stress(loglines)

Expand Down Expand Up @@ -133,7 +181,11 @@ def get_coords_from_log(loglines, natoms):

# delete structures whose energy is np.nan
for i in range(minl):
if np.isnan(energy[i - minl]):
if (
np.isnan(energy[i - minl])
or np.any(np.isnan(coords[i - minl]))
or np.any(np.isnan(cells[i - minl]))
):
del energy[i - minl]
del coords[i - minl]
del cells[i - minl]
Expand All @@ -154,10 +206,6 @@ def get_coords_from_log(loglines, natoms):
if coord_direct[i]:
coords[i] = coords[i].dot(cells[i])

# transfer bohrium to angstrom
cells *= bohr2ang
coords *= bohr2ang

if len(stress) > 0:
virial = np.zeros([len(cells), 3, 3])
for i in range(len(cells)):
Expand Down Expand Up @@ -191,7 +239,11 @@ def get_frame(fname):
with open_file(logf) as f1:
lines = f1.readlines()

energy, cells, coords, force, stress, virial = get_coords_from_log(lines, natoms)
relax_stru_files = get_relax_stru_files(os.path.dirname(logf))

energy, cells, coords, force, stress, virial = get_coords_from_log(
lines, natoms, stru_files=relax_stru_files
)

magmom, magforce = get_mag_force(lines)

Expand Down
54 changes: 54 additions & 0 deletions tests/abacus.relax.readFromSTRUIOND/INPUT
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
INPUT_PARAMETERS RUNNING ABACUS-DFT

#Parameters (1.General)
suffix ABACUS # suffix of OUTPUT DIR
nspin 1 # 1/2/4
symmetry 0 # 0/1
esolver_type ksdft # ksdft/ofdft/sdft/tddft/lj/dp
dft_functional pbe # lda/pbe/scan/hf/pbe0/hse/libxc
ks_solver genelpa # genelpa/scalapack_avx for lcao, cg/dav for pw
vdw_method none # d3/d3_bj/none

#Parameters (2.Iteration)
calculation cell-relax # scf/relax/cell-relax/md
ecutwfc 100 # Rydberg, in lcao is for grid density
scf_thr 1e-7 # Rydberg
scf_nmax 100
#printe 300 # default eq scf_nmax
relax_nmax 30
relax_method bfgs_trad # cg/bfgs/bfgs_trad/sd/"fire"
force_thr_ev 0.05
# stress_thr 1 # GPa

#Parameters (3.Basis)
basis_type lcao # lcao or pw
# kspacing 0.14 0.50 0.14 # replace KPT
gamma_only 1 # 0/1, replace KPT

#Parameters (4.Smearing)
smearing_method mp # mp/gauss/fixed/mp-n/fd
smearing_sigma 0.008 # Rydberg

#Parameters (5.Mixing)
mixing_type broyden # pulay/broyden
mixing_ndim 20
#mixing_beta 0.4 # use default
#mixing_gg0 1.0 # use default

#Parameters (6.Calculation)
cal_force 1
cal_stress 1
#init_chg atomic # atomic/auto/file
#init_wfc atomic # atomic/file
out_stru 1 # print STRU in OUT
out_chg -1 # -1 no, 0 binary, 1 cube, more number as cube prec
out_bandgap 1
out_mul 1 # print charge and mag of atom in mulliken.txt
# out_wfc_lcao 0 # 0 no, 1 txt, 2 dat


# #Parameters (7. Dipole Correction)
# efield_flag 1 # open added potential, if 0, all below useless
# dip_cor_flag 1 # open dipole correction
# efield_dir 1 # direction of dipole correction, 0,1,2 for x,y,z

4 changes: 4 additions & 0 deletions tests/abacus.relax.readFromSTRUIOND/KPT
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
K_POINTS
0
Gamma
1 1 1 0 0 0
34 changes: 34 additions & 0 deletions tests/abacus.relax.readFromSTRUIOND/OUT.ABACUS/STRU_ION1_D
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ATOMIC_SPECIES
Ga 69.7230 Ga.upf upf201
As 74.9216 As.PD04.PBE.UPF upf201

NUMERICAL_ORBITAL
Ga_gga_7au_100Ry_2s2p2d1f.orb
As_gga_8au_100Ry_2s2p1d.orb

LATTICE_CONSTANT
1.8897160000

LATTICE_VECTORS
5.9515362652 -0.0000000000 0.0000000000
-0.0000000000 5.9515362652 -0.0000000000
0.0000000000 -0.0000000000 5.9515362652

ATOMIC_POSITIONS
Direct

Ga #label
0.0000 #magnetism
4 #number of atoms
1.0000000000 0.0000000000 1.0000000000 m 1 1 1
1.0000000000 0.5000000000 0.5000000000 m 1 1 1
0.5000000000 0.0000000000 0.5000000000 m 1 1 1
0.5000000000 0.5000000000 1.0000000000 m 1 1 1

As #label
0.0000 #magnetism
4 #number of atoms
0.2500000000 0.2500000000 0.2500000000 m 1 1 1
0.2500000000 0.7500000000 0.7500000000 m 1 1 1
0.7500000000 0.2500000000 0.7500000000 m 1 1 1
0.7500000000 0.7500000000 0.2500000000 m 1 1 1
34 changes: 34 additions & 0 deletions tests/abacus.relax.readFromSTRUIOND/OUT.ABACUS/STRU_ION2_D
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ATOMIC_SPECIES
Ga 69.7230 Ga.upf upf201
As 74.9216 As.PD04.PBE.UPF upf201

NUMERICAL_ORBITAL
Ga_gga_7au_100Ry_2s2p2d1f.orb
As_gga_8au_100Ry_2s2p1d.orb

LATTICE_CONSTANT
1.8897160000

LATTICE_VECTORS
6.5556050607 -0.0000000000 0.0000000000
-0.0000000000 6.5556050607 -0.0000000000
0.0000000000 -0.0000000000 6.5556050607

ATOMIC_POSITIONS
Direct

Ga #label
0.0000 #magnetism
4 #number of atoms
1.0000000000 0.0000000000 1.0000000000 m 1 1 1
1.0000000000 0.5000000000 0.5000000000 m 1 1 1
0.5000000000 0.0000000000 0.5000000000 m 1 1 1
0.5000000000 0.5000000000 1.0000000000 m 1 1 1

As #label
0.0000 #magnetism
4 #number of atoms
0.2500000000 0.2500000000 0.2500000000 m 1 1 1
0.2500000000 0.7500000000 0.7500000000 m 1 1 1
0.7500000000 0.2500000000 0.7500000000 m 1 1 1
0.7500000000 0.7500000000 0.2500000000 m 1 1 1
34 changes: 34 additions & 0 deletions tests/abacus.relax.readFromSTRUIOND/OUT.ABACUS/STRU_ION3_D
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ATOMIC_SPECIES
Ga 69.7230 Ga.upf upf201
As 74.9216 As.PD04.PBE.UPF upf201

NUMERICAL_ORBITAL
Ga_gga_7au_100Ry_2s2p2d1f.orb
As_gga_8au_100Ry_2s2p1d.orb

LATTICE_CONSTANT
1.8897160000

LATTICE_VECTORS
6.3401004195 -0.0000000000 0.0000000000
-0.0000000000 6.3401004195 -0.0000000000
0.0000000000 -0.0000000000 6.3401004195

ATOMIC_POSITIONS
Direct

Ga #label
0.0000 #magnetism
4 #number of atoms
1.0000000000 0.0000000000 1.0000000000 m 1 1 1
1.0000000000 0.5000000000 0.5000000000 m 1 1 1
0.5000000000 0.0000000000 0.5000000000 m 1 1 1
0.5000000000 0.5000000000 1.0000000000 m 1 1 1

As #label
0.0000 #magnetism
4 #number of atoms
0.2500000000 0.2500000000 0.2500000000 m 1 1 1
0.2500000000 0.7500000000 0.7500000000 m 1 1 1
0.7500000000 0.2500000000 0.7500000000 m 1 1 1
0.7500000000 0.7500000000 0.2500000000 m 1 1 1
34 changes: 34 additions & 0 deletions tests/abacus.relax.readFromSTRUIOND/OUT.ABACUS/STRU_ION4_D
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ATOMIC_SPECIES
Ga 69.7230 Ga.upf upf201
As 74.9216 As.PD04.PBE.UPF upf201

NUMERICAL_ORBITAL
Ga_gga_7au_100Ry_2s2p2d1f.orb
As_gga_8au_100Ry_2s2p1d.orb

LATTICE_CONSTANT
1.8897160000

LATTICE_VECTORS
6.1458183424 -0.0000000000 0.0000000000
-0.0000000000 6.1458183424 -0.0000000000
0.0000000000 -0.0000000000 6.1458183424

ATOMIC_POSITIONS
Direct

Ga #label
0.0000 #magnetism
4 #number of atoms
1.0000000000 0.0000000000 1.0000000000 m 1 1 1
1.0000000000 0.5000000000 0.5000000000 m 1 1 1
0.5000000000 0.0000000000 0.5000000000 m 1 1 1
0.5000000000 0.5000000000 1.0000000000 m 1 1 1

As #label
0.0000 #magnetism
4 #number of atoms
0.2500000000 0.2500000000 0.2500000000 m 1 1 1
0.2500000000 0.7500000000 0.7500000000 m 1 1 1
0.7500000000 0.2500000000 0.7500000000 m 1 1 1
0.7500000000 0.7500000000 0.2500000000 m 1 1 1
34 changes: 34 additions & 0 deletions tests/abacus.relax.readFromSTRUIOND/OUT.ABACUS/STRU_ION5_D
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ATOMIC_SPECIES
Ga 69.7230 Ga.upf upf201
As 74.9216 As.PD04.PBE.UPF upf201

NUMERICAL_ORBITAL
Ga_gga_7au_100Ry_2s2p2d1f.orb
As_gga_8au_100Ry_2s2p1d.orb

LATTICE_CONSTANT
1.8897160000

LATTICE_VECTORS
6.2881167169 -0.0000000039 -0.0000000010
-0.0000000039 6.2881167169 0.0000000005
-0.0000000010 0.0000000005 6.2881167191

ATOMIC_POSITIONS
Direct

Ga #label
0.0000 #magnetism
4 #number of atoms
0.0000000081 0.9999999941 0.9999999959 m 1 1 1
0.0000000081 0.5000000079 0.5000000109 m 1 1 1
0.4999999941 0.9999999941 0.5000000109 m 1 1 1
0.4999999941 0.5000000079 0.9999999959 m 1 1 1

As #label
0.0000 #magnetism
4 #number of atoms
0.2499999989 0.2499999990 0.2499999966 m 1 1 1
0.2499999989 0.7499999990 0.7499999966 m 1 1 1
0.7499999989 0.2499999990 0.7499999966 m 1 1 1
0.7499999989 0.7499999990 0.2499999966 m 1 1 1
34 changes: 34 additions & 0 deletions tests/abacus.relax.readFromSTRUIOND/OUT.ABACUS/STRU_ION_D
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ATOMIC_SPECIES
Ga 69.7230 Ga.upf upf201
As 74.9216 As.PD04.PBE.UPF upf201

NUMERICAL_ORBITAL
Ga_gga_7au_100Ry_2s2p2d1f.orb
As_gga_8au_100Ry_2s2p1d.orb

LATTICE_CONSTANT
1.8897160000

LATTICE_VECTORS
6.2881167169 -0.0000000039 -0.0000000010
-0.0000000039 6.2881167169 0.0000000005
-0.0000000010 0.0000000005 6.2881167191

ATOMIC_POSITIONS
Direct

Ga #label
0.0000 #magnetism
4 #number of atoms
0.0000000081 0.9999999941 0.9999999959 m 1 1 1
0.0000000081 0.5000000079 0.5000000109 m 1 1 1
0.4999999941 0.9999999941 0.5000000109 m 1 1 1
0.4999999941 0.5000000079 0.9999999959 m 1 1 1

As #label
0.0000 #magnetism
4 #number of atoms
0.2499999989 0.2499999990 0.2499999966 m 1 1 1
0.2499999989 0.7499999990 0.7499999966 m 1 1 1
0.7499999989 0.2499999990 0.7499999966 m 1 1 1
0.7499999989 0.7499999990 0.2499999966 m 1 1 1
Loading
Loading