diff --git a/src/org.eclipse.ice.item/src/org/eclipse/ice/item/model/Model.java b/src/org.eclipse.ice.item/src/org/eclipse/ice/item/model/Model.java index 6bc0cc2fa20947cbcbd3a6da5a5bbccd4acabb17..13678018a126213ad2aa80019a2690cdd0cf67ca 100644 --- a/src/org.eclipse.ice.item/src/org/eclipse/ice/item/model/Model.java +++ b/src/org.eclipse.ice.item/src/org/eclipse/ice/item/model/Model.java @@ -65,4 +65,20 @@ public class Model extends Item { return materialsDatabase; } + /* + * (non-Javadoc) + * + * @see org.eclipse.ice.item.Item#copy(org.eclipse.ice.item.Item) + */ + @Override + public void copy(Item item) { + super.copy(item); + // Call setup Form with services to allow any databases that are used by + // various systems to be properly accessed and registered. For example, + // some models may need to do this because they use the material + // database in a ListComponent. + setupFormWithServices(); + + } + } \ No newline at end of file diff --git a/src/org.eclipse.ice.reflectivity/META-INF/MANIFEST.MF b/src/org.eclipse.ice.reflectivity/META-INF/MANIFEST.MF index 893c947f61f317c3fef21fd8e283d2f0330ccb98..33276d88267cb8fadb14f9da847bc925967679ca 100644 --- a/src/org.eclipse.ice.reflectivity/META-INF/MANIFEST.MF +++ b/src/org.eclipse.ice.reflectivity/META-INF/MANIFEST.MF @@ -8,9 +8,12 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.7 Import-Package: org.apache.commons.math;version="2.1.0", org.apache.commons.math.complex;version="2.1.0", org.apache.commons.math.special;version="2.1.0", + org.eclipse.ice.io.csv, + org.eclipse.ice.io.serializable, org.eclipse.ice.item, org.eclipse.ice.item.model, - org.eclipse.ice.materials + org.eclipse.ice.materials, + org.eclipse.ui.forms.widgets Require-Bundle: org.eclipse.ice.datastructures;bundle-version="2.0.0", org.eclipse.core.resources;bundle-version="3.8.101", org.eclipse.core.runtime;bundle-version="3.9.100" diff --git a/src/org.eclipse.ice.reflectivity/src/org/eclipse/ice/reflectivity/ReflectivityModel.java b/src/org.eclipse.ice.reflectivity/src/org/eclipse/ice/reflectivity/ReflectivityModel.java index 39bc19234c08d92b27f1aa10ec236af8bd035a14..2d6cc657395732da975026d326ff5d4b6b84c008 100644 --- a/src/org.eclipse.ice.reflectivity/src/org/eclipse/ice/reflectivity/ReflectivityModel.java +++ b/src/org.eclipse.ice.reflectivity/src/org/eclipse/ice/reflectivity/ReflectivityModel.java @@ -8,12 +8,14 @@ * Contributors: * Initial API and implementation and/or initial documentation - Jay Jay Billings, * Jordan H. Deyton, Dasha Gorin, Alexander J. McCaskey, Taylor Patterson, - * Claire Saunders, Matthew Wang, Anna Wojtowicz + * Claire Saunders, Matthew Wang, Anna Wojtowicz, Kasper Gammeltoft *******************************************************************************/ package org.eclipse.ice.reflectivity; +import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import javax.xml.bind.annotation.XmlRootElement; @@ -25,10 +27,13 @@ import org.eclipse.ice.datastructures.ICEObject.ListComponent; import org.eclipse.ice.datastructures.form.AllowedValueType; import org.eclipse.ice.datastructures.form.DataComponent; import org.eclipse.ice.datastructures.form.Entry; +import org.eclipse.ice.datastructures.form.Form; import org.eclipse.ice.datastructures.form.FormStatus; import org.eclipse.ice.datastructures.form.Material; import org.eclipse.ice.datastructures.form.ResourceComponent; +import org.eclipse.ice.datastructures.resource.ICEResource; import org.eclipse.ice.datastructures.resource.VizResource; +import org.eclipse.ice.io.csv.CSVReader; import org.eclipse.ice.item.model.Model; import org.eclipse.ice.materials.IMaterialsDatabase; import org.eclipse.ice.materials.MaterialWritableTableFormat; @@ -38,7 +43,7 @@ import org.eclipse.ice.materials.MaterialWritableTableFormat; * layered on top of each other. It... * - * @author Jay Jay Billings, Alex McCaskey + * @author Jay Jay Billings, Alex McCaskey, Kasper Gammeltoft */ @XmlRootElement(name = "ReflectivityModel") public class ReflectivityModel extends Model { @@ -48,6 +53,31 @@ public class ReflectivityModel extends Model { */ private final String processActionName = "Calculate Reflectivity"; + /** + * The name for the wave vector entry. + */ + private static final String WaveEntryName = "Wave Vector (Q) file"; + + /** + * The roughness entry name. + */ + private static final String RoughnessEntryName = "Roughness"; + + /** + * The delta q0 entry name. + */ + private static final String deltaQ0EntryName = "deltaQ0"; + + /** + * The delta q1 by q entry name. + */ + private static final String deltaQ1ByQEntryName = "deltaQ1ByQ"; + + /** + * The wave length entry name. + */ + private static final String WaveLengthEntryName = "Wave Length"; + /** * Identification number for the component that contains the parameters. */ @@ -59,6 +89,12 @@ public class ReflectivityModel extends Model { */ public static final int matListId = 2; + /** + * Identification number for the resource component that contains the output + * files. + */ + public static final int resourceCompId = 3; + /** * The constructor. */ @@ -86,19 +122,15 @@ public class ReflectivityModel extends Model { @Override public FormStatus process(String actionName) { - // Local Declarations - FormStatus retVal; + // Local Declarations. Return this value to display the process status + // on the form. + FormStatus retVal = null; if (actionName.equals(processActionName)) { - // Convert the material table to slabs - - // Get the roughness parameter, dQ, dQ/Q and the wavelength - - // Get and load the wave vector and related information - + // Get the material list from the form. ListComponent matList = (ListComponent) form - .getComponent(2); + .getComponent(matListId); ArrayList slabs = new ArrayList(); // Create the slabs from the materials @@ -115,18 +147,115 @@ public class ReflectivityModel extends Model { slabs.add(slab); } - // Calculate the reflectivity + // Get the roughness from the form. + int numRough = Integer.parseInt(((DataComponent) form + .getComponent(paramsCompId)).retrieveEntry( + RoughnessEntryName).getValue()); + + // Get the deltaQ0 from the form. + double deltaQ0 = Double.parseDouble(((DataComponent) form + .getComponent(paramsCompId)) + .retrieveEntry(deltaQ0EntryName).getValue()); + + // Get the deltaQ0 from the form. + double deltaQ1ByQ = Double.parseDouble(((DataComponent) form + .getComponent(paramsCompId)).retrieveEntry( + deltaQ1ByQEntryName).getValue()); + + // Get the wave length from the form. + double wavelength = Double.parseDouble(((DataComponent) form + .getComponent(paramsCompId)).retrieveEntry( + WaveLengthEntryName).getValue()); + + // Get the wave vector from the file picker in the paramters + // component. + double[] waveVector; + String fileName = ((DataComponent) form.getComponent(paramsCompId)) + .retrieveEntry(WaveEntryName).getValue(); + + // Get the file that should have been pulled into the local project. + IFile waveInput = project.getFile(fileName); + + // Get the reader and read in the values. + Form waveForm = new CSVReader().read(waveInput); + ListComponent waveData = (ListComponent) waveForm + .getComponent(1); + + // Pull the data from the form into an array. + waveVector = new double[waveData.size()]; + for (int i = 0; i < waveData.size(); i++) { + String[] dataLine = waveData.get(i); + double dataEntry = Double.parseDouble(dataLine[0]); + waveVector[i] = dataEntry; + } + + // Calculate the reflectivity - FIXME! Add RQ4 parameter ReflectivityCalculator calculator = new ReflectivityCalculator(); - // calculator.getReflectivityProfile(slabs, numRough, deltaQ0, - // deltaQ1ByQ, wavelength, waveVector, getRQ4); + ReflectivityProfile profile = calculator.getReflectivityProfile( + slabs.toArray(new Slab[slabs.size()]), numRough, deltaQ0, + deltaQ1ByQ, wavelength, waveVector, false); + + // Get the data from the profile + double[] reflectivity = profile.reflectivity; + double[] scatDensity = profile.scatteringDensity; + double[] depth = profile.depth; + + // Create the csv data for the reflectivity file + String reflectData = "#features, p_x, p_y\n#units,p_x,p_y\n"; + for (int i = 0; i < reflectivity.length; i++) { + reflectData += Double.toString(reflectivity[i]) + "," + + Double.toString(waveVector[i]) + "\n"; + } + + // Create the stream + ByteArrayInputStream reflectStream = new ByteArrayInputStream( + reflectData.getBytes()); + + // Create the data for the scattering density profile + String scatData = "#features, p_x, p_y\n#units,p_x,p_y\n"; + for (int i = 0; i < depth.length; i++) { + scatData += Double.toString(scatDensity[i]) + "," + + Double.toString(depth[i]) + "\n"; + } - // Write the files + ResourceComponent resComp = (ResourceComponent) form + .getComponent(resourceCompId); - retVal = FormStatus.InfoError; + // Create the stream + ByteArrayInputStream scatStream = new ByteArrayInputStream( + scatData.getBytes()); + + // Write the data to the files. + try { + // First the reflectivity file + VizResource reflectSource = (VizResource)resComp.get(0); + IFile reflectivityFile = project.getFile(reflectSource.getContents().getName()); + reflectivityFile.setContents(new BufferedInputStream( + reflectStream), true, false, null); + + // Then the scattering density file + VizResource scatSource = (VizResource)resComp.get(1); + IFile scatteringFile = project.getFile(scatSource.getContents().getName()); + scatteringFile.setContents(new BufferedInputStream(scatStream), + true, false, null); + + // Catch exceptions, should return an error. + } catch (CoreException | NullPointerException e) { + e.printStackTrace(); + retVal = FormStatus.InfoError; + } + + // Return processed if the value has not already beens set. + if (retVal == null) { + retVal = FormStatus.Processed; + } + + // Some other process action. } else { retVal = super.process(actionName); } + // Finally return retVal. return retVal; } @@ -139,11 +268,11 @@ public class ReflectivityModel extends Model { protected void setupForm() { // FIXME! Simple data entered now for testing - String line1 = "#features,t, p_x, p_y\n"; - String line2 = "#units,t,p_x,p_y\n"; - String line3 = "1.0,1.0,1.0\n"; - String line4 = "2.0,4.0,8.0\n"; - String line5 = "3.0,9.0,27.0\n"; + String line1 = "#features, p_x, p_y\n"; + String line2 = "#units,p_x,p_y\n"; + String line3 = "1.0,1.0\n"; + String line4 = "2.0,4.0\n"; + String line5 = "3.0,9.0\n"; String allLines = line1 + line2 + line3 + line4 + line5; // Create an empty stream for the output files @@ -172,7 +301,7 @@ public class ReflectivityModel extends Model { } }; fileEntry.setId(1); - fileEntry.setName("Wave Vector (Q) file"); + fileEntry.setName(WaveEntryName); fileEntry.setDescription("Wave vector information for this problem."); paramComponent.addEntry(fileEntry); @@ -189,11 +318,58 @@ public class ReflectivityModel extends Model { } }; numLayersEntry.setId(2); - numLayersEntry.setName("Roughness"); + numLayersEntry.setName(RoughnessEntryName); numLayersEntry.setDescription("Number of layers of " + "roughness per material layer."); paramComponent.addEntry(numLayersEntry); + // Add an entry for the deltaQ0 + Entry deltaQ0Entry = new Entry() { + @Override + protected void setup() { + allowedValueType = AllowedValueType.Continuous; + allowedValues.add(".00001"); + allowedValues.add("5.0"); + return; + } + }; + deltaQ0Entry.setId(3); + deltaQ0Entry.setName(deltaQ0EntryName); + deltaQ0Entry + .setDescription("The incident angle of the neutron stream."); + paramComponent.addEntry(deltaQ0Entry); + + // Add an entry for the deltaQ1ByQ + Entry deltaQ1Entry = new Entry() { + @Override + protected void setup() { + allowedValueType = AllowedValueType.Continuous; + allowedValues.add(".00001"); + allowedValues.add("5.0"); + return; + } + }; + deltaQ1Entry.setId(4); + deltaQ1Entry.setName(deltaQ1ByQEntryName); + deltaQ1Entry + .setDescription("The angle of refraction on the neutron stream."); + paramComponent.addEntry(deltaQ1Entry); + + // Add an entry for the wavelength + Entry waveEntry = new Entry() { + @Override + protected void setup() { + allowedValueType = AllowedValueType.Continuous; + allowedValues.add(".000001"); + allowedValues.add("1000"); + return; + } + }; + waveEntry.setId(5); + waveEntry.setName(WaveLengthEntryName); + waveEntry.setDescription("The wavelength of the neutron stream."); + paramComponent.addEntry(waveEntry); + // Configure a list of property names for the materials ArrayList names = new ArrayList(); names.add("Material ID"); @@ -220,6 +396,13 @@ public class ReflectivityModel extends Model { // Make sure to put it in the form! form.addComponent(matList); + // Create a component to hold the output + ResourceComponent resources = new ResourceComponent(); + resources.setName("Results"); + resources.setDescription("Results and Output"); + resources.setId(resourceCompId); + form.addComponent(resources); + if (project != null) { // FIXME! ID is always 1 at this point! String basename = "reflectivityModel_" + getId() + "_"; @@ -255,15 +438,9 @@ public class ReflectivityModel extends Model { scatDensitySource.setId(2); scatDensitySource.setDescription("Data from Stattering " + "Density calculation"); - - // Create a component to hold the output - ResourceComponent resources = new ResourceComponent(); - resources.setName("Results"); - resources.setDescription("Results and Output"); - resources.setId(2); + resources.addResource(reflectivitySource); resources.addResource(scatDensitySource); - form.addComponent(resources); } catch (CoreException | IOException e) { // Complain System.err.println("ReflectivityModel Error: " @@ -404,4 +581,4 @@ public class ReflectivityModel extends Model { return; } -} +} \ No newline at end of file diff --git a/tests/org.eclipse.ice.reflectivity.test/src/org/eclipse/ice/reflectivity/test/ReflectivityModelBuilderTester.java b/tests/org.eclipse.ice.reflectivity.test/src/org/eclipse/ice/reflectivity/test/ReflectivityModelBuilderTester.java index f3fa8e7a71d2d32bb6d9e62950d01ee475a66cca..afea7dc38b7a67f4413c18b178899bc3a7c558f4 100644 --- a/tests/org.eclipse.ice.reflectivity.test/src/org/eclipse/ice/reflectivity/test/ReflectivityModelBuilderTester.java +++ b/tests/org.eclipse.ice.reflectivity.test/src/org/eclipse/ice/reflectivity/test/ReflectivityModelBuilderTester.java @@ -62,7 +62,7 @@ public class ReflectivityModelBuilderTester implements IMaterialsDatabase { // Make sure we have a form and some components assertNotNull(model.getForm()); - assertEquals(2, model.getForm().getComponents().size()); + assertEquals(3, model.getForm().getComponents().size()); // Get the table component list = (ListComponent) model.getForm().getComponent(ReflectivityModel.matListId); @@ -75,6 +75,10 @@ public class ReflectivityModelBuilderTester implements IMaterialsDatabase { // setupFormWithServices() worked. assertNotNull(list.getElementSource()); assertEquals(list.getElementSource(), this); + + // Make sure the other components are being created properly. + assertNotNull(model.getForm().getComponent(ReflectivityModel.paramsCompId)); + assertNotNull(model.getForm().getComponent(ReflectivityModel.resourceCompId)); } diff --git a/tests/org.eclipse.ice.reflectivity.test/src/org/eclipse/ice/reflectivity/test/ReflectivityModelTester.java b/tests/org.eclipse.ice.reflectivity.test/src/org/eclipse/ice/reflectivity/test/ReflectivityModelTester.java index 7794cf6ab3b9ae608f22e369c406fe7db86389c0..bd57fe7bc19aeb0b84c03661b068481af61c0fc5 100644 --- a/tests/org.eclipse.ice.reflectivity.test/src/org/eclipse/ice/reflectivity/test/ReflectivityModelTester.java +++ b/tests/org.eclipse.ice.reflectivity.test/src/org/eclipse/ice/reflectivity/test/ReflectivityModelTester.java @@ -54,7 +54,7 @@ public class ReflectivityModelTester implements IMaterialsDatabase { // Make sure we have a form and some components assertNotNull(model.getForm()); - assertEquals(2, model.getForm().getComponents().size()); + assertEquals(3, model.getForm().getComponents().size()); // Get the table component list = (ListComponent) model.getForm().getComponent(ReflectivityModel.matListId); @@ -67,6 +67,10 @@ public class ReflectivityModelTester implements IMaterialsDatabase { // setupFormWithServices() worked. assertNotNull(list.getElementSource()); assertEquals(list.getElementSource(),this); + + // Make sure the other components are being created properly. + assertNotNull(model.getForm().getComponent(ReflectivityModel.paramsCompId)); + assertNotNull(model.getForm().getComponent(ReflectivityModel.resourceCompId)); return; }