Esempio n. 1
0
            public bool CheckModeWithHess(MatrixByArr hess, double[] masses, ILinAlg la, Mode[] modes = null, double tolerance = 0.00001)
            {
                if (modes == null)
                {
                    modes = ToModes(masses);
                }

                HessMatrix ohess = new HessMatrixDense
                {
                    hess = modes.GetHessian(masses, la),
                };
                double corr = HMath.HCorr(hess.ToArray().HToArray1D(), ohess.ToArray().HToArray1D());

                System.Console.Write("corr(TestHess,HessVibr:{0}  ", corr);

                {
                    HDebug.Assert(hess.ColSize == ohess.ColSize);
                    HDebug.Assert(hess.RowSize == ohess.RowSize);
                    double ltolerance = hess.ToArray().HAbs().HMax() * tolerance;
                    ltolerance = tolerance;
                    for (int c = 0; c < ohess.ColSize; c++)
                    {
                        for (int r = 0; r < ohess.RowSize; r++)
                        {
                            double v    = hess[c, r];
                            double mwv  = ohess[c, r];
                            double diff = Math.Abs(v - mwv);
                            if (diff >= ltolerance)
                            {
                                return(false);
                            }
                        }
                    }
                }
                //{
                //    Matrix vhess  =  hess /  hess.ToArray().HToArray1D().HVar();
                //    Matrix vohess = ohess / ohess.ToArray().HToArray1D().HVar();
                //
                //    HDebug.Assert(hess.ColSize == ohess.ColSize);
                //    HDebug.Assert(hess.RowSize == ohess.RowSize);
                //    HDebug.Assert(vhess.Size == vohess.Size);
                //    for(int c=0; c<vhess.ColSize; c++)
                //    for(int r=0; r<vhess.RowSize; r++)
                //    {
                //        double v = vhess[c,r];
                //        double vo = vohess[c,r];
                //        double diff = Math.Abs(v - vo);
                //        if(diff >= tolerance)
                //        {
                //            HDebug.Assert(false);
                //            return false;
                //        }
                //    }
                //}
                return(true);
            }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        public static HessMatrix GetMulImpl(ILinAlg ila, bool warning, params HessMatrix[] mats)
        {
            if (ila != null)
            {
                if (HDebug.Selftest())
                {
                    Matrix h0 = 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 h1 = HessMatrixDense.FromMatrix(h0);
                    HessMatrix h2 = HessMatrixSparse.FromMatrix(h0);
                    Matrix     t0 = Matrix.GetMul(Matrix.GetMul(h1, h1), h1);
                    {
                        Matrix t1 = GetMulImpl(ila, false, h1, h1, h1); double d1 = (t0 - t1).HAbsMax(); HDebug.Assert(0 == d1);
                        Matrix t2 = GetMulImpl(ila, false, h1, h1, h2); double d2 = (t0 - t2).HAbsMax(); HDebug.Assert(0 == d2);
                        Matrix t3 = GetMulImpl(ila, false, h1, h2, h1); double d3 = (t0 - t3).HAbsMax(); HDebug.Assert(0 == d3);
                        Matrix t4 = GetMulImpl(ila, false, h1, h2, h2); double d4 = (t0 - t4).HAbsMax(); HDebug.Assert(0 == d4);
                        Matrix t5 = GetMulImpl(ila, false, h2, h1, h1); double d5 = (t0 - t5).HAbsMax(); HDebug.Assert(0 == d5);
                        Matrix t6 = GetMulImpl(ila, false, h2, h1, h2); double d6 = (t0 - t6).HAbsMax(); HDebug.Assert(0 == d6);
                        Matrix t7 = GetMulImpl(ila, false, h2, h2, h1); double d7 = (t0 - t7).HAbsMax(); HDebug.Assert(0 == d7);
                        Matrix t8 = GetMulImpl(ila, false, h2, h2, h2); double d8 = (t0 - t8).HAbsMax(); HDebug.Assert(0 == d8);
                    }
                    {
                        Matrix t1 = GetMulImpl(null, false, h1, h1, h1); double d1 = (t0 - t1).HAbsMax(); HDebug.Assert(0 == d1);
                        Matrix t2 = GetMulImpl(null, false, h1, h1, h2); double d2 = (t0 - t2).HAbsMax(); HDebug.Assert(0 == d2);
                        Matrix t3 = GetMulImpl(null, false, h1, h2, h1); double d3 = (t0 - t3).HAbsMax(); HDebug.Assert(0 == d3);
                        Matrix t4 = GetMulImpl(null, false, h1, h2, h2); double d4 = (t0 - t4).HAbsMax(); HDebug.Assert(0 == d4);
                        Matrix t5 = GetMulImpl(null, false, h2, h1, h1); double d5 = (t0 - t5).HAbsMax(); HDebug.Assert(0 == d5);
                        Matrix t6 = GetMulImpl(null, false, h2, h1, h2); double d6 = (t0 - t6).HAbsMax(); HDebug.Assert(0 == d6);
                        Matrix t7 = GetMulImpl(null, false, h2, h2, h1); double d7 = (t0 - t7).HAbsMax(); HDebug.Assert(0 == d7);
                        Matrix t8 = GetMulImpl(null, false, h2, h2, h2); double d8 = (t0 - t8).HAbsMax(); HDebug.Assert(0 == d8);
                    }
                }
            }

            HessMatrix mul = null;

            foreach (HessMatrix mat in mats)
            {
                if (mul == null)
                {
                    mul = mat;
                }
                else
                {
                    mul = GetMulImpl(mul, mat, ila, warning);
                }
            }
            return(mul);
        }
Esempio n. 4
0
        public static HessMatrix GetHessApproxAnm(Vector[] coords, HessMatrix hess, string option, ILinAlg ila)
        {
            int nresi = coords.Length;

            HDebug.Assert(nresi * 3 == hess.ColSize);
            HDebug.Assert(nresi * 3 == hess.RowSize);

            double[,] Kij = new double[nresi, nresi];

            for (int c = 0; c < nresi; c++)
            {
                for (int r = c + 1; r < nresi; r++)
                {
                    HessMatrix anmCR = Hess.GetHessAnm(coords.HSelectByIndex(new int[] { c, r }));
                    //Matrix  anmCR = anm.SubMatrix(new int[] { 0, 1, 2 }, new int[] { 3, 4, 5 });
                    int[]      idxs   = new int[] { c *3 + 0, c *3 + 1, c *3 + 2, r *3 + 0, r *3 + 1, r *3 + 2 };
                    HessMatrix hessCR = new HessMatrixDense {
                        hess = hess.SubMatrix(idxs, idxs)
                    };
                    hessCR = Hess.CorrectHessDiag(hessCR);

                    Vector vecHessCR = hessCR.GetColVectorList().ToVector();
                    Vector vecAnmCR  = anmCR.GetColVectorList().ToVector();

                    double Kcr;
                    switch (option)
                    {
                    case "match magnitude": Kcr = vecHessCR.Dist / vecAnmCR.Dist; break;

                    case "least-mean square": Kcr = LinAlg.VtV(vecAnmCR, vecHessCR) / LinAlg.VtV(vecAnmCR, vecAnmCR); break;

                    default: throw new NotImplementedException();
                    }

                    if (Kcr < 0)
                    {
                        HDebug.Assert(false);
                    }
                    HDebug.Assert(Kcr >= 0);
                    double mag2cahessCR = vecAnmCR.Dist;
                    double mag2caAnmCR  = vecHessCR.Dist;
                    double mag2caAnmCRx = (Kcr * anmCR).GetColVectorList().ToVector().Dist;
                    Kij[c, r] = Kij[r, c] = Kcr;
                }
            }

            //return Kij;
            return(Hess.GetHessAnm(coords, Kij));

            throw new NotImplementedException();
        }
Esempio n. 5
0
        public HessMatrix SubMatrixByAtomsImpl0(IList <int> idxColAtoms, IList <int> idxRowAtoms)
        {
            if (SubMatrixByAtomsImpl0_selftest2)
            {
                SubMatrixByAtomsImpl0_selftest2 = false;
                Matrix thess1 = 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 thess2 = HessMatrixDense.FromMatrix(thess1);
                HessMatrix thess3 = thess2.SubMatrixByAtomsImpl0(new int[] { 0 }, new int[] { 1 });
                Matrix     thess4 = new double[, ] {
                    { 3, 4, 5 }
                    , { 4, 5, 6 }
                    , { 5, 6, 7 }
                };
                HDebug.AssertToleranceMatrix(0, thess3 - thess4);
            }
            HessMatrix nhess = Zeros(idxColAtoms.Count * 3, idxRowAtoms.Count * 3);

            for (int nbc = 0; nbc < idxColAtoms.Count; nbc++)
            {
                for (int nbr = 0; nbr < idxRowAtoms.Count; nbr++)
                {
                    int bc = idxColAtoms[nbc]; if (bc < 0)
                    {
                        continue;
                    }
                    int br = idxRowAtoms[nbr]; if (br < 0)
                    {
                        continue;
                    }
                    if (HasBlock(bc, br) == false)
                    {
                        continue;
                    }
                    MatrixByArr block = GetBlock(bc, br).CloneT(); // hessian matrix for interaction between atom i and j
                    HDebug.Assert(block.IsZero() == false);
                    nhess.SetBlock(nbc, nbr, block);
                }
            }

            return(nhess);
        }
