From df9a521d720dae214556bc18ebb47a396b91eef1 Mon Sep 17 00:00:00 2001 From: Jay Jay Billings Date: Fri, 6 Feb 2015 16:02:28 -0500 Subject: [PATCH] Finished the port of ExtResFixedLambda. Signed-off-by: Jay Jay Billings --- .../reflectivity/ReflectivityCalculator.java | 81 ++++++++++++++++++- .../test/ReflectivityCalculatorTester.java | 70 +++++++++++++--- 2 files changed, 138 insertions(+), 13 deletions(-) diff --git a/src/org.eclipse.ice.reflectivity/src/org/eclipse/ice/reflectivity/ReflectivityCalculator.java b/src/org.eclipse.ice.reflectivity/src/org/eclipse/ice/reflectivity/ReflectivityCalculator.java index 500b71c8c..8cbea0ffb 100644 --- a/src/org.eclipse.ice.reflectivity/src/org/eclipse/ice/reflectivity/ReflectivityCalculator.java +++ b/src/org.eclipse.ice.reflectivity/src/org/eclipse/ice/reflectivity/ReflectivityCalculator.java @@ -138,7 +138,7 @@ public void convolute(double[] waveVector, double delQ0, double delQ1oQ, double wavelength, int numPoints, int numLowPoints, int numHighPoints, double[] refFit) { - double log2 = Math.log(2.0); + double ln2 = Math.log(2.0); double qEff = 0.0, qRes = 0.0, rExp = 0.0, rNorm = 0.0; double[] refTemp = new double[maxPoints]; int nStep = 0; @@ -153,7 +153,7 @@ public void convolute(double[] waveVector, double delQ0, double delQ1oQ, qEff = waveVector[i]; } double qDel = delQ0 + qEff * delQ1oQ; - double twSgSq = 2.0 * qDel * qDel / (8.0 * log2); + double twSgSq = 2.0 * qDel * qDel / (8.0 * ln2); if (twSgSq < 1.0e-10) { twSgSq = 1.0e-10; } @@ -208,4 +208,81 @@ public void convolute(double[] waveVector, double delQ0, double delQ1oQ, return; } + /** + * This operation calculates the length of the low-Q extension of the data + * to be convoluted with the delt-Q full-width half-maximum Gaussian + * resolution function. + * + * @param q + * the wave vector (Q) plus additional space for the convolution. + * This array should have length = numPoints + numLowPoints. + * @param delQ0 + * the zeroth order term of a Taylor expansion of the + * reflectometer resolution function dQ = dQ_0 + (dQ/Q)_1 x Q + + * ... + * @param delQ1oQ + * the zeroth order term of the Q resolution Taylor expansion + * @param numPoints + * the number of points in the wave vector + * @return numLowPoints the number of points in the low-Q extension to q + * used for convolution of the data with the resolution function. + * Returned by ExtResFixedLambda. + */ + public int getLowExtensionLength(double[] waveVector, double delQ0, + double delQ1oQ, int numPoints) { + + double ln2 = Math.log(2.0); + + // Determine the loq-Q extension + double qDel = delQ0 + waveVector[0] * delQ1oQ; + double qStep = waveVector[1] - waveVector[0]; + double twSgSq = Math.max(2.0 * qDel * qDel / (8.0 * ln2), 1.0e-10); + int numLowPoints = 0; + double qR = 0.0; + while (qR * qR / twSgSq <= 6.908) { + numLowPoints++; + qR = qR + qStep; + } + + return numLowPoints; + } + + /** + * This operation calculates the length of the high-Q extension of the data + * to be convoluted with the delt-Q full-width half-maximum Gaussian + * resolution function. + * + * @param q + * the wave vector (Q) plus additional space for the convolution. + * This array should have length = numPoints + numLowPoints. + * @param delQ0 + * the zeroth order term of a Taylor expansion of the + * reflectometer resolution function dQ = dQ_0 + (dQ/Q)_1 x Q + + * ... + * @param delQ1oQ + * the zeroth order term of the Q resolution Taylor expansion + * @param numPoints + * the number of points in the wave vector + * @return numHighPoints the number of points in the high-Q extension to q + * used for convolution of the data with the resolution function. + * Returned by ExtResFixedLambda. + */ + public int getHighExtensionLength(double[] waveVector, double delQ0, + double delQ1oQ, int numPoints) { + + double ln2 = Math.log(2.0); + + // Determine the high-Q extension + double qDel = delQ0 + waveVector[numPoints - 1] * delQ1oQ; + double qStep = waveVector[numPoints - 1] - waveVector[numPoints - 2]; + double twSgSq = 2.0 * qDel * qDel / (8.0 * ln2); + int numHighPoints = 0; + double qR = 0.0; + while (qR * qR / twSgSq <= 6.908) { + numHighPoints++; + qR = qR + qStep; + } + + return numHighPoints; + } } diff --git a/tests/org.eclipse.ice.reflectivity.test/src/org/eclipse/ice/reflectivity/test/ReflectivityCalculatorTester.java b/tests/org.eclipse.ice.reflectivity.test/src/org/eclipse/ice/reflectivity/test/ReflectivityCalculatorTester.java index 16ad2f290..c555b0a56 100644 --- a/tests/org.eclipse.ice.reflectivity.test/src/org/eclipse/ice/reflectivity/test/ReflectivityCalculatorTester.java +++ b/tests/org.eclipse.ice.reflectivity.test/src/org/eclipse/ice/reflectivity/test/ReflectivityCalculatorTester.java @@ -104,6 +104,53 @@ public static void setUpBeforeClass() throws Exception { } + /** + * This class tests + * {@link ReflectivityCalculator#getExtensionLengths(double[], double, double, double, int, int, int)} + * . + */ + @Test + public void testGetExtensionLengths() { + // Load the convolution file + Form form = reader.read(project.getFile("getExtensionLengths.csv")); + ListComponent lines = (ListComponent) form + .getComponent(1); + assertEquals(403, lines.size()); + + // Get the parameters and reference output values + String line[] = lines.get(0); + double delQ0 = Double.valueOf(line[0]); + double delQ1oQ = Double.valueOf(line[1]); + double wavelength = Double.valueOf(line[2]); + int numPoints = Integer.valueOf(line[3]); + int refNumLowPoints = Integer.valueOf(line[4]); + int refNumHighPoints = Integer.valueOf(line[5]); + + // Load the q array. They are padded by numLowPoints for the + // convolution. + double[] waveVector = new double[lines.size() - 1]; + for (int i = 0; i < lines.size() - 1; i++) { + line = lines.get(i + 1); + waveVector[i] = Double.valueOf(line[0]); + } + assertEquals(numPoints, waveVector.length); + assertEquals(4.401373320E-01, waveVector[numPoints - 1], 0.0); + + // Call the function + int numLowPoints = 0, numHighPoints = 0; + ReflectivityCalculator calculator = new ReflectivityCalculator(); + numLowPoints = calculator.getLowExtensionLength(waveVector, delQ0, delQ1oQ, + numPoints); + numHighPoints = calculator.getHighExtensionLength(waveVector, delQ0, delQ1oQ, + numPoints); + + // Check the high and low extension lengths + assertEquals(refNumLowPoints, numLowPoints); + assertEquals(refNumHighPoints, numHighPoints); + + return; + } + /** * This class tests * {@link ReflectivityCalculator#convolute(double[], double, double, double, int, int, double[])} @@ -128,8 +175,8 @@ public void testConvolute() { // Load the q and refFit arrays. They are padded by numLowPoints for the // convolution. - double[] waveVector = new double[lines.size()-1]; - double[] refRefFit = new double[lines.size()-1]; // Reference! + double[] waveVector = new double[lines.size() - 1]; + double[] refRefFit = new double[lines.size() - 1]; // Reference! for (int i = 0; i < lines.size() - 1; i++) { line = lines.get(i + 1); waveVector[i] = Double.valueOf(line[0]); @@ -140,13 +187,13 @@ public void testConvolute() { // Load the tiles form = reader.read(project.getFile("getSpecRefSqrdMod_q841.csv")); - ListComponent tileLines = (ListComponent) form + ListComponent tileLines = (ListComponent) form .getComponent(1); Tile[] tiles = loadTiles(tileLines); assertEquals(173, tiles.length); - + // Compute the initial value of refFit - double[] refFit = new double[lines.size()-1]; + double[] refFit = new double[lines.size() - 1]; double qEff = 0.0; ReflectivityCalculator calculator = new ReflectivityCalculator(); for (int i = 0; i < numHighPoints + numLowPoints + numPoints; i++) { @@ -155,15 +202,15 @@ public void testConvolute() { } else { qEff = waveVector[i]; } - refFit[i] = calculator.getModSqrdSpecRef(qEff,wavelength,tiles); - System.out.println(i + " " + refFit[i]); + refFit[i] = calculator.getModSqrdSpecRef(qEff, wavelength, tiles); } - + // Do the convolution and check the result calculator.convolute(waveVector, delQ0, delQ1oQ, wavelength, numPoints, numLowPoints, numHighPoints, refFit); for (int i = 0; i < refFit.length; i++) { - assertEquals(refRefFit[i], refFit[i], Math.abs(refRefFit[i])/1.0e-4); + assertEquals(refRefFit[i], refFit[i], + Math.abs(refRefFit[i]) / 1.0e-4); } return; @@ -201,7 +248,8 @@ public void testGetSpecRefSqrdMod() { System.out.println(specRefSqrd + " " + expectedSpecRefSqrd); System.out.println("RERR = " + (specRefSqrd - expectedSpecRefSqrd) / expectedSpecRefSqrd); - assertEquals(expectedSpecRefSqrd, specRefSqrd, Math.abs(expectedSpecRefSqrd)/1.0e-4); + assertEquals(expectedSpecRefSqrd, specRefSqrd, + Math.abs(expectedSpecRefSqrd) / 1.0e-4); // Get the two single parameters and the final result out of the data // for the second test case @@ -220,7 +268,7 @@ public void testGetSpecRefSqrdMod() { System.out.println("RERR = " + (specRefSqrd - expectedSpecRefSqrd) / expectedSpecRefSqrd); assertEquals(expectedSpecRefSqrd, specRefSqrd, - Math.abs(expectedSpecRefSqrd/1.0e-4)); + Math.abs(expectedSpecRefSqrd / 1.0e-4)); return; }