def test_mopac_opt_calculation(): calc = Calculation(name='opt', molecule=methylchloride, method=method, keywords=Config.MOPAC.keywords.opt) calc.run() assert os.path.exists('opt_mopac.mop') is True assert os.path.exists('opt_mopac.out') is True assert len(calc.get_final_atoms()) == 5 # Actual energy in Hartrees energy = Constants.eV2ha * -430.43191 assert energy - 0.0001 < calc.get_energy() < energy + 0.0001 assert calc.output.exists() assert calc.output.file_lines is not None assert calc.input.filename == 'opt_mopac.mop' assert calc.output.filename == 'opt_mopac.out' assert calc.terminated_normally() assert calc.optimisation_converged() is True with pytest.raises(CouldNotGetProperty): _ = calc.get_gradients() with pytest.raises(NotImplementedError): _ = calc.optimisation_nearly_converged() with pytest.raises(NotImplementedError): _ = calc.get_imaginary_freqs() with pytest.raises(NotImplementedError): _ = calc.get_normal_mode_displacements(4)
def test_grad(): h2 = Molecule(name='h2', atoms=[Atom('H'), Atom('H', x=0.5)]) grad_calc = Calculation(name='h2_grad', molecule=h2, method=method, keywords=Config.MOPAC.keywords.grad) grad_calc.run() energy = grad_calc.get_energy() assert energy is not None gradients = grad_calc.get_gradients() assert gradients.shape == (2, 3) delta_r = 1E-5 h2_disp = Molecule(name='h2_disp', atoms=[Atom('H'), Atom('H', x=0.5 + delta_r)]) h2_disp.single_point(method) delta_energy = h2_disp.energy - energy # Ha] grad = delta_energy / delta_r # Ha A^-1 # Difference between the absolute and finite difference approximation assert np.abs(gradients[1, 0] - grad) < 1E-1 # Broken gradient file grad_calc.output.filename = 'h2_grad_broken.out' grad_calc.output.file_lines = open('h2_grad_broken.out', 'r').readlines() with pytest.raises(CouldNotGetProperty): _ = grad_calc.get_gradients()
def test_opt_calc(): calc = Calculation(name='opt', molecule=test_mol, method=method, keywords=opt_keywords) calc.run() assert os.path.exists('opt_nwchem.nw') assert os.path.exists('opt_nwchem.out') final_atoms = calc.get_final_atoms() assert len(final_atoms) == 5 assert type(final_atoms[0]) is Atom assert -40.4165 < calc.get_energy() < -40.4164 assert calc.output.exists() assert calc.output.file_lines is not None assert calc.get_imaginary_freqs() == [] assert calc.input.filename == 'opt_nwchem.nw' assert calc.output.filename == 'opt_nwchem.out' assert calc.terminated_normally() assert calc.optimisation_converged() assert calc.optimisation_nearly_converged() is False charges = calc.get_atomic_charges() assert len(charges) == 5 assert all(-1.0 < c < 1.0 for c in charges) # Optimisation should result in small gradients gradients = calc.get_gradients() assert len(gradients) == 5 assert all(-0.1 < np.linalg.norm(g) < 0.1 for g in gradients)
def optimise(self, method, reset_graph=False, calc=None): """ Optimise the geometry of this conformer Arguments: method (autode.wrappers.base.ElectronicStructureMethod): Keyword Arguments: reset_graph (bool): calc (autode.calculation.Calculation): """ logger.info(f'Running optimisation of {self.name}') if calc is not None or reset_graph: raise NotImplementedError opt = Calculation(name=f'{self.name}_opt', molecule=self, method=method, keywords=method.keywords.low_opt, n_cores=Config.n_cores, distance_constraints=self.dist_consts) opt.run() self.energy = opt.get_energy() try: self.set_atoms(atoms=opt.get_final_atoms()) except AtomsNotFound: logger.error(f'Atoms not found for {self.name} but not critical') self.set_atoms(atoms=None) return None
def test_constraints(): os.chdir(os.path.join(here, 'data')) calc = Calculation(name='const_dist_opt', molecule=test_mol, method=method, keywords=opt_keywords, distance_constraints={(0, 1): 1.2}) calc.run() opt_atoms = calc.get_final_atoms() assert 1.199 < np.linalg.norm(opt_atoms[0].coord - opt_atoms[1].coord) < 1.201 calc = Calculation(name='const_cart_opt', molecule=test_mol, method=method, keywords=opt_keywords, cartesian_constraints=[0]) calc.run() opt_atoms = calc.get_final_atoms() assert np.linalg.norm(test_mol.atoms[0].coord - opt_atoms[0].coord) < 1E-3 os.remove('const_cart_opt_g09.com') os.remove('const_dist_opt_g09.com') os.chdir(os.path.join(here))
def test_orca_optts_calculation(): methane = SolvatedMolecule(name='methane', smiles='C') methane.qm_solvent_atoms = [] calc = Calculation(name='test_ts_reopt_optts', molecule=methane, method=method, bond_ids_to_add=[(0, 1)], keywords=opt_keywords, other_input_block='%geom\n' 'Calc_Hess true\n' 'Recalc_Hess 40\n' 'Trust 0.2\n' 'MaxIter 100\nend') calc.run() assert os.path.exists('test_ts_reopt_optts_orca.inp') assert calc.get_normal_mode_displacements(mode_number=6) is not None assert calc.terminated_normally() assert calc.optimisation_converged() assert calc.optimisation_nearly_converged() is False assert len(calc.get_imaginary_freqs()) == 1 # Gradients should be an n_atom x 3 array gradients = calc.get_gradients() assert len(gradients) == 5 assert len(gradients[0]) == 3 assert -599.437 < calc.get_enthalpy() < -599.436 assert -599.469 < calc.get_free_energy() < -599.468
def test_psi4_opt_calculation(): methylchloride = Molecule(name='CH3Cl', smiles='[H]C([H])(Cl)[H]', solvent_name='water') calc = Calculation(name='opt', molecule=methylchloride, method=method, keywords=opt_keywords) calc.run() assert os.path.exists('opt_psi4.inp') is True assert os.path.exists('opt_orca.out') is True assert len(calc.get_final_atoms()) == 5 assert -499.735 < calc.get_energy() < -499.730 assert calc.output.exists() assert calc.output.file_lines is not None assert calc.get_imaginary_freqs() == [] assert calc.input.filename == 'opt_psi4.inp' assert calc.output.filename == 'opt_psi4.out' assert calc.terminated_normally() assert calc.optimisation_converged() calc = Calculation(name='opt', molecule=methylchloride, method=method, keywords=opt_keywords) # If the calculation is not run with calc.run() then there should be no # input and the calc should raise that there is no input with pytest.raises(NoInputError): execute_calc(calc)
def test_gauss_optts_calc(): os.chdir(os.path.join(here, 'data')) calc = Calculation(name='test_ts_reopt_optts', molecule=test_mol, method=method, keywords=optts_keywords, bond_ids_to_add=[(0, 1)]) calc.run() print(calc.input.added_internals) assert os.path.exists('test_ts_reopt_optts_g09.com') bond_added = False for line in open('test_ts_reopt_optts_g09.com', 'r'): if 'B' in line and len(line.split()) == 3: bond_added = True assert line.split()[0] == 'B' assert line.split()[1] == '1' assert line.split()[2] == '2' assert bond_added assert calc.get_normal_mode_displacements(mode_number=6) is not None assert calc.terminated_normally() assert calc.optimisation_converged() assert calc.optimisation_nearly_converged() is False assert len(calc.get_imaginary_freqs()) == 1 assert -40.324 < calc.get_free_energy() < -40.322 assert -40.301 < calc.get_enthalpy() < -40.299 os.remove('test_ts_reopt_optts_g09.com') os.chdir(here)
def optimise(self, method=None, reset_graph=False, calc=None): """ Optimise the geometry using a method Arguments: method (autode.wrappers.base.ElectronicStructureMethod): Keyword Arguments: reset_graph (bool): Reset the molecular graph calc (autode.calculation.Calculation): Different e.g. constrained optimisation calculation """ logger.info(f'Running optimisation of {self.name}') if calc is None: assert method is not None calc = Calculation(name=f'{self.name}_opt', molecule=self, method=method, keywords=method.keywords.opt, n_cores=Config.n_cores) else: assert isinstance(calc, Calculation) calc.run() self.energy = calc.get_energy() self.set_atoms(atoms=calc.get_final_atoms()) self.print_xyz_file(filename=f'{self.name}_optimised_{method.name}.xyz') if reset_graph: make_graph(self) return None
def test_solvation(): methane = Molecule(name='solvated_methane', smiles='C', solvent_name='water') with pytest.raises(UnsuppportedCalculationInput): # Should raise on unsupported calculation type method.implicit_solvation_type = 'xxx' calc = Calculation(name='broken_solvation', molecule=methane, method=method, keywords=sp_keywords) calc.run() method.implicit_solvation_type = 'CPCM' calc = Calculation(name='methane_cpcm', molecule=methane, method=method, keywords=sp_keywords) calc.generate_input() assert any('cpcm' in line.lower() for line in open('methane_cpcm_orca.inp', 'r')) os.remove('methane_cpcm_orca.inp') method.implicit_solvation_type = 'SMD' calc = Calculation(name='methane_smd', molecule=methane, method=method, keywords=sp_keywords) calc.generate_input() assert any('smd' in line.lower() for line in open('methane_smd_orca.inp', 'r')) os.remove('methane_smd_orca.inp')
def get_optimised_species(calc, method, direction, atoms): """Get the species that is optimised from an initial set of atoms""" species = Molecule(name=f'{calc.name}_{direction}', atoms=atoms, charge=calc.molecule.charge, mult=calc.molecule.mult) # Note that for the surface to be the same the keywords.opt and keywords.hess need to match in the level of theory calc = Calculation(name=f'{calc.name}_{direction}', molecule=species, method=method, keywords=method.keywords.opt, n_cores=Config.n_cores) calc.run() try: species.set_atoms(atoms=calc.get_final_atoms()) species.energy = calc.get_energy() make_graph(species) except AtomsNotFound: logger.error(f'{direction} displacement calculation failed') return species
def singlepoint(molecule, method, keywords, n_cores=None): """ Run a single point energy evaluation on a molecule :param molecule: (object) :param method: (autode.ElectronicStructureMethod) :param keywords: (list(str)) Keywords to use for the electronic structure calculation e.g. ['Opt', 'PBE', 'def2-SVP'] :param n_cores: (int) Number of cores to use :return: """ logger.info('Running single point calculation') n_cores = Config.n_cores if n_cores is None else int(n_cores) try: from autode.calculation import Calculation from autode.wrappers.XTB import xtb from autode.wrappers.ORCA import orca from autode.wrappers.keywords import SinglePointKeywords except ModuleNotFoundError: logger.error('autode not found. Calculations not available') raise RequiresAutodE if keywords is None: if method == orca: keywords = SinglePointKeywords( ['SP', 'M062X', 'def2-TZVP', 'RIJCOSX', 'def2/J', 'SlowConv']) logger.warning('No keywords were set for the single point but an ' 'ORCA calculation was requested. ' f'Using {str(keywords)}') elif method == xtb: keywords = xtb.keywords.sp else: logger.critical('No keywords were set for the single-point ' 'calculation') raise Exception else: # If the keywords are specified as a list convert them to a set of # OptKeywords, required for autodE if type(keywords) is list: keywords = SinglePointKeywords(keywords) sp = Calculation(name=molecule.name + '_sp', molecule=molecule, method=method, keywords=keywords, n_cores=n_cores) sp.run() molecule.energy = sp.get_energy() return None
def test_gradients(): os.chdir(os.path.join(here, 'data', 'xtb')) h2 = Molecule(name='h2', atoms=[Atom('H'), Atom('H', x=1.0)]) h2.single_point(method) delta_r = 1E-5 h2_disp = Molecule(name='h2_disp', atoms=[Atom('H'), Atom('H', x=1.0 + delta_r)]) h2_disp.single_point(method) delta_energy = h2_disp.energy - h2.energy # Ha grad = delta_energy / delta_r # Ha A^-1 calc = Calculation(name='h2_grad', molecule=h2, method=method, keywords=method.keywords.grad) calc.run() diff = calc.get_gradients()[1, 0] - grad # Ha A^-1 # Difference between the absolute and finite difference approximation assert np.abs(diff) < 1E-5 # Older xtb version with open('gradient', 'w') as gradient_file: print( '$gradient\n' 'cycle = 1 SCF energy = -4.17404780397 |dE/dxyz| = 0.027866\n' '3.63797523123375 -1.13138130908142 -0.00032759661848 C \n' '5.72449332438353 -1.13197561185651 0.00028950521969 H \n' ' 2.94133258016711 0.22776472016180 -1.42078243039077 H \n' ' 2.94175598539510 -0.58111835182372 1.88747566982948 H \n' '2.94180792167968 -3.04156357656436 -0.46665514803992 H \n' '-1.7221823521705E-05 7.9930724499610E-05 -1.1737079840097E-04\n' ' 1.4116296505865E-02 -4.0359524399270E-05 3.9719638516747E-05\n' '-4.7199424681741E-03 9.0086220034949E-03 -9.4114548523723E-03\n' '-4.6956970257351E-03 3.6356853660431E-03 1.2558467871909E-02\n' ' -4.6834351884340E-03 -1.2683878569638E-02 -3.0693618596526E-03\n' '$end', file=gradient_file) calc = Calculation(name='methane', molecule=Molecule(name='methane', smiles='C'), method=method, keywords=method.keywords.grad) gradients = method.get_gradients(calc) assert gradients.shape == (5, 3) assert np.abs(gradients[0, 0]) < 1E-3 os.chdir(here)
def single_point(self, method): """Calculate the single point energy of the species with a autode.wrappers.base.ElectronicStructureMethod""" logger.info(f'Running single point energy evaluation of {self.name}') sp = Calculation(name=f'{self.name}_sp', molecule=self, method=method, keywords=method.keywords.sp, n_cores=Config.n_cores) sp.run() self.energy = sp.get_energy() return None
def _run_hess_calculation(self, method, temp): """Run a Hessian calculation on this species""" method = method if method is not None else get_hmethod() calc = Calculation(name=f'{self.name}_hess', molecule=self, method=method, keywords=method.keywords.hess, n_cores=Config.n_cores, temp=temp) calc.run() return calc
def set_charges_vdw(species): """Calculate the partial atomic charges to atoms with XTB""" calc = Calculation(name='tmp', molecule=species, method=XTB(), keywords=ade.SinglePointKeywords([])) calc.run() charges = calc.get_atomic_charges() for i, atom in enumerate(species.atoms): atom.charge = charges[i] atom.vdw = get_vdw_radius(atom_label=atom.label) return None
def test_point_charge_calc(): os.chdir(os.path.join(here, 'data')) # Methane single point using a point charge with a unit positive charge # located at (10, 10, 10) calc = Calculation( name='methane_point_charge', molecule=test_mol, method=method, keywords=sp_keywords, point_charges=[PointCharge(charge=1.0, x=10.0, y=10.0, z=10.0)]) calc.run() # Assert that the input file is in the expected configuration for line in open('methane_point_charge_g09.com', 'r'): if 'PBE' in line: assert 'Charge' in line if len(line.split()) == 4: if not line.split()[0].isdigit(): continue x, y, z, charge = line.split() assert float(x) == 10.0 assert float(y) == 10.0 assert float(z) == 10.0 assert float(charge) == 1.0 assert -40.428 < calc.get_energy() < -40.427 # Gaussian needs x-matrix and nosymm in the input line to run optimisations # with point charges.. for opt_keyword in ['Opt', 'Opt=Tight', 'Opt=(Tight)']: calc = Calculation( name='methane_point_charge_o', molecule=test_mol, method=method, keywords=OptKeywords(['PBE1PBE/Def2SVP', opt_keyword]), point_charges=[PointCharge(charge=1.0, x=3.0, y=3.0, z=3.0)]) calc.generate_input() for line in open('methane_point_charge_o_g09.com', 'r').readlines(): if 'PBE' in line: assert 'charge' in line.lower() assert 'z-matrix' in line.lower() and 'nosymm' in line.lower() break os.remove('methane_point_charge_g09.com') os.remove('methane_point_charge_o_g09.com') os.chdir(os.path.join(here))
def test_constraints(): calc = Calculation(name='const_dist_opt', molecule=test_mol, method=method, keywords=opt_keywords, distance_constraints={(0, 1): 1.2}) calc.run() opt_atoms = calc.get_final_atoms() assert 1.199 < np.linalg.norm(opt_atoms[0].coord - opt_atoms[1].coord) < 1.201 calc = Calculation(name='const_cart_opt', molecule=test_mol, method=method, keywords=opt_keywords, cartesian_constraints=[0]) calc.run() opt_atoms = calc.get_final_atoms() assert np.linalg.norm(test_mol.atoms[0].coord - opt_atoms[0].coord) < 1E-3
def test_xtb_calculation(): test_mol = Molecule(name='test_mol', smiles='O=C(C=C1)[C@@](C2NC3C=C2)([H])[C@@]3([H])C1=O') calc = Calculation(name='opt', molecule=test_mol, method=method, keywords=Config.XTB.keywords.opt) calc.run() assert os.path.exists('opt_xtb.xyz') is True assert os.path.exists('opt_xtb.out') is True assert len(calc.get_final_atoms()) == 22 assert calc.get_energy() == -36.990267613593 assert calc.output.exists() assert calc.output.file_lines is not None assert calc.input.filename == 'opt_xtb.xyz' assert calc.output.filename == 'opt_xtb.out' with pytest.raises(NotImplementedError): calc.optimisation_nearly_converged() with pytest.raises(NotImplementedError): calc.get_imaginary_freqs() with pytest.raises(NotImplementedError): calc.get_normal_mode_displacements(4) charges = calc.get_atomic_charges() assert len(charges) == 22 assert all(-1.0 < c < 1.0 for c in charges) const_opt = Calculation(name='const_opt', molecule=test_mol, method=method, distance_constraints={(0, 1): 1.2539792}, cartesian_constraints=[0], keywords=Config.XTB.keywords.opt) const_opt.generate_input() assert os.path.exists('const_opt_xtb.xyz') assert os.path.exists('xcontrol_const_opt_xtb') const_opt.clean_up(force=True) assert not os.path.exists('xcontrol_const_opt_xtb') # Write an empty output file open('tmp.out', 'w').close() const_opt.output.filename = 'tmp.out' const_opt.output.set_lines() # cannot get atoms from an empty file with pytest.raises(AtomsNotFound): _ = const_opt.get_final_atoms()
def test_point_charge(): os.chdir(os.path.join(here, 'data', 'xtb')) test_mol = Molecule(name='test_mol', smiles='C') # Methane with a point charge fairly far away calc = Calculation(name='opt_point_charge', molecule=test_mol, method=method, keywords=Config.XTB.keywords.opt, point_charges=[PointCharge(charge=1.0, x=10, y=1, z=1)]) calc.run() assert -4.178 < calc.get_energy() < -4.175 os.chdir(here)
def test_fix_angle_error(): os.chdir(os.path.join(here, 'data', 'g09')) mol = Molecule(smiles='CC/C=C/CO') mol.name = 'molecule' calc = Calculation(name='angle_fail', molecule=mol, method=method, keywords=opt_keywords) calc.run() assert os.path.exists('angle_fail_g09_cartesian.com') is True assert os.path.exists('angle_fail_g09_internal.com') is True assert calc.output.filename == 'angle_fail_g09_internal.log' assert calc.terminated_normally()
def test_mopac_with_pc(): calc = Calculation(name='opt_pc', molecule=methylchloride, method=method, keywords=Config.MOPAC.keywords.opt, point_charges=[PointCharge(1, x=4, y=4, z=4)]) calc.run() assert os.path.exists('opt_pc_mopac.mop') is True assert os.path.exists('opt_pc_mopac.out') is True assert len(calc.get_final_atoms()) == 5 # Actual energy in Hartrees without any point charges energy = Constants.eV2ha * -430.43191 assert np.abs(calc.get_energy() - energy) > 0.0001
def run_autode(configuration, max_force=None, method=None, n_cores=1): """ Run an orca or xtb calculation -------------------------------------------------------------------------- :param configuration: (gaptrain.configurations.Configuration) :param max_force: (float) or None :param method: (autode.wrappers.base.ElectronicStructureMethod) """ from autode.species import Species from autode.calculation import Calculation from autode.exceptions import CouldNotGetProperty if method.name == 'orca' and GTConfig.orca_keywords is None: raise ValueError("For ORCA training GTConfig.orca_keywords must be" " set. e.g. " "GradientKeywords(['PBE', 'def2-SVP', 'EnGrad'])") # optimisation is not implemented, needs a method to run assert max_force is None and method is not None species = Species(name=configuration.name, atoms=configuration.atoms, charge=configuration.charge, mult=configuration.mult) # allow for an ORCA calculation to have non-default keywords.. not the # cleanest implementation.. kwds = GTConfig.orca_keywords if method.name == 'orca' else method.keywords.grad calc = Calculation(name='tmp', molecule=species, method=method, keywords=kwds, n_cores=n_cores) calc.run() ha_to_ev = 27.2114 try: configuration.forces = -ha_to_ev * calc.get_gradients() except CouldNotGetProperty: logger.error('Failed to set forces') configuration.energy = ha_to_ev * calc.get_energy() configuration.partial_charges = calc.get_atomic_charges() return configuration
def test_solvation(): """Solvation not implemented for psi4""" methane = Molecule(name='solvated_methane', smiles='C', solvent_name='water') with pytest.raises(UnsuppportedCalculationInput): # Should raise an unsupported calculation type, the only # "supported" implicit solvation type is 'not_supported' method.implicit_solvation_type = 'xxx' calc = Calculation(name='broken_solvation', molecule=methane, method=method, keywords=sp_keywords) calc.run()
def test_gauss_opt_calc(): os.chdir(os.path.join(here, 'data')) methylchloride = Molecule(name='CH3Cl', smiles='[H]C([H])(Cl)[H]', solvent_name='water') calc = Calculation(name='opt', molecule=methylchloride, method=method, keywords=opt_keywords) calc.run() assert os.path.exists('opt_g09.com') assert os.path.exists('opt_g09.log') assert len(calc.get_final_atoms()) == 5 assert os.path.exists('opt_g09.xyz') assert calc.get_energy() == -499.729222331 assert calc.output.exists() assert calc.output.file_lines is not None assert calc.get_imaginary_freqs() == [] with pytest.raises(NoNormalModesFound): calc.get_normal_mode_displacements(mode_number=1) assert calc.input.filename == 'opt_g09.com' assert calc.output.filename == 'opt_g09.log' assert calc.terminated_normally() assert calc.optimisation_converged() assert calc.optimisation_nearly_converged() is False charges = calc.get_atomic_charges() assert len(charges) == methylchloride.n_atoms # Should be no very large atomic charges in this molecule assert all(-1.0 < c < 1.0 for c in charges) gradients = calc.get_gradients() assert len(gradients) == methylchloride.n_atoms assert len(gradients[0]) == 3 # Should be no large forces for an optimised molecule assert sum(gradients[0]) < 0.1 os.remove('opt_g09.com') os.chdir(here)
def test_exec_not_avail_method(): orca = ORCA() orca.path = '/a/non/existent/path' assert not orca.available calc = Calculation(name='tmp', molecule=test_mol, method=orca, keywords=orca.keywords.sp) calc.generate_input() with pytest.raises(ex.MethodUnavailable): calc.execute_calculation() with pytest.raises(ex.MethodUnavailable): calc.run()
def test_orca_opt_calculation(): os.chdir(os.path.join(here, 'data')) methylchloride = Molecule(name='CH3Cl', smiles='[H]C([H])(Cl)[H]', solvent_name='water') calc = Calculation(name='opt', molecule=methylchloride, method=method, keywords=opt_keywords) calc.run() assert os.path.exists('opt_orca.inp') is True assert os.path.exists('opt_orca.out') is True assert len(calc.get_final_atoms()) == 5 assert -499.735 < calc.get_energy() < -499.730 assert calc.output.exists() assert calc.output.file_lines is not None assert calc.get_imaginary_freqs() == [] assert calc.input.filename == 'opt_orca.inp' assert calc.output.filename == 'opt_orca.out' assert calc.terminated_normally() assert calc.optimisation_converged() assert calc.optimisation_nearly_converged() is False with pytest.raises(NoNormalModesFound): calc.get_normal_mode_displacements(mode_number=0) # Should have a partial atomic charge for every atom charges = calc.get_atomic_charges() assert len(charges) == 5 assert type(charges[0]) == float assert -1.0 < charges[0] < 1.0 calc = Calculation(name='opt', molecule=methylchloride, method=method, keywords=opt_keywords) # If the calculation is not run with calc.run() then there should be no # input and the calc should raise that there is no input with pytest.raises(NoInputError): execute_calc(calc) os.remove('opt_orca.inp') os.chdir(here)
def test_opt_hf_constraints(): keywords = OptKeywords([ 'driver\n gmax 0.002\n grms 0.0005\n' ' xmax 0.01\n xrms 0.007\n eprec 0.00003\nend', 'basis\n * library Def2-SVP\nend', 'task scf optimize' ]) h2o = Molecule(name='water', smiles='O') calc = Calculation(name='opt_water', molecule=h2o, method=method, keywords=keywords, cartesian_constraints=[0], distance_constraints={(0, 1): 0.95}) calc.run() h2o.atoms = calc.get_final_atoms() assert 0.94 < h2o.distance(0, 1) < 0.96
def test_xtb_calculation(): os.chdir(os.path.join(here, 'data')) XTB.available = True test_mol = Molecule(name='test_mol', smiles='O=C(C=C1)[C@@](C2NC3C=C2)([H])[C@@]3([H])C1=O') calc = Calculation(name='opt', molecule=test_mol, method=method, keywords=Config.XTB.keywords.opt) calc.run() assert os.path.exists('opt_xtb.xyz') is True assert os.path.exists('opt_xtb.out') is True assert len(calc.get_final_atoms()) == 22 assert calc.get_energy() == -36.990267613593 assert calc.output.exists() assert calc.output.file_lines is not None assert calc.input.filename == 'opt_xtb.xyz' assert calc.output.filename == 'opt_xtb.out' with pytest.raises(NotImplementedError): calc.optimisation_nearly_converged() with pytest.raises(NotImplementedError): calc.get_imaginary_freqs() with pytest.raises(NotImplementedError): calc.get_normal_mode_displacements(4) charges = calc.get_atomic_charges() assert len(charges) == 22 assert all(-1.0 < c < 1.0 for c in charges) const_opt = Calculation(name='const_opt', molecule=test_mol, method=method, distance_constraints={(0, 1): 1.2539792}, cartesian_constraints=[0], keywords=Config.XTB.keywords.opt) const_opt.generate_input() assert os.path.exists('xcontrol_const_opt_xtb') os.remove('const_opt_xtb.xyz') os.remove('xcontrol_const_opt_xtb') os.remove('opt_xtb.xyz') os.chdir(here)
def test_constrained_opt(): methane = Molecule(name='methane', smiles='C') calc = Calculation(name='methane_opt', molecule=methane, method=method, keywords=Config.MOPAC.keywords.opt) calc.run() opt_energy = calc.get_energy() # Constrained optimisation with a C–H distance of 1.2 Å # (carbon is the first atom in the file) const = Calculation(name='methane_const', molecule=methane, method=method, keywords=Config.MOPAC.keywords.opt, distance_constraints={(0, 1): 1.2}) const.run() assert opt_energy < const.get_energy()