public bool GetHess4PwIntrAct_selftest(List <ForceField.IForceField> frcflds, double frcfactor) { Vector[] coords = GetCoords(); Vector[] forces = GetVectorsZero(); MatrixByArr[,] hessian = new MatrixByArr[size, size]; { for (int c = 0; c < size; c++) { for (int r = 0; r < size; r++) { hessian[c, r] = new double[3, 3]; } } } Dictionary <string, object> cache = new Dictionary <string, object>(); cache.Add("all nonbondeds", null); PwForceDecomposer forceij = null; double energy = GetPotentialNonbondeds(frcflds, coords, ref forces, ref hessian, cache, forceij); List <ForceField.IForceField> frcflds_nonbondeds = new List <ForceField.IForceField>(); foreach (ForceField.INonbonded frcfld_nonbonded in SelectInFrcflds(frcflds, new List <ForceField.INonbonded>())) { frcflds_nonbondeds.Add(frcfld_nonbonded); } MatrixByArr[,] hess = GetHess4PwIntrAct(frcflds_nonbondeds, frcfactor); if (hess.GetLength(0) != hessian.GetLength(0)) { return(false); } if (hess.GetLength(1) != hessian.GetLength(1)) { return(false); } for (int i = 0; i < hess.GetLength(0); i++) { for (int j = 0; j < hess.GetLength(1); j++) { if (i == j) { continue; } if (hess[i, j].ColSize != hessian[i, j].ColSize) { return(false); } if (hess[i, j].RowSize != hessian[i, j].RowSize) { return(false); } if (HDebug.CheckTolerance(0.00000001, hess[i, j] - hessian[i, j]) == false) { return(false); } } } return(true); }
public static bool CheckHessDiag(Matrix hessian, double tolerSumDiag, string exceptmsg = null) { if (CheckHessDiag_selftest) { CheckHessDiag_selftest = false; Matrix thess = new double[, ] { { -1, -2, 0, 1, 2, 3 } , { -2, -4, -5, 2, 4, 5 } , { -3, -5, -6, 3, 5, 6 } , { 1, -2, 3, -1, 2, -3 } , { 2, -4, -5, -2, 4, 5 } , { -3, 5, 6, 3, -5, -6 } }; HDebug.Assert(Hess.CheckHessDiag(thess, 0.0000001) == false); thess[0, 2] = -3; HDebug.Assert(Hess.CheckHessDiag(thess, 0.0000001) == true); for (int i = 0; i < thess.ColSize; i++) { thess[i, i] = 0; } HDebug.Assert(Hess.CheckHessDiag(thess, 0.0000001) == false); } // tolerSumDiag = 0.000001 HDebug.Assert(hessian.ColSize == hessian.RowSize); HDebug.Assert(hessian.ColSize % 3 == 0); int size = hessian.ColSize / 3; List <double> sums = new List <double>(); for (int i = 0; i < size; i++) { for (int di = 0; di < 3; di++) { for (int dj = 0; dj < 3; dj++) { double diagval = 0; for (int j = 0; j < size; j++) { if (i == j) { continue; } double hessian_idi_jdj = hessian[3 * i + di, 3 * j + dj]; if (double.IsNaN(hessian_idi_jdj)) { HDebug.Assert(false); return(false); } // throw new Exception("NotComputable :"+exceptmsg); if (double.IsInfinity(hessian_idi_jdj)) { HDebug.Assert(false); return(false); } //throw new Exception("NotComputable :"+exceptmsg); diagval += hessian_idi_jdj; } double sum = diagval + hessian[3 * i + di, 3 * i + dj]; sums.Add(sum); } } } double maxsum = Math.Max(Math.Abs(sums.Min()), Math.Abs(sums.Max())); if (HDebug.CheckTolerance(tolerSumDiag, maxsum) == false) { return(false); } return(true); }