def prepare_forte_objects_from_psi4_wfn(options, wfn, mo_space_info): """ Take a psi4 wavefunction object and prepare the ForteIntegrals, SCFInfo, and MOSpaceInfo objects Parameters ---------- options : ForteOptions A Forte ForteOptions object wfn : psi4 Wavefunction A psi4 Wavefunction object mo_space_info : the MO space info read from options A Forte MOSpaceInfo object Returns ------- tuple(ForteIntegrals, SCFInfo, MOSpaceInfo) a tuple containing the ForteIntegrals, SCFInfo, and MOSpaceInfo objects """ # Call methods that project the orbitals (AVAS, embedding) mo_space_info = orbital_projection(wfn, options, mo_space_info) # Build Forte SCFInfo object scf_info = forte.SCFInfo(wfn) # Build a map from Forte StateInfo to the weights state_weights_map = forte.make_state_weights_map(options, mo_space_info) return (state_weights_map, mo_space_info, scf_info)
def test_scfinfo(): """Test the SCFInfo class python API""" ref_energy = -99.50300245245828 geom = """ 1 2 H 0.0 0.0 0.0 F 0.0 0.0 1.0 """ psi4.core.clean() mol = psi4.geometry(geom) psi4.set_options({ 'basis': 'cc-pVDZ', 'scf_type': 'pk', 'reference': 'uhf', 'docc': [3, 0, 1, 0], 'socc': [0, 0, 0, 1] }) _, wfn = psi4.energy('scf', return_wfn=True, molecule=mol) # create an SCFInfo object from the psi4 wavefunction scfinfo = forte.SCFInfo(wfn) assert tuple(scfinfo.nmopi()) == (10, 1, 4, 4) assert tuple(scfinfo.doccpi()) == (3, 0, 1, 0) assert tuple(scfinfo.soccpi()) == (0, 0, 0, 1) assert scfinfo.reference_energy() == pytest.approx(ref_energy, 1.0e-10) assert scfinfo.epsilon_a().nph[0][0] == pytest.approx(-26.97737351, 1.0e-6) assert scfinfo.epsilon_b().nph[0][0] == pytest.approx(-26.92368455, 1.0e-6)
def test_aci1(): import math import psi4 import forte from forte import forte_options ref_aci = -75.010199198896 rel_tol = 1e-9 abs_tol = 1e-8 h2o = psi4.geometry(""" O H 1 0.96 H 1 0.96 2 104.5 """) psi4.set_options({'basis': "sto-3g"}) E_scf, wfn = psi4.energy('scf', return_wfn=True) state = forte.StateInfo(na=5, nb=5, multiplicity=1, twice_ms=0, irrep=0) dim = psi4.core.Dimension([4, 0, 1, 2]) options = psi4.core.get_options() options.set_current_module('FORTE') forte_options.update_psi_options(options) forte.startup() forte.banner() mo_space_info = forte.make_mo_space_info(wfn, forte_options) ints = forte.make_forte_integrals(wfn, options, mo_space_info) scf_info = forte.SCFInfo(wfn) solver = forte.make_active_space_solver('ACI', state, scf_info, mo_space_info, ints, forte_options) energy = solver.compute_energy() assert math.isclose(energy, ref_aci, abs_tol=abs_tol, rel_tol=rel_tol) print("\n\nACI Energy = {}".format(energy)) forte.cleanup()
def prepare_forte_objects(wfn): """ Take a psi4 wavefunction object and prepare the ForteIntegrals, SCFInfo, and MOSpaceInfo objects Parameters ---------- wfn : psi4Wavefunction A psi4 Wavefunction object Returns ------- tuple(ForteIntegrals, SCFInfo, MOSpaceInfo) a tuple containing the ForteIntegrals, SCFInfo, and MOSpaceInfo objects """ # fill in the options object psi4_options = psi4.core.get_options() psi4_options.set_current_module('FORTE') options = forte.forte_options options.get_options_from_psi4(psi4_options) if ('DF' in options.get_str('INT_TYPE')): aux_basis = psi4.core.BasisSet.build( wfn.molecule(), 'DF_BASIS_MP2', psi4.core.get_global_option('DF_BASIS_MP2'), 'RIFIT', psi4.core.get_global_option('BASIS')) wfn.set_basisset('DF_BASIS_MP2', aux_basis) if (options.get_str('MINAO_BASIS')): minao_basis = psi4.core.BasisSet.build( wfn.molecule(), 'MINAO_BASIS', psi4_options.get_str('MINAO_BASIS')) wfn.set_basisset('MINAO_BASIS', minao_basis) # Prepare base objects scf_info = forte.SCFInfo(wfn) mo_space_info = forte.make_mo_space_info(wfn, options) ints = forte.make_forte_integrals(wfn, options, mo_space_info) return (ints, scf_info, mo_space_info)
def run_forte(name, **kwargs): r"""Function encoding sequence of PSI module and plugin calls so that forte can be called via :py:func:`~driver.energy`. For post-scf plugins. >>> energy('forte') """ lowername = name.lower() kwargs = p4util.kwargs_lower(kwargs) # Compute a SCF reference, a wavefunction is return which holds the molecule used, orbitals # Fock matrices, and more ref_wfn = kwargs.get('ref_wfn', None) if ref_wfn is None: ref_wfn = psi4.driver.scf_helper(name, **kwargs) # Get the option object options = psi4.core.get_options() options.set_current_module('FORTE') forte.forte_options.update_psi_options(options) if ('DF' in options.get_str('INT_TYPE')): aux_basis = psi4.core.BasisSet.build( ref_wfn.molecule(), 'DF_BASIS_MP2', psi4.core.get_global_option('DF_BASIS_MP2'), 'RIFIT', psi4.core.get_global_option('BASIS')) ref_wfn.set_basisset('DF_BASIS_MP2', aux_basis) if (options.get_str('MINAO_BASIS')): minao_basis = psi4.core.BasisSet.build(ref_wfn.molecule(), 'MINAO_BASIS', options.get_str('MINAO_BASIS')) ref_wfn.set_basisset('MINAO_BASIS', minao_basis) # Start Forte, initialize ambit my_proc_n_nodes = forte.startup() my_proc, n_nodes = my_proc_n_nodes # Print the banner forte.banner() # Create the MOSpaceInfo object mo_space_info = forte.make_mo_space_info(ref_wfn, forte.forte_options) # Create the AO subspace projector ps = forte.make_aosubspace_projector(ref_wfn, options) state = forte.make_state_info_from_psi_wfn(ref_wfn) scf_info = forte.SCFInfo(ref_wfn) state_weights_map = forte.make_state_weights_map(forte.forte_options, ref_wfn) # Run a method job_type = options.get_str('JOB_TYPE') energy = 0.0 if job_type != 'NONE': start = timeit.timeit() # Make an integral object ints = forte.make_forte_integrals(ref_wfn, options, mo_space_info) # Rotate orbitals before computation orb_type = options.get_str("ORBITAL_TYPE") if orb_type != 'CANONICAL': orb_t = forte.make_orbital_transformation(orb_type, scf_info, forte.forte_options, ints, mo_space_info) orb_t.compute_transformation() Ua = orb_t.get_Ua() Ub = orb_t.get_Ub() ints.rotate_orbitals(Ua, Ub) # Run a method if (job_type == 'NEWDRIVER'): energy = forte_driver(state_weights_map, scf_info, forte.forte_options, ints, mo_space_info) else: energy = forte.forte_old_methods(ref_wfn, options, ints, mo_space_info) end = timeit.timeit() #print('\n\n Your calculation took ', (end - start), ' seconds'); # Close ambit, etc. forte.cleanup() psi4.core.set_scalar_variable('CURRENT ENERGY', energy) return ref_wfn
def prepare_forte_objects(wfn, mo_spaces=None, active_space='ACTIVE', core_spaces=['RESTRICTED_DOCC'], localize=False, localize_spaces=[]): """Take a psi4 wavefunction object and prepare the ForteIntegrals, SCFInfo, and MOSpaceInfo objects Parameters ---------- wfn : psi4 Wavefunction A psi4 Wavefunction object mo_spaces : dict A dictionary with the size of each space (e.g., {'ACTIVE' : [3]}) active_space : str The MO space treated as active (default: 'ACTIVE') core_spaces : list(str) The MO spaces treated as active (default: ['RESTRICTED_DOCC']) localize : bool Do localize the orbitals? (defaul: False) localize_spaces : list(str) A list of spaces to localize (default: []) Returns ------- tuple(ForteIntegrals, ActiveSpaceIntegrals, SCFInfo, MOSpaceInfo, map(StateInfo : list) a tuple containing the ForteIntegrals, SCFInfo, and MOSpaceInfo objects and a map of states and weights """ # fill in the options object psi4_options = psi4.core.get_options() psi4_options.set_current_module('FORTE') options = forte.forte_options options.get_options_from_psi4(psi4_options) if ('DF' in options.get_str('INT_TYPE')): aux_basis = psi4.core.BasisSet.build( wfn.molecule(), 'DF_BASIS_MP2', psi4.core.get_global_option('DF_BASIS_MP2'), 'RIFIT', psi4.core.get_global_option('BASIS')) wfn.set_basisset('DF_BASIS_MP2', aux_basis) if (options.get_str('MINAO_BASIS')): minao_basis = psi4.core.BasisSet.build( wfn.molecule(), 'MINAO_BASIS', psi4_options.get_str('MINAO_BASIS')) wfn.set_basisset('MINAO_BASIS', minao_basis) # Prepare base objects scf_info = forte.SCFInfo(wfn) nmopi = wfn.nmopi() point_group = wfn.molecule().point_group().symbol() if mo_spaces == 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, []) state_weights_map = forte.make_state_weights_map(options, mo_space_info) ints = forte.make_ints_from_psi4(wfn, options, mo_space_info) if localize: localizer = forte.Localize(forte.forte_options, ints, mo_space_info) localizer.set_orbital_space(localize_spaces) localizer.compute_transformation() Ua = localizer.get_Ua() ints.rotate_orbitals(Ua, Ua) # the space that defines the active orbitals. We select only the 'ACTIVE' part # the space(s) with non-active doubly occupied orbitals as_ints = forte.make_active_space_ints(mo_space_info, ints, active_space, core_spaces) return (ints, as_ints, scf_info, mo_space_info, state_weights_map)
def gradient_forte(name, **kwargs): r"""Function encoding sequence of PSI module and plugin calls so that forte can be called via :py:func:`~driver.energy`. For post-scf plugins. >>> gradient('forte') available for : CASSCF """ lowername = name.lower() kwargs = p4util.kwargs_lower(kwargs) # Compute a SCF reference, a wavefunction is return which holds the molecule used, orbitals # Fock matrices, and more ref_wfn = kwargs.get('ref_wfn', None) if ref_wfn is None: ref_wfn = psi4.driver.scf_helper(name, **kwargs) # Get the psi4 option object optstash = p4util.OptionsState(['GLOBALS', 'DERTYPE']) psi4_options = psi4.core.get_options() psi4_options.set_current_module('FORTE') # Get the forte option object options = forte.forte_options options.get_options_from_psi4(psi4_options) if ('DF' in options.get_str('INT_TYPE')): raise Exception('analytic gradient is not implemented for density fitting') if (options.get_str('MINAO_BASIS')): minao_basis = psi4.core.BasisSet.build(ref_wfn.molecule(), 'MINAO_BASIS', options.get_str('MINAO_BASIS')) ref_wfn.set_basisset('MINAO_BASIS', minao_basis) # Start Forte, initialize ambit my_proc_n_nodes = forte.startup() my_proc, n_nodes = my_proc_n_nodes # Print the banner forte.banner() # Create the MOSpaceInfo object mo_space_info = forte.make_mo_space_info(ref_wfn, options) # Call methods that project the orbitals (AVAS, embedding) mo_space_info = orbital_projection(ref_wfn, options, mo_space_info) state = forte.make_state_info_from_psi_wfn(ref_wfn) scf_info = forte.SCFInfo(ref_wfn) state_weights_map = forte.make_state_weights_map(options,ref_wfn) # Run a method job_type = options.get_str('JOB_TYPE') energy = 0.0 if not job_type == 'CASSCF': raise Exception('analytic gradient is only implemented for CASSCF') start = time.time() # Make an integral object ints = forte.make_forte_integrals(ref_wfn, options, mo_space_info) # Rotate orbitals before computation orb_type = options.get_str("ORBITAL_TYPE") if orb_type != 'CANONICAL': orb_t = forte.make_orbital_transformation(orb_type, scf_info, options, ints, mo_space_info) orb_t.compute_transformation() Ua = orb_t.get_Ua() Ub = orb_t.get_Ub() ints.rotate_orbitals(Ua,Ub) # Run gradient computation energy = forte.forte_old_methods(ref_wfn, options, ints, mo_space_info) derivobj = psi4.core.Deriv(ref_wfn) derivobj.set_deriv_density_backtransformed(True) derivobj.set_ignore_reference(True) grad = derivobj.compute() #psi4.core.DerivCalcType.Correlated ref_wfn.set_gradient(grad) optstash.restore() end = time.time() #print('\n\n Your calculation took ', (end - start), ' seconds'); # Close ambit, etc. forte.cleanup() return ref_wfn
def run_forte(name, **kwargs): r"""Function encoding sequence of PSI module and plugin calls so that forte can be called via :py:func:`~driver.energy`. For post-scf plugins. >>> energy('forte') """ lowername = name.lower() kwargs = p4util.kwargs_lower(kwargs) # Compute a SCF reference, a wavefunction is return which holds the molecule used, orbitals # Fock matrices, and more ref_wfn = kwargs.get('ref_wfn', None) if ref_wfn is None: ref_wfn = psi4.driver.scf_helper(name, **kwargs) # Get the option object psi4_options = psi4.core.get_options() psi4_options.set_current_module('FORTE') # Get the forte option object options = forte.forte_options options.get_options_from_psi4(psi4_options) if ('DF' in options.get_str('INT_TYPE')): aux_basis = psi4.core.BasisSet.build(ref_wfn.molecule(), 'DF_BASIS_MP2', options.get_str('DF_BASIS_MP2'), 'RIFIT', options.get_str('BASIS')) ref_wfn.set_basisset('DF_BASIS_MP2', aux_basis) if (options.get_str('MINAO_BASIS')): minao_basis = psi4.core.BasisSet.build(ref_wfn.molecule(), 'MINAO_BASIS', options.get_str('MINAO_BASIS')) ref_wfn.set_basisset('MINAO_BASIS', minao_basis) # Start Forte, initialize ambit my_proc_n_nodes = forte.startup() my_proc, n_nodes = my_proc_n_nodes # Print the banner forte.banner() # Create the MOSpaceInfo object mo_space_info = forte.make_mo_space_info(ref_wfn, options) # Call methods that project the orbitals (AVAS, embedding) mo_space_info = orbital_projection(ref_wfn, options, mo_space_info) # Averaging spin multiplets if doing spin-adapted computation if options.get_str('CORRELATION_SOLVER') == 'SA-MRDSRG': options_dict = options.dict() options_dict['SPIN_AVG_DENSITY']['value'] = True options.set_dict(options_dict) state = forte.make_state_info_from_psi_wfn(ref_wfn) scf_info = forte.SCFInfo(ref_wfn) state_weights_map = forte.make_state_weights_map(options,ref_wfn) # Run a method job_type = options.get_str('JOB_TYPE') energy = 0.0 if job_type == 'NONE': forte.cleanup() return ref_wfn start_pre_ints = time.time() # Make an integral object ints = forte.make_forte_integrals(ref_wfn, options, mo_space_info) start = time.time() # Rotate orbitals before computation (e.g. localization, MP2 natural orbitals, etc.) orb_type = options.get_str("ORBITAL_TYPE") if orb_type != 'CANONICAL': orb_t = forte.make_orbital_transformation(orb_type, scf_info, options, ints, mo_space_info) orb_t.compute_transformation() Ua = orb_t.get_Ua() Ub = orb_t.get_Ub() ints.rotate_orbitals(Ua,Ub) # Run a method if (job_type == 'NEWDRIVER'): energy = forte_driver(state_weights_map, scf_info, options, ints, mo_space_info) else: energy = forte.forte_old_methods(ref_wfn, options, ints, mo_space_info) end = time.time() # Close ambit, etc. forte.cleanup() psi4.core.set_scalar_variable('CURRENT ENERGY', energy) psi4.core.print_out(f'\n\n Time to prepare integrals: {start - start_pre_ints:12.3f} seconds') psi4.core.print_out(f'\n Time to run job : {end - start:12.3f} seconds') psi4.core.print_out(f'\n Total : {end - start:12.3f} seconds') return ref_wfn
def prepare_forte_objects_from_fcidump(options, path='.'): fcidump_file = options.get_str('FCIDUMP_FILE') filename = pathlib.Path(path) / fcidump_file psi4.core.print_out( f'\n Reading integral information from FCIDUMP file {filename}') fcidump = forte.proc.fcidump_from_file(filename, convert_to_psi4=True) irrep_size = { 'c1': 1, 'ci': 2, 'c2': 2, 'cs': 2, 'd2': 4, 'c2v': 4, 'c2h': 4, 'd2h': 8 } nmo = len(fcidump['orbsym']) if 'pntgrp' in fcidump: nirrep = irrep_size[fcidump['pntgrp'].lower()] nmopi_list = [fcidump['orbsym'].count(h) for h in range(nirrep)] else: fcidump['pntgrp'] = 'C1' # set the point group to C1 fcidump['isym'] = 0 # shift by -1 nirrep = 1 nmopi_list = [nmo] nmopi_offset = [sum(nmopi_list[0:h]) for h in range(nirrep)] nmopi = psi4.core.Dimension(nmopi_list) # Create the MOSpaceInfo object mo_space_info = forte.make_mo_space_info(nmopi, fcidump['pntgrp'], options) # Call methods that project the orbitals (AVAS, embedding) # skipped due to lack of functionality # manufacture a SCFInfo object from the FCIDUMP file (this assumes C1 symmetry) nel = fcidump['nelec'] ms2 = fcidump['ms2'] na = (nel + ms2) // 2 nb = nel - na if fcidump['pntgrp'] == 'C1': doccpi = psi4.core.Dimension([nb]) soccpi = psi4.core.Dimension([ms2]) else: doccpi = options.get_int_list('FCIDUMP_DOCC') soccpi = options.get_int_list('FCIDUMP_SOCC') if len(doccpi) + len(soccpi) == 0: print( 'Reading a FCIDUMP file that uses symmetry but no DOCC and SOCC is specified.' ) print( 'Use the FCIDUMP_DOCC and FCIDUMP_SOCC options to specify the number of occupied orbitals per irrep.' ) doccpi = psi4.core.Dimension([0] * nirrep) soccpi = psi4.core.Dimension([0] * nirrep) if 'epsilon' in fcidump: epsilon_a = psi4.core.Vector.from_array(fcidump['epsilon']) epsilon_b = psi4.core.Vector.from_array(fcidump['epsilon']) else: # manufacture Fock matrices epsilon_a = psi4.core.Vector(nmo) epsilon_b = psi4.core.Vector(nmo) hcore = fcidump['hcore'] eri = fcidump['eri'] nmo = fcidump['norb'] for i in range(nmo): val = hcore[i, i] for h in range(nirrep): for j in range(nmopi_offset[h], nmopi_offset[h] + doccpi[h] + soccpi[h]): val += eri[i, i, j, j] - eri[i, j, i, j] for j in range(nmopi_offset[h], nmopi_offset[h] + doccpi[h]): val += eri[i, i, j, j] epsilon_a.set(i, val) val = hcore[i, i] for h in range(nirrep): for j in range(nmopi_offset[h], nmopi_offset[h] + doccpi[h] + soccpi[h]): val += eri[i, i, j, j] for j in range(nmopi_offset[h], nmopi_offset[h] + doccpi[h]): val += eri[i, i, j, j] - eri[i, j, i, j] epsilon_b.set(i, val) scf_info = forte.SCFInfo(nmopi, doccpi, soccpi, 0.0, epsilon_a, epsilon_b) state_info = make_state_info_from_fcidump(fcidump, options) state_weights_map = {state_info: [1.0]} return (state_weights_map, mo_space_info, scf_info, fcidump)