Skip to content

Commit

Permalink
Unit test and wiki for ConvertToMDHelper. Refs #5875
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiSavici committed Jul 8, 2013
1 parent c14c672 commit 3473b8e
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 25 deletions.
20 changes: 15 additions & 5 deletions Code/Mantid/Framework/MDAlgorithms/src/ConvertToMDHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
/*WIKI*
The algorithm will try to calculate the MinValues and MaxValues limits that are required in the ConvertToMD algorithm.
The algorithm will try to calculate the MinValues and MaxValues limits that are required in the ConvertToMD algorithm, using the following procedure:
1. If QDimensions is CopyToMD the first value in MinValues is going to be the workspace minimum X coordinate, and the first value in MaxValues is going to be the maximum X coordinate
2. If QDimensions is |Q| or Q3D, first we calculate the maximum momentum transfer, Qmax. If dEAnalysisMode is Elastic, we convert to Momentum units, find the maximum value, and multiply by 2, since the maximum momentum transfer occurs when the incident beam and the scattered beam are anti-parallel.
If dEAnalysisMode is Direct or Indirect, we convert to DeltaE units, find the minimum and maximum (dEmin, dEmax), calculate to ki and kf. The maximum momentum transfer is ki+kf.
3. If QDimensions is |Q|, the first value of the MinValues is 0, and the first value of MaxValues is Qmax
4. If QDimensions is Q3D, and Q3DFrames is Q the first three values of the MinValues are -Qmax, -Qmax, -Qmax, and the first three values of MaxValues are Qmax, Qmax, Qmax
5. If QDimensions is Q3D, and Q3DFrames is HKL the first three values of the MinValues are -Qmax*a/(2*pi), -Qmax*b/(2*pi), -Qmax*c/(2*pi), and the first three values of MaxValues are Qmax*a/(2*pi), Qmax*b/(2*pi), Qmax*c/(2*pi). Note: for HKL mode one needs to have an OrientedLattice attached to the sample.
6. If QDimensions is |Q| or Q3D, and dEAnalysisMode is Elastic or Inelastic, the next value in MinValues is dEmin, and the next value in MaxValues is dEmax
7. If any OtherDimensions are added, the last values in MinValues (MaxValues) are the minimum (maximum) of each of the sample log values selected
*WIKI*/

#include "MantidMDAlgorithms/ConvertToMDHelper.h"
Expand Down Expand Up @@ -71,8 +81,8 @@ namespace MDAlgorithms
ws_valid->add<InstrumentValidator>();
// the validator which checks if the workspace has axis and any units
ws_valid->add<WorkspaceUnitValidator>("");


//histogram needed by ConvertUnits
ws_valid->add<HistogramValidator>();
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InputWorkspace","",Direction::Input,ws_valid),
"An input Matrix Workspace (Workspace2D or Event workspace) ");

Expand Down Expand Up @@ -127,11 +137,11 @@ namespace MDAlgorithms
void ConvertToMDHelper::exec()
{
std::vector<double> MinValues,MaxValues;

std::string QDimension=getPropertyValue("QDimensions");
std::string GeometryMode=getPropertyValue("dEAnalysisMode");
std::string Q3DFrames=getPropertyValue("Q3DFrames");
std::vector<std::string> OtherDimensions=getProperty("OtherDimensions");

MatrixWorkspace_sptr ws=getProperty("InputWorkspace"),wstemp;
DataObjects::EventWorkspace_sptr evWS;
double xmin,xmax;
Expand All @@ -154,6 +164,7 @@ namespace MDAlgorithms
conv->setProperty("Target","Momentum");
conv->setProperty("Emode","Elastic");
conv->executeAsChildAlg();

wstemp=conv->getProperty("OutputWorkspace");
evWS=boost::dynamic_pointer_cast< Mantid::DataObjects::EventWorkspace >(wstemp);
if (evWS)
Expand Down Expand Up @@ -269,7 +280,6 @@ namespace MDAlgorithms
MaxValues.push_back(p->getStatistics().maximum);
}


setProperty("MinValues",MinValues);
setProperty("MaxValues",MaxValues);
}
Expand Down
222 changes: 202 additions & 20 deletions Code/Mantid/Framework/MDAlgorithms/test/ConvertToMDHelperTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@
#include <cxxtest/TestSuite.h>

