Example #1
0
            public Tuple <Pdb.Atom, int>[] SelectPdbAtoms()
            {
                HDebug.Assert(false); // use ListPdbAtoms()
                List <Tuple <Pdb.Atom, int> > lstPdbatomIdx = new List <Tuple <Pdb.Atom, int> >();

                Pdb.Atom[] pdbatoms = ListPdbAtoms();
                for (int idx = 0; idx < atoms.Count; idx++)
                {
                    if (pdbatoms[idx] == null)
                    {
                        continue;
                    }
                    lstPdbatomIdx.Add(new Tuple <Pdb.Atom, int>(pdbatoms[idx], idx));
                }
                return(lstPdbatomIdx.ToArray());
            }
 ///////////////////////////////////////////////////////////////////////
 // UpdateSubMatrixByAtom
 public void UpdateSubMatrixByAtom(HessMatrix submat, IList <int> idxcolatoms, IList <int> idxrowatoms)
 {
     HDebug.Assert(idxcolatoms.Max() < ColBlockSize);
     HDebug.Assert(idxrowatoms.Max() < RowBlockSize);
     HDebug.Assert(submat.ColBlockSize == idxcolatoms.Count);
     HDebug.Assert(submat.RowBlockSize == idxrowatoms.Count);
     for (int ic = 0; ic < idxcolatoms.Count; ic++)
     {
         for (int ir = 0; ir < idxrowatoms.Count; ir++)
         {
             int hess_ic = idxcolatoms[ic];
             int hess_ir = idxrowatoms[ir];
             SetBlock(hess_ic, hess_ir, submat.GetBlock(ic, ir).CloneT());
         }
     }
 }
