/// <summary> /// Worker method. /// </summary> protected override void WorkerMethod() { string[] vtkFiles = Directory.GetFiles(m_inputDir, "*.vtk"); if ((vtkFiles == null) || (vtkFiles.Length == 0)) { return; } m_outputDir = CreateOutputDir(m_inputDir); string paramsFile = Path.Combine(m_inputDir, "Params.txt"); if (File.Exists(paramsFile)) { File.Copy(paramsFile, Path.Combine(m_outputDir, Path.GetFileName(paramsFile))); } m_numFilesToProcess = vtkFiles.Length; int chunkSize = Environment.ProcessorCount; for (int iStart = 0; iStart < m_numFilesToProcess; iStart += chunkSize) { int iEnd = Math.Min(iStart + chunkSize, m_numFilesToProcess); Parallel.For(iStart, iEnd, i => { // Crop one file, and save the result string inputFile = vtkFiles[i]; ProbabilityDensity[] probs = ProbabilityDensity.ReadFromVtkFile(inputFile); probs[0] = Crop(probs[0], m_xminCrop, m_xmaxCrop, m_yminCrop, m_ymaxCrop); probs[1] = Crop(probs[1], m_xminCrop, m_xmaxCrop, m_yminCrop, m_ymaxCrop); string outFile = Path.Combine(m_outputDir, Path.GetFileName(inputFile)); ProbabilityDensity.SaveToVtkFile(probs, outFile); }); // Report progress to the caller m_currentFileIndex = iEnd - 1; ReportProgress(); if (IsCancelled) { return; } } }
/// <summary> /// Saves the current probability densities to a vtk file. /// </summary> private void SaveOutputFile(string fileSpec) { float time = m_currentTimeStepIndex * m_deltaT; ProbabilityDensity prob1 = GetSingleParticleProbability(1, time); if (prob1 == null) { return; } System.Diagnostics.Trace.WriteLine(prob1.Norm().ToString()); ProbabilityDensity prob2 = GetSingleParticleProbability(2, time); if (prob2 == null) { return; } System.Diagnostics.Trace.WriteLine(" " + prob2.Norm().ToString()); ProbabilityDensity.SaveToVtkFile(new ProbabilityDensity[] { prob1, prob2 }, fileSpec); }
/// <summary> /// Reads density values from a vtk stream. /// </summary> private static ProbabilityDensity FromVtkStream(BinaryReader br, int sizeX, int sizeY, float latticeSpacing) { ProbabilityDensity result = new ProbabilityDensity(sizeX, sizeY, latticeSpacing); unsafe { // For performance, we keep a temporary float value with pointers to its bytes float floatVal = 0.0f; byte *floatBytes0 = (byte *)(&floatVal); byte *floatBytes1 = floatBytes0 + 1; byte *floatBytes2 = floatBytes0 + 2; byte *floatBytes3 = floatBytes0 + 3; // Read the density values ReadTextLine(br); ReadTextLine(br); byte[] bytePlane = br.ReadBytes(sizeX * sizeY * 4); int n = 0; for (int y = 0; y < sizeY; y++) { float[] dataY = result.Data[y]; for (int x = 0; x < sizeX; x++) { *floatBytes3 = bytePlane[n]; *floatBytes2 = bytePlane[n + 1]; *floatBytes1 = bytePlane[n + 2]; *floatBytes0 = bytePlane[n + 3]; dataY[x] = floatVal; n += 4; } } } return(result); }
/// <summary> /// Crops a wavefunction. /// </summary> public static ProbabilityDensity Crop(ProbabilityDensity inputDensity, int xminCrop, int xmaxCrop, int yminCrop, int ymaxCrop) { float[][] modData = Crop(inputDensity.Data, xminCrop, xmaxCrop, yminCrop, ymaxCrop); return(new ProbabilityDensity(modData, inputDensity.LatticeSpacing)); }
/// <summary> /// Computes a single particle probability density by integrating over one of the particle coordinates. /// </summary> private ProbabilityDensity GetSingleParticleProbability(int particleIndex, double time) { int psx = m_gridSizeX; int psy = m_gridSizeY; // Precompute some constants we'll need double fm1 = m_mass1 / m_totalMass; double fm2 = m_mass2 / m_totalMass; double sigmaCmFactorX = Math.Pow(m_sigmaCm.X, 4) + (time * time) / (m_totalMass * m_totalMass); double sigmaCmFactorY = Math.Pow(m_sigmaCm.Y, 4) + (time * time) / (m_totalMass * m_totalMass); double RnormX = m_sigmaCm.X / Math.Sqrt(Math.PI * sigmaCmFactorX); double RnormY = m_sigmaCm.Y / Math.Sqrt(Math.PI * sigmaCmFactorY); Vec2 R0 = fm1 * m_initialPosition1 + fm2 * m_initialPosition2; Vec2 P0 = (m_initialMomentum1 + m_initialMomentum2); double RxOffset = R0.X + time * (P0.X / m_totalMass); double RyOffset = R0.Y + time * (P0.Y / m_totalMass); double RxScale = -(m_sigmaCm.X * m_sigmaCm.X) / sigmaCmFactorX; double RyScale = -(m_sigmaCm.Y * m_sigmaCm.Y) / sigmaCmFactorY; // Precompute the relative wavefunction probabilities ProbabilityDensity relDensity = m_visscherWf.ToRegularWavefunction().GetProbabilityDensity(); // Get a one-particle probability by marginalizing over the joint probability float[][] oneParticleProbs = TdseUtils.Misc.Allocate2DArray(psy, psx); if (particleIndex == 1) { for (int y1 = 0; y1 < psy; y1++) { // Precompute the center-of-mass wavefunction probabilities float[] YExp = new float[psy]; for (int y2 = 0; y2 < psy; y2++) { double RyArg = (fm1 * y1 + fm2 * y2) * m_latticeSpacing - RyOffset; YExp[y2] = (float)(RnormY * Math.Exp(RyScale * RyArg * RyArg)); } TdseUtils.Misc.ForLoop(0, psx, x1 => { float[] XExp = new float[psx]; for (int x2 = 0; x2 < psx; x2++) { double RxArg = (fm1 * x1 + fm2 * x2) * m_latticeSpacing - RxOffset; XExp[x2] = (float)(RnormX * Math.Exp(RxScale * RxArg * RxArg)); } float prob = 0.0f; for (int y2 = 0; y2 < psy; y2++) { float[] relProbsY = relDensity.Data[(y1 - y2) + psy - 1]; int xOffset = x1 + psx - 1; float sum = 0.0f; for (int x2 = 0; x2 < psx; x2++) { sum += XExp[x2] * relProbsY[xOffset - x2]; } prob += sum * YExp[y2]; } oneParticleProbs[y1][x1] = prob * (m_latticeSpacing * m_latticeSpacing); }, m_multiThread); CheckForPause(); if (IsCancelled) { return(null); } } } else { for (int y2 = 0; y2 < psy; y2++) { // Precompute the center-of-mass wavefunction probabilities float[] YExp = new float[psy]; for (int y1 = 0; y1 < psy; y1++) { double RyArg = (fm1 * y1 + fm2 * y2) * m_latticeSpacing - RyOffset; YExp[y1] = (float)(RnormY * Math.Exp(RyScale * RyArg * RyArg)); } TdseUtils.Misc.ForLoop(0, psx, x2 => { float[] XExp = new float[psx]; for (int x1 = 0; x1 < psx; x1++) { double RxArg = (fm1 * x1 + fm2 * x2) * m_latticeSpacing - RxOffset; XExp[x1] = (float)(RnormX * Math.Exp(RxScale * RxArg * RxArg)); } float prob = 0.0f; for (int y1 = 0; y1 < psy; y1++) { float[] relProbsY = relDensity.Data[(y1 - y2) + psy - 1]; int xOffset = -x2 + psx - 1; float sum = 0.0f; for (int x1 = 0; x1 < psx; x1++) { sum += XExp[x1] * relProbsY[x1 + xOffset]; } prob += sum * YExp[y1]; } oneParticleProbs[y2][x2] = prob * (m_latticeSpacing * m_latticeSpacing); }, m_multiThread); CheckForPause(); if (IsCancelled) { return(null); } } } return(new ProbabilityDensity(oneParticleProbs, m_visscherWf.LatticeSpacing)); }