Beispiel #1
0
            public static void Align(IList <Vector> C1
                                     , IList <Anisou> anisou1
                                     , ref IList <Vector> C2
                                     , ref IList <Anisou> anisou2
                                     , HPack <Trans3> outTrans  = null
                                     , HPack <double> optEnergy = null
                                     )
            {
                Trans3 trans = GetTrans(C1, anisou1, C2, anisou2, optEnergy: optEnergy);

                C2      = new List <Vector>(C2);
                anisou2 = new List <Anisou>(anisou2);

                Vector[] nC2 = trans.GetTransformed(C2).ToArray();
                for (int i = 0; i < C2.Count; i++)
                {
                    Vector p2i  = C2[i];
                    Anisou a2i  = anisou2[i];
                    Vector np2i = trans.DoTransform(p2i);
                    Anisou na2i = a2i.Clone();
                    na2i.eigvecs[0] = (trans.DoTransform(p2i + a2i.eigvecs[0]) - np2i).UnitVector();
                    na2i.eigvecs[1] = (trans.DoTransform(p2i + a2i.eigvecs[1]) - np2i).UnitVector();
                    na2i.eigvecs[2] = (trans.DoTransform(p2i + a2i.eigvecs[2]) - np2i).UnitVector();
                    C2[i]           = np2i;
                    anisou2[i]      = na2i;
                }
                if (outTrans != null)
                {
                    outTrans.value = trans;
                }
            }
Beispiel #2
0
 public Pdb GetTransformed(Trans3 trans)
 {
     Element[] nelements = new Element[elements.Length];
     for (int i = 0; i < elements.Length; i++)
     {
         if (typeof(Atom).IsInstanceOfType(elements[i]))
         {
             Atom   atom = (Atom)elements[i];
             Vector pt   = atom.coord;
             pt           = trans.DoTransform(pt);
             atom         = Atom.FromString(atom.GetUpdatedLine(pt));
             nelements[i] = atom;
         }
         else if (typeof(Hetatm).IsInstanceOfType(elements[i]))
         {
             Hetatm hetatm = (Hetatm)elements[i];
             Vector pt     = hetatm.coord;
             pt           = trans.DoTransform(pt);
             hetatm       = Hetatm.FromString(hetatm.GetUpdatedLine(pt));
             nelements[i] = hetatm;
         }
         else
         {
             nelements[i] = elements[i].UpdateElement();
         }
     }
     return(new Pdb(nelements));
 }
Beispiel #3
0
        public double GetRMSD(IList <int> atomidxs, IList <Vector> atomcoords)
        {
            List <Vector> coords = new List <Vector>();

            foreach (int atomidx in atomidxs)
            {
                coords.Add(atoms[atomidx].Coord.Clone());
            }

            Trans3 trans = ICP3.OptimalTransform(coords, atomcoords);

            List <double> SDs = new List <double>();

            for (int i = 0; i < coords.Count; i++)
            {
                Vector moved = trans.DoTransform(coords[i]);
                Vector to    = atomcoords[i];
                double SD    = (moved - to).Dist2;
                SDs.Add(SD);
            }

            double RMSD = Math.Sqrt(SDs.Average());

            return(RMSD);
        }
Beispiel #4
0
            public static void Align(IList <Vector> C1, IList <double> bfactor, ref List <Vector> C2)
            {
                Trans3 trans = GetTrans(C1, bfactor, C2);

                for (int i = 0; i < C2.Count; i++)
                {
                    C2[i] = trans.DoTransform(C2[i]);
                }
            }