Example #3
0
        public static List <List <string> > FromLines_CollectGroup(IList <string> lines)
        {
            List <List <string> > groups = new List <List <string> >();

            for (int i = 0; i < lines.Count; i++)
            {
                string line = lines[i];
                HDebug.Assert(line[0] == ' ');
                if (line[1] != ' ' && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".IndexOf(line[2]) != -1)
                {
                    groups.Add(new List <string>());
                }
                groups.Last().Add(line);
            }
            return(groups);
        }
Example #4
0
            public void BuildHess4PwIntrAct(Universe.AtomPack info, Vector[] coords, out Pair <int, int>[] pwidxs, out PwIntrActInfo[] pwhessinfos)
            {
                Universe.Nonbonded nonbonded = (Universe.Nonbonded)info;

                HDebug.Depreciated("check idx1 and idx2");
                int idx1 = 0; // nonbonded.atoms[0].ID;
                int idx2 = 1; // nonbonded.atoms[1].ID;

                Universe.Atom atom1 = nonbonded.atoms[0];
                Universe.Atom atom2 = nonbonded.atoms[1];
                double        pchij = atom1.Charge * atom2.Charge;

                if (double.IsNaN(pchij))
                {
                    HDebug.Assert(false);
                    pwidxs      = null;
                    pwhessinfos = null;
                    return;
                }

                Vector diff = (coords[idx2] - coords[idx1]);
                double dx   = diff[0];
                double dy   = diff[1];
                double dz   = diff[2];
                double pchi = atom1.Charge;
                double pchj = atom2.Charge;
                double ee   = 80;

                // !V(Lennard-Jones) = Eps,i,j[(Rmin,i,j/ri,j)**12 - 2(Rmin,i,j/ri,j)**6]
                // !epsilon: kcal/mole, Eps,i,j = sqrt(eps,i * eps,j)
                // !Rmin/2: A, Rmin,i,j = Rmin/2,i + Rmin/2,j
                //
                // V(rij) =           (332 * pchij / ee) * rij^-1
                // F(rij) = (   -1) * (332 * pchij / ee) * rij^-2
                // K(rij) = (-2*-1) * (332 * pchij / ee) * rij^-3
//                double pchij = pchi * pchj;
                double rij2 = dx * dx + dy * dy + dz * dz;
                double rij  = Math.Sqrt(rij2);
                double rij3 = rij2 * rij;
                double fij  = (-1) * (332 * pchij / ee) / rij2;
                double kij  = (-2 * -1) * (332 * pchij / ee) / rij3;

                pwidxs         = new Pair <int, int> [1];
                pwidxs[0]      = new Pair <int, int>(0, 1);
                pwhessinfos    = new PwIntrActInfo[1];
                pwhessinfos[0] = new PwIntrActInfo(kij, fij);
            }
Example #5
0
        public static Tuple <Sheet[], Atom[]>[] HSelectAtoms <Atom>(this IList <Sheet> sheets, IList <Atom> atoms)
            where Atom : IAtom
        {
            var chain_resi_atoms = atoms.GroupChainIDResSeq();
            var id_sheets        = sheets.HGroupBySheetID();

            List <Tuple <Sheet[], Atom[]> > list = new List <Tuple <Sheet[], Atom[]> >();

            foreach (string id in id_sheets.Keys)
            {
                Sheet[] idsheets = id_sheets[id];

                List <Atom> idsheets_atoms = new List <Atom>();
                foreach (var sheet in idsheets)
                {
                    HDebug.Exception(sheet.initChainID == sheet.endChainID);
                    char chain = sheet.initChainID;
                    if (chain_resi_atoms.ContainsKey(chain) == false)
                    {
                        continue;
                    }
                    var chainresi_atoms = chain_resi_atoms[chain];

                    int[] resis;
                    resis = new int[] { sheet.initSeqNum, sheet.endSeqNum };
                    resis = resis.HSort();
                    resis = HEnum.HEnumFromTo(resis[0], resis[1]).ToArray();

                    foreach (var resi in resis)
                    {
                        if (chainresi_atoms.ContainsKey(resi) == false)
                        {
                            continue;
                        }
                        idsheets_atoms.AddRange(chainresi_atoms[resi]);
                    }
                }

                list.Add(new Tuple <Sheet[], Atom[]>
                         (
                             idsheets,
                             idsheets_atoms.ToArray()
                         ));
            }

            return(list.ToArray());
        }
Example #6
0
        public static IList <Atom> CloneByReindexByCoords(this IList <Atom> atoms, IList <Vector> coords, int serialStart = 1)
        {
            KDTree.KDTree <object> kdtree = new KDTree.KDTree <object>(3);
            for (int i = 0; i < coords.Count; i++)
            {
                kdtree.insert(coords[i], i);
            }

            Atom[] natoms = new Atom[atoms.Count];
            for (int ai = 0; ai < natoms.Length; ai++)
            {
                Atom   atom   = atoms[ai];
                Vector coord  = atom.coord;
                int    ni     = (int)(kdtree.nearest(coord));
                Vector ncoord = coords[ni];

                double dist = (coord - ncoord).Dist;
                HDebug.AssertTolerance(0.001, dist);
                HDebug.Assert(natoms[ni] == null);

                natoms[ni] = Atom.FromData(serial: serialStart + ni
                                           , name: atom.name
                                           , resName: atom.resName
                                           , chainID: atom.chainID
                                           , resSeq: atom.resSeq
                                           , x: atom.x
                                           , y: atom.y
                                           , z: atom.z
                                           , altLoc: atom.altLoc
                                           , iCode: atom.iCode
                                           , occupancy: atom.occupancy
                                           , tempFactor: atom.tempFactor
                                           , element: atom.element
                                           , charge: atom.charge
                                           );
            }

            if (HDebug.IsDebuggerAttached)
            {
                for (int i = 0; i < natoms.Length; i++)
                {
                    HDebug.Assert(natoms[i] != null);
                }
            }

            return(natoms);
        }
Example #7
0
        public static double GetHessTraceSpringBwAtoms(this HessMatrix hess, int atomi, int atomj)
        {
            HDebug.Assert(hess.ColSize == hess.RowSize);
            HDebug.Assert(hess.ColSize % 3 == 0);
            HDebug.Assert(hess.RowSize % 3 == 0);
            HDebug.Assert(atomi < hess.ColSize / 3);
            HDebug.Assert(atomj < hess.ColSize / 3);
            HDebug.Assert(hess.ColBlockSize == hess.RowBlockSize);
            HDebug.Assert(atomi < hess.ColBlockSize);
            HDebug.Assert(atomj < hess.ColBlockSize);

            double bibj_trace = 0;
            {
                MatrixByArr hess_ij = hess.GetBlock(atomi, atomj);
                if (hess_ij != null)
                {
                    bibj_trace += hess_ij[0, 0];
                    bibj_trace += hess_ij[1, 1];
                    bibj_trace += hess_ij[2, 2];
                }
            }
            double bjbi_trace = 0;
            {
                MatrixByArr hess_ji = hess.GetBlock(atomj, atomi);
                if (hess_ji != null)
                {
                    bjbi_trace += hess_ji[0, 0];
                    bjbi_trace += hess_ji[1, 1];
                    bjbi_trace += hess_ji[2, 2];
                }
            }

            double threshold = 0.00001;

            HDebug.Assert(Math.Abs(bibj_trace - bjbi_trace) < threshold);

            double spr_ij = -1 * (bibj_trace + bjbi_trace) / 2;

            if (selftest_GetHessTraceSpringBwAtoms_hess_atomi_atomj)
            {
                selftest_GetHessTraceSpringBwAtoms_hess_atomi_atomj = false;
                double _spr_ij = GetHessTraceSpringBwAtoms(hess as Matrix, atomi, atomj);
                HDebug.Assert(Math.Abs(spr_ij - _spr_ij) < threshold);
            }

            return(spr_ij);
        }
Example #8
0
        public static IEnumerable <Universe.Nonbonded> EnumNonbondeds
            (Universe.Atoms atoms
            , IList <Vector> coords
            , int size
            , double maxdist             // = 12
            , FuncListTip3pHBond func
            , string[] options
            )
        {
            bool[] waters = new bool[coords.Count];
            KDTree.KDTree <Universe.Atom> kdtree_water = new KDTree.KDTree <Universe.Atom>(3);
            foreach (var atom in atoms)
            {
                if (coords[atom.ID] == null)
                {
                    continue;
                }

                bool water = atom.IsWater();
                waters[atom.ID] = water;
                if (water)
                {
                    kdtree_water.insert(coords[atom.ID], atom);
                }
            }

            Universe.Nonbondeds _nonbondeds;
            _nonbondeds = new Universe.Nonbondeds(atoms, size, maxdist);
            _nonbondeds.UpdateCoords(coords, true);
            foreach (var nonbonded in _nonbondeds.EnumNonbondeds(true))
            {
                HDebug.Assert(nonbonded.atoms.Length == 2);
                var atom0 = nonbonded.atoms[0];
                var atom1 = nonbonded.atoms[1];
                if (waters[atom0.ID] && waters[atom1.ID])
                {
                    continue;
                }

                yield return(nonbonded);
            }

            foreach (Universe.Nonbonded nonbonded in func(atoms, coords, waters, kdtree_water, options))
            {
                yield return(nonbonded);
            }
        }
Example #9
0
File: TNM.cs Project: htna/explsolv
            public static MatrixByArr GetJ(Universe univ, Vector[] coords, List <RotableInfo> rotInfos, string option = null)
            {
                switch (option)
                {
                case "mine":
                {
                    MatrixByArr[,] J = TNM_mine.GetJ(univ, coords, rotInfos, fnInv3x3: null);
                    double tolerance = 0.00001;
                    HDebug.Assert(CheckEckartConditions(univ, coords, rotInfos, J, tolerance: tolerance));
                    //if(Debug.IsDebuggerAttachedWithProb(0.1))
                    //{
                    //    Matrix J0 = Matrix.FromBlockmatrix(J);
                    //    Matrix J1 = Matrix.FromBlockmatrix(GetJ(univ, coords, rotInfos, option:"paper", useNamedLock:false));
                    //    Debug.AssertTolerance(0.00000001, J0 - J1);
                    //}
                    return(MatrixByArr.FromMatrixArray(J));
                }

                case "paper":
                {
                    MatrixByArr[,] J = TNM_paper.GetJ(univ, coords, rotInfos);
                    HDebug.Assert(CheckEckartConditions(univ, coords, rotInfos, J, 0.0000001));
                    return(MatrixByArr.FromMatrixArray(J));
                }

                case "mineopt":
                {
                    MatrixByArr J = TNM_mineopt.GetJ(univ, coords, rotInfos);
                    if (HDebug.IsDebuggerAttached && univ.GetMolecules().Length == 1)
                    {
                        MatrixByArr J0 = TNM.GetJ(univ, coords, rotInfos, option: "paper");
                        MatrixByArr dJ = J - J0;
                        //double tolerance = dJ.ToArray().HAbs().HToArray1D().Mean();
                        //double maxAbsDH  = dJ.ToArray().HAbs().HMax();
                        //if(tolerance == 0)
                        //    tolerance = 0.000001;
                        HDebug.AssertTolerance(0.00000001, dJ);
                    }
                    return(J);
                }

                case null:
                    // my implementation has smaller error while checking Eckart conditions
                    goto case "mineopt";
                }
                return(null);
            }
Example #10
0
            /// <summary>
            /// Nonbonded Term (Van der Waals)
            /// V4 = Epsilon ( 5 (r0_ij / r_ij)^12 - 6 (r0_ij / r_ij)^10 )
            /// </summary>
            /// <param name="caArray_"></param>
            /// <param name="Epsilon"></param>
            /// <returns></returns>
            public static Matrix FourthTerm(IList <Vector> caArray_, double Epsilon)
            {
                VECTORS caArray = new VECTORS(caArray_);
                MATRIX  hessian = new MATRIX(new double[6, 6]);

                // derive the hessian of the first term (off diagonal)
                {
                    int i = 1;
                    int j = 2;

                    double bx        = caArray[i, 1] - caArray[j, 1];
                    double by        = caArray[i, 2] - caArray[j, 2];
                    double bz        = caArray[i, 3] - caArray[j, 3];
                    double distijsqr = pow(sqrt(bx * bx + by * by + bz * bz), 4);

                    // diagonals of off-diagonal super elements (1st term)
                    hessian[3 * i - 2, 3 * j - 2] += -120 * Epsilon * bx * bx / distijsqr;
                    hessian[3 * i - 1, 3 * j - 1] += -120 * Epsilon * by * by / distijsqr;
                    hessian[3 * i, 3 * j]         += -120 * Epsilon * bz * bz / distijsqr;

                    // off-diagonals of off-diagonal super elements (1st term)

                    hessian[3 * i - 2, 3 * j - 1] += -120 * Epsilon * bx * by / distijsqr;
                    hessian[3 * i - 2, 3 * j]     += -120 * Epsilon * bx * bz / distijsqr;
                    hessian[3 * i - 1, 3 * j - 2] += -120 * Epsilon * by * bx / distijsqr;
                    hessian[3 * i - 1, 3 * j]     += -120 * Epsilon * by * bz / distijsqr;
                    hessian[3 * i, 3 * j - 2]     += -120 * Epsilon * bx * bz / distijsqr;
                    hessian[3 * i, 3 * j - 1]     += -120 * Epsilon * by * bz / distijsqr;

                    //Hii
                    //update the diagonals of diagonal super elements

                    hessian[3 * i - 2, 3 * i - 2] += +120 * Epsilon * bx * bx / distijsqr;
                    hessian[3 * i - 1, 3 * i - 1] += +120 * Epsilon * by * by / distijsqr;
                    hessian[3 * i, 3 * i]         += +120 * Epsilon * bz * bz / distijsqr;

                    // update the off-diagonals of diagonal super elements
                    hessian[3 * i - 2, 3 * i - 1] += +120 * Epsilon * bx * by / distijsqr;
                    hessian[3 * i - 2, 3 * i]     += +120 * Epsilon * bx * bz / distijsqr;
                    hessian[3 * i - 1, 3 * i - 2] += +120 * Epsilon * by * bx / distijsqr;
                    hessian[3 * i - 1, 3 * i]     += +120 * Epsilon * by * bz / distijsqr;
                    hessian[3 * i, 3 * i - 2]     += +120 * Epsilon * bz * bx / distijsqr;
                    hessian[3 * i, 3 * i - 1]     += +120 * Epsilon * bz * by / distijsqr;
                }
                HDebug.Assert(hessian.matrix.IsComputable());
                return(hessian.matrix);
            }
Example #11
0
            public static void Compute(Vector[] coords, out double energy, out double force01, out double spring01, double Kb, double b0)
            {
                /// BONDS
                /// !V(bond) = Kb(b - b0)**2
                ///
                ///      p0 ---------- p1
                ///     (+) --→   ←-- (+)
                /// ←-- (-)           (-) --→
                ///
                ///     V        = Kb ( b - b0 ) ^ 2
                ///   d_V / d_b  = 2 Kb ( b - b0 )
                ///  d2_V / d_b2 = 2 Kb
                ///
                ///     b        = b1 + t
                ///   d_b / d_t  = 1
                ///  d2_b / d_t2 = 0
                ///
                /// energy = V
                ///        = Kb ( b - b0 ) ^ 2
                ///
                /// force  = d_V / d_t
                ///        = {d_V / d_b      } * {d_b / d_t}
                ///        = {2 Kb ( b - b0 )} * { 1       }
                ///        =  2 Kb ( b - b0 )
                ///
                /// spring = d_V2 / d2_t
                ///        = d_(d_V / d_t) / d_t
                ///        = d_({d_V / d_b } * {d_b / d_t}) / d_t
                ///        =   {d_{d_V / d_b } / d_t}        *      {d_b / d_t}
                ///          +    {d_V / d_b }               *   {d_{d_b / d_t} / d_t}
                ///        =     {d2_V / d_b2}*{d_b / d_t}   *      {d_b / d_t}
                ///          +    {d_V / d_b }               *     {d2_b / d_t2}
                ///        = {d2_V / d_b2}*{d_b / d_t}*{d_b / d_t}      +      {d_V / d_b    }*{d2_b / d_t2}
                ///        = {2 Kb       }*{ 1       }*{ 1       }      +      {2 Kb (b - b0)}*{ 0         }
                ///        =  2 Kb
                ///////////////////////////////////////////////////////////////////////////////
                Vector pos1 = coords[0];
                Vector pos2 = coords[1];
                double b    = (pos1 - pos2).Dist;

                HDebug.Assert(b != 0, double.IsNaN(b) == false, double.IsInfinity(b) == false);
                energy   = Kb * (b - b0) * (b - b0);
                force01  = 2 * Kb * (b - b0);
                spring01 = 2 * Kb;
                HDebug.AssertIf(force01 > 0, b0 < b); // positive force => attractive
                HDebug.AssertIf(force01 < 0, b < b0); // negative force => repulsive
            }
 public Vector this[int id0, int id1]
 {
     get
     {
         HDebug.Assert(forceij != null);
         if (forceij[id0, id1] == null)
         {
             forceij[id0, id1] = new double[3];
         }
         return(forceij[id0, id1]);
     }
     set
     {
         HDebug.Assert(forceij != null);
         forceij[id0, id1] = value;
     }
 }
        public static Pdb CopyPdbOccupancyForMergedPsf(Pdb copyto, Pdb copyfrom1, Pdb copyfrom2)
        {
            KDTreeDLL.KDTree <Pdb.Atom> kdtree = new KDTreeDLL.KDTree <Pdb.Atom>(3);
            foreach (var atom in copyfrom1.atoms)
            {
                kdtree.insert(atom.coord, atom);
            }
            foreach (var atom in copyfrom2.atoms)
            {
                kdtree.insert(atom.coord, atom);
            }

            Pdb pdb = copyto.Clone();

            List <string> nlines = new List <string>();

            for (int i = 0; i < pdb.elements.Length; i++)
            {
                var elemi = pdb.elements[i];
                if ((elemi is Pdb.Atom) == false)
                {
                    nlines.Add(elemi.line);
                }
                else
                {
                    var atomi = elemi as Pdb.Atom;
                    var atomx = kdtree.nearest(atomi.coord);
                    HDebug.Assert(atomi.coord[0] == atomx.coord[0]);
                    HDebug.Assert(atomi.coord[1] == atomx.coord[1]);
                    HDebug.Assert(atomi.coord[2] == atomx.coord[2]);
                    /// 55 - 60        Real(6.2)     occupancy    Occupancy.
                    char[] line = atomi.line.ToCharArray();
                    line[55 - 1] = atomx.line[55 - 1];
                    line[56 - 1] = atomx.line[56 - 1];
                    line[57 - 1] = atomx.line[57 - 1];
                    line[58 - 1] = atomx.line[58 - 1];
                    line[59 - 1] = atomx.line[59 - 1];
                    line[60 - 1] = atomx.line[60 - 1];
                    string nline = line.HToString();
                    nlines.Add(nline);
                    Pdb.Atom natomi = Pdb.Atom.FromString(nline);
                    pdb.elements[i] = natomi;
                }
            }
            return(pdb);
        }
Example #14
0
        public static MixHessInfo GetHessMixEAnm(Universe univ, IList <Vector> coords, ILinAlg la
                                                 , IList <ResInfo> lstResAllAtom, double anmCutoff, out string errmsg
                                                 , bool bGetIntmInfo, string strBkbnReso
                                                 )
        {
            HDebug.Assert(anmCutoff >= 0);
            var         anmSprcstCutoffAll      = new Tuple <double, double>(1, anmCutoff);
            var         anmSprcstCutoffCoarse   = new Tuple <double, double>(1, anmCutoff);
            var         anmSprcstCutoffMix      = new Tuple <double, double>(1, anmCutoff);
            bool        bAllowAllToCoarse       = true;
            bool        bOnlyCoarseConnBackbone = false;
            MixHessInfo hessinfo = GetHessMixEAnm(univ, coords, la, lstResAllAtom, bAllowAllToCoarse, bOnlyCoarseConnBackbone
                                                  , anmSprcstCutoffAll, anmSprcstCutoffMix, anmSprcstCutoffCoarse
                                                  , out errmsg, bGetIntmInfo, strBkbnReso);

            return(hessinfo);
        }
Example #15
0
        public static void PCA(string pdbpath, string outpathroot, double scaleAnimation)
        {
            HDebug.ToDo();

            Pdb.Atom[] meanconf;
            Matrix     modes;
            Vector     freq;

            PCA(pdbpath, out meanconf, out modes, out freq);

            for (int i = 0; i < freq.Size; i++)
            {
                string   outpath = outpathroot + i.ToString("00") + ".pdb";
                Vector[] mode    = (modes.GetColVector(i) * scaleAnimation / freq[i]).ToVectors(3);
                Pdb.ToFileAnimated(outpath, meanconf, mode);
            }
        }
Example #16
0
 public double GetEnergyFromAnisous(IList <Vector> coordsto)
 {
     HDebug.Assert(size == coordsto.Count);
     Vector[] dcoords = new Vector[size];
     for (int i = 0; i < size; i++)
     {
         dcoords[i] = coords[i] - coordsto[i];
     }
     {
         double energy = 0;
         for (int i = 0; i < size; i++)
         {
             energy += LinAlg.VtMV(dcoords[i], hess[i, i], dcoords[i]);
         }
         return(energy);
     }
 }
Example #17
0
        public static Matrix GetCorrMatrix(this IList <Mode> modes)
        {
            if (HDebug.Selftest())
            {
                HDebug.Assert(GetCorrMatrix_SelfTest(modes, GetCorrMatrix));
            }

            Vector[][] eigvecs = new Vector[modes.Count][];
            double[]   eigvals = new double[modes.Count];
            for (int i = 0; i < modes.Count; i++)
            {
                eigvals[i] = modes[i].eigval;
                eigvecs[i] = modes[i].GetEigvecsOfAtoms();
            }

            int    size = modes[0].size;
            Matrix Dij  = Matrix.Zeros(size, size);
            Vector Dii  = new double[size];

            for (int i = 0; i < eigvals.Length; i++)
            {
                Vector[] eigvec = eigvecs[i];
                double   eigval = eigvals[i];
                for (int c = 0; c < size; c++)
                {
                    for (int r = 0; r < size; r++)
                    {
                        Dij[c, r] += (LinAlg.VtV(eigvec[c], eigvec[r]) / eigval);
                    }
                    Dii[c] += (LinAlg.VtV(eigvec[c], eigvec[c]) / eigval);
                }
            }
            Dij = Dij / modes.Count;
            Dii = Dii / modes.Count;

            // Cij
            for (int c = 0; c < size; c++)
            {
                for (int r = 0; r < size; r++)
                {
                    Dij[c, r] /= Math.Sqrt(Dii[c] * Dii[r]);
                }
            }

            return(Dij);
        }
Example #18
0
        public static double GetTinkerRmsGradient(this Vector[] forcs)
        {
            double gnorm = 0;

            foreach (Vector forc in forcs)
            {
                HDebug.Assert(forc.Size == 3);
                double x2 = forc[0] * forc[0];
                double y2 = forc[1] * forc[1];
                double z2 = forc[2] * forc[2];
                gnorm += (x2 + y2 + z2);
            }
            gnorm = Math.Sqrt(gnorm);
            double grms = gnorm / Math.Sqrt(forcs.Length);

            return(grms);
        }
Example #19
0
            public static int FindType(string[] types, params string[] query)
            {
                int count_X = 0;

                foreach (string type in types)
                {
                    if (type == "X")
                    {
                        count_X++;
                    }
                }

                int length = types.Length;

                HDebug.Assert(types.Length == query.Length);
                {
                    int match = 0;
                    for (int i = 0; i < length; i++)
                    {
                        if (types[i] == "X" || types[i] == query[i])
                        {
                            match++;
                        }
                    }
                    if (match == length)
                    {
                        return(count_X);
                    }
                }
                {
                    int match = 0;
                    for (int i = 0; i < length; i++)
                    {
                        if (types[i] == "X" || types[i] == query[length - i - 1])
                        {
                            match++;
                        }
                    }
                    if (match == length)
                    {
                        return(count_X);
                    }
                }
                // not found
                return(-1);
            }
Example #20
0
        public static MatrixByArr GetHessian(this IList <Mode> modes, IList <double> masses, ILinAlg la)
        {
            /// mode.eigval =             mweigval
            /// mode.eigvec = mass^-0.5 * mweigvec
            ///
            /// W = [mode(1).eigvec, mode(2).eigvec, ... ]
            /// M = masses
            /// D = diag([mode(1).eigval, mode(2).eigval, ...])
            ///
            /// mwH = M^-0.5 H M^-0.5
            /// [V,D] = eig(mwH)
            /// mode.eigval == D
            /// mode.eigvec == M^-0.5 V = W
            ///
            /// W D W' = M^-0.5       V D V       M^-0.5
            ///        = M^-0.5        mwH        M^-0.5
            ///        = M^-0.5 (M^-0.5 H M^-0.5) M^-0.5
            ///        = M^-1           H         M^-1
            /// H = M (M^-1 H M^-1) M
            ///   = M (W    D   W') M
            ///   = (M W)   D   (M W)'
            HDebug.Assert(modes.CheckModeMassReduced(masses.ToArray(), la, 0.000001));

            Vector[] mws = modes.ListEigvec().ToArray();
            for (int iv = 0; iv < mws.Length; iv++)
            {
                for (int i = 0; i < mws[iv].Size; i++)
                {
                    mws[iv][i] = mws[iv][i] * masses[i / 3];
                }
            }
            Vector      ds = modes.ListEigval().ToArray();
            MatrixByArr hess;

            {
                var MW = la.ToILMat(mws.ToMatrix());
                var D  = la.ToILMat(ds).Diag();
                var H  = la.Mul(MW, D, MW.Tr);
                hess = H.ToArray();
                MW.Dispose();
                D.Dispose();
                H.Dispose();
            }
            return(hess);
        }
Example #21
0
        public static char[] ListSecondStruc <ATOM>(this IList <ATOM> atoms, IList <Pdb.Helix> helix, IList <Pdb.Sheet> sheet)
            where ATOM : IAtom
        {
            Dictionary <ATOM, char> atom_struc = new Dictionary <ATOM, char>();
            {
                foreach (Tuple <Pdb.Helix[], ATOM[]> helix_atoms in helix.HSelectAtoms(atoms))
                {
                    foreach (ATOM atom in helix_atoms.Item2)
                    {
                        if (atom_struc.ContainsKey(atom) == false)
                        {
                            atom_struc.Add(atom, 'H');
                        }
                        HDebug.Assert(atom_struc[atom] == 'H');
                    }
                }
                foreach (Tuple <Pdb.Sheet[], ATOM[]> sheet_atoms in sheet.HSelectAtoms(atoms))
                {
                    foreach (ATOM atom in sheet_atoms.Item2)
                    {
                        if (atom_struc.ContainsKey(atom) == false)
                        {
                            atom_struc.Add(atom, 'S');
                        }
                        HDebug.Assert(atom_struc[atom] == 'S');
                    }
                }
            }
            int leng = atoms.Count;

            char[] secondstruc = new char[leng];
            for (int i = 0; i < leng; i++)
            {
                var atom = atoms[i];
                if (atom_struc.ContainsKey(atom))
                {
                    secondstruc[i] = atom_struc[atom];
                }
                else
                {
                    secondstruc[i] = 'L';
                }
            }
            return(secondstruc);
        }
Example #22
0
        public static List <Atom> CloneByUpdateCoord(this IList <Atom> atoms, IList <Vector> coords, string optNullAtom)
        {
            HDebug.Assert(optNullAtom == null);
            HDebug.Assert(atoms.Count == coords.Count);
            int count = Math.Max(atoms.Count, coords.Count);

            Atom[] uatoms = new Atom[count];

            for (int i = 0; i < count; i++)
            {
                Atom atom = atoms[i];
                if (atom == null)
                {
                    switch (optNullAtom)
                    {
                    case null: goto default;

                    case "if null, set as hydrogen":
                    {
                        int maxserial = atoms.HRemoveAll(null).ListSerial().Max();
                        // "ATOM      2  HT1 ARG A  17     -21.666 -13.812 202.912  0.00  0.00      P1   H"
                        atom = Atom.FromData
                               (
                            serial: maxserial + 1,
                            name: "H",
                            resName: uatoms[i - 1].resName,
                            chainID: uatoms[i - 1].chainID,
                            resSeq: uatoms[i - 1].resSeq,
                            x: 0,
                            y: 0,
                            z: 0,
                            element: "H"
                               );
                        maxserial++;
                    }
                    break;

                    default:
                        throw new Exception();
                    }
                }
                uatoms[i] = Atom.FromString(atom.GetUpdatedLine(coords[i]));
            }
            return(uatoms.ToList());
        }
Example #23
0
            public static Trans3 GetTrans(IList <Vector> C1, IList <Vector> C2, HPack <int> optIter = null, HPack <List <double> > optListWeightSum = null)
            {
                HDebug.Assert(C1.Count == C2.Count);
                int size = C1.Count;

                if (optIter != null)
                {
                    optIter.value = 0;
                }

                Trans3 trans0 = new Trans3(new double[3], 1, Quaternion.UnitRotation);
                Trans3 trans1 = Geometry.AlignPointPoint.MinRMSD.GetTrans(C2, C1);

                if (optListWeightSum != null)
                {
                    optListWeightSum.value = new List <double>();
                }
                while ((trans0.TransformMatrix - trans1.TransformMatrix).ToArray().HAbs().HMax() > 0.00000001)
                {
                    if (optIter != null)
                    {
                        optIter.value++;
                    }
                    Vector[] C2trans = trans1.GetTransformed(C2).ToArray();

                    double[] weight = new double[size];
                    double[] dist2s = new double[size];
                    for (int i = 0; i < size; i++)
                    {
                        double dist2 = (C1[i] - C2trans[i]).Dist2;
                        dist2     = HMath.Between(0.001, dist2, double.PositiveInfinity);
                        weight[i] = 1 / dist2;
                        dist2s[i] = dist2;
                    }
                    if (optListWeightSum != null)
                    {
                        optListWeightSum.value.Add(weight.Sum());
                    }

                    trans0 = trans1;
                    trans1 = Geometry.AlignPointPoint.MinRMSD.GetTrans(C2, C1, weight);
                }

                return(trans1);
            }
Example #24
0
        public static Vector GetBFactor(Matrix hess
                                        , int numIgnoreSmallEigval = 6       // the number to ignore the eigenvalues
                                        , bool zeroForNegEigval    = true    // set zero for the negative eigen values
                                        )
        {
            HDebug.Assert(GetBFactorSelfTest());

            int n = hess.ColSize / 3;

            HDebug.Assert(n * 3 == hess.ColSize);

            HDebug.Assert(hess.RowSize == hess.ColSize);
            InfoPack lextra = new InfoPack();

            Vector[] eigvec;
            double[] eigval;
            NumericSolver.Eig(hess.ToArray(), out eigvec, out eigval);

            double[]   abs_eigval            = eigval.HAbs();
            List <int> idx_sorted_abs_eigval = new List <int>(abs_eigval.HIdxSorted());

            for (int i = 0; i < numIgnoreSmallEigval; i++)
            {
                idx_sorted_abs_eigval.RemoveAt(0);
            }

            Vector bfactor = new double[n];

            foreach (int idx in idx_sorted_abs_eigval)
            {
                if (eigval[idx] < 0)
                {
                    continue;
                }
                for (int i = 0; i < n; i++)
                {
                    double dx = eigvec[idx][i * 3 + 0];
                    double dy = eigvec[idx][i * 3 + 1];
                    double dz = eigvec[idx][i * 3 + 2];
                    bfactor[i] += (dx * dx + dy * dy + dz * dz) / eigval[idx];
                }
            }

            return(bfactor);
        }
Example #25
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);
        }
