Esempio n. 1
0
        public override SimulationResult Simulate()
        {
            PerturbDistances(1.0);
            SimulationResult sr = base.Simulate();

            sr.SimulationMethod |= SimulationMethods.ErrorEstimation;
            return(sr);
        }
Esempio n. 2
0
        public override SimulationResult Simulate()
        {
            SimulationResult srrefined = base.Simulate();

            srrefined.SimulationMethod |= SimulationMethods.Refinement;
            InitializeStructureForRefinement(srrefined);
            srrefined.RefinedLabelingPositions = RedoAV();
            srrefined.E = SetState(srrefined);
            return(srrefined);
        }
Esempio n. 3
0
 public override double SetState(SimulationResult sr0)
 {
     InitializeStructureForRefinement(sr0);
     if ((sr0.SimulationMethod & SimulationMethods.Refinement) == 0 || sr0.RefinedLabelingPositions == null)
     {
         this.LabelingPositions = RedoAV();
     }
     else
     {
         this.LabelingPositions = sr0.RefinedLabelingPositions;
     }
     return(base.SetState(sr0));
 }
Esempio n. 4
0
        /// <summary>
        /// Combines units into one "molecule" (mainly to be able to redo AV). Only a few properties are copied
        /// </summary>
        /// <param name="sr">Structure</param>
        public Molecule(SimulationResult sr)
        {
            MoleculeList units = sr.Molecules;

            Prepared     = false;
            Name         = "Structure " + sr.InternalNumber.ToString();
            FullFileName = "";
            Error        = "";

            NAtoms = 0;
            for (int i = 0; i < units.Count; i++)
            {
                NAtoms += units[i].NAtoms;
            }

            AtomMass = new double[NAtoms];
            vdWR     = new double[NAtoms];

            OriginalAtomID = new int[NAtoms];
            Atoms          = new string[NAtoms];
            AtomsStandard  = new string[NAtoms];
            Mass           = 0.0;
            CM             = new Vector3();
            Molecule m, m0 = units[0];
            int      n = 0;
            Vector3  r;

            // local coordinates
            XLocal = new double[NAtoms];
            YLocal = new double[NAtoms];
            ZLocal = new double[NAtoms];

            for (int i = 0; i < units.Count; i++)
            {
                m = units[i];
                for (int j = 0; j < m.NAtoms; j++)
                {
                    r = sr.Rotation[i] * (new Vector3(m.XLocal[j], m.YLocal[j], m.ZLocal[j]))
                        - m0.CM + m.CM + sr.Translation[i];
                    XLocal[n]         = r.X;
                    YLocal[n]         = r.Y;
                    ZLocal[n]         = r.Z;
                    AtomMass[n]       = m.AtomMass[j];
                    vdWR[n]           = m.vdWR[j];
                    OriginalAtomID[n] = m.OriginalAtomID[j];
                    Atoms[n]          = m.Atoms[j];
                    AtomsStandard[n]  = m.AtomsStandard[j];
                    n++;
                }
            }
        }
Esempio n. 5
0
        public override double SetState(SimulationResult sr)
        {
            PrepareSimulation();
            _originalstate = sr;
            for (int i = 0; i < _molecules.Count; i++)
            {
                translation[i] = sr.Translation[i] - _molecules[0].CM + _molecules[i].CM;
                rotation[i]    = sr.Rotation[i];
            }
            double E = ForceAndTorque();

            CheckForClashes(true);
            return(E + Eclash);
        }
