def test__from_data(): """ test vmat.from_data """ vma1 = vmat.from_data( syms=vmat.symbols(CH4O_VMA), key_mat=vmat.key_matrix(CH4O_VMA), name_mat=vmat.name_matrix(CH4O_VMA), ) assert vma1 == CH4O_VMA vma2 = vmat.from_data( syms=vmat.symbols(CH4O_VMA_NO_NONES), key_mat=vmat.key_matrix(CH4O_VMA_NO_NONES), name_mat=vmat.name_matrix(CH4O_VMA_NO_NONES), ) assert vma2 == CH4O_VMA vma1 = list(map(list, vma1)) vma2 = list(map(list, vma2)) assert vmat.is_valid(vma1) assert vmat.is_valid(vma2) vma1[0] += [None] vma2[0][1] = vma2[0][1] + (None,) assert not vmat.is_valid(vma1) assert not vmat.is_valid(vma2)
def dihedral_angle_coordinate_name(zma, key1, key2, key3, key4): """ get the name of dihedral coordinate for a set of 4 atoms :param zma: the z-matrix :type zma: automol Z-Matrix data structure :param key1: the first key :type key1: int :param key2: the second key :type key2: int :param key3: the third key :type key3: int :param key4: the fourth key :type key4: int :rtype: str """ if key1 > key4: key1, key2, key3, key4 = key4, key3, key2, key1 name_mat = name_matrix(zma) key_mat = key_matrix(zma) assert ( key_mat[key4][0] == key3 and key_mat[key4][1] == key2 and key_mat[key4][2] == key1 ), ("{:d}-{:d}-{:d}-{:d} is not a dihedral coordinate in this zmatrix:\n{}" .format(key1, key2, key3, key4, string(zma, one_indexed=False))) name = name_mat[key4][2] return name
def from_geometry(vma, geo): """ Build a Z-Matrix from a V-Matrix and a molecular geometry. :param vma: V-Matrix :type vma: automol V-Matrix data structure :param geo: molecular geometry :type geo: automol molecular geometry data structure :rtype: automol Z-Matrix data structure """ assert symbols(vma) == symbols(geo) symbs = symbols(vma) key_mat = key_matrix(vma) name_mat = name_matrix(vma) val_mat = numpy.empty(numpy.shape(key_mat), dtype=numpy.object_) for row, key_row in enumerate(key_mat): if row > 0: val_mat[row, 0] = automol.geom.base.distance(geo, row, *key_row[:1]) if row > 1: val_mat[row, 1] = automol.geom.base.central_angle( geo, row, *key_row[:2]) if row > 2: val_mat[row, 2] = automol.geom.base.dihedral_angle( geo, row, *key_row[:3]) zma = from_data(symbs, key_mat, val_mat, name_mat) return zma
def geometry_with_dummy_atoms(zma): """ Convert a Z-Matrix to a molecular geometry that includes dummy atoms. :param zma: Z-Matrix :type zma: automol Z-Matrix data structure :rtype: automol molecular geometry data structure """ syms = symbols(zma) natms = len(syms) key_mat = key_matrix(zma) val_mat = value_matrix(zma) xyzs = numpy.zeros((natms, 3)) for key in range(1, natms): vals = val_mat[key][:min(key, 3)] keys = key_mat[key][:min(key, 3)] ref_xyzs = xyzs[list(keys)] xyz = util.vec.from_internals(*itertools.chain(*zip(vals, ref_xyzs))) xyzs[key] = xyz geo = create.geom.from_data(syms, xyzs) return geo
def remove_atom(zma, key): """ Remove an atom from a Z-Matrix. Error raised if attempting to remove atom other atoms depend on. :param zma: Z-Matrix :type zma: automol Z-Matrix data structure :param key: key of atom to remove :type key: str :rtype: automol Z-Matrix data structure """ symbs = list(symbols(zma)) symbs.pop(key) key_mat = list(key_matrix(zma)) key_mat.pop(key) key_mat = numpy.array(key_mat, dtype=numpy.object_) if (key_mat == key).any(): raise ValueError( "Other atoms in z-matrix depend on atom {}".format(key)) key_map = numpy.vectorize(lambda x: x if (x is None or x < key) else x - 1) key_mat = key_map(key_mat) val_mat = list(value_matrix(zma)) val_mat.pop(key) name_mat = list(name_matrix(zma)) name_mat.pop(key) zma = from_data(symbs, key_mat, val_mat, name_mat) return zma
def test__from_data(): """ test vmat.from_data """ vma = vmat.from_data( symbs=vmat.symbols(CH4O_VMA), key_mat=vmat.key_matrix(CH4O_VMA), name_mat=vmat.name_matrix(CH4O_VMA), ) assert vma == CH4O_VMA
def vmatrix(zma): """ Parse and return the V-Matrix component of a Z-Matrix. :param zma: Z-Matrix :type zma: automol Z-Matrix data structure """ return vmat.from_data(symbs=symbols(zma), key_mat=key_matrix(zma), name_mat=name_matrix(zma))
def key_matrix(zma, shift=0): """ Obtain the key matrix of the Z-Matrix that contains the coordinate atom keys by row and column. :param zma: Z-Matrix :type zma: automol Z-Matrix data structure :param shift: value to shift the keys by when obtaining the key matrix :type shift: int :rtype: tuple(tuple(int)) """ return vmat.key_matrix(zma, shift=shift)
def add_atom(zma, sym, key_row, val_row, name_row=None, one_indexed=False, angstrom=True, degree=True): """ Add an atom to a Z-Matrix. :param zma: Z-Matrix :type zma: automol Z-Matrix data structure :param symb: symbol of atom to add :type symb: str :param key_row: row of keys to define new atom added to key matrix :type key_row: tuple(int) :param val_row: row of values to define new atom added to name matrix :type val_row: tuple(float) :param name_row: row of names to define new atom added to name matrix :type name_row: tuple(str) :param one_indexed: parameter to store keys in one-indexing :type one_indexed: bool :param angstrom: parameter to control Bohr->Angstrom conversion :type angstrom: bool :param degree: parameter to control radian->degree conversion :type degree: bool :rtype: automol Z-Matrix data structure """ symbs = symbols(zma) symbs += (sym, ) key_mat = key_matrix(zma, shift=(1 if one_indexed else 0)) key_mat += (key_row, ) val_mat = value_matrix(zma, angstrom=angstrom, degree=degree) val_mat += (val_row, ) name_mat = None if name_row is None else name_matrix(zma) + (name_row, ) zma = from_data(symbs, key_mat, val_mat, name_mat, one_indexed=one_indexed, angstrom=angstrom, degree=degree) return zma
def set_value_matrix(zma, val_mat): """ Re-set the name matrix of a Z-Matrix using the input value matrix. :param zma: Z-Matrix :type zma: automol Z-Matrix data structure :param val_mat: matrix of Z-Matrix coordinate values :type val_mat: tuple(tuple(int)) :rtype: automol Z-Matrix data structure """ symbs = symbols(zma) key_mat = key_matrix(zma) name_mat = name_matrix(zma) zma = from_data(symbs, key_mat, val_mat, name_mat) return zma
def set_name_matrix(zma, name_mat): """ Re-set the name matrix of a Z-Matrix using the input name matrix. :param zma: Z-Matrix :type zma: automol Z-Matrix data structure :param name_mat: matrix of Z-Matrix coordinate names :type name_mat: tuple(tuple(int)) :rtype: automol Z-Matrix data structure """ syms = symbols(zma) val_mat = value_matrix(zma) key_mat = key_matrix(zma) zma = create.zmat.from_data(syms, key_mat, val_mat, name_mat) return zma
def rename(zma, name_dct): """ Rename a subset of the coordinates of a Z-Matrix. :param zma: Z-Matrix :type zma: automol Z-Matrix data structure :param name_dct: mapping from old coordinate names to new ones :type name_dct: dict[str: str] :rtype: automol Z-Matrix data strucutre """ symbs = symbols(zma) key_mat = key_matrix(zma) val_mat = value_matrix(zma) vma = vmat.rename(zma, name_dct) name_mat = vmat.name_matrix(vma) zma = from_data(symbs, key_mat, val_mat, name_mat) return zma
def dummy_neighbor_keys(zma): """ Obtain keys to dummy atoms in the Z-Matrix, along with their neighboring atoms. :param zma: Z-Matrix :type zma: automol Z-Matrix data structure :returns: a dictionary with the dummy atoms as keys and their neighbors as values :rtype: dict[int: int] """ key_mat = key_matrix(zma) dum_keys = dummy_keys(zma) key_dct = {} for dum_key in dum_keys: ngb_key = key_mat[dum_key][0] if ngb_key is None: ngb_key = next(row for row, (k, _, _) in enumerate(key_mat) if k == dum_key) key_dct[dum_key] = ngb_key return key_dct
def distance_coordinate_name(zma, key1, key2): """ get the name of a distance coordinate for a given bond :param zma: the z-matrix :type zma: automol Z-Matrix data structure :param key1: the first key in the torsion axis (rotational bond) :type key1: int :param key2: the second key in the torsion axis (rotational bond) :type key2: int :rtype: str """ key1, key2 = sorted([key1, key2]) name_mat = name_matrix(zma) key_mat = key_matrix(zma) assert key_mat[key2][0] == key1, ( "{:d}-{:d} is not a distance coordinate in this zmatrix:\n{}".format( key1, key2, string(zma, one_indexed=False))) name = name_mat[key2][0] return name
def central_angle_coordinate_name(zma, key1, key2, key3): """ get the name of angle coordinate for a set of 3 atoms :param zma: the z-matrix :type zma: automol Z-Matrix data structure :param key1: the first key :type key1: int :param key2: the second key (central atom) :type key2: int :param key3: the third key :type key3: int :rtype: str """ key1, key3 = sorted([key1, key3]) name_mat = name_matrix(zma) key_mat = key_matrix(zma) assert key_mat[key3][0] == key2 and key_mat[key3][1] == key1, ( "{:d}-{:d}-{:d} is not a distance coordinate in this zmatrix:\n{}". format(key1, key2, key3, string(zma, one_indexed=False))) name = name_mat[key3][1] return name
def string(zma, one_indexed=True, angstrom=True, degree=True): """ Write a Z-Matrix object to a string. :param zma: Z-Matrix :type zma: automol Z-Matrix data structure :param one_indexed: parameter to store keys in one-indexing :type one_indexed: bool :param angstrom: parameter to control Bohr->Angstrom conversion :type angstrom: bool :param degree: parameter to control radian->degree conversion :type degree: bool :rtype: str """ shift = 1 if one_indexed else 0 zma_str = aw.zmat.write(symbs=symbols(zma), key_mat=key_matrix(zma, shift=shift), name_mat=name_matrix(zma), val_dct=value_dictionary(zma, angstrom=angstrom, degree=degree)) return zma_str
def key_matrix(zma, shift=0): """ coordinate atom keys, by z-matrix row and column """ return vmat.key_matrix(zma, shift=shift)