Skip to content

Commit

Permalink
Added sanity tests and fixed a bug with multiple custom basis sets in…
Browse files Browse the repository at this point in the history
… ORCA
  • Loading branch information
RaphaelRobidas committed May 24, 2023
1 parent 49dca17 commit 34be971
Show file tree
Hide file tree
Showing 5 changed files with 833 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ccinput/packages/orca.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def handle_custom_basis_sets(self):
# TODO: handle auxiliary basis sets

if success:
custom_bs += _custom_bs.strip()
custom_bs += _custom_bs
else:
bs = bse.get_basis(
bs_keyword, fmt="ORCA", elements=[el_num], header=False
Expand All @@ -334,7 +334,7 @@ def handle_custom_basis_sets(self):
custom_bs += "end"

if custom_bs != "":
self.blocks.append(BS_TEMPLATE.format(custom_bs))
self.blocks.append(BS_TEMPLATE.format(custom_bs.strip()))

def handle_xyz(self):
lines = [i + "\n" for i in clean_xyz(self.calc.xyz).split("\n") if i != ""]
Expand Down
11 changes: 11 additions & 0 deletions ccinput/tests/structures/Me2I_cation.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
9
Title
C -4.08000 1.73450 0.00000
H -3.72330 0.72570 0.00000
H -3.72330 2.23890 -0.87370
H -5.15000 1.73460 0.00000
I -3.38000 2.72450 1.71460
C -3.38010 0.90580 2.76470
H -2.69410 0.22710 2.30250
H -3.08250 1.07890 3.77780
H -4.36370 0.48490 2.74880
54 changes: 54 additions & 0 deletions ccinput/tests/test_orca.py
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,60 @@ def test_opt_DFT_custom_bs_ecp(self):

self.assertTrue(self.is_equivalent(REF, inp.input_file))

def test_opt_DFT_two_custom_bs_ecp(self):
params = {
"nproc": 8,
"type": "Geometrical Optimisation",
"file": "Ph2I_cation.xyz",
"software": "ORCA",
"charge": "+1",
"method": "B3LYP",
"basis_set": "6-31+G(d,p)",
"custom_basis_sets": "I=Def2-TZVPD;H=Def2-TZVP",
}

inp = self.generate_calculation(**params)

REF = """
!OPT B3LYP 6-31+G(d,p)
*xyz 1 1
C -3.06870 -2.28540 0.00000
C -1.67350 -2.28540 0.00000
C -0.97600 -1.07770 0.00000
C -1.67360 0.13090 -0.00120
C -3.06850 0.13080 -0.00170
C -3.76610 -1.07740 -0.00070
H -3.61840 -3.23770 0.00040
H -1.12400 -3.23790 0.00130
H 0.12370 -1.07760 0.00060
H -1.12340 1.08300 -0.00130
H -4.86570 -1.07720 -0.00090
I -4.11890 1.94920 -0.00350
C -4.64360 2.85690 -1.82310
C -3.77180 3.76300 -2.42740
C -5.86360 2.55380 -2.42750
C -4.12020 4.36650 -3.63560
H -2.81040 4.00240 -1.95030
C -6.21180 3.15650 -3.63650
H -6.55070 1.83950 -1.95140
C -5.34050 4.06290 -4.24060
H -3.43340 5.08120 -4.11170
H -7.17360 2.91710 -4.11310
H -5.61500 4.53870 -5.19320
*
%basis
NewGTO I "Def2-TZVPD" end
NewECP I "def2-ECP" end
NewGTO H "Def2-TZVP" end
end
%MaxCore 125
%pal
nprocs 8
end
"""

self.assertTrue(self.is_equivalent(REF, inp.input_file))

def test_opt_DFT_custom_bs_ecp_synonym(self):
params = {
"nproc": 8,
Expand Down
111 changes: 109 additions & 2 deletions ccinput/tests/testing_utilities.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import shlex
import subprocess
import tempfile
from unittest import TestCase

from ccinput.wrapper import gen_obj
from ccinput.wrapper import gen_input, get_input_from_args, get_parser
from ccinput.wrapper import gen_obj, gen_input, get_input_from_args, get_parser


class InputTests(TestCase):
Expand Down Expand Up @@ -77,3 +78,109 @@ def struct(self, name):
return os.path.join(
"/".join(__file__.split("/")[:-1]), "structures/", name + ".xyz"
)


class CalculationTests(InputTests):
def run_calc(self, **params):
obj = self.generate_calculation(**params)
with tempfile.TemporaryDirectory() as tmpdir:
self.run_software(obj, tmpdir)
return self.get_energy(os.path.join(tmpdir))

energies = []

def known_energy(self, E, params, fail=False):
if E == -1:
raise Exception("Invalid calculation")
for entry in self.energies:
if entry[1] == E:
if not fail:
print("")
print("Clash detected:")
print(entry[0])
print(params)
print("")
return True

self.energies.append([params, E])
return False


class GaussianCalculationTests(CalculationTests):
def run_software(self, obj, tmpdir):
with open(os.path.join(tmpdir, "calc.com"), "w") as out:
out.write(obj.input_file)

with open(os.path.join(tmpdir, "gaussian.log"), "w") as out:
ret = subprocess.run(
shlex.split("g16 calc.com"), cwd=tmpdir, stdout=out, stderr=out
)

if ret.returncode != 0:
print(f"Calculation ended with return code {ret.returncode}")

def get_energy(self, tmpdir):
path = os.path.join(tmpdir, "calc.log")
with open(path) as f:
lines = f.readlines()
ind = len(lines) - 1
while lines[ind].find("SCF Done") == -1:
ind -= 1
return float(lines[ind].split()[4])


class OrcaCalculationTests(CalculationTests):
def run_software(self, obj, tmpdir):
with open(os.path.join(tmpdir, "calc.inp"), "w") as out:
out.write(obj.input_file)

with open(os.path.join(tmpdir, "calc.out"), "w") as out:
ret = subprocess.run(
shlex.split("orca calc.inp"), cwd=tmpdir, stdout=out, stderr=out
)

if ret.returncode != 0:
print(f"Calculation ended with return code {ret.returncode}")

def get_energy(self, tmpdir):
path = os.path.join(tmpdir, "calc.out")
with open(path) as f:
lines = f.readlines()
ind = len(lines) - 1

for line in lines:
print(line)
while lines[ind].find("FINAL SINGLE POINT ENERGY") == -1:
ind -= 1

return float(lines[ind].split()[4].strip())


class XtbCalculationTests(CalculationTests):
def run_software(self, obj, tmpdir):
os.system(f"cp {obj.calc.file} {tmpdir}/{obj.get_output_name()}")

if obj.input_file != "":
with open(os.path.join(tmpdir, "input"), "w") as out:
out.write(obj.input_file)

with open(os.path.join(tmpdir, "calc.out"), "w") as out:
ret = subprocess.run(
shlex.split(obj.command), cwd=tmpdir, stdout=out, stderr=out
)

if ret.returncode != 0:
print(f"Calculation ended with return code {ret.returncode}")

def get_energy(self, tmpdir):
path = os.path.join(tmpdir, "calc.out")
with open(path) as f:
lines = f.readlines()
ind = len(lines) - 1

for line in lines:
print(line)
while lines[ind].find("TOTAL ENERGY") == -1:
ind -= 1

return float(lines[ind].split()[3].strip())
Loading

0 comments on commit 34be971

Please sign in to comment.