Esempio n. 6
0
        /// <summary>
        /// Best rotation to overlay with a reference structure
        /// See Kabsch papers for details: Kabsch W. Acta Cryst. A32 (1976) 922, A34 (1978) 827
        /// It is assumed that translations are already calculated in the same (weighted/unweighted) mode!!!
        /// </summary>
        /// <param name="refsr">reference structure</param>
        /// <param name="weighted">apply weights = mass</param>
        public void CalculateBestFitRotation(SimulationResult refsr, Boolean weighted)
        {
            if (Double.IsNaN(this.E) || Double.IsNaN(refsr.E))
            {
                this.BestFitRotation = Matrix3.E;
                return;
            }
            Vector3  r, r1, r2;
            Int32    NAtomstotal = 0, n;
            Molecule m;
            Double   w;

            for (Int32 i = 0; i < this.Molecules.Count; i++)
            {
                NAtomstotal += this.Molecules[i].NAtoms;
            }

            // rotation
            Mapack.Matrix Rxt = new Mapack.Matrix(NAtomstotal, 3);
            Mapack.Matrix Ry  = new Mapack.Matrix(3, NAtomstotal);
            n = 0;
            for (Int32 i = 0; i < this.Molecules.Count; i++)
            {
                m = this.Molecules[i];
                for (Int32 j = 0; j < m.NAtoms; j++)
                {
                    r         = new Vector3(m.XLocal[j], m.YLocal[j], m.ZLocal[j]);
                    r1        = this.Rotation[i] * r + this.Translation[i] + m.CM + this.BestFitTranslation;
                    r2        = refsr.Rotation[i] * r + refsr.Translation[i] + m.CM + refsr.BestFitTranslation;
                    w         = weighted ? m.AtomMass[j] : 1.0;
                    Rxt[n, 0] = r1.X; Rxt[n, 1] = r1.Y; Rxt[n, 2] = r1.Z;
                    Ry[0, n]  = r2.X * w; Ry[1, n] = r2.Y * w; Ry[2, n] = r2.Z * w;
                    n++;
                }
            }
            // Kabsch solution
            Mapack.Matrix R = Ry * Rxt;
            Mapack.SingularValueDecomposition svdR = new Mapack.SingularValueDecomposition(R);
            Mapack.Matrix V  = svdR.VMatrix;
            Mapack.Matrix rS = new Mapack.Matrix(3, 3);
            rS[0, 0] = 1.0 / svdR.Diagonal[0];
            rS[1, 1] = 1.0 / svdR.Diagonal[1];
            rS[2, 2] = (R.Determinant > 0.0) ? 1.0 / svdR.Diagonal[2] : -1.0 / svdR.Diagonal[2];
            Mapack.Matrix Um = R * V * rS * V.Transpose();
            Matrix3       U  = new Matrix3(Um[0, 0], Um[0, 1], Um[0, 2],
                                           Um[1, 0], Um[1, 1], Um[1, 2], Um[2, 0], Um[2, 1], Um[2, 2]);

            this.BestFitRotation = Matrix3.RepairRotation(U);
        }
Esempio n. 7
0
 private void InitializeStructureForRefinement(SimulationResult sr0)
 {
     sr          = sr0;
     mt          = new Molecule(sr);
     molstart    = new int[sr.Molecules.Count];
     molnatoms   = new int[sr.Molecules.Count];
     molstart[0] = 0;
     for (int i = 1; i < sr.Molecules.Count; i++)
     {
         molstart[i] = molstart[i - 1] + sr.Molecules[i - 1].NAtoms;
     }
     for (int i = 0; i < sr.Molecules.Count; i++)
     {
         molnatoms[i] = sr.Molecules[i].NAtoms;
     }
 }
