public static double GetHessAnmSpringBwAtoms(this HessMatrix hess, IList <Vector> coords, int atomi, int atomj) { HDebug.Assert(hess.ColSize == hess.RowSize); HDebug.Assert(hess.ColSize % 3 == 0); HDebug.Assert(hess.RowSize % 3 == 0); HDebug.Assert(atomi < hess.ColSize / 3); HDebug.Assert(atomj < hess.ColSize / 3); HDebug.Assert(coords.Count == hess.ColSize / 3); HDebug.Assert(hess.ColBlockSize == hess.RowBlockSize); HDebug.Assert(atomi < hess.ColBlockSize); HDebug.Assert(atomj < hess.ColBlockSize); HDebug.Assert(coords.Count == hess.ColBlockSize); Vector coordij = (coords[atomj] - coords[atomi]).UnitVector(); double anm_spr_ij = 0; { MatrixByArr hess_ij = hess.GetBlock(atomi, atomj); if (hess_ij != null) { anm_spr_ij = LinAlg.VtMV(coordij, hess_ij, coordij); } } double anm_spr_ji = 0; { MatrixByArr hess_ji = hess.GetBlock(atomj, atomi); if (hess_ji != null) { anm_spr_ji = LinAlg.VtMV(coordij, hess_ji, coordij); } } double threshold = 0.00001; HDebug.Assert(Math.Abs(anm_spr_ij - anm_spr_ji) < threshold); double spr_ij = -1 * (anm_spr_ij + anm_spr_ji) / 2; if (selftest_GetHessAnmSpringBwAtoms_hess_coords_atomi_atomj) { selftest_GetHessAnmSpringBwAtoms_hess_coords_atomi_atomj = false; double _spr_ij = GetHessAnmSpringBwAtoms(hess as Matrix, coords, atomi, atomj); HDebug.Assert(Math.Abs(spr_ij - _spr_ij) < threshold); } return(spr_ij); }
public static double GetHessTraceSpringBwAtoms(this HessMatrix hess, int atomi, int atomj) { HDebug.Assert(hess.ColSize == hess.RowSize); HDebug.Assert(hess.ColSize % 3 == 0); HDebug.Assert(hess.RowSize % 3 == 0); HDebug.Assert(atomi < hess.ColSize / 3); HDebug.Assert(atomj < hess.ColSize / 3); HDebug.Assert(hess.ColBlockSize == hess.RowBlockSize); HDebug.Assert(atomi < hess.ColBlockSize); HDebug.Assert(atomj < hess.ColBlockSize); double bibj_trace = 0; { MatrixByArr hess_ij = hess.GetBlock(atomi, atomj); if (hess_ij != null) { bibj_trace += hess_ij[0, 0]; bibj_trace += hess_ij[1, 1]; bibj_trace += hess_ij[2, 2]; } } double bjbi_trace = 0; { MatrixByArr hess_ji = hess.GetBlock(atomj, atomi); if (hess_ji != null) { bjbi_trace += hess_ji[0, 0]; bjbi_trace += hess_ji[1, 1]; bjbi_trace += hess_ji[2, 2]; } } double threshold = 0.00001; HDebug.Assert(Math.Abs(bibj_trace - bjbi_trace) < threshold); double spr_ij = -1 * (bibj_trace + bjbi_trace) / 2; if (selftest_GetHessTraceSpringBwAtoms_hess_atomi_atomj) { selftest_GetHessTraceSpringBwAtoms_hess_atomi_atomj = false; double _spr_ij = GetHessTraceSpringBwAtoms(hess as Matrix, atomi, atomj); HDebug.Assert(Math.Abs(spr_ij - _spr_ij) < threshold); } return(spr_ij); }
public void SetBlock(IList <int> bcs, IList <int> brs, HessMatrix subhess) { HDebug.Assert(bcs.Count == subhess.ColBlockSize); HDebug.Assert(brs.Count == subhess.RowBlockSize); for (int ic = 0; ic < bcs.Count; ic++) { for (int ir = 0; ir < brs.Count; ir++) { int bc = bcs[ic]; int br = brs[ir]; SetBlock(bc, br, subhess.GetBlock(ic, ir)); } } }
/////////////////////////////////////////////////////////////////////// // UpdateSubMatrixByAtom public void UpdateSubMatrixByAtom(HessMatrix submat, IList <int> idxcolatoms, IList <int> idxrowatoms) { HDebug.Assert(idxcolatoms.Max() < ColBlockSize); HDebug.Assert(idxrowatoms.Max() < RowBlockSize); HDebug.Assert(submat.ColBlockSize == idxcolatoms.Count); HDebug.Assert(submat.RowBlockSize == idxrowatoms.Count); for (int ic = 0; ic < idxcolatoms.Count; ic++) { for (int ir = 0; ir < idxrowatoms.Count; ir++) { int hess_ic = idxcolatoms[ic]; int hess_ir = idxrowatoms[ir]; SetBlock(hess_ic, hess_ir, submat.GetBlock(ic, ir).CloneT()); } } }
public static bool HessMatrixSparseEqual(HessMatrix hess, HessMatrix equal) { if (hess.ColSize != equal.ColSize) { return(false); } if (hess.RowSize != equal.RowSize) { return(false); } foreach (var bc_br_bval in hess.EnumBlocks()) { int bc = bc_br_bval.Item1; int br = bc_br_bval.Item2; var bv = bc_br_bval.Item3; var bv0 = equal.GetBlock(bc, br); if (bv0 == null) { return(false); } if ((bv - bv0).HAbsMax() != 0) { return(false); } } foreach (var bc_br_bval in equal.EnumBlocks()) { int bc = bc_br_bval.Item1; int br = bc_br_bval.Item2; var bv = bc_br_bval.Item3; var bv0 = hess.GetBlock(bc, br); if (bv0 == null) { return(false); } if ((bv - bv0).HAbsMax() != 0) { return(false); } } return(true); }
public static CGetHessCoarseResiIterImpl GetHessCoarseResiIterImpl_Matlab_IterLowerTri (object[] atoms , HessMatrix H , List <int>[] lstNewIdxRemv , double thres_zeroblk , ILinAlg ila , bool cloneH , string[] options // { "pinv(D)" } ) { ila = null; if (cloneH) { H = H.CloneHess(); } bool process_disp_console = true; if (options != null && options.Contains("print process")) { process_disp_console = true; } bool parallel = true; /// keep only lower triangle of H (lower block triangles) { HashSet <Tuple <int, int, MatrixByArr> > lstUppTrig = new HashSet <Tuple <int, int, MatrixByArr> >(); foreach (ValueTuple <int, int, MatrixByArr> bc_br_bval in H.EnumBlocks()) { int bc = bc_br_bval.Item1; int br = bc_br_bval.Item2; if (bc < br) { lstUppTrig.Add(bc_br_bval.ToTuple()); } } foreach (Tuple <int, int, MatrixByArr> bc_br_bval in lstUppTrig) { int bc = bc_br_bval.Item1; int br = bc_br_bval.Item2; HDebug.Assert(bc < br); H.SetBlock(bc, br, null); } } GC.Collect(); List <DateTime> process_time = new List <DateTime>(); //System.Console.WriteLine("begin coarse-graining"); List <HessCoarseResiIterInfo> iterinfos = new List <HessCoarseResiIterInfo>(); for (int iter = lstNewIdxRemv.Length - 1; iter >= 0; iter--) { process_time.Clear(); if (process_disp_console) { process_time.Add(DateTime.UtcNow); System.Console.Write(" - {0:000} : ", iter); } //int[] ikeep = lstNewIdxRemv[iter].Item1; int[] iremv = lstNewIdxRemv[iter].ToArray(); int iremv_min = iremv.Min(); int iremv_max = iremv.Max(); HDebug.Assert(H.ColBlockSize == H.RowBlockSize); int blksize = H.ColBlockSize; //HDebug.Assert(ikeep.Max() < blksize); //HDebug.Assert(iremv.Max() < blksize); //HDebug.Assert(iremv.Max()+1 == blksize); //HDebug.Assert(iremv.Max() - iremv.Min() + 1 == iremv.Length); int[] idxkeep = HEnum.HEnumFromTo(0, iremv_min - 1).ToArray(); int[] idxremv = HEnum.HEnumFromTo(iremv_min, iremv_max).ToArray(); //HDebug.Assert(idxkeep.HUnionWith(idxremv).Length == blksize); HessCoarseResiIterInfo iterinfo = new HessCoarseResiIterInfo(); iterinfo.sizeHessBlkMat = idxremv.Max() + 1; // H.ColBlockSize; iterinfo.numAtomsRemoved = idxremv.Length; iterinfo.time0 = DateTime.UtcNow; //////////////////////////////////////////////////////////////////////////////////////// // make C sparse double C_density0; double C_density1; { double thres_absmax = thres_zeroblk; C_density0 = 0; List <Tuple <int, int> > lstIdxToMakeZero = new List <Tuple <int, int> >(); foreach (var bc_br_bval in H.EnumBlocksInCols(idxremv)) { int bc = bc_br_bval.Item1; int br = bc_br_bval.Item2; var bval = bc_br_bval.Item3; if (br >= iremv_min) { // bc_br is in D, not in C continue; } C_density0++; double absmax_bval = bval.HAbsMax(); if (absmax_bval < thres_absmax) { lstIdxToMakeZero.Add(new Tuple <int, int>(bc, br)); } } C_density1 = C_density0 - lstIdxToMakeZero.Count; foreach (var bc_br in lstIdxToMakeZero) { int bc = bc_br.Item1; int br = bc_br.Item2; HDebug.Assert(bc > br); var Cval = H.GetBlock(bc, br); var Dval = H.GetBlock(bc, bc); var Aval = H.GetBlock(br, br); var Bval = Cval.Tr(); H.SetBlock(bc, br, null); // nCval = Cval -Cval H.SetBlock(bc, bc, Dval + Cval); // nDval = Dval - (-Cval) = Dval + Cval // nBval = Bval -Bval H.SetBlock(br, br, Aval + Bval); // nAval = Aval - (-Bval) = Aval + Bval } iterinfo.numSetZeroBlock = lstIdxToMakeZero.Count; iterinfo.numNonZeroBlock = (int)C_density1; C_density0 /= (idxkeep.Length * idxremv.Length); C_density1 /= (idxkeep.Length * idxremv.Length); } //////////////////////////////////////////////////////////////////////////////////////// // get A, B, C, D HessMatrix A = H; // HessMatrix A = H.SubMatrixByAtoms(false, idxkeep, idxkeep); // HessMatrix B = H.SubMatrixByAtoms(false, idxkeep, idxremv); HessMatrix C; // HessMatrix C = H.SubMatrixByAtoms(false, idxremv, idxkeep, parallel:parallel); HessMatrix D; // HessMatrix D = H.SubMatrixByAtoms(false, idxremv, idxremv, parallel:parallel); { C = H.Zeros(idxremv.Length * 3, idxkeep.Length * 3); D = H.Zeros(idxremv.Length * 3, idxremv.Length * 3); //List<Tuple<int, int, MatrixByArr>> lst_bc_br_bval = H.EnumBlocksInCols(idxremv).ToList(); //foreach(var bc_br_bval in lst_bc_br_bval) foreach (var bc_br_bval in H.EnumBlocksInCols(idxremv)) { int bc = bc_br_bval.Item1; int br = bc_br_bval.Item2; var bval = bc_br_bval.Item3; H.SetBlock(bc, br, null); if (bc > iremv_max) { HDebug.Assert(false); continue; } if (br > iremv_max) { HDebug.Assert(false); continue; } if (br < iremv_min) { int nc = bc - iremv_min; int nr = br; HDebug.Assert(C.HasBlock(nc, nr) == false); C.SetBlock(nc, nr, bval.CloneT()); } else { int nc = bc - iremv_min; int nr = br - iremv_min; HDebug.Assert(D.HasBlock(nc, nr) == false); D.SetBlock(nc, nr, bval); if (nc != nr) { HDebug.Assert(D.HasBlock(nr, nc) == false); D.SetBlock(nr, nc, bval.Tr()); } } } HDebug.Assert(H.EnumBlocksInCols(idxremv).Count() == 0); } if (process_disp_console) { process_time.Add(DateTime.UtcNow); int ptc = process_time.Count; System.Console.Write("CD({0:00.00} min), ", (process_time[ptc - 1] - process_time[ptc - 2]).TotalMinutes); } //////////////////////////////////////////////////////////////////////////////////////// // Get B.inv(D).C HessMatrix B_invD_C; { { B_invD_C = GetHessCoarseResiIterImpl_Matlab_IterLowerTri_Get_BInvDC(A, C, D, process_disp_console , options , thld_BinvDC: thres_zeroblk / lstNewIdxRemv.Length , parallel: parallel ); } if (process_disp_console) { process_time.Add(DateTime.UtcNow); int ptc = process_time.Count; System.Console.Write("B.invD.C({0:00.00} min), ", (process_time[ptc - 1] - process_time[ptc - 2]).TotalMinutes); } GC.Collect(0); } //////////////////////////////////////////////////////////////////////////////////////// // Get A - B.inv(D).C /// iterinfo.numAddIgnrBlock = A.UpdateAdd(B_invD_C, -1, null, thres_zeroblk/lstNewIdxRemv.Length, parallel:parallel); { HessMatrix __this = A; HessMatrix other = B_invD_C; double _thres_NearZeroBlock = thres_zeroblk / lstNewIdxRemv.Length; int[] _count = new int[1]; int[] _count_ignored = new int[1]; //foreach(var bc_br_bval in other.EnumBlocks()) Action <ValueTuple <int, int, MatrixByArr> > func = delegate(ValueTuple <int, int, MatrixByArr> bc_br_bval) { _count[0]++; int bc = bc_br_bval.Item1; int br = bc_br_bval.Item2; MatrixByArr other_bmat = bc_br_bval.Item3; if (bc < br) { return; // continue; } if (other_bmat.HAbsMax() <= _thres_NearZeroBlock) { /// other_bmat = other_bmat -other_bmat; /// other_diag = other_diag - (-other_bmat) = other_diag + other_bmat; /// this_diag = this_diat - B_invD_C /// = this_diat - other_diag /// = this_diat - (other_diag + other_bmat) /// = this_diat - other_diag - other_bmat /// = (this_diat - other_bmat) - other_diag /// = (this_diat - other_bmat) - (processed later) /// = (this_diat - other_bmat) MatrixByArr this_diag = __this.GetBlock(bc, bc); MatrixByArr new_diag = this_diag - other_bmat; __this.SetBlockLock(bc, bc, new_diag); other_bmat = null; lock (_count_ignored) _count_ignored[0]++; } if (other_bmat != null) { MatrixByArr this_bmat = __this.GetBlock(bc, br); if (this_bmat == null) { this_bmat = new double[3, 3]; } MatrixByArr new_bmat = this_bmat - other_bmat; __this.SetBlockLock(bc, br, new_bmat); } }; if (parallel) { HParallel.ForEach(other.EnumBlocks(), func); } else { foreach (var bc_br_bval in other.EnumBlocks()) { func(bc_br_bval); } } iterinfo.numAddIgnrBlock = _count_ignored[0]; } if (process_disp_console) { process_time.Add(DateTime.UtcNow); int ptc = process_time.Count; System.Console.Write("A-BinvDC({0:00.00} min), ", (process_time[ptc - 1] - process_time[ptc - 2]).TotalMinutes); } //HessMatrix nH = A - B_invD_C; //nH = ((nH + nH.Tr())/2).ToHessMatrix(); //////////////////////////////////////////////////////////////////////////////////////// // Replace A -> H H = A; //////////////////////////////////////////////////////////////////////////////////////// // print iteration log iterinfo.usedMemoryByte = GC.GetTotalMemory(false); iterinfo.time1 = DateTime.UtcNow; iterinfos.Add(iterinfo); if (process_disp_console) { System.Console.Write("summary(makezero {0,5}, nonzero {1,5}, numIgnMul {2,7}, numRemvAtoms {3,3}, {4,5:0.00} min, {5} mb, {6}x{6}, nzeroBlk/Atom {7:0.00}), GC(" , iterinfo.numSetZeroBlock , iterinfo.numNonZeroBlock , iterinfo.numAddIgnrBlock , iterinfo.numAtomsRemoved , iterinfo.compTime.TotalMinutes , iterinfo.usedMemoryByte / (1024 * 1024) , (idxkeep.Length * 3) , ((double)iterinfo.numNonZeroBlock / idxremv.Length) ); } GC.Collect(); if (process_disp_console) { System.Console.WriteLine(")"); } } int numca = H.ColBlockSize - lstNewIdxRemv.HListCount().Sum(); //System.Console.WriteLine("finish coarse-graining"); { int[] idxkeep = HEnum.HEnumCount(numca).ToArray(); H = H.SubMatrixByAtoms(false, idxkeep, idxkeep, false); } { H.MakeNearZeroBlockAsZero(thres_zeroblk); } GC.Collect(); //System.Console.WriteLine("finish resizing"); return(new CGetHessCoarseResiIterImpl { iterinfos = iterinfos, H = H, }); }
public static HessMatrix GetHessAnm(IList <Vector> coords, IEnumerable <Tuple <int, int, double> > enumKij) { if (GetHessAnm_selftest) { GetHessAnm_selftest = false; Vector[] tcoords = new Vector[] { new Vector(0, 0, 0), new Vector(1, 0, 0), new Vector(0, 2, 0), new Vector(0, 0, 3), new Vector(4, 5, 0), new Vector(0, 6, 7), new Vector(8, 0, 9), new Vector(-1, -1, -1), }; Matrix tKij = Matrix.Ones(8, 8); for (int i = 0; i < 8; i++) { tKij[i, i] = 0; } HessMatrix hess0 = GetHessAnm_debug(tcoords, tKij); HessMatrix hess1 = GetHessAnm(tcoords, tKij.HEnumNonZeros()); HDebug.Assert((hess0 - hess1).HAbsMax() == 0); } //Debug.Assert(AnmHessianSelfTest()); int n = coords.Count; HessMatrix hess = null; if (n < 100) { hess = HessMatrixDense.ZerosDense(n * 3, n * 3); } else { hess = HessMatrixSparse.ZerosSparse(n * 3, n * 3); } int numset = 0; foreach (var kij in enumKij) { int i = kij.Item1; int j = kij.Item2; double k_ij = kij.Item3; { if (i == j) { HDebug.Assert(k_ij == 0); continue; } if (coords[j] == null) { continue; } Vector vec_ij = coords[j] - coords[i]; Vector unit_ij = vec_ij.UnitVector(); HDebug.Assert(double.IsNaN(k_ij) == false); if (k_ij == 0) { continue; } MatrixByArr hessij = new double[3, 3]; hessij[0, 0] = -k_ij * unit_ij[0] * unit_ij[0]; hessij[0, 1] = -k_ij * unit_ij[0] * unit_ij[1]; hessij[0, 2] = -k_ij * unit_ij[0] * unit_ij[2]; hessij[1, 0] = -k_ij * unit_ij[1] * unit_ij[0]; hessij[1, 1] = -k_ij * unit_ij[1] * unit_ij[1]; hessij[1, 2] = -k_ij * unit_ij[1] * unit_ij[2]; hessij[2, 0] = -k_ij * unit_ij[2] * unit_ij[0]; hessij[2, 1] = -k_ij * unit_ij[2] * unit_ij[1]; hessij[2, 2] = -k_ij * unit_ij[2] * unit_ij[2]; hess.SetBlock(i, j, hessij); MatrixByArr hessii = hess.GetBlock(i, i) - hessij; hess.SetBlock(i, i, hessii); numset++; } } return(hess); }
public static CGetHessCoarseResiIterImpl GetHessCoarseResiIterImpl_Matlab_experimental (object[] atoms , HessMatrix H , List <int>[] lstNewIdxRemv , double thres_zeroblk , ILinAlg ila , bool cloneH , string[] options ) { ila = null; if (cloneH) { H = H.CloneHess(); } bool process_disp_console = true; if (options != null && options.Contains("print process")) { process_disp_console = true; } bool parallel = true; GC.Collect(0); DateTime[] process_time = new DateTime[6]; //System.Console.WriteLine("begin coarse-graining"); List <HessCoarseResiIterInfo> iterinfos = new List <HessCoarseResiIterInfo>(); for (int iter = lstNewIdxRemv.Length - 1; iter >= 0; iter--) { if (process_disp_console) { process_time[0] = DateTime.UtcNow; System.Console.Write(" - {0:000} : ", iter); } //int[] ikeep = lstNewIdxRemv[iter].Item1; int[] iremv = lstNewIdxRemv[iter].ToArray(); HDebug.Assert(H.ColBlockSize == H.RowBlockSize); int blksize = H.ColBlockSize; //HDebug.Assert(ikeep.Max() < blksize); //HDebug.Assert(iremv.Max() < blksize); //HDebug.Assert(iremv.Max()+1 == blksize); //HDebug.Assert(iremv.Max() - iremv.Min() + 1 == iremv.Length); int[] idxkeep = HEnum.HEnumFromTo(0, iremv.Min() - 1).ToArray(); int[] idxremv = HEnum.HEnumFromTo(iremv.Min(), iremv.Max()).ToArray(); //HDebug.Assert(idxkeep.HUnionWith(idxremv).Length == blksize); HessCoarseResiIterInfo iterinfo = new HessCoarseResiIterInfo(); iterinfo.sizeHessBlkMat = idxremv.Max() + 1; // H.ColBlockSize; iterinfo.numAtomsRemoved = idxremv.Length; iterinfo.time0 = DateTime.UtcNow; double C_density0 = double.NaN; double C_density1 = double.NaN; { //HessMatrix A = H.SubMatrixByAtoms(false, idxkeep, idxkeep); HessMatrix A = H; //HessMatrix B = H.SubMatrixByAtoms(false, idxkeep, idxremv); HessMatrix C, D; /// HessMatrix C = H.SubMatrixByAtoms(false, idxremv, idxkeep, parallel:parallel); /// HessMatrix D = H.SubMatrixByAtoms(false, idxremv, idxremv, parallel:parallel); { C = H.Zeros(idxremv.Length * 3, idxkeep.Length * 3); D = H.Zeros(idxremv.Length * 3, idxremv.Length * 3); int iremv_min = iremv.Min(); int iremv_max = iremv.Max(); foreach (var bc_br_bval in H.EnumBlocksInCols(idxremv)) { int bc = bc_br_bval.Item1; int br = bc_br_bval.Item2; var bval = bc_br_bval.Item3; if (bc > iremv_max) { continue; } if (br > iremv_max) { continue; } if (br < iremv_min) { int nc = bc - iremv_min; int nr = br; C.SetBlock(nc, nr, bval.CloneT()); } else { int nc = bc - iremv_min; int nr = br - iremv_min; D.SetBlock(nc, nr, bval.CloneT()); } } } if (process_disp_console) { process_time[1] = process_time[2] = DateTime.UtcNow; System.Console.Write("CD({0:00.00} min), ", (process_time[2] - process_time[0]).TotalMinutes); } // make B,C sparse //int B_cntzero = B.MakeNearZeroBlockAsZero(thres_zeroblk); C_density0 = C.RatioUsedBlocks; /// iterinfo.numSetZeroBlock = C.MakeNearZeroBlockAsZero(thres_zeroblk); { double thres_absmax = thres_zeroblk; List <Tuple <int, int> > lstIdxToMakeZero = new List <Tuple <int, int> >(); foreach (var bc_br_bval in C.EnumBlocks()) { int bc = bc_br_bval.Item1; int br = bc_br_bval.Item2; var bval = bc_br_bval.Item3; double absmax_bval = bval.HAbsMax(); if (absmax_bval < thres_absmax) { lstIdxToMakeZero.Add(new Tuple <int, int>(bc, br)); } } foreach (var bc_br in lstIdxToMakeZero) { int cc = bc_br.Item1; int cr = bc_br.Item2; var Cval = C.GetBlock(cc, cr); C.SetBlock(cc, cr, null); var Dval = D.GetBlock(cc, cc); // nCval = Cval -Cval D.SetBlock(cc, cc, Dval + Cval); // nDval = Dval - (-Cval) = Dval + Cval int bc = cr; int br = cc; var Bval = Cval.Tr(); var Aval = A.GetBlock(bc, bc); // nBval = Bval -Bval A.SetBlock(bc, bc, Aval + Bval); // nAval = Aval - (-Bval) = Aval + Bval } iterinfo.numSetZeroBlock = lstIdxToMakeZero.Count; } //int B_nzeros = B.NumUsedBlocks; double B_nzeros_ = Math.Sqrt(B_nzeros); iterinfo.numNonZeroBlock = C.NumUsedBlocks; C_density1 = C.RatioUsedBlocks; HessMatrix B_invD_C = Get_BInvDC(A, C, D, process_disp_console , options , parallel: parallel ); if (process_disp_console) { process_time[4] = DateTime.UtcNow; System.Console.Write("B.invD.C({0:00.00} min), ", (process_time[4] - process_time[3]).TotalMinutes); } /// iterinfo.numAddIgnrBlock = A.UpdateAdd(B_invD_C, -1, null, thres_zeroblk/lstNewIdxRemv.Length, parallel:parallel); { HessMatrix _this = A; HessMatrix other = B_invD_C; double thres_NearZeroBlock = thres_zeroblk / lstNewIdxRemv.Length; int count = 0; int count_ignored = 0; foreach (var bc_br_bval in other.EnumBlocks()) { count++; int bc = bc_br_bval.Item1; int br = bc_br_bval.Item2; MatrixByArr other_bmat = bc_br_bval.Item3; if (other_bmat.HAbsMax() <= thres_NearZeroBlock) { // other_bmat = other_bmat -other_bmat; // other_diag = other_diag - (-other_bmat) = other_diag + other_bmat; // this_diag = this_diat - B_invD_C // = this_diat - other_diag // = this_diat - (other_diag + other_bmat) // = this_diat - other_diag - other_bmat // = (this_diat - other_bmat) - other_diag // = (this_diat - other_bmat) - (processed later) // = (this_diat - other_bmat) MatrixByArr this_diag = _this.GetBlock(bc, bc); MatrixByArr new_diag = this_diag - other_bmat; _this.SetBlock(bc, bc, new_diag); other_bmat = null; count_ignored++; } if (other_bmat != null) { MatrixByArr this_bmat = _this.GetBlock(bc, br); MatrixByArr new_bmat = this_bmat - other_bmat; _this.SetBlock(bc, br, new_bmat); } } iterinfo.numAddIgnrBlock = count_ignored; } //HessMatrix nH = A - B_invD_C; //nH = ((nH + nH.Tr())/2).ToHessMatrix(); H = A; } iterinfo.usedMemoryByte = GC.GetTotalMemory(false); iterinfo.time1 = DateTime.UtcNow; iterinfos.Add(iterinfo); if (process_disp_console) { System.Console.WriteLine("summary(makezero {0,5}, nonzero {1,5}, numIgnMul {2,7}, numRemvAtoms {3,3}, {4,5:0.00} sec, {5} mb, {6}x{6}, nzeroBlk/Atom {7:0.00})" , iterinfo.numSetZeroBlock , iterinfo.numNonZeroBlock , iterinfo.numAddIgnrBlock , iterinfo.numAtomsRemoved , iterinfo.compSec , iterinfo.usedMemoryByte / (1024 * 1024) , (idxkeep.Length * 3) , ((double)iterinfo.numNonZeroBlock / idxremv.Length) ); } GC.Collect(0); } int numca = H.ColBlockSize - lstNewIdxRemv.HListCount().Sum(); //System.Console.WriteLine("finish coarse-graining"); { int[] idxkeep = HEnum.HEnumCount(numca).ToArray(); H = H.SubMatrixByAtoms(false, idxkeep, idxkeep, false); } { H.MakeNearZeroBlockAsZero(thres_zeroblk); } GC.Collect(0); //System.Console.WriteLine("finish resizing"); return(new CGetHessCoarseResiIterImpl { iterinfos = iterinfos, H = H, }); }
public static HessForcInfo GetCoarseHessForcSubSimple (object[] atoms , HessMatrix hess , Vector[] forc , List <int>[] lstNewIdxRemv , double thres_zeroblk , ILinAlg ila , bool cloneH , string[] options // { "pinv(D)" } ) { HessMatrix H = hess; Vector F = forc.ToVector(); if (cloneH) { H = H.CloneHess(); } bool process_disp_console = false; bool parallel = true; for (int iter = lstNewIdxRemv.Length - 1; iter >= 0; iter--) { //int[] ikeep = lstNewIdxRemv[iter].Item1; int[] iremv = lstNewIdxRemv[iter].ToArray(); int iremv_min = iremv.Min(); int iremv_max = iremv.Max(); HDebug.Assert(H.ColBlockSize == H.RowBlockSize); int blksize = H.ColBlockSize; //HDebug.Assert(ikeep.Max() < blksize); //HDebug.Assert(iremv.Max() < blksize); //HDebug.Assert(iremv.Max()+1 == blksize); //HDebug.Assert(iremv.Max() - iremv.Min() + 1 == iremv.Length); int[] idxkeep = HEnum.HEnumFromTo(0, iremv_min - 1).ToArray(); int[] idxremv = HEnum.HEnumFromTo(iremv_min, iremv_max).ToArray(); //HDebug.Assert(idxkeep.HUnionWith(idxremv).Length == blksize); IterInfo iterinfo = new IterInfo(); iterinfo.sizeHessBlkMat = idxremv.Max() + 1; // H.ColBlockSize; iterinfo.numAtomsRemoved = idxremv.Length; iterinfo.time0 = DateTime.UtcNow; //////////////////////////////////////////////////////////////////////////////////////// // make C sparse double C_density0; double C_density1; { double thres_absmax = thres_zeroblk; C_density0 = 0; List <Tuple <int, int> > lstIdxToMakeZero = new List <Tuple <int, int> >(); foreach (var bc_br_bval in H.EnumBlocksInCols(idxremv)) { int bc = bc_br_bval.Item1; int br = bc_br_bval.Item2; var bval = bc_br_bval.Item3; if (br >= iremv_min) { // bc_br is in D, not in C continue; } C_density0++; double absmax_bval = bval.HAbsMax(); if (absmax_bval < thres_absmax) { lstIdxToMakeZero.Add(new Tuple <int, int>(bc, br)); } } C_density1 = C_density0 - lstIdxToMakeZero.Count; foreach (var bc_br in lstIdxToMakeZero) { int bc = bc_br.Item1; int br = bc_br.Item2; HDebug.Assert(bc > br); var Cval = H.GetBlock(bc, br); var Dval = H.GetBlock(bc, bc); var Aval = H.GetBlock(br, br); var Bval = Cval.Tr(); H.SetBlock(bc, br, null); // nCval = Cval -Cval H.SetBlock(bc, bc, Dval + Cval); // nDval = Dval - (-Cval) = Dval + Cval // nBval = Bval -Bval H.SetBlock(br, br, Aval + Bval); // nAval = Aval - (-Bval) = Aval + Bval } iterinfo.numSetZeroBlock = lstIdxToMakeZero.Count; iterinfo.numNonZeroBlock = (int)C_density1; C_density0 /= (idxkeep.Length * idxremv.Length); C_density1 /= (idxkeep.Length * idxremv.Length); } //////////////////////////////////////////////////////////////////////////////////////// // get A, B, C, D HessMatrix A = H.SubMatrixByAtoms(false, idxkeep, idxkeep); HessMatrix B = H.SubMatrixByAtoms(false, idxkeep, idxremv); HessMatrix C = H.SubMatrixByAtoms(false, idxremv, idxkeep); HessMatrix D = H.SubMatrixByAtoms(false, idxremv, idxremv); Vector nF; Vector nG; { nF = new double[idxkeep.Length * 3]; nG = new double[idxremv.Length * 3]; for (int i = 0; i < idxkeep.Length * 3; i++) { nF[i] = F[i]; } for (int i = 0; i < idxremv.Length * 3; i++) { nG[i] = F[i + nF.Size]; } } Matlab.PutMatrix("A", A, true); Matlab.PutMatrix("B", B, true); Matlab.PutMatrix("C", C, true); Matlab.PutMatrix("D", D, true); Matlab.PutVector("F", nF); Matlab.PutVector("G", nG); //////////////////////////////////////////////////////////////////////////////////////// // Get B.inv(D).C // // var BInvDC_BInvDG = Get_BInvDC_BInvDG_WithSqueeze(C, D, nG, process_disp_console // , options // , thld_BinvDC: thres_zeroblk/lstNewIdxRemv.Length // , parallel: parallel // ); // HessMatrix B_invD_C = BInvDC_BInvDG.Item1; // Vector B_invD_G = BInvDC_BInvDG.Item2; // GC.Collect(0); Matlab.Execute("BinvD = B * inv(D);"); Matlab.Execute("clear B, D;"); Matlab.Execute("BinvDC = BinvD * C;"); Matlab.Execute("BinvDG = BinvD * G;"); //////////////////////////////////////////////////////////////////////////////////////// // Get A - B.inv(D).C // F - B.inv(D).G Matlab.Execute("HH = A - BinvDC;"); Matlab.Execute("FF = F - BinvDG;"); //////////////////////////////////////////////////////////////////////////////////////// // Replace A -> H H = Matlab.GetMatrix("HH", H.Zeros, true); F = Matlab.GetVector("FF"); Matlab.Execute("clear;"); { ValueTuple <HessMatrix, Vector> BBInvDDCC_BBInvDDGG = Get_BInvDC_BInvDG_Simple (C , D , nG , process_disp_console: process_disp_console , thld_BinvDC: thres_zeroblk / lstNewIdxRemv.Length , parallel: parallel ); HessMatrix HH = A - BBInvDDCC_BBInvDDGG.Item1; Vector FF = nF - BBInvDDCC_BBInvDDGG.Item2; double dbg_HH = (HH - H).HAbsMax(); double dbg_FF = (FF - F).ToArray().MaxAbs(); HDebug.Assert(Math.Abs(dbg_HH) < 0.00000001); HDebug.Assert(Math.Abs(dbg_FF) < 0.00000001); } { ValueTuple <HessMatrix, Vector> BBInvDDCC_BBInvDDGG = Get_BInvDC_BInvDG (C , D , nG , process_disp_console: process_disp_console , options: new string[0] , thld_BinvDC: thres_zeroblk / lstNewIdxRemv.Length , parallel: parallel ); HessMatrix HH = A - BBInvDDCC_BBInvDDGG.Item1; Vector FF = nF - BBInvDDCC_BBInvDDGG.Item2; double dbg_HH = (HH - H).HAbsMax(); double dbg_FF = (FF - F).ToArray().MaxAbs(); HDebug.Assert(Math.Abs(dbg_HH) < 0.00000001); HDebug.Assert(Math.Abs(dbg_FF) < 0.00000001); } { ValueTuple <HessMatrix, Vector> BBInvDDCC_BBInvDDGG = Get_BInvDC_BInvDG_WithSqueeze (C , D , nG , process_disp_console: process_disp_console , options: new string[0] , thld_BinvDC: thres_zeroblk / lstNewIdxRemv.Length , parallel: parallel ); HessMatrix HH = A - BBInvDDCC_BBInvDDGG.Item1; Vector FF = nF - BBInvDDCC_BBInvDDGG.Item2; double dbg_HH = (HH - H).HAbsMax(); double dbg_FF = (FF - F).ToArray().MaxAbs(); HDebug.Assert(Math.Abs(dbg_HH) < 0.00000001); HDebug.Assert(Math.Abs(dbg_FF) < 0.00000001); } GC.Collect(); } HDebug.Assert(H.ColSize == H.RowSize); HDebug.Assert(H.ColSize == F.Size); return(new HessForcInfo { hess = H, forc = F.ToVectors(3), }); }
static HessMatrix GetMulImpl(HessMatrix left, HessMatrix right, ILinAlg ila, bool warning) { if (HDebug.Selftest()) { Matrix h1 = new double[, ] { { 0, 1, 2, 3, 4, 5 } , { 1, 2, 3, 4, 5, 6 } , { 2, 3, 4, 5, 6, 7 } , { 3, 4, 5, 6, 7, 8 } , { 4, 5, 6, 7, 8, 9 } , { 5, 6, 7, 8, 9, 0 } }; HessMatrix h2 = HessMatrixSparse.FromMatrix(h1); Matrix h11 = Matrix.GetMul(h1, h1); HessMatrix h22 = HessMatrix.GetMulImpl(h2, h2, null, false); Matrix hdiff = h11 - h22; HDebug.AssertToleranceMatrix(0, hdiff); } if ((left is HessMatrixDense) && (right is HessMatrixDense)) { if (ila != null) { return(new HessMatrixDense { hess = ila.Mul(left, right) }); } if (warning) { HDebug.ToDo("Check (HessMatrixDense * HessMatrixDense) !!!"); } } Dictionary <int, Dictionary <int, Tuple <int, int, MatrixByArr> > > left_ic_rows = new Dictionary <int, Dictionary <int, Tuple <int, int, MatrixByArr> > >(); foreach (var ic_row in left.EnumRowBlocksAll()) { left_ic_rows.Add(ic_row.Item1, ic_row.Item2.HToDictionaryWithKeyItem2()); } Dictionary <int, Dictionary <int, Tuple <int, int, MatrixByArr> > > right_ir_cols = new Dictionary <int, Dictionary <int, Tuple <int, int, MatrixByArr> > >(); foreach (var ir_col in right.EnumColBlocksAll()) { right_ir_cols.Add(ir_col.Item1, ir_col.Item2.HToDictionaryWithKeyItem1()); } HessMatrix mul = null; if ((left is HessMatrixDense) && (right is HessMatrixDense)) { mul = HessMatrixDense.ZerosDense(left.ColSize, right.RowSize); } else { mul = HessMatrixSparse.ZerosSparse(left.ColSize, right.RowSize); } for (int ic = 0; ic < left.ColBlockSize; ic++) { var left_row = left_ic_rows[ic]; if (left_row.Count == 0) { continue; } for (int ir = 0; ir < right.RowBlockSize; ir++) { var right_col = right_ir_cols[ir]; if (right_col.Count == 0) { continue; } foreach (var left_ck in left_row) { int ik = left_ck.Key; HDebug.Assert(ic == left_ck.Value.Item1); HDebug.Assert(ik == left_ck.Value.Item2); if (right_col.ContainsKey(ik)) { var right_kr = right_col[ik]; HDebug.Assert(ik == right_kr.Item1); HDebug.Assert(ir == right_kr.Item2); MatrixByArr mul_ckr = mul.GetBlock(ic, ir) + left_ck.Value.Item3 * right_kr.Item3; mul.SetBlock(ic, ir, mul_ckr); } } } } return(mul); }