private void SimulateTEM(ref ProgressReporter progressReporter, ref Stopwatch timer, ref CancellationToken ct) { mCL.initialiseCTEMSimulation(CurrentResolution, SimRegion.xStart, SimRegion.yStart, SimRegion.xFinish, SimRegion.yFinish, isFull3D, isFD, dz, integrals); // Reset atoms incase TDS has been used mCL.sortStructure(false); // Use Background worker to progress through each step var NumberOfSlices = 0; mCL.getNumberSlices(ref NumberOfSlices, isFD); // Seperate into setup, loop over slices and final steps to allow for progress reporting. for (var i = 1; i <= NumberOfSlices; i++) { if (ct.IsCancellationRequested == true) { break; } timer.Start(); mCL.doMultisliceStep(i, NumberOfSlices); timer.Stop(); var mem = mCL.getCLMemoryUsed(); // Report progress of the work. float ms = timer.ElapsedMilliseconds; progressReporter.ReportProgress((val) => { CancelButton.IsEnabled = true; UI_UpdateSimulationProgress(ms, NumberOfSlices, 1, 1, i, mem); }, i); } }
private void SimulateCBED(int TDSruns, ref ProgressReporter progressReporter, ref Stopwatch timer, ref CancellationToken ct) { mCL.initialiseSTEMSimulation(CurrentResolution, SimRegion.xStart, SimRegion.yStart, SimRegion.xFinish, SimRegion.yFinish, isFull3D, isFD, dz, integrals, 1); //int posX = CurrentResolution / 2; //int posY = CurrentResolution / 2; var posx = (CBED_xpos - SimRegion.xStart) / pixelScale; var posy = (CBED_ypos - SimRegion.yStart) / pixelScale; // Use Background worker to progress through each step var NumberOfSlices = 0; mCL.getNumberSlices(ref NumberOfSlices, isFD); // Seperate into setup, loop over slices and final steps to allow for progress reporting. var runs = 1; if (doTDS_CBED) { runs = TDSruns; } TDSImage = new float[CurrentResolution * CurrentResolution]; for (var j = 0; j < runs; j++) { mCL.sortStructure(doTDS_CBED); mCL.initialiseSTEMWaveFunction(posx, posy, 1); for (var i = 1; i <= NumberOfSlices; i++) { if (ct.IsCancellationRequested == true) { break; } timer.Start(); mCL.doMultisliceStep(i, NumberOfSlices); timer.Stop(); var mem = mCL.getCLMemoryUsed(); float ms = timer.ElapsedMilliseconds; // Report progress of the work. progressReporter.ReportProgress((val) => { CancelButton.IsEnabled = true; // Note: code passed to "ReportProgress" can access UI elements freely. UI_UpdateSimulationProgress(ms, NumberOfSlices, runs, j, i, mem); }, i); } // After a complete run if TDS need to sum up the DIFF... //mCL.AddTDSDiffImage(TDSImage, CurrentResolution); mCL.getDiffImage(TDSImage, CurrentResolution); // Sum it in C++ also for the stem pixel measurement... //mCL.AddTDS(); if (ct.IsCancellationRequested == true) { break; } progressReporter.ReportProgress((val) => { CancelButton.IsEnabled = false; UpdateTDSImage(); }, j); } }
private void SimulateSTEM(int TDSruns, ref ProgressReporter progressReporter, ref Stopwatch timer, ref CancellationToken ct, int multistem) { LockedDetectors = Detectors; LockedArea = STEMRegion; foreach (DetectorItem dt in LockedDetectors) { dt.PixelScaleX = LockedArea.getxInterval; dt.PixelScaleY = LockedArea.getyInterval; dt.SetPositionReadoutElements(ref LeftXCoord, ref LeftYCoord); } if (LockedDetectors.Count == 0) { var result = MessageBox.Show("No Detectors Have Been Set", "", MessageBoxButton.OK, MessageBoxImage.Error); return; } int numPix = LockedArea.xPixels * LockedArea.yPixels; int pix = 0; foreach (DetectorItem i in LockedDetectors) { i.ImageData = new float[numPix]; i.Min = float.MaxValue; i.Max = float.MinValue; } int runs = 1; if (doTDS_STEM) { runs = TDSruns; } int totalPix = numPix * runs; mCL.initialiseSTEMSimulation(CurrentResolution, SimRegion.xStart, SimRegion.yStart, SimRegion.xFinish, SimRegion.yFinish, isFull3D, isFD, dz, integrals, multistem); float xInterval = LockedArea.getxInterval; float yInterval = LockedArea.getyInterval; //List<float[]> TDSImages = new List<float[]>(); List <float> fCoordxs = new List <float>(); List <Tuple <Int32, Int32> > Pixels = new List <Tuple <Int32, Int32> >(); for (int posY = 0; posY < LockedArea.yPixels; posY++) { for (int posX = 0; posX < LockedArea.xPixels; posX++) { Pixels.Add(new Tuple <Int32, Int32>(posX, posY)); } } for (int j = 0; j < runs; j++) { Shuffler.Shuffle <Tuple <Int32, Int32> >(Pixels); // Reset image contrast limits for every run.... foreach (DetectorItem i in LockedDetectors) { i.Min = float.MaxValue; i.Max = float.MinValue; } //for (int posY = 0; posY < LockedArea.yPixels * LockedArea.xPixels; posY+=multistem) // won't loop over end? //{ int posY = 0; int conPix = multistem; // numPix // multistem while (posY < numPix) { mCL.sortStructure(doTDS_STEM); int thisPosY = posY; if (posY + multistem > numPix && posY + multistem - numPix + 1 < multistem) { conPix = numPix - posY; posY = numPix; } else { posY += multistem; } for (int i = 1; i <= conPix; i++) { mCL.initialiseSTEMWaveFunction(((LockedArea.xStart + Pixels[(thisPosY + i - 1)].Item1 * xInterval - SimRegion.xStart) / pixelScale), ((LockedArea.yStart + Pixels[(thisPosY + i - 1)].Item2 * yInterval - SimRegion.yStart) / pixelScale), i); } // Use Background worker to progress through each step int NumberOfSlices = 0; mCL.getNumberSlices(ref NumberOfSlices, isFD); // Seperate into setup, loop over slices and final steps to allow for progress reporting. for (int i = 1; i <= NumberOfSlices; i++) { if (ct.IsCancellationRequested == true) { break; } timer.Start(); mCL.doMultisliceStep(i, NumberOfSlices, conPix); timer.Stop(); int mem = mCL.getCLMemoryUsed(); float ms = timer.ElapsedMilliseconds; progressReporter.ReportProgress((val) => { CancelButton.IsEnabled = true; // Note: code passed to "ReportProgress" can access UI elements freely. UI_UpdateSimulationProgressSTEM(ms, totalPix, pix, NumberOfSlices, i, mem); }, i); } pix += conPix; if (ct.IsCancellationRequested == true) { break; } for (int i = 1; i <= conPix; i++) { // After a complete run if TDS need to sum up the DIFF... //mCL.AddTDSDiffImage(TDSImages[i-1], CurrentResolution,i); // Sum it in C++ also for the stem pixel measurement... //mCL.AddTDS(i); mCL.getSTEMDiff(i); } progressReporter.ReportProgress((val) => { CancelButton.IsEnabled = false; //UpdateTDSImage(); }, j); for (int p = 1; p <= conPix; p++) { // loop through and get each STEM pixel for each detector at the same time foreach (DetectorItem i in LockedDetectors) { float pixelVal = mCL.getSTEMPixel(i.Inner, i.Outer, i.xCentre, i.yCentre, p); float newVal = i.ImageData[LockedArea.xPixels * Pixels[thisPosY + p - 1].Item2 + Pixels[thisPosY + p - 1].Item1] + pixelVal; i.ImageData[LockedArea.xPixels * Pixels[thisPosY + p - 1].Item2 + Pixels[thisPosY + p - 1].Item1] = newVal; //if (j == runs-1) // Only use final values to set contrast limits //{ if (newVal < i.Min) { i.Min = newVal; } if (newVal > i.Max) { i.Max = newVal; } //} } } // This will update display after each tds run... progressReporter.ReportProgress((val) => { foreach (DetectorItem i in LockedDetectors) { UpdateDetectorImage(i); } }, thisPosY); if (ct.IsCancellationRequested == true) { break; } } // Reset TDS arrays after pixel values retrieved //mCL.ClearTDS(multistem); //fCoordxs.Clear(); if (ct.IsCancellationRequested == true) { break; } } }