Esempio n. 8
0
        public override SimulationResult Simulate()
        {
            // initial energy
            Elast = ForceAndTorque();
            CheckForClashes(true);
            Elast += Eclash;

            int ntotal       = 0; // current number of iterations
            int nsuccessfull = 0; // number of successfull iterations

            //const int nsuccessfull_max = 2000; // required number of successfull iterations between snapshots

            // simulate: main loop
            do
            {
                nsuccessfull += this.Iterate() ? 1 : 0;
                ntotal++;
            } while (nsuccessfull < this.SimulationParameters.MaxIterations);//while (ntotal < this.SimulationParameters.MaxIterations && nsuccessfull < nsuccessfull_max);

            // save result
            SimulationResult sr = new SimulationResult();

            for (int i = 0; i < translation.Length; i++)
            {
                this.rotation[i] = Matrix3.RepairRotation(rotation[i]);
            }
            sr.E = ForceAndTorque();
            CheckForClashes(true);
            sr.E += Eclash;
            sr.ParentStructure  = _originalstate.InternalNumber;
            sr.Eclash           = Eclash;
            sr.Ebond            = Ebond;
            sr.Converged        = (ntotal < this.SimulationParameters.MaxIterations);
            sr.SimulationMethod = _originalstate.SimulationMethod | SimulationMethods.MetropolisSampling;
            sr.Translation      = new Vector3[translation.Length];
            sr.Rotation         = new Matrix3[translation.Length];
            for (int i = 0; i < translation.Length; i++)
            {
                sr.Translation[i] = Matrix3.Transpose(rotation[0]) * (translation[i] - translation[0])
                                    - _molecules[i].CM + _molecules[0].CM;
                this.rotation[i] = Matrix3.RepairRotation(rotation[i]);
                sr.Rotation[i]   = Matrix3.RepairRotation(Matrix3.Transpose(rotation[0]) * rotation[i]);
            }
            sr.Molecules = _molecules;
            sr.CalculateBestFitTranslation(false);
            return(sr);
        }
Esempio n. 9
0
        /// <summary>
        /// Randomly redistributes subunits until no clashes are detected
        /// </summary>
        public override double SetState()
        {
            Vector3 tshake;
            Vector3 ushake;
            double  angleshake, uz, uxy, uphi;
            Matrix3 rotshake;
            int     lastshaked;

            PrepareSimulation();
            _originalstate = new SimulationResult()
            {
                InternalNumber = -1
            };

            for (int i = 0; i < _molecules.Count; i++)
            {
                translation[i] = new Vector3(0.0, 0.0, 0.0);
                rotation[i]    = Matrix3.E;
            }
            do
            {
                for (int i = 1; i < _molecules.Count; i++)
                {
                    lastshaked = 0;
                    if (!_molecules[i].Selected)
                    {
                        tshake          = (new Vector3(_rnd.NextDouble(), _rnd.NextDouble(), _rnd.NextDouble()) + (-0.5)) * 10.0;
                        translation[i] += tshake;
                        lastshaked      = i;
                        uz              = -1.0 + _rnd.NextDouble() * 2.0;
                        uxy             = Math.Sqrt(1.0 - uz * uz);
                        uphi            = _rnd.NextDouble() * 2.0 * Math.PI;
                        ushake          = new Vector3(uxy * Math.Cos(uphi), uxy * Math.Sin(uphi), uz);
                        angleshake      = 2.0 * Math.PI * _rnd.NextDouble();
                        rotshake        = Matrix3.Rotation(ushake, angleshake);
                        rotation[i]     = rotshake * rotation[i];
                    }
                    else
                    {
                        translation[i] = translation[lastshaked] + _molecules[i].CM - _molecules[lastshaked].CM;
                        rotation[i]    = rotation[lastshaked];
                    }
                }
            }while (CheckForClashes(false));
            return(ForceAndTorque());
        }
Esempio n. 10
0
        public override double SetState(SimulationResult sr)
        {
            // set model distances as "true"
            Distance     rmodeltmp;
            DistanceList rlisttmp = new DistanceList(this.Distances.Count);

            rlisttmp.AddRange(this.Distances);
            for (Int32 i = 0; i < rlisttmp.Count; i++)
            {
                rmodeltmp   = rlisttmp[i];
                rmodeltmp.R = sr.ModelDistance(this.LabelingPositions.Find(rmodeltmp.Position1),
                                               this.LabelingPositions.Find(rmodeltmp.Position2));
                rlisttmp[i] = rmodeltmp;
            }
            this.Distances = rlisttmp;

            return(base.SetState(sr));
        }