Beispiel #5
0
            public int QuasiNewton(List <ForceField.IForceField> frcflds
                                   , double threshold
                                   , double?k = null
                                   , double max_atom_movement = 0.01
                                   , int?max_iteration        = null
                                   , IMinimizeLogger logger   = null                  // logger = new MinimizeLogger_PrintEnergyForceMag(logpath);

                                   , HPack <double> optOutEnergy        = null        // optional output for final energy
                                   , List <Vector> optOutForces         = null        // optional output for final force vectors
                                   , HPack <double> optOutForcesNorm1   = null        // optional output for norm of final force vectors
                                   , HPack <double> optOutForcesNorm2   = null        // optional output for norm of final force vectors
                                   , HPack <double> optOutForcesNormInf = null        // optional output for norm of final force vectors
                                   )
            {
                if (k == null)
                {
                    k = double.MaxValue;
                    foreach (ForceField.IForceField frcfld in frcflds)
                    {
                        double?kk = frcfld.GetDefaultMinimizeStep();
                        if (kk.HasValue)
                        {
                            k = Math.Min(k.Value, kk.Value);
                        }
                    }
                }

                Graph <Universe.Atom[], Universe.Bond> univ_flexgraph = univ.BuildFlexibilityGraph();
                List <Universe.RotableInfo>            univ_rotinfos  = univ.GetRotableInfo(univ_flexgraph);
                // double k = 0.0001;
                int iter = 0;

                // 0. Initial configuration of atoms
                Vector[] coords       = univ.GetCoords();
                Vector[] coords0      = univ.GetCoords();
                bool[]   atomsMovable = null;
                {
                    atomsMovable = new bool[size];
                    for (int i = 0; i < size; i++)
                    {
                        atomsMovable[i] = true;
                    }
                }

                Dictionary <string, object> cache = new Dictionary <string, object>();

                Vector[]    forces = null;
                MatrixByArr hessian;
                double      energy = 0;

                double[] dtor;
                double   forces_NormInf;
                double   forces_Norm1;
                double   forces_Norm2;

                do
                {
                    if (logger != null)
                    {
                        if (forces != null)
                        {
                            logger.log(iter, coords, energy, forces, atomsMovable);
                        }
                        {
                            Vector[] coordsx = coords.HClone <Vector>();
                            Trans3   trans   = ICP3.OptimalTransform(coordsx, coords0);
                            trans.DoTransform(coordsx);
                            logger.logTrajectory(univ, iter, coordsx);
                        }
                    }

                    //GetPotentialAndTorForce(frcflds, coords
                    //                       , out energy, out forces, out hessian, out dtor
                    //                       , cache);
                    {
                        MatrixByArr J = Paper.TNM.GetJ(univ, coords, univ_rotinfos);

                        Vector[] forces0 = univ.GetVectorsZero();
                        hessian = new double[size * 3, size *3];
                        energy  = univ.GetPotential(frcflds, coords, ref forces0, ref hessian, cache);
                        forces  = univ.GetVectorsZero();
                        dtor    = Paper.TNM.GetRotAngles(univ, coords, hessian, forces0, J: J, forceProjectedByTorsional: forces);

                        for (int i = 0; i < forces0.Length; i++)
                        {
                            forces0[i] = forces0[i] * 0.001;
                        }
                        dtor = Paper.TNM.GetRotAngles(univ, coords, hessian, forces0, J: J);
                    }

                    forces_NormInf = NormInf(forces, atomsMovable); // NormInf(forces_prd, atomsMovable);
                    forces_Norm1   = Norm(1, forces, atomsMovable); // Norm(1, forces_prd, atomsMovable);
                    forces_Norm2   = Norm(2, forces, atomsMovable); // Norm(2, forces_prd, atomsMovable);

                    if (forces_NormInf < 0.0001)
                    {
                        System.Environment.Exit(0);
                        break;
                    }

                    coords = Paper.TNM.RotateTorsionals(coords, dtor, univ_rotinfos);
                    iter++;
                }while(true);

                while (true)
                {
//                    if(forces.IsComputable == false)
//                    {
//                        System.Console.Error.WriteLine("non-computable components while doing steepest-descent");
//                        Environment.Exit(0);
//                    }
                    if (logger != null)
                    {
                        logger.log(iter, coords, energy, forces, atomsMovable);
                        logger.logTrajectory(univ, iter, coords);
                        //if(iter %10 == 0)
                        //{
                        //    System.IO.Directory.CreateDirectory("output");
                        //    string pdbname = string.Format("mini.conju.{0:D5}.pdb", iter);
                        //    pdb.ToFile("output\\"+pdbname, coords.ToArray());
                        //    System.IO.File.AppendAllLines("output\\mini.conju.[animation].pml", new string[] { "load "+pdbname+", 1A6G" });
                        //}
                    }
                    // 1. Save the position of atoms
                    // 2. Calculate the potential energy of system and the net forces on atoms
                    // 3. Check if every force reaches to zero,
                    //    , and END if yes
                    bool stopIteration = false;
                    if (forces_NormInf < threshold)
                    {
                        stopIteration = true;
                    }
                    if ((max_iteration != null) && (iter >= max_iteration.Value))
                    {
                        stopIteration = true;
                    }
                    if (stopIteration)
                    {
                        // double check
                        cache = new Dictionary <string, object>(); // reset cache
                        //energy = GetPotential(frcflds, coords, out forces, cache);
//                        energy = univ.GetPotential(frcflds, coords, ref forces._vecs, ref hessian, cache);
                        forces_NormInf = NormInf(forces, atomsMovable);
                        forces_Norm1   = Norm(1, forces, atomsMovable);
                        forces_Norm2   = Norm(2, forces, atomsMovable);

                        if (forces_NormInf < threshold)
                        {
                            if (iter != 1)
                            {
                                univ.SetCoords(coords);
                            }
                            //{
                            //    string pdbname = string.Format("mini.conju.{0:D5}.pdb", iter);
                            //    pdb.ToFile("output\\"+pdbname, coords.ToArray());
                            //    System.IO.File.AppendAllLines("output\\mini.conju.[animation].pml", new string[] { "load "+pdbname+", 1A6G" });
                            //}
                            {
                                if (optOutEnergy != null)
                                {
                                    optOutEnergy.value = energy;
                                }
                                if (optOutForces != null)
                                {
                                    optOutForces.Clear(); optOutForces.AddRange(forces.ToArray());
                                }
                                if (optOutForcesNorm1 != null)
                                {
                                    optOutForcesNorm1.value = forces_Norm1;
                                }
                                if (optOutForcesNorm2 != null)
                                {
                                    optOutForcesNorm2.value = forces_Norm2;
                                }
                                if (optOutForcesNormInf != null)
                                {
                                    optOutForcesNormInf.value = forces_NormInf;
                                }
                            }
                            return(iter);
                        }
                    }
                    // 4. Move atoms with conjugated gradient
                    //Vectors coords_prd;
                    {
                        if ((iter > 0) && (iter % 100 == 0))
                        {
                            cache = new Dictionary <string, object>(); // reset cache
                        }
                        if (iter > 1)
                        {
//                            Debug.Assert(forces0 != null);
//                            double r = Vectors.VtV(forces, forces).Sum() / Vectors.VtV(forces0, forces0).Sum();
//                            h          = forces + r * h;
//                            double kk      = k.Value;
//                            double hNormInf = NormInf(h, atomsMovable);
//                            if(kk*hNormInf > max_atom_movement)
//                                // make the maximum movement as atomsMovable
//                                kk = max_atom_movement/(hNormInf);
//                            //double kk = (k*h.NormsInf().NormInf() < max_atom_movement) ? k : (max_atom_movement/h.NormsInf().NormInf());
//                            //double   kk = (k.Value*NormInf(h,atomsMovable) < max_atom_movement)? k.Value : (max_atom_movement/NormInf(h,atomsMovable));
//                            double[] dangles = TNM.GetRotAngles(univ, coords._vecs, hessian, (kk * h)._vecs);
//                            coords_prd = TNM.RotateTorsionals(coords, dangles, univ_rotinfos);
                        }
                        else
                        {
//                            // same to the steepest descent for the first iteration
//                            h          = forces;
//                            double kk      = k.Value;
//                            double hNormInf = NormInf(h, atomsMovable);
//                            if(kk*hNormInf > max_atom_movement)
//                                // make the maximum movement as atomsMovable
//                                kk = max_atom_movement/(hNormInf);
//                            //double kk = (k*h.NormsInf().NormInf() < max_atom_movement) ? k : (max_atom_movement/h.NormsInf().NormInf());
//                            //double   kk = (k.Value*NormInf(h,atomsMovable) < max_atom_movement)? k.Value : (max_atom_movement/NormInf(h, atomsMovable));
//
//                            //double[] dangles = TNM.GetRotAngles(this, coords, kk * h, 1);
//                            double[] dangles = TNM.GetRotAngles(univ, coords._vecs, hessian, (kk * h)._vecs);
//                            coords_prd = TNM.RotateTorsionals(coords, dangles, univ_rotinfos);
//
//    //                        coords_prd = AddConditional(coords, kk * h, atomsMovable);
                        }
                    }
                    // 5. Predict energy or forces on atoms
                    iter++;
                    //double energy_prd;
                    Vectors     forces_prd  = univ.GetVectorsZero();
                    MatrixByArr hessian_prd = new double[size * 3, size *3];
                    //double energy_prd = GetPotential(frcflds, coords_prd, out forces_prd, cache); iter++;

//                    GetPotentialAndTorForce(frcflds, coords_prd
//                           , out energy_prd, out forces_prd._vecs, out hessian_prd
//                           , cache);
//    //                double energy_prd = univ.GetPotential(frcflds, coords_prd, ref forces_prd._vecs, ref hessian_prd, cache);
//    //                Vector[] dcoord_prd = univ.GetVectorsZero();
//    //                double[] dangles_prd = TNM.GetRotAngles(univ, coords_prd, forces_prd, 1, dcoordsRotated: dcoord_prd);
//    //                         dangles_prd = TNM.GetRotAngles(univ, coords_prd, hessian_prd, forces_prd, forceProjectedByTorsional: dcoord_prd);
//                    //Vectors coords_prd2 = coords_prd.Clone();
//                    //TNM.RotateTorsionals(coords_prd2, dangles_prd, univ_rotinfos);
//
//                    double forces_prd_NormInf = NormInf(forces_prd, atomsMovable); // NormInf(forces_prd, atomsMovable);
//                    double forces_prd_Norm1   = Norm(1, forces_prd, atomsMovable); // Norm(1, forces_prd, atomsMovable);
//                    double forces_prd_Norm2   = Norm(2, forces_prd, atomsMovable); // Norm(2, forces_prd, atomsMovable);
//                    // 6. Check if the predicted forces or energy will exceed over the limit
//                    //    , and goto 1 if no
//    //                doSteepDeescent = true;
//                    //if((doSteepDeescent == false) || ((energy_prd <= energy) && (forces_prd_NormInf < forces_NormInf+1.0))
//                    if((energy_prd < energy+0.1) && (forces_prd_NormInf < forces_NormInf+0.0001))
//                    {
//                        energy0 = energy;
//                        forces0 = forces;
//                        coords = coords_prd;
//                        forces = forces_prd;
//                        hessian = hessian_prd;
//                        energy = energy_prd;
//                        forces_NormInf = forces_prd_NormInf;
//                        forces_Norm1   = forces_prd_Norm1;
//                        forces_Norm2   = forces_prd_Norm2;
//                        continue;
//                    }
//                    if(logger != null)
//                        logger.log(iter, coords_prd, energy_prd, forces_prd, atomsMovable, "will do steepest");
//                    // 7. Back to saved configuration
//                    // 8. Move atoms with simple gradient
//                    {
//                        // same to the steepest descent
//                        h          = forces;
//                        double kk      = k.Value;
//                        double hNormInf = NormInf(h, atomsMovable);
//                        if(kk*hNormInf > max_atom_movement)
//                            // make the maximum movement as atomsMovable
//                            kk = max_atom_movement/(hNormInf);
//                        double[] dangles = TNM.GetRotAngles(univ, coords._vecs, hessian, (kk * h)._vecs);
//                        coords_prd = TNM.RotateTorsionals(coords, dangles, univ_rotinfos);
//                    }
//                    forces_prd = univ.GetVectorsZero();
//                    hessian_prd = new double[size*3, size*3];
//                    //energy_prd = univ.GetPotential(frcflds, coords_prd, ref forces_prd._vecs, ref hessian_prd, cache);
//                    GetPotentialAndTorForce(frcflds, coords_prd
//                           , out energy_prd, out forces_prd._vecs, out hessian_prd
//                           , cache);
//                    forces_prd_NormInf = NormInf(forces_prd, atomsMovable);
//                    forces_prd_Norm1   = Norm(1, forces_prd, atomsMovable);
//                    forces_prd_Norm2   = Norm(2, forces_prd, atomsMovable);
//
//                    energy0 = energy;
//                    forces0 = forces;
//                    coords = coords_prd;
//                    forces = forces_prd;
//                    energy = energy_prd;
//                    forces_NormInf = forces_prd_NormInf;
//                    forces_Norm1   = forces_prd_Norm1;
//                    forces_Norm2   = forces_prd_Norm2;
//                    // 9. goto 1
                }
            }
