Esempio n. 1
0
        public static void GetPotential_SelfTest(string rootpath, string[] args)
        {
            if (GetPotential_SelfTest_do == false)
            {
                return;
            }

            GetPotential_SelfTest_do = false;
            Namd.Psf psf = Namd.Psf.FromFile(rootpath + @"\Sample\alanin.psf");
            Pdb      pdb = Pdb.FromFile(rootpath + @"\Sample\alanin.pdb");

            Namd.Prm prm = Namd.Prm.FromFileXPlor(rootpath + @"\Sample\alanin.params", new TextLogger());

            Universe univ = Universe.Build(psf, prm, pdb, false);

            List <ForceField.IForceField> frcflds = ForceField.GetMindyForceFields();

            Vector[]    forces  = univ.GetVectorsZero();
            MatrixByArr hessian = null;
            Dictionary <string, object> cache = new Dictionary <string, object>();
            double energy = univ.GetPotential(frcflds, ref forces, ref hessian, cache);
            double toler  = 0.000005;

            HDebug.AssertTolerance(toler, SelfTest_alanin_energy - energy);
            HDebug.AssertTolerance(toler, SelfTest_alanin_energy_bonds - (double)cache["energy_bonds     "]);
            HDebug.AssertTolerance(toler, SelfTest_alanin_energy_angles - (double)cache["energy_angles    "]);
            HDebug.AssertTolerance(toler, SelfTest_alanin_energy_dihedrals - (double)cache["energy_dihedrals "]);
            HDebug.AssertTolerance(toler, SelfTest_alanin_energy_impropers - (double)cache["energy_impropers "]);
            HDebug.AssertTolerance(toler, SelfTest_alanin_energy_nonbondeds - (double)cache["energy_nonbondeds"]);
            HDebug.AssertTolerance(toler, SelfTest_alanin_energy_unknowns - (double)cache["energy_customs   "]);
            HDebug.AssertTolerance(toler, SelfTest_alanin_forces.GetLength(0) - forces.Length);
            for (int i = 0; i < forces.Length; i++)
            {
                HDebug.Assert(forces[i].Size == 3);
                HDebug.AssertTolerance(toler, SelfTest_alanin_forces[i, 0] - forces[i][0]);
                HDebug.AssertTolerance(toler, SelfTest_alanin_forces[i, 1] - forces[i][1]);
                HDebug.AssertTolerance(toler, SelfTest_alanin_forces[i, 2] - forces[i][2]);
            }
        }