Esempio n. 11
0
        /// <summary>
        /// RMSD compared to another structure
        /// </summary>
        /// <param name="sr_other">another structure</param>
        /// <returns>rmsd value</returns>
        public Double RMSD(SimulationResult sr_other, Boolean bestfit, Boolean selected_only)
        {
            Double   sd = 0.0;
            Matrix3  U;
            Vector3  r, t;
            Int32    NAtomstotal = 0;
            Molecule m;

            if (bestfit)
            {
                this.CalculateBestFitRotation(sr_other, false);
            }

            for (Int32 i = 0; i < this.Molecules.Count; i++)
            {
                m = this.Molecules[i];
                if (selected_only && !m.Selected)
                {
                    continue;
                }
                if (bestfit)
                {
                    U = this.BestFitRotation * this.Rotation[i] - sr_other.Rotation[i];
                    t = this.BestFitRotation * (this.Translation[i] + m.CM + this.BestFitTranslation)
                        - sr_other.Translation[i] - m.CM - sr_other.BestFitTranslation;
                }
                else
                {
                    U = this.Rotation[i] - sr_other.Rotation[i];
                    t = this.Translation[i] - sr_other.Translation[i];
                }
                for (Int32 j = 0; j < m.NAtoms; j++)
                {
                    r   = new Vector3(m.XLocal[j], m.YLocal[j], m.ZLocal[j]);
                    sd += Vector3.SquareNormDiff(U * r, t);
                }
                NAtomstotal += m.NAtoms;
            }
            return(Math.Sqrt(sd / (Double)NAtomstotal));
        }
Esempio n. 12
0
        private void SaveEnergyTable(String efilename)
        {
            String           fname = this.filenametextBox.Text;
            SimulationResult tmp;

            using (StreamWriter sw = new StreamWriter(efilename, false))
            {
                _rmsdref = _vsref ? _rmsdref : _dataToSave[0];
                String rmsrefdtext = "RMSD vs " + _rmsdref.InternalNumber;
                sw.WriteLine("File\tchi2\tchi2_bond\tchi2_clash\tConvergence\tMethod\t" + "RMSD vs previous" +
                             '\t' + rmsrefdtext + '\t' + rmsrefdtext + " (selected)");
                tmp = _dataToSave[0];
                sw.WriteLine(String.Format("{0}{1}\t{2:F8}\t{3:F4}\t{4:F4}\t{5}\t{6}\t---\t{7:F6}\t{8:F6}", fname, tmp.InternalNumber,
                                           tmp.E, tmp.Ebond, tmp.Eclash, tmp.Converged, tmp.SimulationMethod, tmp.RMSD(_rmsdref, _bestfit), tmp.RMSD(_rmsdref, _bestfit, true)));
                for (Int32 i = 1; i < _dataToSave.Length; i++)
                {
                    tmp = _dataToSave[i];
                    sw.WriteLine(String.Format("{0}{1}\t{2:F8}\t{3:F4}\t{4:F4}\t{5}\t{6}\t{7:F6}\t{8:F6}\t{9:F6}", fname, tmp.InternalNumber,
                                               tmp.E, tmp.Ebond, tmp.Eclash, tmp.Converged, tmp.SimulationMethod,
                                               tmp.RMSD(_dataToSave[i - 1], _bestfit), tmp.RMSD(_rmsdref, _bestfit), tmp.RMSD(_rmsdref, _bestfit, true)));
                }
                sw.Close();
            }
        }
Esempio n. 13
0
 /// <summary>
 /// Sets an initial state of the simulation from an intermediate result. Must be overriden in the derived class.
 /// </summary>
 /// <param name="sr">Starting structure.</param>
 /// <returns>"Energy".</returns>
 public virtual double SetState(SimulationResult sr)
 {
     throw new NotImplementedException();
 }