Example #26
0
            public static double[] GetBFactor(Mode[] modes, double[] mass = null)
            {
                if (HDebug.Selftest())
                #region selftest
                {
                    using (new Matlab.NamedLock("SELFTEST"))
                    {
                        Matlab.Clear("SELFTEST");
                        Matlab.Execute("SELFTEST.hess = rand(30);");
                        Matlab.Execute("SELFTEST.hess = SELFTEST.hess + SELFTEST.hess';");
                        Matlab.Execute("SELFTEST.invhess = inv(SELFTEST.hess);");
                        Matlab.Execute("SELFTEST.bfactor3 = diag(SELFTEST.invhess);");
                        Matlab.Execute("SELFTEST.bfactor = SELFTEST.bfactor3(1:3:end) + SELFTEST.bfactor3(2:3:end) + SELFTEST.bfactor3(3:3:end);");
                        MatrixByArr selftest_hess    = Matlab.GetMatrix("SELFTEST.hess");
                        Mode[]      selftest_mode    = Hess.GetModesFromHess(selftest_hess);
                        Vector      selftest_bfactor = BFactor.GetBFactor(selftest_mode);
                        Vector      selftest_check   = Matlab.GetVector("SELFTEST.bfactor");
                        Vector      selftest_diff    = selftest_bfactor - selftest_check;
                        HDebug.AssertTolerance(0.00000001, selftest_diff);
                        Matlab.Clear("SELFTEST");
                    }
                }
                #endregion

                int size = modes[0].size;
                if (mass != null)
                {
                    HDebug.Assert(size == mass.Length);
                }
                double[] bfactor = new double[size];
                for (int i = 0; i < size; i++)
                {
                    foreach (Mode mode in modes)
                    {
                        bfactor[i] += mode.eigvec[i * 3 + 0] * mode.eigvec[i * 3 + 0] / mode.eigval;
                        bfactor[i] += mode.eigvec[i * 3 + 1] * mode.eigvec[i * 3 + 1] / mode.eigval;
                        bfactor[i] += mode.eigvec[i * 3 + 2] * mode.eigvec[i * 3 + 2] / mode.eigval;
                    }
                    if (mass != null)
                    {
                        bfactor[i] /= mass[i];
                    }
                }
                return(bfactor);
            }
