public static Nonbonded FromString(string line, ITextLogger logger) { Element elem = Element.FromString(line, 1, 6); //Debug.Assert(0 == elem.parms[0]); // "FE 0.010000 0.000000 0.650000" happens if (!(0 == elem.parms[0])) { logger.Log(string.Format("Warning: Nonbonded params ({0}): 1st parameter should be in general == 0", line)); } double epsilon = elem.parms[1]; double Rmin2 = elem.parms[2]; //Debug.Assert(Rmin2 > 0); if (!(Rmin2 > 0)) { logger.Log(string.Format("Error : Nonbonded params ({0}): 3rd parameter (Rmin2) must be '> 0'", line)); } double eps_14 = double.NaN; double Rmin2_14 = double.NaN; if (elem.parms.Length > 3) { HDebug.Assert(0 == elem.parms[3]); eps_14 = elem.parms[4]; Rmin2_14 = elem.parms[5]; //Debug.Assert(Rmin2_14 > 0); if (!(Rmin2_14 > 0)) { logger.Log(string.Format("Error : Nonbonded params ({0}): 6th parameter (Rmin2_14) must be '> 0'", line)); } } Nonbonded nonbonded = new Nonbonded(line, elem.types, epsilon, Rmin2, eps_14, Rmin2_14); return(nonbonded); }
public void RemoveHydrogens(ITextLogger logger) { foreach (Atom atom in new List <Atom>(atoms.ToArray())) { if (atom.IsHydrogen() == false) { continue; } if (atom.Bonds.Count != 1) { logger.Log("warning in removing hydrogen: {" + atom + "} has '!=1' number of bonded atom(s)"); } // isolate the atom, and remove it Atom bonded; { List <Atom> bondeds = new List <Atom>(atom.Bonds[0].atoms); bondeds.Remove(atom); HDebug.Assert(bondeds.Count == 1); bonded = bondeds[0]; } atom.Isolate(this); // assign its partial charge and mass to its bonded heavy atom bonded.Charge += atom.Charge; bonded.Mass += atom.Mass; // atoms.Remove(atom); } }
public Nonbonded FindNonbonded(string type0, ITextLogger logger) { if (_FindNonbonded.ContainsKey(type0) == false) { Nonbonded found = null; foreach (Nonbonded nonbonded in nonbondeds) { if (nonbonded.types[0] == type0) { if (found != null) { bool writelog = ((found.epsilon != nonbonded.epsilon) || (found.Rmin2 != nonbonded.Rmin2) || (found.eps_14 != nonbonded.eps_14) || (found.Rmin2_14 != nonbonded.Rmin2_14)); if (writelog) { logger.Log(string.Format("Nonbonded params of {0}-(eps {1}, rmin2 {2}, esp_14 {3}, rmin2_14 {4}) is replaced to ({5}, {6}, {7}, {8})", type0, found.epsilon, found.Rmin2, found.eps_14, found.Rmin2_14, nonbonded.epsilon, nonbonded.Rmin2, nonbonded.eps_14, nonbonded.Rmin2_14)); } } found = nonbonded; } } HDebug.Assert(found != null); HDebug.AssertIf(double.IsNaN(found.Rmin2) == false, found.Rmin2 > 0); HDebug.AssertIf(double.IsNaN(found.Rmin2_14) == false, found.Rmin2_14 > 0); _FindNonbonded.Add(type0, found); } return(_FindNonbonded[type0]); }
public static Nonbonded FromStringXPlor(string line, ITextLogger logger) { Element elem = Element.FromString(line, 1, 4); double epsilon = elem.parms[0]; double Rmin2 = elem.parms[1]; //Debug.Assert(Rmin2 > 0); if (!(Rmin2 > 0)) { logger.Log(string.Format("Error : Nonbonded params ({0}): 3rd parameter (Rmin2) must be '> 0'", line)); } double eps_14 = elem.parms[2]; double Rmin2_14 = elem.parms[3]; //Debug.Assert(Rmin2_14 > 0); if (!(Rmin2_14 > 0)) { logger.Log(string.Format("Error : Nonbonded params ({0}): 4th parameter (Rmin2_14) must be '> 0'", line)); } Nonbonded nonbonded = new Nonbonded(line, elem.types, epsilon, Rmin2, eps_14, Rmin2_14); return(nonbonded); }
public static Improper FromString(string line, ITextLogger logger) { Element elem = Element.FromString(line, 4, 3); double Kpsi = elem.parms[0]; //Debug.Assert(elem.parms[1] == 0); if (!(elem.parms[1] == 0)) { logger.Log(string.Format("Warning: Improper params ({0}): first parameter should be '== 0' in general", line)); } double psi0 = elem.parms[2] * Math.PI / 180.0; Improper improper = new Improper(line, elem.types, Kpsi, psi0); return(improper); }
public Angle FindAngle(string type0, string type1, string type2, ITextLogger logger) { string key = type0 + "-" + type1 + "-" + type2; if (_FindAngle.ContainsKey(key) == false) { Angle found = null; int found_count_X = int.MaxValue; foreach (Angle angle in angles) { int count_X = FindType(angle.types, type0, type1, type2); if (count_X == -1) { continue; } if (count_X < found_count_X) { found = angle; found_count_X = count_X; } else { bool writelog = ((count_X != found_count_X) || (found.Ktheta != angle.Ktheta) || (found.Theta0 != angle.Theta0) || (found.Kub != angle.Kub) || (found.S0 != angle.S0)); if (writelog) { logger.Log(string.Format("Angle params of {0}-{1}-{2}-(Ktheta {3}, Theta0 {4}, Kub {5}, S0 {6}) is replaced to ({7}, {8}, {9}, {10})", type0, type1, type2, found.Ktheta, found.Theta0, found.Kub, found.S0, angle.Ktheta, angle.Theta0, angle.Kub, angle.S0)); } } } HDebug.Assert(found != null); _FindAngle.Add(key, found); } return(_FindAngle[key]); }
public Improper FindImproper(string type0, string type1, string type2, string type3, ITextLogger logger) { string key = type0 + "-" + type1 + "-" + type2 + "-" + type3; if (_FindImproper.ContainsKey(key) == false) { Improper found = null; int found_count_X = int.MaxValue; foreach (Improper improper in impropers) { int count_X = FindType(improper.types, type0, type1, type2, type3); if (count_X == -1) { continue; } if (count_X < found_count_X) { found = improper; found_count_X = count_X; } else { bool writelog = ((count_X != found_count_X) || (found.Kchi != improper.Kchi) || (found.n != improper.n) || (found.delta != improper.delta)); if (writelog) { logger.Log(string.Format("Improper params of {0}-{1}-{2}-{3}-(Kchi {4}, n {5}, delta {6}) is replaced to ({7}, {8}, {9})", type0, type1, type2, type3, found.Kchi, found.n, found.delta, improper.Kchi, improper.n, improper.delta)); } } } HDebug.Assert(found != null); _FindImproper.Add(key, found); } return(_FindImproper[key]); }
public Bond FindBond(string type0, string type1, ITextLogger logger) { string key = type0 + "-" + type1; if (_FindBond.ContainsKey(key) == false) { Bond found = null; int found_count_X = int.MaxValue; foreach (Bond bond in bonds) { int count_X = FindType(bond.types, type0, type1); if (count_X == -1) { continue; } if (count_X < found_count_X) { found = bond; found_count_X = count_X; } else { bool writelog = ((count_X != found_count_X) || (found.Kb != bond.Kb) || (found.b0 != bond.b0)); if (writelog) { logger.Log(string.Format("Bond params of {0}-{1}-(Kb {2}, b0 {3}) is replaced to ({4}, {5})", type0, type1, found.Kb, found.b0, bond.Kb, bond.b0)); } } } HDebug.Assert(found != null); _FindBond.Add(key, found); } return(_FindBond[key]); }
public static Universe Build(Namd.Psf psf, Namd.Prm prm, Pdb pdb, bool?ignore_neg_occupancy, ITextLogger logger) { Universe univ = new Universe(); // atoms Atoms atoms = new Atoms(univ); Dictionary <int, Atom> id_atom = new Dictionary <int, Atom>(); Pdb.Atom[] pdb_atoms = pdb.atoms; HDebug.Assert(psf.atoms.Length == pdb_atoms.Length); for (int i = 0; i < psf.atoms.Length; i++) { HDebug.Assert(pdb_atoms[i].try_serial == null || psf.atoms[i].AtomId == pdb_atoms[i].serial); // when num atoms in pdb is too large (>99999), it is marked as ***** string type0 = psf.atoms[i].AtomType; Atom atom = new Atom(psf.atoms[i], prm.FindNonbonded(type0, logger), pdb_atoms[i]); atom.Coord = pdb_atoms[i].coord; { if (ignore_neg_occupancy == null) { if (pdb_atoms[i].occupancy < 0) { throw new HException("unhandled negative occupancy during building universe"); } } else { if (ignore_neg_occupancy.Value && pdb_atoms[i].occupancy < 0) { continue; } } } atoms.Add(atom); id_atom.Add(psf.atoms[i].AtomId, atom); } // bonds Bonds bonds = new Bonds(); for (int i = 0; i < psf.bonds.GetLength(0); i++) { int id0 = psf.bonds[i, 0]; if (id_atom.ContainsKey(id0) == false) { continue; } Atom atom0 = id_atom[id0]; string type0 = atom0.AtomType; int id1 = psf.bonds[i, 1]; if (id_atom.ContainsKey(id1) == false) { continue; } Atom atom1 = id_atom[id1]; string type1 = atom1.AtomType; Bond bond = new Bond(atom0, atom1, prm.FindBond(type0, type1, logger)); bonds.Add(bond); atom0.Bonds.Add(bond); atom0.Inter123.Add(atom1); atom0.Inter12.Add(atom1); atom1.Bonds.Add(bond); atom1.Inter123.Add(atom0); atom1.Inter12.Add(atom0); } // angles Angles angles = new Angles(); for (int i = 0; i < psf.angles.GetLength(0); i++) { int id0 = psf.angles[i, 0]; if (id_atom.ContainsKey(id0) == false) { continue; } Atom atom0 = id_atom[id0]; string type0 = atom0.AtomType; int id1 = psf.angles[i, 1]; if (id_atom.ContainsKey(id1) == false) { continue; } Atom atom1 = id_atom[id1]; string type1 = atom1.AtomType; int id2 = psf.angles[i, 2]; if (id_atom.ContainsKey(id2) == false) { continue; } Atom atom2 = id_atom[id2]; string type2 = atom2.AtomType; var prm_angle = prm.FindAngle(type0, type1, type2, logger); if (prm_angle == null) { HDebug.Assert(false); logger.Log(string.Format ("try to add non-existing angle (({0}, prm {1})-({2}, prm {3})-({4}, prm {5})) in prm" , atom0, type0, atom1, type1, atom2, type2 )); continue; } Angle angle = new Angle(atom0, atom1, atom2, prm_angle); angles.Add(angle); atom0.Angles.Add(angle); atom0.Inter123.Add(atom1); atom0.Inter123.Add(atom2); atom1.Angles.Add(angle); atom1.Inter123.Add(atom2); atom1.Inter123.Add(atom0); atom2.Angles.Add(angle); atom2.Inter123.Add(atom0); atom2.Inter123.Add(atom1); } // dihedrals Dihedrals dihedrals = new Dihedrals(); for (int i = 0; i < psf.dihedrals.GetLength(0); i++) { int id0 = psf.dihedrals[i, 0]; if (id_atom.ContainsKey(id0) == false) { continue; } Atom atom0 = id_atom[id0]; string type0 = atom0.AtomType; int id1 = psf.dihedrals[i, 1]; if (id_atom.ContainsKey(id1) == false) { continue; } Atom atom1 = id_atom[id1]; string type1 = atom1.AtomType; int id2 = psf.dihedrals[i, 2]; if (id_atom.ContainsKey(id2) == false) { continue; } Atom atom2 = id_atom[id2]; string type2 = atom2.AtomType; int id3 = psf.dihedrals[i, 3]; if (id_atom.ContainsKey(id3) == false) { continue; } Atom atom3 = id_atom[id3]; string type3 = atom3.AtomType; var prm_dihedrals = prm.FindDihedral(type0, type1, type2, type3, logger); if (prm_dihedrals.Length == 0) { HDebug.Assert(false); logger.Log(string.Format ("try to add non-existing dihedral (({0}, prm {1})-({2}, prm {3})-({4}, prm {5})-({6}, prm {7})) in prm" , atom0, type0, atom1, type1, atom2, type2, atom3, type3 )); continue; } foreach (var prm_dihedral in prm_dihedrals) { Dihedral dihedral; if (atom0.ID < atom3.ID) { dihedral = new Dihedral(atom0, atom1, atom2, atom3, prm_dihedral); } else { dihedral = new Dihedral(atom3, atom2, atom1, atom0, prm_dihedral); } dihedrals.Add(dihedral); atom0.Dihedrals.Add(dihedral); //atom0.Inter123.Add(atom1); atom0.Inter123.Add(atom2); atom0.Inter123.Add(atom3); atom1.Dihedrals.Add(dihedral); //atom1.Inter123.Add(atom2); atom1.Inter123.Add(atom3); atom1.Inter123.Add(atom0); atom2.Dihedrals.Add(dihedral); //atom2.Inter123.Add(atom3); atom2.Inter123.Add(atom0); atom2.Inter123.Add(atom1); atom3.Dihedrals.Add(dihedral); //atom3.Inter123.Add(atom0); atom3.Inter123.Add(atom1); atom3.Inter123.Add(atom2); } } // impropers Impropers impropers = new Impropers(); for (int i = 0; i < psf.impropers.GetLength(0); i++) { int id0 = psf.impropers[i, 0]; if (id_atom.ContainsKey(id0) == false) { continue; } Atom atom0 = id_atom[id0]; string type0 = atom0.AtomType; int id1 = psf.impropers[i, 1]; if (id_atom.ContainsKey(id1) == false) { continue; } Atom atom1 = id_atom[id1]; string type1 = atom1.AtomType; int id2 = psf.impropers[i, 2]; if (id_atom.ContainsKey(id2) == false) { continue; } Atom atom2 = id_atom[id2]; string type2 = atom2.AtomType; int id3 = psf.impropers[i, 3]; if (id_atom.ContainsKey(id3) == false) { continue; } Atom atom3 = id_atom[id3]; string type3 = atom3.AtomType; Improper improper = new Improper(atom0, atom1, atom2, atom3, prm.FindImproper(type0, type1, type2, type3, logger)); impropers.Add(improper); atom0.Impropers.Add(improper); //atom0.Inter123.Add(atom1); atom0.Inter123.Add(atom2); atom0.Inter123.Add(atom3); atom1.Impropers.Add(improper); //atom1.Inter123.Add(atom2); atom1.Inter123.Add(atom3); atom1.Inter123.Add(atom0); atom2.Impropers.Add(improper); //atom2.Inter123.Add(atom3); atom2.Inter123.Add(atom0); atom2.Inter123.Add(atom1); atom3.Impropers.Add(improper); //atom3.Inter123.Add(atom0); atom3.Inter123.Add(atom1); atom3.Inter123.Add(atom2); } // 1-4 interactions for (int i = 0; i < atoms.Count; i++) { HashSet <Atom> Inter14 = new HashSet <Atom>(); BuildInter1toN(atoms[i], 4, Inter14); // find all atoms for 1-4 interaction Inter14.Remove(atoms[i]); // remove self foreach (Atom atom in atoms[i].Inter123) { Inter14.Remove(atom); // remove all 1-2, 1-3 interactions } atoms[i].Inter14 = Inter14; } Nonbonded14s nonbonded14s = new Nonbonded14s(); nonbonded14s.Build(atoms); //// nonbondeds //// do not make this list in advance, because it depends on the atom positions //Nonbondeds nonbondeds = new Nonbondeds(); //nonbondeds.Build(atoms); //Universe univ = new Universe(); univ.pdb = pdb; univ.refs.Add("psf", psf); univ.refs.Add("prm", prm); univ.atoms = atoms; univ.bonds = bonds; univ.angles = angles; univ.dihedrals = dihedrals; univ.impropers = impropers; //univ.nonbondeds = nonbondeds ; // do not make this list in advance, because it depends on the atom positions univ.nonbonded14s = nonbonded14s; HDebug.Assert(univ.Verify()); return(univ); }