Esempio n. 14
0
        private void SaveSimulationResult(SimulationResult sr, String fname, Boolean addpdbsave)
        {
            String  converged = sr.Converged ? " (converged)" : " (not converged)";
            String  fullfname = fname + ".pml";
            Vector3 u; Double theta;

            using (StreamWriter sw = new StreamWriter(fullfname, false))
            {
                sw.WriteLine("# Energy = " + sr.E.ToString("F8") + converged);
                for (Int32 i = 0; i < molecules.Count; i++)
                {
                    sw.WriteLine("load " + molecules[i].FullFileName);
                }
                for (Int32 i = 1; i < molecules.Count; i++)
                {
                    theta = Matrix3.AngleAndAxis(sr.Rotation[i], out u) * 180.0 / Math.PI;
                    sw.WriteLine("rotate [" + u.ToString() + "], " + theta.ToString("F3")
                                 + ", " + molecules[i].Name + ", origin=[" + molecules[i].CM.ToString() + "]");
                    sw.WriteLine("translate [" + sr.Translation[i].ToString() + "], " + molecules[i].Name);
                }
                if (_bestfit)
                {
                    sw.WriteLine("select all");
                    sw.WriteLine("translate [" + sr.BestFitTranslation.ToString() + "], sele");
                    theta = Matrix3.AngleAndAxis(sr.BestFitRotation, out u) * 180.0 / Math.PI;
                    sw.WriteLine("rotate [" + u.ToString() + "], " + theta.ToString("F3")
                                 + ", sele, origin=[0, 0, 0]");
                }
                if (addpdbsave)
                {
                    sw.WriteLine("select all");
                    sw.WriteLine("save " + fname + ".pdb, sele");
                }
                String lpnames = "";
                if (lpcheckBox.Checked)
                {
                    Int32            im;
                    Vector3          r;
                    LabelingPosition lpref;
                    foreach (LabelingPosition l in labelingpos)
                    {
                        im = molecules.FindIndex(l.Molecule);
                        if ((sr.SimulationMethod & SimulationMethods.Refinement) != 0)
                        {
                            lpref = sr.RefinedLabelingPositions.Find(l.Name);
                        }
                        else
                        {
                            lpref = l;
                        }
                        r = sr.Rotation[im] * (lpref - molecules[im].CM) + molecules[im].CM + sr.Translation[im];
                        sw.WriteLine("pseudoatom " + l.Name + ", pos=[" + r.ToString() + "]");
                        sw.WriteLine("label " + l.Name + ", \"" + l.Name + "\"");
                        sw.WriteLine("show spheres, " + l.Name);
                        if (l.Dye == DyeType.Donor)
                        {
                            sw.WriteLine("color green, " + l.Name);
                        }
                        if (l.Dye == DyeType.Acceptor)
                        {
                            sw.WriteLine("color red, " + l.Name);
                        }
                        lpnames += " + " + l.Name;
                    }
                }
                if (_bestfit && lpnames != String.Empty)
                {
                    sw.WriteLine("deselect");
                    sw.WriteLine("select " + lpnames.Substring(3));
                    sw.WriteLine("translate [" + sr.BestFitTranslation.ToString() + "], sele");
                    theta = Matrix3.AngleAndAxis(sr.BestFitRotation, out u) * 180.0 / Math.PI;
                    sw.WriteLine("rotate [" + u.ToString() + "], " + theta.ToString("F3")
                                 + ", sele, origin=[0, 0, 0]");
                }
                sw.WriteLine("deselect");
                sw.Close();
            }
        }
