Skip to content

Commit b048b37

Browse files
authored
Make improvements to the format for OpenMX (#619)
Dear developers, I would like to make some improvements on the format for OpenMX. 1. Change the format name from `openmx/out` to `openmx/md` 2. Add the if statement to read geometry optimization trajectories 3. Add the if statement to check if the SCF calculation has converged Thank you in advance.
1 parent 0b8014f commit b048b37

File tree

7 files changed

+1591
-1407
lines changed

7 files changed

+1591
-1407
lines changed

dpdata/openmx/omx.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
energy_convert = EnergyConversion("hartree", "eV").value()
1616
force_convert = ForceConversion("hartree/bohr", "eV/angstrom").value()
1717

18+
import warnings
1819
from collections import OrderedDict
1920

2021
### iterout.c from OpenMX soure code: column numbers and physical quantities ###
@@ -28,6 +29,12 @@
2829
# /* 12: magnetic moment (muB) */
2930
# /* 13,14: angles of spin */
3031

32+
# 15: scf_convergence_flag (optional)
33+
#
34+
# 1. Move the declaration of `scf_convergence_flag` in `DFT.c` to `openmx_common.h`.
35+
# 2. Add `scf_convergence_flag` output to the end of `iterout.c` where `*.md` is written.
36+
# 3. Recompile OpenMX.
37+
3138

3239
def load_atom(lines):
3340
atom_names = []
@@ -70,9 +77,18 @@ def load_cells(lines):
7077
for index, line in enumerate(lines):
7178
if "Cell_Vectors=" in line:
7279
parts = line.split()
73-
cell.append([float(parts[12]), float(parts[13]), float(parts[14])])
74-
cell.append([float(parts[15]), float(parts[16]), float(parts[17])])
75-
cell.append([float(parts[18]), float(parts[19]), float(parts[20])])
80+
if len(parts) == 21: # MD.Type is NVT_NH
81+
cell.append([float(parts[12]), float(parts[13]), float(parts[14])])
82+
cell.append([float(parts[15]), float(parts[16]), float(parts[17])])
83+
cell.append([float(parts[18]), float(parts[19]), float(parts[20])])
84+
elif len(parts) == 16: # MD.Type is Opt
85+
cell.append([float(parts[7]), float(parts[8]), float(parts[9])])
86+
cell.append([float(parts[10]), float(parts[11]), float(parts[12])])
87+
cell.append([float(parts[13]), float(parts[14]), float(parts[15])])
88+
else:
89+
raise RuntimeError(
90+
"Does the file System.Name.md contain unsupported calculation results?"
91+
)
7692
cells.append(cell)
7793
cell = []
7894
cells = np.array(cells)
@@ -104,6 +120,9 @@ def load_coords(lines, atom_names, natoms):
104120
parts = line.split()
105121
for_line = [float(parts[1]), float(parts[2]), float(parts[3])]
106122
coord.append(for_line)
123+
# It may be necessary to recompile OpenMX to make scf convergence determination.
124+
if len(parts) == 15 and parts[14] == "0":
125+
warnings.warn("SCF in System.Name.md has not converged!")
107126
if cnt == natoms:
108127
coords.append(coord)
109128
cnt = 0

dpdata/plugins/openmx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
from dpdata.format import Format
44

55

6-
@Format.register("openmx/out")
6+
@Format.register("openmx/md")
77
class OPENMXFormat(Format):
88
"""Format for the `OpenMX <https://www.openmx-square.org/>`.
99
1010
OpenMX (Open source package for Material eXplorer) is a nano-scale material simulation package based on DFT, norm-conserving pseudopotentials, and pseudo-atomic localized basis functions.
1111
1212
Note that two output files, System.Name.dat and System.Name.md, are required.
1313
14-
Use the `openmx/out` keyword argument to supply this format.
14+
Use the `openmx/md` keyword argument to supply this format.
1515
"""
1616

1717
@Format.post("rot_lower_triangular")

tests/openmx/Methane.md

Lines changed: 1400 additions & 1400 deletions
Large diffs are not rendered by default.

tests/openmx/Methane2.dat

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#
2+
# File Name
3+
#
4+
5+
System.CurrrentDirectory ./ # default=./
6+
System.Name Methane2
7+
level.of.stdout 1 # default=1 (1-3)
8+
level.of.fileout 1 # default=1 (0-2)
9+
10+
#
11+
# Definition of Atomic Species
12+
#
13+
14+
Species.Number 2
15+
<Definition.of.Atomic.Species
16+
H H5.0-s2 H_PBE19
17+
C C5.0-s2p2 C_PBE19
18+
Definition.of.Atomic.Species>
19+
20+
#
21+
# Atoms
22+
#
23+
24+
Atoms.Number 5
25+
Atoms.SpeciesAndCoordinates.Unit Ang # Ang|AU
26+
<Atoms.SpeciesAndCoordinates
27+
1 C 0.300000 0.000000 0.000000 2.0 2.0
28+
2 H -0.889981 -0.629312 0.000000 0.5 0.5
29+
3 H 0.000000 0.629312 -0.889981 0.5 0.5
30+
4 H 0.000000 0.629312 0.889981 0.5 0.5
31+
5 H 0.889981 -0.629312 0.000000 0.5 0.5
32+
Atoms.SpeciesAndCoordinates>
33+
Atoms.UnitVectors.Unit Ang # Ang|AU
34+
#<Atoms.UnitVectors
35+
# 10.0 0.0 0.0
36+
# 0.0 10.0 0.0
37+
# 0.0 0.0 10.0
38+
#Atoms.UnitVectors>
39+
40+
#
41+
# SCF or Electronic System
42+
#
43+
44+
scf.XcType GGA-PBE # LDA|LSDA-CA|LSDA-PW|GGA-PBE
45+
scf.SpinPolarization off # On|Off|NC
46+
scf.ElectronicTemperature 100.0 # default=300 (K)
47+
scf.energycutoff 200.0 # default=150 (Ry)
48+
scf.maxIter 1 # default=40
49+
scf.EigenvalueSolver cluster # DC|GDC|Cluster|Band
50+
scf.Kgrid 1 1 1 # means n1 x n2 x n3
51+
scf.Mixing.Type rmm-diis # Simple|Rmm-Diis|Gr-Pulay|Kerker|Rmm-Diisk
52+
scf.Init.Mixing.Weight 0.30 # default=0.30
53+
scf.Min.Mixing.Weight 0.001 # default=0.001
54+
scf.Max.Mixing.Weight 0.400 # default=0.40
55+
scf.Mixing.History 15 # default=5
56+
scf.Mixing.StartPulay 4 # default=6
57+
scf.criterion 1.0e-8 # default=1.0e-6 (Hartree)
58+
59+
#
60+
# MD or Geometry Optimization
61+
#
62+
63+
MD.Type opt # Nomd|Opt|DIIS|NVE|NVT_VS|NVT_NH
64+
MD.Opt.DIIS.History 7 # default=7
65+
MD.Opt.StartDIIS 5 # default=5
66+
MD.maxIter 5 # default=1
67+
MD.TimeStep 1.0 # default=0.5 (fs)
68+
MD.Opt.criterion 1.0e-4 # default=1.0e-4 (Hartree/bohr)

tests/openmx/Methane2.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
5
2+
time= 0.000 (fs) Energy= -8.15440 (Hartree) Cell_Vectors= 10.00000 0.00000 0.00000 0.00000 10.00000 0.00000 0.00000 0.00000 10.00000
3+
C 0.30000 0.00000 0.00000 -0.36382 0.22843 -0.00000 0.00000 0.00000 0.00000 0.17513 0.00000 0.00000 0.00000 0
4+
H -0.88998 -0.62931 0.00000 0.04918 0.01544 0.00000 0.00000 0.00000 0.00000 -0.07837 0.00000 0.00000 0.00000 0
5+
H 0.00000 0.62931 -0.88998 0.02120 -0.00206 -0.00338 0.00000 0.00000 0.00000 -0.01532 0.00000 0.00000 0.00000 0
6+
H 0.00000 0.62931 0.88998 0.02120 -0.00206 0.00338 0.00000 0.00000 0.00000 -0.01532 0.00000 0.00000 0.00000 0
7+
H 0.88998 -0.62931 0.00000 0.23151 -0.22432 -0.00000 0.00000 0.00000 0.00000 -0.06612 0.00000 0.00000 0.00000 0
8+
5
9+
time= 1.000 (fs) Energy= -8.22382 (Hartree) Cell_Vectors= 10.00000 0.00000 0.00000 0.00000 10.00000 0.00000 0.00000 0.00000 10.00000
10+
C 0.21037 0.05628 -0.00000 -0.12208 -0.03279 0.00000 0.00000 0.00000 0.00000 0.18366 0.00000 0.00000 0.00000 0
11+
H -0.87786 -0.62551 0.00000 0.03791 0.01857 0.00000 0.00000 0.00000 0.00000 -0.08412 0.00000 0.00000 0.00000 0
12+
H 0.00522 0.62881 -0.89081 0.00960 0.01866 -0.02578 0.00000 0.00000 0.00000 -0.03522 0.00000 0.00000 0.00000 0
13+
H 0.00522 0.62881 0.89081 0.00960 0.01866 0.02578 0.00000 0.00000 0.00000 -0.03522 0.00000 0.00000 0.00000 0
14+
H 0.94702 -0.68458 -0.00000 0.04642 -0.02939 -0.00000 0.00000 0.00000 0.00000 -0.02910 0.00000 0.00000 0.00000 0
15+
5
16+
time= 2.000 (fs) Energy= -8.24265 (Hartree) Cell_Vectors= 10.00000 0.00000 0.00000 0.00000 10.00000 0.00000 0.00000 0.00000 10.00000
17+
C 0.12898 0.03442 0.00000 -0.04237 -0.04096 0.00000 0.00000 0.00000 0.00000 0.18611 0.00000 0.00000 0.00000 0
18+
H -0.85259 -0.61313 0.00000 0.01505 0.00643 0.00000 0.00000 0.00000 0.00000 -0.06398 0.00000 0.00000 0.00000 0
19+
H 0.01162 0.64125 -0.90800 0.00781 0.01155 -0.01439 0.00000 0.00000 0.00000 -0.04343 0.00000 0.00000 0.00000 0
20+
H 0.01162 0.64125 0.90800 0.00781 0.01155 0.01439 0.00000 0.00000 0.00000 -0.04343 0.00000 0.00000 0.00000 0
21+
H 0.97796 -0.70417 -0.00000 0.00515 0.00555 -0.00000 0.00000 0.00000 0.00000 -0.03526 0.00000 0.00000 0.00000 0
22+
5
23+
time= 3.000 (fs) Energy= -8.24626 (Hartree) Cell_Vectors= 10.00000 0.00000 0.00000 0.00000 10.00000 0.00000 0.00000 0.00000 10.00000
24+
C 0.10073 0.00711 0.00000 -0.01636 -0.00597 0.00000 0.00000 0.00000 0.00000 0.18796 0.00000 0.00000 0.00000 0
25+
H -0.84256 -0.60884 0.00000 -0.00199 -0.00521 -0.00000 0.00000 0.00000 0.00000 -0.05693 0.00000 0.00000 0.00000 0
26+
H 0.01683 0.64895 -0.91760 0.00694 0.00262 -0.00333 0.00000 0.00000 0.00000 -0.04635 0.00000 0.00000 0.00000 0
27+
H 0.01683 0.64895 0.91760 0.00694 0.00262 0.00333 0.00000 0.00000 0.00000 -0.04635 0.00000 0.00000 0.00000 0
28+
H 0.98139 -0.70048 -0.00000 0.00184 0.00495 -0.00000 0.00000 0.00000 0.00000 -0.03833 0.00000 0.00000 0.00000 0
29+
5
30+
time= 4.000 (fs) Energy= -8.24682 (Hartree) Cell_Vectors= 10.00000 0.00000 0.00000 0.00000 10.00000 0.00000 0.00000 0.00000 10.00000
31+
C 0.08982 0.00313 0.00000 -0.00704 -0.00134 0.00000 0.00000 0.00000 0.00000 0.19038 0.00000 0.00000 0.00000 0
32+
H -0.84389 -0.61232 0.00000 -0.00554 -0.00705 0.00000 0.00000 0.00000 0.00000 -0.05558 0.00000 0.00000 0.00000 0
33+
H 0.02145 0.65069 -0.91982 0.00591 0.00121 -0.00163 0.00000 0.00000 0.00000 -0.04727 0.00000 0.00000 0.00000 0
34+
H 0.02145 0.65069 0.91982 0.00591 0.00121 0.00163 0.00000 0.00000 0.00000 -0.04727 0.00000 0.00000 0.00000 0
35+
H 0.98262 -0.69717 -0.00000 -0.00051 0.00564 -0.00000 0.00000 0.00000 0.00000 -0.04026 0.00000 0.00000 0.00000 0

tests/test_openmx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ def test_coord(self):
5050

5151
class TestOPENMXTraj(unittest.TestCase, TestOPENMXTRAJProps):
5252
def setUp(self):
53-
self.system = dpdata.System("openmx/Methane", fmt="openmx/out")
53+
self.system = dpdata.System("openmx/Methane", fmt="openmx/md")
5454

5555

5656
class TestOPENMXLabeledTraj(unittest.TestCase, TestOPENMXTRAJProps):
5757
def setUp(self):
58-
self.system = dpdata.LabeledSystem("openmx/Methane", fmt="openmx/out")
58+
self.system = dpdata.LabeledSystem("openmx/Methane", fmt="openmx/md")
5959

6060

6161
if __name__ == "__main__":
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import unittest
2+
3+
import numpy as np
4+
from context import dpdata
5+
6+
7+
class TestOPENMXTRAJProps:
8+
def test_atom_names(self):
9+
self.assertEqual(self.system.data["atom_names"], ["C", "H"])
10+
11+
def test_atom_numbs(self):
12+
self.assertEqual(self.system.data["atom_numbs"], [1, 4])
13+
14+
def test_atom_types(self):
15+
for ii in range(0, 1):
16+
self.assertEqual(self.system.data["atom_types"][ii], 0)
17+
for ii in range(1, 5):
18+
self.assertEqual(self.system.data["atom_types"][ii], 1)
19+
20+
def test_cell(self):
21+
ref = 10.0 * np.eye(3)
22+
self.assertEqual(self.system.get_nframes(), 5)
23+
for ff in range(self.system.get_nframes()):
24+
for ii in range(3):
25+
for jj in range(3):
26+
self.assertEqual(self.system["cells"][ff][ii][jj], ref[ii][jj])
27+
28+
def test_coord(self):
29+
with open("openmx/Methane2.md") as md_file:
30+
lines = md_file.readlines()
31+
lines = lines[-5:]
32+
coords = []
33+
for line in lines:
34+
parts = line.split()
35+
for_line = [float(parts[1]), float(parts[2]), float(parts[3])]
36+
coords.append(for_line)
37+
coords = np.array(coords)
38+
celll = 10.0
39+
## Applying PBC ##
40+
for ii in range(5):
41+
for jj in range(3):
42+
if coords[ii][jj] < 0:
43+
coords[ii][jj] += celll
44+
elif coords[ii][jj] >= celll:
45+
coords[ii][jj] -= celll
46+
self.assertAlmostEqual(
47+
self.system["coords"][-1][ii][jj], coords[ii][jj]
48+
)
49+
50+
51+
class TestOPENMXTraj(unittest.TestCase, TestOPENMXTRAJProps):
52+
def setUp(self):
53+
self.system = dpdata.System("openmx/Methane2", fmt="openmx/md")
54+
55+
56+
class TestOPENMXLabeledTraj(unittest.TestCase, TestOPENMXTRAJProps):
57+
def setUp(self):
58+
self.system = dpdata.LabeledSystem("openmx/Methane2", fmt="openmx/md")
59+
60+
61+
if __name__ == "__main__":
62+
unittest.main()

0 commit comments

Comments
 (0)