/////////////////////////// public void Add2(int id0, int id1, Vector[] lcoords, Vector[] lforces) { if (forceij == null) { return; } HDebug.Assert(lcoords.Length == 2); HDebug.Assert(lforces.Length == 2); if ((lforces[0].Dist2 == 0) && (lforces[1].Dist2 == 0)) { return; } if (HDebug.IsDebuggerAttached) { // check net force HDebug.AssertTolerance(0.0001, lforces[0] + lforces[1]); Vector diff = lcoords[1] - lcoords[0]; HDebug.AssertTolerance(0.0001, LinAlg.Angle(diff, lforces[0])); HDebug.AssertTolerance(0.0001, LinAlg.Angle(diff, lforces[1])); HDebug.AssertTolerance(0.0001, LinAlg.Angle(lforces[0], lforces[1])); } this[id0, id1] += lforces[0]; this[id1, id0] += lforces[1]; }
public static void GetModes(Matrix hess, out Matrix modes, out Vector freqs) { HDebug.Depreciated("use others"); HDebug.Assert(GetModesSelftest()); HDebug.Assert(hess.RowSize == hess.ColSize); Matrix eigvec; Vector eigval; NumericSolver.Eig(hess.ToArray(), out eigvec, out eigval); int n = hess.RowSize; modes = new double[n, n - 6]; freqs = new double[n - 6]; for (int r = 0; r < 6; r++) { HDebug.AssertTolerance(0.00000001, eigval[r]); } for (int r = 6; r < n; r++) { int rr = r - 6; freqs[rr] = eigval[r]; for (int c = 0; c < n; c++) { modes[c, rr] = eigvec[c, r]; } } }
public HessMatrix ReshapeByAtomImpl0(IList <int> idxatms, bool ignNegIdx) { HDebug.Assert(idxatms.Count == idxatms.HUnion().Length); // check no-duplication of indexes HessMatrix reshape = Zeros(idxatms.Count * 3, idxatms.Count * 3); for (int nbc = 0; nbc < idxatms.Count; nbc++) { for (int nbr = 0; nbr < idxatms.Count; nbr++) { int bc = idxatms[nbc]; if (ignNegIdx && bc < 0) { continue; } int br = idxatms[nbr]; if (ignNegIdx && br < 0) { continue; } if (HasBlock(bc, br) == false) { if (HDebug.IsDebuggerAttached) { if (GetBlock(bc, br) != null) { HDebug.AssertTolerance(0, GetBlock(bc, br).ToArray()); } } continue; } MatrixByArr blkrc = GetBlock(bc, br); reshape.SetBlock(nbc, nbr, blkrc.CloneT()); } } return(reshape); }
public static Vector GetFrcByFij(IList <Vector> coords, double[,] F, Vector frc = null) { int size = coords.Count; if (frc == null) { frc = new double[size * 3]; } /// Parallel.For(0, size, delegate(int i) for (int i = 0; i < size; i++) { for (int j = i; j < size; j++) { if (i == j) { continue; } if (F[i, j] == 0) { continue; } HDebug.AssertTolerance(0.00000001, F[i, j] - F[j, i]); Vector frc_i = GetFrciByFij(coords[i], coords[j], F[i, j]); int i3 = i * 3; int j3 = j * 3; frc[i3 + 0] += frc_i[0]; frc[j3 + 0] += -1 * frc_i[0]; frc[i3 + 1] += frc_i[1]; frc[j3 + 1] += -1 * frc_i[1]; frc[i3 + 2] += frc_i[2]; frc[j3 + 2] += -1 * frc_i[2]; } } /// ); return(frc); }
public Mode GetNormalized() { if (HDebug.Selftest()) { Mode lmode0 = new Mode { eigval = 0.123, eigvec = new double[] { 1, 2, 3, 2, 3, 4, 3, 4, 5 } }; Mode lmode1 = lmode0.GetNormalized(); HDebug.Assert(lmode0.eigvec.Size == lmode1.eigvec.Size); MatrixByArr lhess0 = new double[lmode0.eigvec.Size, lmode0.eigvec.Size]; MatrixByArr lhess1 = new double[lmode1.eigvec.Size, lmode1.eigvec.Size]; lmode0.GetHessian(lhess0); lmode1.GetHessian(lhess1); double tolbase = Math.Max(lhess0.ToArray().HMax(), lhess1.ToArray().HMax()); HDebug.AssertTolerance(tolbase * 0.00000001, lhess0 - lhess1); } /// H = sum vi' * di * vi /// = sum (vi/|vi|)' * (di |vi|^2) * (vi/|vi|) double leigvec = eigvec.Dist; Vector neigvec = eigvec.UnitVector(); return(new Mode { th = th, eigval = eigval * leigvec * leigvec, eigvec = neigvec }); }
public static void SelfTest() { if (HDebug.Selftest()) { Random rand = new Random(0); int colblksize = 3; int rowblksize = 10; int layersize = 3; var mat = Matrix.Zeros(colblksize * 3, rowblksize * 3); var hess = new HessMatrixLayeredArray(colblksize * 3, rowblksize * 3, layersize); HDebug.Assert(mat.ColSize == hess.ColSize); HDebug.Assert(mat.RowSize == hess.RowSize); int count = mat.ColSize * mat.RowSize * 10; for (int i = 0; i < count; i++) { int c = rand.NextInt(0, colblksize * 3 - 1); int r = rand.NextInt(0, rowblksize * 3 - 1); double v = rand.NextDouble(); mat[c, r] = v; hess[c, r] = v; HDebug.AssertTolerance(double.Epsilon, (mat - hess).ToArray()); } for (int i = 0; i < 700; i++) { int c = rand.NextInt(0, colblksize * 3 - 1); int r = rand.NextInt(0, rowblksize * 3 - 1); double v = 0; mat[c, r] = v; hess[c, r] = v; HDebug.AssertTolerance(double.Epsilon, (mat - hess).ToArray()); } } }
public static double Corr(Vector bfactor1, Vector bfactor2, bool ignore_nan = false) { double hcorr = HMath.HCorr(bfactor1, bfactor2, ignore_nan); if (HDebug.IsDebuggerAttached) { double corr = double.NaN; using (new Matlab.NamedLock("CORR")) { Matlab.Clear("CORR"); Matlab.PutVector("CORR.bfactor1", bfactor1); Matlab.PutVector("CORR.bfactor2", bfactor2); if (ignore_nan) { Matlab.Execute("CORR.idxnan = isnan(CORR.bfactor1) | isnan(CORR.bfactor2);"); Matlab.Execute("CORR.bfactor1 = CORR.bfactor1(~CORR.idxnan);"); Matlab.Execute("CORR.bfactor2 = CORR.bfactor2(~CORR.idxnan);"); } if (Matlab.GetValueInt("min(size(CORR.bfactor1))") != 0) { corr = Matlab.GetValue("corr(CORR.bfactor1, CORR.bfactor2)"); } Matlab.Clear("CORR"); } if ((double.IsNaN(hcorr) && double.IsNaN(corr)) == false) { HDebug.AssertTolerance(0.00000001, hcorr - corr); } //HDebug.ToDo("use HMath.HCorr(...) instead"); } return(hcorr); }
public void GetHessian(MatrixByArr hess) { if (HDebug.Selftest()) { Mode tmode = new Mode(); tmode.eigval = 2; tmode.eigvec = new double[] { 1, 2, 3 }; MatrixByArr thess0 = new double[, ] { { 2, 4, 6 } , { 4, 8, 12 } , { 6, 12, 18 } }; MatrixByArr thess1 = new double[3, 3]; tmode.GetHessian(thess1); HDebug.AssertTolerance(0.00000001, thess0 - thess1); } HDebug.Assert(hess.RowSize == eigvec.Size, hess.ColSize == eigvec.Size); //unsafe { double[] pvec = eigvec._data; { for (int c = 0; c < eigvec.Size; c++) { for (int r = 0; r < eigvec.Size; r++) { hess[c, r] += eigval * pvec[c] * pvec[r]; } } } } }
public static double GetPotentialUpdated_Compute <FFUNIT>(FFUNIT ffUnit, GetPotentialUpdated_ComputeFunc <FFUNIT> ffCompute , Vector[] coords0, Vector[] coords , Vector[] dforces , int[] buffIdx, Vector[] buffCoords, Vector[] buffForces , bool compOld, bool compNew ) where FFUNIT : Universe.AtomPack { int length = ffUnit.atoms.Length; int [] idx = buffIdx; for (int i = 0; i < length; i++) { idx[i] = ffUnit.atoms[i].ID; } Vector[] lcoords = buffCoords; Vector[] lforces = buffForces; MatrixByArr[,] lhessian = null; double denergy = 0; if (compNew) //(compOpt == CompOpt.compBothOldNew || compOpt == CompOpt.compOnlyNew) { double lenergy = 0; for (int i = 0; i < length; i++) { lcoords[i] = coords[idx[i]]; } for (int i = 0; i < length; i++) { lforces[i].SetZero(); } ffCompute(ffUnit, lcoords, ref lenergy, ref lforces, ref lhessian); denergy += lenergy; HDebug.AssertTolerance(0.00000001, lforces.Take(length).Mean()); for (int i = 0; i < length; i++) { dforces[idx[i]] += lforces[i]; } } if (compOld && coords0 != null) //(coords0 != null && (compOpt == CompOpt.compBothOldNew || compOpt == CompOpt.compOnlyOld)) { double lenergy = 0; for (int i = 0; i < length; i++) { lcoords[i] = coords0[idx[i]]; } for (int i = 0; i < length; i++) { lforces[i].SetZero(); } ffCompute(ffUnit, lcoords, ref lenergy, ref lforces, ref lhessian); denergy -= lenergy; HDebug.AssertTolerance(0.00000001, lforces.Take(length).Mean()); for (int i = 0; i < length; i++) { dforces[idx[i]] -= lforces[i]; } } return(denergy); }
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 HessMatrix GetHessByKijFij(IList <Vector> coords, double[,] K, double[,] F, HessMatrix hess = null) { int size = coords.Count; if (hess == null) { hess = HessMatrixDense.ZerosDense(size * 3, size * 3); } if (K == null) { K = new double[size, size]; } if (F == null) { F = new double[size, size]; } /// Parallel.For(0, size, delegate(int i) for (int i = 0; i < size; i++) { for (int j = i; j < size; j++) { if (i == j) { continue; } if (K[i, j] == 0 && F[i, j] == 0) { continue; } MatrixByArr hessij = GetHessByKijFij(coords[i], coords[j], K[i, j], F[i, j]); HDebug.AssertTolerance(0.00000001, K[i, j] - K[j, i]); HDebug.AssertTolerance(0.00000001, F[i, j] - F[j, i]); if (HDebug.IsDebuggerAttached) { HDebug.AssertTolerance(0.00000001, hessij - GetHessByKijFij(coords[j], coords[i], K[j, i], F[j, i])); } int n0 = i * 3; int n1 = j * 3; for (int di = 0; di < 3; di++) { for (int dj = 0; dj < 3; dj++) { hess[n0 + di, n0 + dj] -= hessij[di, dj]; hess[n0 + di, n1 + dj] += hessij[di, dj]; hess[n1 + di, n0 + dj] += hessij[di, dj]; hess[n1 + di, n1 + dj] -= hessij[di, dj]; } } } } /// ); return(hess); }
public double GetPotentialBonds(List <ForceField.IForceField> frcflds, Vector[] coords, ref Vector[] forces, ref MatrixByArr[,] hessian, Dictionary <string, object> cache, PwForceDecomposer forceij, double[,] pwfrc = null, double[,] pwspr = null) { List <ForceField.IBond> frcfld_bonds = SelectInFrcflds(frcflds, new List <ForceField.IBond>()); double energy = 0; if (frcfld_bonds.Count == 0) { return(energy); } double stat_min = double.MaxValue; double stat_max = double.MinValue; double netstat_min = double.MaxValue; double netstat_max = double.MinValue; Vector[] lcoords = new Vector[2]; Vector[] lforces = (forces == null) ? null : new Vector[2]; for (int i = 0; i < bonds.Count; i++) { int id0 = bonds[i].atoms[0].ID; lcoords[0] = coords[id0]; int id1 = bonds[i].atoms[1].ID; lcoords[1] = coords[id1]; foreach (ForceField.IBond frcfld in frcfld_bonds) { if (forces != null) { lforces[0] = new double[3]; lforces[1] = new double[3]; } MatrixByArr[,] lhess = (hessian == null) ? null : LinAlg.CreateMatrixArray(2, 2, new double[3, 3]); frcfld.Compute(bonds[i], lcoords, ref energy, ref lforces, ref lhess, pwfrc, pwspr); HDebug.Assert(double.IsNaN(energy) == false, double.IsInfinity(energy) == false); if (forces != null) { forces[id0] += lforces[0]; forces[id1] += lforces[1]; forceij.AddBond(id0, id1, lcoords, lforces); HDebug.AssertTolerance(0.00000001, lforces[0].Dist2 - lforces[1].Dist2); double netstat = lforces[0].Dist2 + lforces[1].Dist2; netstat_min = Math.Min(netstat_min, netstat); netstat_max = Math.Max(netstat_max, netstat); stat_min = Math.Min(stat_min, lforces[0].Dist); stat_max = Math.Max(stat_max, lforces[0].Dist); stat_min = Math.Min(stat_min, lforces[1].Dist); stat_max = Math.Max(stat_max, lforces[1].Dist); } if (hessian != null) { hessian[id0, id0] += lhess[0, 0]; hessian[id0, id1] += lhess[0, 1]; hessian[id1, id0] += lhess[1, 0]; hessian[id1, id1] += lhess[1, 1]; } } } return(energy); }
public static double[,] ModeContribToBFactorCorr(IList <Mode> modes1, IList <Mode> modes2) { /// Similar to "double[] ModeContribToBFactorCorr(bfactor1, modes2)", /// the correlation between bfactor1 and bfactor 2 can be decomposed as /// the list of correlation contributions by "normalized bfactor by modes1.m_i" /// and "normalized bfactor by modes2.m_j" /// in a matrix form. /// /// therefore, the correlation of bfactor1 and bfactor(modes2) is determined as their normalized dot-product: /// corr = dot(nbfactor1, nbfactor2) / (n-1) /// = 1/(n-1) * dot(nbfactor1, nmwbfactor(modes2.m1) + nmwbfactor(modes2.m2) + ... ) /// = 1/(n-1) * ( dot(nbfactor1, nmwbfactor(modes2.m1)) + dot(nbfactor1, nmwbfactor(modes2.m2)) + ... ) /// = 1/(n-1) * ( dot(nmwbfactor(modes1.m1), nmwbfactor(modes2.m1)) + dot(nmwbfactor(modes1.m1), nmwbfactor(modes2.m2)) + ... /// dot(nmwbfactor(modes1.m2), nmwbfactor(modes2.m1)) + dot(nmwbfactor(modes1.m2), nmwbfactor(modes2.m2)) + ... ) /// = 1/(n-1) * sum_{ i1=1..m1, i2=1..m2 } dot(nmwbfactor(modes1.m_i), nmwbfactor(modes2.m_j)) /// = sum_{ i1=1..m1, i2=1..m2 } dot(nmwbfactor(modes1.m_i), nmwbfactor(modes2.m_j))/(n-1) /// = sum_{ i1=1..m1, i2=1..m2 } "correlation contribution by modes1.m_i and modes2.m_j" /// Vector[] nbfactor1mw = GetBFactorModewiseNormalized(modes1); Vector[] nbfactor2mw = GetBFactorModewiseNormalized(modes2); int n = nbfactor1mw[0].Size; int m1 = modes1.Count; HDebug.Assert(nbfactor1mw.Length == m1); int m2 = modes2.Count; HDebug.Assert(nbfactor2mw.Length == m2); MatrixByArr contrib = new double[m1, m2]; for (int i1 = 0; i1 < m1; i1++) { for (int i2 = 0; i2 < m2; i2++) { HDebug.Assert(nbfactor1mw[i1].Size == n); HDebug.Assert(nbfactor2mw[i2].Size == n); contrib[i1, i2] = LinAlg.VtV(nbfactor1mw[i1], nbfactor2mw[i2]) / (n - 1); } } if (HDebug.IsDebuggerAttached) { Vector bfactor1 = modes1.GetBFactor().ToArray(); Vector nbfactor1 = GetBFactorNormalized(bfactor1); Vector bfactor2 = modes2.GetBFactor().ToArray(); Vector nbfactor2 = GetBFactorNormalized(bfactor2); double corr0 = HMath.HCorr(bfactor1, bfactor2); double corr1 = LinAlg.VtV(nbfactor1, nbfactor2) / (n - 1); double corr2 = contrib.ToArray().HSum(); HDebug.AssertTolerance(0.00000001, corr0 - corr1, corr0 - corr2, corr1 - corr2); } return(contrib); }
public static double[] ModeContribToBFactorCorr(Vector bfactor1, IList <Mode> modes2) { /// the correlation(corr) of two bfactors (bfactor1, bfactor 2) is the same to /// the dot product of their normalizeds (mean 0, variance 1): /// corr = Math.HCorr( bfactor1, bfactor2) /// = Math.HCorr(nbfactor1, nbfactor2) /// = LinAlg.VtV(nbfactor1, nbfactor2) (for biased estimation) /// = LinAlg.VtV(nbfactor1, nbfactor2) / (n-1) (for unbiased estimation) /// /// bfactor of mode(m1,m2,...) can be determined by the sum of bfactors of each mode: /// bfactor(mode) = bfactor(m1) + bfactor(m2) + ... /// /// in the similar manner, the normalized bfactor of mode(m1,m2,...) is determined by /// sum of normalized-modewise-bfactor (GetBFactorModewiseNormalized): /// normal-bfactor(mode) = nmwbfactor(m1) + nmwbfactor(m2) + ... /// /// therefore, the correlation of bfactor1 and bfactor(modes2) is determined as their normalized dot-product: /// corr = dot(nbfactor1, nbfactor2) / (n-1) /// = dot(nbfactor1, nmwbfactor(modes2.m1) + nmwbfactor(modes2.m2) + ... ) / (n-1) /// = dot(nbfactor1, nmwbfactor(modes2.m1))/(n-1) + dot(nbfactor1, nmwbfactor(modes2.m2)) / (n-1) + ... /// Vector nbfactor1 = GetBFactorNormalized(bfactor1); Vector[] nbfactor2mw = GetBFactorModewiseNormalized(modes2); int n = bfactor1.Size; int m = modes2.Count; HDebug.Assert(nbfactor2mw.Length == m); Vector contrib = new double[m]; for (int i = 0; i < m; i++) { contrib[i] = LinAlg.VtV(nbfactor1, nbfactor2mw[i]) / (n - 1); } if (HDebug.IsDebuggerAttached) { Vector bfactor2 = modes2.GetBFactor().ToArray(); Vector nbfactor2 = GetBFactorNormalized(bfactor2); double corr0 = HMath.HCorr(bfactor1, bfactor2); double corr1 = LinAlg.VtV(nbfactor1, nbfactor2) / (n - 1); double corr2 = contrib.Sum(); HDebug.AssertTolerance(0.00000001, corr0 - corr1, corr0 - corr2, corr1 - corr2); } return(contrib); }
static void UpdateMassWeightedHess(Matrix hess, Vector mass) { if (HDebug.Selftest()) //if(GetMassWeightedHess_selftest1) #region selftest { //HDebug.ToDo("replace examplt not to use blocked hessian matrix"); //GetMassWeightedHess_selftest1 = false; MatrixByArr[,] _bhess = new MatrixByArr[2, 2]; _bhess[0, 0] = new double[3, 3] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; _bhess[0, 1] = _bhess[0, 0] + 10; _bhess[1, 0] = _bhess[0, 0] + 20; _bhess[1, 1] = _bhess[0, 0] + 30; Vector _mass = new double[2] { 2, 3 }; MatrixByArr _hess = MatrixByArr.FromMatrixArray(_bhess); Matrix _mwhess = GetMassWeightedHess(_hess, _mass); MatrixByArr[,] _mwbhess = GetMassWeightedHess(_bhess, _mass); HDebug.AssertTolerance(0.00000001, MatrixByArr.FromMatrixArray(_mwbhess) - _mwhess.ToArray()); } #endregion HDebug.Exception(hess.ColSize == mass.Size); HDebug.Assert(hess.ColSize == hess.RowSize); HDebug.Assert(hess.ColSize % 3 == 0); Vector mass05 = mass.ToArray().HSqrt(); // mass weighted hessian // MH = M^(-1/2) * H * M^(-1/2) // MH_ij = H_IJ * sqrt(M[i] * M[j]) { // mass weighted block hessian for (int i = 0; i < hess.ColSize; i++) { for (int j = 0; j < hess.RowSize; j++) { //if(i == j) continue; hess[i, j] = hess[i, j] / (mass05[i] * mass05[j]); //mbhess[i, i] -= mbhess[i, j]; } } } }
public HessMatrix SubMatrixByAtomsImpl(bool ignNegIdx, IList <int> idxAtoms) { if (SubMatrixByAtomsImpl_selftest) { SubMatrixByAtomsImpl_selftest = false; Vector[] tcoords = new Vector[] { new Vector(1, 2, 3), new Vector(1, 3, 2), new Vector(1, 2, 9), }; HessMatrix thess0 = Hess.GetHessAnm(tcoords); int[] tidxs = new int[] { 0, 2 }; HessMatrix thess1a = thess0.SubMatrixByAtoms(false, tidxs); HessMatrix thess1b = new double[, ] { { thess0[0, 0], thess0[0, 1], thess0[0, 2], thess0[0, 6], thess0[0, 7], thess0[0, 8] }, { thess0[1, 0], thess0[1, 1], thess0[1, 2], thess0[1, 6], thess0[1, 7], thess0[1, 8] }, { thess0[2, 0], thess0[2, 1], thess0[2, 2], thess0[2, 6], thess0[2, 7], thess0[2, 8] }, { thess0[6, 0], thess0[6, 1], thess0[6, 2], thess0[6, 6], thess0[6, 7], thess0[6, 8] }, { thess0[7, 0], thess0[7, 1], thess0[7, 2], thess0[7, 6], thess0[7, 7], thess0[7, 8] }, { thess0[8, 0], thess0[8, 1], thess0[8, 2], thess0[8, 6], thess0[8, 7], thess0[8, 8] }, }; // thess1a = Hess.CorrectHessDiag(thess1a); // diagonal of original matrix contains the interaction between 0-1 and 1-2 also, // HessMatrix thess1b = Hess.GetHessAnm(tcoords.HSelectByIndex(tidxs)); // while new generated hessian matrix does not. Matrix tdiffhess = thess1a - thess1b; double max_tdiffhess = tdiffhess.ToArray().HAbs().HMax(); HDebug.Exception(0 == max_tdiffhess); } HessMatrix nhess = SubMatrixByAtomsImpl(ignNegIdx, idxAtoms, idxAtoms, true); if (HDebug.IsDebuggerAttached && idxAtoms.Count < 1000) { List <int> idx3Atoms = new List <int>(); foreach (int idx in idxAtoms) { for (int i = 0; i < 3; i++) { idx3Atoms.Add(idx * 3 + i); } } Matrix tnhess = this.SubMatrix(idx3Atoms, idx3Atoms); double max2_tdiffhess = (nhess - tnhess).ToArray().HAbs().HMax(); HDebug.AssertTolerance(0.00000001, max2_tdiffhess); } return(nhess); }
public static MatrixByArr GetJ(Universe univ, Vector[] coords, List <RotableInfo> rotInfos, string option = null) { switch (option) { case "mine": { MatrixByArr[,] J = TNM_mine.GetJ(univ, coords, rotInfos, fnInv3x3: null); double tolerance = 0.00001; HDebug.Assert(CheckEckartConditions(univ, coords, rotInfos, J, tolerance: tolerance)); //if(Debug.IsDebuggerAttachedWithProb(0.1)) //{ // Matrix J0 = Matrix.FromBlockmatrix(J); // Matrix J1 = Matrix.FromBlockmatrix(GetJ(univ, coords, rotInfos, option:"paper", useNamedLock:false)); // Debug.AssertTolerance(0.00000001, J0 - J1); //} return(MatrixByArr.FromMatrixArray(J)); } case "paper": { MatrixByArr[,] J = TNM_paper.GetJ(univ, coords, rotInfos); HDebug.Assert(CheckEckartConditions(univ, coords, rotInfos, J, 0.0000001)); return(MatrixByArr.FromMatrixArray(J)); } case "mineopt": { MatrixByArr J = TNM_mineopt.GetJ(univ, coords, rotInfos); if (HDebug.IsDebuggerAttached && univ.GetMolecules().Length == 1) { MatrixByArr J0 = TNM.GetJ(univ, coords, rotInfos, option: "paper"); MatrixByArr dJ = J - J0; //double tolerance = dJ.ToArray().HAbs().HToArray1D().Mean(); //double maxAbsDH = dJ.ToArray().HAbs().HMax(); //if(tolerance == 0) // tolerance = 0.000001; HDebug.AssertTolerance(0.00000001, dJ); } return(J); } case null: // my implementation has smaller error while checking Eckart conditions goto case "mineopt"; } return(null); }
public static IList <Atom> CloneByReindexByCoords(this IList <Atom> atoms, IList <Vector> coords, int serialStart = 1) { KDTree.KDTree <object> kdtree = new KDTree.KDTree <object>(3); for (int i = 0; i < coords.Count; i++) { kdtree.insert(coords[i], i); } Atom[] natoms = new Atom[atoms.Count]; for (int ai = 0; ai < natoms.Length; ai++) { Atom atom = atoms[ai]; Vector coord = atom.coord; int ni = (int)(kdtree.nearest(coord)); Vector ncoord = coords[ni]; double dist = (coord - ncoord).Dist; HDebug.AssertTolerance(0.001, dist); HDebug.Assert(natoms[ni] == null); natoms[ni] = Atom.FromData(serial: serialStart + ni , name: atom.name , resName: atom.resName , chainID: atom.chainID , resSeq: atom.resSeq , x: atom.x , y: atom.y , z: atom.z , altLoc: atom.altLoc , iCode: atom.iCode , occupancy: atom.occupancy , tempFactor: atom.tempFactor , element: atom.element , charge: atom.charge ); } if (HDebug.IsDebuggerAttached) { for (int i = 0; i < natoms.Length; i++) { HDebug.Assert(natoms[i] != null); } } return(natoms); }
public static double[] GetBFactor(Mode[] modes, double[] mass = null) { if (HDebug.Selftest()) #region selftest { using (new Matlab.NamedLock("SELFTEST")) { Matlab.Clear("SELFTEST"); Matlab.Execute("SELFTEST.hess = rand(30);"); Matlab.Execute("SELFTEST.hess = SELFTEST.hess + SELFTEST.hess';"); Matlab.Execute("SELFTEST.invhess = inv(SELFTEST.hess);"); Matlab.Execute("SELFTEST.bfactor3 = diag(SELFTEST.invhess);"); Matlab.Execute("SELFTEST.bfactor = SELFTEST.bfactor3(1:3:end) + SELFTEST.bfactor3(2:3:end) + SELFTEST.bfactor3(3:3:end);"); MatrixByArr selftest_hess = Matlab.GetMatrix("SELFTEST.hess"); Mode[] selftest_mode = Hess.GetModesFromHess(selftest_hess); Vector selftest_bfactor = BFactor.GetBFactor(selftest_mode); Vector selftest_check = Matlab.GetVector("SELFTEST.bfactor"); Vector selftest_diff = selftest_bfactor - selftest_check; HDebug.AssertTolerance(0.00000001, selftest_diff); Matlab.Clear("SELFTEST"); } } #endregion int size = modes[0].size; if (mass != null) { HDebug.Assert(size == mass.Length); } double[] bfactor = new double[size]; for (int i = 0; i < size; i++) { foreach (Mode mode in modes) { bfactor[i] += mode.eigvec[i * 3 + 0] * mode.eigvec[i * 3 + 0] / mode.eigval; bfactor[i] += mode.eigvec[i * 3 + 1] * mode.eigvec[i * 3 + 1] / mode.eigval; bfactor[i] += mode.eigvec[i * 3 + 2] * mode.eigvec[i * 3 + 2] / mode.eigval; } if (mass != null) { bfactor[i] /= mass[i]; } } return(bfactor); }
public static bool CheckEckartConditions(Universe univ, List <RotableInfo> rotInfos, MatrixByArr[,] J) { int n = univ.atoms.Count; int m = rotInfos.Count; // for (int a = 0; a < m; a++) { MatrixByArr mJ = new double[3, 1]; for (int i = 0; i < n; i++) { mJ += univ.atoms[i].Mass * J[i, a]; } HDebug.AssertTolerance(0.00001, mJ); } return(true); }
public Vector GetMasses(int dim = 1) { Vector masses = atoms.ToArray().GetMasses(dim); if (HDebug.IsDebuggerAttached) { double[] tmasses = new double[size * dim]; for (int i = 0; i < size; i++) { for (int j = 0; j < dim; j++) { tmasses[i * dim + j] = atoms[i].Mass; } } HDebug.AssertTolerance(0, masses - tmasses); } return(masses); }
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 Universe Build(string xyzpath, bool loadxyzLatest, string prmpath, string pdbpath) { Tinker.Xyz xyz = Tinker.Xyz.FromFile(xyzpath, false); Tinker.Xyz xyz1 = Tinker.Xyz.FromFile(xyzpath, loadxyzLatest); Tinker.Prm prm = Tinker.Prm.FromFile(prmpath); Pdb pdb = Pdb.FromFile(pdbpath); Universe univ = Build(xyz, prm, pdb, 0.002); if (HDebug.IsDebuggerAttached) { HDebug.Assert(xyz.atoms.Length == univ.size); Vector[] xyzcoords = xyz.atoms.HListCoords(); Vector[] unvcoords = univ.GetCoords(); for (int i = 0; i < univ.size; i++) { Vector dcoord = xyzcoords[i] - unvcoords[i]; HDebug.AssertTolerance(0.00000001, dcoord); } } univ.SetCoords(xyz1.atoms.HListCoords()); return(univ); }
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 MatrixByArr HessSpring(IList <Vector> coords, double K) { if (HessSpring_selftest) { HessSpring_selftest = false; HTLib3.Vector[] coords0 = new HTLib3.Vector[] { coords[0].ToArray(), coords[1].ToArray(), coords[2].ToArray(), coords[3].ToArray(), }; MatrixByArr HSpr0 = HTLib3.Bioinfo.Hess._HessTorSpr(coords0, K).ToArray(); MatrixByArr HSpr1 = HessSpring(coords, K); HDebug.AssertTolerance(0.00000001, HSpr0 - HSpr1); } Vector dpi_dr = dPI_dR(coords); MatrixByArr Hspr = K * LinAlg.VVt(dpi_dr, dpi_dr); return(Hspr); }
public static Vector[] GetBFactorModewise(IList <Mode> modes) { /// The "b-factor of modes (m1,m2,m3,...)" is the same to /// the "b-factor of m1" + "b-factor of m2" + "b-factor of m3" + ... /// int m = modes.Count; Vector[] mwbfactors = new Vector[m]; for (int i = 0; i < m; i++) { mwbfactors[i] = (new Mode[] { modes[i] }).GetBFactor().ToArray(); } if (HDebug.IsDebuggerAttached) { Vector bfactor0 = modes.GetBFactor().ToArray(); Vector bfactor1 = mwbfactors.HSum(); HDebug.AssertTolerance(0.00000001, bfactor0 - bfactor1); } return(mwbfactors); }
public static Vector GetBFactorNormalized(Vector bfactor) { /// nbfactor (normalized bfactor) has mean 0 /// and variance 1. /// The correlation (corr) of two normalized bfactors (nbfactor1, nbfactor2) is the dot product of them: /// corr = Math.HCorr( bfactor1, bfactor2) /// = Math.HCorr(nbfactor1, nbfactor2) /// = LinAlg.VtV(nbfactor1, nbfactor2) (for biased estimation) /// = LinAlg.VtV(nbfactor1, nbfactor2) / (n-1) (for unbiased estimation) /// double avg = bfactor.HAvg(); double var = bfactor.HVar(); double std = Math.Sqrt(var); int n = bfactor.Size; Vector nbfactor = new double[n]; for (int i = 0; i < n; i++) { nbfactor[i] = (bfactor[i] - avg) / std; } HDebug.AssertTolerance(0.00000001, nbfactor.HAvg()); HDebug.AssertTolerance(0.00000001, nbfactor.HVar() - 1); return(nbfactor); }
public static HessMatrix GetHessFixDiag(HessMatrix hess) { int size = hess.ColSize / 3; HDebug.Assert(size * 3 == hess.ColSize, size * 3 == hess.RowSize); HessMatrix fix = hess.Zeros(size * 3, size * 3); for (int c = 0; c < size; c++) { for (int r = 0; r < size; r++) { if (c == r) { continue; } double v; HDebug.AssertTolerance(0.00000001, hess[c * 3 + 0, r * 3 + 0] - hess[r * 3 + 0, c * 3 + 0]); HDebug.AssertTolerance(0.00000001, hess[c * 3 + 0, r * 3 + 1] - hess[r * 3 + 1, c * 3 + 0]); HDebug.AssertTolerance(0.00000001, hess[c * 3 + 0, r * 3 + 2] - hess[r * 3 + 2, c * 3 + 0]); HDebug.AssertTolerance(0.00000001, hess[c * 3 + 1, r * 3 + 0] - hess[r * 3 + 0, c * 3 + 1]); HDebug.AssertTolerance(0.00000001, hess[c * 3 + 1, r * 3 + 1] - hess[r * 3 + 1, c * 3 + 1]); HDebug.AssertTolerance(0.00000001, hess[c * 3 + 1, r * 3 + 2] - hess[r * 3 + 2, c * 3 + 1]); HDebug.AssertTolerance(0.00000001, hess[c * 3 + 2, r * 3 + 0] - hess[r * 3 + 0, c * 3 + 2]); HDebug.AssertTolerance(0.00000001, hess[c * 3 + 2, r * 3 + 1] - hess[r * 3 + 1, c * 3 + 2]); HDebug.AssertTolerance(0.00000001, hess[c * 3 + 2, r * 3 + 2] - hess[r * 3 + 2, c * 3 + 2]); v = fix[c * 3 + 0, r *3 + 0] = hess[c * 3 + 0, r *3 + 0]; fix[c * 3 + 0, c *3 + 0] -= v; HDebug.Assert(double.IsNaN(v) == false); v = fix[c * 3 + 0, r *3 + 1] = hess[c * 3 + 0, r *3 + 1]; fix[c * 3 + 0, c *3 + 1] -= v; HDebug.Assert(double.IsNaN(v) == false); v = fix[c * 3 + 0, r *3 + 2] = hess[c * 3 + 0, r *3 + 2]; fix[c * 3 + 0, c *3 + 2] -= v; HDebug.Assert(double.IsNaN(v) == false); v = fix[c * 3 + 1, r *3 + 0] = hess[c * 3 + 1, r *3 + 0]; fix[c * 3 + 1, c *3 + 0] -= v; HDebug.Assert(double.IsNaN(v) == false); v = fix[c * 3 + 1, r *3 + 1] = hess[c * 3 + 1, r *3 + 1]; fix[c * 3 + 1, c *3 + 1] -= v; HDebug.Assert(double.IsNaN(v) == false); v = fix[c * 3 + 1, r *3 + 2] = hess[c * 3 + 1, r *3 + 2]; fix[c * 3 + 1, c *3 + 2] -= v; HDebug.Assert(double.IsNaN(v) == false); v = fix[c * 3 + 2, r *3 + 0] = hess[c * 3 + 2, r *3 + 0]; fix[c * 3 + 2, c *3 + 0] -= v; HDebug.Assert(double.IsNaN(v) == false); v = fix[c * 3 + 2, r *3 + 1] = hess[c * 3 + 2, r *3 + 1]; fix[c * 3 + 2, c *3 + 1] -= v; HDebug.Assert(double.IsNaN(v) == false); v = fix[c * 3 + 2, r *3 + 2] = hess[c * 3 + 2, r *3 + 2]; fix[c * 3 + 2, c *3 + 2] -= v; HDebug.Assert(double.IsNaN(v) == false); } } if (HDebug.IsDebuggerAttached) { for (int c = 0; c < size * 3; c++) { double sum0 = 0; double sum1 = 0; double sum2 = 0; for (int r = 0; r < size * 3; r += 3) { sum0 += fix[c, r + 0]; sum1 += fix[c, r + 1]; sum2 += fix[c, r + 2]; if (c / 3 != r / 3) { HDebug.Assert(fix[c, r + 0] == hess[c, r + 0]); HDebug.Assert(fix[c, r + 1] == hess[c, r + 1]); HDebug.Assert(fix[c, r + 2] == hess[c, r + 2]); } } HDebug.AssertTolerance(0.00000001, sum0); HDebug.AssertTolerance(0.00000001, sum1); HDebug.AssertTolerance(0.00000001, sum2); } } return(fix); }
public static Matrix OverlapSigned(IList <Mode> modes1, double[] mass1, double[] mass2, IList <Mode> modes2, ILinAlg ila, bool bResetUnitVector) { Matrix mat1; Matrix mat2; string mat12opt = "memory-save"; switch (mat12opt) { case "initial": { Vector[] eigvecs1 = new Vector[modes1.Count]; for (int i = 0; i < modes1.Count; i++) { eigvecs1[i] = modes1[i].eigvec.Clone(); } Vector[] eigvecs2 = new Vector[modes2.Count]; for (int i = 0; i < modes2.Count; i++) { eigvecs2[i] = modes2[i].eigvec.Clone(); } { // 1. Hess // 2. mwHess <- M^-0.5 * H * M^-0.5 // 3. [V,D] <- eig(mwHess) // 4. mrMode <- M^-0.5 * V // // overlap: vi . vj // 1. vi <- M^0.5 * mrMode_i // 2. vj <- M^0.5 * mrMode_j // 3. vi.vj <- dot(vi,vj) // // [ 2 ] [4] [2*4] [ 8] // [ 2 ] [5] [2*5] [10] // M * v = [ 2 ] * [6] = [2*6] = [12] // [ 3 ] [7] [3*7] [21] // [ 3 ] [8] [3*8] [24] // [ 3 ] [9] [3*9] [27] // // V1 <- sqrt(M1) * V1 // V2 <- sqrt(M2) * V2 // 1. get sqrt(mass) 2. for each eigenvector 3 eigveci[j] = eigvec[j] * sqrt_mass[j/3] if (mass1 != null) { double[] mass1sqrt = mass1.HSqrt(); for (int i = 0; i < eigvecs1.Length; i++) { for (int j = 0; j < eigvecs1[i].Size; j++) { eigvecs1[i][j] *= mass1sqrt[j / 3]; } } } if (mass2 != null) { double[] mass2sqrt = mass2.HSqrt(); for (int i = 0; i < eigvecs2.Length; i++) { for (int j = 0; j < eigvecs2[i].Size; j++) { eigvecs2[i][j] *= mass2sqrt[j / 3]; } } } } if (bResetUnitVector) { for (int i = 0; i < modes1.Count; i++) { eigvecs1[i] = eigvecs1[i].UnitVector(); } for (int i = 0; i < modes2.Count; i++) { eigvecs2[i] = eigvecs2[i].UnitVector(); } } mat1 = eigvecs1.ToMatrix(false); HDebug.Assert(mat1.ColSize == eigvecs1.Length); eigvecs1 = null; GC.Collect(0); mat2 = eigvecs2.ToMatrix(true); HDebug.Assert(mat2.RowSize == eigvecs2.Length); eigvecs2 = null; GC.Collect(0); } break; case "memory-save": { int vecsize = modes1[0].eigvec.Size; HDebug.Exception(vecsize == modes1[0].eigvec.Size); HDebug.Exception(vecsize == modes2[0].eigvec.Size); //Vector[] eigvecs1 = new Vector[modes1.Count]; for(int i=0; i<modes1.Count; i++) eigvecs1[i] = modes1[i].eigvec.Clone(); //if(mass1 != null) { double[] mass1sqrt = mass1.HSqrt(); for(int i=0; i<modes1.Count; i++) for(int j=0; j<eigvecs1[i].Size; j++) eigvecs1[i][j] *= mass1sqrt[j/3]; } //if(bResetUnitVector) for(int i=0; i<modes1.Count; i++) eigvecs1[i] = eigvecs1[i].UnitVector(); //mat1 = eigvecs1.ToMatrix(false); HDebug.Assert(mat1.ColSize == modes1.Count); eigvecs1 = null; GC.Collect(0); mat1 = Matrix.Zeros(modes1.Count, vecsize); double[] mass1sqrt = null; if (mass1 != null) { mass1sqrt = mass1.HSqrt(); } for (int i = 0; i < modes1.Count; i++) { Vector eigvecs1i = modes1[i].eigvec.Clone(); if (mass1 != null) { for (int j = 0; j < eigvecs1i.Size; j++) { eigvecs1i[j] *= mass1sqrt[j / 3]; } } if (bResetUnitVector) { eigvecs1i = eigvecs1i.UnitVector(); } for (int j = 0; j < eigvecs1i.Size; j++) { mat1[i, j] = eigvecs1i[j]; } } HDebug.Assert(mat1.ColSize == modes1.Count); GC.Collect(0); //Vector[] eigvecs2 = new Vector[modes2.Count]; for(int i=0; i<modes2.Count; i++) eigvecs2[i] = modes2[i].eigvec.Clone(); //if(mass2 != null) { double[] mass2sqrt = mass2.HSqrt(); for(int i=0; i<modes2.Count; i++) for(int j=0; j<eigvecs2[i].Size; j++) eigvecs2[i][j] *= mass2sqrt[j/3]; } //if(bResetUnitVector) for(int i=0; i<modes2.Count; i++) eigvecs2[i] = eigvecs2[i].UnitVector(); //mat2 = eigvecs2.ToMatrix(true ); HDebug.Assert(mat2.RowSize == modes2.Count); eigvecs2 = null; GC.Collect(0); mat2 = Matrix.Zeros(vecsize, modes2.Count); double[] mass2sqrt = null; if (mass2 != null) { mass2sqrt = mass2.HSqrt(); } for (int i = 0; i < modes2.Count; i++) { Vector eigvecs2i = modes2[i].eigvec.Clone(); if (mass2 != null) { for (int j = 0; j < eigvecs2i.Size; j++) { eigvecs2i[j] *= mass2sqrt[j / 3]; } } if (bResetUnitVector) { eigvecs2i = eigvecs2i.UnitVector(); } for (int j = 0; j < eigvecs2i.Size; j++) { mat2[j, i] = eigvecs2i[j]; } } HDebug.Assert(mat2.RowSize == modes2.Count); GC.Collect(0); } break; default: throw new NotImplementedException(); } HDebug.Assert(mat1.RowSize == mat2.ColSize); Matrix overlap = null; if (ila != null) { overlap = ila.Mul(mat1, mat2); } else { overlap = Matlab.ila.Mul(mat1, mat2); } mat1 = mat2 = null; GC.Collect(0); //overlap.UpdateAbs(); if (HDebug.IsDebuggerAttached) { double[] sum_c2 = new double[overlap.ColSize]; double[] sum_r2 = new double[overlap.RowSize]; for (int c = 0; c < overlap.ColSize; c++) { for (int r = 0; r < overlap.RowSize; r++) { double v = overlap[c, r]; double v2 = v * v; HDebug.Assert(v2 <= 1.00000000001); sum_c2[c] += v2; sum_r2[r] += v2; } } for (int c = 0; c < overlap.ColSize; c++) { HDebug.AssertTolerance(0.00001, sum_c2[c] - 1.0); } for (int r = 0; r < overlap.RowSize; r++) { HDebug.AssertTolerance(0.00001, sum_r2[r] - 1.0); } } return(overlap); }
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); }