def test_three_system_interaction_mediator(self): subsystems = [self.sub1, self.sub2, self.sub3] mediator = interaction_mediator.InteractionMediator(subsystems) self.assertEqual(len(mediator.supersystems), 2) sub_1 = [self.sub2, self.sub4] sub_2 = [self.sub1, self.sub3] supersystem_1 = cluster_supersystem.ClusterSuperSystem(sub_1, 'lda') supersystem_2 = cluster_supersystem.ClusterSuperSystem(sub_2, 'm06') sup_list = [supersystem_1, supersystem_2] for i in range(len(mediator.supersystems)): test = mediator.supersystems[i] self.assertEqual(test.fs_method, sup_list[i].fs_method) self.assertEqual(test.proj_oper, sup_list[i].proj_oper) self.assertEqual(test.ft_cycles, sup_list[i].ft_cycles) self.assertEqual(test.ft_conv, sup_list[i].ft_conv) self.assertEqual(test.ft_grad, sup_list[i].ft_grad) self.assertEqual(test.ft_damp, sup_list[i].ft_damp) self.assertEqual(test.ft_initguess, sup_list[i].ft_initguess) self.assertEqual(test.ft_updatefock, sup_list[i].ft_updatefock) self.assertEqual(test.fs_cycles, sup_list[i].fs_cycles) self.assertEqual(test.fs_conv, sup_list[i].fs_conv) self.assertEqual(test.fs_grad, sup_list[i].fs_grad) self.assertEqual(test.fs_damp, sup_list[i].fs_damp) self.assertEqual(test.fs_shift, sup_list[i].fs_shift) self.assertEqual(test.fs_smearsigma, sup_list[i].fs_smearsigma) self.assertEqual(test.fs_initguess, sup_list[i].fs_initguess) self.assertEqual(test.grid_level, sup_list[i].grid_level) self.assertEqual(test.rho_cutoff, sup_list[i].rho_cutoff) self.assertEqual(test.fs_verbose, sup_list[i].fs_verbose)
def test_excited_supersystem(self): mol = gto.Mole() mol.verbose = 3 mol.atom = ''' H 0. -2.757 2.857 H 0. 2.757 2.857 ''' mol.basis = '3-21g' mol.build() env_method = 'm06' hl_method = 'ccsd' subsys = cluster_subsystem.ClusterHLSubSystem(mol, env_method, hl_method) mol2 = gto.Mole() mol2.verbose = 3 mol2.atom = ''' He 1.0 20.0 0.0 He 3.0 20.0 0.0''' mol2.basis = '3-21g' mol2.build() env_method = 'm06' subsys2 = cluster_subsystem.ClusterEnvSubSystem(mol2, env_method) mol12 = helpers.concat_mols([mol, mol2]) fs_scf_obj = helpers.gen_scf_obj(mol12, 'm06') supersystem = cluster_supersystem.ClusterSuperSystem([subsys, subsys2], 'm06', fs_scf_obj, init_guess='minao', fs_excited=True, fs_excited_dict={'nroots':4}, ft_excited_relax=True, ft_excited_dict={'nroots':2}) self.assertTrue(supersystem.fs_excited) self.assertTrue(supersystem.ft_excited_relax) self.assertDictEqual(supersystem.fs_excited_dict, {'nroots':4}) self.assertDictEqual(supersystem.ft_excited_dict, {'nroots':2}) self.assertEqual(supersystem.fs_excited_nroots, 4) self.assertEqual(supersystem.ft_excited_nroots, 2)
def test_basic_interaction_mediator(self): subsystems = [self.sub2, self.sub3] mediator = interaction_mediator.InteractionMediator(subsystems) self.assertEqual(len(mediator.supersystems), 1) mol12 = helpers.concat_mols([self.sub2.mol, self.sub3.mol]) fs_scf_obj = helpers.gen_scf_obj(mol12, 'm06') supersystem_1 = cluster_supersystem.ClusterSuperSystem(subsystems, 'm06', fs_scf_obj) for i in range(len(mediator.supersystems)): test = mediator.supersystems[i] self.assertEqual(test.env_method, supersystem_1.env_method) self.assertEqual(test.proj_oper, supersystem_1.proj_oper) self.assertEqual(test.max_cycle, supersystem_1.max_cycle) self.assertEqual(test.conv_tol, supersystem_1.conv_tol) self.assertEqual(test.damp, supersystem_1.damp) self.assertEqual(test.init_guess, supersystem_1.init_guess) self.assertEqual(test.fock_subcycles, supersystem_1.fock_subcycles) test_fs_scf = test.fs_scf_obj sup_fs_scf = supersystem_1.fs_scf_obj self.assertEqual(test_fs_scf.max_cycle, sup_fs_scf.max_cycle) self.assertEqual(test_fs_scf.conv_tol, sup_fs_scf.conv_tol) self.assertEqual(test_fs_scf.damp, sup_fs_scf.damp) self.assertEqual(test_fs_scf.level_shift_factor, sup_fs_scf.level_shift_factor) self.assertEqual(test_fs_scf.init_guess, sup_fs_scf.init_guess) self.assertEqual(test_fs_scf.grids.level, sup_fs_scf.grids.level) self.assertEqual(test_fs_scf.small_rho_cutoff, sup_fs_scf.small_rho_cutoff)
def test_partialghost_supersystem(self): mol = gto.Mole() mol.verbose = 3 mol.atom = ''' H 0. -2.757 2.857 H 0. 2.757 2.857 ghost:H 0. 0. 2.857 ''' mol.basis = '3-21g' mol.build() env_method = 'm06' hl_method = 'ccsd' subsys = cluster_subsystem.ClusterHLSubSystem(mol, env_method, hl_method) mol2 = gto.Mole() mol2.verbose = 3 mol2.atom = ''' He 1.0 20.0 0.0 He 3.0 20.0 0.0''' mol2.basis = '3-21g' mol2.build() env_method = 'm06' subsys2 = cluster_subsystem.ClusterEnvSubSystem(mol2, env_method) mol12 = helpers.concat_mols([mol, mol2]) fs_scf_obj = helpers.gen_scf_obj(mol12, 'b3lyp') supersystem = cluster_supersystem.ClusterSuperSystem([subsys, subsys2], 'b3lyp', fs_scf_obj, init_guess='minao') self.assertEqual(supersystem.env_method, 'b3lyp') self.assertEqual(supersystem.proj_oper, 'huz') self.assertEqual(supersystem.init_guess, 'minao')
def test_density_fitting_supersystem(self): mol = gto.Mole() mol.verbose = 3 mol.atom = ''' H 0. -2.757 2.857 H 0. 2.757 2.857 ''' mol.basis = '3-21g' mol.build() env_method = 'm06' hl_method = 'ccsd' subsys = cluster_subsystem.ClusterHLSubSystem(mol, env_method, hl_method) mol2 = gto.Mole() mol2.verbose = 3 mol2.atom = ''' He 1.0 20.0 0.0 He 3.0 20.0 0.0''' mol2.basis = '3-21g' mol2.build() env_method = 'm06' subsys2 = cluster_subsystem.ClusterEnvSubSystem(mol2, env_method) mol12 = helpers.concat_mols([mol, mol2]) fs_scf_obj = helpers.gen_scf_obj(mol12, 'm06', density_fitting=True) supersystem = cluster_supersystem.ClusterSuperSystem([subsys, subsys2], 'm06', fs_scf_obj, init_guess='minao') #Check SCF object scf_obj = supersystem.fs_scf_obj from pyscf.df.df_jk import _DFHF self.assertTrue(isinstance(scf_obj, _DFHF)) self.assertEqual(scf_obj.xc, 'm06') self.assertEqual(scf_obj.density_fitting, True)
def test_hsros_supersystem(self): mol = gto.Mole() mol.verbose = 3 mol.atom = ''' H 0. -2.757 2.857 H 0. 2.757 2.857 ''' mol.basis = '3-21g' mol.spin = 2 mol.build() env_method = 'm06' hl_method = 'ccsd' subsys = cluster_subsystem.ClusterHLSubSystem(mol, env_method, hl_method) mol2 = gto.Mole() mol2.verbose = 3 mol2.atom = ''' He 1.0 20.0 0.0 He 3.0 20.0 0.0''' mol2.basis = '3-21g' mol2.build() env_method = 'm06' subsys2 = cluster_subsystem.ClusterEnvSubSystem(mol2, env_method) mol12 = helpers.concat_mols([mol, mol2]) fs_scf_obj = helpers.gen_scf_obj(mol12, 'm06') supersystem = cluster_supersystem.ClusterSuperSystem([subsys, subsys2], 'm06', fs_scf_obj, init_guess='minao') #Check SCF object scf_obj = supersystem.fs_scf_obj comp_scf_obj = dft.ROKS(gto.mole.conc_mol(mol, mol2)) self.assertEqual(type(scf_obj), type(comp_scf_obj)) self.assertEqual(scf_obj.xc, 'm06')
def test_ovlpghost_supersystem(self): mol = gto.Mole() mol.verbose = 3 mol.atom = ''' H 0. -2.757 2.857 H 0. 2.757 2.857 ghost:He 1.0 20.0 0.0 ghost:H 0. 0. 2.857 ''' mol.basis = '3-21g' mol.build() env_method = 'm06' hl_method = 'ccsd' subsys = cluster_subsystem.ClusterHLSubSystem(mol, env_method, hl_method) mol2 = gto.Mole() mol2.verbose = 3 mol2.atom = ''' ghost:H 0. 2.757 2.857 He 1.0 20.0 0.0 He 3.0 20.0 0.0''' mol2.basis = '3-21g' mol2.build() env_method = 'm06' subsys2 = cluster_subsystem.ClusterEnvSubSystem(mol2, env_method) supersystem = cluster_supersystem.ClusterSuperSystem([subsys, subsys2], 'b3lyp', ft_initguess='minao') self.assertEqual(supersystem.fs_method, 'b3lyp') self.assertEqual(supersystem.proj_oper, 'huz') self.assertEqual(supersystem.ft_cycles, 100) self.assertEqual(supersystem.ft_conv, 1e-8) self.assertEqual(supersystem.ft_grad, None) self.assertEqual(supersystem.ft_initguess, 'minao') self.assertEqual(supersystem.ft_updatefock, 0) self.assertEqual(supersystem.fs_cycles, None) self.assertEqual(supersystem.fs_conv, None) self.assertEqual(supersystem.fs_grad, None) self.assertEqual(supersystem.fs_damp, 0) self.assertEqual(supersystem.fs_shift, 0) self.assertEqual(supersystem.fs_smearsigma, 0) self.assertEqual(supersystem.fs_initguess, None) self.assertEqual(supersystem.grid_level, None) self.assertEqual(supersystem.rho_cutoff, None) self.assertEqual(supersystem.fs_verbose, None)
def test_basic_supersystem(self): mol = gto.Mole() mol.verbose = 3 mol.atom = ''' H 0. -2.757 2.857 H 0. 2.757 2.857 ''' mol.basis = '3-21g' mol.build() env_method = 'm06' hl_method = 'ccsd' subsys = cluster_subsystem.ClusterHLSubSystem(mol, env_method, hl_method) mol2 = gto.Mole() mol2.verbose = 3 mol2.atom = ''' He 1.0 20.0 0.0 He 3.0 20.0 0.0''' mol2.basis = '3-21g' mol2.build() env_method = 'm06' subsys2 = cluster_subsystem.ClusterEnvSubSystem(mol2, env_method) mol12 = helpers.concat_mols([mol, mol2]) fs_scf_obj = helpers.gen_scf_obj(mol12, 'm06') supersystem = cluster_supersystem.ClusterSuperSystem([subsys, subsys2], 'm06', fs_scf_obj, init_guess='minao') self.assertEqual(supersystem.env_method, 'm06') self.assertEqual(supersystem.proj_oper, 'huz') self.assertEqual(supersystem.max_cycle, 200) self.assertEqual(supersystem.conv_tol, 1e-9) self.assertEqual(supersystem.damp, 0) self.assertEqual(supersystem.init_guess, 'minao') self.assertEqual(supersystem.fock_subcycles, 1) #Check SCF object scf_obj = supersystem.fs_scf_obj comp_scf_obj = dft.RKS(gto.mole.conc_mol(mol, mol2)) self.assertEqual(type(scf_obj), type(comp_scf_obj)) print (comp_scf_obj.mol._basis) print (supersystem.mol._basis) self.assertTrue(gto.same_mol(comp_scf_obj.mol, supersystem.mol, cmp_basis=False)) self.assertEqual(scf_obj.xc, 'm06')
def gen_supersystems(sup_kwargs, subsystems, filename, scrdir): """Generates the list of supersystems made from all subsystems. Parameters __________ sup_kwargs : array An array of supersystem kwarg dictionaries. subsystems : array An array of subsystem objects. filename : str The name of the input file. scrdir : str The path to the scratch directory Returns _______ Array A list of supersystem objects. """ supersystems = [] sorted_subs = sorted(subsystems, key=lambda x: x.env_order) while len(sorted_subs) > 0: curr_order = sorted_subs[0].env_order curr_method = sorted_subs[0].env_method curr_sup_kwargs = {} sub_sup_kwargs = {} if sup_kwargs is not None: match_sup_kwargs = [ x for x in sup_kwargs if x['env_order'] == curr_order ] sub_sup_kwargs = [ x for x in sup_kwargs if x['env_order'] == (curr_order + 1) ] assert len(match_sup_kwargs) < 2, 'Ambigious supersystem settings' curr_sup_kwargs = match_sup_kwargs[0] higher_order_subs = [ x for x in sorted_subs if x.env_order > curr_order ] sub_list = [] while len(sorted_subs) > 0 and sorted_subs[0].env_order == curr_order: sub_list.append(sorted_subs.pop(0)) if len(higher_order_subs) > 0: if len(sub_sup_kwargs) > 0: combined_subs = combine_subsystems(higher_order_subs, curr_method, fs_kwargs=sub_sup_kwargs[0]) else: combined_subs = combine_subsystems(higher_order_subs, curr_method) sub_list.append(combined_subs) mol_list = [x.mol for x in sub_list] full_mol = helpers.concat_mols(mol_list) fs_env_settings = curr_sup_kwargs.pop('fs_env_settings', None) if fs_env_settings: env_method = fs_env_settings.pop('env_method', None) if not env_method: env_method = sub_list[0].env_method fs_scf_obj = helpers.gen_scf_obj(full_mol, env_method, **fs_env_settings) else: env_method = sub_list[0].env_method fs_scf_obj = helpers.gen_scf_obj(full_mol, env_method) embed_dict = curr_sup_kwargs.get('embed_settings', None) if embed_dict: embed_dict['filename'] = filename supersystem = cluster_supersystem.ClusterSuperSystem( sub_list, env_method, fs_scf_obj, env_order=curr_order, **embed_dict) else: supersystem = cluster_supersystem.ClusterSuperSystem( sub_list, env_method, fs_scf_obj, env_order=curr_order, filename=filename) supersystems.append(supersystem) return supersystems
def test_custom_supersystem(self): t_file = tempfile.NamedTemporaryFile() mol = gto.Mole() mol.verbose = 3 mol.atom = ''' O 0.0 0.0 0.0 H 0. -2.757 2.857 H 0. 2.757 2.857''' mol.basis = 'aug-cc-pVDZ' mol.build() env_method = 'm06' hl_method = 'ccsd' hl_dict = {'froz_orbs': 10} subsys = cluster_subsystem.ClusterHLSubSystem(mol, env_method, hl_method, filename=t_file.name, hl_dict=hl_dict) mol2 = gto.Mole() mol2.verbose = 3 mol2.atom = ''' O 1.0 0.0 0.0 O 3.0 0.0 0.0''' mol2.basis = 'aug-cc-pVDZ' mol2.verbose = 1 mol2.build() env_method = 'm06' subsys2 = cluster_subsystem.ClusterEnvSubSystem(mol2, env_method, filename=t_file.name) mol12 = helpers.concat_mols([mol, mol2]) fs_scf_obj = helpers.gen_scf_obj(mol12, 'b3lyp', max_cycle=3, conv_tol=2, damp=1, level_shift_factor=2.1, init_guess='atom', grid_level=2, small_rho_cutoff=1e-2, save_orbs=True, save_density=True, stability_analysis='internal') supersystem = cluster_supersystem.ClusterSuperSystem([subsys, subsys2], 'b3lyp', fs_scf_obj, proj_oper='huzfermi', max_cycle=2, conv_tol=1e-1, diis_num=3, init_guess='1e', fock_subcycles=1, set_fermi=-0.1, damp=0.5, compare_density=True, save_density=True, save_orbs=True, filename=t_file.name) self.assertEqual(supersystem.env_method, 'b3lyp') self.assertEqual(supersystem.proj_oper, 'huzfermi') self.assertEqual(supersystem.filename, t_file.name) self.assertEqual(supersystem.max_cycle, 2) self.assertEqual(supersystem.conv_tol, 1e-1) self.assertEqual(supersystem.damp, 0.5) self.assertIsInstance(supersystem.emb_diis, lib.diis.DIIS) self.assertIsInstance(supersystem.emb_diis_2, lib.diis.DIIS) self.assertEqual(supersystem.set_fermi, -0.1) self.assertEqual(supersystem.init_guess, '1e') self.assertEqual(supersystem.fock_subcycles, 1) self.assertEqual(supersystem.compare_density, True) self.assertEqual(supersystem.save_orbs, True) self.assertEqual(supersystem.save_density, True) fs_scf_obj = supersystem.fs_scf_obj self.assertEqual(fs_scf_obj.max_cycle, 3) self.assertEqual(fs_scf_obj.conv_tol, 2) self.assertEqual(fs_scf_obj.damp, 1) self.assertEqual(fs_scf_obj.level_shift_factor, 2.1) self.assertEqual(fs_scf_obj.init_guess, 'atom') self.assertEqual(fs_scf_obj.grids.level, 2) self.assertEqual(fs_scf_obj.small_rho_cutoff, 1e-2) self.assertEqual(fs_scf_obj.verbose, 1) self.assertEqual(fs_scf_obj.stability_analysis, 'internal') self.assertEqual(fs_scf_obj.save_orbs, True) self.assertEqual(fs_scf_obj.save_density, True)
react_total_energy += ccsd_t.kernel(react_cc, react_cc.ao2mo()) trans_mf = scf.RHF(trans_mol) trans_total_energy = trans_mf.kernel() trans_cc = cc.CCSD(trans_mf) trans_total_energy += trans_cc.kernel()[0] trans_total_energy += ccsd_t.kernel(trans_cc, trans_cc.ao2mo()) print("Canonical Difference") print(trans_total_energy - react_total_energy) sub1_react = cluster_subsystem.ClusterActiveSubSystem(sub1_react_mol, dft_method, active_method) sub2_react = cluster_subsystem.ClusterEnvSubSystem(sub2_react_mol, dft_method) sup_react = cluster_supersystem.ClusterSuperSystem([sub1_react, sub2_react], dft_method) sup_react.freeze_and_thaw() sup_react.get_active_energy() sup_react_energy = sup_react.get_supersystem_energy() react_energy = sup_react_energy - sup_react.subsystems[ 0].env_energy + sup_react.subsystems[0].active_energy sub1_trans = cluster_subsystem.ClusterActiveSubSystem(sub1_trans_mol, dft_method, active_method) sub2_trans = cluster_subsystem.ClusterEnvSubSystem(sub2_trans_mol, dft_method) sup_trans = cluster_supersystem.ClusterSuperSystem([sub1_trans, sub2_trans], dft_method) sup_trans.freeze_and_thaw() sup_trans.get_active_energy() sup_trans_energy = sup_trans.get_supersystem_energy()
sub1_trans_mol.atom = sub1_trans sub1_trans_mol.basis = basis_to_use sub1_trans_mol.charge = -2 sub1_trans_mol.build() sub2_trans_mol = gto.Mole() sub2_trans_mol.atom = sub2_trans sub2_trans_mol.basis = basis_to_use sub2_trans_mol.charge = 1 sub2_trans_mol.build() sub1_react = cluster_subsystem.ClusterActiveSubSystem(sub1_react_mol, dft_method, active_method) sub2_react = cluster_subsystem.ClusterEnvSubSystem(sub2_react_mol, dft_method) sup_react = cluster_supersystem.ClusterSuperSystem([sub1_react, sub2_react], dft_method, ft_initguess='localsup', ft_updatefock=1) sup_react.freeze_and_thaw() sub1_trans = cluster_subsystem.ClusterActiveSubSystem(sub1_trans_mol, dft_method, active_method) sub2_trans = cluster_subsystem.ClusterEnvSubSystem(sub2_trans_mol, dft_method) sup_trans = cluster_supersystem.ClusterSuperSystem([sub1_trans, sub2_trans], dft_method, ft_initguess='localsup', ft_updatefock=1) sup_trans.freeze_and_thaw()
def optimal_density_damping(): output_filename = 'optimal_density_damping_results.out' molO = MolObjects() moliter = iter(molO) x = next(moliter) num = 0 while x is not None: for gp in grid_points: for ig in init_guess: for xc in xc_fun: for fud in range(2): for pud in range(fud, 2): header_string = f"molnum: {num}\nbasis: {x[0].basis}\ncharge: {x[0].charge}\ngridsize: {gp}\ninitguess: {ig}\nxc_fun: {xc}\nfock_update: {fud}\nproj_update: {pud}\n" with open(output_filename, 'a') as fout: fout.write(header_string) sub1 = cluster_subsystem.ClusterEnvSubSystem( x[0], xc, damp=-1) sub2 = cluster_subsystem.ClusterEnvSubSystem( x[1], xc, damp=-1) sup = cluster_supersystem.ClusterSuperSystem( [sub1, sub2], xc, fs_grid_level=gp, ft_cycles=100, ft_initguess=ig, ft_updatefock=fud, ft_updateproj=pud, ft_diis=None) sup.init_density() start_time = time.time() sup.freeze_and_thaw() end_time = time.time() elapsed_time = end_time - start_time cycles = sup.ft_iter write_string = f" FT cycles: {cycles}\n Elapsed Time: {elapsed_time}\n Average time per cycle: {elapsed_time/float(cycles)}\n Sub1 E: {sub1.get_env_energy()}\n Sub2 E: {sub2.get_env_energy()}\n\n" with open(output_filename, 'a') as fout: fout.write(write_string) if x[0].spin != 0 or x[1].spin != 0: sub1 = cluster_subsystem.ClusterEnvSubSystem( x[0], xc, unrestricted=True, damp=-1) sub2 = cluster_subsystem.ClusterEnvSubSystem( x[1], xc, unrestricted=True, damp=-1) sup = cluster_supersystem.ClusterSuperSystem( [sub1, sub2], xc, fs_grid_level=gp, ft_cycles=100, ft_initguess=ig, ft_updatefock=fud, ft_updateproj=pud, fs_unrestricted=True, ft_unrestricted=True, ft_diis=None) sup.init_density() start_time = time.time() sup.freeze_and_thaw() end_time = time.time() elapsed_time = end_time - start_time cycles = sup.ft_iter write_string = f"Unrestricted\n FT cycles: {cycles}\n Elapsed Time: {elapsed_time}\n Average time per cycle: {elapsed_time/float(cycles)}\n Sub1 E: {sub1.get_env_energy()}\n Sub2 E: {sub2.get_env_energy()}\n\n" num += 2 x = next(moliter) print("Progress") print(f"{num/2} Done Total")