Beispiel #6
0
            public static Trans3 GetTrans(IList <Vector> C1, IList <Anisou> anisou1, IList <Vector> C2, IList <Anisou> anisou2, HPack <double> optEnergy = null)
            {
                int size = C1.Count;

                HDebug.Assert(size == C1.Count, size == C2.Count);

                Vector[] srcs = new Vector[size * 3 * 2]; // sources
                Vector[] tars = new Vector[size * 3 * 2]; // targets
                double[] weis = new double[size * 3 * 2]; // weights
                double[] engs = new double[size * 3 * 2]; // energies

                //for(int i=0; i<size; i++)
                //{
                //    Debug.Assert(anisou1[i].eigvals[0] >= 0); W1s[i*3+0] = (anisou1[i].eigvals[0] <= 0) ? 0 : 1 / anisou1[i].eigvals[0];
                //    Debug.Assert(anisou1[i].eigvals[1] >= 0); W1s[i*3+1] = (anisou1[i].eigvals[1] <= 0) ? 0 : 1 / anisou1[i].eigvals[1];
                //    Debug.Assert(anisou1[i].eigvals[2] >= 0); W1s[i*3+2] = (anisou1[i].eigvals[2] <= 0) ? 0 : 1 / anisou1[i].eigvals[2];
                //    enrgs[i*3+0] = enrgs[i*3+1] = enrgs[i*3+2] = double.NaN;
                //}

                //Trans3 trans = ICP3.OptimalTransform(C2, C1);
                Trans3 trans    = new Trans3(new double[] { 0, 0, 0 }, 1, Quaternion.UnitRotation);// = transICP3.OptimalTransformWeighted(C2, C1, W1s);
                int    iter     = 0;
                int    iter_max = 1000;

                //double enrg = double.NaN;
                while (iter < iter_max)
                {
                    iter++;

                    //for(int i=0; i<size; i++)
                    System.Threading.Tasks.Parallel.For(0, size, delegate(int i)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Vector p1 = C1[i];
                            Vector n1 = anisou1[i].axes[j];
                            double w1 = anisou1[i].eigvals[j]; w1 = (w1 <= 0) ? 0 : 1 / w1;

                            Vector p2 = trans.DoTransform(C2[i]);
                            Vector n2 = (trans.DoTransform(C2[i] + anisou2[i].axes[j]) - p2).UnitVector();
                            double w2 = anisou2[i].eigvals[j]; w2 = (w2 <= 0) ? 0 : 1 / w2;

                            Vector clo12 = p2 - LinAlg.DotProd(n1, p2 - p1) * n1; // closest point from p1 to plane2
                            Vector clo21 = p1 - LinAlg.DotProd(n2, p1 - p2) * n2; // closest point from p1 to plane2
                            HDebug.AssertTolerance(0.00001, LinAlg.DotProd(clo12 - p1, n1));
                            HDebug.AssertTolerance(0.00001, LinAlg.DotProd(clo21 - p2, n2));

                            // p2 -> (pt closest to p2 on plane1 with w1)
                            srcs[(i * 3 + j) * 2 + 0] = p2;
                            tars[(i * 3 + j) * 2 + 0] = clo12;
                            weis[(i * 3 + j) * 2 + 0] = w1;
                            engs[(i * 3 + j) * 2 + 0] = w1 * (p2 - clo12).Dist2;
                            // inverse of {p1 -> (pt closest to p1 on plane2 with w2)}
                            // = (pt closest to p1 on plane2 with w2) -> p1
                            srcs[(i * 3 + j) * 2 + 1] = clo21;
                            tars[(i * 3 + j) * 2 + 1] = p1;
                            weis[(i * 3 + j) * 2 + 1] = w2;
                            engs[(i * 3 + j) * 2 + 1] = w2 * (clo21 - p1).Dist2;
                        }
                    });
                    Trans3 dtrans = ICP3.OptimalTransformWeighted(srcs, tars, weis);
                    trans = Trans3.AppendTrans(trans, dtrans);
                    double max_dtrans_matrix = (dtrans.TransformMatrix - LinAlg.Eye(4)).ToArray().HAbs().HMax();
                    if (max_dtrans_matrix < 0.0000001)
                    {
                        break;
                    }
                }

                if (optEnergy != null)
                {
                    optEnergy.value = engs.Sum() / size;
                }

                return(trans);
            }
