Skip to content

Commit aef435f

Browse files
committed
Read xyz from orca input
1 parent 7768686 commit aef435f

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

qstack/compound.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ def xyz_to_mol(fin, basis="def2-svp", charge=0, spin=0, ignore=False, unit='ANG'
1818
basis (str or dict): Basis set.
1919
charge (int): Charge of the molecule.
2020
spin (int): Spin of the molecule (alpha electrons - beta electrons).
21+
ignore (bool): If assume molecule closed-shell an assign charge either 0 or -1
22+
unit (str): units (Ang or Bohr)
23+
ecp (str) : ECP to use
2124
2225
Returns:
2326
A pyscf Mole object containing the molecule information.
2427
"""
2528

2629
# Open and read the file
27-
f = open(fin, "r")
28-
molxyz = "\n".join(f.read().split("\n")[2:])
29-
f.close()
30+
with open(fin, "r") as f:
31+
molxyz = "\n".join(f.read().split("\n")[2:])
3032

3133
# Define attributes to the Mole object and build it
3234
mol = gto.Mole()

qstack/orcaio.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,46 @@
66
from qstack.tools import reorder_ao
77

88

9+
def read_input(fname, basis, ecp=None):
10+
"""Read the structure from an Orca input (XYZ coordinates in simple format only)
11+
12+
Note: we do not read basis set info from the file.
13+
TODO: read also %coords block?
14+
15+
Args:
16+
fname (str) : path to file
17+
basis (str/dict) : basis name, path to file, or dict in the pyscf format
18+
Kwargs:
19+
ecp (str) : ECP to use
20+
21+
Returns:
22+
pyscf Mole object.
23+
"""
24+
25+
with open(fname, "r") as f:
26+
lines = [x.strip() for x in f.readlines()]
27+
28+
command_line = '\n'.join(y[1:] for y in filter(lambda x: x.startswith('!'), lines)).lower().split()
29+
if 'bohrs' in command_line:
30+
unit = 'Bohr'
31+
else:
32+
unit = 'Angstrom'
33+
34+
idx_xyz_0, idx_xyz_1 = [y[0] for y in filter(lambda x: x[1].startswith('*'), enumerate(lines))][:2]
35+
charge, mult = map(int, lines[idx_xyz_0][1:].split()[1:])
36+
molxyz = '\n'.join(lines[idx_xyz_0+1:idx_xyz_1])
37+
38+
mol = pyscf.gto.Mole()
39+
mol.atom = molxyz
40+
mol.charge = charge
41+
mol.spin = mult-1
42+
mol.unit = unit
43+
mol.basis = basis
44+
mol.ecp = ecp
45+
mol.build()
46+
return mol
47+
48+
949
def read_density(mol, basename, directory='./', version=500, openshell=False, reorder_dest='pyscf'):
1050
"""Read densities from an ORCA output.
1151

tests/test_orca.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,18 @@ def test_orca_gbw_reader_def2tzvp():
7575
assert np.linalg.norm(mol_dip-mol_dip_true) < 1e-5
7676

7777

78+
def test_orca_input_reader():
79+
path = os.path.dirname(os.path.realpath(__file__))
80+
mol0 = qstack.compound.xyz_to_mol(path+'/data/orca/H2O.xyz', 'sto3g', charge=1, spin=1)
81+
mol = qstack.orcaio.read_input(path+'/data/orca/H2O.orca504.inp', 'sto3g')
82+
assert mol.natm == mol0.natm
83+
assert mol.nelectron == mol0.nelectron
84+
assert np.all(mol.elements==mol0.elements)
85+
assert np.allclose(mol.atom_coords(), mol0.atom_coords())
86+
87+
7888
if __name__ == '__main__':
89+
test_orca_input_reader()
7990
test_orca_density_reader()
8091
test_orca_gbw_reader()
8192
test_orca_gbw_reader_def2tzvp()

0 commit comments

Comments
 (0)