Esempio n. 2
0
        public static void SelfTest(string rootpath, string[] args)
        {
            //{
            //    Pdb pdb = Pdb.FromFile(rootpath + @"\Sample\1a6g.pdb");
            //    HTLib2.Bioinfo.Universe.Build(pdb);
            //}
            GetPotential_SelfTest(rootpath, args);

            Universe univ = null;

            string sample = null;

            //sample = "1a6g_autopsf.unfolded";
            //sample = "1a6g_autopsf";
            switch (sample)
            {
            case "alanin":
            {
                List <ForceField.IForceField> frcflds = ForceField.GetMindyForceFields();
                double k         = 0.0001;
                double threshold = 0.001;
                //int iter = univ.Minimize_ConjugateGradient_v0(frcflds, k, threshold);
                int    randomPurturb = 0;      // no random purturbation
                bool[] atomsMovable  = null;   // update all atoms
                int    iter_conjgrad;
                {
                    Namd.Psf psf = Namd.Psf.FromFile(rootpath + @"\Sample\alanin.psf");
                    Pdb      pdb = Pdb.FromFile(rootpath + @"\Sample\alanin.pdb");
                    Namd.Prm prm = Namd.Prm.FromFileXPlor(rootpath + @"\Sample\alanin.params", new TextLogger());
                    univ = Universe.Build(psf, prm, pdb, false);

                    //frcflds = new List<ForceField.IForceField>();
                    //frcflds.Add(new ForceField.MindyBond());
                    //frcflds.Add(new ForceField.MindyAngle());
                    //frcflds.Add(new ForceField.MindyDihedral());
                    //frcflds.Add(new ForceField.MindyImproper());
                    //frcflds.Add(new ForceField.MindyNonbondedLennardJones());
                    //frcflds.Add(new ForceField.MindyNonbondedElectrostatic());

                    //iter_conjgrad = univ.Minimize_ConjugateGradient_v1(0, frcflds, k, 0.1, null, threshold, randomPurturb, atomsMovable, new MinimizeLogger_PrintEnergyForceMag(), null, null);

                    //frcflds = new List<ForceField.IForceField>();
                    //frcflds.Add(new ForceField.MindyBond());
                    //frcflds.Add(new ForceField.MindyAngle());
                    //frcflds.Add(new ForceField.MindyImproper());
                    //frcflds.Add(new ForceField.MindyNonbondedLennardJones());
                    //frcflds.Add(new ForceField.MindyNonbondedElectrostatic());

                    //iter_conjgrad = univ.Minimize_ConjugateGradient_v1(0, frcflds, k*0.1, 0.1, null, threshold, randomPurturb, atomsMovable, new MinimizeLogger_PrintEnergyForceMag(), null, null);

                    frcflds = new List <ForceField.IForceField>();
                    if (HDebug.False)
                    {
                        frcflds.Add(new ForceField.MindyBond());
                    }
                    else
                    {
                        frcflds.Add(new ForceField.PwBond());
                    }
                    if (HDebug.False)
                    {
                        frcflds.Add(new ForceField.MindyAngle());
                    }
                    else
                    {
                        frcflds.Add(new ForceField.PwAngle());
                    }
                    frcflds.Add(new ForceField.MindyDihedral());
                    if (HDebug.False)
                    {
                        frcflds.Add(new ForceField.MindyImproper());
                    }
                    else
                    {
                        frcflds.Add(new ForceField.PwImproper());
                    }
                    if (HDebug.False)
                    {
                        frcflds.Add(new ForceField.MindyNonbondedElectrostatic());
                    }
                    else
                    {
                        frcflds.Add(new ForceField.PwElec());
                    }
                    if (HDebug.False)
                    {
                        frcflds.Add(new ForceField.MindyNonbondedLennardJones());
                    }
                    else
                    {
                        frcflds.Add(new ForceField.PwVdw());
                    }

                    univ.LoadCoords(@"D:\xxxx.coords");
                    iter_conjgrad = univ.Minimize_ConjugateGradient_v1(0, frcflds, k, 0.01, null, threshold, randomPurturb, atomsMovable, new MinimizeLogger_PrintEnergyForceMag(), null, null);
                    univ.SaveCoords(@"D:\xxxx.coords");
                    System.Console.WriteLine("=======================================================================");
                    System.Console.WriteLine("=======================================================================");
                    System.Console.WriteLine("=======================================================================");
                }
                {
                    //Psf psf = Psf.FromFile(rootpath + @"\Sample\alanin.psf");
                    //Pdb pdb = Pdb.FromFile(rootpath + @"\Sample\alanin.pdb");
                    //Prm prm = Prm.FromFileXPlor(rootpath + @"\Sample\alanin.params", new TextLogger());
                    //univ = Universe.Build(psf, prm, pdb);

                    //frcflds = new List<ForceField.IForceField>();
                    //if(true) frcflds.Add(new ForceField.MindyBond()); else frcflds.Add(new ForceField.PwBond());
                    //if(true) frcflds.Add(new ForceField.MindyAngle()); else frcflds.Add(new ForceField.PwAngle());
                    //frcflds.Add(new ForceField.MindyDihedral());
                    //if(true) frcflds.Add(new ForceField.MindyImproper()); else frcflds.Add(new ForceField.PwImproper());
                    //if(true) frcflds.Add(new ForceField.MindyNonbondedElectrostatic()); else frcflds.Add(new ForceField.PwElec());
                    //if(true) frcflds.Add(new ForceField.MindyNonbondedLennardJones()); else frcflds.Add(new ForceField.PwVdw());
                    univ.atoms[0].Coord += new Vector(0.1, 0.1, 0.1);
                    //iter_conjgrad = univ.Minimize_ConjugateGradient_v1(0, frcflds, k, 0.001, null, threshold, randomPurturb, atomsMovable, new MinimizeLogger_PrintEnergyForceMag(), null, null);
                    iter_conjgrad = univ.Minimize_ConjugateGradient_AtomwiseUpdate(frcflds,
                                                                                   threshold: threshold,
                                                                                   k: k,
                                                                                   max_atom_movement: 0.001,
                                                                                   max_iteration: null,
                                                                                   atomsMovable: atomsMovable,
                                                                                   logger: new MinimizeLogger_PrintEnergyForceMag()
                                                                                   );
                }
                k = 0.0005;
                HDebug.Assert(false);
                //int iter_stepdsnt = univ.Minimize_SteepestDescent(frcflds, k, threshold, System.Console.Error);
            }
            break;

            case "1a6g_autopsf":
            {
                Namd.Psf psf = Namd.Psf.FromFile(rootpath + @"\Sample\1a6g_autopsf.psf");
                Pdb      pdb = Pdb.FromFile(rootpath + @"\Sample\1a6g_autopsf.pdb");
                Namd.Prm prm = Namd.Prm.FromFile(rootpath + @"\Sample\1a6g_autopsf.prm", new TextLogger());

                univ = Universe.Build(psf, prm, pdb, false);

                List <ForceField.IForceField> frcflds = ForceField.GetMindyForceFields();
                double threshold    = 0.001;
                string minimize_ver = "v1";
                switch (minimize_ver)
                {
                //case "v2":
                //    {
                //        double atom_max_move = 0.03;
                //        int iter_conjgrad = univ.Minimize_ConjugateGradient_v2(frcflds, atom_max_move, threshold, System.Console.Error);
                //        break;
                //    }
                case "v1":
                {
                    double k             = 0.0001;
                    int    randomPurturb = 0;              // no random purturbation
                    bool[] atomsMovable  = null;           // update all atoms
                    int    iter_conjgrad = univ.Minimize_ConjugateGradient_v1(0, frcflds, k, 0.1, null, threshold, randomPurturb, atomsMovable, new MinimizeLogger_PrintEnergyForceMag(), null, null);
                    break;
                }

                default:
                    goto case "v1";
                }
                //atom_max_move = 0.03;
                //int iter_stepdsnt = univ.Minimize_SteepestDescent(frcflds, atom_max_move, threshold, System.Console.Error);
            }
            break;

            case "1a6g_autopsf.unfolded":
            {
                Namd.Psf psf = Namd.Psf.FromFile(rootpath + @"\Sample\1a6g_autopsf.psf");
                Pdb      pdb = Pdb.FromFile(rootpath + @"\Sample\1a6g_autopsf.pdb");
                {
                    Random        rand   = new Random(1);
                    List <Vector> coords = pdb.atoms.ListCoord();

                    for (int i = 0; i < coords.Count; i++)
                    {
                        double x = i * 0.1 + rand.NextDouble() * 0.01;
                        double y = i * 0.1 + rand.NextDouble() * 0.01;
                        double z = i * 0.1 + rand.NextDouble() * 0.01;
                        coords[i] = new Vector(x, y, z);
                    }
                    pdb.ToFile(rootpath + @"\Sample\1a6g_autopsf.unfolded.pdb", coords);
                    pdb = Pdb.FromFile(rootpath + @"\Sample\1a6g_autopsf.unfolded.pdb");
                }
                Namd.Prm prm = Namd.Prm.FromFile(rootpath + @"\Sample\1a6g_autopsf.prm", new TextLogger());

                univ = Universe.Build(psf, prm, pdb, false);

                List <ForceField.IForceField> frcflds = ForceField.GetMindyForceFields();
                double threshold    = 0.01;
                string minimize_ver = "v1";
                switch (minimize_ver)
                {
                //case "v2":
                //    {
                //        double atom_max_move = 0.1;
                //        int iter_conjgrad = univ.Minimize_ConjugateGradient_v2(frcflds, atom_max_move, threshold, System.Console.Error);
                //        break;
                //    }
                case "v1":
                {
                    double k             = 0.0001;
                    int    randomPurturb = 100;
                    bool[] atomsMovable  = null;                // updates all atoms
                    int    iter_conjgrad = univ.Minimize_ConjugateGradient_v1(0, frcflds, k, 0.1, null, threshold, randomPurturb, atomsMovable, new MinimizeLogger_PrintEnergyForceMag(), null, null);
                    break;
                }

                default:
                    goto case "v1";
                }
                //atom_max_move = 0.03;
                //int iter_stepdsnt = univ.Minimize_SteepestDescent(frcflds, atom_max_move, threshold, System.Console.Error);
            }
            break;

            default:
                goto case "alanin";
            }
        }
