示例#1
0
        public static Mode[] GetModes(MatrixByArr hess, string cachepath = null)
        {
            Vector[] eigvec;
            double[] eigval;
            if (cachepath != null && HFile.Exists(cachepath))
            {
                HSerialize.Deserialize(cachepath, null, out eigval, out eigvec);
            }
            else
            {
                HDebug.Verify(NumericSolver.Eig(hess, out eigvec, out eigval));
                HSerialize.SerializeDepreciated(cachepath, null, eigval, eigvec);
            }

            List <Mode> modes;

            {   // sort by eigenvalues
                int[] idx = eigval.HAbs().HIdxSorted();
                modes = new List <Mode>(idx.Length);
                for (int i = 0; i < eigval.Length; i++)
                {
                    Mode mode = new Mode {
                        eigval = eigval[idx[i]],
                        eigvec = eigvec[idx[i]]
                    };
                    modes.Add(mode);
                }
            }

            return(modes.ToArray());
        }
示例#2
0
 public static bool SelfTest()
 {
     HDebug.Verify(Hess.GetHessAnmSelfTest());
     HDebug.Verify(Hess.GetHessGnmSelfTest());
     HDebug.Verify(Hess.GetModesSelftest());
     HDebug.Verify(Hess.GetBFactorSelfTest());
     //HDebug.Verify(Hess.HessSbNMA.SelfTest());
     return(true);
 }
示例#3
0
 void GetMoleculeRec(Molecule mole, Atom atom)
 {
     if (mole.atoms.Contains(atom))
     {
         return;
     }
     mole.atoms.Add(atom);
     foreach (var bond in atom.Bonds)
     {
         if (mole.bonds.Contains(bond))
         {
             continue;
         }
         HDebug.Verify(mole.bonds.Add(bond));
         HDebug.Assert(bond.atoms.Length == 2);
         Atom batom = (bond.atoms[0] == atom) ? bond.atoms[1] : bond.atoms[0];
         HDebug.Assert(object.ReferenceEquals(atom, batom) == false);
         GetMoleculeRec(mole, batom);
     }
 }
示例#4
0
        public static Anisou[] FromHessian(Mode[] modesMassWeighted, double[] mass, double scale = 10000 *1000
                                           , string cachepath = null
                                           )
        {
            if (cachepath != null && HFile.Exists(cachepath))
            {
                List <Anisou> lstanisou;
                HDebug.Verify(HSerialize.Deserialize(cachepath, null, out lstanisou));
                return(lstanisou.ToArray());
            }

            int size = modesMassWeighted.Size();

            HDebug.Assert(size == mass.Length);
            Anisou[] anisous = new Anisou[size];

            for (int i = 0; i < size; i++)
            {
                MatrixByArr invHii = new double[3, 3];
                foreach (Mode mode in modesMassWeighted)
                {
                    Vector modei = mode.GetEigvecOfAtom(i);
                    invHii = invHii + LinAlg.VVt(modei, modei) * mode.eigval;
                }

                MatrixByArr Ui = invHii * scale / mass[i];

                anisous[i] = Anisou.FromMatrix(Ui);
            }

            if (cachepath != null)
            {
                HSerialize.Serialize(cachepath, null, new List <Anisou>(anisous));
            }

            return(anisous);
        }
示例#5
0
        public Molecule[] GetMolecules()
        {
            List <Molecule> moles  = new List <Molecule>();
            HashSet <Atom>  latoms = new HashSet <Atom>(atoms.ToArray());

            while (latoms.Count > 0)
            {
                Molecule mole = GetMolecule(latoms.First());
                foreach (var matom in mole.atoms)
                {
                    HDebug.Verify(latoms.Remove(matom));
                }
                moles.Add(mole);
            }

            if (HDebug.IsDebuggerAttached)
            {
                HashSet <Atom> datoms = new HashSet <Atom>();
                HashSet <Bond> dbonds = new HashSet <Bond>();
                foreach (var mole in moles)
                {
                    foreach (var atom in mole.atoms)
                    {
                        datoms.Add(atom);
                    }
                    foreach (var bond in mole.bonds)
                    {
                        dbonds.Add(bond);
                    }
                }
                HDebug.Assert(datoms.Count == atoms.Count);
                HDebug.Assert(dbonds.Count == bonds.Count);
            }

            return(moles.ToArray());
        }
示例#6
0
        //public static Mode[] GetModes(Matrix hess)
        //{
        //    return GetModesFromHess(hess);
        //}
        public static Mode[] GetModesFromHess(Matrix hess)
        {
            //string cachepath = null;
            HDebug.Depreciated("use Mode[] GetModesFromHess(Matrix hess, ILinAlg la)");

            Vector[] eigvec;
            double[] eigval;
            //if(cachepath != null && HFile.Exists(cachepath))
            //{
            //    HSerialize.Deserialize(cachepath, null, out eigval, out eigvec);
            //}
            //else
            //{
            HDebug.Verify(NumericSolver.Eig(hess.ToArray(), out eigvec, out eigval));
            //    if(cachepath != null)
            //        HSerialize.SerializeDepreciated(cachepath, null, eigval, eigvec);
            //}

            List <Mode> modes;

            {   // sort by eigenvalues
                int[] idx = eigval.HAbs().HIdxSorted();
                modes = new List <Mode>(idx.Length);
                for (int i = 0; i < eigval.Length; i++)
                {
                    Mode mode = new Mode
                    {
                        eigval = eigval[idx[i]],
                        eigvec = eigvec[idx[i]]
                    };
                    modes.Add(mode);
                }
            }

            return(modes.ToArray());
        }
