public MatrixByArr[,] GetHess4PwIntrAct(List <ForceField.IForceField> frcflds, double frcfactor) { if (GetHess4PwIntrAct_doselftest) { GetHess4PwIntrAct_doselftest = false; HDebug.Assert(GetHess4PwIntrAct_selftest(frcflds, frcfactor)); } Vector[] coords = GetCoords(); Dictionary <Pair <int, int>, ForceField.PwIntrActInfo> pwintractinfos = new Dictionary <Pair <int, int>, ForceField.PwIntrActInfo>(); for (int i = 0; i < size - 1; i++) { for (int j = i + 1; j < size; j++) { pwintractinfos.Add(new Pair <int, int>(i, j), new ForceField.PwIntrActInfo()); } } GetHess4PwIntrActBonds(frcflds, coords, pwintractinfos); GetHess4PwIntrActAngles(frcflds, coords, pwintractinfos); GetHess4PwIntrActImpropers(frcflds, coords, pwintractinfos); GetHess4PwIntrActNonbondeds(frcflds, coords, pwintractinfos); MatrixByArr[,] hessblk = new MatrixByArr[size, size]; Vector[] frcs = new Vector[size]; for (int i = 0; i < size; i++) { hessblk[i, i] = new double[3, 3]; frcs[i] = new double[3]; } foreach (Pair <int, int> key in pwintractinfos.Keys) { int i = key.Item1; int j = key.Item2; ForceField.PwIntrActInfo pwintractinfo = pwintractinfos[key]; double fij = pwintractinfo.Fij * frcfactor; double kij = pwintractinfo.Kij; hessblk[i, j] = ForceField.GetHessianBlock(coords[i], coords[j], kij, fij); hessblk[j, i] = ForceField.GetHessianBlock(coords[i], coords[j], kij, fij); hessblk[i, i] -= hessblk[i, j]; hessblk[j, j] -= hessblk[j, i]; ///////////////////////////////////////// Vector uvec_ij = (coords[j] - coords[i]).UnitVector(); frcs[i] += uvec_ij * fij; frcs[j] += -uvec_ij * fij; } double[] frcmags = new double[size]; for (int i = 0; i < size; i++) { frcmags[i] = frcs[i].Dist; } double frcmag_max = frcmags.Max(); double frcmag_avg = frcmags.Average(); double frcmag_med = frcmags.Median(); return(hessblk); }
public virtual void Compute(Universe.Atom atom1, Universe.Atom atom2, Vector[] coords, ref double energy, ref Vector[] forces, ref MatrixByArr[,] hessian, double[,] pwfrc = null, double[,] pwspr = null) { HDebug.Depreciated("check idx1 and idx2"); int idx1 = 0; // nonbonded.atoms[0].ID; int idx2 = 1; // nonbonded.atoms[1].ID; Vector diff = (coords[idx2] - coords[idx1]); double dx = diff[0]; double dy = diff[1]; double dz = diff[2]; double pchi = atom1.Charge; double pchj = atom2.Charge; double ee = 80; /////////////////////////////////////////////////////////////////////////////// // energy double lenergy = EnergyEs(dx, dy, dz, pchi, pchj, ee); energy += lenergy; /////////////////////////////////////////////////////////////////////////////// // force if (forces != null) { Vector f12 = ForceEs(dx, dy, dz, pchi, pchj, ee); forces[idx1] += f12; forces[idx2] += -f12; } /////////////////////////////////////////////////////////////////////////////// // hessian if (hessian != null) { //string option = "Spring+Force"; //Vector diff01 = (coords[1] - coords[0]); //Vector diff10 = (coords[0] - coords[1]); //hessian[0, 1] += SprngEs(diff01, pchi, pchj, ee, option); //hessian[1, 0] += SprngEs(diff10, pchi, pchj, ee, option); { // !V(Lennard-Jones) = Eps,i,j[(Rmin,i,j/ri,j)**12 - 2(Rmin,i,j/ri,j)**6] // !epsilon: kcal/mole, Eps,i,j = sqrt(eps,i * eps,j) // !Rmin/2: A, Rmin,i,j = Rmin/2,i + Rmin/2,j // // V(rij) = (332 * pchij / ee) * rij^-1 // F(rij) = ( -1) * (332 * pchij / ee) * rij^-2 // K(rij) = (-2*-1) * (332 * pchij / ee) * rij^-3 double pchij = pchi * pchj; double rij2 = dx * dx + dy * dy + dz * dz; double rij = Math.Sqrt(rij2); double rij3 = rij2 * rij; double fij = (-1) * (332 * pchij / ee) / rij2; double kij = (-2 * -1) * (332 * pchij / ee) / rij3; fij *= optHessianForceFactor; //Matrix Hij = ForceField.GetHessianBlock(coords[0], coords[1], kij, fij); hessian[0, 1] += ForceField.GetHessianBlock(coords[0], coords[1], kij, fij); hessian[1, 0] += ForceField.GetHessianBlock(coords[1], coords[0], kij, fij); } } }
public virtual void Compute(Universe.Atom atom1, Universe.Atom atom2, Vector[] coords, ref double energy, ref Vector[] forces, ref MatrixByArr[,] hessian, double[,] pwfrc = null, double[,] pwspr = null) { Vector pos0 = coords[0]; Vector pos1 = coords[1]; double pchi = atom1.Charge; double pchj = atom2.Charge; double ee = 80; double pchij = pchi * pchj; double lenergy, forceij, springij; Compute(coords, out lenergy, out forceij, out springij, pchij, ee); double abs_forceij = Math.Abs(forceij); if (pwfrc != null) { pwfrc[0, 1] = pwfrc[1, 0] = forceij; } if (pwspr != null) { pwspr[0, 1] = pwspr[1, 0] = springij; } /////////////////////////////////////////////////////////////////////////////// // energy energy += lenergy; /////////////////////////////////////////////////////////////////////////////// // force if (forces != null) { Vector frc0, frc1; GetForceVector(pos0, pos1, forceij, out frc0, out frc1); forces[0] += frc0; forces[1] += frc1; } /////////////////////////////////////////////////////////////////////////////// // hessian if (hessian != null) { hessian[0, 1] += ForceField.GetHessianBlock(coords[0], coords[1], springij, forceij); hessian[1, 0] += ForceField.GetHessianBlock(coords[1], coords[0], springij, forceij); } }
public static void GetPotential_SelfTest(string rootpath, string[] args) { if (GetPotential_SelfTest_do == false) { return; } GetPotential_SelfTest_do = false; Namd.Psf psf = Namd.Psf.FromFile(rootpath + @"\Sample\alanin.psf"); Pdb pdb = Pdb.FromFile(rootpath + @"\Sample\alanin.pdb"); Namd.Prm prm = Namd.Prm.FromFileXPlor(rootpath + @"\Sample\alanin.params", new TextLogger()); Universe univ = Universe.Build(psf, prm, pdb, false); List <ForceField.IForceField> frcflds = ForceField.GetMindyForceFields(); Vector[] forces = univ.GetVectorsZero(); MatrixByArr hessian = null; Dictionary <string, object> cache = new Dictionary <string, object>(); double energy = univ.GetPotential(frcflds, ref forces, ref hessian, cache); double toler = 0.000005; HDebug.AssertTolerance(toler, SelfTest_alanin_energy - energy); HDebug.AssertTolerance(toler, SelfTest_alanin_energy_bonds - (double)cache["energy_bonds "]); HDebug.AssertTolerance(toler, SelfTest_alanin_energy_angles - (double)cache["energy_angles "]); HDebug.AssertTolerance(toler, SelfTest_alanin_energy_dihedrals - (double)cache["energy_dihedrals "]); HDebug.AssertTolerance(toler, SelfTest_alanin_energy_impropers - (double)cache["energy_impropers "]); HDebug.AssertTolerance(toler, SelfTest_alanin_energy_nonbondeds - (double)cache["energy_nonbondeds"]); HDebug.AssertTolerance(toler, SelfTest_alanin_energy_unknowns - (double)cache["energy_customs "]); HDebug.AssertTolerance(toler, SelfTest_alanin_forces.GetLength(0) - forces.Length); for (int i = 0; i < forces.Length; i++) { HDebug.Assert(forces[i].Size == 3); HDebug.AssertTolerance(toler, SelfTest_alanin_forces[i, 0] - forces[i][0]); HDebug.AssertTolerance(toler, SelfTest_alanin_forces[i, 1] - forces[i][1]); HDebug.AssertTolerance(toler, SelfTest_alanin_forces[i, 2] - forces[i][2]); } }
public static void SelfTest(string rootpath, string[] args) { //{ // Pdb pdb = Pdb.FromFile(rootpath + @"\Sample\1a6g.pdb"); // HTLib2.Bioinfo.Universe.Build(pdb); //} GetPotential_SelfTest(rootpath, args); Universe univ = null; string sample = null; //sample = "1a6g_autopsf.unfolded"; //sample = "1a6g_autopsf"; switch (sample) { case "alanin": { List <ForceField.IForceField> frcflds = ForceField.GetMindyForceFields(); double k = 0.0001; double threshold = 0.001; //int iter = univ.Minimize_ConjugateGradient_v0(frcflds, k, threshold); int randomPurturb = 0; // no random purturbation bool[] atomsMovable = null; // update all atoms int iter_conjgrad; { Namd.Psf psf = Namd.Psf.FromFile(rootpath + @"\Sample\alanin.psf"); Pdb pdb = Pdb.FromFile(rootpath + @"\Sample\alanin.pdb"); Namd.Prm prm = Namd.Prm.FromFileXPlor(rootpath + @"\Sample\alanin.params", new TextLogger()); univ = Universe.Build(psf, prm, pdb, false); //frcflds = new List<ForceField.IForceField>(); //frcflds.Add(new ForceField.MindyBond()); //frcflds.Add(new ForceField.MindyAngle()); //frcflds.Add(new ForceField.MindyDihedral()); //frcflds.Add(new ForceField.MindyImproper()); //frcflds.Add(new ForceField.MindyNonbondedLennardJones()); //frcflds.Add(new ForceField.MindyNonbondedElectrostatic()); //iter_conjgrad = univ.Minimize_ConjugateGradient_v1(0, frcflds, k, 0.1, null, threshold, randomPurturb, atomsMovable, new MinimizeLogger_PrintEnergyForceMag(), null, null); //frcflds = new List<ForceField.IForceField>(); //frcflds.Add(new ForceField.MindyBond()); //frcflds.Add(new ForceField.MindyAngle()); //frcflds.Add(new ForceField.MindyImproper()); //frcflds.Add(new ForceField.MindyNonbondedLennardJones()); //frcflds.Add(new ForceField.MindyNonbondedElectrostatic()); //iter_conjgrad = univ.Minimize_ConjugateGradient_v1(0, frcflds, k*0.1, 0.1, null, threshold, randomPurturb, atomsMovable, new MinimizeLogger_PrintEnergyForceMag(), null, null); frcflds = new List <ForceField.IForceField>(); if (HDebug.False) { frcflds.Add(new ForceField.MindyBond()); } else { frcflds.Add(new ForceField.PwBond()); } if (HDebug.False) { frcflds.Add(new ForceField.MindyAngle()); } else { frcflds.Add(new ForceField.PwAngle()); } frcflds.Add(new ForceField.MindyDihedral()); if (HDebug.False) { frcflds.Add(new ForceField.MindyImproper()); } else { frcflds.Add(new ForceField.PwImproper()); } if (HDebug.False) { frcflds.Add(new ForceField.MindyNonbondedElectrostatic()); } else { frcflds.Add(new ForceField.PwElec()); } if (HDebug.False) { frcflds.Add(new ForceField.MindyNonbondedLennardJones()); } else { frcflds.Add(new ForceField.PwVdw()); } univ.LoadCoords(@"D:\xxxx.coords"); iter_conjgrad = univ.Minimize_ConjugateGradient_v1(0, frcflds, k, 0.01, null, threshold, randomPurturb, atomsMovable, new MinimizeLogger_PrintEnergyForceMag(), null, null); univ.SaveCoords(@"D:\xxxx.coords"); System.Console.WriteLine("======================================================================="); System.Console.WriteLine("======================================================================="); System.Console.WriteLine("======================================================================="); } { //Psf psf = Psf.FromFile(rootpath + @"\Sample\alanin.psf"); //Pdb pdb = Pdb.FromFile(rootpath + @"\Sample\alanin.pdb"); //Prm prm = Prm.FromFileXPlor(rootpath + @"\Sample\alanin.params", new TextLogger()); //univ = Universe.Build(psf, prm, pdb); //frcflds = new List<ForceField.IForceField>(); //if(true) frcflds.Add(new ForceField.MindyBond()); else frcflds.Add(new ForceField.PwBond()); //if(true) frcflds.Add(new ForceField.MindyAngle()); else frcflds.Add(new ForceField.PwAngle()); //frcflds.Add(new ForceField.MindyDihedral()); //if(true) frcflds.Add(new ForceField.MindyImproper()); else frcflds.Add(new ForceField.PwImproper()); //if(true) frcflds.Add(new ForceField.MindyNonbondedElectrostatic()); else frcflds.Add(new ForceField.PwElec()); //if(true) frcflds.Add(new ForceField.MindyNonbondedLennardJones()); else frcflds.Add(new ForceField.PwVdw()); univ.atoms[0].Coord += new Vector(0.1, 0.1, 0.1); //iter_conjgrad = univ.Minimize_ConjugateGradient_v1(0, frcflds, k, 0.001, null, threshold, randomPurturb, atomsMovable, new MinimizeLogger_PrintEnergyForceMag(), null, null); iter_conjgrad = univ.Minimize_ConjugateGradient_AtomwiseUpdate(frcflds, threshold: threshold, k: k, max_atom_movement: 0.001, max_iteration: null, atomsMovable: atomsMovable, logger: new MinimizeLogger_PrintEnergyForceMag() ); } k = 0.0005; HDebug.Assert(false); //int iter_stepdsnt = univ.Minimize_SteepestDescent(frcflds, k, threshold, System.Console.Error); } break; case "1a6g_autopsf": { Namd.Psf psf = Namd.Psf.FromFile(rootpath + @"\Sample\1a6g_autopsf.psf"); Pdb pdb = Pdb.FromFile(rootpath + @"\Sample\1a6g_autopsf.pdb"); Namd.Prm prm = Namd.Prm.FromFile(rootpath + @"\Sample\1a6g_autopsf.prm", new TextLogger()); univ = Universe.Build(psf, prm, pdb, false); List <ForceField.IForceField> frcflds = ForceField.GetMindyForceFields(); double threshold = 0.001; string minimize_ver = "v1"; switch (minimize_ver) { //case "v2": // { // double atom_max_move = 0.03; // int iter_conjgrad = univ.Minimize_ConjugateGradient_v2(frcflds, atom_max_move, threshold, System.Console.Error); // break; // } case "v1": { double k = 0.0001; int randomPurturb = 0; // no random purturbation bool[] atomsMovable = null; // update all atoms int iter_conjgrad = univ.Minimize_ConjugateGradient_v1(0, frcflds, k, 0.1, null, threshold, randomPurturb, atomsMovable, new MinimizeLogger_PrintEnergyForceMag(), null, null); break; } default: goto case "v1"; } //atom_max_move = 0.03; //int iter_stepdsnt = univ.Minimize_SteepestDescent(frcflds, atom_max_move, threshold, System.Console.Error); } break; case "1a6g_autopsf.unfolded": { Namd.Psf psf = Namd.Psf.FromFile(rootpath + @"\Sample\1a6g_autopsf.psf"); Pdb pdb = Pdb.FromFile(rootpath + @"\Sample\1a6g_autopsf.pdb"); { Random rand = new Random(1); List <Vector> coords = pdb.atoms.ListCoord(); for (int i = 0; i < coords.Count; i++) { double x = i * 0.1 + rand.NextDouble() * 0.01; double y = i * 0.1 + rand.NextDouble() * 0.01; double z = i * 0.1 + rand.NextDouble() * 0.01; coords[i] = new Vector(x, y, z); } pdb.ToFile(rootpath + @"\Sample\1a6g_autopsf.unfolded.pdb", coords); pdb = Pdb.FromFile(rootpath + @"\Sample\1a6g_autopsf.unfolded.pdb"); } Namd.Prm prm = Namd.Prm.FromFile(rootpath + @"\Sample\1a6g_autopsf.prm", new TextLogger()); univ = Universe.Build(psf, prm, pdb, false); List <ForceField.IForceField> frcflds = ForceField.GetMindyForceFields(); double threshold = 0.01; string minimize_ver = "v1"; switch (minimize_ver) { //case "v2": // { // double atom_max_move = 0.1; // int iter_conjgrad = univ.Minimize_ConjugateGradient_v2(frcflds, atom_max_move, threshold, System.Console.Error); // break; // } case "v1": { double k = 0.0001; int randomPurturb = 100; bool[] atomsMovable = null; // updates all atoms int iter_conjgrad = univ.Minimize_ConjugateGradient_v1(0, frcflds, k, 0.1, null, threshold, randomPurturb, atomsMovable, new MinimizeLogger_PrintEnergyForceMag(), null, null); break; } default: goto case "v1"; } //atom_max_move = 0.03; //int iter_stepdsnt = univ.Minimize_SteepestDescent(frcflds, atom_max_move, threshold, System.Console.Error); } break; default: goto case "alanin"; } }
public virtual void Compute(Universe.AtomPack nonbonded, Vector[] coords, ref double energy, ref Vector[] forces, ref MatrixByArr[,] hessian , double radi, double radj, double epsi, double epsj, double[,] pwfrc = null, double[,] pwspr = null) { HDebug.Depreciated("check idx1 and idx2"); int idx1 = 0; // nonbonded.atoms[0].ID; int idx2 = 1; // nonbonded.atoms[1].ID; Universe.Atom atom1 = nonbonded.atoms[0]; Universe.Atom atom2 = nonbonded.atoms[1]; Vector diff = (coords[idx2] - coords[idx1]); double dx = diff[0]; double dy = diff[1]; double dz = diff[2]; //double radi = atom1.Rmin2; //double radj = atom2.Rmin2; //double epsi = atom1.epsilon; //double epsj = atom2.epsilon; /////////////////////////////////////////////////////////////////////////////// // energy energy += EnergyVdw(dx, dy, dz, radi, radj, epsi, epsj); /////////////////////////////////////////////////////////////////////////////// // force if (forces != null) { Vector f12 = ForceVdw(dx, dy, dz, radi, radj, epsi, epsj); forces[idx1] += f12; forces[idx2] += -f12; } /////////////////////////////////////////////////////////////////////////////// // hessian if (hessian != null) { //string option = "Spring+Force"; //Vector diff01 = (coords[1] - coords[0]); //Vector diff10 = (coords[0] - coords[1]); //hessian[0, 1] += SprngVdw(diff01, radi, radj, epsi, epsj, option); //hessian[1, 0] += SprngVdw(diff10, radi, radj, epsi, epsj, option); { // !V(Lennard-Jones) = Eps,i,j[(Rmin,i,j/ri,j)**12 - 2(Rmin,i,j/ri,j)**6] // !epsilon: kcal/mole, Eps,i,j = sqrt(eps,i * eps,j) // !Rmin/2: A, Rmin,i,j = Rmin/2,i + Rmin/2,j // // V(r) = epsij * r0^12 * rij^-12 - 2 * epsij * r0^6 * rij^-6 // F(r) = -12 * epsij * r0^12 * rij^-13 - -6*2 * epsij * r0^6 * rij^-7 // K(r) = -13*-12 * epsij * r0^12 * rij^-14 - -7*-6*2 * epsij * r0^6 * rij^-8 double r = (radi + radj); double r6 = Math.Pow(r, 6); double r12 = Math.Pow(r, 12); double rij2 = (dx * dx + dy * dy + dz * dz); double rij = Math.Sqrt(rij2); double rij7 = Math.Pow(rij2, 3) * rij; double rij8 = Math.Pow(rij2, 4); double rij13 = Math.Pow(rij2, 6) * rij; double rij14 = Math.Pow(rij2, 7); double epsij = epsi * epsj; double fij = (-12) * epsij * r12 / rij13 - (-6 * 2) * epsij * r6 / rij7; double kij = (-13 * -12) * epsij * r12 / rij14 - (-7 * -6 * 2) * epsij * r6 / rij8; fij *= optHessianForceFactor; //Matrix Hij = ForceField.GetHessianBlock(coords[0], coords[1], kij, fij); hessian[0, 1] += ForceField.GetHessianBlock(coords[0], coords[1], kij, fij); hessian[1, 0] += ForceField.GetHessianBlock(coords[1], coords[0], kij, fij); } } }