Skip to content

Commit

Permalink
Add test for untested helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
brianz98 committed Nov 26, 2024
1 parent 21e6c4b commit 8934166
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
41 changes: 2 additions & 39 deletions forte/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,45 +98,11 @@ def psi4_casscf(geom, basis, reference, restricted_docc, active, options={}) ->
# pipe output to the file output.dat
psi4.core.set_output_file("output.dat", True)

# psi4.core.clean()

# run scf and return the energy and a wavefunction object (will work only if pass return_wfn=True)
E_scf, wfn = psi4.energy("casscf", molecule=mol, return_wfn=True)
return (E_scf, wfn)


def psi4_casscf(geom, basis, mo_spaces):
"""
Run a Psi4 SCF.
:param geom: a string for molecular geometry
:param basis: a string for basis set
:param reference: a string for the type of reference
:return: a tuple of (scf energy, psi4 Wavefunction)
"""
psi4.core.clean()
mol = psi4.geometry(geom)

psi4.set_options(
{
"basis": basis,
"scf_type": "pk",
"e_convergence": 1e-13,
"d_convergence": 1e-6,
"restricted_docc": mo_spaces["RESTRICTED_DOCC"],
"active": mo_spaces["ACTIVE"],
"mcscf_maxiter": 100,
"mcscf_e_convergence": 1.0e-11,
"mcscf_r_convergence": 1.0e-6,
"mcscf_diis_start": 20,
}
)
psi4.core.set_output_file("output.dat", False)

Escf, wfn = psi4.energy("casscf", return_wfn=True)
psi4.core.clean()
return Escf, wfn


def psi4_cubeprop(wfn, path=".", orbs=[], nocc=0, nvir=0, density=False, frontier_orbitals=False, load=False):
"""
Run a psi4 cubeprop computation to generate cube files from a given Wavefunction object
Expand Down Expand Up @@ -184,7 +150,7 @@ def psi4_cubeprop(wfn, path=".", orbs=[], nocc=0, nvir=0, density=False, frontie


def prepare_forte_objects(
wfn, mo_spaces=None, active_space="ACTIVE", core_spaces=["RESTRICTED_DOCC"], localize=False, localize_spaces=[]
wfn, mo_spaces, active_space="ACTIVE", core_spaces=["RESTRICTED_DOCC"], localize=False, localize_spaces=[]
):
"""Take a psi4 wavefunction object and prepare the ForteIntegrals, SCFInfo, and MOSpaceInfo objects
Expand Down Expand Up @@ -235,10 +201,7 @@ def prepare_forte_objects(
point_group = wfn.molecule().point_group().symbol()

# create a MOSpaceInfo object
if mo_spaces is None:
mo_space_info = forte.make_mo_space_info(nmopi, point_group, options)
else:
mo_space_info = forte.make_mo_space_info_from_map(nmopi, point_group, mo_spaces)
mo_space_info = forte.make_mo_space_info_from_map(nmopi, point_group, mo_spaces)

# These variables are needed in make_state_weights_map
nel = wfn.nalpha() + wfn.nbeta()
Expand Down
34 changes: 34 additions & 0 deletions tests/pytest/helpers/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def test_helpers():
import math
import pytest
import psi4
import forte
import forte.utils

psi4.core.clean()

geom = """
O 0.0000 0.0000 0.1173
H 0.0000 0.7572 -0.4692
H 0.0000 -0.7572 -0.4692
symmetry c2v
"""
(E_scf, wfn_scf) = forte.utils.psi4_scf(geom,basis='6-31g',reference='rhf')
assert math.isclose(E_scf, -75.98397447271522)

(E_casscf, wfn_casscf) = forte.utils.psi4_casscf(geom,basis='6-31g',reference='rhf', restricted_docc=[2,0,0,1], active=[2,0,1,1])
assert math.isclose(E_casscf, -75.9998515885993)

res = forte.utils.prepare_forte_objects(wfn_casscf, {"RESTRICTED_DOCC": [2,0,0,1], "ACTIVE": [2,0,1,1]})
mo_space_info = res["mo_space_info"]
# mo_space_info.corr_absolute_mo("RESTRICTED_DOCC") == [0, 1, 9]
assert mo_space_info.corr_absolute_mo("RESTRICTED_DOCC")[2] == 9

psi4.core.clean()

if __name__ == "__main__":
test_helpers()

0 comments on commit 8934166

Please sign in to comment.