Ejemplo n.º 1
0
        public bool StartProc()
        {
            if (tmp == null)
            {
                return(false);
            }
            uint MemRequested, MemLockRequested;

            t = new Thread(new ThreadStart(tmp.ForeWork));
            t.Start();
            t.Join();
            if (!tmp.ForeWorkResult)
            {
                return(false);
            }
            MemRequested     = tmp.MemRequested();
            MemLockRequested = tmp.MemLockRequested();

            MemMan.Compact();
            MemMan.MEMORYSTATUSEX MS;
            MS = MemMan.MemoryStatus();
            try
            {
                MemMan.curProc.MaxWorkingSet = (IntPtr)MS.TotalPhys;
                MemMan.curProc.MinWorkingSet = (IntPtr)(MemLockRequested + 0x800000);
            }
            catch (Exception e)
            {
                Console.WriteLine("\n>>>>>>> " + e.Message);
                Console.WriteLine(">>>>>>> Can't take the ownership on " + ((MemLockRequested + 0x800000) / (1024 * 1024)).ToString("####0.0") + "Mb RAM");
                Console.WriteLine(">>>>>>> Probably the progamm is running not under an administrative account,\n>>>>>>> or too many other applications are running");
                Console.WriteLine(">>>>>>> or too many other applications are running.");
                Console.WriteLine(">>>>>>> Calculations will continue, but may slow down.");
            }


            if (!MM.Allocate(MemRequested))
            {
                Console.WriteLine(ExI + " ----> virtual allocation failed for " + MemRequested.ToString() + " bytes");
                return(false);
            }

            // optionally lock the memory
            if (!MM.Lock(0, MemLockRequested))
            {
                Console.WriteLine(">>>>>>> Unable to lock the memory.");
                Console.WriteLine(">>>>>>> Calculations will continue, but will slow down.");
            }
            unsafe
            {
                tmp.data = (double *)MM.BaseAddress;
            }
            t = new Thread(new ThreadStart(tmp.StartCalculations));
            t.Start();
            t.Join();
            MM.Free();
            return(true);
        }