示例#7
0
        public static Anisou[] FromHessian(MatrixByArr hessMassWeighted, double[] mass, double scale = 10000 *1000
                                           , string cachepath = null
                                           )
        {
            /// Estimation of "anisotropic temperature factors" (ANISOU)
            ///
            /// delta = hess^-1 * force
            ///       = (0 + V7*V7'/L7 + V8*V8'/L8 + V9*V9'/L9 + ...) * force    (* assume that 1-6 eigvecs/eigvals are ignored, because rot,trans *)
            ///
            /// Assume that force[i] follows gaussian distributions N(0,1). Here, if there are 1000 samples, let denote i-th force as fi, and its j-th element as fi[j]
            /// Then, $V7' * fi = si7, V8' * fi = si8, ...$ follows gaussian distribution N(0,1), too.
            /// Its moved position by k-th eigen component is determined then, as
            ///     dik = (Vk * Vk' / Lk) * Fi
            ///         = Vk / Lk * (Vk' * Fi)
            ///         = Vk / Lk * Sik.
            /// Additionally, the moved position j-th atom is:
            ///     dik[j] = Vk[j] / Lk[j] * Sik.
            /// and its correlation matrix is written as (because its mean position is 0 !!!):
            ///     Cik[j] = dik[j] * dik[j]'
            ///            = [dik[j]_x * dik[j]_x    dik[j]_x * dik[j]_y    dik[j]_x * dik[j]_z]
            ///              [dik[j]_y * dik[j]_x    dik[j]_y * dik[j]_y    dik[j]_y * dik[j]_z]
            ///              [dik[j]_z * dik[j]_x    dik[j]_z * dik[j]_y    dik[j]_z * dik[j]_z]
            ///            = (Vk[j] * Vk[j]') / (Lk[j]*Lk[j]) * (Sik*Sik).
            ///
            /// Note that Sik*Sik follows the chi-square distribution, because Sik follows the gaussian distribution N(0,1).
            /// Additionally, note that the thermal fluctuation is (not one projection toward k-th eigen component with only i-th force, but) the results of 1..i.. forced movements and 1..k.. eigen components.
            /// Therefore, for j-th atom, the accumulation of the correlation over all forces (1..i..) with all eigen components (1..k..) is:
            ///     C[j] = sum_{i,k} {(Vk[j] * Vk[j]') / (Lk[j]*Lk[j]) * (Sik*Sik)}.
            ///
            /// Here, Sik is normal distribution independent to i and k. Therefore, the mean of C[j] is
            ///     E(C[j]) = E( sum_{i,k} {(Vk[j] * Vk[j]') / (Lk[j]*Lk[j]) * (Sik*Sik)} )
            ///             = sum_{i,k} E( (Vk[j] * Vk[j]') / (Lk[j]*Lk[j]) * (Sik*Sik) )
            ///             = sum_{i,k} { (Vk[j] * Vk[j]') / (Lk[j]*Lk[j]) * E(Sik*Sik) }
            ///             = sum_{i,k} { (Vk[j] * Vk[j]') / (Lk[j]*Lk[j]) * 1          }          (* because mean of E(x*x)=1 where x~N(0,1) *)
            ///             = sum_{k} { (Vk[j] * Vk[j]') / (Lk[j]*Lk[j]) }
            ///
            /// Note that E(C[j]) is same to the j-th diagonal component of inverse hessian matrix (except, the eigenvalues are squared).
            ///
            /// Fixation: Gromacx generate the ensemble X by
            ///               X[j] = sum_{k} {Vk / sqrt(Lk[j]) / sqrt(mass[j]) * x_k},
            ///           where x~N(0,1). However, the above is assumed as
            ///               X[j] = sum_{k} {Vk / Lk[j] * x_k}.
            ///           In order to apply the assumption by the Gromacs ensemble, The equation should be fixed as
            ///               E(C[j]) = sum_{k} { (Vk[j] * Vk[j]') / (sqrt(Lk[j])*sqrt(Lk[j])) }
            ///                       = sum_{k} { (Vk[j] * Vk[j]') / Lk[j] / mass[j] }
            ///

            int size = mass.Length;

            HDebug.Assert(hessMassWeighted.RowSize == size * 3, hessMassWeighted.ColSize == size * 3);
            Anisou[] anisous = new Anisou[size];

            if (cachepath != null && HFile.Exists(cachepath))
            {
                List <Anisou> lstanisou;
                HDebug.Verify(HSerialize.Deserialize <List <Anisou> >(cachepath, null, out lstanisou));
                anisous = lstanisou.ToArray();
                return(anisous);
            }

            // anisotropic temperature factors
            using (new Matlab.NamedLock("ANISOU"))
            {
                Matlab.Clear("ANISOU");
                Matlab.PutMatrix("ANISOU.H", hessMassWeighted);
                Matlab.Execute("[ANISOU.V,ANISOU.D] = eig(ANISOU.H);");
                Matlab.Execute("ANISOU.D = diag(ANISOU.D);");                                 // get diagonal
                {
                    Matlab.Execute("[ANISOU.sortD, ANISOU.sortIdxD] = sort(abs(ANISOU.D));"); // sorted index of abs(D)
                    Matlab.Execute("ANISOU.D(ANISOU.sortIdxD(1:6)) = 0;");                    // set the 6 smallest eigenvalues as zero
                    //Matlab.Execute("ANISOU.D(ANISOU.D < 0) = 0;");                              // set negative eigenvalues as zero
                }
                //{
                //    Matlab.Execute("ANISOU.D(1:6) = 0;");
                //}
                Matlab.Execute("ANISOU.invD = 1 ./ ANISOU.D;");                             // set invD
                Matlab.Execute("ANISOU.invD(ANISOU.D == 0) = 0;");                          // set Inf (by divided by zero) as zero
                //Matlab.Execute("ANISOU.D = ANISOU.D .^ 2;"); // assume the gromacs ensemble condition
                Matlab.Execute("ANISOU.invH = ANISOU.V * diag(ANISOU.invD) * ANISOU.V';");
                for (int i = 0; i < size; i++)
                {
                    string      idx = string.Format("{0}:{1}", i * 3 + 1, i * 3 + 3);
                    MatrixByArr U   = Matlab.GetMatrix("ANISOU.invH(" + idx + "," + idx + ")");
                    U *= (scale / mass[i]);

                    anisous[i] = Anisou.FromMatrix(U);
                }
                Matlab.Clear("ANISOU");
            }

            if (cachepath != null)
            {
                HSerialize.Serialize(cachepath, null, new List <Anisou>(anisous));
            }

            return(anisous);
        }
