def GetGdirAngle(coords1, coords2, coords3, r_21=None, r_23=None): """Calculate direction of energy gradients between bond angle atoms. Args: coords1 (float*): 3 cartesian coordinates [Angstrom] of atom1. coords2 (float*): 3 cartesian coordinates [Angstrom] of atom2. coords3 (float*): 3 cartesian coordinates [Angstrom] of atom3. r_21 (float): Distance between atom2 and atom1 (default None). r_23 (float): Distance between atom2 and atom3 (default None). Returns: gdir1 (float*), gdir2 (float*), gdir3 (float*): vectors in the direction of max increasing bond angle. """ if r_21 is not None: r_21 = geomcalc.GetRij(coords2, coords1) if r_23 is not None: r_23 = geomcalc.GetRij(coords2, coords3) u_21 = geomcalc.GetUij(coords2, coords1, r_21) u_23 = geomcalc.GetUij(coords2, coords3, r_23) cp = geomcalc.GetUcp(u_21, u_23) gdir1 = geomcalc.GetUcp(u_21, cp) / r_21 gdir3 = geomcalc.GetUcp(cp, u_23) / r_23 gdir2 = -1.0 * (gdir1 + gdir3) return gdir1, gdir2, gdir3
def GetGdirOutofplane(coords1, coords2, coords3, coords4, oop, r_31=None, r_32=None, r_34=None): """Calculate direction of energy gradients between outofplane atoms. Args: coords1 (float*): 3 cartesian coordinates [Angstrom] of atom1. coords2 (float*): 3 cartesian coordinates [Angstrom] of atom2. coords3 (float*): 3 cartesian coordinates [Angstrom] of atom3. coords4 (float*): 3 cartesian coordinates [Angstrom] of atom4. oop (float): Out-of-plane angles bewteen atoms 1, 2, 3, and 4. r_31 (float): Distance between atom3 and atom1 (default None). r_32 (float): Distance between atom3 and atom2 (default None). r_34 (float): Distance between atom3 and atom4 (default None). Returns: gdir1 (float*), gdir2 (float*), gdir3 (float*), gdir4 (float*): Vectors in the direction of max increasing outofplane angle. """ if r_31 is not None: r_31 = geomcalc.GetRij(coords3, coords1) if r_32 is not None: r_32 = geomcalc.GetRij(coords3, coords2) if r_34 is not None: r_34 = geomcalc.GetRij(coords3, coords4) u_31 = geomcalc.GetUij(coords3, coords1, r_31) u_32 = geomcalc.GetUij(coords3, coords2, r_32) u_34 = geomcalc.GetUij(coords3, coords4, r_34) cp_3234 = geomcalc.GetCp(u_32, u_34) cp_3431 = geomcalc.GetCp(u_34, u_31) cp_3132 = geomcalc.GetCp(u_31, u_32) a_132 = geomcalc.GetAijk(coords1, coords3, coords2) s_132 = math.sin(const.DEG2RAD * a_132) c_132 = math.cos(const.DEG2RAD * a_132) c_oop = math.cos(const.DEG2RAD * oop) t_oop = math.tan(const.DEG2RAD * oop) gdir1 = ((1.0 / r_31) * (cp_3234 / (c_oop * s_132) - (t_oop / s_132**2) * (u_31 - c_132 * u_32))) gdir2 = ((1.0 / r_32) * (cp_3431 / (c_oop * s_132) - (t_oop / s_132**2) * (u_32 - c_132 * u_31))) gdir4 = ((1.0 / r_34) * (cp_3132 / (c_oop * s_132) - (t_oop * u_34))) gdir3 = -1.0 * (gdir1 + gdir2 + gdir4) return gdir1, gdir2, gdir3, gdir4
def GetEBoundI(k_box, bound, coords, origin, boundtype): """Calculate simulation boundary energy of an atom. Args: k_box (float): Spring constant [kcal/(mol*A^2)] of boundary. bound (float): Distance from origin [Angstrom] of boundary. coords (float*): Array of cartesian coordinates [Angstrom] of atom. origin (float*): Array of cartesian coordiantes [Angstrom] of origin of simulation. boundtype (str): 'cube' or 'sphere', type of boundary condition. Returns: e_bound_i (float): Boundary energy [kcal/mol] of atom. """ e_bound_i = 0.0 if (boundtype == 'cube'): for j in range(const.NUMDIM): scale = 1.0 if (abs(coords[j] - origin[j]) >= bound) else 0.0 e_bound_i += (scale * k_box * (abs(coords[j] - origin[j]) - bound)**2) elif (boundtype == 'sphere'): r_io = geomcalc.GetRij(origin, coords) u_io = geomcalc.GetUij(origin, coords) scale = 1.0 if (r_io >= bound) else 0.0 e_bound_i += scale * k_box * (r_io - bound)**2 return e_bound_i
def GetGNonbonded(g_vdw, g_elst, atoms, nonints, dielectric): """Calculate non-bonded energy gradients between all nonbonded atom pairs. Computes van der waals and electrostatic energy gradient [kcal/(mol*A)] components between all pairs of non-bonded atoms in a system. Args: g_vdw (float**): Nx3 array of molecule's van der waals gradients. g_elst (float**): Nx3 array of molecule's electrostatic gradients. atoms (mmlib.molecule.Atom*): Array of molecule's Atom objects. nonints (set(int, int)): Set of atomic index pairs of atoms without nonbonded interactions due to covalent (near-)ajecency. dielectric (float): Dielectric constant of molecule. """ g_vdw.fill(0.0) g_elst.fill(0.0) for i, j in itertools.combinations(range(len(atoms)), 2): if (i, j) in nonints: continue atom1, atom2 = atoms[i], atoms[j] r_ij = geomcalc.GetRij(atom1.coords, atom2.coords) dir1, dir2 = GetGDirInter(atom1.coords, atom2.coords, r_ij) eps_ij = atom1.sreps * atom2.sreps ro_ij = atom1.ro + atom2.ro g_elst_mag = GetGMagElstIJ(r_ij, atom1.charge, atom2.charge, dielectric) g_vdw_mag = GetGMagVdwIJ(r_ij, eps_ij, ro_ij) g_vdw[i] += g_vdw_mag * dir1 g_vdw[j] += g_vdw_mag * dir2 g_elst[i] += g_elst_mag * dir1 g_elst[j] += g_elst_mag * dir2
def GetGNonbonded(mol): """Calculate vdw and elst energy gradients for all nonbonded atom pairs. Args: mol (mmlib.molecule.Molecule): Molecule object with associated Atom objects with geometry and parameter data. """ mol.g_nonbonded.fill(0.0) mol.g_vdw.fill(0.0) mol.g_elst.fill(0.0) for i in range(mol.n_atoms): at1 = mol.atoms[i] for j in range(i + 1, mol.n_atoms): if not j in mol.nonints[i]: at2 = mol.atoms[j] r_ij = geomcalc.GetRij(at1.coords, at2.coords) dir1, dir2 = GetGdirInter(at1.coords, at2.coords, r_ij) eps_ij = at1.sreps * at2.sreps ro_ij = at1.ro + at2.ro g_elst = GetGElstIJ(r_ij, at1.charge, at2.charge, mol.dielectric) g_vdw = GetGVdwIJ(r_ij, eps_ij, ro_ij) mol.g_vdw[i] += g_vdw * dir1 mol.g_vdw[j] += g_vdw * dir2 mol.g_elst[i] += g_elst * dir1 mol.g_elst[j] += g_elst * dir2
def GetGBoundI(k_box, bound, coord, origin, boundtype): """Calculate energy gradient magnitude of boundary energy. Args: k_box (float): Spring constant [kcal/(mol*A^2)] of boundary. bound (float): Distance from origin [Angstrom] of boundary. coords (float*): Array of cartesian coordinates [Angstrom] of atom. origin (float*): Array of cartesian coordiantes [Angstrom] of origin of simulation. boundtype (str): 'cube' or 'sphere', type of boundary condition. Returns: g_bound_i (float): Magnitude of energy gradient [kcal/(mol*A)]. """ g_bound_i = numpy.zeros(const.NUMDIM) if boundtype == 'cube': for j in range(const.NUMDIM): sign = 1.0 if (coord[j] - origin[j]) <= 0.0 else -1.0 scale = 1.0 if abs(coord[j] - origin[j]) >= bound else 0.0 g_bound_i[j] = (-2.0 * sign * scale * k_box * (abs(coord[j]) - bound)) elif boundtype == 'sphere': r_io = geomcalc.GetRij(origin, coord) u_io = geomcalc.GetUij(origin, coord) scale = 1.0 if r_io >= bound else 0.0 g_bound_i = 2.0 * scale * k_box * (r_io - bound) * u_io return g_bound_i
def GetENonbonded(atoms, nonints, dielectric): """Calculate non-bonded interaction energy between all atoms. Computes van der waals and electrostatic energy [kcal/mol] components between all pairs of non-bonded atoms in a system. Args: atoms (mmlib.molecule.Atom*): Array of Atom objects containing Cartesian coordinates and molecular mechanics parameters. nonints (set(int, int)): Set of atomic index pairs of atoms without nonbonded interactions due to covalent (near-)adjacency. dielectric (float): Dielectric constant of molecule. Returns: e_vdw (float): Van der waals energy [kcal/mol] of molecule. e_elst (float): Electrostatic energy [kcal/mol] of molecule. """ e_vdw, e_elst = 0.0, 0.0 for i, j in itertools.combinations(range(len(atoms)), 2): if (i, j) in nonints: continue at1, at2 = atoms[i], atoms[j] r_ij = geomcalc.GetRij(at1.coords, at2.coords) eps_ij = at1.sreps * at2.sreps ro_ij = at1.ro + at2.ro e_elst += GetEElstIJ(r_ij, at1.charge, at2.charge, dielectric) e_vdw += GetEVdwIJ(r_ij, eps_ij, ro_ij) return e_vdw, e_elst
def GetGdirTorsion(coords1, coords2, coords3, coords4, r_12=None, r_23=None, r_34=None): """Calculate direction of energy gradients between torsion atoms. Args: coords1 (float*): 3 cartesian coordinates [Angstrom] of atom1. coords2 (float*): 3 cartesian coordinates [Angstrom] of atom2. coords3 (float*): 3 cartesian coordinates [Angstrom] of atom3. coords4 (float*): 3 cartesian coordinates [Angstrom] of atom4. r_12 (float): Distance between atom1 and atom2 (default None). r_23 (float): Distance between atom2 and atom3 (default None). r_34 (float): Distance between atom3 and atom4 (default None). Returns: gdir1 (float*), gdir2 (float*), gdir3 (float*), gdir4 (float*): Vectors in the direction of max increasing torsion angle. """ if r_12 is not None: r_12 = geomcalc.GetRij(coords1, coords2) if r_23 is not None: r_23 = geomcalc.GetRij(coords2, coords3) if r_34 is not None: r_34 = geomcalc.GetRij(coords3, coords4) u_21 = geomcalc.GetUij(coords2, coords1, r_12) u_34 = geomcalc.GetUij(coords3, coords4, r_34) u_23 = geomcalc.GetUij(coords2, coords3, r_23) u_32 = -1.0 * u_23 a_123 = geomcalc.GetAijk(coords1, coords2, coords3, r_12, r_23) a_432 = geomcalc.GetAijk(coords4, coords3, coords2, r_34, r_23) s_123 = math.sin(const.DEG2RAD * a_123) s_432 = math.sin(const.DEG2RAD * a_432) c_123 = math.cos(const.DEG2RAD * a_123) c_432 = math.cos(const.DEG2RAD * a_432) gdir1 = geomcalc.GetUcp(u_21, u_23) / (r_12 * s_123) gdir4 = geomcalc.GetUcp(u_34, u_32) / (r_34 * s_432) gdir2 = (r_12 / r_23 * c_123 - 1.0) * gdir1 - (r_34 / r_23 * c_432) * gdir4 gdir3 = (r_34 / r_23 * c_432 - 1.0) * gdir4 - (r_12 / r_23 * c_123) * gdir1 return gdir1, gdir2, gdir3, gdir4
def UpdateBonds(bonds): """Update all bond lengths [Angstrom] within a molecule object. Args: bonds (mmlib.molecule.Bond*): Array of molecule's Bond objects. """ for bond in mol.bonds: c1 = mol.atoms[bond.at1].coords c2 = mol.atoms[bond.at2].coords bond.r_ij = geomcalc.GetRij(c1, c2) mol.bond_graph[bond.at1][bond.at2] = bond.r_ij mol.bond_graph[bond.at2][bond.at1] = bond.r_ij
def UpdateBonds(mol): """Update all bond lengths [Angstrom] within a molecule object. Args: mol (mmlib.molecule.Molecule): Molecule object with bond data. """ for p in range(mol.n_bonds): b = mol.bonds[p] c1 = mol.atoms[b.at1].coords c2 = mol.atoms[b.at2].coords b.r_ij = geomcalc.GetRij(c1, c2) mol.bond_graph[b.at1][b.at2] = b.r_ij mol.bond_graph[b.at2][b.at1] = b.r_ij
def UpdateBonds(bonds, atoms, bond_graph): """Update all bond lengths [Angstrom] within a molecule object. Args: bonds (mmlib.molecule.Bond*): Array of molecule's Bond objects. atoms (mmlib.molecule.Atom*): Array of molecule's Atom objects. bond_graph (int:(int:float)): Dictionary of bond connectivity. """ for bond in bonds: c1 = atoms[bond.at1].coords c2 = atoms[bond.at2].coords bond.r_ij = geomcalc.GetRij(c1, c2) bond_graph[bond.at1][bond.at2] = bond.r_ij bond_graph[bond.at2][bond.at1] = bond.r_ij
def GetBondFromPrm(record, atoms): """Parses bond record into a Bond object. Args: record (str*): Array of strings from line of prm file. atoms (mmlib.molecule.Atom*): Array of molecule's Atom objects. Returns: bond (mmlib.molecule.Bond): Bond object with attributes from record. """ at1, at2 = (x - 1 for x in map(int, record[1:3])) k_b, r_eq = tuple(map(float, record[3:5])) c1, c2 = (atoms[i].coords for i in (at1, at2)) r_ij = geomcalc.GetRij(c1, c2) return molecule.Bond(at1, at2, r_ij, r_eq, k_b)
def _GetBond(mol, record): """Parse bond record into a bond object and append to molecule. Appends mmlib.molecule.Bond object to mmlib.molecule.Molecule object. Contents of bond object include (int) 2 atomic indices, (float) spring constant [kcal/(mol*A^2)], (float) equilibrium bond length [Angstrom], (float) bond length [Angstrom]. Args: mol (mmlib.molecule.Molecule): Molecule to append bond. record (str*): Array of strings from line of prm file. """ at1, at2 = [x-1 for x in list(map(int, record[1:3]))] k_b, r_eq = list(map(float, record[3:5])) c1, c2 = [mol.atoms[i].coords for i in [at1, at2]] r_ij = geomcalc.GetRij(c1, c2) bond = molecule.Bond(at1, at2, r_ij, r_eq, k_b) mol.bonds.append(bond) mol.bond_graph[at1][at2] = r_ij mol.bond_graph[at2][at1] = r_ij
def GetBonds(records, atoms): """Parses bond records into an array of Bond objects. Args: records (str**): 2d array of strings from lines of prm file. atoms (mmlib.molecule.Atom*): Array of molecule's Atom objects. Returns: bonds (mmlib.molecule.Bond*): Array of Bond objects with parameters from records. """ bonds = [] for record in records: if not record[0].upper() == 'BOND': continue at1, at2 = [x - 1 for x in list(map(int, record[1:3]))] k_b, r_eq = list(map(float, record[3:5])) c1, c2 = [atoms[i].coords for i in [at1, at2]] r_ij = geomcalc.GetRij(c1, c2) bonds.append(molecule.Bond(at1, at2, r_ij, r_eq, k_b)) return bonds
def GetENonbonded(mol): """Calculate non-bonded interaction energy between all atoms. Computes van der waals and electrostatic energy [kcal/mol] components between all pairs of non-bonded atoms in a system. Args: mol (mmlib.molecule.Molecule): Molecule object with Atom objects containing cartesian coordinates and molecular mechanics parameters """ mol.e_nonbonded, mol.e_vdw, mol.e_elst = 0.0, 0.0, 0.0 for i in range(mol.n_atoms): at1 = mol.atoms[i] for j in range(i + 1, mol.n_atoms): if not j in mol.nonints[i]: at2 = mol.atoms[j] r_ij = geomcalc.GetRij(at1.coords, at2.coords) eps_ij = at1.sreps * at2.sreps ro_ij = at1.ro + at2.ro mol.e_elst += GetEElstIJ(r_ij, at1.charge, at2.charge, mol.dielectric) mol.e_vdw += GetEVdwIJ(r_ij, eps_ij, ro_ij)
def testUnitLength(self): """Asserts unit length for points one distance unit apart.""" params = ORIGIN, NEGATIVE_UNIT_X self.assertAlmostEqual(geomcalc.GetRij(*params), 1.0)
def testSamePoint(self): """Asserts zero distance between identical points.""" params = ARBITRARY_XYZ1, ARBITRARY_XYZ1 self.assertAlmostEqual(geomcalc.GetRij(*params), 0.0)
def testOnAxisX(self): """Asserts correct value for points separated along x-axis.""" params = POSITIVE_ARBITRARY_X, NEGATIVE_ARBITRARY_X self.assertAlmostEqual(geomcalc.GetRij(*params), 12.3099410)
def testOnAxisY(self): """Asserts correct value for points separated along y-axis.""" params = NEGATIVE_ARBITRARY_Y, POSITIVE_ARBITRARY_Y self.assertAlmostEqual(geomcalc.GetRij(*params), 12.2413450)
def testReflexive(self): """Asserts same value for inverted order of inputs.""" params = ARBITRARY_XYZ2, ARBITRARY_XYZ1 self.assertAlmostEqual(geomcalc.GetRij(*params), 12.1693138)
def testArbitrary(self): """Asserts correct distance between arbitrary points in 3d space.""" params = ARBITRARY_XYZ1, ARBITRARY_XYZ2 self.assertAlmostEqual(geomcalc.GetRij(*params), 12.1693138)
def testOnAxisZ(self): """Asserts correct value for points separated along z-axis.""" params = POSITIVE_ARBITRARY_Z, NEGATIVE_ARBITRARY_Z self.assertAlmostEqual(geomcalc.GetRij(*params), 12.6247660)