Example #27
0
        public static Matrix GetCorrMatrixMatlab(this IList <Mode> modes)
        {
            if (HDebug.Selftest())
            {
                HDebug.Assert(GetCorrMatrix_SelfTest(modes, GetCorrMatrixMatlab));
            }

            //  Dii = zeros(n, 1);
            //  For[i=1, i<=nmodes, i++,
            //      Dii = Dii + Table[Dot[vec,vec],{vec,modei}]/eigvi;
            //      ];
            //  Dij = zeros(n, n);
            //  For[i=1, i<=nmodes, i++,
            //      Dijx = Dijx + modei_x.Transpose[modei_x] / eigvi;
            //      Dijy = Dijy + modei_y.Transpose[modei_y] / eigvi;
            //      Dijz = Dijz + modei_z.Transpose[modei_z] / eigvi;
            //      Dii = Dii + Table[Dot[vec,vec],{vec,modei}]/eigvi;
            //      ];
            //  Dij = Dij / nmodes;
            //  Dii = Dii / nmodes;
            //  Cij = Dij / Sqrt[Transpose[{Dii}].{Dii}];
            //  corr = Cij;

            Matrix MD = modes.ListEigvec().ToMatrix();
            Vector EV = modes.ListEigval().ToArray();
            Matrix corrmat;

            using (new Matlab.NamedLock(""))
            {
                Matlab.Clear();
                Matlab.PutMatrix("MD", MD, true);
                Matlab.PutVector("EV", EV);
                Matlab.Execute("nmodes = length(EV);");
                Matlab.Execute("iEV   = diag(1 ./ EV);");
                Matlab.Execute("Dijx = MD(1:3:end,:); Dijx = Dijx*iEV*Dijx'; Dij=    Dijx; clear Dijx;");
                Matlab.Execute("Dijy = MD(2:3:end,:); Dijy = Dijy*iEV*Dijy'; Dij=Dij+Dijy; clear Dijy;");
                Matlab.Execute("Dijz = MD(3:3:end,:); Dijz = Dijz*iEV*Dijz'; Dij=Dij+Dijz; clear Dijz;");
                Matlab.Execute("clear MD; clear EV; clear iEV;");
                Matlab.Execute("Dij = Dij / nmodes;");
                Matlab.Execute("Dii = diag(Dij);");
                Matlab.Execute("Dij = Dij ./ sqrt(Dii*Dii');");
                corrmat = Matlab.GetMatrix("Dij", true);
            }
            return(corrmat);
        }
        public double GetGetTorsionalAngle(Atom atom1, Atom atom2)
        {
            List <double> torangle = new List <double>();

            foreach (Dihedral dihedral in dihedrals)
            {
                if ((dihedral.atoms[1].ID == atom1.ID) && (dihedral.atoms[2].ID == atom2.ID))
                {
                    torangle.Add(Geometry.TorsionalAngle(atoms[0].Coord, atoms[1].Coord, atoms[2].Coord, atoms[3].Coord));
                }
                if ((dihedral.atoms[2].ID == atom1.ID) && (dihedral.atoms[1].ID == atom2.ID))
                {
                    torangle.Add(Geometry.TorsionalAngle(atoms[3].Coord, atoms[2].Coord, atoms[1].Coord, atoms[0].Coord));
                }
            }
            HDebug.Assert(torangle.Count == 1);
            return(torangle.Average());
        }
Example #29
0
                public static bool CheckEckartConditions(Universe univ, List <RotableInfo> rotInfos, MatrixByArr[,] J)
                {
                    int n = univ.atoms.Count;
                    int m = rotInfos.Count;

                    //
                    for (int a = 0; a < m; a++)
                    {
                        MatrixByArr mJ = new double[3, 1];
                        for (int i = 0; i < n; i++)
                        {
                            mJ += univ.atoms[i].Mass * J[i, a];
                        }
                        HDebug.AssertTolerance(0.00001, mJ);
                    }

                    return(true);
                }
Example #30
0
        public Vector GetMasses(int dim = 1)
        {
            Vector masses = atoms.ToArray().GetMasses(dim);

            if (HDebug.IsDebuggerAttached)
            {
                double[] tmasses = new double[size * dim];
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < dim; j++)
                    {
                        tmasses[i * dim + j] = atoms[i].Mass;
                    }
                }
                HDebug.AssertTolerance(0, masses - tmasses);
            }
            return(masses);
        }