#include "MantidMDAlgorithms/ConvertToMDHelper.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidDataHandling/LoadEventNexus.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidKernel/TimeSeriesProperty.h"


using Mantid::MDAlgorithms::ConvertToMDHelper;

using Mantid::MDAlgorithms::ConvertToMDHelper;
class ConvertToMDHelperTest : public CxxTest::TestSuite
{
public:
Expand All @@ -23,38 +32,211 @@ class ConvertToMDHelperTest : public CxxTest::TestSuite
TS_ASSERT( alg.isInitialized() )
}

void test_exec()
void test_direct1D()
{
// Name of the output workspace.
std::string outWSName("ConvertToMDHelperTest_OutputWS");


Mantid::API::FrameworkManager::Instance();
ConvertToMDHelper alg;
Mantid::API::MatrixWorkspace_sptr ws=MakeWorkspace(-50,1,true,60,0);
WorkspaceCreationHelper::storeWS(WSName,ws);

TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("REPLACE_PROPERTY_NAME_HERE!!!!", "value") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", WSName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("QDimensions","|Q|") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("dEAnalysisMode","Direct") );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );

// Retrieve the workspace from data service. TODO: Change to your desired type
Workspace_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<Workspace>(outWSName) );
TS_ASSERT(ws);
if (!ws) return;

// TODO: Check the results

// Check the results

TS_ASSERT_EQUALS(alg.getPropertyValue("MinValues"),"0,-50");
TS_ASSERT_EQUALS(alg.getPropertyValue("MaxValues"),"12.667,50");
// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
Mantid::API::AnalysisDataService::Instance().remove(WSName);
}

void test_direct3D()
{

Mantid::API::FrameworkManager::Instance();
ConvertToMDHelper alg;
Mantid::API::MatrixWorkspace_sptr ws=MakeWorkspace(-50,1,true,60,0);
WorkspaceCreationHelper::storeWS(WSName,ws);

TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", WSName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("QDimensions","Q3D") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("dEAnalysisMode","Direct") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Q3DFrames","Q") );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
// Check the results
TS_ASSERT_EQUALS(alg.getPropertyValue("MinValues"),"-12.667,-12.667,-12.667,-50");
TS_ASSERT_EQUALS(alg.getPropertyValue("MaxValues"),"12.667,12.667,12.667,50");
// Remove workspace from the data service.
Mantid::API::AnalysisDataService::Instance().remove(WSName);
}

void test_Something()
void test_direct3DHKL()
{

Mantid::API::FrameworkManager::Instance();
ConvertToMDHelper alg;
Mantid::API::MatrixWorkspace_sptr ws=MakeWorkspace(-50,1,true,60,0);
WorkspaceCreationHelper::storeWS(WSName,ws);

TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", WSName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("QDimensions","Q3D") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("dEAnalysisMode","Direct") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Q3DFrames","HKL") );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
// Check the results
TS_ASSERT_EQUALS(alg.getPropertyValue("MinValues"),"-4.03205,-6.04807,-8.06409,-50");
TS_ASSERT_EQUALS(alg.getPropertyValue("MaxValues"),"4.03205,6.04807,8.06409,50");
// Remove workspace from the data service.
Mantid::API::AnalysisDataService::Instance().remove(WSName);
}

void test_indirect1D()
{

Mantid::API::FrameworkManager::Instance();
ConvertToMDHelper alg;
Mantid::API::MatrixWorkspace_sptr ws=MakeWorkspace(-2.5,0.05,true,0,5);
WorkspaceCreationHelper::storeWS(WSName,ws);

TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", WSName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("QDimensions","|Q|") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("dEAnalysisMode","Indirect") );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
// Check the results

TS_ASSERT_EQUALS(alg.getPropertyValue("MinValues"),"0,-2.5");
TS_ASSERT_EQUALS(alg.getPropertyValue("MaxValues"),"3.45587,2.5");
// Remove workspace from the data service.
Mantid::API::AnalysisDataService::Instance().remove(WSName);
}

