Skip to content

Commit

Permalink
Refs #6670 Second pass at the test file
Browse files Browse the repository at this point in the history
  • Loading branch information
jmborr committed May 13, 2013
1 parent cc05bdd commit d1ac766
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 44 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/Algorithms/CMakeLists.txt
Expand Up @@ -543,6 +543,7 @@ set ( TEST_FILES
MergeRunsTest.h
MinusTest.h
ModeratorTzeroTest.h
ModeratorTzero2Test.h
MonteCarloAbsorptionTest.h
MultipleScatteringCylinderAbsorptionTest.h
MultiplyRangeTest.h
Expand Down
Expand Up @@ -96,7 +96,7 @@ class DLLExport ModeratorTzero2 : public Mantid::API::Algorithm
const double m_convfactor;
/// Maximum number of iterations when calculating the emission time from the moderator
size_t m_niter;
/// tolerance for calculating E1, in picoseconds
/// tolerance for calculating E1, in micro-seconds
double m_tolTOF;
/// string containing the heuristic regression for the moderator emission time versus neutron energy
std::string m_formula;
Expand Down
38 changes: 11 additions & 27 deletions Code/Mantid/Framework/Algorithms/src/ModeratorTzero2.cpp
Expand Up @@ -70,44 +70,28 @@ void ModeratorTzero2::init()
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InputWorkspace","",Direction::Input,wsValidator), "The name of the input workspace, containing events and/or histogram data, in units of time-of-flight");
//declare the output workspace
declareProperty(new WorkspaceProperty<MatrixWorkspace>("OutputWorkspace","",Direction::Output), "The name of the output workspace");
declareProperty(new Kernel::PropertyWithValue<double>("tolTOF", 1.0, Kernel::Direction::Input),"Tolerance in the calculation of the emission time, in microseconds");
declareProperty(new Kernel::PropertyWithValue<size_t>("niter", 1, Kernel::Direction::Input),"Number of iterations (default:1)");
declareProperty(new Kernel::PropertyWithValue<double>("tolTOF", 0.1, Kernel::Direction::Input),"Tolerance in the calculation of the emission time, in microseconds");
declareProperty(new Kernel::PropertyWithValue<size_t>("Niter", 1, Kernel::Direction::Input),"Number of iterations (default:1)");

} // end of void ModeratorTzero2::init()