Esempio n. 15
0
        public override SimulationResult Simulate()
        {
            double   E, K, Etot, theta, abst, normF, normT;
            Molecule m;
            Vector3  tnorm;

            // select dt and do the first time step
            E = ForceAndTorque();
            CheckForClashes(true);
            E         = E + Eclash;
            dt        = dr4dt / Math.Sqrt(2.0 * Math.Max(E, Nmolecules) / minmass) * SimulationParameters.TimeStepFactor;
            rdt       = 1.0 / dt;
            Etot_last = E;

            double Mtot = 0.0, ktot = 0.0;

            for (int i = 0; i < Nmolecules; i++)
            {
                Mtot += _molecules[i].Mass;
            }
            for (int i = 0; i < kplus.Length; i++)
            {
                ktot += Math.Max(kplus[i], kminus[i]);
            }
            viscosity_per_dt = SimulationParameters.ViscosityFactor * 2.0 * dt * Math.Sqrt(ktot / Mtot);
            viscosity        = viscosity_per_dt * dt;
            viscosity        = Math.Max(minviscosity, viscosity);
            viscosity        = Math.Min(maxviscosity, viscosity);

            for (int i = 0; i < Nmolecules; i++)
            {
                m = _molecules[i];
                translationm1[i] = translation[i];
                rotationm1[i]    = rotation[i];

                translation[i] += force[i] * (0.5 / m.Mass) * dt * dt;

                abst         = Vector3.Abs(torque[i]);
                tnorm        = torque[i] * (1.0 / abst);
                theta        = 0.5 * dt * dt * abst / m.SimpleI;
                rotation[i] *= Matrix3.Rotation(tnorm, theta);
            }

            // main loop
            int niter = 0, maxniter;

            optimize_selected_now = (SimulationParameters.OptimizeSelected == OptimizeSelected.Selected ||
                                     SimulationParameters.OptimizeSelected == OptimizeSelected.SelectedThenAll);
            if (SimulationParameters.OptimizeSelected == OptimizeSelected.SelectedThenAll)
            {
                maxniter = SimulationParameters.MaxIterations / 2;
            }
            else
            {
                maxniter = SimulationParameters.MaxIterations;
            }
            do
            {
                E    = Iterate(out normF, out normT, out K);
                Etot = E + K;
                niter++;
                if ((niter << 28) == 0 || Eclash > 10.0 || Etot > Etot_last + maxdE)
                {
                    Cleanup(ref Etot);
                }
                Etot_last = Etot;
            }while (((normF > SimulationParameters.FTolerance) || (normT > SimulationParameters.TTolerance) ||
                     (K > SimulationParameters.KTolerance * Nmolecules)) && (niter < maxniter));

            // repeat if selected then all
            if (SimulationParameters.OptimizeSelected == OptimizeSelected.SelectedThenAll)
            {
                dt        = dr4dt / Math.Sqrt(2.0 * Math.Max(E, Nmolecules) / minmass) * SimulationParameters.TimeStepFactor;
                rdt       = 1.0 / dt;
                viscosity = SimulationParameters.ViscosityFactor * 2.0 * dt * Math.Sqrt(ktot / Mtot);
                viscosity = Math.Max(minviscosity, viscosity);
                viscosity = Math.Min(maxviscosity, viscosity);

                niter = 0;
                optimize_selected_now = false;
                do
                {
                    E    = Iterate(out normF, out normT, out K);
                    Etot = E + K;
                    niter++;
                    if ((niter << 28) == 0 || Eclash > 10.0 || Etot > Etot_last + maxdE)
                    {
                        Cleanup(ref Etot);
                    }
                    Etot_last = Etot;
                }while (((normF > SimulationParameters.FTolerance) || (normT > SimulationParameters.TTolerance) ||
                         (K > SimulationParameters.KTolerance * Nmolecules)) && (niter < maxniter));
            }

            SimulationResult sr = new SimulationResult();

            sr.E                = E;
            sr.Eclash           = Eclash;
            sr.Ebond            = Ebond;
            sr.Converged        = (niter < SimulationParameters.MaxIterations);
            sr.Translation      = new Vector3[Nmolecules];
            sr.Rotation         = new Matrix3[Nmolecules];
            sr.ParentStructure  = _originalstate.InternalNumber;
            sr.SimulationMethod = _originalstate.SimulationMethod | SimulationMethods.Docking;
            for (int i = 0; i < Nmolecules; i++)
            {
                sr.Translation[i] = Matrix3.Transpose(rotation[0]) * (translation[i] - translation[0])
                                    - _molecules[i].CM + _molecules[0].CM;
                sr.Rotation[i] = Matrix3.RepairRotation(Matrix3.Transpose(rotation[0]) * rotation[i]);
            }
            sr.Molecules = _molecules;
            sr.CalculateBestFitTranslation(false);
            return(sr);
        }
Esempio n. 16
0
 public Double RMSD(SimulationResult sr2, Boolean bestfit)
 {
     return(this.RMSD(sr2, bestfit, false));
 }