Esempio n. 6
0
        //public static void GetHessAnm(IList<Vector> coords, double cutoff, MatrixSparse<MatrixByArr> hess)
        //{
        //    int n = coords.Count;
        //
        //    MatrixSparse<MatrixByArr> lhess = null;
        //    if(HDebug.Selftest())
        //        lhess = new MatrixSparse<MatrixByArr>(n, n, hess.GetDefault);
        //
        //    double cutoff2 = cutoff * cutoff;
        //    for(int i=0; i<n; i++)
        //    {
        //        if(coords[i] == null)
        //            continue;
        //        for(int j=0; j<n; j++)
        //        {
        //            if(i == j)
        //                continue;
        //            if(coords[j] == null)
        //                continue;
        //            Vector vec_ij = coords[j] - coords[i];
        //            double dist2 = vec_ij.Dist2;
        //            if(dist2 > cutoff2)
        //                continue;
        //
        //            Vector unit_ij = vec_ij.UnitVector();
        //            double k_ij = 1;
        //            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[i, j] -= hessij;
        //            hess[i, i] += hessij;
        //            if(lhess != null)
        //            {
        //                lhess[i, j] -= hessij;
        //                lhess[i, i] += hessij;
        //            }
        //        }
        //    }
        //
        //    if(lhess != null)
        //    {
        //        MatrixByArr lhess0 = GetHessAnm(coords, cutoff);
        //        MatrixByArr lhess1 = MatrixByArr.FromMatrixArray(lhess.ToArray());
        //        HDebug.AssertTolerance(0.00000000001, lhess0-lhess1);
        //    }
        //}
        static HessMatrix GetHessAnm_debug(IList <Vector> coords, Matrix Kij)
        {
            //Debug.Assert(AnmHessianSelfTest());

            int        n    = coords.Count;
            HessMatrix hess = HessMatrixDense.ZerosDense(n * 3, n * 3);

            for (int i = 0; i < n; i++)
            {
                if (coords[i] == null)
                {
                    continue;
                }
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        HDebug.Assert(Kij[i, j] == 0);
                        continue;
                    }
                    if (coords[j] == null)
                    {
                        continue;
                    }
                    Vector vec_ij  = coords[j] - coords[i];
                    Vector unit_ij = vec_ij.UnitVector();
                    double k_ij    = Kij[i, j];
                    HDebug.Assert(double.IsNaN(k_ij) == false);
                    if (k_ij == 0)
                    {
                        continue;
                    }
                    hess[i * 3 + 0, j *3 + 0] = -k_ij * unit_ij[0] * unit_ij[0];    hess[i * 3 + 0, i *3 + 0] += k_ij * unit_ij[0] * unit_ij[0];
                    hess[i * 3 + 0, j *3 + 1] = -k_ij * unit_ij[0] * unit_ij[1];    hess[i * 3 + 0, i *3 + 1] += k_ij * unit_ij[0] * unit_ij[1];
                    hess[i * 3 + 0, j *3 + 2] = -k_ij * unit_ij[0] * unit_ij[2];    hess[i * 3 + 0, i *3 + 2] += k_ij * unit_ij[0] * unit_ij[2];
                    hess[i * 3 + 1, j *3 + 0] = -k_ij * unit_ij[1] * unit_ij[0];    hess[i * 3 + 1, i *3 + 0] += k_ij * unit_ij[1] * unit_ij[0];
                    hess[i * 3 + 1, j *3 + 1] = -k_ij * unit_ij[1] * unit_ij[1];    hess[i * 3 + 1, i *3 + 1] += k_ij * unit_ij[1] * unit_ij[1];
                    hess[i * 3 + 1, j *3 + 2] = -k_ij * unit_ij[1] * unit_ij[2];    hess[i * 3 + 1, i *3 + 2] += k_ij * unit_ij[1] * unit_ij[2];
                    hess[i * 3 + 2, j *3 + 0] = -k_ij * unit_ij[2] * unit_ij[0];    hess[i * 3 + 2, i *3 + 0] += k_ij * unit_ij[2] * unit_ij[0];
                    hess[i * 3 + 2, j *3 + 1] = -k_ij * unit_ij[2] * unit_ij[1];    hess[i * 3 + 2, i *3 + 1] += k_ij * unit_ij[2] * unit_ij[1];
                    hess[i * 3 + 2, j *3 + 2] = -k_ij * unit_ij[2] * unit_ij[2];    hess[i * 3 + 2, i *3 + 2] += k_ij * unit_ij[2] * unit_ij[2];
                }
            }
            return(hess);
        }
Esempio n. 7
0
        public static HessMatrix GetHessAnm(IList <Vector> coords)
        {
            //Debug.Assert(AnmHessianSelfTest());

            int        n    = coords.Count;
            HessMatrix hess = HessMatrixDense.ZerosDense(n * 3, n * 3);

            for (int i = 0; i < n; i++)
            {
                if (coords[i] == null)
                {
                    continue;
                }
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    if (coords[j] == null)
                    {
                        continue;
                    }
                    Vector vec_ij = coords[j] - coords[i];
                    double dist2  = vec_ij.Dist2;

                    Vector unit_ij = vec_ij.UnitVector();
                    double k_ij    = 1.0 / dist2;
                    hess[i * 3 + 0, j *3 + 0] = double.Epsilon - k_ij * unit_ij[0] * unit_ij[0]; hess[i * 3 + 0, i *3 + 0] += k_ij * unit_ij[0] * unit_ij[0];
                    hess[i * 3 + 0, j *3 + 1] = double.Epsilon - k_ij * unit_ij[0] * unit_ij[1]; hess[i * 3 + 0, i *3 + 1] += k_ij * unit_ij[0] * unit_ij[1];
                    hess[i * 3 + 0, j *3 + 2] = double.Epsilon - k_ij * unit_ij[0] * unit_ij[2]; hess[i * 3 + 0, i *3 + 2] += k_ij * unit_ij[0] * unit_ij[2];
                    hess[i * 3 + 1, j *3 + 0] = double.Epsilon - k_ij * unit_ij[1] * unit_ij[0]; hess[i * 3 + 1, i *3 + 0] += k_ij * unit_ij[1] * unit_ij[0];
                    hess[i * 3 + 1, j *3 + 1] = double.Epsilon - k_ij * unit_ij[1] * unit_ij[1]; hess[i * 3 + 1, i *3 + 1] += k_ij * unit_ij[1] * unit_ij[1];
                    hess[i * 3 + 1, j *3 + 2] = double.Epsilon - k_ij * unit_ij[1] * unit_ij[2]; hess[i * 3 + 1, i *3 + 2] += k_ij * unit_ij[1] * unit_ij[2];
                    hess[i * 3 + 2, j *3 + 0] = double.Epsilon - k_ij * unit_ij[2] * unit_ij[0]; hess[i * 3 + 2, i *3 + 0] += k_ij * unit_ij[2] * unit_ij[0];
                    hess[i * 3 + 2, j *3 + 1] = double.Epsilon - k_ij * unit_ij[2] * unit_ij[1]; hess[i * 3 + 2, i *3 + 1] += k_ij * unit_ij[2] * unit_ij[1];
                    hess[i * 3 + 2, j *3 + 2] = double.Epsilon - k_ij * unit_ij[2] * unit_ij[2]; hess[i * 3 + 2, i *3 + 2] += k_ij * unit_ij[2] * unit_ij[2];
                }
            }
            return(hess);
        }
