Skip to content

Commit 04ebd55

Browse files
committed
Read xyz containing several structures
1 parent fd72340 commit 04ebd55

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

qstack/compound.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,44 @@ def xyz_comment_line_parser(line):
9090
return props
9191

9292

93+
def xyz_to_mol_all(inp, basis="def2-svp", charge=None, spin=None, ignore=False, unit=None, ecp=None, parse_comment=False):
94+
"""Read xyz file containing multiple molecules.
95+
96+
Args:
97+
inp (str): Path of the xyz file to read.
98+
basis (str or dict): Basis set. Defaults to "def2-svp".
99+
charge (list[int]): Provide/override charge of the molecule. Defaults to None.
100+
spin (list[int]): Provide/override spin of the molecule (alpha electrons - beta electrons). Defaults to None.
101+
ignore (bool): If True, assume molecule is closed-shell and assign charge either 0 or -1. Defaults to False.
102+
unit (str): Provide/override units (Ang or Bohr). Defaults to None.
103+
ecp (str): ECP to use. Defaults to None.
104+
parse_comment (bool): Whether to parse the comment line for properties. Defaults to False.
105+
106+
Returns:
107+
list[pyscf.gto.Mole]: pyscf Mole object containing the molecule information.
108+
"""
109+
with open(inp) as f:
110+
lines = f.read().strip().split('\n')
111+
112+
xyzs = []
113+
cursor = Cursor(action='slicer')
114+
while cursor.i < len(lines):
115+
natm = int(lines[cursor.i].strip())
116+
xyzs.append("\n".join(lines[cursor.add(natm+2)]))
117+
118+
if charge is None:
119+
charge = [None]*len(xyzs)
120+
if spin is None:
121+
spin = [None]*len(xyzs)
122+
123+
mols = []
124+
for xyz, ch, sp in zip(xyzs, charge, spin, strict=True):
125+
mols.append(xyz_to_mol(xyz, basis=basis, charge=ch, spin=sp, ignore=ignore, unit=unit, ecp=ecp, parse_comment=parse_comment))
126+
return mols
127+
128+
93129
def xyz_to_mol(inp, basis="def2-svp", charge=None, spin=None, ignore=False, unit=None, ecp=None, parse_comment=False):
94-
"""Read a molecular file in xyz format and returns a pyscf Mole object.
130+
"""Read a molecular file in xyz format and return a pyscf Mole object.
95131
96132
Args:
97133
inp (str): Path of the xyz file to read, or xyz file contents.

tests/data/2xH2O.xyz

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
3
2+
Water molecule
3+
O 0.00000 0.00000 0.11779
4+
H 0.00000 0.75545 -0.47116
5+
H 0.00000 -0.75545 -0.47116
6+
3
7+
8+
O 0.000000 0.000000 0.000000
9+
H 0.986101 0.000000 -0.000000
10+
H -0.687570 0.606955 0.000000

tests/test_compound.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,20 @@ def test_commentline():
6060
assert mol.charge == -1
6161

6262

63+
def test_multi():
64+
path = os.path.dirname(os.path.realpath(__file__))
65+
molpath = path+'/data/2xH2O.xyz'
66+
mols = compound.xyz_to_mol_all(molpath)
67+
assert len(mols) == 2
68+
assert mols[0].natm == 3
69+
assert mols[1].natm == 3
70+
71+
6372
if __name__ == '__main__':
6473
test_reader()
6574
test_reader_ignore()
6675
test_makeauxmol()
6776
test_rotate_molecule()
6877
test_mol_to_xyz()
6978
test_commentline()
79+
test_multi()

0 commit comments

Comments
 (0)