Skip to content

Commit 8d9cef7

Browse files
authored
Improve robustness of openmx/md cell parsing and support new output format (#967)
Dear developers, This PR improves the robustness of the `openmx/md` parser to support multiple output file formats, including both newer and older versions. Main changes: - Improve robustness of `Cell_Vectors` parsing by removing dependency on fixed column positions - Add support for the `scf_conv` flag introduced in OpenMX version 4.0 (upcoming) Thank you for your consideration. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Emits explicit SCF-convergence warnings when parsing OpenMX trajectories. * **Improvements** * More robust parsing of cell vectors across OpenMX MD output variants. * Simplified handling of atomic species ordering/deduplication. * **Tests** * Added new OpenMX test module and Au111Surface test snapshot. * Removed legacy Methane2 snapshot and an older convergence test module. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent a056d78 commit 8d9cef7

7 files changed

Lines changed: 290 additions & 212 deletions

File tree

dpdata/openmx/omx.py

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,6 @@
2727
import warnings
2828
from collections import OrderedDict
2929

30-
### iterout.c from OpenMX soure code: column numbers and physical quantities ###
31-
# /* 1: */
32-
# /* 2,3,4: */
33-
# /* 5,6,7: force *
34-
# /* 8: x-component of velocity */
35-
# /* 9: y-component of velocity */
36-
# /* 10: z-component of velocity */
37-
# /* 11: Net charge, electron charge is defined to be negative. */
38-
# /* 12: magnetic moment (muB) */
39-
# /* 13,14: angles of spin */
40-
41-
# 15: scf_convergence_flag (optional)
42-
#
43-
# 1. Move the declaration of `scf_convergence_flag` in `DFT.c` to `openmx_common.h`.
44-
# 2. Add `scf_convergence_flag` output to the end of `iterout.c` where `*.md` is written.
45-
# 3. Recompile OpenMX.
46-
4730

4831
def load_atom(lines):
4932
atom_names = []
@@ -56,9 +39,8 @@ def load_atom(lines):
5639
elif atom_names_mode:
5740
parts = line.split()
5841
atom_names.append(parts[1])
59-
natoms = len(atom_names)
6042
atom_names_original = atom_names
61-
atom_names = list(OrderedDict.fromkeys(set(atom_names))) # Python>=3.7
43+
atom_names = list(OrderedDict.fromkeys(set(atom_names)))
6244
atom_names = sorted(
6345
atom_names, key=atom_names_original.index
6446
) # Unique ordering of atomic species
@@ -82,24 +64,20 @@ def load_atom(lines):
8264

8365

8466
def load_cells(lines):
85-
cell, cells = [], []
86-
for index, line in enumerate(lines):
67+
cells = []
68+
for line in lines:
8769
if "Cell_Vectors=" in line:
88-
parts = line.split()
89-
if len(parts) == 21: # MD.Type is NVT_NH
90-
cell.append([float(parts[12]), float(parts[13]), float(parts[14])])
91-
cell.append([float(parts[15]), float(parts[16]), float(parts[17])])
92-
cell.append([float(parts[18]), float(parts[19]), float(parts[20])])
93-
elif len(parts) == 16: # MD.Type is Opt
94-
cell.append([float(parts[7]), float(parts[8]), float(parts[9])])
95-
cell.append([float(parts[10]), float(parts[11]), float(parts[12])])
96-
cell.append([float(parts[13]), float(parts[14]), float(parts[15])])
97-
else:
98-
raise RuntimeError(
99-
"Does the file System.Name.md contain unsupported calculation results?"
100-
)
70+
part = line.split("Cell_Vectors=")[1]
71+
parts = part.split()
72+
values = list(map(float, parts[:9]))
73+
cell = [values[0:3], values[3:6], values[6:9]]
10174
cells.append(cell)
102-
cell = []
75+
# Checking SCF converged or not
76+
for token in line.split():
77+
if token.startswith("scf_conv="):
78+
scf_conv = int(token.split("=")[1])
79+
if scf_conv == 0:
80+
warnings.warn("SCF not converged!", stacklevel=2)
10381
cells = np.array(cells)
10482
return cells
10583

@@ -119,7 +97,7 @@ def load_param_file(fname: FileType, mdname: FileType):
11997
def load_coords(lines, atom_names, natoms):
12098
cnt = 0
12199
coord, coords = [], []
122-
for index, line in enumerate(lines):
100+
for line in lines:
123101
if "time=" in line:
124102
continue
125103
for atom_name in atom_names:
@@ -129,9 +107,6 @@ def load_coords(lines, atom_names, natoms):
129107
parts = line.split()
130108
for_line = [float(parts[1]), float(parts[2]), float(parts[3])]
131109
coord.append(for_line)
132-
# It may be necessary to recompile OpenMX to make scf convergence determination.
133-
if len(parts) == 15 and parts[14] == "0":
134-
warnings.warn("SCF in System.Name.md has not converged!")
135110
if cnt == natoms:
136111
coords.append(coord)
137112
cnt = 0
@@ -180,7 +155,7 @@ def load_energy(lines):
180155
def load_force(lines, atom_names, atom_numbs):
181156
cnt = 0
182157
field, fields = [], []
183-
for index, line in enumerate(lines):
158+
for line in lines:
184159
if "time=" in line:
185160
continue
186161
for atom_name in atom_names:
@@ -209,7 +184,7 @@ def to_system_label(fname, mdname):
209184

210185

211186
if __name__ == "__main__":
212-
file_name = "Cdia"
187+
file_name = "Au111Surface"
213188
fname = f"{file_name}.dat"
214189
mdname = f"{file_name}.md"
215190
atom_names, atom_numbs, atom_types, cells = load_param_file(fname, mdname)
@@ -219,7 +194,7 @@ def to_system_label(fname, mdname):
219194
print(atom_names)
220195
print(atom_numbs)
221196
print(atom_types)
222-
# print(cells.shape)
223-
# print(coords.shape)
224-
# print(len(energy))
225-
# print(force.shape)
197+
# print(cells.shape)
198+
# print(coords.shape)
199+
# print(len(energy))
200+
# print(force.shape)

tests/openmx/Au111Surface.dat

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
#
2+
# File Name
3+
#
4+
5+
System.CurrrentDirectory ./ # default=./
6+
System.Name Au111Surface
7+
level.of.stdout 1 # default=1 (1-3)
8+
level.of.fileout 0 # default=1 (1-3)
9+
10+
#
11+
# Definition of Atomic Species
12+
#
13+
14+
Species.Number 2
15+
<Definition.of.Atomic.Species
16+
Au Au7.0-s3p2d2 Au_CA19
17+
H H6.0-s2p1 H_CA19
18+
Definition.of.Atomic.Species>
19+
20+
#
21+
# Atoms
22+
#
23+
24+
Atoms.Number 4
25+
Atoms.SpeciesAndCoordinates.Unit FRAC # Ang|AU
26+
<Atoms.SpeciesAndCoordinates
27+
1 Au 0.10000000000000 0.33333333333333 0.66666666666667 8.5 8.5 0.0 0.0 0.0 0.0 0 off
28+
2 Au 0.11111111111111 0.66666666666667 0.33333333333333 8.5 8.5 0.0 0.0 0.0 0.0 0 off
29+
3 Au 0.12222222222222 0.00000000000000 0.00000000000000 8.5 8.5 0.0 0.0 0.0 0.0 0 off
30+
4 H 0.13333333333333 0.33333333333333 0.66666666666667 0.5 0.5 0.0 0.0 0.0 0.0 0 off
31+
Atoms.SpeciesAndCoordinates>
32+
#5 H 0.14444444444444 0.66666666666667 0.33333333333333 0.5 0.5 0.0 0.0 0.0 0.0 0 off
33+
#6 H 0.15555555555555 0.00000000000000 0.00000000000000 0.5 0.5 0.0 0.0 0.0 0.0 0 off
34+
35+
Atoms.UnitVectors.Unit Ang # Ang|AU
36+
<Atoms.UnitVectors
37+
211.862723 0.000000 0.000000
38+
0.000000 2.883086 0.000000
39+
0.000000 -1.441543 2.496826
40+
Atoms.UnitVectors>
41+
42+
#
43+
# SCF or Electronic System
44+
#
45+
46+
scf.XcType LSDA-CA # LDA|LSDA-CA|LSDA-PW
47+
scf.SpinPolarization NC # On|Off|NC
48+
scf.partialCoreCorrection on # On|Off
49+
scf.Hubbard.U off # On|Off , default=off
50+
scf.Hubbard.Occupation dual # onsite|full|dual , default=dual
51+
scf.SpinOrbit.Coupling on # On|Off , default=off
52+
scf.Constraint.NC.Spin off # On|Off , default=off
53+
scf.Constraint.NC.Spin.v 1.0 # default=0.0(ev)
54+
scf.ElectronicTemperature 100.0 # default=300 (K)
55+
scf.energycutoff 200.0 # default=150 (Ry)
56+
scf.maxIter 500 # default=40
57+
scf.EigenvalueSolver Band # Recursion|Cluster|Band
58+
scf.Kgrid 1 12 12 # means n1 x n2 x n3
59+
scf.ProExpn.VNA on # on|off, default=on
60+
scf.Mixing.Type Rmm-Diisk # Simple|Rmm-Diis|Gr-Pulay
61+
scf.Init.Mixing.Weight 0.01 # default=0.30
62+
scf.Min.Mixing.Weight 0.001 # default=0.001
63+
scf.Max.Mixing.Weight 0.01 # default=0.40
64+
scf.Mixing.History 30 # default=5
65+
scf.Mixing.StartPulay 20 # default=6
66+
scf.criterion 1.0e-8 # default=1.0e-6 (Hartree)
67+
ESM.switch off # off, on1=v|v|v, on2=m|v|m, on3=v|v|m, on4=on2+EF
68+
ESM.buffer.range 4.5 # default=10.0 (ang)
69+
ESM.wall.switch on
70+
ESM.wall.position 6.7 # default=10.0 (ang)
71+
ESM.wall.height 100.0 # default=100.0 (eV)
72+
73+
#
74+
# 1D FFT
75+
#
76+
77+
1DFFT.NumGridK 900 # default=900
78+
1DFFT.NumGridR 900 # default=900
79+
1DFFT.EnergyCutoff 3600.0 # default=3DFFT.EnergyCutoff*3.0 (Ry)
80+
81+
#
82+
# Orbital Optimization
83+
#
84+
85+
orbitalOpt.Method off # Off|Unrestricted|Restricted
86+
orbitalOpt.InitCoes Symmetrical # Symmetrical|Free
87+
orbitalOpt.initPrefactor 0.1 # default=0.1
88+
orbitalOpt.scf.maxIter 15 # default=12
89+
orbitalOpt.MD.maxIter 5 # default=5
90+
orbitalOpt.per.MDIter 20 # default=1000000
91+
orbitalOpt.criterion 1.0e-4 # default=1.0e-4 (Hartree/borh)^2
92+
93+
#
94+
# output of contracted orbitals
95+
#
96+
97+
CntOrb.fileout off # on|off, default=off
98+
Num.CntOrb.Atoms 3 # default=1
99+
<Atoms.Cont.Orbitals
100+
1
101+
5
102+
9
103+
Atoms.Cont.Orbitals>
104+
105+
#
106+
# SCF Order-N
107+
#
108+
109+
orderN.HoppingRanges 9.2 # default=5.0 (Ang)
110+
orderN.NumHoppings 3 # default=2
111+
orderN.KrylovH.order 600 # default=400
112+
orderN.recalc.EM off
113+
114+
#
115+
# restart using *.rst
116+
#
117+
118+
scf.restart off
119+
120+
121+
#
122+
# MD or Geometry Optimization
123+
#
124+
125+
MD.Type Nomd # Nomd|Constant_Energy_MD|Opt
126+
MD.maxIter 1000 # default=1
127+
MD.Opt.DIIS.History 3 # default=3
128+
MD.Opt.StartDIIS 5 # default=5
129+
MD.TimeStep 1 # default=0.5 (fs)
130+
MD.Opt.criterion 1.0e-4 # default=1.0e-4 (Hartree/bohr)
131+
132+
#
133+
# Band dispersion
134+
#
135+
136+
Band.dispersion off # on|off, default=off
137+
#Band.Nkpath 3
138+
#<Band.kpath
139+
# 135 0.0 0.500000 0.000000 0.0 0.000000 0.000000 M G
140+
# 135 0.0 0.000000 0.000000 0.0 0.333333 0.333333 G K
141+
# 135 0.0 0.333333 0.333333 0.0 0.500000 0.000000 K M
142+
#Band.kpath>
143+
144+
145+
#
146+
# MO output
147+
#
148+
149+
MO.fileout off # on|off
150+
num.HOMOs 3 # default=2
151+
num.LUMOs 3 # default=2
152+
153+
MO.Nkpoint 1 # default=1
154+
<MO.kpoint
155+
0.0 0.0 0.0
156+
MO.kpoint>
157+
158+
#
159+
# DOS and PDOS
160+
#
161+
162+
DosGauss.fileout off # on|off , default=off
163+
DosGauss.Num.Mesh 500 # default=200
164+
DosGauss.Width 0.1 # default=0.2(eV)
165+
166+
Dos.fileout off # on|off, default=off
167+
Dos.Erange -10.0 10.0 # default = -20 20
168+
Dos.Kgrid 12 12 12 # default = Kgrid1 Kgrid2 Kgrid3
169+
170+
171+
HS.fileout on # on|off, default=off
172+
173+
174+
#
175+
# Spin textures; Mulliken population
176+
#
177+
178+
Filename.scfout Au111Surface.scfout # default: default
179+
Filename.outdata Au111Surface_BD # default: default
180+
Calc.Type BandDispersion # default: MulPOnly
181+
Energy.Range -1.0 1.0 # eV; default: 0.0 0.0
182+
Band.Nkpath 2
183+
<Band.kpath
184+
135 0.0 0.500000 0.000000 0.0 0.000000 0.000000 M G
185+
135 0.0 0.000000 0.000000 0.0 -0.500000 0.000000 G -M
186+
Band.kpath>
187+
Filename.atomMulP Au111Surface_BD.AMulPBand # default: default
188+
Filename.xyzdata Au111Surface_BD_MC # default: default
189+
Num.of.Extract.Atom 3 # default: 1
190+
Extract.Atom 1 2 3 # default: 1 2 ... (Num.of.Extract.Atom)
191+
MulP.Vec.Scale 0.1 0.1 0.1 # default: 1.0 1.0 1.0
192+
Data.Reduction 1 # default: 1

tests/openmx/Au111Surface.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
4
2+
time= 0.000 (fs) Energy= -317.65405 (Hartree) Cell_Vectors= 211.86272 0.00000 0.00000 0.00000 2.88309 0.00000 0.00000 -1.44154 2.49683 scf_conv=1
3+
Au 21.18627 -0.00000 1.66455 0.00620 0.00000 -0.00012 0.00000 0.00000 0.00000 0.00297 0.00000 159.37740 172.68083
4+
Au 23.54030 1.44154 0.83228 0.00359 -0.00000 0.00000 0.00000 0.00000 0.00000 -0.00169 0.00000 156.20488 170.35407
5+
Au 25.89433 0.00000 0.00000 0.01489 -0.00000 0.00011 0.00000 0.00000 0.00000 0.03068 0.00000 170.57058 146.22112
6+
H 28.24836 -0.00000 1.66455 -0.02466 -0.00000 0.00001 0.00000 0.00000 0.00000 -0.03197 0.00000 16.47069 -11.89518
7+
4
8+
time= 1.000 (fs) Energy= -317.65405 (Hartree) Cell_Vectors= 211.86272 0.00000 0.00000 0.00000 2.88309 0.00000 0.00000 -1.44154 2.49683 scf_conv=0
9+
Au 21.18627 -0.00000 1.66455 -0.12489 -0.00000 -0.00012 0.00000 0.00000 0.00000 0.00297 0.00000 159.37740 172.68083
10+
Au 23.54030 1.44154 0.83228 -0.00359 -0.00000 0.00000 0.00000 0.00000 0.00000 -0.00169 0.00000 156.20488 170.35407
11+
Au 25.89433 0.00000 0.00000 0.07945 0.00000 0.00011 0.00000 0.00000 0.00000 0.03068 0.00000 170.57058 146.22112
12+
H 28.24836 -0.00000 1.66455 0.04904 0.00000 0.00001 0.00000 0.00000 0.00000 -0.03197 0.00000 16.47069 -11.89518

tests/openmx/Methane2.dat

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)