10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-11-18 20:13:07 +01:00
quantum_package/tests/unit_test/unit_test.py

138 lines
3.5 KiB
Python
Raw Normal View History

2015-03-27 19:03:15 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import subprocess
import os
2015-03-30 10:15:35 +02:00
import sys
2015-03-27 19:03:15 +01:00
qpackage_root = os.environ['QPACKAGE_ROOT']
EZFIO = "{0}/EZFIO".format(qpackage_root)
sys.path = [EZFIO + "/Python"] + sys.path
from ezfio import ezfio
2015-03-30 10:15:35 +02:00
from collections import defaultdict
# ~#~#~ #
# O p t #
# ~#~#~ #
2015-03-27 19:03:15 +01:00
precision = 1.e-8
2015-03-30 10:15:35 +02:00
# A test get a geo file and a basis file.
# A global dict containt the result for this test
# A test return True or Raise a error !
# More ezfio condition you set, beter it is
2015-03-27 19:03:15 +01:00
# You cannot order the test flow.
# So if you dont whant to remarque on test (for example the HF), set
# a global variable and check for it
global has_hf_alredy
has_hf_alredy = False
2015-03-30 10:15:35 +02:00
def init_folder(geo, basis, mult=1):
'''
Take a geo in arg (aka a existing geo.xyz in test/)
And create the geo.ezfio with the adeguate basis and multipliciti
DO NOT CHECK IS THE EZFIO FOLDER ALREADY EXIST
'''
2015-03-27 19:03:15 +01:00
2015-03-30 10:15:35 +02:00
cmd = "cp {0}/tests/{1}.xyz .".format(qpackage_root, geo)
2015-03-27 19:03:15 +01:00
subprocess.check_call([cmd], shell=True)
2015-03-30 10:15:35 +02:00
cmd = "qp_create_ezfio_from_xyz -b {0} -m {1} {2}.xyz".format(basis,
mult,
geo)
2015-03-27 19:03:15 +01:00
subprocess.check_call([cmd], shell=True)
2015-03-30 10:15:35 +02:00
def get_error_message(l_exepected, l_cur):
l_msg = ["Need {0} get {1}".format(i,j) for i,j in zip(l_exepected,l_cur)]
return "\n".join(l_msg)
def run_hf(geo, basis):
"""
Run a simle by default hf
EZFIO path = geo.ezfio
"""
global has_hf_alredy
has_hf_alredy = True
2015-03-27 19:03:15 +01:00
ref_energy = defaultdict(dict)
ref_energy["sto-3g"]["methane"] = -39.7267433402
2015-03-30 10:15:35 +02:00
init_folder(geo, basis)
2015-03-27 19:03:15 +01:00
2015-03-30 11:47:56 +02:00
ezfio.set_file("{0}.ezfio".format(geo))
ezfio.set_hartree_fock_mo_guess_type("Huckel")
ezfio.set_hartree_fock_thresh_scf("1.e-10")
ezfio.set_hartree_fock_n_it_scf_max("100")
2015-03-30 10:15:35 +02:00
cmd = "qp_run SCF {0}.ezfio/".format(geo)
2015-03-27 19:03:15 +01:00
subprocess.check_call([cmd], shell=True)
cur_e = ezfio.get_hartree_fock_energy()
2015-03-30 10:15:35 +02:00
ref_e = ref_energy[basis][geo]
2015-03-27 19:03:15 +01:00
2015-03-30 10:15:35 +02:00
if abs(cur_e - ref_e) <= precision:
return True
else:
raise ValueError(get_error_message([ref_e], [cur_e]))
2015-03-27 19:03:15 +01:00
2015-03-30 10:15:35 +02:00
def run_full_ci_10k_pt2_end(geo, basis):
"""
Run a Full_ci with 10k with the TruePT2
EZFIO path = geo.ezfio
"""
2015-03-27 19:03:15 +01:00
ref_energy_var = defaultdict(dict)
ref_energy_pt2 = defaultdict(dict)
ref_energy_var["sto-3g"]["methane"] = -0.398058753535695E+02
ref_energy_pt2["sto-3g"]["methane"] = -0.398059182483741E+02
2015-03-30 10:15:35 +02:00
ezfio.set_file("{0}.ezfio".format(geo))
2015-03-27 19:03:15 +01:00
ezfio.full_ci_do_pt2_end = True
ezfio.full_ci_n_det_max_fci = 10000
ezfio.full_ci_pt2_max = 1.e-8
2015-03-30 10:15:35 +02:00
cmd = "qp_run full_ci {0}.ezfio/".format(geo)
2015-03-27 19:03:15 +01:00
subprocess.check_call([cmd], shell=True)
cur_var = ezfio.get_full_ci_energy()
cur_pt2 = ezfio.get_full_ci_energy_pt2()
2015-03-30 10:15:35 +02:00
ref_var = ref_energy_var[basis][geo]
ref_pt2 = ref_energy_pt2[basis][geo]
2015-03-27 19:03:15 +01:00
2015-03-30 10:15:35 +02:00
t = [abs(cur_var - ref_var) <= precision,
abs(cur_pt2 - ref_pt2) <= precision]
2015-03-27 19:03:15 +01:00
2015-03-30 10:15:35 +02:00
if all(t):
return True
2015-03-27 19:03:15 +01:00
else:
2015-03-30 10:15:35 +02:00
raise ValueError(get_error_message([ref_var, ref_pt2],
[cur_var, cur_pt2]))
def run_big_test(geo, basis):
if not has_hf_alredy:
run_hf(geo, basis)
2015-03-30 10:15:35 +02:00
run_full_ci_10k_pt2_end(geo, basis)
return True
2015-03-27 19:03:15 +01:00
class SimplisticTest(unittest.TestCase):
def test_full_ci_10k_pt2_end(self):
self.assertTrue(run_big_test("methane", "sto-3g"))
if __name__ == '__main__':
unittest.main()