def test_incorrect_mol_spec(fd_incorrect_zmatrix_var): ''' Tests that incorrect lines in the molecule specification fail to be read.''' # checks parse error raised when freezecode set: freeze_code_line = 'H 1 1.0 2.0 3.0' symbol, pos = _get_atoms_info(freeze_code_line) with pytest.raises(ParseError): _get_cartesian_atom_coords(symbol, pos) # checks parse error raised when 'alternate' z-matrix # definition is used incorrect_zmatrix = 'C4 O1 0.8 C2 121.4 O2 150.0 1' with pytest.raises(ParseError): _get_zmatrix_line(incorrect_zmatrix) incorrect_symbol = 'C1-7 0 1 2 3' # Checks parse error raised when # molecule specifications for molecular mechanics # calculations have been used. with pytest.raises(ParseError): _validate_symbol_string(incorrect_symbol) # Expect warning as constants aren't supported so they're # set as vars instead: with pytest.warns(UserWarning): # Expect error as undefined var appears in matrix: with pytest.raises(ParseError): read_gaussian_in(fd_incorrect_zmatrix_var, True)
def test_read_write_gaussian_cartesian_basis_set(fd_cartesian_basis_set, cartesian_setup): # Tests reading a Gaussian input file with: # - Cartesian coordinates for the atom positions. # - ASE formatted method and basis # - Masses defined using readiso section atoms, params = cartesian_setup atoms.pbc = None atoms.cell = None iso_params = {'temperature': '300', 'pressure': '1.0', 'scale': '1.0'} params.update(iso_params) params['opt'] = 'tight maxcyc=100' params['frequency'] = 'anharmonic' params['basis'] = 'gen' params['method'] = 'g1' params['fitting_basis'] = 'tzvpfit' params['save'] = '' params['basis_set'] = _basis_set_text atoms_new = read_gaussian_in(fd_cartesian_basis_set, True) atoms_new.set_masses(_get_iso_masses(atoms_new)) _check_atom_properties(atoms, atoms_new, params) # Now we have tested reading the input, we can test writing it # and reading it back in. # Expect warning due to g1 being a composite method: with pytest.warns(UserWarning): _test_write_gaussian(atoms_new, params)
def test_read_write_gaussian_cartesian(fd_cartesian, cartesian_setup): '''Tests the read_gaussian_in and write_gaussian_in methods. For the input text given by each fixture we do the following: - Check reading in the text generates the Atoms object and Calculator that we would expect to get. - Check that writing out the resulting Atoms object and reading it back in generates the same Atoms object and parameters. ''' # Tests reading a Gaussian input file with: # - Cartesian coordinates for the atom positions. # - ASE formatted method and basis # - PBCs # - All nuclei properties set # - Masses defined using nuclei properties atoms, params = cartesian_setup params['nmagmlist'] = np.array([None, -8.89, None]) params['zefflist'] = np.array([None, -1, None]) params['znuclist'] = np.array([None, None, 2]) params['qmomlist'] = np.array([None, None, 1]) params['radnuclearlist'] = np.array([None, None, 1]) params['spinlist'] = np.array([None, None, 1]) # Expect warning due to fragments not being supported: with pytest.warns(UserWarning): atoms_new = read_gaussian_in(fd_cartesian, True) atoms_new.set_masses(_get_iso_masses(atoms_new)) _check_atom_properties(atoms, atoms_new, params) # Now we have tested reading the input, we can test writing it # and reading it back in. _test_write_gaussian(atoms_new, params)
def test_read_write_gaussian_zmatrix(fd_zmatrix): # Tests reading a Gaussian input file with: # - Z-matrix format for structure definition # - Variables in the Z-matrix # - Masses defined using 'ReadIso' # - Method and basis not formatted by ASE # - Basis file used instead of standard basis set. positions = np.array([ [+0.000, +0.000, +0.000], [+1.310, +0.000, +0.000], [-0.160, +1.300, +0.000], [+1.150, +1.300, +0.000], [-0.394, -0.446, +1.031], [-0.394, -0.446, -1.031], [+1.545, +1.746, -1.031], [+1.545, +1.746, +1.031], ]) masses = [None] * 8 masses[1] = 0.1134289259 atoms = Atoms('BH2BH4', positions=positions, masses=masses) params = { 'chk': 'example.chk', 'nprocshared': '16', 'output_type': 't', 'b3lyp': None, 'gen': None, 'opt': 'tight, maxcyc=100', 'freq': None, 'integral': 'ultrafine', 'charge': 0, 'mult': 1, 'temperature': '300', 'pressure': '1.0', 'basisfile': '@basis-set-filename.gbs' } params['isolist'] = np.array(masses) # Note that although the freq is set to ReadIso in the input text, # here we have set it to None. This is because when reading in a file # this option does not get saved to the calculator, it instead saves # the temperature, pressure, scale and masses separately. # Test reading the gaussian input atoms_new = read_gaussian_in(fd_zmatrix, True) atoms_new.set_masses(_get_iso_masses(atoms_new)) _check_atom_properties(atoms, atoms_new, params) # Now we have tested reading the input, we can test writing it # and reading it back in. # ASE does not support reading in output files in 'terse' format, so # it does not support writing input files with this setting. Therefore, # the output type automatically set to P in the case that T is chosen params['output_type'] = 'p' _test_write_gaussian(atoms_new, params)
def _test_write_gaussian(atoms, params_expected, properties=None): '''Writes atoms to gaussian input file, reads this back in and checks that the resulting atoms object is equal to atoms and the calculator has parameters params_expected''' atoms.calc.label = 'gaussian_input_file' out_file = atoms.calc.label + '.com' atoms_expected = atoms.copy() atoms.calc.write_input(atoms, properties=properties) with open(out_file) as fd: atoms_written = read_gaussian_in(fd, True) if _get_iso_masses(atoms_written): atoms_written.set_masses(_get_iso_masses(atoms_written)) _check_atom_properties(atoms_expected, atoms_written, params_expected)
def test_read_gaussian_in_command(fd_command_set): # Expect error if 'command' is set in link0 section as this # would try to set the command for the calculator: with pytest.raises(TypeError): read_gaussian_in(fd_command_set, True)
def test_read_gaussian_in_errors(fd_command_set, unsupported_file): with pytest.raises(ParseError): read_gaussian_in(unsupported_file, True)