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 static void SelfTest() { if (HDebug.Selftest() == false) { return; } string temppath = @"K:\temp\"; string tinkerpath_testgrad = "\"" + @"C:\Program Files\Tinker\bin-win64-8.2.1\testgrad.exe" + "\""; string tinkerpath_testhess = "\"" + @"C:\Program Files\Tinker\bin-win64-8.2.1\testhess.exe" + "\""; var xyz = Tinker.Xyz.FromLines(SelftestData.lines_1L2Y_xyz); var prm = Tinker.Prm.FromLines(SelftestData.lines_charmm22_prm); var univ = Universe.Build(xyz, prm); var testhess = Tinker.Run.Testhess(tinkerpath_testhess, xyz, prm, temppath , HessMatrixZeros: HessMatrixLayeredArray.ZerosHessMatrixLayeredArray ); var testgrad = Tinker.Run.Testgrad(tinkerpath_testgrad, xyz, prm, temppath); var hessinfo = Hess.HessInfo.FromTinker(xyz, prm, testhess.hess); var hessforcinfo = HessForc.Coarse.HessForcInfo.From(hessinfo); hessforcinfo.forc = testgrad.anlyts.GetForces(xyz.atoms); var coarseinfo_debug = HessForc.Coarse.GetCoarseHessForc (hessforcinfo , coords: hessinfo.coords , GetIdxKeepListRemv: GetIdxKeepListRemv , ila: null , thres_zeroblk: double.Epsilon , options: new string[] { "Debug" } ); var coarseinfo_simple = HessForc.Coarse.GetCoarseHessForc (hessforcinfo , coords: hessinfo.coords , GetIdxKeepListRemv: GetIdxKeepListRemv , ila: null , thres_zeroblk: double.Epsilon , options: new string[] { "SubSimple" } ); double absmax_simple = (coarseinfo_debug.hess - coarseinfo_simple.hess).HAbsMax(); HDebug.Assert(Math.Abs(absmax_simple) < 0.00000001); double absmax_simple_forc = (coarseinfo_debug.forc.ToVector() - coarseinfo_simple.forc.ToVector()).ToArray().MaxAbs(); HDebug.Assert(Math.Abs(absmax_simple_forc) < 0.00000001); var coarseinfo_1iter = HessForc.Coarse.GetCoarseHessForc (hessforcinfo , coords: hessinfo.coords , GetIdxKeepListRemv: GetIdxKeepListRemv , ila: null , thres_zeroblk: double.Epsilon , options: new string[] { "OneIter" } ); double absmax_1iter = (coarseinfo_debug.hess - coarseinfo_1iter.hess).HAbsMax(); HDebug.Assert(Math.Abs(absmax_1iter) < 0.00000001); double absmax_1iter_forc = (coarseinfo_debug.forc.ToVector() - coarseinfo_1iter.forc.ToVector()).ToArray().MaxAbs(); HDebug.Assert(Math.Abs(absmax_1iter_forc) < 0.00000001); var coarseinfo_iter = HessForc.Coarse.GetCoarseHessForc (hessforcinfo , coords: hessinfo.coords , GetIdxKeepListRemv: GetIdxKeepListRemv , ila: null , thres_zeroblk: double.Epsilon , options: null ); double absmax_iter = (coarseinfo_debug.hess - coarseinfo_iter.hess).HAbsMax(); HDebug.Assert(Math.Abs(absmax_iter) < 0.00000001); double absmax_iter_forc = (coarseinfo_debug.forc.ToVector() - coarseinfo_iter.forc.ToVector()).ToArray().MaxAbs(); HDebug.Assert(Math.Abs(absmax_iter_forc) < 0.00000001); double tolerance = 1.0E-6; // 0.00001; var coarseinfo_1iter_tolerant = HessForc.Coarse.GetCoarseHessForc (hessforcinfo , coords: hessinfo.coords , GetIdxKeepListRemv: GetIdxKeepListRemv , ila: null , thres_zeroblk: tolerance , options: new string[] { "OneIter" } ); double absmax_1iter_tolerant = (coarseinfo_debug.hess - coarseinfo_1iter_tolerant.hess).HAbsMax(); HDebug.Assert(Math.Abs(absmax_1iter_tolerant) < tolerance * 10); double absmax_1iter_tolerant_forc = (coarseinfo_debug.forc.ToVector() - coarseinfo_1iter_tolerant.forc.ToVector()).ToArray().MaxAbs(); HDebug.Assert(Math.Abs(absmax_1iter_tolerant_forc) < tolerance * 10); var coarseinfo_iter_tolerant = HessForc.Coarse.GetCoarseHessForc (hessforcinfo , coords: hessinfo.coords , GetIdxKeepListRemv: GetIdxKeepListRemv , ila: null , thres_zeroblk: tolerance , options: null ); double absmax_iter_tolerant = (coarseinfo_debug.hess - coarseinfo_iter_tolerant.hess).HAbsMax(); HDebug.Assert(Math.Abs(absmax_iter_tolerant) < tolerance * 10); double absmax_iter_tolerant_forc = (coarseinfo_debug.forc.ToVector() - coarseinfo_iter_tolerant.forc.ToVector()).ToArray().MaxAbs(); HDebug.Assert(Math.Abs(absmax_iter_tolerant_forc) < tolerance * 10); string tempfilepath = HFile.GetTempPath(temppath, "test_serialzation_CoarseHessForc.dat"); HSerialize.Serialize(tempfilepath, null, coarseinfo_iter_tolerant); var coarseinfo_iter_tolerant2 = HSerialize.Deserialize <HessForcInfo>(tempfilepath, null); double absmax_iter_tolerant_file = (coarseinfo_iter_tolerant.hess - coarseinfo_iter_tolerant.hess).HAbsMax(); HDebug.Assert(Math.Abs(absmax_iter_tolerant_file) == 0); double absmax_iter_tolerant_file_forc = (coarseinfo_iter_tolerant.forc.ToVector() - coarseinfo_iter_tolerant.forc.ToVector()).ToArray().MaxAbs(); HDebug.Assert(Math.Abs(absmax_iter_tolerant_file_forc) == 0); HFile.Delete(tempfilepath); }