Beispiel #7
0
                public static void WriteCircles(string pmlpath
                                                , string objname
                                                , IList <Circle> lst_circle
                                                //, int numLinesInCircle = 50
                                                //, double? linewidth = null
                                                //, double alpha=1.0, double red=1.0, double green=1.0, double blue=1.0
                                                , bool append = false
                                                )
                {
                    StreamWriter file;

                    if (append)
                    {
                        file = HFile.AppendText(pmlpath);
                    }
                    else
                    {
                        file = HFile.CreateText(pmlpath);
                    }
                    {
                        //StreamWriter file = File.AppendText(pypath);
                        file.WriteLine("from pymol.cgo import *");
                        file.WriteLine("from pymol import cmd");
                        file.WriteLine("obj = [");
                        foreach (var circle in lst_circle)
                        {
                            //Vector center        = center_normal_radii_numseg_color_width.Item1;
                            //Vector normal        = center_normal_radii_numseg_color_width.Item2;
                            //double radii         = center_normal_radii_numseg_color_width.Item3;
                            //int numLinesInCircle = center_normal_radii_numseg_color_width.Item4;
                            //double? alpha        = center_normal_radii_numseg_color_width.Item5.Item1;
                            //double? red          = center_normal_radii_numseg_color_width.Item5.Item2;
                            //double? green        = center_normal_radii_numseg_color_width.Item5.Item3;
                            //double? blue         = center_normal_radii_numseg_color_width.Item5.Item4;
                            //double? linewidth    = center_normal_radii_numseg_color_width.Item6;

                            Trans3 trans = Trans3.GetTransformNoScale(new double[3] {
                                0, 0, 0
                            }
                                                                      , new double[3] {
                                0, 0, 1
                            }
                                                                      , circle.center
                                                                      , circle.center + circle.normal.UnitVector()
                                                                      );
                            Vector[] pts = new Vector[circle.numLinesInCircle + 1];
                            double   di  = (2 * Math.PI) / circle.numLinesInCircle;
                            for (int i = 0; i < circle.numLinesInCircle; i++)
                            {
                                Vector lpt = new double[3] {
                                    circle.radii *Math.Cos(di * i), circle.radii *Math.Sin(di * i), 0
                                };
                                pts[i] = trans.DoTransform(lpt);
                            }
                            pts[circle.numLinesInCircle] = pts[0];

                            file.Write("BEGIN, LINE_STRIP, ");
                            if (circle.linewidth != null)
                            {
                                file.Write("LINEWIDTH, {0:0.000}, ", circle.linewidth);
                            }
                            if (circle.alpha != null)
                            {
                                file.Write("ALPHA, {0:0.000},", circle.alpha);
                            }
                            if (circle.color != null)
                            {
                                file.Write("COLOR, {0:0.000}, {1:0.000}, {2:0.000}, ", circle.color.Item1, circle.color.Item2, circle.color.Item3);
                            }
                            for (int i = 0; i < pts.Length; i++)
                            {
                                if (i % 10 == 0)
                                {
                                    file.WriteLine();
                                    file.Write("       ");
                                }
                                file.Write("VERTEX,   {0:0.000},  {1:0.000},   {2:0.000}, ", pts[i][0] + 0.000001, pts[i][1] + 0.000001, pts[i][2] + 0.000001);
                            }
                            file.WriteLine();
                            file.WriteLine("       END, ");
                        }
                        file.WriteLine("]");
                        // file.WriteLine("cmd.load_cgo(obj,\"channel_path\")");
                        file.WriteLine("cmd.load_cgo(obj,\"" + objname + "\")");
                        file.WriteLine();
                    }
                    file.Close();
                }
            public int ConjugateGradient(List <ForceField.IForceField> frcflds
                                         , double threshold
                                         , double?k = null
                                         , double max_atom_movement = 0.01
                                         , int?max_iteration        = null
                                         , IMinimizeLogger logger   = null            // logger = new MinimizeLogger_PrintEnergyForceMag(logpath);

                                         , HPack <double> optOutEnergy        = null  // optional output for final energy
                                         , List <Vector> optOutForces         = null  // optional output for final force vectors
                                         , HPack <double> optOutForcesNorm1   = null  // optional output for norm of final force vectors
                                         , HPack <double> optOutForcesNorm2   = null  // optional output for norm of final force vectors
                                         , HPack <double> optOutForcesNormInf = null  // optional output for norm of final force vectors
                                         )
            {
                if (k == null)
                {
                    k = double.MaxValue;
                    foreach (ForceField.IForceField frcfld in frcflds)
                    {
                        double?kk = frcfld.GetDefaultMinimizeStep();
                        if (kk.HasValue)
                        {
                            k = Math.Min(k.Value, kk.Value);
                        }
                    }
                }

                Graph <Universe.Atom[], Universe.Bond> univ_flexgraph = univ.BuildFlexibilityGraph();
                List <Universe.RotableInfo>            univ_rotinfos  = univ.GetRotableInfo(univ_flexgraph);
                // double k = 0.0001;
                int iter = 0;

                // 0. Initial configuration of atoms
                Vector[] coords0 = univ.GetCoords();
                Vectors  coords  = univ.GetCoords();

                bool[] atomsMovable = null;
                if (atomsMovable == null)
                {
                    atomsMovable = new bool[size];
                    for (int i = 0; i < size; i++)
                    {
                        atomsMovable[i] = true;
                    }
                }

                Vectors h      = univ.GetVectorsZero();
                Vectors forces = univ.GetVectorsZero();
                Dictionary <string, object> cache;
                double energy;

                cache = new Dictionary <string, object>();
                GetPotentialAndTorForce(frcflds, coords
                                        , out energy, out forces._vecs
                                        , cache);

                double  forces_NormInf = NormInf(forces, atomsMovable);
                double  forces_Norm1   = Norm(1, forces, atomsMovable);
                double  forces_Norm2   = Norm(2, forces, atomsMovable);
                Vectors forces0        = forces;
                double  energy0        = energy;

                while (true)
                {
                    if (forces.IsComputable == false)
                    {
                        System.Console.Error.WriteLine("non-computable components while doing steepest-descent");
                        HEnvironment.Exit(0);
                    }
                    if (logger != null)
                    {
                        logger.log(iter, coords, energy, forces, atomsMovable);
                        {   // logger.logTrajectory(univ, iter, coords);
                            Vector[] coordsx = coords._vecs.HClone <Vector>();
                            Trans3   trans   = ICP3.OptimalTransform(coordsx, coords0);
                            trans.DoTransform(coordsx);
                            logger.logTrajectory(univ, iter, coordsx);
                        }
                    }
                    // 1. Save the position of atoms
                    // 2. Calculate the potential energy of system and the net forces on atoms
                    // 3. Check if every force reaches to zero,
                    //    , and END if yes
                    bool stopIteration = false;
                    if (forces_NormInf < threshold)
                    {
                        stopIteration = true;
                    }
                    if ((max_iteration != null) && (iter >= max_iteration.Value))
                    {
                        stopIteration = true;
                    }
                    if (stopIteration)
                    {
                        // double check
                        cache = new Dictionary <string, object>(); // reset cache
                        GetPotentialAndTorForce(frcflds, coords
                                                , out energy, out forces._vecs
                                                , cache);
                        forces_NormInf = NormInf(forces, atomsMovable);
                        forces_Norm1   = Norm(1, forces, atomsMovable);
                        forces_Norm2   = Norm(2, forces, atomsMovable);

                        if (forces_NormInf < threshold)
                        {
                            if (iter != 1)
                            {
                                univ.SetCoords((Vector[])coords);
                            }
                            {
                                if (optOutEnergy != null)
                                {
                                    optOutEnergy.value = energy;
                                }
                                if (optOutForces != null)
                                {
                                    optOutForces.Clear(); optOutForces.AddRange(forces.ToArray());
                                }
                                if (optOutForcesNorm1 != null)
                                {
                                    optOutForcesNorm1.value = forces_Norm1;
                                }
                                if (optOutForcesNorm2 != null)
                                {
                                    optOutForcesNorm2.value = forces_Norm2;
                                }
                                if (optOutForcesNormInf != null)
                                {
                                    optOutForcesNormInf.value = forces_NormInf;
                                }
                            }
                            return(iter);
                        }
                    }
                    // 4. Move atoms with conjugated gradient
                    Vectors coords_prd;
                    {
                        if ((iter > 0) && (iter % 100 == 0))
                        {
                            cache = new Dictionary <string, object>(); // reset cache
                        }
                        if (iter >= 1)
                        {
                            HDebug.Assert(forces0 != null);
                            double r = Vectors.VtV(forces, forces).Sum() / Vectors.VtV(forces0, forces0).Sum();
                            h = forces + r * h;
                            double kk       = k.Value;
                            double hNormInf = NormInf(h, atomsMovable);
                            if (kk * hNormInf > max_atom_movement)
                            {
                                // make the maximum movement as atomsMovable
                                kk = max_atom_movement / (hNormInf);
                            }

                            Vector dangles = Paper.TNM.GetRotAngles(univ, coords, kk * h, 1);
                            //dangles *= -1;
                            coords_prd = Paper.TNM.RotateTorsionals(coords, dangles, univ_rotinfos);
                        }
                        else
                        {
                            // same to the steepest descent for the first iteration
                            h = forces;
                            double kk       = k.Value;
                            double hNormInf = NormInf(h, atomsMovable);
                            if (kk * hNormInf > max_atom_movement)
                            {
                                // make the maximum movement as atomsMovable
                                kk = max_atom_movement / (hNormInf);
                            }

                            Vector dangles = Paper.TNM.GetRotAngles(univ, coords, kk * h, 1);
                            //dangles *= -1;
                            coords_prd = Paper.TNM.RotateTorsionals(coords, dangles, univ_rotinfos);
                        }
                    }
                    // 5. Predict energy or forces on atoms
                    iter++;
                    double  energy_prd;
                    Vectors forces_prd = univ.GetVectorsZero();
                    GetPotentialAndTorForce(frcflds, coords_prd
                                            , out energy_prd, out forces_prd._vecs
                                            , cache);

                    //                double energy_prd = univ.GetPotential(frcflds, coords_prd, ref forces_prd._vecs, ref hessian_prd, cache);
                    //                Vector[] dcoord_prd = univ.GetVectorsZero();
                    //                double[] dangles_prd = TNM.GetRotAngles(univ, coords_prd, forces_prd, 1, dcoordsRotated: dcoord_prd);
                    //                         dangles_prd = TNM.GetRotAngles(univ, coords_prd, hessian_prd, forces_prd, forceProjectedByTorsional: dcoord_prd);
                    //Vectors coords_prd2 = coords_prd.Clone();
                    //TNM.RotateTorsionals(coords_prd2, dangles_prd, univ_rotinfos);

                    double forces_prd_NormInf = NormInf(forces_prd, atomsMovable); // NormInf(forces_prd, atomsMovable);
                    double forces_prd_Norm1   = Norm(1, forces_prd, atomsMovable); // Norm(1, forces_prd, atomsMovable);
                    double forces_prd_Norm2   = Norm(2, forces_prd, atomsMovable); // Norm(2, forces_prd, atomsMovable);
                    // 6. Check if the predicted forces or energy will exceed over the limit
                    //    , and goto 1 if no
                    if ((energy_prd < energy + 0.1) && (forces_prd_NormInf < forces_NormInf + 0.0001))
                    {
                        energy0        = energy;
                        forces0        = forces;
                        coords         = coords_prd;
                        forces         = forces_prd;
                        energy         = energy_prd;
                        forces_NormInf = forces_prd_NormInf;
                        forces_Norm1   = forces_prd_Norm1;
                        forces_Norm2   = forces_prd_Norm2;
                        continue;
                    }
                    if (logger != null)
                    {
                        logger.log(iter, coords_prd, energy_prd, forces_prd, atomsMovable, "will do steepest");
                    }
                    // 7. Back to saved configuration
                    // 8. Move atoms with simple gradient
                    {
                        // same to the steepest descent
                        h = forces;
                        double kk       = k.Value;
                        double hNormInf = NormInf(h, atomsMovable);
                        if (kk * hNormInf > max_atom_movement)
                        {
                            // make the maximum movement as atomsMovable
                            kk = max_atom_movement / (hNormInf);
                        }
                        Vector dangles = Paper.TNM.GetRotAngles(univ, coords, kk * h, 1);
                        //dangles *= -1;
                        coords_prd = Paper.TNM.RotateTorsionals(coords, dangles, univ_rotinfos);
                    }
                    forces_prd = univ.GetVectorsZero();
                    //energy_prd = univ.GetPotential(frcflds, coords_prd, ref forces_prd._vecs, ref hessian_prd, cache);
                    GetPotentialAndTorForce(frcflds, coords_prd
                                            , out energy_prd, out forces_prd._vecs
                                            , cache);
                    forces_prd_NormInf = NormInf(forces_prd, atomsMovable);
                    forces_prd_Norm1   = Norm(1, forces_prd, atomsMovable);
                    forces_prd_Norm2   = Norm(2, forces_prd, atomsMovable);

                    energy0        = energy;
                    forces0        = forces;
                    coords         = coords_prd;
                    forces         = forces_prd;
                    energy         = energy_prd;
                    forces_NormInf = forces_prd_NormInf;
                    forces_Norm1   = forces_prd_Norm1;
                    forces_Norm2   = forces_prd_Norm2;
                    // 9. goto 1
                }
            }