示例#8
0
            public static Tuple <string, string, int, string>[] FromResnResi  // (operation, wt_resn, wt_resi, mut_resn)[]
                (Tuple <string, int>[]  wt_resn_resi
                , Tuple <string, int>[] mut_resn_resi
                , Tuple <int, int> rngLcsLen
                )
            {
                var lcs = HSequence.LongCommSubseq.GetLongCommSubseq(wt_resn_resi, mut_resn_resi);

                if (rngLcsLen != null)
                {
                    if ((lcs.lcs_resn.Length < rngLcsLen.Item1) || (rngLcsLen.Item2 < lcs.lcs_resn.Length))
                    {
                        return(null);
                    }
                }

                List <Tuple <string, string, int, string> > mutationinfos = new List <Tuple <string, string, int, string> >();

                // (operation, wt_resn, wt_resi, mut_resn)
                //var aminoacids = HBioinfo.AminoAcids.ToDictionaryBy3Letter(true);
                //string[] mutate = new string[0];
                for (int i = 0; i < lcs.lcs_oper1to2.Length; i++)
                {
                    if (lcs.lcs_oper1to2[i].Item1 == LCS.Oper.Match)
                    {
                        continue;
                    }

                    /// 1. collecting deletes and inserts until next match
                    List <int> idxs_del = new List <int>();
                    List <int> idxs_ins = new List <int>();
                    int        last_j   = i;
                    for (int j = i; j < lcs.lcs_oper1to2.Length; j++)
                    {
                        if (lcs.lcs_oper1to2[j].Item1 == LCS.Oper.Match)
                        {
                            break;
                        }
                        if (lcs.lcs_oper1to2[j].Item1 == LCS.Oper.Delete)
                        {
                            idxs_del.Add(j);
                        }
                        if (lcs.lcs_oper1to2[j].Item1 == LCS.Oper.Insert)
                        {
                            idxs_ins.Add(j);
                        }
                        last_j = j;
                    }
                    /// 2. determine matching del-ins
                    List <Tuple <int, int> > idxs_del_ins = new List <Tuple <int, int> >();
                    while ((idxs_del.Count != 0) && (idxs_ins.Count != 0))
                    {
                        int idx_del = (i == 0) ? idxs_del.Last() : idxs_del.First();
                        int idx_ins = idxs_ins.First();
                        idxs_del_ins.Add(new Tuple <int, int>(idx_del, idx_ins));
                        HDebug.Verify(idxs_del.Remove(idx_del));
                        HDebug.Verify(idxs_ins.Remove(idx_ins));
                    }
                    /// 3. handle mutation informations
                    while ((idxs_del.Count != 0) || (idxs_ins.Count != 0) || (idxs_del_ins.Count != 0))
                    {
                        Tuple <int, int> idx_del_ins = null;
                        int idx_del = int.MaxValue; if (idxs_del.Count > 0)
                        {
                            idx_del = idxs_del.First();
                        }
                        int idx_ins = int.MaxValue; if (idxs_ins.Count > 0)
                        {
                            idx_ins = idxs_ins.First();
                        }
                        int idx_rep = int.MaxValue; if (idxs_del_ins.Count > 0)
                        {
                            idx_del_ins = idxs_del_ins.First(); idx_rep = idx_del_ins.HToArray().Min();
                        }
                        int min_idx = HMath.HMin(idx_del, idx_ins, idx_rep);
                        HDebug.Assert(min_idx != int.MaxValue);

                        if (idx_del == min_idx)
                        {
                            string resn1 = lcs.lcs_oper1to2[idx_del].Item2;
                            int    resi1 = lcs.lcs_oper1to2[idx_del].Item3.Value;
                            //string mutatei = string.Format("delete {0}{1}", aminoacids[resn].name1, resi);
                            //mutate = mutate.HAdd(mutatei);
                            mutationinfos.Add(new Tuple <string, string, int, string>("delete", resn1, resi1, null));
                            HDebug.Verify(idxs_del.Remove(idx_del));
                            continue;
                        }
                        if (idx_ins == min_idx)
                        {
                            string resn_insertafter = (i == 0) ? "   "        : lcs.lcs_oper1to2[i - 1].Item2;
                            int    resi_insertafter = (i == 0) ? int.MinValue : lcs.lcs_oper1to2[i - 1].Item3.Value;
                            string resn2            = lcs.lcs_oper1to2[idx_ins].Item2;
                            HDebug.Assert(lcs.lcs_oper1to2[idx_ins].Item3 == null);
                            mutationinfos.Add(new Tuple <string, string, int, string>("insert", resn_insertafter, resi_insertafter, resn2));
                            HDebug.Verify(idxs_ins.Remove(idx_ins));
                            continue;
                        }
                        if (idx_rep == min_idx)
                        {
                            int    idel  = idx_del_ins.Item1;
                            int    iins  = idx_del_ins.Item2;
                            string resn1 = lcs.lcs_oper1to2[idel].Item2;
                            int    resi1 = lcs.lcs_oper1to2[idel].Item3.Value;
                            string resn2 = lcs.lcs_oper1to2[iins].Item2;
                            HDebug.Assert(lcs.lcs_oper1to2[iins].Item3 == null);
                            //string mutatei = string.Format("{0}{1}{2}", aminoacids[resn1].name1, resi1, aminoacids[resn2].name1);
                            //mutate = mutate.HAdd(mutatei);
                            mutationinfos.Add(new Tuple <string, string, int, string>("replace", resn1, resi1, resn2));
                            HDebug.Verify(idxs_del_ins.Remove(idx_del_ins));
                            continue;
                        }
                    }
                    i = last_j;
                    continue;
                }

                return(mutationinfos.ToArray());
            }
