Skip to content

Commit

Permalink
adding mpenergies parser for psi4 (cclib#1459)
Browse files Browse the repository at this point in the history
  • Loading branch information
amandadumi authored Oct 5, 2024
1 parent 6e0a597 commit 9ac61cb
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 6 deletions.
1 change: 1 addition & 0 deletions cclib/attribute_parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from cclib.attribute_parsers.mocoeffs import mocoeffs
from cclib.attribute_parsers.moenergies import moenergies
from cclib.attribute_parsers.mosyms import mosyms
from cclib.attribute_parsers.mpenergies import mpenergies
from cclib.attribute_parsers.mult import mult
from cclib.attribute_parsers.natom import natom
from cclib.attribute_parsers.nbasis import nbasis
Expand Down
50 changes: 50 additions & 0 deletions cclib/attribute_parsers/mpenergies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) 2024, the cclib development team
#
# This file is part of cclib (http://cclib.github.io) and is distributed under
# the terms of the BSD 3-Clause License.
from typing import Optional

from cclib.attribute_parsers import utils
from cclib.attribute_parsers.base_parser import base_parser

import numpy as np


class mpenergies(base_parser):
"""
Docstring? Units?
"""

known_codes = ["psi4"]

@staticmethod
def psi4(file_handler, ccdata) -> Optional[dict]:
line = file_handler.last_line
# This is for the older conventional MP2 code in 4.0b5.
mp_trigger = "MP2 Total Energy (a.u.)"
if line.strip()[: len(mp_trigger)] == mp_trigger:
mpenergy = utils.convertor(float(line.split()[-1]), "hartree", "eV")
if getattr(ccdata, "mpenergies") is None:
this_mpenergies = []
this_mpenergies.append([mpenergy])
return {mpenergies.__name__: this_mpenergies}
# This is for the newer DF-MP2 code in 4.0.
if "DF-MP2 Energies" in line:
while "Total Energy" not in line:
line = file_handler.virtual_next()
mpenergy = utils.convertor(float(line.split()[3]), "hartree", "eV")
if getattr(ccdata, "mpenergies") is None:
this_mpenergies = []
this_mpenergies.append([mpenergy])
return {mpenergies.__name__: np.array(this_mpenergies)}
return None

@staticmethod
def parse(file_handler, program: str, ccdata) -> Optional[dict]:
constructed_data = None
if program in mpenergies.known_codes:
file_handler.virtual_set()
program_parser = getattr(mpenergies, program)
constructed_data = program_parser(file_handler, ccdata)
file_handler.virtual_reset()
return constructed_data
1 change: 1 addition & 0 deletions cclib/combinator/combinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class combinator:
cprops.aooverlaps,
cprops.atommasses,
cprops.mosyms,
cprops.mpenergies,
cprops.nmo,
cprops.atombasis,
cprops.scftargets,
Expand Down
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ python_files =
testBasis.py
testCC.py
testSP.py
testMP.py
testSPun.py
test_code_in_code.py
test_tree.py
Expand Down
15 changes: 12 additions & 3 deletions test/data/testMP.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ class GenericMP2Test:

def testsizeandshape(self, data) -> None:
"""(MP2) Are the dimensions of mpenergies correct?"""
assert data.mpenergies.shape == (len(data.scfenergies), self.level - 1)
assert data._ccCollection._parsed_data[0].mpenergies.shape == (
len(data._ccCollection._parsed_data[0].scfenergies),
self.level - 1,
)

def testsign(self, data) -> None:
"""Are the Moller-Plesset corrections negative?"""
if self.level == 2:
corrections = data.mpenergies[:, 0] - data.scfenergies
corrections = (
data._ccCollection._parsed_data[0].mpenergies[:, 0]
- data._ccCollection._parsed_data[0].scfenergies
)
else:
corrections = data.mpenergies[:, self.level - 2] - data.mpenergies[:, self.level - 3]
corrections = (
data._ccCollection._parsed_data[0].mpenergies[:, self.level - 2]
- data._ccCollection._parsed_data[0].mpenergies[:, self.level - 3]
)
assert numpy.all(corrections < 0.0)


Expand Down
6 changes: 3 additions & 3 deletions test/testdata
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ CC Psi4 Psi4CCSDPTTest basicPsi4-1.7 water_ccsd(t).
# MP ORCA GenericMP3Test basicORCA4.1 water_mp3.out
# MP ORCA GenericMP3Test basicORCA4.2 water_mp3.out
# MP ORCA GenericMP3Test basicORCA5.0 water_mp3.out
# MP Psi4 GenericMP2Test basicPsi4-1.2.1 water_mp2.out
# MP Psi4 GenericMP2Test basicPsi4-1.3.1 water_mp2.out
# MP Psi4 GenericMP2Test basicPsi4-1.7 water_mp2.out
MP Psi4 GenericMP2Test basicPsi4-1.2.1 water_mp2.out
MP Psi4 GenericMP2Test basicPsi4-1.3.1 water_mp2.out
MP Psi4 GenericMP2Test basicPsi4-1.7 water_mp2.out
# MP QChem GenericMP2Test basicQChem5.1 water_mp2.out
# MP QChem GenericMP3Test basicQChem5.1 water_mp3.out
# MP QChem QChemMP4SDQTest basicQChem5.1 water_mp4sdq.out
Expand Down

0 comments on commit 9ac61cb

Please sign in to comment.