File: test_gaussian_out.py

package info (click to toggle)
python-ase 3.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,484 kB
  • sloc: python: 148,112; xml: 2,728; makefile: 110; javascript: 47
file content (358 lines) | stat: -rw-r--r-- 14,018 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# fmt: off
"""Tests for the gaussian-out format."""
from io import StringIO

import numpy as np
import pytest

from ase import units
from ase.io import read
from ase.io.formats import match_magic

BUF_H2O = r"""
 Entering Gaussian System, Link 0=g16

...

 ******************************************
 Gaussian 16:  ES64L-G16RevA.03 25-Dec-2016
                 6-Apr-2021
 ******************************************

...

                          Input orientation:
 ---------------------------------------------------------------------
 Center     Atomic      Atomic             Coordinates (Angstroms)
 Number     Number       Type             X           Y           Z
 ---------------------------------------------------------------------
      1          8           0        0.000000    0.000000    0.119262
      2          1           0        0.000000    0.763239   -0.477047
      3          1           0        0.000000   -0.763239   -0.477047
 ---------------------------------------------------------------------

...

 SCF Done:  E(RHF) =  -75.9834173665     A.U. after   10 cycles
"""

BUF_H2O_MULLIKEN = r"""
 (Enter /opt/bwhpc/common/chem/gaussian/g16.C.01/x86_64-Intel-avx2-source/g16/l601.exe)
...
 Mulliken charges:
               1
     1  O   -0.792441
     2  H    0.396221
     3  H    0.396221
 Sum of Mulliken charges =  -0.00000
"""  # noqa: E501

BUF_H2O_LOWDIN = r"""
 Lowdin Atomic Charges:
               1
     1  O   -0.584488
     2  H    0.292244
     3  H    0.292244
 Sum of Lowdin charges  =  -0.00000
"""

BUF_H2O_L601_DIPOLE = r"""
 Dipole moment (field-independent basis, Debye):
    X=              0.0000    Y=             -0.0000    Z=             -2.6431  Tot=              2.6431
"""  # noqa: E501

BUF_H2O_HIRSHFELD = r"""
 Hirshfeld charges, spin densities, dipoles, and CM5 charges using IRadAn=      5:
              Q-H        S-H        Dx         Dy         Dz        Q-CM5
     1  O   -0.338600   0.000000   0.000000  -0.000000  -0.367327  -0.665793
     2  H    0.169300   0.000000  -0.000000   0.161888  -0.145504   0.332897
     3  H    0.169300   0.000000  -0.000000  -0.161888  -0.145504   0.332897
       Tot  -0.000000   0.000000  -0.000000  -0.000000  -0.658335  -0.000000
"""  # noqa: E501

BUF_H2O_L716 = r"""
 (Enter /opt/bwhpc/common/chem/gaussian/g16.C.01/x86_64-Intel-avx2-source/g16/l716.exe)
 Dipole        = 3.27065103D-16-1.33226763D-15-1.03989005D+00
 -------------------------------------------------------------------
 Center     Atomic                   Forces (Hartrees/Bohr)
 Number     Number              X              Y              Z
 -------------------------------------------------------------------
      1        8          -0.000000000   -0.000000000   -0.036558637
      2        1          -0.000000000   -0.003968101    0.018279318
      3        1           0.000000000    0.003968101    0.018279318
 -------------------------------------------------------------------
"""  # noqa: E501

BUF_O2 = r"""
                          Input orientation:
 ---------------------------------------------------------------------
 Center     Atomic      Atomic             Coordinates (Angstroms)
 Number     Number       Type             X           Y           Z
 ---------------------------------------------------------------------
      1          8           0        0.000000    0.000000    0.622978
      2          8           0        0.000000    0.000000   -0.622978
 ---------------------------------------------------------------------

...

 SCF Done:  E(UHF) =  -149.541919412     A.U. after   10 cycles
"""

BUF_O2_MULLIKEN = r"""
 (Enter /opt/bwhpc/common/chem/gaussian/g16.C.01/x86_64-Intel-avx2-source/g16/l601.exe)
...
 Mulliken charges and spin densities:
               1          2
     1  O    0.000000   1.000000
     2  O   -0.000000   1.000000
 Sum of Mulliken charges =  -0.00000   2.00000
"""  # noqa: E501

BUF_F2_RHF = r"""
 Entering Gaussian System, Link 0=g16
...
                          Input orientation:
 ---------------------------------------------------------------------
 Center     Atomic      Atomic             Coordinates (Angstroms)
 Number     Number       Type             X           Y           Z
 ---------------------------------------------------------------------
      1          9           0        0.000000    0.000000    0.700000
      2          9           0        0.000000    0.000000   -0.700000
 ---------------------------------------------------------------------
...
 SCF Done:  E(RHF) =  -198.700044583     A.U. after    8 cycles
"""