示例#9
0
            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);
            }
示例#10
0
            public static Universe Build(Pdb pdb, Gromacs.Top top, ITextLogger logger)
            {
                Universe univ = new Universe();

                // atoms
                List <Top.Atom>          top_atoms         = top.elements.SelectSourceExtTop().ListAtom().SelectMatchToPdb(pdb.atoms);
                List <Top.Bond>          top_bonds         = top.elements.SelectSourceExtTop().ListType <Top.Bond>();
                List <Top.Pair>          top_pairs         = top.elements.SelectSourceExtTop().ListType <Top.Pair>(); ///Debug.ToDo("handle pairtype <= nbnd 1-4");
                List <Top.Angle>         top_angles        = top.elements.SelectSourceExtTop().ListType <Top.Angle>();
                List <Top.Dihedral>      top_dihedral      = top.elements.SelectSourceExtTop().ListType <Top.Dihedral>();
                List <Top.Atomtypes>     top_atomtypes     = top.elements.ListAtomtypes();
                List <Top.Bondtypes>     top_bondtypes     = top.elements.ListType <Top.Bondtypes>();
                List <Top.Angletypes>    top_angletypes    = top.elements.ListType <Top.Angletypes>();
                List <Top.Dihedraltypes> top_dihedraltypes = top.elements.ListType <Top.Dihedraltypes>();
                List <Top.Pairtypes>     top_pairtypes     = top.elements.ListType <Top.Pairtypes>();   ///Debug.ToDo("handle pairtype <= nbnd 1-4");

                {
                    ///Debug.ToDo("handle here");
                    List <Top.LineElement> elems = new List <Top.LineElement>(top.elements);
                    foreach (var elem in top_atoms)
                    {
                        HDebug.Verify(elems.Remove(elem));
                    }
                    foreach (var elem in top_bonds)
                    {
                        HDebug.Verify(elems.Remove(elem));
                    }
                    foreach (var elem in top_pairs)
                    {
                        HDebug.Verify(elems.Remove(elem));
                    }
                    foreach (var elem in top_angles)
                    {
                        HDebug.Verify(elems.Remove(elem));
                    }
                    foreach (var elem in top_dihedral)
                    {
                        HDebug.Verify(elems.Remove(elem));
                    }
                    foreach (var elem in top_atomtypes)
                    {
                        HDebug.Verify(elems.Remove(elem));
                    }
                    foreach (var elem in top_bondtypes)
                    {
                        HDebug.Verify(elems.Remove(elem));
                    }
                    foreach (var elem in top_angletypes)
                    {
                        HDebug.Verify(elems.Remove(elem));
                    }
                    foreach (var elem in top_dihedraltypes)
                    {
                        HDebug.Verify(elems.Remove(elem));
                    }
                    foreach (var elem in top_pairtypes)
                    {
                        HDebug.Verify(elems.Remove(elem));
                    }
                    List <Top.LineElement> srcitp = new List <Top.LineElement>();
                    List <Top.LineElement> srctop = new List <Top.LineElement>();
                    for (int i = 0; i < elems.Count; i++)
                    {
                        if ((elems[i].source as string).EndsWith(".itp"))
                        {
                            srcitp.Add(elems[i]); elems[i] = null; continue;
                        }
                        if ((elems[i].source as string).EndsWith(".top"))
                        {
                            srctop.Add(elems[i]); elems[i] = null; continue;
                        }
                    }
                    elems = elems.HRemoveAllNull().ToList();
                    HDebug.Assert(elems.Count == 0);
                }

                Atoms atoms = new Atoms(univ);

                HDebug.Assert(pdb.atoms.Length == top_atoms.Count);
                for (int i = 0; i < top_atoms.Count; i++)
                {
                    //Debug.Assert(psf.atoms[i].AtomId == pdb.atoms[i].serial);
                    //string type0 = psf.atoms[i].AtomType;
                    //Atom atom = new Atom(psf.atoms[i], prm.FindNonbonded(type0, logger), pdb.atoms[i]);
                    //atom.Coord = pdb.atoms[i].coord;
                    //atoms.Add(atom);
                    Gromacs.Top.Atom atom = top_atoms[i];
                    HDebug.Assert(i + 1 == atom.cgnr);
                    HDebug.Assert(atom.cgnr == pdb.atoms[i].serial);
                    //Gromacs.Top.Atomtypes atomtype = top_atomtypes[atom.type];
                    List <Gromacs.Top.Atomtypes> atomtypes = FindTypes(top_atomtypes, atom.type);
                    HDebug.Assert(atomtypes.Count == 1);
                    Gromacs.Top.Atomtypes atomtype = atomtypes.Last();
                    //Debug.Assert(atom.charge == atomtype.charge);
                    //Debug.Assert(atom.mass   == atomtype.mass  );
                    Atom uatom = new Atom(atom.cgnr, atom.atom, atom.type, pdb.atoms[i].element.Trim()
                                          , atom.resnr, atom.residu
                                          , atom.charge, atom.mass
                                          , atomtype.epsilon, atomtype.sigma
                                          , double.NaN, double.NaN
                                          , atom, atomtype
                                          );
                    uatom.Coord = pdb.atoms[i].coord;
                    atoms.Add(uatom);
                }

                // bonds
                Bonds bonds = new Bonds();

                for (int i = 0; i < top_bonds.Count; i++)
                {
                    int idx0 = top_bonds[i].ai - 1; Atom atom0 = atoms[idx0]; string type0 = atom0.AtomType;
                    int idx1 = top_bonds[i].aj - 1; Atom atom1 = atoms[idx1]; string type1 = atom1.AtomType;
                    List <Gromacs.Top.Bondtypes> bondtypes = FindTypes(top_bondtypes, type0, type1);
                    //Debug.Assert(bondtypes.Count == 1);
                    Gromacs.Top.Bondtypes bondtype         = bondtypes.Last();
                    //Gromacs.Top.Bondtypes bondtype = top_bondtypes[Gromacs.Top.Bondtypes.GetStringKey(type0,type1)];
                    Bond bond = new Bond(atom0, atom1, bondtype.kb, bondtype.b0, bondtype);
                    bonds.Add(bond);
                    atom0.Bonds.Add(bond); atom0.Inter123.Add(atom1); atom0.Inter12.Add(atom1);
                    atom1.Bonds.Add(bond); atom1.Inter123.Add(atom0); atom1.Inter12.Add(atom0);
                }

                // angles
                Angles angles = new Angles();

                for (int i = 0; i < top_angles.Count; i++)
                {
                    int idx0 = top_angles[i].ai - 1; Atom atom0 = atoms[idx0]; string type0 = atom0.AtomType;
                    int idx1 = top_angles[i].aj - 1; Atom atom1 = atoms[idx1]; string type1 = atom1.AtomType;
                    int idx2 = top_angles[i].ak - 1; Atom atom2 = atoms[idx2]; string type2 = atom2.AtomType;
                    List <Gromacs.Top.Angletypes> angletypes = FindTypes(top_angletypes, type0, type1, type2);
                    //Debug.Assert(angletypes.Count == 1);
                    Gromacs.Top.Angletypes angletype         = angletypes.Last();
                    //Gromacs.Top.Angletypes angletype = top_angletypes[Gromacs.Top.Bondtypes.GetStringKey(type0, type1, type2)];
                    Angle angle = new Angle(atom0, atom1, atom2
                                            , angletype.cth, angletype.th0, angletype.cub, angletype.ub0
                                            , angletype
                                            );
                    angles.Add(angle);
                    atom0.Angles.Add(angle); atom0.Inter123.Add(atom1); atom0.Inter123.Add(atom2);
                    atom1.Angles.Add(angle); atom1.Inter123.Add(atom2); atom1.Inter123.Add(atom0);
                    atom2.Angles.Add(angle); atom2.Inter123.Add(atom0); atom2.Inter123.Add(atom1);
                }

                // dihedrals
                Dihedrals dihedrals = new Dihedrals();
                Impropers impropers = new Impropers();

                for (int i = 0; i < top_dihedral.Count; i++)
                {
                    int idx0 = top_dihedral[i].ai - 1; Atom atom0 = atoms[idx0]; string type0 = atom0.AtomType;
                    int idx1 = top_dihedral[i].aj - 1; Atom atom1 = atoms[idx1]; string type1 = atom1.AtomType;
                    int idx2 = top_dihedral[i].ak - 1; Atom atom2 = atoms[idx2]; string type2 = atom2.AtomType;
                    int idx3 = top_dihedral[i].al - 1; Atom atom3 = atoms[idx3]; string type3 = atom3.AtomType;
                    int funct = top_dihedral[i].funct;
                    List <Gromacs.Top.Dihedraltypes> dihedraltypes = FindTypes(top_dihedraltypes, type0, type1, type2, type3);
                    {
                        List <Gromacs.Top.Dihedraltypes> ldihedraltypes = new List <Top.Dihedraltypes>(dihedraltypes);
                        // select funct matching to its query
                        for (int j = 0; j < ldihedraltypes.Count;)
                        {
                            if (ldihedraltypes[j].func == funct)
                            {
                                j++;
                            }
                            else
                            {
                                ldihedraltypes.RemoveAt(j);
                            }
                        }
                        // if there are no matching query but improper, select the improper
                        if ((ldihedraltypes.Count == 0) && (dihedraltypes[0].func == 2))
                        {
                            ldihedraltypes.Add(dihedraltypes[0]);
                        }
                        dihedraltypes = new List <Top.Dihedraltypes>(ldihedraltypes);
                    }
                    //{
                    //    // if dihedral(func==9) and improper(func==2) are mixed
                    //    // select only improper
                    //    bool has_func_2 = false;
                    //    for(int j=0; j<dihedraltypes.Count; j++)
                    //        if(dihedraltypes[j].func == 2)
                    //            has_func_2 = true;
                    //    if(has_func_2)
                    //    {
                    //        for(int j=0; j<dihedraltypes.Count; )
                    //        {
                    //            if(dihedraltypes[j].func == 2)
                    //                j++;
                    //            else
                    //                dihedraltypes.RemoveAt(j);
                    //        }
                    //    }
                    //}
                    if (dihedraltypes[0].func == 9)
                    {
                        // dihedral
                        for (int j = 1; j < dihedraltypes.Count; j++)
                        {
                            HDebug.Assert(dihedraltypes[0].func == dihedraltypes[j].func);
                            HDebug.Assert(dihedraltypes[0].i.Trim() == dihedraltypes[j].i.Trim());
                            HDebug.Assert(dihedraltypes[0].j.Trim() == dihedraltypes[j].j.Trim());
                            HDebug.Assert(dihedraltypes[0].k.Trim() == dihedraltypes[j].k.Trim());
                            HDebug.Assert(dihedraltypes[0].l.Trim() == dihedraltypes[j].l.Trim());
                        }
                        Gromacs.Top.Dihedraltypes dihedraltype = dihedraltypes.Last();
                        Dihedral dihedral = new Dihedral(atom0, atom1, atom2, atom3
                                                         , dihedraltype.cp, dihedraltype.mult, dihedraltype.phi0
                                                         , dihedraltype
                                                         );
                        dihedrals.Add(dihedral);
                        atom0.Dihedrals.Add(dihedral); //atom0.Inter123.Add(atom1); atom0.Inter123.Add(atom2); atom0.Inter123.Add(atom3);
                        atom1.Dihedrals.Add(dihedral); //atom1.Inter123.Add(atom2); atom1.Inter123.Add(atom3); atom1.Inter123.Add(atom0);
                        atom2.Dihedrals.Add(dihedral); //atom2.Inter123.Add(atom3); atom2.Inter123.Add(atom0); atom2.Inter123.Add(atom1);
                        atom3.Dihedrals.Add(dihedral); //atom3.Inter123.Add(atom0); atom3.Inter123.Add(atom1); atom3.Inter123.Add(atom2);
                        continue;
                    }
                    if (dihedraltypes[0].func == 2)
                    {
                        // improper
                        for (int j = 1; j < dihedraltypes.Count; j++)
                        {
                            HDebug.Assert(dihedraltypes[0].func == dihedraltypes[j].func);
                            HDebug.Assert(dihedraltypes[0].i.Trim() == dihedraltypes[j].i.Trim());
                            HDebug.Assert(dihedraltypes[0].j.Trim() == dihedraltypes[j].j.Trim());
                            HDebug.Assert(dihedraltypes[0].k.Trim() == dihedraltypes[j].k.Trim());
                            HDebug.Assert(dihedraltypes[0].l.Trim() == dihedraltypes[j].l.Trim());
                        }
                        Gromacs.Top.Dihedraltypes impropertype = dihedraltypes.Last();
                        Improper improper = new Improper(atom0, atom1, atom2, atom3
                                                         , impropertype.cp, impropertype.phi0
                                                         , impropertype
                                                         );
                        impropers.Add(improper);
                        atom0.Impropers.Add(improper); //atom0.Inter123.Add(atom1); atom0.Inter123.Add(atom2); atom0.Inter123.Add(atom3);
                        atom1.Impropers.Add(improper); //atom1.Inter123.Add(atom2); atom1.Inter123.Add(atom3); atom1.Inter123.Add(atom0);
                        atom2.Impropers.Add(improper); //atom2.Inter123.Add(atom3); atom2.Inter123.Add(atom0); atom2.Inter123.Add(atom1);
                        atom3.Impropers.Add(improper); //atom3.Inter123.Add(atom0); atom3.Inter123.Add(atom1); atom3.Inter123.Add(atom2);
                        continue;
                    }
                    HDebug.Assert(false);


                    //Debug.Assert(dihedraltypes.Count == 1);
                    //
                    //for(int j=0; j<dihedraltypes.Count; j++)
                    //    if(dihedraltypes[j].func != 2)
                    //        dihedraltype = dihedraltypes[j];
                    //Debug.Assert(dihedraltype != null);
                    //List<Gromacs.Top.Dihedraltypes> dihedraltypes = top_dihedraltypes[Gromacs.Top.Dihedraltypes.GetStringKey(type0, type1, type2, type3)];
                }

                // 1-4 interactions
                for (int i = 0; i < atoms.Count; i++)
                {
                    HashSet <Atom> Inter14 = new HashSet <Atom>();
                    BuildInter1toN(atoms[i], 4, Inter14); // find all atoms for 1-4 interaction
                    Inter14.Remove(atoms[i]);             // remove self
                    foreach (Atom atom in atoms[i].Inter123)
                    {
                        Inter14.Remove(atom);             // remove all 1-2, 1-3 interactions
                    }
                    atoms[i].Inter14 = Inter14;
                }
                Nonbonded14s nonbonded14s = new Nonbonded14s();

                nonbonded14s.Build(atoms);

                //// nonbondeds
                //// do not make this list in advance, because it depends on the atom positions
                //Nonbondeds nonbondeds = new Nonbondeds();
                //nonbondeds.Build(atoms);


                //Universe univ = new Universe();
                univ.pdb = pdb;
                univ.refs.Add("top", top);
                univ.atoms     = atoms;
                univ.bonds     = bonds;
                univ.angles    = angles;
                univ.dihedrals = dihedrals;
                univ.impropers = impropers;
                //univ.nonbondeds   = nonbondeds  ;  // do not make this list in advance, because it depends on the atom positions
                univ.nonbonded14s = nonbonded14s;

                HDebug.Assert(univ.Verify());
                return(univ);
            }
