private static void FourthTerm(IList <Vector> caArray, double Epsilon, HessMatrix hessian, int i, int j) { int[] idx = new int[] { i, j }; Vector[] lcaArray = new Vector[] { caArray[idx[0]], caArray[idx[1]] }; Matrix lhess = FourthTerm(lcaArray, Epsilon); for (int c = 0; c < 2; c++) { for (int dc = 0; dc < 3; dc++) { for (int r = 0; r < 2; r++) { for (int dr = 0; dr < 3; dr++) { hessian[idx[c] * 3 + dc, idx[r] * 3 + dr] += lhess[c * 3 + dc, r *3 + dr]; } } } } if (HDebug.IsDebuggerAttachedWithProb(0.1)) { if (caArray.Count == 2) { // avoid stack overflow return; } HessMatrix lhess0 = new double[6, 6]; FourthTerm(lcaArray, Epsilon, lhess0, 0, 1); FourthTerm(lcaArray, Epsilon, lhess0, 1, 0); double r2 = (lcaArray[0] - lcaArray[1]).Dist2; Matrix dhess0 = (120 * Epsilon / r2) * Hess.GetHessAnm(lcaArray, double.PositiveInfinity); HDebug.AssertToleranceMatrix(0.00000001, lhess0 - dhess0); } }
static double GetPotentialUpdated_ProbToCheckWithGetPotential = 0;//1;//0.1; public double GetPotentialUpdated(List <ForceField.IForceField> frcflds , double?energy0, Vector[] coords0, Vector[] forces0 , Vector[] coords, Vector[] forces , ref Nonbondeds_v1 nonbondeds ) { if (GetPotentialUpdated_SelfTestDo == true) #region selftest { GetPotentialUpdated_SelfTest(frcflds, energy0, coords0, forces0, coords, forces); } #endregion bool[] updated = new bool[size]; if ((energy0 == null) || (nonbondeds == null)) { HDebug.AssertIf(energy0 == null, coords0 == null); HDebug.AssertIf(energy0 == null, forces0 == null); energy0 = 0; coords0 = null; forces0 = null; for (int i = 0; i < size; i++) { forces[i] = new double[3]; } for (int i = 0; i < size; i++) { updated[i] = true; } } else { HDebug.Assert(size == coords0.Length); HDebug.Assert(size == forces0.Length); HDebug.Assert(size == coords.Length); HDebug.Assert(size == forces.Length); for (int i = 0; i < size; i++) { forces[i] = forces0[i].Clone(); } int countskip = 0; //double[] dist2s = new double[size]; //for(int i=0; i<size; i++) // dist2s[i] = (coords0[i] - coords[i]).Dist2; //int[] idxsorted = dist2s.IdxSorted(); //for(int i=0; i<size; i++) // updated[i] = (dist2s[i] > (0.000001*0.000001)); //for(int i=size/2; i<size; i++) // updated[idxsorted[i]] = true; for (int i = 0; i < size; i++) { updated[i] = (coords0[i] != coords[i]); } for (int i = 0; i < size; i++) { countskip += (updated[i] == false) ? 1 : 0; } if ((size - countskip) * 10 > size) { energy0 = 0; coords0 = null; forces0 = null; for (int i = 0; i < size; i++) { forces[i] = new double[3]; } for (int i = 0; i < size; i++) { updated[i] = true; } } System.Console.Write(" countskip({0:000}) ", countskip); } Vector[] dforces = GetVectorsZero(); double denergy = 0; denergy += GetPotentialUpdated_ComputeCustomsBond(frcflds, updated, 0, coords0, coords, dforces); denergy += GetPotentialUpdated_ComputeCustomsAngle(frcflds, updated, 0, coords0, coords, dforces); denergy += GetPotentialUpdated_ComputeCustomsDihedral(frcflds, updated, 0, coords0, coords, dforces); denergy += GetPotentialUpdated_ComputeCustomsImproper(frcflds, updated, 0, coords0, coords, dforces); denergy += GetPotentialUpdated_ComputeCustomsNonbonded(frcflds, updated, 0, coords0, coords, dforces, ref nonbondeds); //energy += GetPotentialUpdated_ComputeCustomsCustoms (frcflds, updated, 0, coords0, forces0, coords, forces); double energy = energy0.Value + denergy; for (int i = 0; i < size; i++) { forces[i] += dforces[i]; } #region commented from threading //Nonbondeds lnonbondeds = nonbondeds; // //double energy = energy0.Value; // //object lockobj = new object(); //System.Threading.Tasks.ParallelOptions parallelOptions = new ParallelOptions(); ////parallelOptions.MaxDegreeOfParallelism = 1; //Parallel.ForEach(compunits_bonded, parallelOptions, delegate(GetForcesCompUnit compunit) //{ // if(compunit == null) // { // // in the first iteration, collect nonbonded components // GetForces_CollectCompUnitNonbonded(frcflds, updated, coords0, coords, ref lnonbondeds, compunits_nonbonded); // } // else // { // Triple<double, int[], Vector[]> denergy_idx_dforces = compunit.Compute(coords0, coords); // double denergy = denergy_idx_dforces.first; // int[] idx = denergy_idx_dforces.second; // Vector[] dforces = denergy_idx_dforces.third; // Debug.Assert(idx.Length == dforces.Length); // lock(lockobj) // { // energy += denergy; // for(int i=0; i<idx.Length; i++) // forces[idx[i]] += dforces[i]; // } // } //}); //nonbondeds = lnonbondeds; //Parallel.ForEach(compunits_nonbonded, parallelOptions, delegate(GetForcesCompUnit compunit) //{ // Triple<double, int[], Vector[]> denergy_idx_dforces = compunit.Compute(coords0, coords); // double denergy = denergy_idx_dforces.first; // int[] idx = denergy_idx_dforces.second; // Vector[] dforces = denergy_idx_dforces.third; // Debug.Assert(idx.Length == dforces.Length); // lock(lockobj) // { // energy += denergy; // for(int i=0; i<idx.Length; i++) // forces[idx[i]] += dforces[i]; // } //}); #endregion if (HDebug.IsDebuggerAttachedWithProb(GetPotentialUpdated_ProbToCheckWithGetPotential)) #region check force with GetPotential(...) { //Vector[] _forces0 = GetVectorsZero(); //Matrix _hessian0 = null; //double _energy0 = GetPotential(frcflds, coords0, ref _forces0, ref _hessian0, new Dictionary<string,object>()); //Debug.AssertTolerance(0.00000001, energy0 - _energy0); //Debug.AssertTolerance(0.00000001, Vector.Sub(forces0, _forces0)); Vector[] _forces = GetVectorsZero(); MatrixByArr _hessian = null; double _energy = GetPotential(frcflds, coords, ref _forces, ref _hessian, new Dictionary <string, object>()); HDebug.AssertTolerance(0.00000001, energy - _energy); HDebug.AssertToleranceVector(0.00000001, Vector.Sub(forces, _forces)); } #endregion //if(cache != null) //{ // if(cache.ContainsKey("energy_bonds ") == false) cache.Add("energy_bonds ", 0); cache["energy_bonds "] = energy_bonds ; // if(cache.ContainsKey("energy_angles ") == false) cache.Add("energy_angles ", 0); cache["energy_angles "] = energy_angles ; // if(cache.ContainsKey("energy_dihedrals ") == false) cache.Add("energy_dihedrals ", 0); cache["energy_dihedrals "] = energy_dihedrals ; // if(cache.ContainsKey("energy_impropers ") == false) cache.Add("energy_impropers ", 0); cache["energy_impropers "] = energy_impropers ; // if(cache.ContainsKey("energy_nonbondeds") == false) cache.Add("energy_nonbondeds", 0); cache["energy_nonbondeds"] = energy_nonbondeds; // if(cache.ContainsKey("energy_customs ") == false) cache.Add("energy_customs ", 0); cache["energy_customs "] = energy_customs ; //} return(energy); }