Esempio n. 8
0
        /// Lei Zhou and Steven A. Siegelbaum,
        /// Effects of Surface Water on Protein Dynamics Studied by a Novel Coarse-Grained Normal Mode Approach
        /// Biophysical Journal, Volume 94, May 2008
        /// http://www.ncbi.nlm.nih.gov/pmc/articles/PMC2292380/pdf/3461.pdf
        ///
        /// Divide Hessian matrix into heavy atoms and others:
        ///   Hess = [ HH HL ]
        ///          [ LH LL ],
        /// where H and L imply heavy and light atoms, respectively.
        ///
        /// Using block inverse matrix
        ///   [A B]-1 = [ (A-B D^-1 C)^-1  ... ]
        ///   [C D]     [ ...              ... ],
        /// find the HH coarse-grain block of Hessian matrix:
        ///   Hess_HH = HH - HL * LL^-1 * LH

        public static HessMatrix GetHessCoarseBlkmat(Matrix hess, IList <int> idx_heavy, ILinAlg ila, double?chkDiagToler, string invtype, params object[] invopt)
        {
            List <int> idxhess = new List <int>();

            foreach (int idx in idx_heavy)
            {
                idxhess.Add(idx * 3 + 0);
                idxhess.Add(idx * 3 + 1);
                idxhess.Add(idx * 3 + 2);
            }

            HessMatrix hess_HH = new HessMatrixDense {
                hess = hess.InvOfSubMatrixOfInv(idxhess, ila, invtype, invopt)
            };

            if (chkDiagToler == null)
            {
                chkDiagToler = 0.000001;
            }
            HDebug.Assert(Hess.CheckHessDiag(hess_HH, chkDiagToler.Value));
            return(hess_HH);
        }
            public static CGetHessCoarseResiIterImpl GetHessCoarseResiIterImpl_ILinAlg_20150329(HessMatrix H, List <int>[] lstNewIdxRemv, double thres_zeroblk, ILinAlg ila, bool cloneH)
            {
                if (cloneH)
                {
                    H = H.CloneHess();
                }

                //System.Console.WriteLine("begin coarse-graining");
                List <HessCoarseResiIterInfo> iterinfos = new List <HessCoarseResiIterInfo>();

                for (int iter = lstNewIdxRemv.Length - 1; iter >= 0; 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;
                    {
                        //HessMatrix    A = H.SubMatrixByAtoms(false, idxkeep, idxkeep);
                        HessMatrix A = H;
                        //HessMatrix    B = H.SubMatrixByAtoms(false, idxkeep, idxremv);
                        HessMatrix C    = H.SubMatrixByAtoms(false, idxremv, idxkeep);
                        HessMatrix D    = H.SubMatrixByAtoms(false, idxremv, idxremv);
                        HessMatrix invD = new HessMatrixDense {
                            hess = ila.InvSymm(D)
                        };

                        // make B,C sparse
                        //int B_cntzero = B.MakeNearZeroBlockAsZero(thres_zeroblk);
                        iterinfo.numSetZeroBlock = C.MakeNearZeroBlockAsZero(thres_zeroblk);
                        //int B_nzeros = B.NumUsedBlocks; double B_nzeros_ = Math.Sqrt(B_nzeros);
                        iterinfo.numNonZeroBlock = C.NumUsedBlocks;

                        HessMatrix B        = C.Tr();
                        HessMatrix B_invD_C = B * invD * C;
                        iterinfo.numAddIgnrBlock = A.UpdateAdd(B_invD_C, -1, null, thres_zeroblk / lstNewIdxRemv.Length);
                        //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);

                    //System.Console.WriteLine(" - {0:000} : makezero {1,5}, nonzero {2,5}, numIgnMul {3,7}, numRemvAtoms {4,3}, {5,5:0.00} sec, {6} mb, {7}x{7}"
                    //                        , iter
                    //                        , iterinfo.numSetZeroBlock
                    //                        , iterinfo.numNonZeroBlock
                    //                        , iterinfo.numAddIgnrBlock
                    //                        , iterinfo.numAtomsRemoved
                    //                        , iterinfo.compSec
                    //                        , iterinfo.usedMemoryByte/(1024*1024)
                    //                        , (idxkeep.Length*3)
                    //                        );

                    GC.Collect();
                }

                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 CGetHessCoarseResiIterImpl GetHessCoarseResiIterImpl_ILinAlg(HessMatrix H, List <int>[] lstNewIdxRemv, double thres_zeroblk, ILinAlg ila, bool cloneH)
            {
                if (cloneH)
                {
                    H = H.CloneHess();
                }

                bool process_disp_console = true;

                DateTime[] process_time = new DateTime[7];

                //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 = H.SubMatrixByAtoms(false, idxremv, idxkeep);                                      if (process_disp_console)
                        {
                            process_time[1] = DateTime.UtcNow; System.Console.Write("C({0:00.00} min), ", (process_time[1] - process_time[0]).TotalMinutes);
                        }
                        HessMatrix D = H.SubMatrixByAtoms(false, idxremv, idxremv);                                      if (process_disp_console)
                        {
                            process_time[2] = DateTime.UtcNow; System.Console.Write("D({0:00.00} min), ", (process_time[2] - process_time[1]).TotalMinutes);
                        }
                        HessMatrix invD = new HessMatrixDense {
                            hess = ila.InvSymm(D)
                        };                                      if (process_disp_console)
                        {
                            process_time[3] = DateTime.UtcNow; System.Console.Write("invD({0:00.00} min), ", (process_time[3] - process_time[2]).TotalMinutes);
                        }

                        // make B,C sparse
                        //int B_cntzero = B.MakeNearZeroBlockAsZero(thres_zeroblk);
                        C_density0 = C.RatioUsedBlocks;
                        iterinfo.numSetZeroBlock = C.MakeNearZeroBlockAsZero(thres_zeroblk);                                if (process_disp_console)
                        {
                            process_time[4] = DateTime.UtcNow; System.Console.Write("sparseC({0:00.00} min), ", (process_time[4] - process_time[3]).TotalMinutes);
                        }
                        //int B_nzeros = B.NumUsedBlocks; double B_nzeros_ = Math.Sqrt(B_nzeros);
                        iterinfo.numNonZeroBlock = C.NumUsedBlocks;
                        C_density1 = C.RatioUsedBlocks;

                        HessMatrix B        = C.Tr();
                        HessMatrix B_invD_C = HessMatrix.GetMul(ila, B, invD, C); /* B * invD * C;*/ if (process_disp_console)
                        {
                            process_time[5] = DateTime.UtcNow; System.Console.Write("B.invD.C({0:00.00} min), ", (process_time[5] - process_time[4]).TotalMinutes);
                        }
                        iterinfo.numAddIgnrBlock = A.UpdateAdd(B_invD_C, -1, null, thres_zeroblk / lstNewIdxRemv.Length);     if (process_disp_console)
                        {
                            process_time[6] = DateTime.UtcNow; System.Console.Write("A+BinvDC({0:00.00} min), ", (process_time[6] - process_time[5]).TotalMinutes);
                        }
                        //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,
                });
            }