Ejemplo n.º 2
0
        override public bool RunCalculation()
        {
            float    progress;
            DateTime start;
            TimeSpan period;
            double   StartThickness = CurThickness;

            int i, iSlice = 0, iWave = 0;

            if ((ThWaves == null) || (ThWaves.Length == 0))
            {
                return(true);
            }
            if ((double)ThWaves[iWave] <= CurThickness)
            {
                bool result = ((LastSavedWave = SaveWav(CurThickness)) != null);
                iWave++;
                Console.WriteLine("\rWave at " + CurThickness.ToString("######0.00") + "A unloaded " + result + "                             ");
            }
            if ((PotType == MSProject.PotTypes.PG_curve_TV_pullZ) || (PotType == MSProject.PotTypes.PG_curve_DW_pullZ))
            {
                hanwidth       = 8;
                hanning        = new double[hanwidth];
                nn             = Math.Min(N, (uint)Math.Pow(2, Math.Ceiling(Math.Log(((atrad + 0.5) / a * N + hanwidth) / (1.0 - 2.0 / hanwidth), 2)) + 1));
                mm             = Math.Min(M, (uint)Math.Pow(2, Math.Ceiling(Math.Log(((atrad + 0.5) / b * M + hanwidth) / (1.0 - 2.0 / hanwidth), 2)) + 1));
                atSFT_forward  = new SFT();
                atSFT_backward = new SFT();
                atM            = new MemMan();
                if (!atM.Allocate(nn * mm * 2 * sizeof(double)))
                {
                    return(false);
                }
                if (!atM.Lock(0, nn * mm * 2 * sizeof(double)))
                {
                    return(false);
                }
                buf = (double *)atM.LockAddress;
                atSFT_forward.MakePlan(buf, nn, mm, 1);
                atSFT_backward.MakePlan(buf, nn, mm, -1);
                lambda  = 0.387818 / Math.Sqrt(E / 1000.0 * (1.0 + 0.978459e-3 * E / 1000));   // E in V
                K       = lambda * Math.Sqrt(1.0 + Math.Pow((0.0242621 / lambda), 2));
                nninner = (uint)Math.Min(nn, (uint)(atrad) / a * N * 2 + Math.Max(1, nn / 2 - hanwidth - (uint)(atrad) / a * N));
                mminner = (uint)Math.Min(mm, (uint)(atrad) / b * M * 2 + Math.Max(1, mm / 2 - hanwidth - (uint)(atrad) / b * M));
//				nninner=(uint)Math.Min(nn,(uint)(atrad)/a*N*2) ;
//				mminner=(uint)Math.Min(mm,(uint)(atrad)/b*M*2) ;
                offix = (int)(nn - nninner);
                offiy = (int)(mm - mminner) / 2;
                for (i = 0; i < hanwidth; i++)
                {
                    hanning[i] = (Math.Cos(i * Math.PI / hanwidth) + 1) * 0.5;
                }
            }

            start = DateTime.Now;
            while (iWave < ThWaves.Length)
            {
                iSlice = (int)(iSlice % NSlices) + 1;


                // Load Slice
                if (!LoadSlice(iSlice))
                {
                    return(false);
                }
                // apply Phase Gratting
                if (NAtoms > 0)
                {
                    if ((PotType == MSProject.PotTypes.PG_DT_TV_periodic) ||
                        (PotType == MSProject.PotTypes.PG_DT_TV_cut) ||
                        (PotType == MSProject.PotTypes.PG_curve_TV_periodic) ||
                        (PotType == MSProject.PotTypes.PG_curve_TV_cut) ||
                        (PotType == MSProject.PotTypes.PG_curve_TV_pullZ))
                    {                           // некорректно работает с gam!=90
                        MusliRandom vibr = new MusliRandom();
                        for (i = 0; i < NAtoms; i++)
                        {
                            x[i]       *= a; y[i] *= b;
                            vibr.StDevX = vibr.StDevY = Math.Sqrt(DW[i] * 0.5) / (2.0 * Math.PI);
                            vibr.Vobble(ref x[i], ref y[i]);
                            if (PotType == MSProject.PotTypes.PG_curve_TV_pullZ)
                            {
                                z[i] += vibr.NextGauss();
                            }
                            if (x[i] < 0)
                            {
                                x[i] += a;
                            }
                            if (x[i] >= a)
                            {
                                x[i] -= a;
                            }
                            if (y[i] < 0)
                            {
                                y[i] += b;
                            }
                            if (y[i] >= b)
                            {
                                y[i] -= b;
                            }
                        }
                    }

                    fixed(double *_x = x, _y = y, _dw = DW, _ads = ads, _occ = occ)
                    {
                        fixed(int *_type_ = type_, _type = type)
                        {
                            switch (PotType)
                            {
                            case MSProject.PotTypes.PG_DT_DW_periodic:
                                PG_rs_DT_DW_periodic(a, b, gam, E, atrad, N, M, NAtoms, _x, _y, _dw, _ads, _occ, _type, data, &progress);
                                break;

                            case MSProject.PotTypes.PG_DT_DW_cut:
                                PG_rs_DT_DW_cutpot(a, b, gam, E, atrad, N, M, NAtoms, _x, _y, _dw, _ads, _occ, _type, data, &progress);
                                break;

                            case MSProject.PotTypes.PG_curve_DW_periodic:
                                PG_rs_curve_DW_periodic(a, b, gam, E, atrad, N, M, NAtoms, _x, _y, _ads, _occ, _type_, CurveHolder, CurveLength, dela, data, &progress);
                                break;

                            case MSProject.PotTypes.PG_curve_DW_cut:
                                PG_rs_curve_DW_cutpot(a, b, gam, E, atrad, N, M, NAtoms, _x, _y, _ads, _occ, _type_, CurveHolder, CurveLength, dela, data, &progress);
                                break;

                            case MSProject.PotTypes.PG_DT_TV_periodic:
                                PG_rs_DT_TV_periodic(a, b, gam, E, atrad, N, M, NAtoms, _x, _y, _ads, _occ, _type, data, &progress);
                                break;

                            case MSProject.PotTypes.PG_DT_TV_cut:
                                PG_rs_DT_TV_cutpot(a, b, gam, E, atrad, N, M, NAtoms, _x, _y, _ads, _occ, _type, data, &progress);
                                break;

                            case MSProject.PotTypes.PG_curve_TV_periodic:
                                PG_rs_curve_TV_periodic(a, b, gam, E, atrad, N, M, NAtoms, _x, _y, _ads, _occ, _type_, CurveHolder, CurveLength, dela, data, &progress);
                                break;

                            case MSProject.PotTypes.PG_curve_TV_cut:
                                PG_rs_curve_TV_cutpot(a, b, gam, E, atrad, N, M, NAtoms, _x, _y, _ads, _occ, _type_, CurveHolder, CurveLength, dela, data, &progress);
                                break;

                            case MSProject.PotTypes.PG_curve_DW_pullZ:
                                PG_rs_pullZ();
                                break;

                            case MSProject.PotTypes.PG_curve_TV_pullZ:
                                PG_rs_pullZ();
                                break;

                            default:
                                Console.WriteLine("Phase Gratting " + PotType.ToString() + " is not supported in " + this.ToString());
                                break;
                            }
                        }
                    }
                }

                //Apply Propagator
                SFT_direct.MakeSFT(data);
                ApplyPropagator(a, b, gam, CurSliThickness, E, X_tilt_crystal, Y_tilt_crystal, N, M, data, &progress);
                CurThickness += CurSliThickness;
                if ((double)ThWaves[iWave] <= CurThickness)
                {
                    if (IsSaveSTEM)
                    {
                        SaveSTEM(iWave);
                    }

                    if (IsSaveDiffractions)
                    {
                        Console.WriteLine("\rDiffraction at " + CurThickness.ToString("######0.00") + "A unloaded " + (SaveDiffraction(CurThickness) != null) + "                                   ");
                    }
                }
                SFT_backward.MakeSFT(data);
                period = DateTime.Now - start;
                if (!IsSaveSTEM)
                {
                    Console.Write("\rPropagated " + NAtoms + " atoms, at total " + CurThickness.ToString("######0.00") + "A at a speed of " + ((double)((CurThickness - StartThickness) * TimeSpan.TicksPerSecond / period.Ticks * 3600)).ToString("###0.0") + "A/h     ");
                }
                if ((double)ThWaves[iWave] <= CurThickness)
                {
                    iWave++;
                    if (IsSaveWaves)
                    {
                        Console.WriteLine("\rWave at " + CurThickness.ToString("######0.00") + "A unloaded " + ((LastSavedWave = SaveWav(CurThickness)) != null) + "                                   ");
                    }
                }
            }             // calculation cycle
            return(true);
        }