BUF_F2_MP2 = BUF_F2_RHF + r"""
...
 E2 =    -0.4264521750D+00 EUMP2 =    -0.19912649675787D+03
"""

BUF_F2_CCSD = BUF_F2_MP2 + r"""
...
 Wavefunction amplitudes converged. E(Corr)=     -199.13391098
"""

BUF_F2_CCSD_T = BUF_F2_CCSD + r"""
...
 CCSD(T)= -0.19914648303D+03
"""

BUF_H2O_OPT = r""" Entering Gaussian System, Link 0=g16
 Initial command:
...
(Enter /soft/gaussian/16-c.02/g16/l202.exe)
                          Input orientation:
 ---------------------------------------------------------------------
 Center     Atomic      Atomic             Coordinates (Angstroms)
 Number     Number       Type             X           Y           Z
 ---------------------------------------------------------------------
      1          8           0        0.000000    0.000000    0.117782
      2          1           0        0.000000    0.756577   -0.476307
      3          1           0       -0.000000   -0.756577   -0.476307
 ---------------------------------------------------------------------
                    Distance matrix (angstroms):
                    1          2          3
     1  O    0.000000
     2  H    0.961952   0.000000
     3  H    0.961952   1.513154   0.000000
 Stoichiometry    H2O
 Framework group  C2V[C2(O),SGV(H2)]
 Deg. of freedom     2
 Full point group                 C2V     NOp   4
 RotChk:  IX=0 Diff= 3.20D-16
 Largest Abelian subgroup         C2V     NOp   4
 Largest concise Abelian subgroup C2      NOp   2
                         Standard orientation:
 ---------------------------------------------------------------------
 Center     Atomic      Atomic             Coordinates (Angstroms)
 Number     Number       Type             X           Y           Z
 ---------------------------------------------------------------------
      1          8           0        0.000000    0.000000    0.118818
      2          1           0       -0.000000    0.756577   -0.475272
      3          1           0       -0.000000   -0.756577   -0.475272
 ---------------------------------------------------------------------

 ...
SCF Done:  E(RB3LYP) =  -76.4259945508     A.U. after    9 cycles

***** Axes restored to original set *****
 -------------------------------------------------------------------
 Center     Atomic                   Forces (Hartrees/Bohr)
 Number     Number              X              Y              Z
 -------------------------------------------------------------------
      1        8          -0.000000000    0.000000000   -0.006569831
      2        1          -0.000000000   -0.005632960    0.003284915
      3        1           0.000000000    0.005632960    0.003284915
 -------------------------------------------------------------------

 ...

 (Enter /soft/gaussian/16-c.02/g16/l202.exe)
                          Input orientation:
 ---------------------------------------------------------------------
 Center     Atomic      Atomic             Coordinates (Angstroms)
 Number     Number       Type             X           Y           Z
 ---------------------------------------------------------------------
      1          8           0       -0.000000   -0.000000    0.117858
      2          1           0        0.000000    0.756683   -0.476345
      3          1           0       -0.000000   -0.756683   -0.476345
 ---------------------------------------------------------------------
                    Distance matrix (angstroms):
                    1          2          3
     1  O    0.000000
     2  H    0.962105   0.000000
     3  H    0.962105   1.513366   0.000000
 Stoichiometry    H2O
 Framework group  C2V[C2(O),SGV(H2)]
 Deg. of freedom     2
 Full point group                 C2V     NOp   4
 RotChk:  IX=0 Diff= 4.65D-16
 Largest Abelian subgroup         C2V     NOp   4
 Largest concise Abelian subgroup C2      NOp   2
                         Standard orientation:
 ---------------------------------------------------------------------
 Center     Atomic      Atomic             Coordinates (Angstroms)
 Number     Number       Type             X           Y           Z
 ---------------------------------------------------------------------
      1          8           0       -0.000000    0.000000    0.118841
      2          1           0        0.000000    0.756683   -0.475362
      3          1           0       -0.000000   -0.756683   -0.475362
 ---------------------------------------------------------------------

 ...

 SCF Done:  E(RB3LYP) =  -76.4260779687     A.U. after    7 cycles

 ...

***** Axes restored to original set *****
 -------------------------------------------------------------------
 Center     Atomic                   Forces (Hartrees/Bohr)
 Number     Number              X              Y              Z
 -------------------------------------------------------------------
      1        8          -0.000000000   -0.000000000    0.000176354
      2        1           0.000000000    0.000122881   -0.000088177
      3        1           0.000000000   -0.000122881   -0.000088177
 -------------------------------------------------------------------
 Cartesian Forces:  Max     0.000176354 RMS     0.000092406

"""


def test_match_magic():
    """Test if the file type can be guessed correctly."""
    bytebuf = BUF_H2O.encode('ascii')
    assert match_magic(bytebuf).name == 'gaussian-out'