void test_elastic1D()
{
TSM_ASSERT( "You forgot to write a test!", 0);

Mantid::API::FrameworkManager::Instance();
ConvertToMDHelper alg;
Mantid::API::MatrixWorkspace_sptr ws=MakeWorkspace(25000,10,false,0,0);
WorkspaceCreationHelper::storeWS(WSName,ws);

TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", WSName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("QDimensions","|Q|") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("dEAnalysisMode","Elastic") );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
// Check the results

TS_ASSERT_EQUALS(alg.getPropertyValue("MinValues"),"0");
TS_ASSERT_EQUALS(alg.getPropertyValue("MaxValues"),"2.54437");
// Remove workspace from the data service.
Mantid::API::AnalysisDataService::Instance().remove(WSName);
}

void test_elastic1DandExtra()
{

Mantid::API::FrameworkManager::Instance();
ConvertToMDHelper alg;
Mantid::API::MatrixWorkspace_sptr ws=MakeWorkspace(25000,10,false,0,0);
WorkspaceCreationHelper::storeWS(WSName,ws);

TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", WSName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("QDimensions","|Q|") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("dEAnalysisMode","Elastic") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OtherDimensions","doubleProp") );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
// Check the results

TS_ASSERT_EQUALS(alg.getPropertyValue("MinValues"),"0,5.55");
TS_ASSERT_EQUALS(alg.getPropertyValue("MaxValues"),"2.54437,10.55");
// Remove workspace from the data service.
Mantid::API::AnalysisDataService::Instance().remove(WSName);
}

private:
std::string WSName="CMDHelper";


Mantid::API::MatrixWorkspace_sptr MakeWorkspace(double xmin,double dx,bool deltaEUnits,
double Ei, double Ef)
{

Mantid::API::MatrixWorkspace_sptr ws= WorkspaceCreationHelper::Create2DWorkspaceBinned(1,100,xmin, dx);

if((Ei>0 || Ef>0)&&deltaEUnits)
{
ws->getAxis(0)->setUnit("DeltaE");
}
else
{
ws->getAxis(0)->setUnit("TOF");
}

Mantid::Geometry::Instrument_sptr testInst(new Mantid::Geometry::Instrument);
ws->setInstrument(testInst);
// Define a source and sample position
//Define a source component
Mantid::Geometry::ObjComponent *source = new Mantid::Geometry::ObjComponent("moderator", Mantid::Geometry::Object_sptr(), testInst.get());
source->setPos(Mantid::Kernel::V3D(0, 0.0, -15.));
testInst->add(source);
testInst->markAsSource(source);
// Define a sample as a simple sphere
Mantid::Geometry::ObjComponent *sample = new Mantid::Geometry::ObjComponent("samplePos", Mantid::Geometry::Object_sptr(), testInst.get());
testInst->setPos(0.0, 0.0, 0.0);
testInst->add(sample);
testInst->markAsSamplePos(sample);
//Detector
Mantid::Geometry::Detector * physicalPixel = new Mantid::Geometry::Detector("pixel", 1, testInst.get());
physicalPixel->setPos(0.5,0,5.0);
testInst->add(physicalPixel);
testInst->markAsDetector(physicalPixel);

ws->getSpectrum(0)->addDetectorID(physicalPixel->getID());


if (Ei>0)
{
ws->mutableRun().addProperty(new Mantid::Kernel::PropertyWithValue<double>("Ei",Ei));
}

if (Ef>0)
{
Mantid::Geometry::ParameterMap pmap(ws->instrumentParameters());
pmap.addDouble(physicalPixel,"Efixed",Ef);
ws->replaceInstrumentParameters(pmap);
}

ws->mutableSample().setOrientedLattice(new Mantid::Geometry::OrientedLattice(2,3,4,90,90,90));

Mantid::Kernel::TimeSeriesProperty<double> * p = new Mantid::Kernel::TimeSeriesProperty<double>("doubleProp");
TS_ASSERT_THROWS_NOTHING( p->addValue("2007-11-30T16:17:00",9.99) );
TS_ASSERT_THROWS_NOTHING( p->addValue("2007-11-30T16:17:10",7.55) );
TS_ASSERT_THROWS_NOTHING( p->addValue("2007-11-30T16:17:20",5.55) );
TS_ASSERT_THROWS_NOTHING( p->addValue("2007-11-30T16:17:30",10.55) );

ws->mutableRun().addLogData(p);

return ws;
}
};


#endif /* MANTID_MDALGORITHMS_CONVERTTOMDHELPERTEST_H_ */
#endif /* MANTID_MDALGORITHMS_CONVERTTOMDHELPERTEST_H_ */

0 comments on commit 3473b8e

Please sign in to comment.