Beispiel #9
0
            public static Trans3 GetTrans(IList <Vector> C1, IList <Vector> C2, HPack <int> optIter = null, HPack <List <double> > optListWeightSum = null)
            {
                HDebug.Assert(false);
                HDebug.Assert(C1.Count == C2.Count);
                int size = C1.Count;

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

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

                if (optListWeightSum != null)
                {
                    optListWeightSum.value = new List <double>();
                }
                double maxdist = 0;

                for (int i = 0; i < size; i++)
                {
                    Vector diff = C1[i] - trans1.DoTransform(C2[i]); maxdist = Math.Max(maxdist, diff.Dist);
                }
                while ((trans0.TransformMatrix - trans1.TransformMatrix).ToArray().HAbs().HMax() > 0.00000001)
                {
                    iter++;
                    if (optIter != null)
                    {
                        optIter.value++;
                    }
                    Vector[] C2trans = trans1.GetTransformed(C2).ToArray();

                    Vector dist2s = new double[size];
                    Vector dists  = new double[size];
                    for (int i = 0; i < size; i++)
                    {
                        double dist2 = (C1[i] - C2trans[i]).Dist2;
                        dist2     = HMath.Between(0.00001, dist2, double.PositiveInfinity);
                        dist2s[i] = dist2;
                        dists[i]  = Math.Sqrt(dist2);
                    }
                    //int[] idxsort = dist2s.ToArray().IdxSorted().Reverse();
                    Vector weight = dists.Clone() / dists.ToArray().Max();
                    for (int i = 0; i < size; i++)
                    {
                        double w = weight[i];
                        //w = (w*2-1)*4;
                        w         = (w - 1) * 4;
                        w         = 1 / (1 + Math.Exp(-1 * w));
                        weight[i] = w;
                    }
                    //weight[idxsort[i]] = (i <= 6) ? 1 : 0;
                    if (optListWeightSum != null)
                    {
                        optListWeightSum.value.Add(weight.Sum());
                    }

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

                    maxdist = 0;
                    for (int i = 0; i < size; i++)
                    {
                        Vector diff = C1[i] - trans1.DoTransform(C2[i]); maxdist = Math.Max(maxdist, diff.Dist);
                    }
                }

                return(trans1);
            }