def test_gaussian_out_l601():
    """Test if positions and energy are parsed correctly.

    Test also if dipole moment is parsed correctly from `l601.exe`.
    This corresponds to the options without `Forces` and `Pop=None`.
    """
    buf = BUF_H2O + BUF_H2O_MULLIKEN + BUF_H2O_L601_DIPOLE
    atoms = read(StringIO(buf), format='gaussian-out')
    assert str(atoms.symbols) == 'OH2'
    assert atoms.positions == pytest.approx(np.array([
        [+0.000000, +0.000000, +0.119262],
        [+0.000000, +0.763239, -0.477047],
        [+0.000000, -0.763239, -0.477047],
    ]))
    assert not any(atoms.pbc)
    assert atoms.cell.rank == 0

    energy = atoms.get_potential_energy()
    assert energy / units.Ha == pytest.approx(-75.9834173665)

    charges_ref = pytest.approx(np.array([-0.792441, +0.396221, +0.396221]))
    assert atoms.get_charges() == charges_ref

    dipole_moment_ref = pytest.approx(np.array([+0.0000, -0.0000, -2.6431]))
    assert atoms.get_dipole_moment() / units.Debye == dipole_moment_ref


def test_gaussian_out_l716():
    """Test if forces and dipole moment are parsed correctly from `l716.exe`.

    This corresponds to the options with `Forces` and `Pop=None`.
    """
    atoms = read(StringIO(BUF_H2O + BUF_H2O_L716), format='gaussian-out')
    forces = atoms.get_forces()
    assert forces / (units.Ha / units.Bohr) == pytest.approx(np.array([
        [-0.000000000, -0.000000000, -0.036558637],
        [-0.000000000, -0.003968101, +0.018279318],
        [+0.000000000, +0.003968101, +0.018279318],
    ]))
    assert atoms.get_dipole_moment() / units.Bohr == pytest.approx(np.array(
        [+3.27065103e-16, -1.33226763e-15, -1.03989005e+00],
    ))


def test_gaussian_out_lowdin():
    """Test if Löwdin charges are parsed correctly from `l601.exe`.

    This corresponds to the options with `IOp(6/80=1)`.
    """
    buf = BUF_H2O + BUF_H2O_MULLIKEN + BUF_H2O_LOWDIN
    atoms = read(StringIO(buf), format='gaussian-out')

    charges_ref = pytest.approx(np.array([-0.584488, +0.292244, +0.292244]))
    assert atoms.get_charges() == charges_ref


def test_gaussian_out_hirshfeld():
    """Test if Hirshfeld charges are parsed correctly from `l601.exe`.

    This corresponds to the options with `Pop=Hirshfeld`.
    """
    buf = BUF_H2O + BUF_H2O_MULLIKEN + BUF_H2O_HIRSHFELD
    atoms = read(StringIO(buf), format='gaussian-out')

    charges_ref = pytest.approx(np.array([-0.338600, +0.169300, +0.169300]))
    assert atoms.get_charges() == charges_ref

    assert atoms.get_magnetic_moments() == pytest.approx(np.zeros(3))


def test_spin_polarized():
    """Test if spin polarized calculations are parsed correctly."""
    buf = BUF_O2 + BUF_O2_MULLIKEN
    atoms = read(StringIO(buf), format='gaussian-out')

    assert atoms.get_charges() == pytest.approx(np.zeros(2))

    assert atoms.get_magnetic_moments() == pytest.approx(np.ones(2))


def test_mp2():
    """Test if the MP2 energy is parsed correctly."""
    atoms = read(StringIO(BUF_F2_MP2), format="gaussian-out")
    energy = atoms.get_potential_energy()
    assert energy / units.Ha == pytest.approx(-0.19912649675787e+03)


def test_ccsd():
    """Test if the CCSD energy is parsed correctly."""
    atoms = read(StringIO(BUF_F2_CCSD), format="gaussian-out")
    energy = atoms.get_potential_energy()
    assert energy / units.Ha == pytest.approx(-199.13391098)


def test_ccsd_t():
    """Test if the CCSD(T) energy is parsed correctly."""
    atoms = read(StringIO(BUF_F2_CCSD_T), format="gaussian-out")
    energy = atoms.get_potential_energy()
    assert energy / units.Ha == pytest.approx(-0.19914648303e+03)


def test_gaussian_opt():
    """Test if we determine the correct number of geometries in the output
    produced during optimization"""
    atoms = read(StringIO(BUF_H2O_OPT), format="gaussian-out", index=':')
    assert len(atoms) == 2

    # Ensure the energy and forces were parsed and differ
    assert atoms[0].get_potential_energy() != atoms[1].get_potential_energy()
    assert not np.isclose(atoms[0].get_forces(), atoms[1].get_forces()).all()