def test_input_gen(): xtb = XTB() calc = Calculation(name='tmp', molecule=test_mol, method=xtb, keywords=xtb.keywords.sp) Config.keep_input_files = True calc.generate_input() assert os.path.exists('tmp_xtb.xyz') calc.clean_up() # Clean-up should do nothing if keep_input_files = True assert os.path.exists('tmp_xtb.xyz') # but should be able to be forced calc.clean_up(force=True) assert not os.path.exists('tmp_xtb.xyz') # Test the keywords parsing unsupported_func = Functional('PBE', orca='PBE') calc_kwds = Calculation(name='tmp', molecule=test_mol, method=xtb, keywords=SinglePointKeywords([unsupported_func])) with pytest.raises(ex.UnsuppportedCalculationInput): calc_kwds.generate_input()
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 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_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_opt_single_atom(): h = Molecule(name='H', smiles='[H]') calc = Calculation(name='opt_h', molecule=h, method=method, keywords=opt_keywords) calc.generate_input() # Can't do an optimisation of a hydrogen atom.. assert os.path.exists('opt_h_nwchem.nw') input_lines = open('opt_h_nwchem.nw', 'r').readlines() assert 'opt' not in [keyword.lower() for keyword in input_lines[0].split()] os.remove('opt_h_nwchem.nw')
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_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_keyword_setting(): orca = ORCA() orca.keywords.sp.functional = 'B3LYP' # Setter should generate a Functional from the keyword string assert isinstance(orca.keywords.sp.functional, Functional) calc = Calculation(name='tmp', molecule=test_mol.copy(), method=orca, keywords=orca.keywords.sp) calc.generate_input() assert calc.input.exists() # B3LYP should now be in the in input inp_lines = open(calc.input.filename, 'r').readlines() assert any('B3LYP' in line for line in inp_lines) # With a keyword without ORCA defined then raise an exception with pytest.raises(UnsuppportedCalculationInput): orca.keywords.sp.functional = Functional(name='B3LYP', g09='B3LYP') calc = Calculation(name='tmp', molecule=test_mol.copy(), method=orca, keywords=orca.keywords.sp) calc.generate_input() # Without a default wavefunction method defined in the single point method # we can't set keywords.wf with pytest.raises(ValueError): orca.keywords.sp.wf_method = 'HF' # but if we have a WF method in the keywords we should be able to set it orca.keywords.sp = SinglePointKeywords( [WFMethod('MP2'), BasisSet('def2-TZVP')]) orca.keywords.sp.wf_method = 'HF' assert orca.keywords.sp.wf_method == 'HF'
def test_single_atom_opt(): mol = Molecule(smiles='[H]') mol.name = 'molecule' calc = Calculation(name='H', molecule=mol, method=method, keywords=opt_keywords, n_cores=2) calc.generate_input() assert os.path.exists('H_g09.com') input_file_lines = open('H_g09.com', 'r').readlines() n_cores_set = False for line in input_file_lines: if 'PBE' in line: assert 'Opt' not in line if '%nprocshared=2' in line: n_cores_set = True assert n_cores_set