Beispiel #10
0
        public static IEnumerable <ValueTuple <int, Pdb.Atom> > EnumAtomsInWaterBox
            (double maxx
            , double maxy
            , double maxz
            , double dist2water = 2.9 //2.8 // 2.7564;
            , bool randOrient   = true
            , string opt        = "water in uniform cube"
            )
        {
            System.Random rand = null;
            if (randOrient)
            {
                rand = new System.Random();
            }
            //List<Pdb.Atom> atoms = new List<Pdb.Atom>();
            int file   = 1;
            int serial = 1;
            int resSeq = 1;

            IEnumerable <Vector> enumer = null;

            switch (opt)
            {
            case "water in uniform cube": enumer = Geometry.EnumPointsInVolume_UniformCube(maxx / dist2water, maxy / dist2water, maxz / dist2water); break;

            case "water in uniform tetrahedron": enumer = Geometry.EnumPointsInVolume_UniformTetrahedron(maxx / dist2water, maxy / dist2water, maxz / dist2water); break;
            }

            foreach (Vector pt in enumer)
            {
                resSeq++;
                double x = pt[0] * dist2water;
                double y = pt[1] * dist2water;
                double z = pt[2] * dist2water;
                //var atom = Pdb.Atom.FromData(i+1, "H", "HOH", '1', i+1, pt[0], pt[1], pt[2]);

                /// ATOM   8221  OH2 TIP32 547      11.474  41.299  26.099  1.00  0.00           O
                /// ATOM   8222  H1  TIP32 547      12.474  41.299  26.099  0.00  0.00           H
                /// ATOM   8223  H2  TIP32 547      11.148  42.245  26.099  0.00  0.00           H
                Vector[] pthoh = new Vector[]
                {
                    new double[] { 0.000000, 0.000000, 0.000000 },
                    new double[] { 0.000000, 0.000000, 0.957200 },
                    new double[] { 0.926627, 0.000000, 0.239987 },
                };
                if (randOrient != null)
                {
                    Vector axis = new double[]
                    { (rand.NextDouble() * 10) % 5
                      , (rand.NextDouble() * 10) % 5
                      , (rand.NextDouble() * 10) % 5 };
                    Trans3 trans = Trans3.FromRotate(axis.UnitVector(), rand.NextDouble() * Math.PI * 4);
                    trans.DoTransform(pthoh);
                }

                if ((serial >= 99990) || (resSeq >= 9990))
                {
                    file++;
                    serial = 1;
                    resSeq = 1;
                }

                serial++; Pdb.Atom OH2 = Pdb.Atom.FromData(serial, "OH2", "HOH", '1', resSeq + 1, x + pthoh[0][0], y + pthoh[0][1], z + pthoh[0][2], element: "O"); yield return(new ValueTuple <int, Pdb.Atom>(file, OH2)); //atoms.Add(OH2);

                serial++; Pdb.Atom H1 = Pdb.Atom.FromData(serial, "H1", "HOH", '1', resSeq + 1, x + pthoh[1][0], y + pthoh[1][1], z + pthoh[1][2], element: "H"); yield return(new ValueTuple <int, Pdb.Atom>(file, H1));    //atoms.Add(H1 );

                serial++; Pdb.Atom H2 = Pdb.Atom.FromData(serial, "H2", "HOH", '1', resSeq + 1, x + pthoh[2][0], y + pthoh[2][1], z + pthoh[2][2], element: "H"); yield return(new ValueTuple <int, Pdb.Atom>(file, H2));    //atoms.Add(H2 );
            }
        }