Esempio n. 11
0
            public static CTesthess ReadHess
                (Xyz xyz
                , string outputpath
                , Dictionary <string, string[]> optOutSource  // =null
                , Func <int, int, HessMatrix> HessMatrixZeros // =null
                )
            {
                int size = xyz.atoms.Length;

                #region format: output.txt
                ///      ######################################################################
                ///    ##########################################################################
                ///   ###                                                                      ###
                ///  ###            TINKER  ---  Software Tools for Molecular Design            ###
                ///  ##                                                                          ##
                ///  ##                        Version 6.2  February 2013                        ##
                ///  ##                                                                          ##
                ///  ##               Copyright (c)  Jay William Ponder  1990-2013               ##
                ///  ###                           All Rights Reserved                          ###
                ///   ###                                                                      ###
                ///    ##########################################################################
                ///      ######################################################################
                ///
                ///
                ///  Atoms with an Unusual Number of Attached Atoms :
                ///
                ///  Type           Atom Name      Atom Type       Expected    Found
                ///
                ///  Valence        1496-NR2           69              2         3
                ///  Valence        2438-FE           107              6         5
                ///
                ///  Diagonal Hessian Elements for Each Atom :
                ///
                ///       Atom                 X               Y               Z
                ///
                ///          1            1445.9830       1358.3447       1484.8642
                ///          2             212.9895        294.4584        604.5062
                ///          3             187.1986        751.6727        125.0336
                ///          4             813.4799        132.1743        151.5707
                ///        ...
                ///       2507             178.7277        479.7434        243.4103
                ///       2508             969.8813        977.2507       1845.6726
                ///       2509             390.1648        232.0577       1029.6844
                ///       2510             322.3674        337.8749        996.8230
                ///
                ///  Sum of Diagonal Hessian Elements :          5209042.9060
                ///
                ///  Hessian Matrix written to File :  test.hes
                #endregion
                Tuple <int, double, double, double>[] outDiagHessElems = new Tuple <int, double, double, double> [size];
                double?outSumDiagHessElem = null;
                string outHessMatFile     = null;
                string outHessFormat      = null;
                {
                    string step = "";
                    foreach (string _line in HFile.ReadLines(outputpath))
                    {
                        string line = _line.Trim();
                        if (line.Length == 0)
                        {
                            continue;
                        }
                        if (line.Trim().StartsWith("#"))
                        {
                            continue;
                        }
                        if (line.Contains(" :"))
                        {
                            if (line.Contains("Atoms with an Unusual Number of Attached Atoms"))
                            {
                                step = "";                  continue;
                            }
                            else if (line.Contains("Diagonal Hessian Elements for Each Atom"))
                            {
                                step = "hess diagonal";     continue;
                            }
                            else if (line.Contains("Sum of Diagonal Hessian Elements"))
                            {
                                step = "sum hess diagonal";
                            }
                            else if (line.Contains("Hessian Matrix written to File"))
                            {
                                step = "full hess file";
                            }
                            else if (line.Contains("Hessian Sparse Matrix written to File"))
                            {
                                step = "sparse hess file";
                            }
                            else if (line.Contains("Select Smaller Size for Sparse Hessian Matrix"))
                            {
                                continue;
                            }
                            else
                            {
                                HDebug.Assert(false);
                                continue;
                            }
                        }
                        switch (step)
                        {
                        case "":
                            continue;

                        case "hess diagonal":
                        {
                            string[] tokens = line.Split().HRemoveAll("");
                            if (tokens[0] == "Atom")
                            {
                                continue;
                            }
                            int    Atom = int.Parse(tokens[0]);         // ; if(   int.TryParse(tokens[0], out Atom) == false) continue;
                            double X    = double.Parse(tokens[1]);      // ; if(double.TryParse(tokens[1], out    X) == false) continue;
                            double Y    = double.Parse(tokens[2]);      // ; if(double.TryParse(tokens[2], out    Y) == false) continue;
                            double Z    = double.Parse(tokens[3]);      // ; if(double.TryParse(tokens[3], out    Z) == false) continue;
                            HDebug.Assert(outDiagHessElems[Atom - 1] == null);
                            outDiagHessElems[Atom - 1] = new Tuple <int, double, double, double>(Atom, X, Y, Z);
                        }
                            continue;

                        case "sum hess diagonal":
                        {
                            string[] tokens = line.Split().HRemoveAll("");
                            double   sum    = double.Parse(tokens.Last());    // if(double.TryParse(tokens.Last(), out sum) == false) continue;
                            outSumDiagHessElem = sum;
                        }
                            step = "";
                            continue;

                        case   "full hess file": outHessFormat = "full matrix"; goto case "hess file";

                        case "sparse hess file": outHessFormat = "sparse matrix"; goto case "hess file";

                        case "hess file":
                        {
                            string[] tokens         = line.Split().HRemoveAll("");
                            string   outputpath_dir = HFile.GetFileInfo(outputpath).Directory.FullName;
                            outHessMatFile = outputpath_dir + "\\" + tokens.Last();
                        }
                            step = "";
                            continue;

                        default:
                            HDebug.Assert(false);
                            step = "";
                            continue;
                        }
                    }
                }
                #region Format: test.hes
                /// Diagonal Hessian Elements  (3 per Atom)
                ///
                ///   1445.9830   1358.3447   1484.8642    212.9895    294.4584    604.5062
                ///    187.1986    751.6727    125.0336    813.4799    132.1743    151.5707
                ///   1167.7472   1188.3459   1162.5475    268.8291    498.0188    143.2438
                ///   ...
                /// Off-diagonal Hessian Elements for Atom     1 X
                ///     23.0878     14.6380   -214.0545     96.9308   -186.7740   -205.1460
                ///   -208.7035    -28.9630   -788.9124     39.6748    126.4491   -190.5074
                ///   ...
                /// Off-diagonal Hessian Elements for Atom     1 Y
                ///    -64.1616     97.2697   -292.3813    264.1922   -184.4701   -728.8687
                ///   -109.9103     15.4168   -152.5717    -11.7733     18.8369   -188.4875
                ///   ...
                /// Off-diagonal Hessian Elements for Atom     1 Z
                ///   -155.9409    219.8782   -610.0622    -10.0041    -62.8209   -156.1112
                ///     77.0829    -14.2127   -166.0525     53.1834    -97.4207   -347.9315
                ///   ...
                /// Off-diagonal Hessian Elements for Atom     2 X
                ///   -106.5166    185.8395     19.7546    -10.0094     25.8072     -8.1335
                ///     35.8501    -64.8320      3.8761    -13.8893      9.4266      0.1312
                ///   ...
                ///
                ///   ...
                ///
                /// Off-diagonal Hessian Elements for Atom  2508 X
                ///    488.4590    -69.2721   -338.4824   -155.1656    253.3732   -297.4214
                ///   -164.8301   -191.3769
                /// Off-diagonal Hessian Elements for Atom  2508 Y
                ///     74.0161   -149.1060   -273.4863    176.4975   -170.6527   -341.1639
                ///   -263.6141
                /// Off-diagonal Hessian Elements for Atom  2508 Z
                ///    304.9942    223.7496   -851.3028   -242.9481   -310.7953   -821.2532
                /// Off-diagonal Hessian Elements for Atom  2509 X
                ///    198.4498   -331.6530     78.8172     17.3909    -30.0512
                /// Off-diagonal Hessian Elements for Atom  2509 Y
                ///   -227.5916     25.7235     74.5131    -53.2690
                /// Off-diagonal Hessian Elements for Atom  2509 Z
                ///     46.7873     20.7615   -168.0704
                /// Off-diagonal Hessian Elements for Atom  2510 X
                ///    252.6853    247.2963
                /// Off-diagonal Hessian Elements for Atom  2510 Y
                ///    343.6797
                #endregion
                HessMatrix hess;
                {
                    if (HessMatrixZeros != null)
                    {
                        hess = HessMatrixZeros(size * 3, size * 3);
                    }
                    else
                    {
                        switch (outHessFormat)
                        {
                        case "full matrix": hess = HessMatrixDense.ZerosDense(size * 3, size * 3); break;

                        case "sparse matrix": hess = HessMatrixSparse.ZerosSparse(size * 3, size * 3); break;

                        default:              hess = null;                                             break;
                        }
                    }
                    //HDebug.SetEpsilon(hess);
                    string step    = "";
                    int    diagidx = -1;
                    int    offidx1 = -1;
                    int    offidx2 = -1;

                    /// string[] lines = HFile.ReadAllLines(outHessMatFile);
                    /// GC.WaitForFullGCComplete();
                    /// object[] tokenss = new object[lines.Length];
                    ///
                    /// Parallel.For(0, lines.Length, delegate(int i)
                    /// //for(int i=0; i<lines.Length; i++)
                    /// {
                    ///     string line = lines[i];
                    ///     string[] stokens = line.Trim().Split().RemoveAll("");
                    ///     if(stokens.Length != 0)
                    ///     {
                    ///         double[] dtokens = new double[stokens.Length];
                    ///         for(int j=0; j<stokens.Length; j++)
                    ///             if(double.TryParse(stokens[j], out dtokens[j]) == false)
                    ///             {
                    ///                 tokenss[i] = stokens;
                    ///                 goto endfor;
                    ///             }
                    ///         tokenss[i] = dtokens;
                    ///     }
                    /// endfor: ;
                    /// });

                    /// System.IO.StreamReader matreader = HFile.OpenText(outHessMatFile);
                    /// while(matreader.EndOfStream == false)
                    /// for(int i=0; i<lines.Length; i++)
                    /// foreach(string line in lines)
                    foreach (string line in HFile.HEnumAllLines(outHessMatFile))
                    {
                        ///     string line = lines[i];
                        ///     string line = matreader.ReadLine();
                        string[] tokens = line.Trim().Split().HRemoveAll("");
                        if (tokens.Length == 0)
                        {
                            continue;
                        }
                        ///     if(tokenss[i] == null)
                        ///         continue;
                        ///     string[] stokens = (tokenss[i] as string[]);
                        if (tokens[0] == "Diagonal" && tokens[1] == "Hessian" && tokens[2] == "Elements")
                        {
                            step = "Diagonal"; diagidx = 0; continue;
                        }
                        if (tokens[0] == "Off-diagonal" && tokens[1] == "Hessian" && tokens[2] == "Elements")
                        {
                            step = "Off-diagonal"; offidx1++; offidx2 = offidx1 + 1; continue;
                        }
                        if (tokens[0] == "Off-diagonal" && tokens[1] == "sparse" && tokens[2] == "Hessian" &&
                            tokens[3] == "Indexes/Elements" && tokens[4] == "for" && tokens[5] == "Atom")
                        {
                            step    = "Off-diagonal sparse";
                            offidx1 = (int.Parse(tokens[6]) - 1) * 3;
                            switch (tokens[7])
                            {
                            case "X": break;

                            case "Y": offidx1 += 1; break;

                            case "Z": offidx1 += 2; break;

                            default: throw new HException();
                            }
                            continue;
                        }

                        ///     double[] dtokens = (tokenss[i] as double[]);
                        ///     tokens = new string[20];
                        ///     for(int i=0; i*12<line.Length; i++)
                        ///         tokens[i] = line.Substring(i*12, 12).Trim();
                        ///     tokens = tokens.HRemoveAll("").ToArray();
                        ///     tokens = tokens.HRemoveAll(null).ToArray();

                        switch (step)
                        {
                        case "Diagonal":
                        {
                            ///                 foreach(double val in dtokens)
                            foreach (string token in tokens)
                            {
                                HDebug.Assert(token.Last() != ' ');
                                double val = double.Parse(token);
                                HDebug.Assert(hess[diagidx, diagidx] == 0);
                                hess[diagidx, diagidx] = val;
                                diagidx++;
                            }
                        }
                            continue;

                        case "Off-diagonal":
                        {
                            ///                 foreach(double val in dtokens)
                            foreach (string token in tokens)
                            {
                                HDebug.Assert(token.Last() != ' ');
                                //double val = double.Parse(token);
                                double val;
                                bool   succ = double.TryParse(token, out val);
                                if (succ == false)
                                {
                                    HDebug.Assert(false);
                                    goto case "exception 0";
                                }
                                if (size < 3000)
                                {
                                    HDebug.Assert(hess[offidx1, offidx2] == 0);
                                    HDebug.Assert(hess[offidx2, offidx1] == 0);
                                }
                                if (val != 0)
                                {
                                    hess[offidx1, offidx2] = val;
                                    hess[offidx2, offidx1] = val;
                                }
                                offidx2++;
                            }
                        }
                            continue;

                        case "Off-diagonal sparse":
                        {
                            HDebug.Assert(tokens.Length % 2 == 0);
                            for (int i = 0; i < tokens.Length; i += 2)
                            {
                                HDebug.Assert(tokens[i].Last() != ' ');
                                HDebug.Assert(tokens[i + 1].Last() != ' ');
                                //double val = double.Parse(token);
                                double val;
                                bool   succ = true;
                                succ &= int.TryParse(tokens[i], out offidx2); offidx2--;
                                succ &= double.TryParse(tokens[i + 1], out val);
                                if (succ == false)
                                {
                                    HDebug.Assert(false);
                                    goto case "exception 0";
                                }
                                if (size < 3000)
                                {
                                    HDebug.Assert(hess[offidx1, offidx2] == 0);
                                    HDebug.Assert(hess[offidx2, offidx1] == 0);
                                }
                                if (val != 0)
                                {
                                    hess[offidx1, offidx2] = val;
                                    hess[offidx2, offidx1] = val;
                                }
                            }
                        }
                            continue;

                        default:
                            HDebug.Assert(false);
                            goto case "exception 1";

                        case "exception 0":
                            throw new HException("incomplete hessian file (0)");

                        case "exception 1":
                            throw new HException("incomplete hessian file (1)");
                        }
                        throw new HException("incomplete hessian file (2)");
                    }
                    /// matreader.Close();

                    if (HDebug.IsDebuggerAttached && size < 3000)
                    {
                        //for(int c=0; c<size*3; c++)
                        //    for(int r=0; r<size*3; r++)
                        //        if(hess[c, r] == double.Epsilon)
                        //            throw new Exception("incomplete hessian file (3)");
                        //
                        //for(int c=0; c<size*3; c++)
                        //{
                        //    double sum = 0;
                        //    double max = double.NegativeInfinity;
                        //    for(int r=0; r<size*3; r++)
                        //    {
                        //        max = Math.Max(max, hess[c, r]);
                        //        sum += hess[c, r];
                        //        HDebug.Assert(hess[c, r] != double.Epsilon);
                        //    }
                        //    HDebug.Assert(Math.Abs(sum) < max/1000);
                        //    HDebug.AssertTolerance(0.1, sum);
                        //}

                        Hess.CheckHessDiag(hess, 0.1, null);

                        for (int i = 0; i < size; i++)
                        {
                            HDebug.Assert(outDiagHessElems[i].Item1 == i + 1);
                            HDebug.AssertTolerance(0.000001, outDiagHessElems[i].Item2 - hess[i * 3 + 0, i * 3 + 0]);
                            HDebug.AssertTolerance(0.000001, outDiagHessElems[i].Item3 - hess[i * 3 + 1, i * 3 + 1]);
                            HDebug.AssertTolerance(0.000001, outDiagHessElems[i].Item4 - hess[i * 3 + 2, i * 3 + 2]);
                        }
                    }
                }

                if (optOutSource != null)
                {
                    optOutSource.Add("test.hes", HFile.HEnumAllLines(outHessMatFile).ToArray());
                }

                return(new CTesthess
                {
                    hess = hess,
                    xyz = xyz,
                });
            }
