def _check_molecule_format(val): """If it seems to be zmatrix rather than xyz format we convert before returning""" atoms = [x.strip() for x in val.split(';')] if atoms is None or len(atoms) < 1: raise QiskitChemistryError('Molecule format error: ' + val) # Anx xyz format has 4 parts in each atom, if not then do zmatrix convert parts = [x.strip() for x in atoms[0].split(' ')] if len(parts) != 4: try: zmat = [] for atom in atoms: parts = [x.strip() for x in atom.split(' ')] z = [parts[0]] for i in range(1, len(parts), 2): z.append(int(parts[i])) z.append(float(parts[i + 1])) zmat.append(z) xyz = z2xyz(zmat) new_val = "" for i in range(len(xyz)): atm = xyz[i] if i > 0: new_val += "; " new_val += "{} {} {} {}".format(atm[0], atm[1], atm[2], atm[3]) return new_val except Exception as exc: raise QiskitChemistryError('Failed to convert atom string: ' + val) from exc return val
def _check_molecule_format(val): """If it seems to be zmatrix rather than xyz format we convert before returning""" atoms = [x.strip() for x in val.split(";")] if atoms is None or len(atoms) < 1: # pylint: disable=len-as-condition raise QiskitNatureError("Molecule format error: " + val) # An xyz format has 4 parts in each atom, if not then do zmatrix convert # Allows dummy atoms, using symbol 'X' in zmatrix format for coord computation to xyz parts = [x.strip() for x in atoms[0].split(" ")] if len(parts) != 4: try: zmat = [] for atom in atoms: parts = [x.strip() for x in atom.split(" ")] z = [parts[0]] for i in range(1, len(parts), 2): z.append(int(parts[i])) z.append(float(parts[i + 1])) zmat.append(z) xyz = z2xyz(zmat) new_val = "" for atm in xyz: if atm[0].upper() == "X": continue if new_val: new_val += "; " new_val += "{} {} {} {}".format(atm[0], atm[1], atm[2], atm[3]) return new_val except Exception as exc: raise QiskitNatureError("Failed to convert atom string: " + val) from exc return val
def test_convert(self): self.assertTrue(cartesians_equal(z2xyz([['H']]), [['H',0.0,0.0,0.0]])) self.assertTrue(cartesians_equal(z2xyz([['H'], ['H', 1, 0.7]]), [['H',0.0,0.0,0.0],['H',0.7,0.0,0.0]])) self.assertTrue(cartesians_equal(z2xyz([['O'],['H',1,1.0],['H',1,1.0,2,90.0]]), [['O',0.0,0.0,0.0],['H',1.0,0.0,0.0],['H',0.0,1.0,0.0]])) self.assertTrue(cartesians_equal(z2xyz([['H'],['O',1,1],['O',2,1,1,90.],['H',3,1,2,90.,1,180.]]), [['H',0,0,0],['O',1,0,0],['O',1,1,0],['H',2,1,0]])) self.assertTrue(cartesians_equal(z2xyz([['H'],['O',1,1],['O',2,1,1,90.],['H',3,1,2,90.,1,0.]]), [['H',0,0,0],['O',1,0,0],['O',1,1,0],['H',0,1,0]])) self.assertTrue(cartesians_equal(z2xyz([['H'],['O',1,1],['O',2,1,1,90.],['H',3,1,2,90.,1,90.]]), [['H',0,0,0],['O',1,0,0],['O',1,1,0],['H',1,1,-1]])) self.assertTrue(cartesians_equal(z2xyz([['H'],['O',1,1],['O',2,1,1,90.],['H',3,1,2,90.,1,-90.]]), [['H',0,0,0],['O',1,0,0],['O',1,1,0],['H',1,1,1]])) return
def _check_molecule_format(val: str) -> str: """If it seems to be zmatrix rather than xyz format we convert before returning""" # pylint: disable=import-error from pyquante2.geo.zmatrix import z2xyz atoms = [x.strip() for x in val.split(";")] if atoms is None or len(atoms) < 1: raise QiskitNatureError("Molecule format error: " + val) # An xyz format has 4 parts in each atom, if not then do zmatrix convert # Allows dummy atoms, using symbol 'X' in zmatrix format for coord computation to xyz parts = [x.strip() for x in atoms[0].split()] if len(parts) != 4: try: zmat = [] for atom in atoms: parts = [x.strip() for x in atom.split()] z: List[Union[str, int, float]] = [parts[0]] for i in range(1, len(parts), 2): z.append(int(parts[i])) z.append(float(parts[i + 1])) zmat.append(z) xyz = z2xyz(zmat) new_val = "" for atm in xyz: if atm[0].upper() == "X": continue if new_val: new_val += "; " new_val += f"{atm[0]} {atm[1]} {atm[2]} {atm[3]}" return new_val except Exception as exc: raise QiskitNatureError("Failed to convert atom string: " + val) from exc return val