示例#11
0
 public void Isolate(Universe univ)
 {
     foreach (Bond bond        in new List <Bond>(Bonds))
     {
         foreach (Atom atom in bond.atoms)
         {
             if (atom != this)
             {
                 HDebug.Verify(atom.Bonds.Remove(bond));
             }
         }
         HDebug.Verify(Bonds.Remove(bond)); HDebug.Verify(univ.bonds.Remove(bond));
     }
     foreach (Angle angle       in new List <Angle>(Angles))
     {
         foreach (Atom atom in angle.atoms)
         {
             if (atom != this)
             {
                 HDebug.Verify(atom.Angles.Remove(angle));
             }
         }
         HDebug.Verify(Angles.Remove(angle)); HDebug.Verify(univ.angles.Remove(angle));
     }
     foreach (Dihedral dihedral    in new List <Dihedral>(Dihedrals))
     {
         foreach (Atom atom in dihedral.atoms)
         {
             if (atom != this)
             {
                 HDebug.Verify(atom.Dihedrals.Remove(dihedral));
             }
         }
         HDebug.Verify(Dihedrals.Remove(dihedral)); HDebug.Verify(univ.dihedrals.Remove(dihedral));
     }
     foreach (Improper improper    in new List <Improper>(Impropers))
     {
         foreach (Atom atom in improper.atoms)
         {
             if (atom != this)
             {
                 HDebug.Verify(atom.Impropers.Remove(improper));
             }
         }
         HDebug.Verify(Impropers.Remove(improper)); HDebug.Verify(univ.impropers.Remove(improper));
     }
     foreach (Nonbonded14 nonbonded14 in new List <Nonbonded14>(Nonbonded14s))
     {
         foreach (Atom atom in nonbonded14.atoms)
         {
             if (atom != this)
             {
                 HDebug.Verify(atom.Nonbonded14s.Remove(nonbonded14));
             }
         }
         HDebug.Verify(Nonbonded14s.Remove(nonbonded14)); HDebug.Verify(univ.nonbonded14s.Remove(nonbonded14));
     }
     //public List<Nonbonded> Nonbondeds = new List<Nonbonded>();
     foreach (Atom atom in new List <Atom>(Inter12))
     {
         HDebug.Assert(atom != this); HDebug.Verify(atom.Inter12.Remove(this)); HDebug.Verify(Inter12.Remove(atom));
     }
     foreach (Atom atom in new List <Atom>(Inter123))
     {
         HDebug.Assert(atom != this); HDebug.Verify(atom.Inter123.Remove(this)); HDebug.Verify(Inter123.Remove(atom));
     }
     foreach (Atom atom in new List <Atom>(Inter14))
     {
         HDebug.Assert(atom != this); HDebug.Verify(atom.Inter14.Remove(this)); HDebug.Verify(Inter14.Remove(atom));
     }
 }