Esempio n. 12
0
            public static CGetHessCoarseResiIterImpl GetHessCoarseResiIterImpl_Matlab(HessMatrix H, List <int>[] lstNewIdxRemv, double thres_zeroblk)
            {
                HessMatrix CGH = null;
                List <HessCoarseResiIterInfo> iterinfos = new List <HessCoarseResiIterInfo>();

                using (new Matlab.NamedLock("CGHessIter"))
                {
                    Matlab.PutSparseMatrix("CG.H", H.GetMatrixSparse(), 3, 3);
                    Matlab.PutValue("CG.th", thres_zeroblk);
                    Matlab.PutValue("CG.iter", lstNewIdxRemv.Length);
                    for (int iter = lstNewIdxRemv.Length - 1; iter >= 0; iter--)
                    {
                        int[] iremv   = lstNewIdxRemv[iter].ToArray();
                        int[] idxkeep = HEnum.HEnumFromTo(0, iremv.Min() - 1).ToArray();
                        int[] idxremv = HEnum.HEnumFromTo(iremv.Min(), iremv.Max()).ToArray();
                        Matlab.PutVector("CG.idxkeep", idxkeep);
                        Matlab.PutVector("CG.idxremv", idxremv);
                        Matlab.Execute("CG.idxkeep = sort([CG.idxkeep*3+1; CG.idxkeep*3+2; CG.idxkeep*3+3]);");
                        Matlab.Execute("CG.idxremv = sort([CG.idxremv*3+1; CG.idxremv*3+2; CG.idxremv*3+3]);");

                        HessCoarseResiIterInfo iterinfo = new HessCoarseResiIterInfo();
                        iterinfo.sizeHessBlkMat  = idxremv.Max() + 1; // H.ColBlockSize;
                        iterinfo.numAtomsRemoved = idxremv.Length;
                        iterinfo.idxkeep         = idxkeep.HClone();
                        iterinfo.idxremv         = idxremv.HClone();
                        iterinfo.time0           = DateTime.UtcNow;

                        if (HDebug.IsDebuggerAttached)
                        {
                            int maxkeep = Matlab.GetValueInt("max(CG.idxkeep)");
                            int minremv = Matlab.GetValueInt("min(CG.idxremv)");
                            HDebug.Assert(maxkeep + 1 == minremv);
                            int maxremv = Matlab.GetValueInt("max(CG.idxremv)");
                            int Hsize   = Matlab.GetValueInt("max(size(CG.H))");
                            HDebug.Assert(Hsize == maxremv);
                            int idxsize = Matlab.GetValueInt("length(union(CG.idxkeep,CG.idxremv))");
                            HDebug.Assert(Hsize == idxsize);
                        }
                        Matlab.Execute("CG.A = CG.H(CG.idxkeep, CG.idxkeep);");
                        Matlab.Execute("CG.B = CG.H(CG.idxkeep, CG.idxremv);");
                        //Matlab.Execute("CG.C = CG.H(CG.idxremv, CG.idxkeep);");
                        Matlab.Execute("CG.D = CG.H(CG.idxremv, CG.idxremv);");
                        HDebug.Assert(false);
                        Matlab.Execute("CG.B(abs(CG.B) < CG.th) = 0;");     /// matlab cannot handle this call. Matlab try to use 20G memory.
                        Matlab.Execute("CG.BDC = CG.B * inv(full(CG.D)) * CG.B';");
                        Matlab.Execute("CG.BDC = sparse(CG.BDC);");
                        Matlab.Execute("CG.BDC(abs(CG.BDC) < (CG.th / CG.iter)) = 0;");
                        Matlab.Execute("CG.H = CG.A - sparse(CG.BDC);");

                        iterinfo.numSetZeroBlock = -1;
                        iterinfo.numNonZeroBlock = -1;
                        iterinfo.numAddIgnrBlock = -1;
                        iterinfo.usedMemoryByte  = -1;
                        iterinfo.time1           = DateTime.UtcNow;
                        iterinfos.Add(iterinfo);

                        System.Console.WriteLine(" - {0:000} : makezero {1,5}, nonzero {2,5}, numIgnMul {3,7}, numRemvAtoms {4,3}, {5,5:0.00} sec, {6} mb, {7}x{7}"
                                                 , iter
                                                 , iterinfo.numSetZeroBlock
                                                 , iterinfo.numNonZeroBlock
                                                 , iterinfo.numAddIgnrBlock
                                                 , iterinfo.numAtomsRemoved
                                                 , iterinfo.compSec
                                                 , iterinfo.usedMemoryByte / (1024 * 1024)
                                                 , (idxkeep.Length * 3)
                                                 );
                    }
                    Matrix CG_H = Matlab.GetMatrix("CG.H");
                    CGH = new HessMatrixDense {
                        hess = CG_H
                    };
                }

                return(new CGetHessCoarseResiIterImpl
                {
                    iterinfos = iterinfos,
                    H = CGH,
                });
            }