Esempio n. 3
0
 public static Tuple <Xyz, Prm> FromNamd(Pdb pdb, Namd.Psf psf, Namd.Prm prm)
 {
     return(BuilderFromNamd.BuildFromNamd(pdb, psf, prm));
 }
Esempio n. 4
0
            public static Tuple <Xyz, Prm> BuildFromNamd(Pdb pdb, Namd.Psf psf, Namd.Prm prm)
            {
                var univ = Universe.BuilderNamd.Build(psf, prm, pdb, null, null);

                /// Atom(Id,Class,Type,Desc,Mass)
                /// * Id   : Vdw(Id,Rmin2,Epsilon)
                ///          Charge(Id,pch)
                ///          Biotype(BioId,Name,Resi,Id)
                /// * Class: Vdw14(Class,Rmin2_14,Eps_14)
                ///          Bond(Class1,Class2,Kb,b0)
                ///          Angle(Class1,Class2,Class3,Ktheta,Theta0)
                ///          Ureybrad(Class1,Class2,Class3,Kub,S0)
                ///          Improper(Class1,Class2,Class3,Class4,Kpsi,psi0)
                ///          Torsion(Class1,Class2,Class3,Class4,Kchi0,delta0,n0,Kchi1,delta1,n1,Kchi2,delta2,n2)
                ///   Type :
                Dictionary <Tuple <string, string, double, double, double, int>, int> key_id = new Dictionary <Tuple <string, string, double, double, double, int>, int>();
                Dictionary <string, int> type_cls = new Dictionary <string, int>();

                Dictionary <int, Prm.Atom>    id_atom    = new Dictionary <int, Prm.Atom>();
                Dictionary <int, Prm.Vdw>     cls_vdw    = new Dictionary <int, Prm.Vdw>();
                Dictionary <int, Prm.Charge>  id_charge  = new Dictionary <int, Prm.Charge>();
                Dictionary <int, Prm.Biotype> id_biotype = new Dictionary <int, Prm.Biotype>();
                Dictionary <int, Prm.Vdw14>   cls_vdw14  = new Dictionary <int, Prm.Vdw14>();

                Dictionary <int, Tuple <string, Vector, int, List <int> > > xyzid_info = new Dictionary <int, Tuple <string, Vector, int, List <int> > >();

                ///                      1                     NH3
                /// ------------------------------------------------------------------------------------------------------------------
                ///      pdb: "ATOM      1  N   GLY A   2      24.776  -0.687  28.652  1.00  0.00      A    N"
                /// namd-psf:    "       1 A    2    GLY  N    NH3   -0.300000       14.0070           0"
                /// namd-prm:                                 "NH3    0.000000  -0.200000     1.850000 ! ALLOW   POL"
                /// tink-xyz:                         "     1  NH3   24.776000   -0.687000   28.652000    65     2     5     6     7"
                /// tink-prm:         "atom         65   26    NH3   "Ammonium Nitrogen"            7    14.007    4"
                foreach (var uatom in univ.atoms)
                {
                    HDebug.Assert(uatom.sources.Length == 3);
                    Pdb.Atom           pdbatom = uatom.sources.HFirstByType(null as Pdb.Atom); HDebug.Assert(pdbatom != null);
                    Namd.Psf.Atom      psfatom = uatom.sources.HFirstByType(null as Namd.Psf.Atom); HDebug.Assert(psfatom != null);
                    Namd.Prm.Nonbonded prmnbnd = uatom.sources.HFirstByType(null as Namd.Prm.Nonbonded); HDebug.Assert(prmnbnd != null);
                    string             name    = psfatom.AtomName.Trim();
                    string             resn    = psfatom.ResidueName.Trim();
                    string             type    = psfatom.AtomType.Trim();
                    double             rmin2   = prmnbnd.Rmin2;
                    double             eps     = prmnbnd.epsilon;
                    double             chrg    = psfatom.Charge;
                    int    valnc = uatom.Bonds.Count;
                    string desc  = string.Format("{0}({1})-{2}", name, type, resn);

                    var key = UnivAtomToTinkKey(uatom);
                    if (key_id.ContainsKey(key) == false)
                    {
                        key_id.Add(key, key_id.Count + 1);
                    }

                    if (type_cls.ContainsKey(type) == false)
                    {
                        type_cls.Add(type, type_cls.Count + 1);
                    }

                    int    tink_id   = key_id[key];
                    int    tink_cls  = type_cls[type];
                    string tink_type = type;

                    if (id_atom.ContainsKey(tink_id) == false)
                    {
                        Prm.Atom tink_atom = Prm.Atom.FromData
                                                 (Id: tink_id
                                                 , Class: tink_cls
                                                 , Type: tink_type
                                                 , Description: desc
                                                 , AtomicNumber: null
                                                 , Mass: psfatom.Mass
                                                 , Valence: valnc
                                                 );
                        id_atom.Add(tink_id, tink_atom);
                    }
                    else
                    {
                        Prm.Atom tink_atom = id_atom[tink_id];
                        tink_id  = tink_atom.Id;
                        tink_cls = tink_atom.Class;
                        HDebug.Exception(tink_atom.Type == tink_type);
                        HDebug.Exception(tink_atom.Description == desc);
                        HDebug.Exception(Math.Abs(tink_atom.Mass - psfatom.Mass) < 0.001);
                        HDebug.Exception(tink_atom.Valence == valnc);
                    }

                    if (cls_vdw.ContainsKey(tink_cls) == false)
                    {
                        Prm.Vdw tink_vdw = Prm.Vdw.FromData
                                               (Class: tink_cls
                                               , Rmin2: prmnbnd.Rmin2
                                               , Epsilon: prmnbnd.epsilon
                                               );
                        cls_vdw.Add(tink_cls, tink_vdw);
                    }
                    else
                    {
                        Prm.Vdw tink_vdw = cls_vdw[tink_cls];
                        HDebug.Exception(tink_vdw.Rmin2 == prmnbnd.Rmin2);
                        HDebug.Exception(tink_vdw.Epsilon == prmnbnd.epsilon);
                    }

                    HDebug.AssertIf(double.IsNaN(prmnbnd.Rmin2_14) == true, double.IsNaN(prmnbnd.eps_14) == true);
                    HDebug.AssertIf(double.IsNaN(prmnbnd.Rmin2_14) == false, double.IsNaN(prmnbnd.eps_14) == false);
                    if (double.IsNaN(prmnbnd.Rmin2_14) == false && double.IsNaN(prmnbnd.eps_14) == false)
                    {
                        if (cls_vdw14.ContainsKey(tink_cls) == false)
                        {
                            Prm.Vdw14 tink_vdw14 = Prm.Vdw14.FromData
                                                       (Class: tink_cls
                                                       , Rmin2_14: prmnbnd.Rmin2_14
                                                       , Eps_14: prmnbnd.eps_14
                                                       );
                            cls_vdw14.Add(tink_cls, tink_vdw14);
                        }
                        else
                        {
                            Prm.Vdw14 tink_vdw14 = cls_vdw14[tink_cls];
                            HDebug.Exception(tink_vdw14.Rmin2_14 == prmnbnd.Rmin2_14);
                            HDebug.Exception(tink_vdw14.Eps_14 == prmnbnd.eps_14);
                        }
                    }
                    else
                    {
                        HDebug.Exception(cls_vdw14.ContainsKey(tink_cls) == false);
                    }

                    if (id_charge.ContainsKey(tink_id) == false)
                    {
                        Prm.Charge tink_charge = Prm.Charge.FromData
                                                     (Id: tink_id
                                                     , pch: psfatom.Charge
                                                     );
                        id_charge.Add(tink_id, tink_charge);
                    }
                    else
                    {
                        Prm.Charge tink_charge = id_charge[tink_id];
                        HDebug.Exception(tink_charge.pch == psfatom.Charge);
                    }

                    if (id_biotype.ContainsKey(tink_id) == false)
                    {
                        Prm.Biotype tink_biotype = Prm.Biotype.FromData
                                                       (BioId: id_biotype.Count + 1
                                                       , Name: name
                                                       , Resn: string.Format("{0}({1})-{2}", name, type, resn)
                                                       , Id: tink_id
                                                       );
                        id_biotype.Add(tink_id, tink_biotype);
                    }
                    else
                    {
                        Prm.Biotype tink_biotype = id_biotype[tink_id];
                        HDebug.Exception(tink_biotype.Name == name);
                        HDebug.Exception(tink_biotype.Resn == string.Format("{0}({1})-{2}", name, type, resn));
                        HDebug.Exception(tink_biotype.Id == tink_id);
                    }

                    var xyzid   = uatom.AtomId;
                    var xyzinfo = new Tuple <string, Vector, int, List <int> >
                                      (tink_type    // AtomType
                                      , uatom.Coord // X, Y, Z
                                      , tink_id     // AtomId
                                      , new List <int>()
                                      );
                    xyzid_info.Add(xyzid, xyzinfo);
                }

                Dictionary <Tuple <int, int>, Prm.Bond> cls_bond = new Dictionary <Tuple <int, int>, Prm.Bond>();

                foreach (var ubond in univ.bonds)
                {
                    var key0 = UnivAtomToTinkKey(ubond.atoms[0]); string type0 = key0.Item2; int cls0 = type_cls[type0];
                    var key1 = UnivAtomToTinkKey(ubond.atoms[1]); string type1 = key1.Item2; int cls1 = type_cls[type1];

                    Tuple <int, int> cls01;
                    if (cls0 < cls1)
                    {
                        cls01 = new Tuple <int, int>(cls0, cls1);
                    }
                    else
                    {
                        cls01 = new Tuple <int, int>(cls1, cls0);
                    }

                    if (cls_bond.ContainsKey(cls01) == false)
                    {
                        Prm.Bond tink_bond01 = Prm.Bond.FromData
                                                   (Class1: cls01.Item1
                                                   , Class2: cls01.Item2
                                                   , Kb: ubond.Kb
                                                   , b0: ubond.b0
                                                   );
                        cls_bond.Add(cls01, tink_bond01);
                    }
                    else
                    {
                        Prm.Bond tink_bond01 = cls_bond[cls01];
                        HDebug.Exception(Math.Abs(tink_bond01.Kb - ubond.Kb) < 0.01);
                        HDebug.Exception(tink_bond01.b0 == ubond.b0);
                    }

                    int xyzid0 = ubond.atoms[0].AtomId;
                    int xyzid1 = ubond.atoms[1].AtomId;
                    xyzid_info[xyzid0].Item4.Add(xyzid1);
                    xyzid_info[xyzid1].Item4.Add(xyzid0);
                }

                Dictionary <Tuple <int, int, int>, Prm.Angle>    cls_angle    = new Dictionary <Tuple <int, int, int>, Prm.Angle>();
                Dictionary <Tuple <int, int, int>, Prm.Ureybrad> cls_ureybrad = new Dictionary <Tuple <int, int, int>, Prm.Ureybrad>();

                foreach (var uangle in univ.angles)
                {
                    var key0 = UnivAtomToTinkKey(uangle.atoms[0]); string type0 = key0.Item2; int cls0 = type_cls[type0];
                    var key1 = UnivAtomToTinkKey(uangle.atoms[1]); string type1 = key1.Item2; int cls1 = type_cls[type1];
                    var key2 = UnivAtomToTinkKey(uangle.atoms[2]); string type2 = key2.Item2; int cls2 = type_cls[type2];

                    Tuple <int, int, int> cls012;
                    if (cls0 < cls2)
                    {
                        cls012 = new Tuple <int, int, int>(cls0, cls1, cls2);
                    }
                    else
                    {
                        cls012 = new Tuple <int, int, int>(cls2, cls1, cls0);
                    }

                    double uangle_Theta0 = 180.0 * uangle.Theta0 / Math.PI;

                    if (cls_angle.ContainsKey(cls012) == false)
                    {
                        Prm.Angle tink_angle012 = Prm.Angle.FromData
                                                      (Class1: cls012.Item1
                                                      , Class2: cls012.Item2
                                                      , Class3: cls012.Item3
                                                      , Ktheta: uangle.Ktheta
                                                      , Theta0: uangle_Theta0
                                                      );
                        cls_angle.Add(cls012, tink_angle012);

                        HDebug.Exception(cls_ureybrad.ContainsKey(cls012) == false);
                        if (uangle.Kub != 0)
                        {
                            Prm.Ureybrad tink_ureybrad = Prm.Ureybrad.FromData
                                                             (Class1: cls012.Item1
                                                             , Class2: cls012.Item2
                                                             , Class3: cls012.Item3
                                                             , Kub: uangle.Kub
                                                             , S0: uangle.S0
                                                             );
                            cls_ureybrad.Add(cls012, tink_ureybrad);
                        }
                    }
                    else
                    {
                        Prm.Angle tink_angle012 = cls_angle[cls012];
                        HDebug.Exception(tink_angle012.Ktheta == uangle.Ktheta);
                        HDebug.Exception(Math.Abs(tink_angle012.Theta0 - uangle_Theta0) < 0.01);

                        if (uangle.Kub != 0)
                        {
                            Prm.Ureybrad tink_ureybrad = cls_ureybrad[cls012];
                            HDebug.Exception(tink_ureybrad.Kub == uangle.Kub);
                            HDebug.Exception(tink_ureybrad.S0 == uangle.S0);
                        }
                        else
                        {
                            HDebug.Exception(cls_ureybrad.ContainsKey(cls012) == false);
                        }
                    }
                }

                Dictionary <Tuple <int, int, int, int>, Prm.Improper> cls_improper = new Dictionary <Tuple <int, int, int, int>, Prm.Improper>();

                foreach (var improper in univ.impropers)
                {
                    var key0 = UnivAtomToTinkKey(improper.atoms[0]); string type0 = key0.Item2; int cls0 = type_cls[type0];
                    var key1 = UnivAtomToTinkKey(improper.atoms[1]); string type1 = key1.Item2; int cls1 = type_cls[type1];
                    var key2 = UnivAtomToTinkKey(improper.atoms[2]); string type2 = key2.Item2; int cls2 = type_cls[type2];
                    var key3 = UnivAtomToTinkKey(improper.atoms[3]); string type3 = key3.Item2; int cls3 = type_cls[type3];

                    Tuple <int, int, int, int> cls0123 = new Tuple <int, int, int, int>(cls0, cls1, cls2, cls3);
                    //if(cls0 < cls2) cls0123 = new Tuple<int, int, int, int>(cls0, cls1, cls2, cls3);
                    //else            cls0123 = new Tuple<int, int, int, int>(cls3, cls2, cls1, cls0);

                    double improper_psi0 = 180.0 * improper.psi0 / Math.PI;

                    if (cls_improper.ContainsKey(cls0123) == false)
                    {
                        Prm.Improper tink_improper = Prm.Improper.FromData
                                                         (Class1: cls0
                                                         , Class2: cls1
                                                         , Class3: cls2
                                                         , Class4: cls3
                                                         , Kpsi: improper.Kpsi
                                                         , psi0: improper_psi0
                                                         );
                        cls_improper.Add(cls0123, tink_improper);
                    }
                    else
                    {
                        Prm.Improper tink_improper = cls_improper[cls0123];
                        HDebug.Exception(tink_improper.Kpsi == improper.Kpsi);
                        HDebug.Exception(Math.Abs(tink_improper.psi0 - improper_psi0) < 0.01);
                    }
                }

                Dictionary <Tuple <int, int, int, int>, List <Universe.Dihedral> > cls_dihedrals = new Dictionary <Tuple <int, int, int, int>, List <Universe.Dihedral> >();

                foreach (var atom in univ.atoms)
                {
                    var inter1234s = atom.ListInterAtom1234();
                    foreach (var inter1234 in inter1234s)
                    {
                        HDebug.Assert(inter1234.Count == 4);
                        var key0 = UnivAtomToTinkKey(inter1234[0]); string type0 = key0.Item2; int cls0 = type_cls[type0];
                        var key1 = UnivAtomToTinkKey(inter1234[1]); string type1 = key1.Item2; int cls1 = type_cls[type1];
                        var key2 = UnivAtomToTinkKey(inter1234[2]); string type2 = key2.Item2; int cls2 = type_cls[type2];
                        var key3 = UnivAtomToTinkKey(inter1234[3]); string type3 = key3.Item2; int cls3 = type_cls[type3];

                        Tuple <int, int, int, int> cls0123 = new Tuple <int, int, int, int>(cls0, cls1, cls2, cls3);
                        Tuple <int, int, int, int> cls3210 = new Tuple <int, int, int, int>(cls3, cls2, cls1, cls0);

                        if (cls_dihedrals.ContainsKey(cls0123) == false)
                        {
                            cls_dihedrals.Add(cls0123, new List <Universe.Dihedral>());
                        }
                        if (cls_dihedrals.ContainsKey(cls3210) == false)
                        {
                            cls_dihedrals.Add(cls3210, new List <Universe.Dihedral>());
                        }
                    }
                }
                foreach (var dihedral in univ.dihedrals)
                {
                    var key0 = UnivAtomToTinkKey(dihedral.atoms[0]); string type0 = key0.Item2; int cls0 = type_cls[type0];
                    var key1 = UnivAtomToTinkKey(dihedral.atoms[1]); string type1 = key1.Item2; int cls1 = type_cls[type1];
                    var key2 = UnivAtomToTinkKey(dihedral.atoms[2]); string type2 = key2.Item2; int cls2 = type_cls[type2];
                    var key3 = UnivAtomToTinkKey(dihedral.atoms[3]); string type3 = key3.Item2; int cls3 = type_cls[type3];

                    Tuple <int, int, int, int> cls0123 = new Tuple <int, int, int, int>(cls0, cls1, cls2, cls3);
                    Tuple <int, int, int, int> cls3210 = new Tuple <int, int, int, int>(cls3, cls2, cls1, cls0);

                    cls_dihedrals[cls0123].Add(dihedral);
                    cls_dihedrals[cls3210].Add(dihedral);
                }
                Dictionary <Tuple <int, int, int, int>, Prm.Torsion> cls_torsion = new Dictionary <Tuple <int, int, int, int>, Prm.Torsion>();

                foreach (var cls0123 in cls_dihedrals.Keys)
                {
                    Universe.Dihedral[] dihedrals = cls_dihedrals[cls0123].ToArray();

                    Dictionary <Tuple <double, double>, List <double> > delta_n_Kchis = new Dictionary <Tuple <double, double>, List <double> >();
                    foreach (var dihedral in dihedrals)
                    {
                        double dihedral_delta = 180.0 * dihedral.delta / Math.PI;
                        var    delta_n        = new Tuple <double, double>(dihedral_delta, dihedral.n);
                        if (delta_n_Kchis.ContainsKey(delta_n) == false)
                        {
                            delta_n_Kchis.Add(delta_n, new List <double>());
                        }
                        delta_n_Kchis[delta_n].Add(dihedral.Kchi);
                    }

                    Tuple <double, double>[] lst_delta_n = delta_n_Kchis.Keys.ToArray();
                    HDebug.Exception(lst_delta_n.Length <= 3);

                    double?Kchi0 = null; if (lst_delta_n.Length >= 1)
                    {
                        Kchi0 = delta_n_Kchis[lst_delta_n[0]].Mean();
                    }
                    double?delta0 = null; if (lst_delta_n.Length >= 1)
                    {
                        delta0 = lst_delta_n[0].Item1;
                    }
                    double?n0 = null; if (lst_delta_n.Length >= 1)
                    {
                        n0 = lst_delta_n[0].Item2;
                    }
                    double?Kchi1 = null; if (lst_delta_n.Length >= 2)
                    {
                        Kchi1 = delta_n_Kchis[lst_delta_n[1]].Mean();
                    }
                    double?delta1 = null; if (lst_delta_n.Length >= 2)
                    {
                        delta1 = lst_delta_n[1].Item1;
                    }
                    double?n1 = null; if (lst_delta_n.Length >= 2)
                    {
                        n1 = lst_delta_n[1].Item2;
                    }
                    double?Kchi2 = null; if (lst_delta_n.Length >= 3)
                    {
                        Kchi2 = delta_n_Kchis[lst_delta_n[2]].Mean();
                    }
                    double?delta2 = null; if (lst_delta_n.Length >= 3)
                    {
                        delta2 = lst_delta_n[2].Item1;
                    }
                    double?n2 = null; if (lst_delta_n.Length >= 3)
                    {
                        n2 = lst_delta_n[2].Item2;
                    }

                    Prm.Torsion tink_torsion = Prm.Torsion.FromData
                                                   (Class1: cls0123.Item1
                                                   , Class2: cls0123.Item2
                                                   , Class3: cls0123.Item3
                                                   , Class4: cls0123.Item4
                                                   , Kchi0: Kchi0
                                                   , delta0: delta0
                                                   , n0: n0
                                                   , Kchi1: Kchi1
                                                   , delta1: delta1
                                                   , n1: n1
                                                   , Kchi2: Kchi2
                                                   , delta2: delta2
                                                   , n2: n2
                                                   );
                    cls_torsion.Add(cls0123, tink_torsion);
                }

                List <string> prmlines;

                #region build lines
                {
                    List <string> lines = new List <string>();
                    lines.Add("");
                    lines.Add("      ##############################");
                    lines.Add("      ##                          ##");
                    lines.Add("      ##  Force Field Definition  ##");
                    lines.Add("      ##                          ##");
                    lines.Add("      ##############################");
                    lines.Add("");
                    lines.Add("");
                    lines.Add("forcefield              CHARMM22");
                    lines.Add("");
                    lines.Add("vdwtype                 LENNARD-JONES");
                    lines.Add("radiusrule              ARITHMETIC");
                    lines.Add("radiustype              R-MIN");
                    lines.Add("radiussize              RADIUS");
                    lines.Add("epsilonrule             GEOMETRIC");
                    lines.Add("vdw-14-scale            1.0");
                    lines.Add("chg-14-scale            1.0");
                    lines.Add("electric                332.0716");
                    lines.Add("dielectric              1.0");
                    lines.Add("");
                    lines.Add("");
                    lines.Add("      #############################");
                    lines.Add("      ##                         ##");
                    lines.Add("      ##  Atom Type Definitions  ##");
                    lines.Add("      ##                         ##");
                    lines.Add("      #############################");
                    lines.Add("");
                    lines.Add("");
                    foreach (int id in id_atom.Keys.ToArray().HSort())
                    {
                        lines.Add(id_atom[id].line);
                    }
                    lines.Add("");
                    lines.Add("");
                    lines.Add("      ################################");
                    lines.Add("      ##                            ##");
                    lines.Add("      ##  Van der Waals Parameters  ##");
                    lines.Add("      ##                            ##");
                    lines.Add("      ################################");
                    lines.Add("");
                    lines.Add("");
                    foreach (int cls in cls_vdw.Keys.ToArray().HSort())
                    {
                        lines.Add(cls_vdw[cls].line);
                    }
                    lines.Add("");
                    lines.Add("");
                    lines.Add("      ####################################");
                    lines.Add("      ##                                ##");
                    lines.Add("      ##  1-4 Van der Waals Parameters  ##");
                    lines.Add("      ##                                ##");
                    lines.Add("      ####################################");
                    lines.Add("");
                    lines.Add("");
                    foreach (int cls in cls_vdw14.Keys.ToArray().HSort())
                    {
                        lines.Add(cls_vdw14[cls].line);
                    }
                    lines.Add("");
                    lines.Add("");
                    lines.Add("      ##################################");
                    lines.Add("      ##                              ##");
                    lines.Add("      ##  Bond Stretching Parameters  ##");
                    lines.Add("      ##                              ##");
                    lines.Add("      ##################################");
                    lines.Add("");
                    lines.Add("");
                    foreach (var bond in cls_bond)
                    {
                        lines.Add(bond.Value.line);
                    }
                    lines.Add("");
                    lines.Add("");
                    lines.Add("      ################################");
                    lines.Add("      ##                            ##");
                    lines.Add("      ##  Angle Bending Parameters  ##");
                    lines.Add("      ##                            ##");
                    lines.Add("      ################################");
                    lines.Add("");
                    lines.Add("");
                    foreach (var angle in cls_angle)
                    {
                        lines.Add(angle.Value.line);
                    }
                    lines.Add("");
                    lines.Add("");
                    lines.Add("      ###############################");
                    lines.Add("      ##                           ##");
                    lines.Add("      ##  Urey-Bradley Parameters  ##");
                    lines.Add("      ##                           ##");
                    lines.Add("      ###############################");
                    lines.Add("");
                    lines.Add("");
                    foreach (var ureybrad in cls_ureybrad)
                    {
                        lines.Add(ureybrad.Value.line);
                    }
                    lines.Add("");
                    lines.Add("");
                    lines.Add("      ####################################");
                    lines.Add("      ##                                ##");
                    lines.Add("      ##  Improper Dihedral Parameters  ##");
                    lines.Add("      ##                                ##");
                    lines.Add("      ####################################");
                    lines.Add("");
                    lines.Add("");
                    foreach (var improper in cls_improper)
                    {
                        lines.Add(improper.Value.line);
                    }
                    lines.Add("");
                    lines.Add("");
                    lines.Add("      ############################");
                    lines.Add("      ##                        ##");
                    lines.Add("      ##  Torsional Parameters  ##");
                    lines.Add("      ##                        ##");
                    lines.Add("      ############################");
                    lines.Add("");
                    lines.Add("");
                    foreach (var torsion in cls_torsion)
                    {
                        lines.Add(torsion.Value.line);
                    }
                    lines.Add("");
                    lines.Add("");
                    lines.Add("      ########################################");
                    lines.Add("      ##                                    ##");
                    lines.Add("      ##  Atomic Partial Charge Parameters  ##");
                    lines.Add("      ##                                    ##");
                    lines.Add("      ########################################");
                    lines.Add("");
                    lines.Add("");
                    foreach (int id in id_charge.Keys.ToArray().HSort())
                    {
                        lines.Add(id_charge[id].line);
                    }
                    lines.Add("");
                    lines.Add("");
                    lines.Add("      ########################################");
                    lines.Add("      ##                                    ##");
                    lines.Add("      ##  Biopolymer Atom Type Conversions  ##");
                    lines.Add("      ##                                    ##");
                    lines.Add("      ########################################");
                    lines.Add("");
                    lines.Add("");
                    foreach (int id in id_biotype.Keys.ToArray().HSort())
                    {
                        lines.Add(id_biotype[id].line);
                    }
                    lines.Add("");
                    prmlines = lines;
                }
                #endregion

                Dictionary <string, string> atomtype_natomtype;
                {
                    atomtype_natomtype = xyzid_info.Values.ToArray().HListItem1().HToHashSet().HToDictionaryAsKey("");
                    foreach (string atomtype in atomtype_natomtype.Keys.ToArray())
                    {
                        string natomtype = atomtype;
                        if (natomtype.Length > 3)
                        {
                            Dictionary <char, char> map = new Dictionary <char, char>
                            {
                                { '0', '1' }, { '1', '2' }, { '2', '3' }, { '3', '4' }, { '4', '5' },
                                { '5', '6' }, { '6', '7' }, { '7', '8' }, { '8', '9' }, { '9', 'A' },
                                { 'A', 'B' }, { 'B', 'C' }, { 'C', 'D' }, { 'D', 'E' }, { 'E', 'F' },
                                { 'F', 'G' }, { 'G', 'H' }, { 'H', 'I' }, { 'I', 'J' }, { 'J', 'K' },
                                { 'K', 'L' }, { 'L', 'M' }, { 'M', 'N' }, { 'N', 'O' }, { 'O', 'P' },
                                { 'P', 'Q' }, { 'Q', 'R' }, { 'R', 'S' }, { 'S', 'T' }, { 'T', 'U' },
                                { 'U', 'V' }, { 'V', 'W' }, { 'W', 'X' }, { 'X', 'Y' }, { 'Y', 'Z' },
                                { 'Z', '0' },
                            };
                            string temptype = atomtype.Substring(0, 3);
                            foreach (var key in map.Keys.ToArray())
                            {
                                if (map[key] == temptype[2])
                                {
                                    map[key] = ' ';     // marking ending point as ' '
                                }
                            }
                            while (true)
                            {
                                if (temptype[2] == ' ')
                                {
                                    HDebug.Exception();
                                }

                                if ((atomtype_natomtype.ContainsKey(temptype) == false) && (atomtype_natomtype.ContainsValue(temptype) == false))
                                {
                                    natomtype = temptype;
                                    break;
                                }

                                temptype = temptype.Substring(0, 2) + map[temptype[2]];
                            }
                        }
                        atomtype_natomtype[atomtype] = natomtype;
                    }
                }
                List <string> xyzlines;
                {
                    xyzlines = new List <string>();
                    xyzlines.Add(Xyz.Header.FromData(xyzid_info.Count, "", "", "").line);
                    foreach (var xyzid in xyzid_info.Keys.ToArray().HSort())
                    {
                        int    id       = xyzid;
                        string atomtype = xyzid_info[xyzid].Item1;
                        atomtype = atomtype_natomtype[atomtype];
                        double x         = xyzid_info[xyzid].Item2[0];
                        double y         = xyzid_info[xyzid].Item2[1];
                        double z         = xyzid_info[xyzid].Item2[2];
                        int    atomid    = xyzid_info[xyzid].Item3;
                        int[]  bondedids = xyzid_info[xyzid].Item4.ToArray();
                        xyzlines.Add(Xyz.Atom.FromData(id, atomtype, x, y, z, atomid, bondedids).line);
                    }
                }

                {
                    for (int i = 0; i < prmlines.Count; i++)
                    {
                        if (prmlines[i].StartsWith("atom "))
                        {
                            var atom  = Prm.Atom.FromLine(prmlines[i]);
                            var natom = Prm.Atom.FromData
                                            (Id: atom.Id
                                            , Class: atom.Class
                                            , Type: atomtype_natomtype[atom.Type]
                                            , Description: atom.Description
                                            , AtomicNumber: atom.AtomicNumber
                                            , Mass: atom.Mass
                                            , Valence: atom.Valence
                                            );
                            prmlines[i] = natom.line;
                        }
                    }
                }

                Prm tink_prm = Prm.FromLines(prmlines);
                Xyz tink_xyz = Xyz.FromLines(xyzlines);
                return(new Tuple <Xyz, Prm>(tink_xyz, tink_prm));
            }