void ModeratorTzero2::exec()
{
m_tolTOF = getProperty("tolTOF"); //time unit increment, in picoseconds;
m_niter=getProperty("niter"); // number of iterations
m_tolTOF = getProperty("tolTOF"); //Tolerance in the calculation of the emission time, in microseconds
m_niter=getProperty("Niter"); // number of iterations
const MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
m_instrument = inputWS->getInstrument(); // pointer to the instrument

//deltaE-mode (should be "indirect")
std::string Emode;
try
{
Emode = m_instrument->getStringParameter("deltaE-mode")[0];
g_log.debug() << "Instrument Geometry: " << Emode << std::endl;
if(Emode != "indirect")
{
throw std::invalid_argument("Instrument geometry must be of type indirect.");
}
}
catch (Exception::NotFoundError &)
{
g_log.error("Unable to retrieve instrument geometry (direct or indirect) parameter");
throw Exception::InstrumentDefinitionError("Unable to retrieve instrument geometry (direct or indirect) parameter", inputWS->getTitle());
}
std::vector<std::string> Emode=m_instrument->getStringParameter("deltaE-mode");
if(!Emode.size()) throw Exception::InstrumentDefinitionError("Unable to retrieve instrument geometry (direct or indirect) parameter", inputWS->getTitle());
if(Emode[0]!= "indirect") throw Exception::InstrumentDefinitionError("Instrument geometry must be of type indirect.");

// extract formula from instrument parameters
const ParameterMap& pmap = inputWS->constInstrumentParameters();
Instrument_const_sptr instrument=inputWS->getInstrument();
Parameter_sptr par=pmap.getRecursive(instrument->getChild(0).get(),"t0_formula");
if (!par)
{
throw std::invalid_argument("t0_formula not found in parameters");
}
m_formula=par->asString();
std::vector<std::string> t0_formula=m_instrument->getStringParameter("t0_formula");
if(!t0_formula.size()) throw Exception::InstrumentDefinitionError("Unable to retrieve t0_formula among instrument parameters");
m_formula=t0_formula[0];

//Run execEvent if eventWorkSpace
EventWorkspace_const_sptr eventWS = boost::dynamic_pointer_cast<const EventWorkspace>(inputWS);
if (eventWS != NULL)
Expand Down
42 changes: 26 additions & 16 deletions Code/Mantid/Framework/Algorithms/test/ModeratorTzero2Test.h
Expand Up @@ -20,38 +20,46 @@ using namespace Mantid::Algorithms;
using Mantid::DataObjects::Workspace2D_sptr;
using Mantid::MantidVecPtr;

class ModeratorTzeroTest2 : public CxxTest::TestSuite
class ModeratorTzero2Test : public CxxTest::TestSuite
{
public:

// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static ModeratorTzero2Test* createSuite() { return new ModeratorTzero2Test(); }
static void destroySuite( ModeratorTzero2Test *suite ) { delete suite; }

ModeratorTzero2Test() : m_convFact(1.0e-6*sqrt(2*Mantid::PhysicalConstants::meV/Mantid::PhysicalConstants::NeutronMass)) { }

/// Test when no deltaE-mode (direct or indirect) found among the instrument parameters
void testThrowsDeltaEmode()
void testExecThrowsDeltaEmode()
{
Workspace2D_sptr testWS = createTestHistoWorkspace();
const std::string inputName("testWS");
Workspace2D_sptr testWS = createTestHistoWorkspace();
AnalysisDataService::Instance().add(inputName, testWS); // register the workspace in the analysis data service
ModeratorTzero2 alg;
initializeAlgorithm(alg,inputName,inputName);
TS_ASSERT_THROWS_EQUALS(alg.execute(), const std::invalid_argument &e, std::string(e.what()), "Unable to retrieve instrument geometry (direct or indirect) parameter" );
TS_ASSERT_THROWS(alg.execute(), Mantid::Kernel::Exception::InstrumentDefinitionError);
AnalysisDataService::Instance().remove(inputName); // unregister the workspace from the analysis data service
}

/// Test when no t0_formula found among the instrument parameters
void testThrowsTzeroFormula()
void testExecThrowsTzeroFormula()
{
Workspace2D_sptr testWS = createTestHistoWorkspace();
const std::string inputName("testWS");
AnalysisDataService::Instance().add(inputName, testWS); // register the workspace in the analysis data service
Mantid::Geometry::Instrument_sptr instrument = testWS->getInstrument(); // pointer to the instrument
Mantid::Geometry::Instrument_const_sptr instrument = testWS->getInstrument(); // pointer to the instrument
testWS->instrumentParameters().addString(instrument->getChild(0).get(),"deltaE-mode", "indirect");
std::vector<std::string> Emode=instrument->getStringParameter("deltaE-mode");
ModeratorTzero2 alg;
initializeAlgorithm(alg,inputName,inputName);
TS_ASSERT_THROWS_EQUALS(alg.execute(), const std::invalid_argument &e, std::string(e.what()), "t0_formula not found in parameters");
TS_ASSERT_THROWS(alg.execute(), Mantid::Kernel::Exception::InstrumentDefinitionError);
AnalysisDataService::Instance().remove(inputName); // unregister the workspace from the analysis data service
}

/// Test transformation on a histogram workspace
void testHistogramWorkspace()
void testExecHistogramWorkspace()
{
Workspace2D_sptr testWS = createTestHistoWorkspace();
const std::string inputName("testWS");
Expand Down Expand Up @@ -91,7 +99,7 @@ void testHistogramWorkspace()
}

/// Test transformation on an event workspace
void testEventWorkspace()
void testExecEventWorkspace()
{
Mantid::DataObjects::EventWorkspace_sptr testWS=createTestEventWorkspace();
const std::string inputName("testWS");
Expand All @@ -105,7 +113,7 @@ void testEventWorkspace()
MatrixWorkspace_sptr outws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(outputName);
Mantid::DataObjects::EventWorkspace_sptr outWS(boost::dynamic_pointer_cast<Mantid::DataObjects::EventWorkspace>(outws));
Mantid::Geometry::IObjComponent_const_sptr sample = testWS->getInstrument()->getSample();
double L1,L2,E2,v2,t2,tof,t1,v1,E1;
double L1,L2,E2,v2,t2,t1,v1,E1;
mu::Parser parser;
parser.DefineVar("incidentEnergy", &E1); // associate E1 to this parser
parser.SetExpr(formula);
Expand All @@ -128,7 +136,7 @@ void testEventWorkspace()
t1=intofs[itof]-t2;
v1=L1/t1;
E1=m_convFact*v1*v1; // Energy in meV if v1 in meter/microsecond
TS_ASSERT_DELTA(outtofs[itof], tof-parser.Eval(), 1e-08); //evaluate the formula on the X-axis
TS_ASSERT_DELTA(outtofs[itof], intofs[itof]-parser.Eval(), 1e-08); //evaluate the formula on the X-axis
}

}
Expand Down Expand Up @@ -173,7 +181,7 @@ Mantid::DataObjects::EventWorkspace_sptr createTestEventWorkspace()
Mantid::DataObjects::EventWorkspace_sptr testWS=WorkspaceCreationHelper::createEventWorkspaceWithFullInstrument(numBanks, numPixels);
testWS->getAxis(0)->unit()=Mantid::Kernel::UnitFactory::Instance().create("TOF");
boost::mt19937 rng;
boost::normal_distribution nd(6493.0, 724.0); //normal distribution with mean=6493 and standard distribution=724
boost::normal_distribution<> nd(6493.0, 724.0); //normal distribution with mean=6493 and standard distribution=724
boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor(rng, nd); // random number generator
const size_t numHists=testWS->getNumberHistograms();
ModeratorTzero2 alg;
Expand All @@ -197,22 +205,24 @@ Mantid::DataObjects::EventWorkspace_sptr createTestEventWorkspace()
void addFormula(const std::string &formula, MatrixWorkspace_sptr workspace)
{
//deltaE-mode (should be "indirect")
Mantid::Geometry::Instrument_sptr instrument = workspace->getInstrument(); // pointer to the instrument
Mantid::Geometry::Instrument_const_sptr instrument = workspace->getInstrument(); // pointer to the instrument
workspace->instrumentParameters().addString(instrument->getChild(0).get(),"deltaE-mode", "indirect");
workspace->instrumentParameters().addString(instrument->getChild(0).get(),"t0_formula",formula);
}

/// Avoids writing these over and over
void initializeAlgorithm(ModeratorTzero2 &alg, const std::string &inputName, const std::string &outputName)
{
const size_t niter(1);
TS_ASSERT_THROWS_NOTHING(alg.initialize());
TS_ASSERT( alg.isInitialized() );
alg.setPropertyValue("InputWorkspace", inputName);
alg.setPropertyValue("OutputWorkspace", outputName);
alg.setProperty("Niter",niter); // make sure only one iteration for proper comparison
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize());
TS_ASSERT( alg.isInitialized() );
}

static const double m_convFact = 1.0e-6*sqrt(2*Mantid::PhysicalConstants::meV/Mantid::PhysicalConstants::NeutronMass);
const double m_convFact;
};

#endif /*MANTID_ALGORITHMS_MODERATORTZERO2TEST_H_*/

0 comments on commit d1ac766

Please sign in to comment.