Esempio n. 13
0
        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);
        }
Esempio n. 14
0
        public static HessMatrixSparse FromMatrix(Matrix mat, bool parallel = false)
        {
            if (mat is HessMatrixSparse)
            {
                return((mat as HessMatrixSparse).CloneT());
            }

            HDebug.Assert(mat.ColSize % 3 == 0);
            HDebug.Assert(mat.RowSize % 3 == 0);
            HessMatrixSparse hess = ZerosSparse(mat.ColSize, mat.RowSize);

            {
                HessMatrixDense dense = new HessMatrixDense {
                    hess = mat
                };

                Action <Tuple <int, int> > func = delegate(Tuple <int, int> bc_br)
                {
                    int bc = bc_br.Item1;
                    int br = bc_br.Item2;
                    if (dense.HasBlock(bc, br) == false)
                    {
                        return;
                    }

                    var bval = dense.GetBlock(bc, br);
                    lock (hess)
                        hess.SetBlock(bc, br, bval);
                };
                if (parallel)
                {
                    Parallel.ForEach(dense.EnumIndicesAll(), func);
                }
                else
                {
                    foreach (var bc_br_bval in dense.EnumIndicesAll())
                    {
                        func(bc_br_bval);
                    }
                }
            }

            if (HDebug.IsDebuggerAttached)
            {
                // old version
                HessMatrixSparse dhess = ZerosSparse(mat.ColSize, mat.RowSize);
                int colsize            = mat.ColSize;
                int rowsize            = mat.RowSize;
                for (int c = 0; c < colsize; c++)
                {
                    for (int r = 0; r < rowsize; r++)
                    {
                        if (mat[c, r] != 0)
                        {
                            dhess[c, r] = mat[c, r];
                        }
                    }
                }
                HDebug.Assert(HessMatrixSparseEqual(hess, dhess));

                if (HDebug.IsDebuggerAttached && ((mat.ColSize / 3) < 3000))
                {
                    double maxAbsDiff = mat.HAbsMaxDiffWith(hess);
                    HDebug.Assert(maxAbsDiff == 0);
                }
            }

            return(hess);
        }
            private static HessMatrix GetHessCoarseResiIterImpl_Matlab_IterLowerTri_Get_BInvDC
                (HessMatrix A
                , HessMatrix C
                , HessMatrix D
                , bool process_disp_console
                , string[] options
                , double?thld_BinvDC = null
                , bool parallel      = false
                )
            {
                if (options == null)
                {
                    options = new string[0];
                }

                HessMatrix            B_invD_C;
                Dictionary <int, int> Cbr_CCbr = new Dictionary <int, int>();
                List <int>            CCbr_Cbr = new List <int>();

                foreach (ValueTuple <int, int, MatrixByArr> bc_br_bval in C.EnumBlocks())
                {
                    int Cbr = bc_br_bval.Item2;
                    if (Cbr_CCbr.ContainsKey(Cbr) == false)
                    {
                        HDebug.Assert(Cbr_CCbr.Count == CCbr_Cbr.Count);
                        int CCbr = Cbr_CCbr.Count;
                        Cbr_CCbr.Add(Cbr, CCbr);
                        CCbr_Cbr.Add(Cbr);
                        HDebug.Assert(CCbr_Cbr[CCbr] == Cbr);
                    }
                }

                HessMatrix CC = C.Zeros(C.ColSize, Cbr_CCbr.Count * 3);

                {
                    Action <ValueTuple <int, int, MatrixByArr> > func = delegate(ValueTuple <int, int, MatrixByArr> bc_br_bval)
                    {
                        int Cbc = bc_br_bval.Item1; int CCbc = Cbc;
                        int Cbr = bc_br_bval.Item2; int CCbr = Cbr_CCbr[Cbr];
                        var bval = bc_br_bval.Item3;
                        lock (CC)
                            CC.SetBlock(CCbc, CCbr, bval);
                    };

                    if (parallel)
                    {
                        Parallel.ForEach(C.EnumBlocks(), func);
                    }
                    else
                    {
                        foreach (var bc_br_bval in C.EnumBlocks())
                        {
                            func(bc_br_bval);
                        }
                    }
                }
                if (process_disp_console)
                {
                    System.Console.Write("squeezeC({0,6}->{1,6} blk), ", C.RowBlockSize, CC.RowBlockSize);
                }
                {
                    /// If a diagonal element of D is null, that row and column should be empty.
                    /// This assume that the atom is removed. In this case, the removed diagonal block
                    /// is replace as the 3x3 identity matrix.
                    ///
                    ///  [B1  0] [ A 0 ]^-1 [C1 C2 C3] = [B1  0] [ A^-1  0    ] [C1 C2 C3]
                    ///  [B2  0] [ 0 I ]    [ 0  0  0]   [B2  0] [ 0     I^-1 ] [ 0  0  0]
                    ///  [B3  0]                         [B3  0]
                    ///                                = [B1.invA  0] [C1 C2 C3]
                    ///                                  [B2.invA  0] [ 0  0  0]
                    ///                                  [B3.invA  0]
                    ///                                = [B1.invA.C1  B1.invA.C2  B1.invA.C3]
                    ///                                  [B2.invA.C1  B2.invA.C2  B2.invA.C3]
                    ///                                  [B3.invA.C1  B3.invA.C2  B3.invA.C3]
                    ///
                    {
                        //HDebug.Exception(D.ColBlockSize == D.RowBlockSize);
                        for (int bi = 0; bi < D.ColBlockSize; bi++)
                        {
                            if (D.HasBlock(bi, bi) == true)
                            {
                                continue;
                            }
                            //for(int bc=0; bc< D.ColBlockSize; bc++) HDebug.Exception( D.HasBlock(bc, bi) == false);
                            //for(int br=0; br< D.RowBlockSize; br++) HDebug.Exception( D.HasBlock(bi, br) == false);
                            //for(int br=0; br<CC.RowBlockSize; br++) HDebug.Exception(CC.HasBlock(bi, br) == false);
                            D.SetBlock(bi, bi, new double[3, 3] {
                                { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }
                            });
                        }
                    }

                    HessMatrix BB_invDD_CC;
                    using (new Matlab.NamedLock(""))
                    {
                        Matlab.Execute("clear;");   if (process_disp_console)
                        {
                            System.Console.Write("matlab(");
                        }
                        Matlab.PutMatrix("C", CC);  if (process_disp_console)
                        {
                            System.Console.Write("C");                                                  //Matlab.PutSparseMatrix("C", CC.GetMatrixSparse(), 3, 3);
                        }
                        Matlab.PutMatrix("D", D);   if (process_disp_console)
                        {
                            System.Console.Write("D");
                        }

                        // Matlab.Execute("BinvDC = (C' / D) * C;");
                        {
                            if (options.Contains("pinv(D)"))
                            {
                                Matlab.Execute("BinvDC = C' * pinv(D) * C;");
                            }
                            if (options.Contains("/D -> pinv(D)"))
                            {
                                string msg = Matlab.Execute("BinvDC = (C' / D) * C;", true);
                                if (msg != "")
                                {
                                    Matlab.Execute("BinvDC = C' * pinv(D) * C;");
                                }
                            }
                            else if (options.Contains("/D"))
                            {
                                Matlab.Execute("BinvDC = (C' / D) * C;");
                            }
                            else
                            {
                                Matlab.Execute("BinvDC = (C' / D) * C;");
                            }
                        }
                        if (process_disp_console)
                        {
                            System.Console.Write("X");
                        }

                        //Matrix BBinvDDCC = Matlab.GetMatrix("BinvDC", true);
                        if (thld_BinvDC != null)
                        {
                            Matlab.Execute("BinvDC(find(BinvDC < " + thld_BinvDC.ToString() + ")) = 0;");
                        }
                        if (Matlab.GetValue("nnz(BinvDC)/numel(BinvDC)") > 0.5 || HDebug.True)
                        {
                            Func <int, int, HessMatrix> Zeros = delegate(int colsize, int rowsize)
                            {
                                return(HessMatrixDense.ZerosDense(colsize, rowsize));
                            };
                            BB_invDD_CC = Matlab.GetMatrix("BinvDC", Zeros, true);
                            if (process_disp_console)
                            {
                                System.Console.Write("Y), ");
                            }
                        }
                        else
                        {
                            Matlab.Execute("[i,j,s] = find(sparse(BinvDC));");
                            TVector <int>    listi = Matlab.GetVectorLargeInt("i", true);
                            TVector <int>    listj = Matlab.GetVectorLargeInt("j", true);
                            TVector <double> lists = Matlab.GetVectorLarge("s", true);
                            int colsize            = Matlab.GetValueInt("size(BinvDC,1)");
                            int rowsize            = Matlab.GetValueInt("size(BinvDC,2)");

                            Dictionary <ValueTuple <int, int>, MatrixByArr> lst_bc_br_bval = new Dictionary <ValueTuple <int, int>, MatrixByArr>();
                            for (long i = 0; i < listi.SizeLong; i++)
                            {
                                int    c = listi[i] - 1; int bc = c / 3; int ic = c % 3;
                                int    r = listj[i] - 1; int br = r / 3; int ir = r % 3;
                                double v = lists[i];
                                ValueTuple <int, int> bc_br = new ValueTuple <int, int>(bc, br);
                                if (lst_bc_br_bval.ContainsKey(bc_br) == false)
                                {
                                    lst_bc_br_bval.Add(bc_br, new double[3, 3]);
                                }
                                lst_bc_br_bval[bc_br][ic, ir] = v;
                            }

                            //  Matrix BBinvDDCC = Matrix.Zeros(colsize, rowsize);
                            //  for(int i=0; i<listi.Length; i++)
                            //      BBinvDDCC[listi[i]-1, listj[i]-1] = lists[i];
                            //  //GC.Collect(0);
                            BB_invDD_CC = D.Zeros(colsize, rowsize);
                            foreach (var bc_br_bval in lst_bc_br_bval)
                            {
                                int bc   = bc_br_bval.Key.Item1;
                                int br   = bc_br_bval.Key.Item2;
                                var bval = bc_br_bval.Value;
                                BB_invDD_CC.SetBlock(bc, br, bval);
                            }
                            if (process_disp_console)
                            {
                                System.Console.Write("Z), ");
                            }

                            if (HDebug.IsDebuggerAttached)
                            {
                                for (int i = 0; i < listi.Size; i++)
                                {
                                    int    c = listi[i] - 1;
                                    int    r = listj[i] - 1;
                                    double v = lists[i];
                                    HDebug.Assert(BB_invDD_CC[c, r] == v);
                                }
                            }
                        }
                        Matlab.Execute("clear;");
                    }
                    //GC.Collect(0);

                    B_invD_C = A.Zeros(C.RowSize, C.RowSize);
                    {
                        //  for(int bcc=0; bcc<CCbr_Cbr.Count; bcc++)
                        //  {
                        //      int bc = CCbr_Cbr[bcc];
                        //      for(int brr=0; brr<CCbr_Cbr.Count; brr++)
                        //      {
                        //          int br   = CCbr_Cbr[brr];
                        //          HDebug.Assert(B_invD_C.HasBlock(bc, br) == false);
                        //          if(BB_invDD_CC.HasBlock(bcc, brr) == false)
                        //              continue;
                        //          var bval = BB_invDD_CC.GetBlock(bcc, brr);
                        //          B_invD_C.SetBlock(bc, br, bval);
                        //          HDebug.Exception(A.HasBlock(bc, bc));
                        //          HDebug.Exception(A.HasBlock(br, br));
                        //      }
                        //  }
                        Action <ValueTuple <int, int, MatrixByArr> > func = delegate(ValueTuple <int, int, MatrixByArr> bcc_brr_bval)
                        {
                            int bcc  = bcc_brr_bval.Item1;
                            int brr  = bcc_brr_bval.Item2;
                            var bval = bcc_brr_bval.Item3;

                            int bc = CCbr_Cbr[bcc];
                            int br = CCbr_Cbr[brr];
                            //lock(B_invD_C)
                            B_invD_C.SetBlockLock(bc, br, bval);
                        };

                        if (parallel)
                        {
                            Parallel.ForEach(BB_invDD_CC.EnumBlocks(), func);
                        }
                        else
                        {
                            foreach (var bcc_brr_bval in BB_invDD_CC.EnumBlocks())
                            {
                                func(bcc_brr_bval);
                            }
                        }
                    }
                }
                GC.Collect(0);
                return(B_invD_C);
            }
