Skip to content

Commit 4340e60

Browse files
authored
Fix prepare_forte_objects and a tutorial (#431)
* Fix prepare_forte_objects and a tutorial * Add test for untested helper functions
1 parent ec20a9a commit 4340e60

File tree

3 files changed

+202
-67
lines changed

3 files changed

+202
-67
lines changed

forte/utils/helpers.py

+9-40
Original file line numberDiff line numberDiff line change
@@ -98,45 +98,11 @@ def psi4_casscf(geom, basis, reference, restricted_docc, active, options={}) ->
9898
# pipe output to the file output.dat
9999
psi4.core.set_output_file("output.dat", True)
100100

101-
# psi4.core.clean()
102-
103101
# run scf and return the energy and a wavefunction object (will work only if pass return_wfn=True)
104102
E_scf, wfn = psi4.energy("casscf", molecule=mol, return_wfn=True)
105103
return (E_scf, wfn)
106104

107105

108-
def psi4_casscf(geom, basis, mo_spaces):
109-
"""
110-
Run a Psi4 SCF.
111-
:param geom: a string for molecular geometry
112-
:param basis: a string for basis set
113-
:param reference: a string for the type of reference
114-
:return: a tuple of (scf energy, psi4 Wavefunction)
115-
"""
116-
psi4.core.clean()
117-
mol = psi4.geometry(geom)
118-
119-
psi4.set_options(
120-
{
121-
"basis": basis,
122-
"scf_type": "pk",
123-
"e_convergence": 1e-13,
124-
"d_convergence": 1e-6,
125-
"restricted_docc": mo_spaces["RESTRICTED_DOCC"],
126-
"active": mo_spaces["ACTIVE"],
127-
"mcscf_maxiter": 100,
128-
"mcscf_e_convergence": 1.0e-11,
129-
"mcscf_r_convergence": 1.0e-6,
130-
"mcscf_diis_start": 20,
131-
}
132-
)
133-
psi4.core.set_output_file("output.dat", False)
134-
135-
Escf, wfn = psi4.energy("casscf", return_wfn=True)
136-
psi4.core.clean()
137-
return Escf, wfn
138-
139-
140106
def psi4_cubeprop(wfn, path=".", orbs=[], nocc=0, nvir=0, density=False, frontier_orbitals=False, load=False):
141107
"""
142108
Run a psi4 cubeprop computation to generate cube files from a given Wavefunction object
@@ -184,7 +150,7 @@ def psi4_cubeprop(wfn, path=".", orbs=[], nocc=0, nvir=0, density=False, frontie
184150

185151

186152
def prepare_forte_objects(
187-
wfn, mo_spaces=None, active_space="ACTIVE", core_spaces=["RESTRICTED_DOCC"], localize=False, localize_spaces=[]
153+
wfn, mo_spaces, active_space="ACTIVE", core_spaces=["RESTRICTED_DOCC"], localize=False, localize_spaces=[]
188154
):
189155
"""Take a psi4 wavefunction object and prepare the ForteIntegrals, SCFInfo, and MOSpaceInfo objects
190156
@@ -235,15 +201,18 @@ def prepare_forte_objects(
235201
point_group = wfn.molecule().point_group().symbol()
236202

237203
# create a MOSpaceInfo object
238-
if mo_spaces is None:
239-
mo_space_info = forte.make_mo_space_info(nmopi, point_group, options)
240-
else:
241-
mo_space_info = forte.make_mo_space_info_from_map(nmopi, point_group, mo_spaces)
204+
mo_space_info = forte.make_mo_space_info_from_map(nmopi, point_group, mo_spaces)
205+
206+
# These variables are needed in make_state_weights_map
207+
nel = wfn.nalpha() + wfn.nbeta()
208+
multiplicity = 1
242209

210+
options.set_int("NEL", nel)
211+
options.set_int("MULTIPLICITY", multiplicity)
243212
state_weights_map = forte.make_state_weights_map(options, mo_space_info)
244213

245214
# make a ForteIntegral object
246-
ints = forte.make_ints_from_psi4(wfn, options, mo_space_info)
215+
ints = forte.make_ints_from_psi4(wfn, options, scf_info, mo_space_info)
247216

248217
if localize:
249218
localizer = forte.Localize(forte.forte_options, ints, mo_space_info)

tests/pytest/helpers/test_helpers.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
def test_helpers():
6+
import math
7+
import pytest
8+
import psi4
9+
import forte
10+
import forte.utils
11+
12+
psi4.core.clean()
13+
14+
geom = """
15+
O 0.0000 0.0000 0.1173
16+
H 0.0000 0.7572 -0.4692
17+
H 0.0000 -0.7572 -0.4692
18+
symmetry c2v
19+
"""
20+
(E_scf, wfn_scf) = forte.utils.psi4_scf(geom,basis='6-31g',reference='rhf')
21+
assert math.isclose(E_scf, -75.98397447271522)
22+
23+
(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])
24+
assert math.isclose(E_casscf, -75.9998515885993)
25+
26+
res = forte.utils.prepare_forte_objects(wfn_casscf, {"RESTRICTED_DOCC": [2,0,0,1], "ACTIVE": [2,0,1,1]})
27+
mo_space_info = res["mo_space_info"]
28+
# mo_space_info.corr_absolute_mo("RESTRICTED_DOCC") == [0, 1, 9]
29+
assert mo_space_info.corr_absolute_mo("RESTRICTED_DOCC")[2] == 9
30+
31+
psi4.core.clean()
32+
33+
if __name__ == "__main__":
34+
test_helpers()

0 commit comments

Comments
 (0)