public double GetRMSD(IList <int> atomidxs, IList <Vector> atomcoords) { List <Vector> coords = new List <Vector>(); foreach (int atomidx in atomidxs) { coords.Add(atoms[atomidx].Coord.Clone()); } Trans3 trans = ICP3.OptimalTransform(coords, atomcoords); List <double> SDs = new List <double>(); for (int i = 0; i < coords.Count; i++) { Vector moved = trans.DoTransform(coords[i]); Vector to = atomcoords[i]; double SD = (moved - to).Dist2; SDs.Add(SD); } double RMSD = Math.Sqrt(SDs.Average()); return(RMSD); }
public static Trans3 GetTrans(IList <Vector> C1, IList <Anisou> anisou1, IList <Vector> C2, HPack <double> optEnergy = null) { int size = C1.Count; HDebug.Assert(size == C1.Count, size == C2.Count); Vector[] C1s = C1.ToArray().Clone(3); Vector[] C2s = C2.ToArray().Clone(3); double[] W1s = new double[size * 3]; double[] enrgs = new double[size * 3]; for (int i = 0; i < size; i++) { HDebug.Assert(anisou1[i].eigvals[0] >= 0); W1s[i * 3 + 0] = (anisou1[i].eigvals[0] <= 0) ? 0 : 1 / anisou1[i].eigvals[0]; HDebug.Assert(anisou1[i].eigvals[1] >= 0); W1s[i * 3 + 1] = (anisou1[i].eigvals[1] <= 0) ? 0 : 1 / anisou1[i].eigvals[1]; HDebug.Assert(anisou1[i].eigvals[2] >= 0); W1s[i * 3 + 2] = (anisou1[i].eigvals[2] <= 0) ? 0 : 1 / anisou1[i].eigvals[2]; enrgs[i * 3 + 0] = enrgs[i * 3 + 1] = enrgs[i * 3 + 2] = double.NaN; } //Trans3 trans = ICP3.OptimalTransform(C2, C1); Trans3 trans = new Trans3(new double[] { 0, 0, 0 }, 1, Quaternion.UnitRotation);// = transICP3.OptimalTransformWeighted(C2, C1, W1s); int iter = 0; int iter_max = 1000; //double enrg = double.NaN; while (iter < iter_max) { iter++; Vector[] C2sUpdated = trans.GetTransformed(C2s); //for(int i=0; i<size; i++) System.Threading.Tasks.Parallel.For(0, size, delegate(int i) { for (int j = 0; j < 3; j++) { Vector planeNormal = anisou1[i].axes[j]; Vector planeBase = C1[i]; Vector query = C2sUpdated[i * 3 + j]; Vector closest = query - LinAlg.DotProd(planeNormal, query - planeBase) * planeNormal; HDebug.AssertTolerance(0.00001, LinAlg.DotProd(closest - planeBase, planeNormal)); C1s[i * 3 + j] = closest; enrgs[i * 3 + j] = W1s[i * 3 + j] * (query - closest).Dist2; } }); Trans3 dtrans = ICP3.OptimalTransformWeighted(C2sUpdated, C1s, W1s); trans = Trans3.AppendTrans(trans, dtrans); double max_dtrans_matrix = (dtrans.TransformMatrix - LinAlg.Eye(4)).ToArray().HAbs().HMax(); if (max_dtrans_matrix < 0.0000001) { break; } } if (optEnergy != null) { optEnergy.value = enrgs.Sum() / size; } return(trans); }
public static Trans3 GetTrans(IList <Vector> C1 , IList <Vector> C2 , IList <double> weight //, Pack<List<Vector>> C2new = null ) { if (HDebug.Selftest()) { double[] tweight = new double[weight.Count]; for (int i = 0; i < tweight.Length; i++) { tweight[i] = 0.2; } Trans3 ttrans0 = GetTrans(C1, C2, tweight); Trans3 ttrans1 = MinRMSD.GetTrans(C1, C2); HDebug.AssertTolerance(0.0001, ttrans0.ds - ttrans1.ds); HDebug.AssertTolerance(0.0001, ttrans0.dt - ttrans1.dt); HDebug.AssertTolerance(0.0001, new Vector(ttrans0.dr.ToArray()) - ttrans1.dr.ToArray()); HDebug.AssertTolerance(0.0001, (ttrans0.TransformMatrix - ttrans1.TransformMatrix)); } HDebug.Assert(C1.Count == C2.Count); HDebug.Assert(C1.Count == weight.Count); //Trans3 trans = ICP3.OptimalTransform(C2, C1); Trans3 trans = ICP3.OptimalTransformWeighted(C2, C1, weight); if (HDebug.IsDebuggerAttached) { Vector[] C2updated = trans.GetTransformed(C2).ToArray(); double RMSD0 = 0; double RMSD1 = 0; for (int i = 0; i < C1.Count; i++) { RMSD0 += (C1[i] - C2[i]).Dist2; RMSD1 += (C1[i] - C2updated[i]).Dist2; } //Debug.AssertTolerance(0.00000001, Math.Abs(RMSD1 - RMSD0)); } return(trans); }
public static Trans3 GetTrans(IList <Vector> C1 , IList <Vector> C2 // ref //, Pack<List<Vector>> C2new = null ) { HDebug.Assert(C1.Count == C2.Count); Trans3 trans = ICP3.OptimalTransform(C2, C1); if (HDebug.IsDebuggerAttached) { Vector[] C2updated = trans.GetTransformed(C2).ToArray(); double RMSD0 = 0; double RMSD1 = 0; for (int i = 0; i < C1.Count; i++) { RMSD0 += (C1[i] - C2[i]).Dist2; RMSD1 += (C1[i] - C2updated[i]).Dist2; } RMSD0 /= C1.Count; RMSD1 /= C1.Count; //HDebug.AssertTolerance(0.00000001, Math.Abs(RMSD1 - RMSD0)); } return(trans); }
public int QuasiNewton(List <ForceField.IForceField> frcflds , double threshold , double?k = null , double max_atom_movement = 0.01 , int?max_iteration = null , IMinimizeLogger logger = null // logger = new MinimizeLogger_PrintEnergyForceMag(logpath); , HPack <double> optOutEnergy = null // optional output for final energy , List <Vector> optOutForces = null // optional output for final force vectors , HPack <double> optOutForcesNorm1 = null // optional output for norm of final force vectors , HPack <double> optOutForcesNorm2 = null // optional output for norm of final force vectors , HPack <double> optOutForcesNormInf = null // optional output for norm of final force vectors ) { if (k == null) { k = double.MaxValue; foreach (ForceField.IForceField frcfld in frcflds) { double?kk = frcfld.GetDefaultMinimizeStep(); if (kk.HasValue) { k = Math.Min(k.Value, kk.Value); } } } Graph <Universe.Atom[], Universe.Bond> univ_flexgraph = univ.BuildFlexibilityGraph(); List <Universe.RotableInfo> univ_rotinfos = univ.GetRotableInfo(univ_flexgraph); // double k = 0.0001; int iter = 0; // 0. Initial configuration of atoms Vector[] coords = univ.GetCoords(); Vector[] coords0 = univ.GetCoords(); bool[] atomsMovable = null; { atomsMovable = new bool[size]; for (int i = 0; i < size; i++) { atomsMovable[i] = true; } } Dictionary <string, object> cache = new Dictionary <string, object>(); Vector[] forces = null; MatrixByArr hessian; double energy = 0; double[] dtor; double forces_NormInf; double forces_Norm1; double forces_Norm2; do { if (logger != null) { if (forces != null) { logger.log(iter, coords, energy, forces, atomsMovable); } { Vector[] coordsx = coords.HClone <Vector>(); Trans3 trans = ICP3.OptimalTransform(coordsx, coords0); trans.DoTransform(coordsx); logger.logTrajectory(univ, iter, coordsx); } } //GetPotentialAndTorForce(frcflds, coords // , out energy, out forces, out hessian, out dtor // , cache); { MatrixByArr J = Paper.TNM.GetJ(univ, coords, univ_rotinfos); Vector[] forces0 = univ.GetVectorsZero(); hessian = new double[size * 3, size *3]; energy = univ.GetPotential(frcflds, coords, ref forces0, ref hessian, cache); forces = univ.GetVectorsZero(); dtor = Paper.TNM.GetRotAngles(univ, coords, hessian, forces0, J: J, forceProjectedByTorsional: forces); for (int i = 0; i < forces0.Length; i++) { forces0[i] = forces0[i] * 0.001; } dtor = Paper.TNM.GetRotAngles(univ, coords, hessian, forces0, J: J); } forces_NormInf = NormInf(forces, atomsMovable); // NormInf(forces_prd, atomsMovable); forces_Norm1 = Norm(1, forces, atomsMovable); // Norm(1, forces_prd, atomsMovable); forces_Norm2 = Norm(2, forces, atomsMovable); // Norm(2, forces_prd, atomsMovable); if (forces_NormInf < 0.0001) { System.Environment.Exit(0); break; } coords = Paper.TNM.RotateTorsionals(coords, dtor, univ_rotinfos); iter++; }while(true); while (true) { // if(forces.IsComputable == false) // { // System.Console.Error.WriteLine("non-computable components while doing steepest-descent"); // Environment.Exit(0); // } if (logger != null) { logger.log(iter, coords, energy, forces, atomsMovable); logger.logTrajectory(univ, iter, coords); //if(iter %10 == 0) //{ // System.IO.Directory.CreateDirectory("output"); // string pdbname = string.Format("mini.conju.{0:D5}.pdb", iter); // pdb.ToFile("output\\"+pdbname, coords.ToArray()); // System.IO.File.AppendAllLines("output\\mini.conju.[animation].pml", new string[] { "load "+pdbname+", 1A6G" }); //} } // 1. Save the position of atoms // 2. Calculate the potential energy of system and the net forces on atoms // 3. Check if every force reaches to zero, // , and END if yes bool stopIteration = false; if (forces_NormInf < threshold) { stopIteration = true; } if ((max_iteration != null) && (iter >= max_iteration.Value)) { stopIteration = true; } if (stopIteration) { // double check cache = new Dictionary <string, object>(); // reset cache //energy = GetPotential(frcflds, coords, out forces, cache); // energy = univ.GetPotential(frcflds, coords, ref forces._vecs, ref hessian, cache); forces_NormInf = NormInf(forces, atomsMovable); forces_Norm1 = Norm(1, forces, atomsMovable); forces_Norm2 = Norm(2, forces, atomsMovable); if (forces_NormInf < threshold) { if (iter != 1) { univ.SetCoords(coords); } //{ // string pdbname = string.Format("mini.conju.{0:D5}.pdb", iter); // pdb.ToFile("output\\"+pdbname, coords.ToArray()); // System.IO.File.AppendAllLines("output\\mini.conju.[animation].pml", new string[] { "load "+pdbname+", 1A6G" }); //} { if (optOutEnergy != null) { optOutEnergy.value = energy; } if (optOutForces != null) { optOutForces.Clear(); optOutForces.AddRange(forces.ToArray()); } if (optOutForcesNorm1 != null) { optOutForcesNorm1.value = forces_Norm1; } if (optOutForcesNorm2 != null) { optOutForcesNorm2.value = forces_Norm2; } if (optOutForcesNormInf != null) { optOutForcesNormInf.value = forces_NormInf; } } return(iter); } } // 4. Move atoms with conjugated gradient //Vectors coords_prd; { if ((iter > 0) && (iter % 100 == 0)) { cache = new Dictionary <string, object>(); // reset cache } if (iter > 1) { // Debug.Assert(forces0 != null); // double r = Vectors.VtV(forces, forces).Sum() / Vectors.VtV(forces0, forces0).Sum(); // h = forces + r * h; // double kk = k.Value; // double hNormInf = NormInf(h, atomsMovable); // if(kk*hNormInf > max_atom_movement) // // make the maximum movement as atomsMovable // kk = max_atom_movement/(hNormInf); // //double kk = (k*h.NormsInf().NormInf() < max_atom_movement) ? k : (max_atom_movement/h.NormsInf().NormInf()); // //double kk = (k.Value*NormInf(h,atomsMovable) < max_atom_movement)? k.Value : (max_atom_movement/NormInf(h,atomsMovable)); // double[] dangles = TNM.GetRotAngles(univ, coords._vecs, hessian, (kk * h)._vecs); // coords_prd = TNM.RotateTorsionals(coords, dangles, univ_rotinfos); } else { // // same to the steepest descent for the first iteration // h = forces; // double kk = k.Value; // double hNormInf = NormInf(h, atomsMovable); // if(kk*hNormInf > max_atom_movement) // // make the maximum movement as atomsMovable // kk = max_atom_movement/(hNormInf); // //double kk = (k*h.NormsInf().NormInf() < max_atom_movement) ? k : (max_atom_movement/h.NormsInf().NormInf()); // //double kk = (k.Value*NormInf(h,atomsMovable) < max_atom_movement)? k.Value : (max_atom_movement/NormInf(h, atomsMovable)); // // //double[] dangles = TNM.GetRotAngles(this, coords, kk * h, 1); // double[] dangles = TNM.GetRotAngles(univ, coords._vecs, hessian, (kk * h)._vecs); // coords_prd = TNM.RotateTorsionals(coords, dangles, univ_rotinfos); // // // coords_prd = AddConditional(coords, kk * h, atomsMovable); } } // 5. Predict energy or forces on atoms iter++; //double energy_prd; Vectors forces_prd = univ.GetVectorsZero(); MatrixByArr hessian_prd = new double[size * 3, size *3]; //double energy_prd = GetPotential(frcflds, coords_prd, out forces_prd, cache); iter++; // GetPotentialAndTorForce(frcflds, coords_prd // , out energy_prd, out forces_prd._vecs, out hessian_prd // , cache); // // double energy_prd = univ.GetPotential(frcflds, coords_prd, ref forces_prd._vecs, ref hessian_prd, cache); // // Vector[] dcoord_prd = univ.GetVectorsZero(); // // double[] dangles_prd = TNM.GetRotAngles(univ, coords_prd, forces_prd, 1, dcoordsRotated: dcoord_prd); // // dangles_prd = TNM.GetRotAngles(univ, coords_prd, hessian_prd, forces_prd, forceProjectedByTorsional: dcoord_prd); // //Vectors coords_prd2 = coords_prd.Clone(); // //TNM.RotateTorsionals(coords_prd2, dangles_prd, univ_rotinfos); // // double forces_prd_NormInf = NormInf(forces_prd, atomsMovable); // NormInf(forces_prd, atomsMovable); // double forces_prd_Norm1 = Norm(1, forces_prd, atomsMovable); // Norm(1, forces_prd, atomsMovable); // double forces_prd_Norm2 = Norm(2, forces_prd, atomsMovable); // Norm(2, forces_prd, atomsMovable); // // 6. Check if the predicted forces or energy will exceed over the limit // // , and goto 1 if no // // doSteepDeescent = true; // //if((doSteepDeescent == false) || ((energy_prd <= energy) && (forces_prd_NormInf < forces_NormInf+1.0)) // if((energy_prd < energy+0.1) && (forces_prd_NormInf < forces_NormInf+0.0001)) // { // energy0 = energy; // forces0 = forces; // coords = coords_prd; // forces = forces_prd; // hessian = hessian_prd; // energy = energy_prd; // forces_NormInf = forces_prd_NormInf; // forces_Norm1 = forces_prd_Norm1; // forces_Norm2 = forces_prd_Norm2; // continue; // } // if(logger != null) // logger.log(iter, coords_prd, energy_prd, forces_prd, atomsMovable, "will do steepest"); // // 7. Back to saved configuration // // 8. Move atoms with simple gradient // { // // same to the steepest descent // h = forces; // double kk = k.Value; // double hNormInf = NormInf(h, atomsMovable); // if(kk*hNormInf > max_atom_movement) // // make the maximum movement as atomsMovable // kk = max_atom_movement/(hNormInf); // double[] dangles = TNM.GetRotAngles(univ, coords._vecs, hessian, (kk * h)._vecs); // coords_prd = TNM.RotateTorsionals(coords, dangles, univ_rotinfos); // } // forces_prd = univ.GetVectorsZero(); // hessian_prd = new double[size*3, size*3]; // //energy_prd = univ.GetPotential(frcflds, coords_prd, ref forces_prd._vecs, ref hessian_prd, cache); // GetPotentialAndTorForce(frcflds, coords_prd // , out energy_prd, out forces_prd._vecs, out hessian_prd // , cache); // forces_prd_NormInf = NormInf(forces_prd, atomsMovable); // forces_prd_Norm1 = Norm(1, forces_prd, atomsMovable); // forces_prd_Norm2 = Norm(2, forces_prd, atomsMovable); // // energy0 = energy; // forces0 = forces; // coords = coords_prd; // forces = forces_prd; // energy = energy_prd; // forces_NormInf = forces_prd_NormInf; // forces_Norm1 = forces_prd_Norm1; // forces_Norm2 = forces_prd_Norm2; // // 9. goto 1 } }
public static Trans3 GetTrans(IList <Vector> C1, IList <Anisou> anisou1, IList <Vector> C2, IList <Anisou> anisou2, HPack <double> optEnergy = null) { int size = C1.Count; HDebug.Assert(size == C1.Count, size == C2.Count); Vector[] srcs = new Vector[size * 3 * 2]; // sources Vector[] tars = new Vector[size * 3 * 2]; // targets double[] weis = new double[size * 3 * 2]; // weights double[] engs = new double[size * 3 * 2]; // energies //for(int i=0; i<size; i++) //{ // Debug.Assert(anisou1[i].eigvals[0] >= 0); W1s[i*3+0] = (anisou1[i].eigvals[0] <= 0) ? 0 : 1 / anisou1[i].eigvals[0]; // Debug.Assert(anisou1[i].eigvals[1] >= 0); W1s[i*3+1] = (anisou1[i].eigvals[1] <= 0) ? 0 : 1 / anisou1[i].eigvals[1]; // Debug.Assert(anisou1[i].eigvals[2] >= 0); W1s[i*3+2] = (anisou1[i].eigvals[2] <= 0) ? 0 : 1 / anisou1[i].eigvals[2]; // enrgs[i*3+0] = enrgs[i*3+1] = enrgs[i*3+2] = double.NaN; //} //Trans3 trans = ICP3.OptimalTransform(C2, C1); Trans3 trans = new Trans3(new double[] { 0, 0, 0 }, 1, Quaternion.UnitRotation);// = transICP3.OptimalTransformWeighted(C2, C1, W1s); int iter = 0; int iter_max = 1000; //double enrg = double.NaN; while (iter < iter_max) { iter++; //for(int i=0; i<size; i++) System.Threading.Tasks.Parallel.For(0, size, delegate(int i) { for (int j = 0; j < 3; j++) { Vector p1 = C1[i]; Vector n1 = anisou1[i].axes[j]; double w1 = anisou1[i].eigvals[j]; w1 = (w1 <= 0) ? 0 : 1 / w1; Vector p2 = trans.DoTransform(C2[i]); Vector n2 = (trans.DoTransform(C2[i] + anisou2[i].axes[j]) - p2).UnitVector(); double w2 = anisou2[i].eigvals[j]; w2 = (w2 <= 0) ? 0 : 1 / w2; Vector clo12 = p2 - LinAlg.DotProd(n1, p2 - p1) * n1; // closest point from p1 to plane2 Vector clo21 = p1 - LinAlg.DotProd(n2, p1 - p2) * n2; // closest point from p1 to plane2 HDebug.AssertTolerance(0.00001, LinAlg.DotProd(clo12 - p1, n1)); HDebug.AssertTolerance(0.00001, LinAlg.DotProd(clo21 - p2, n2)); // p2 -> (pt closest to p2 on plane1 with w1) srcs[(i * 3 + j) * 2 + 0] = p2; tars[(i * 3 + j) * 2 + 0] = clo12; weis[(i * 3 + j) * 2 + 0] = w1; engs[(i * 3 + j) * 2 + 0] = w1 * (p2 - clo12).Dist2; // inverse of {p1 -> (pt closest to p1 on plane2 with w2)} // = (pt closest to p1 on plane2 with w2) -> p1 srcs[(i * 3 + j) * 2 + 1] = clo21; tars[(i * 3 + j) * 2 + 1] = p1; weis[(i * 3 + j) * 2 + 1] = w2; engs[(i * 3 + j) * 2 + 1] = w2 * (clo21 - p1).Dist2; } }); Trans3 dtrans = ICP3.OptimalTransformWeighted(srcs, tars, weis); trans = Trans3.AppendTrans(trans, dtrans); double max_dtrans_matrix = (dtrans.TransformMatrix - LinAlg.Eye(4)).ToArray().HAbs().HMax(); if (max_dtrans_matrix < 0.0000001) { break; } } if (optEnergy != null) { optEnergy.value = engs.Sum() / size; } return(trans); }
public int ConjugateGradient(List <ForceField.IForceField> frcflds , double threshold , double?k = null , double max_atom_movement = 0.01 , int?max_iteration = null , IMinimizeLogger logger = null // logger = new MinimizeLogger_PrintEnergyForceMag(logpath); , HPack <double> optOutEnergy = null // optional output for final energy , List <Vector> optOutForces = null // optional output for final force vectors , HPack <double> optOutForcesNorm1 = null // optional output for norm of final force vectors , HPack <double> optOutForcesNorm2 = null // optional output for norm of final force vectors , HPack <double> optOutForcesNormInf = null // optional output for norm of final force vectors ) { if (k == null) { k = double.MaxValue; foreach (ForceField.IForceField frcfld in frcflds) { double?kk = frcfld.GetDefaultMinimizeStep(); if (kk.HasValue) { k = Math.Min(k.Value, kk.Value); } } } Graph <Universe.Atom[], Universe.Bond> univ_flexgraph = univ.BuildFlexibilityGraph(); List <Universe.RotableInfo> univ_rotinfos = univ.GetRotableInfo(univ_flexgraph); // double k = 0.0001; int iter = 0; // 0. Initial configuration of atoms Vector[] coords0 = univ.GetCoords(); Vectors coords = univ.GetCoords(); bool[] atomsMovable = null; if (atomsMovable == null) { atomsMovable = new bool[size]; for (int i = 0; i < size; i++) { atomsMovable[i] = true; } } Vectors h = univ.GetVectorsZero(); Vectors forces = univ.GetVectorsZero(); Dictionary <string, object> cache; double energy; cache = new Dictionary <string, object>(); GetPotentialAndTorForce(frcflds, coords , out energy, out forces._vecs , cache); double forces_NormInf = NormInf(forces, atomsMovable); double forces_Norm1 = Norm(1, forces, atomsMovable); double forces_Norm2 = Norm(2, forces, atomsMovable); Vectors forces0 = forces; double energy0 = energy; while (true) { if (forces.IsComputable == false) { System.Console.Error.WriteLine("non-computable components while doing steepest-descent"); HEnvironment.Exit(0); } if (logger != null) { logger.log(iter, coords, energy, forces, atomsMovable); { // logger.logTrajectory(univ, iter, coords); Vector[] coordsx = coords._vecs.HClone <Vector>(); Trans3 trans = ICP3.OptimalTransform(coordsx, coords0); trans.DoTransform(coordsx); logger.logTrajectory(univ, iter, coordsx); } } // 1. Save the position of atoms // 2. Calculate the potential energy of system and the net forces on atoms // 3. Check if every force reaches to zero, // , and END if yes bool stopIteration = false; if (forces_NormInf < threshold) { stopIteration = true; } if ((max_iteration != null) && (iter >= max_iteration.Value)) { stopIteration = true; } if (stopIteration) { // double check cache = new Dictionary <string, object>(); // reset cache GetPotentialAndTorForce(frcflds, coords , out energy, out forces._vecs , cache); forces_NormInf = NormInf(forces, atomsMovable); forces_Norm1 = Norm(1, forces, atomsMovable); forces_Norm2 = Norm(2, forces, atomsMovable); if (forces_NormInf < threshold) { if (iter != 1) { univ.SetCoords((Vector[])coords); } { if (optOutEnergy != null) { optOutEnergy.value = energy; } if (optOutForces != null) { optOutForces.Clear(); optOutForces.AddRange(forces.ToArray()); } if (optOutForcesNorm1 != null) { optOutForcesNorm1.value = forces_Norm1; } if (optOutForcesNorm2 != null) { optOutForcesNorm2.value = forces_Norm2; } if (optOutForcesNormInf != null) { optOutForcesNormInf.value = forces_NormInf; } } return(iter); } } // 4. Move atoms with conjugated gradient Vectors coords_prd; { if ((iter > 0) && (iter % 100 == 0)) { cache = new Dictionary <string, object>(); // reset cache } if (iter >= 1) { HDebug.Assert(forces0 != null); double r = Vectors.VtV(forces, forces).Sum() / Vectors.VtV(forces0, forces0).Sum(); h = forces + r * h; double kk = k.Value; double hNormInf = NormInf(h, atomsMovable); if (kk * hNormInf > max_atom_movement) { // make the maximum movement as atomsMovable kk = max_atom_movement / (hNormInf); } Vector dangles = Paper.TNM.GetRotAngles(univ, coords, kk * h, 1); //dangles *= -1; coords_prd = Paper.TNM.RotateTorsionals(coords, dangles, univ_rotinfos); } else { // same to the steepest descent for the first iteration h = forces; double kk = k.Value; double hNormInf = NormInf(h, atomsMovable); if (kk * hNormInf > max_atom_movement) { // make the maximum movement as atomsMovable kk = max_atom_movement / (hNormInf); } Vector dangles = Paper.TNM.GetRotAngles(univ, coords, kk * h, 1); //dangles *= -1; coords_prd = Paper.TNM.RotateTorsionals(coords, dangles, univ_rotinfos); } } // 5. Predict energy or forces on atoms iter++; double energy_prd; Vectors forces_prd = univ.GetVectorsZero(); GetPotentialAndTorForce(frcflds, coords_prd , out energy_prd, out forces_prd._vecs , cache); // double energy_prd = univ.GetPotential(frcflds, coords_prd, ref forces_prd._vecs, ref hessian_prd, cache); // Vector[] dcoord_prd = univ.GetVectorsZero(); // double[] dangles_prd = TNM.GetRotAngles(univ, coords_prd, forces_prd, 1, dcoordsRotated: dcoord_prd); // dangles_prd = TNM.GetRotAngles(univ, coords_prd, hessian_prd, forces_prd, forceProjectedByTorsional: dcoord_prd); //Vectors coords_prd2 = coords_prd.Clone(); //TNM.RotateTorsionals(coords_prd2, dangles_prd, univ_rotinfos); double forces_prd_NormInf = NormInf(forces_prd, atomsMovable); // NormInf(forces_prd, atomsMovable); double forces_prd_Norm1 = Norm(1, forces_prd, atomsMovable); // Norm(1, forces_prd, atomsMovable); double forces_prd_Norm2 = Norm(2, forces_prd, atomsMovable); // Norm(2, forces_prd, atomsMovable); // 6. Check if the predicted forces or energy will exceed over the limit // , and goto 1 if no if ((energy_prd < energy + 0.1) && (forces_prd_NormInf < forces_NormInf + 0.0001)) { energy0 = energy; forces0 = forces; coords = coords_prd; forces = forces_prd; energy = energy_prd; forces_NormInf = forces_prd_NormInf; forces_Norm1 = forces_prd_Norm1; forces_Norm2 = forces_prd_Norm2; continue; } if (logger != null) { logger.log(iter, coords_prd, energy_prd, forces_prd, atomsMovable, "will do steepest"); } // 7. Back to saved configuration // 8. Move atoms with simple gradient { // same to the steepest descent h = forces; double kk = k.Value; double hNormInf = NormInf(h, atomsMovable); if (kk * hNormInf > max_atom_movement) { // make the maximum movement as atomsMovable kk = max_atom_movement / (hNormInf); } Vector dangles = Paper.TNM.GetRotAngles(univ, coords, kk * h, 1); //dangles *= -1; coords_prd = Paper.TNM.RotateTorsionals(coords, dangles, univ_rotinfos); } forces_prd = univ.GetVectorsZero(); //energy_prd = univ.GetPotential(frcflds, coords_prd, ref forces_prd._vecs, ref hessian_prd, cache); GetPotentialAndTorForce(frcflds, coords_prd , out energy_prd, out forces_prd._vecs , cache); forces_prd_NormInf = NormInf(forces_prd, atomsMovable); forces_prd_Norm1 = Norm(1, forces_prd, atomsMovable); forces_prd_Norm2 = Norm(2, forces_prd, atomsMovable); energy0 = energy; forces0 = forces; coords = coords_prd; forces = forces_prd; energy = energy_prd; forces_NormInf = forces_prd_NormInf; forces_Norm1 = forces_prd_Norm1; forces_Norm2 = forces_prd_Norm2; // 9. goto 1 } }
public static Trans3 GetTrans(IList <Vector> C1, IList <double> bfactor, IList <Vector> C2) { Trans3 trans; double[] weight = new double[bfactor.Count]; { for (int i = 0; i < weight.Length; i++) { weight[i] = 1.0 / bfactor[i]; } } { int size = C1.Count; Vector MassCenter1 = new double[3]; Vector MassCenter2 = new double[3]; { double weight_sum = 0; for (int i = 0; i < size; i++) { weight_sum += weight[i]; MassCenter1 += weight[i] * C1[i]; MassCenter2 += weight[i] * C2[i]; } MassCenter1 = MassCenter1 / weight_sum; MassCenter2 = MassCenter2 / weight_sum; } Vector[] nc1 = new Vector[size]; Vector[] nc2 = new Vector[size]; { for (int i = 0; i < size; i++) { nc1[i] = C1[i] - MassCenter1; nc2[i] = C2[i] - MassCenter2; } } Quaternion rot = ICP3.OptimalRotationWeighted(nc2, nc1, weight); Quaternion urot = Quaternion.UnitRotation; Trans3 trans1 = new Trans3(-MassCenter2, 1, urot); Trans3 trans2 = new Trans3(new double[3], 1, rot); Trans3 trans3 = new Trans3(MassCenter1, 1, urot); trans = trans1; trans = Trans3.AppendTrans(Trans3.AppendTrans(trans1, trans2), trans3); HDebug.AssertTolerance(0.00000001, trans.DoTransform(MassCenter2) - MassCenter1); return(trans); } // /// original source // { // trans = ICP3.OptimalTransformWeighted(C2, C1, weight); // if(HDebug.IsDebuggerAttached) // { // Vector[] C2updated = trans.GetTransformed(C2).ToArray(); // double RMSD0 = 0; // double RMSD1 = 0; // for(int i=0; i<C1.Count; i++) // { // RMSD0 += (C1[i]-C2[i]).Dist2; // RMSD1 += (C1[i]-C2updated[i]).Dist2; // } // HDebug.Assert(RMSD1 <= RMSD0); // } // return trans; // } }