Esempio n. 16
0
        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);
        }
Esempio n. 17
0
        public HessMatrix SubMatrixByAtomsImpl
            (bool ignNegIdx         // [false]
            , IList <int> idxColAtoms
            , IList <int> idxRowAtoms
            , bool bCloneBlock
            , bool parallel = false
            )
        {
            Dictionary <int, int[]> col_idx2nidx = new Dictionary <int, int[]>();
            HashSet <int>           col_idxs     = new HashSet <int>();

            for (int nidx = 0; nidx < idxColAtoms.Count; nidx++)
            {
                int idx = idxColAtoms[nidx];
                if (idx < 0)
                {
                    if (ignNegIdx)
                    {
                        continue;
                    }
                    throw new IndexOutOfRangeException();
                }
                if (col_idx2nidx.ContainsKey(idx) == false)
                {
                    col_idx2nidx.Add(idx, new int[0]);
                }
                col_idx2nidx[idx] = col_idx2nidx[idx].HAdd(nidx);
                col_idxs.Add(idx);
            }
            Dictionary <int, int[]> row_idx2nidx = new Dictionary <int, int[]>();

            for (int nidx = 0; nidx < idxRowAtoms.Count; nidx++)
            {
                int idx = idxRowAtoms[nidx];
                if (idx < 0)
                {
                    if (ignNegIdx)
                    {
                        continue;
                    }
                    throw new IndexOutOfRangeException();
                }
                if (row_idx2nidx.ContainsKey(idx) == false)
                {
                    row_idx2nidx.Add(idx, new int[0]);
                }
                row_idx2nidx[idx] = row_idx2nidx[idx].HAdd(nidx);
            }

            HessMatrix nhess = Zeros(idxColAtoms.Count * 3, idxRowAtoms.Count * 3);

            {
                Action <ValueTuple <int, int, MatrixByArr> > func = delegate(ValueTuple <int, int, MatrixByArr> bc_br_bval)
                {
                    int bc = bc_br_bval.Item1; if (col_idx2nidx.ContainsKey(bc) == false)
                    {
                        return;
                    }
                    int br = bc_br_bval.Item2; if (row_idx2nidx.ContainsKey(br) == false)
                    {
                        return;
                    }
                    var bval = bc_br_bval.Item3;
                    if (bCloneBlock)
                    {
                        foreach (int nbc in col_idx2nidx[bc])
                        {
                            foreach (int nbr in row_idx2nidx[br])
                            {
                                lock (nhess)
                                    nhess.SetBlock(nbc, nbr, bval.CloneT());
                            }
                        }
                    }
                    else
                    {
                        foreach (int nbc in col_idx2nidx[bc])
                        {
                            foreach (int nbr in row_idx2nidx[br])
                            {
                                lock (nhess)
                                    nhess.SetBlock(nbc, nbr, bval);
                            }
                        }
                    }
                };

                if (parallel)
                {
                    Parallel.ForEach(EnumBlocksInCols(col_idxs.ToArray()), func);
                }
                else
                {
                    foreach (var bc_br_bval in EnumBlocksInCols(col_idxs.ToArray()))
                    {
                        func(bc_br_bval);
                    }
                }
            }
            if (SubMatrixByAtomsImpl_selftest2)
            {
                SubMatrixByAtomsImpl_selftest2 = false;
                HessMatrix tnhess = SubMatrixByAtomsImpl0(idxColAtoms, idxRowAtoms);
                HDebug.Assert(HessMatrix.HessMatrixSparseEqual(nhess, tnhess));
                //////////////////////////////////////////
                Matrix thess1 = 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 thess2 = HessMatrixDense.FromMatrix(thess1);
                HessMatrix thess3 = thess2.SubMatrixByAtomsImpl(false, new int[] { 0 }, new int[] { 1 }, true);
                Matrix     thess4 = new double[, ] {
                    { 3, 4, 5 }
                    , { 4, 5, 6 }
                    , { 5, 6, 7 }
                };
                HDebug.AssertToleranceMatrix(0, thess3 - thess4);
            }

            return(nhess);
        }