示例#12
0
                public Vector[] x;  // eigenvector

                public static Frame FromLines(string[] lines)
                {
                    Frame frame   = new Frame();
                    int   lineidx = 0;

                    {
                        //eigenvec.trr frame 0:
                        string[] tokens = lines[lineidx++].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                        HDebug.Assert(tokens.Length == 3);
                        frame.srcfile = tokens[0];                  // eigenvec.trr
                        HDebug.Assert(tokens[1] == "frame");        // frame
                        HDebug.Assert(tokens[2].Last() == ':');     // 0:
                        frame.frameidx = int.Parse(tokens[2].Replace(":", ""));
                    }
                    {
                        //   natoms=       920  step=         0  time=0.0000000e+00  lambda=         1
                        string[] tokens = lines[lineidx++].Split(new char[] { ' ', '\t', '=' }, StringSplitOptions.RemoveEmptyEntries);
                        HDebug.Assert(tokens[0] == "natoms");
                        frame.natoms = int.Parse(tokens[1]);
                        HDebug.Assert(tokens[2] == "step");
                        frame.step = int.Parse(tokens[3]);
                        HDebug.Assert(tokens[4] == "time");
                        frame.time = double.Parse(tokens[5]);
                        HDebug.Assert(tokens[6] == "lambda");
                        frame.lambda = int.Parse(tokens[7]);
                    }
                    {
                        //box (3x3):
                        //   box[    0]={ 0.00000e+00,  0.00000e+00,  0.00000e+00}
                        //   box[    1]={ 0.00000e+00,  0.00000e+00,  0.00000e+00}
                        //   box[    2]={ 0.00000e+00,  0.00000e+00,  0.00000e+00}
                        lineidx++; HDebug.Assert(lines[lineidx - 1] == "   box (3x3):");
                        lineidx++; //Debug.Assert(lines[lineidx-1] == "      box[    0]={ 0.00000e+00,  0.00000e+00,  0.00000e+00}");
                        lineidx++; //Debug.Assert(lines[lineidx-1] == "      box[    1]={ 0.00000e+00,  0.00000e+00,  0.00000e+00}");
                        lineidx++; //Debug.Assert(lines[lineidx-1] == "      box[    2]={ 0.00000e+00,  0.00000e+00,  0.00000e+00}");
                    }
                    {
                        //   x (920x3):
                        //      x[    0]={ 1.03414e+00,  8.38227e-01,  9.45561e-01}
                        //      x[    1]={ 9.80454e-01,  9.12340e-01,  8.96750e-01}
                        //      x[    2]={ 1.02299e+00,  7.46792e-01,  8.98052e-01}
                        //      x[    3]={ 9.96949e-01,  8.30545e-01,  1.04402e+00}
                        //      ...
                        string line = lines[lineidx++]; //x (920x3):
                        HDebug.Assert(line.StartsWith("   x ("), line.EndsWith("):"));
                        List <Vector> x = new List <Vector>();
                        for (; lineidx < lines.Length; lineidx++)
                        {
                            line = lines[lineidx];
                            string valname;
                            int    validx;
                            Vector val;
                            HDebug.Verify(ReadVector(line, out valname, out validx, out val));
                            HDebug.Assert(valname == "x");
                            HDebug.Assert(validx == x.Count);
                            HDebug.Assert(val.Size == 3);
                            x.Add(val);
                        }
                        frame.x = x.ToArray();
                    }
                    return(frame);
                }