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 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 double GetPotentialCustoms(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.ICustom> frcfld_customs = SelectInFrcflds(frcflds, new List <ForceField.ICustom>()); double energy = 0; foreach (ForceField.ICustom frcfld in frcfld_customs) { Vector[] lforces = (forces == null) ? null : GetVectorsZero(); frcfld.Compute(atoms, coords, ref energy, ref lforces, ref hessian, pwfrc, pwspr); HDebug.Assert(double.IsNaN(energy) == false, double.IsInfinity(energy) == false); if (forces != null) { for (int i = 0; i < size; i++) { forces[i] += lforces[i]; } forceij.AddCustom(coords, lforces); } } return(energy); }
public double GetPotentialNonbondeds(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.INonbonded> frcfld_nonbondeds = SelectInFrcflds(frcflds, new List <ForceField.INonbonded>()); double energy = 0; if (frcfld_nonbondeds.Count == 0) { return(energy); } Vector[] lcoords = new Vector[2]; Vector[] lforces = (forces == null) ? null : new Vector[2]; Nonbondeds_v1 nonbondeds = null; if (cache == null) { cache = new Dictionary <string, object>(); } if (cache.ContainsKey("all nonbondeds")) { // compute all non-bondeds double nonbondeds_maxdist = double.PositiveInfinity; nonbondeds = new Nonbondeds_v1(atoms, size, nonbondeds_maxdist); nonbondeds.UpdateNonbondeds(coords, 0); cache.Add("nonbondeds", nonbondeds); } else if (cache.ContainsKey("nonbondeds")) { // use existing cached nonbondeds, but update it as well nonbondeds = (Nonbondeds_v1)cache["nonbondeds"]; nonbondeds.UpdateNonbondeds(coords, 0.01); } else { // create default nonbondeds, and add it into cache nonbondeds = new Nonbondeds_v1(atoms, size, 12); nonbondeds.UpdateNonbondeds(coords, 0); cache.Add("nonbondeds", nonbondeds); } double stat_min = double.MaxValue; double stat_max = double.MinValue; double netstat_min = double.MaxValue; double netstat_max = double.MinValue; foreach (Nonbonded nonbond in nonbondeds) { ///{ // this removes interactions in-between rigid atoms. /// Atom atom0 = nonbond.atoms[0]; /// Atom atom1 = nonbond.atoms[1]; /// HashSet<Atom> atomsInRigid0 = GetAtomsInRigid(atom0); /// if(atomsInRigid0.Contains(atom1)) /// continue; ///} int id0 = nonbond.atoms[0].ID; lcoords[0] = coords[id0]; int id1 = nonbond.atoms[1].ID; lcoords[1] = coords[id1]; foreach (ForceField.INonbonded frcfld in frcfld_nonbondeds) { 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]); double[,] lpwfrc = new double[2, 2]; double[,] lpwspr = new double[2, 2]; frcfld.Compute(nonbond, lcoords, ref energy, ref lforces, ref lhess, lpwfrc, lpwspr); HDebug.Assert(double.IsNaN(energy) == false, double.IsInfinity(energy) == false); if (forces != null) { forces[id0] += lforces[0]; forces[id1] += lforces[1]; if (forceij != null) { forceij.AddNonbonded(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]; } if (pwfrc != null) { pwfrc[id0, id0] += lpwfrc[0, 0]; pwfrc[id0, id1] += lpwfrc[0, 1]; pwfrc[id1, id0] += lpwfrc[1, 0]; pwfrc[id1, id1] += lpwfrc[1, 1]; } if (pwspr != null) { pwspr[id0, id0] += lpwspr[0, 0]; pwspr[id0, id1] += lpwspr[0, 1]; pwspr[id1, id0] += lpwspr[1, 0]; pwspr[id1, id1] += lpwspr[1, 1]; } } } double stat14_min = double.MaxValue; double stat14_max = double.MinValue; double netstat14_min = double.MaxValue; double netstat14_max = double.MinValue; foreach (Nonbonded14 nonbond in nonbonded14s) { int id0 = nonbond.atoms[0].ID; lcoords[0] = coords[id0]; int id1 = nonbond.atoms[1].ID; lcoords[1] = coords[id1]; foreach (ForceField.INonbonded frcfld in frcfld_nonbondeds) { 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]); double[,] lpwfrc = new double[2, 2]; double[,] lpwspr = new double[2, 2]; frcfld.Compute(nonbond, 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]; if (forceij != null) { forceij.AddNonbonded14(id0, id1, lcoords, lforces); } HDebug.AssertTolerance(0.00000001, lforces[0].Dist2 - lforces[1].Dist2); double netstat = lforces[0].Dist2 + lforces[1].Dist2; netstat14_min = Math.Min(netstat14_min, netstat); netstat14_max = Math.Max(netstat14_max, netstat); stat14_min = Math.Min(stat14_min, lforces[0].Dist); stat14_max = Math.Max(stat14_max, lforces[0].Dist); stat14_min = Math.Min(stat14_min, lforces[1].Dist); stat14_max = Math.Max(stat14_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]; } if (pwfrc != null) { pwfrc[id0, id0] += lpwfrc[0, 0]; pwfrc[id0, id1] += lpwfrc[0, 1]; pwfrc[id1, id0] += lpwfrc[1, 0]; pwfrc[id1, id1] += lpwfrc[1, 1]; } if (pwspr != null) { pwspr[id0, id0] += lpwspr[0, 0]; pwspr[id0, id1] += lpwspr[0, 1]; pwspr[id1, id0] += lpwspr[1, 0]; pwspr[id1, id1] += lpwspr[1, 1]; } } } return(energy); }