Esempio n. 18
0
            public static ValueTuple <HessMatrix, Vector> Get_BInvDC_BInvDG_Simple
                (HessMatrix CC
                , HessMatrix DD
                , Vector GG
                , bool process_disp_console
                , double?thld_BinvDC = null
                , bool parallel      = false
                )
            {
                HessMatrix BB_invDD_CC;
                Vector     BB_invDD_GG;

                using (new Matlab.NamedLock(""))
                {
                    Matlab.Execute("clear;"); if (process_disp_console)
                    {
                        System.Console.Write("matlab(");
                    }
                    Matlab.PutMatrix("C", CC); if (process_disp_console)
                    {
                        System.Console.Write("C");                                                 //Matlab.PutSparseMatrix("C", C.GetMatrixSparse(), 3, 3);
                    }
                    Matlab.PutMatrix("D", DD); if (process_disp_console)
                    {
                        System.Console.Write("D");
                    }
                    Matlab.PutVector("G", GG); if (process_disp_console)
                    {
                        System.Console.Write("G");
                    }
                    // Matlab.Execute("BinvDC = C' * inv(D) * C;");
                    {
                        Matlab.Execute("BinvDC = C' * inv(D);");
                        Matlab.Execute("BinvD_G = BinvDC * G;");
                        Matlab.Execute("BinvDC  = BinvDC * C;");
                    }

                    BB_invDD_GG = Matlab.GetVector("BinvD_G");

                    //Matrix BBinvDDCC = Matlab.GetMatrix("BinvDC", true);
                    if (thld_BinvDC != null)
                    {
                        Matlab.Execute("BinvDC(find(abs(BinvDC) < " + thld_BinvDC.ToString() + ")) = 0;");
                    }

                    Func <int, int, HessMatrix> Zeros = delegate(int colsize, int rowsize)
                    {
                        return(HessMatrixDense.ZerosDense(colsize, rowsize));
                    };
                    BB_invDD_CC = Matlab.GetMatrix("BinvDC", Zeros, true);
                    if (process_disp_console)
                    {
                        System.Console.Write("Y), ");
                    }

                    Matlab.Execute("clear;");
                }
                //GC.Collect(0);

                return(new ValueTuple <HessMatrix, Vector>
                           (BB_invDD_CC
                           , BB_invDD_GG
                           ));
            }
Esempio n. 19
0
        public static HessMatrix GetHessAnm(IList <Vector> coords, IList <double> cutoffs, string options = "")
        {
            if (coords.Count > 10000)
            {
                HDebug.ToDo("System size is too big. Use EnumHessAnmSpr() and other GetHessAnm()");
            }

            //Debug.Assert(AnmHessianSelfTest());

            HDebug.Assert(coords.Count == cutoffs.Count);

            double[] cutoffs2 = new double[cutoffs.Count];
            for (int i = 0; i < cutoffs.Count; i++)
            {
                cutoffs2[i] = cutoffs[i] * cutoffs[i];
            }

            double Epsilon = 0;// double.Epsilon;

            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);
            }
            Action <int> comp_hess_i = delegate(int i)
            {
                if (coords[i] == null)
                {
                    return;
                }
                //continue;
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    if (coords[j] == null)
                    {
                        continue;
                    }
                    Vector vec_ij  = coords[j] - coords[i];
                    double dist2   = vec_ij.Dist2;
                    double cutoff2 = Math.Max(cutoffs2[i], cutoffs2[j]);
                    if (dist2 > cutoff2)
                    {
                        if (Epsilon == 0)
                        {
                            continue;
                        }
                        HDebug.Assert(hess[i * 3 + 0, j * 3 + 0] == 0); hess[i * 3 + 0, j *3 + 0] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 0, j * 3 + 1] == 0); hess[i * 3 + 0, j *3 + 1] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 0, j * 3 + 2] == 0); hess[i * 3 + 0, j *3 + 2] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 1, j * 3 + 0] == 0); hess[i * 3 + 1, j *3 + 0] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 1, j * 3 + 1] == 0); hess[i * 3 + 1, j *3 + 1] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 1, j * 3 + 2] == 0); hess[i * 3 + 1, j *3 + 2] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 2, j * 3 + 0] == 0); hess[i * 3 + 2, j *3 + 0] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 2, j * 3 + 1] == 0); hess[i * 3 + 2, j *3 + 1] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 2, j * 3 + 2] == 0); hess[i * 3 + 2, j *3 + 2] = Epsilon;
                        continue;
                    }
                    Vector unit_ij = vec_ij.UnitVector();
                    double k_ij = 1; double val;
                    val = k_ij * unit_ij[0] * unit_ij[0]; hess[i * 3 + 0, j *3 + 0] = Epsilon - val; hess[i * 3 + 0, i *3 + 0] += val;  // hess[i*3+0, j*3+0] = Epsilon - k_ij * unit_ij[0]*unit_ij[0]; hess[i*3+0, i*3+0] += k_ij * unit_ij[0]*unit_ij[0];
                    val = k_ij * unit_ij[0] * unit_ij[1]; hess[i * 3 + 0, j *3 + 1] = Epsilon - val; hess[i * 3 + 0, i *3 + 1] += val;  // hess[i*3+0, j*3+1] = Epsilon - k_ij * unit_ij[0]*unit_ij[1]; hess[i*3+0, i*3+1] += k_ij * unit_ij[0]*unit_ij[1];
                    val = k_ij * unit_ij[0] * unit_ij[2]; hess[i * 3 + 0, j *3 + 2] = Epsilon - val; hess[i * 3 + 0, i *3 + 2] += val;  // hess[i*3+0, j*3+2] = Epsilon - k_ij * unit_ij[0]*unit_ij[2]; hess[i*3+0, i*3+2] += k_ij * unit_ij[0]*unit_ij[2];
                    val = k_ij * unit_ij[1] * unit_ij[0]; hess[i * 3 + 1, j *3 + 0] = Epsilon - val; hess[i * 3 + 1, i *3 + 0] += val;  // hess[i*3+1, j*3+0] = Epsilon - k_ij * unit_ij[1]*unit_ij[0]; hess[i*3+1, i*3+0] += k_ij * unit_ij[1]*unit_ij[0];
                    val = k_ij * unit_ij[1] * unit_ij[1]; hess[i * 3 + 1, j *3 + 1] = Epsilon - val; hess[i * 3 + 1, i *3 + 1] += val;  // hess[i*3+1, j*3+1] = Epsilon - k_ij * unit_ij[1]*unit_ij[1]; hess[i*3+1, i*3+1] += k_ij * unit_ij[1]*unit_ij[1];
                    val = k_ij * unit_ij[1] * unit_ij[2]; hess[i * 3 + 1, j *3 + 2] = Epsilon - val; hess[i * 3 + 1, i *3 + 2] += val;  // hess[i*3+1, j*3+2] = Epsilon - k_ij * unit_ij[1]*unit_ij[2]; hess[i*3+1, i*3+2] += k_ij * unit_ij[1]*unit_ij[2];
                    val = k_ij * unit_ij[2] * unit_ij[0]; hess[i * 3 + 2, j *3 + 0] = Epsilon - val; hess[i * 3 + 2, i *3 + 0] += val;  // hess[i*3+2, j*3+0] = Epsilon - k_ij * unit_ij[2]*unit_ij[0]; hess[i*3+2, i*3+0] += k_ij * unit_ij[2]*unit_ij[0];
                    val = k_ij * unit_ij[2] * unit_ij[1]; hess[i * 3 + 2, j *3 + 1] = Epsilon - val; hess[i * 3 + 2, i *3 + 1] += val;  // hess[i*3+2, j*3+1] = Epsilon - k_ij * unit_ij[2]*unit_ij[1]; hess[i*3+2, i*3+1] += k_ij * unit_ij[2]*unit_ij[1];
                    val = k_ij * unit_ij[2] * unit_ij[2]; hess[i * 3 + 2, j *3 + 2] = Epsilon - val; hess[i * 3 + 2, i *3 + 2] += val;  // hess[i*3+2, j*3+2] = Epsilon - k_ij * unit_ij[2]*unit_ij[2]; hess[i*3+2, i*3+2] += k_ij * unit_ij[2]*unit_ij[2];
                }
            };

            if (options.Split(';').Contains("parallel"))
            {
                System.Threading.Tasks.Parallel.For(0, n, comp_hess_i);
            }
            else
            {
                for (int i = 0; i < n; i++)
                {
                    comp_hess_i(i);
                }
            }

            return(hess);
        }