Esempio n. 5
0
 public static Universe Build(Namd.Psf psf, Namd.Prm prm, Pdb pdb, bool?ignore_neg_occupancy, ITextLogger logger)
 {
     return(BuilderNamd.Build(psf, prm, pdb, ignore_neg_occupancy, logger));
 }
Esempio n. 6
0
            public static Universe Build(Namd.Psf psf, Namd.Prm prm, Pdb pdb, bool?ignore_neg_occupancy, ITextLogger logger)
            {
                Universe univ = new Universe();

                // atoms
                Atoms atoms = new Atoms(univ);
                Dictionary <int, Atom> id_atom = new Dictionary <int, Atom>();

                Pdb.Atom[] pdb_atoms = pdb.atoms;
                HDebug.Assert(psf.atoms.Length == pdb_atoms.Length);
                for (int i = 0; i < psf.atoms.Length; i++)
                {
                    HDebug.Assert(pdb_atoms[i].try_serial == null || psf.atoms[i].AtomId == pdb_atoms[i].serial); // when num atoms in pdb is too large (>99999), it is marked as *****
                    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;
                    {
                        if (ignore_neg_occupancy == null)
                        {
                            if (pdb_atoms[i].occupancy < 0)
                            {
                                throw new HException("unhandled negative occupancy during building universe");
                            }
                        }
                        else
                        {
                            if (ignore_neg_occupancy.Value && pdb_atoms[i].occupancy < 0)
                            {
                                continue;
                            }
                        }
                    }
                    atoms.Add(atom);
                    id_atom.Add(psf.atoms[i].AtomId, atom);
                }

                // bonds
                Bonds bonds = new Bonds();

                for (int i = 0; i < psf.bonds.GetLength(0); i++)
                {
                    int id0 = psf.bonds[i, 0]; if (id_atom.ContainsKey(id0) == false)
                    {
                        continue;
                    }
                    Atom atom0 = id_atom[id0]; string type0 = atom0.AtomType;
                    int  id1 = psf.bonds[i, 1]; if (id_atom.ContainsKey(id1) == false)
                    {
                        continue;
                    }
                    Atom atom1 = id_atom[id1]; string type1 = atom1.AtomType;
                    Bond bond = new Bond(atom0, atom1, prm.FindBond(type0, type1, logger));
                    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 < psf.angles.GetLength(0); i++)
                {
                    int id0 = psf.angles[i, 0]; if (id_atom.ContainsKey(id0) == false)
                    {
                        continue;
                    }
                    Atom atom0 = id_atom[id0]; string type0 = atom0.AtomType;
                    int  id1 = psf.angles[i, 1]; if (id_atom.ContainsKey(id1) == false)
                    {
                        continue;
                    }
                    Atom atom1 = id_atom[id1]; string type1 = atom1.AtomType;
                    int  id2 = psf.angles[i, 2]; if (id_atom.ContainsKey(id2) == false)
                    {
                        continue;
                    }
                    Atom atom2 = id_atom[id2]; string type2 = atom2.AtomType;
                    var  prm_angle = prm.FindAngle(type0, type1, type2, logger);
                    if (prm_angle == null)
                    {
                        HDebug.Assert(false);
                        logger.Log(string.Format
                                       ("try to add non-existing angle (({0}, prm {1})-({2}, prm {3})-({4}, prm {5})) in prm"
                                       , atom0, type0, atom1, type1, atom2, type2
                                       ));
                        continue;
                    }
                    Angle angle = new Angle(atom0, atom1, atom2, prm_angle);
                    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();

                for (int i = 0; i < psf.dihedrals.GetLength(0); i++)
                {
                    int id0 = psf.dihedrals[i, 0]; if (id_atom.ContainsKey(id0) == false)
                    {
                        continue;
                    }
                    Atom atom0 = id_atom[id0]; string type0 = atom0.AtomType;
                    int  id1 = psf.dihedrals[i, 1]; if (id_atom.ContainsKey(id1) == false)
                    {
                        continue;
                    }
                    Atom atom1 = id_atom[id1]; string type1 = atom1.AtomType;
                    int  id2 = psf.dihedrals[i, 2]; if (id_atom.ContainsKey(id2) == false)
                    {
                        continue;
                    }
                    Atom atom2 = id_atom[id2]; string type2 = atom2.AtomType;
                    int  id3 = psf.dihedrals[i, 3]; if (id_atom.ContainsKey(id3) == false)
                    {
                        continue;
                    }
                    Atom atom3 = id_atom[id3]; string type3 = atom3.AtomType;
                    var  prm_dihedrals = prm.FindDihedral(type0, type1, type2, type3, logger);
                    if (prm_dihedrals.Length == 0)
                    {
                        HDebug.Assert(false);
                        logger.Log(string.Format
                                       ("try to add non-existing dihedral (({0}, prm {1})-({2}, prm {3})-({4}, prm {5})-({6}, prm {7})) in prm"
                                       , atom0, type0, atom1, type1, atom2, type2, atom3, type3
                                       ));
                        continue;
                    }
                    foreach (var prm_dihedral in prm_dihedrals)
                    {
                        Dihedral dihedral;
                        if (atom0.ID < atom3.ID)
                        {
                            dihedral = new Dihedral(atom0, atom1, atom2, atom3, prm_dihedral);
                        }
                        else
                        {
                            dihedral = new Dihedral(atom3, atom2, atom1, atom0, prm_dihedral);
                        }
                        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);
                    }
                }

                // impropers
                Impropers impropers = new Impropers();

                for (int i = 0; i < psf.impropers.GetLength(0); i++)
                {
                    int id0 = psf.impropers[i, 0]; if (id_atom.ContainsKey(id0) == false)
                    {
                        continue;
                    }
                    Atom atom0 = id_atom[id0]; string type0 = atom0.AtomType;
                    int  id1 = psf.impropers[i, 1]; if (id_atom.ContainsKey(id1) == false)
                    {
                        continue;
                    }
                    Atom atom1 = id_atom[id1]; string type1 = atom1.AtomType;
                    int  id2 = psf.impropers[i, 2]; if (id_atom.ContainsKey(id2) == false)
                    {
                        continue;
                    }
                    Atom atom2 = id_atom[id2]; string type2 = atom2.AtomType;
                    int  id3 = psf.impropers[i, 3]; if (id_atom.ContainsKey(id3) == false)
                    {
                        continue;
                    }
                    Atom     atom3 = id_atom[id3]; string type3 = atom3.AtomType;
                    Improper improper = new Improper(atom0, atom1, atom2, atom3, prm.FindImproper(type0, type1, type2, type3, logger));
                    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);
                }

                // 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("psf", psf);
                univ.refs.Add("prm", prm);
                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);
            }