diff --git a/arkane/common.py b/arkane/common.py index 22ddbe07df..435913edcd 100644 --- a/arkane/common.py +++ b/arkane/common.py @@ -42,7 +42,7 @@ import rmgpy.constants as constants from rmgpy import __version__ -from rmgpy.molecule.element import elementList +from rmgpy.molecule.element import elementList, getElement from rmgpy.molecule.translator import toInChI, toInChIKey from rmgpy.pdep.collision import SingleExponentialDown from rmgpy.quantity import ScalarQuantity, ArrayQuantity @@ -195,26 +195,22 @@ def update_species_attributes(self, species=None): def update_xyz_string(self): """ - Return an xyz string built from self.conformer + Generate an xyz string built from self.conformer, and standardize the result + + Returns: + str: 3D coordinates in an XYZ format. """ + xyz_list = list() if self.conformer is not None and self.conformer.number is not None: - # generate the xyz-format string from the Conformer coordinates - xyz_string = '{0}\n{1}'.format(len(self.conformer.number.value_si), self.label) - for i, coorlist in enumerate(self.conformer.coordinates.value_si): - for element in elementList: - if element.number == int(self.conformer.number.value_si[i]): - element_symbol = element.symbol - break - else: - raise ValueError('Could not find element symbol corresponding to atom number {0}'.format( - self.conformer.number.value_si[i])) - xyz_string += '\n{0} {1} {2} {3}'.format(element_symbol, - coorlist[0], - coorlist[1], - coorlist[2]) - else: - xyz_string = '' - return xyz_string + # generate the xyz-format string from self.conformer.coordinates and self.conformer.number + xyz_list.append(str(len(self.conformer.number.value_si))) + xyz_list.append(self.label) + for number, coordinate in zip(self.conformer.number.value_si, self.conformer.coordinates.value_si): + element_symbol = getElement(int(number)).symbol + row = '{0:4}'.format(element_symbol) + row += '{0:14.8f}{1:14.8f}{2:14.8f}'.format(*(coordinate * 1e10).tolist()) # convert m to Angstrom + xyz_list.append(row) + return '\n'.join(xyz_list) def save_yaml(self, path): """ diff --git a/arkane/commonTest.py b/arkane/commonTest.py index 0d8daf37d5..96313e42f0 100644 --- a/arkane/commonTest.py +++ b/arkane/commonTest.py @@ -366,8 +366,18 @@ def test_create_and_load_yaml(self): self.assertEqual(arkane_spc.level_of_theory, 'cbs-qb3') self.assertIsInstance(arkane_spc.thermo_data, ThermoData) self.assertTrue(arkane_spc.use_hindered_rotors) - self.assertTrue('C 7.54e-14 1.193e-13 5.52e-14' in arkane_spc.xyz) self.assertIsInstance(arkane_spc.chemkin_thermo_string, str) + expected_xyz = """8 +C2H6 +C 0.00075400 0.00119300 0.00055200 +H 0.00074000 0.00117100 1.09413800 +H 1.04376600 0.00117100 -0.32820200 +H -0.44760300 0.94289500 -0.32825300 +C -0.76014200 -1.20389600 -0.55748300 +H -0.76012800 -1.20387400 -1.65106900 +H -0.31178500 -2.14559800 -0.22867800 +H -1.80315400 -1.20387400 -0.22872900""" + self.assertEqual(arkane_spc.xyz, expected_xyz) def test_load_existing_yaml(self): """