/// 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);
        }
Exemple #2
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,
                });
            }
            public static HessMatrix GetHess(Universe univ)
            {
                Vector[] coords = univ.GetCoords();

                HessMatrix hessian_spr = HessMatrixSparse.ZerosSparse(univ.size * 3, univ.size * 3);

                Universe.Nonbondeds_v1 nonbondeds = new Universe.Nonbondeds_v1(univ.atoms, univ.size, 12);
                nonbondeds.UpdateNonbondeds(coords, 0);
                hessian_spr = STeM.GetHessBond(coords, univ.bonds, null, hessian: hessian_spr); HDebug.Verify(Hess.CheckHessDiag(hessian_spr, 0.000001, "non-computable exception while generating STeM hess_spr with bond"));
                hessian_spr = STeM.GetHessAngle(coords, univ.angles, true, null, null, hessian: hessian_spr); HDebug.Verify(Hess.CheckHessDiag(hessian_spr, 0.000001, "non-computable exception while generating STeM hess_spr with angle"));
                hessian_spr = STeM.GetHessImproper(coords, univ.impropers, null, hessian: hessian_spr); HDebug.Verify(Hess.CheckHessDiag(hessian_spr, 0.000001, "non-computable exception while generating STeM hess_spr with improper"));
                hessian_spr = STeM.GetHessDihedral(coords, univ.dihedrals, null, null, hessian: hessian_spr); HDebug.Verify(Hess.CheckHessDiag(hessian_spr, 0.000001, "non-computable exception while generating STeM hess_spr with dihedral"));
                hessian_spr = STeM.GetHessVdw(coords, univ.nonbonded14s.GetEnumerable(), null, null, hessian: hessian_spr); HDebug.Verify(Hess.CheckHessDiag(hessian_spr, 0.000001, "non-computable exception while generating STeM hess_spr with vdw14"));
                hessian_spr = STeM.GetHessElec(coords, univ.nonbonded14s.GetEnumerable(), hessian: hessian_spr); HDebug.Verify(Hess.CheckHessDiag(hessian_spr, 0.000001, "non-computable exception while generating STeM hess_spr with elec14"));
                hessian_spr = STeM.GetHessVdw(coords, nonbondeds.GetEnumerable(), null, null, hessian: hessian_spr); HDebug.Verify(Hess.CheckHessDiag(hessian_spr, 0.000001, "non-computable exception while generating STeM hess_spr with vdw"));
                hessian_spr = STeM.GetHessElec(coords, nonbondeds.GetEnumerable(), hessian: hessian_spr); HDebug.Verify(Hess.CheckHessDiag(hessian_spr, 0.000001, "non-computable exception while generating STeM hess_spr with elec"));
                //hessian_spr = GetHessSprElec(coords, nonbondeds                                   , hessian: hessian_spr); Debug.Verify(Hess.CheckHessDiag(hessian_spr));
                //hessian_spr = GetHessSprVdw(coords, nonbondeds                                    , hessian: hessian_spr); Debug.Verify(Hess.CheckHessDiag(hessian_spr));
                hessian_spr = Hess.CorrectHessDiag(hessian_spr);                                                           HDebug.Verify(Hess.CheckHessDiag(hessian_spr, 0.000001, "non-computable exception while generating STeM hess_spr"));

                return(hessian_spr);
            }
Exemple #4
0
        public static bool CheckHessDiag(Matrix hessian, double tolerSumDiag, string exceptmsg = null)
        {
            if (CheckHessDiag_selftest)
            {
                CheckHessDiag_selftest = false;

                Matrix thess = new double[, ] {
                    { -1, -2, 0, 1, 2, 3 }
                    , { -2, -4, -5, 2, 4, 5 }
                    , { -3, -5, -6, 3, 5, 6 }
                    , { 1, -2, 3, -1, 2, -3 }
                    , { 2, -4, -5, -2, 4, 5 }
                    , { -3, 5, 6, 3, -5, -6 }
                };
                HDebug.Assert(Hess.CheckHessDiag(thess, 0.0000001) == false);
                thess[0, 2] = -3;
                HDebug.Assert(Hess.CheckHessDiag(thess, 0.0000001) == true);
                for (int i = 0; i < thess.ColSize; i++)
                {
                    thess[i, i] = 0;
                }
                HDebug.Assert(Hess.CheckHessDiag(thess, 0.0000001) == false);
            }

            // tolerSumDiag = 0.000001
            HDebug.Assert(hessian.ColSize == hessian.RowSize);
            HDebug.Assert(hessian.ColSize % 3 == 0);
            int           size = hessian.ColSize / 3;
            List <double> sums = new List <double>();

            for (int i = 0; i < size; i++)
            {
                for (int di = 0; di < 3; di++)
                {
                    for (int dj = 0; dj < 3; dj++)
                    {
                        double diagval = 0;
                        for (int j = 0; j < size; j++)
                        {
                            if (i == j)
                            {
                                continue;
                            }
                            double hessian_idi_jdj = hessian[3 * i + di, 3 * j + dj];
                            if (double.IsNaN(hessian_idi_jdj))
                            {
                                HDebug.Assert(false); return(false);
                            }                                                                              // throw new Exception("NotComputable :"+exceptmsg);
                            if (double.IsInfinity(hessian_idi_jdj))
                            {
                                HDebug.Assert(false); return(false);
                            }                                                                              //throw new Exception("NotComputable :"+exceptmsg);
                            diagval += hessian_idi_jdj;
                        }
                        double sum = diagval + hessian[3 * i + di, 3 * i + dj];
                        sums.Add(sum);
                    }
                }
            }
            double maxsum = Math.Max(Math.Abs(sums.Min()), Math.Abs(sums.Max()));

            if (HDebug.CheckTolerance(tolerSumDiag, maxsum) == false)
            {
                return(false);
            }
            return(true);
        }