Skip to content

Commit

Permalink
refs #7144 More up-to-date version
Browse files Browse the repository at this point in the history
Q and QSquared values in the algorithm are given in units of Angstroms
  • Loading branch information
DereckKachere committed Jun 20, 2013
1 parent 94b7243 commit 66017e5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 71 deletions.
16 changes: 8 additions & 8 deletions Code/Mantid/Framework/Algorithms/src/ConvertSpectrumAxis2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,28 @@ namespace Algorithms
efixed = DBL_MIN;
}

const double sineTheta = sin(twoTheta/2);
const double sineTheta = sin(twoTheta);

//Calculate the wavelength to allow it to be used to convert to elasticQ.
double wavelength = Mantid::PhysicalConstants::h/(sqrt(2*efixed*Mantid::PhysicalConstants::NeutronMass));
//The constant k.
const double k = (2*M_PI)/wavelength;

// The MomentumTransfer value.
double elasticQ = k*2*sineTheta;
double elasticQInMetres = k*2*sineTheta;
double elasticQInAngstroms = elasticQInMetres * pow(10, -10);

if(targetUnit == "ElasticQ")
{
m_indexMap.insert(std::make_pair(elasticQ, i));
m_indexMap.insert(std::make_pair(elasticQInAngstroms, i));
}

else if(targetUnit == "ElasticQSquared")
{
// The QSquared value.
double elasticQSquared = elasticQ*elasticQ;
double elasticQSquaredInAngstroms = elasticQInAngstroms*elasticQInAngstroms;

m_indexMap.insert(std::make_pair(elasticQSquared, i));
m_indexMap.insert(std::make_pair(elasticQSquaredInAngstroms, i));
}
}
}
Expand Down Expand Up @@ -250,8 +251,7 @@ namespace Algorithms
}
else
{
throw std::invalid_argument("Could not retrieve Efixed. Please provide a value.");
g_log.warning() << "Efixed could not be found for detector " << detector->getID() << ", please provide a value\n";
throw std::invalid_argument("Could not retrieve Efixed from the workspace. Please provide a value.");
}
}
else if( emode == 2 )
Expand All @@ -270,8 +270,8 @@ namespace Algorithms
}
else
{
throw std::invalid_argument("Could not retrieve Efixed. Please provide a value.");
g_log.warning() << "Efixed could not be found for detector " << detector->getID() << ", please provide a value\n";
throw std::invalid_argument("Could not retrieve Efixed from the detector. Please provide a value.");
}
}
}
Expand Down
112 changes: 49 additions & 63 deletions Code/Mantid/Framework/Algorithms/test/ConvertSpectrumAxis2Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,54 @@ class ConvertSpectrumAxis2Test : public CxxTest::TestSuite
{
private:

void do_algorithm_run(std::string target, std::string inputWS, std::string outputWS, bool startYNegative = false)
void do_algorithm_run(std::string target, std::string inputWS, std::string outputWS, bool startYNegative = true)
{
auto testWS = WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(3, 1, false, startYNegative);
AnalysisDataService::Instance().addOrReplace(inputWS, testWS);

Mantid::Algorithms::ConvertSpectrumAxis2 conv;

//conv.setPropertyValue("EFixed", "10.0");

conv.initialize();

TS_ASSERT_THROWS_NOTHING( conv.setPropertyValue("InputWorkspace",inputWS) );
TS_ASSERT_THROWS_NOTHING( conv.setPropertyValue("OutputWorkspace",outputWS) );
TS_ASSERT_THROWS_NOTHING( conv.setPropertyValue("Target",target) );
TS_ASSERT_THROWS_NOTHING( conv.setPropertyValue("EFixed","10.0") );

TS_ASSERT_THROWS_NOTHING( conv.execute() );
TS_ASSERT( conv.isExecuted() );
}

public:

void estName()
void testName()
{
Mantid::Algorithms::ConvertSpectrumAxis2 conv;
TS_ASSERT_EQUALS( conv.name(), "ConvertSpectrumAxis" );
}

void estVersion()
void testVersion()
{
Mantid::Algorithms::ConvertSpectrumAxis2 conv;
TS_ASSERT_EQUALS( conv.version(), 2 );
}

void estInit()
void testInit()
{
Mantid::Algorithms::ConvertSpectrumAxis2 conv;
TS_ASSERT_THROWS_NOTHING( conv.initialize() );
TS_ASSERT( conv.isInitialized() );
}

void estTargetSignedTheta() // Old version of alg did not follow the naming convention for units. Keep till the old version is deprecated.
void testTargetSignedTheta()
{
const std::string inputWS("inWS");
const std::string outputSignedThetaAxisWS("outSignedThetaWS");
const std::string inputWS2("inWS2");
const std::string outputSignedThetaAxisWS2("outSignedThetaWS2");

do_algorithm_run("signed_theta", inputWS, outputSignedThetaAxisWS, true);
do_algorithm_run("SignedTheta", inputWS2, outputSignedThetaAxisWS2, true);
do_algorithm_run("signed_theta", inputWS, outputSignedThetaAxisWS);
do_algorithm_run("SignedTheta", inputWS2, outputSignedThetaAxisWS2);

MatrixWorkspace_const_sptr outputSignedTheta,outputSignedTheta2;
TS_ASSERT_THROWS_NOTHING( outputSignedTheta = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(outputSignedThetaAxisWS) );
Expand Down Expand Up @@ -100,31 +100,31 @@ class ConvertSpectrumAxis2Test : public CxxTest::TestSuite
AnalysisDataService::Instance().remove(outputSignedThetaAxisWS2);
}

void estTargetTheta() // Compatible with old version of alg. Keep till the old version is deprecated.
void testTargetTheta() // Compatible with old version of alg. Keep till the old version is deprecated.
{
const std::string inputWS("inWS");
const std::string outputWS("outWS");
//const std::string inputWS2("inWS2");
//const std::string outputWS2("outWS2");
const std::string inputWS2("inWS2");
const std::string outputWS2("outWS2");

do_algorithm_run("theta", inputWS, outputWS);
//do_algorithm_run("Theta", inputWS2, outputWS2);
do_algorithm_run("Theta", inputWS2, outputWS2);

MatrixWorkspace_const_sptr input,output,input2,output2;
TS_ASSERT_THROWS_NOTHING( input = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(inputWS) );
TS_ASSERT_THROWS_NOTHING( output = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(outputWS) );
//TS_ASSERT_THROWS_NOTHING( input2 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(inputWS2) );
//TS_ASSERT_THROWS_NOTHING( output2 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(outputWS2) );
TS_ASSERT_THROWS_NOTHING( input2 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(inputWS2) );
TS_ASSERT_THROWS_NOTHING( output2 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(outputWS2) );

// Should now have a numeric axis up the side, with units of angle
const Axis* thetaAxis = 0;
TS_ASSERT_THROWS_NOTHING( thetaAxis = output->getAxis(1) );
TS_ASSERT( thetaAxis->isNumeric() );
TS_ASSERT_EQUALS( thetaAxis->unit()->caption(), "Scattering angle" );
TS_ASSERT_EQUALS( thetaAxis->unit()->label(), "degrees" );
TS_ASSERT_DELTA( (*thetaAxis)(0), 1.1458, 0.0001 );
TS_ASSERT_DELTA( (*thetaAxis)(1), 0.0000, 0.0001 );
TS_ASSERT_DELTA( (*thetaAxis)(2), 1.1458, 0.0001 );
TS_ASSERT_DELTA( (*thetaAxis)(0), 0.0000, 0.0001 );
TS_ASSERT_DELTA( (*thetaAxis)(1), 1.1458, 0.0001 );

// Check axis is correct length
TS_ASSERT_THROWS( (*thetaAxis)(3), Mantid::Kernel::Exception::IndexError );

Expand All @@ -135,40 +135,34 @@ class ConvertSpectrumAxis2Test : public CxxTest::TestSuite
TS_ASSERT_EQUALS( input->readX(1), output->readX(1) );
TS_ASSERT_EQUALS( input->readY(1), output->readY(1) );
TS_ASSERT_EQUALS( input->readE(1), output->readE(1) );
TS_ASSERT_EQUALS( input->readX(2), output->readX(0) );
TS_ASSERT_EQUALS( input->readY(2), output->readY(0) );
TS_ASSERT_EQUALS( input->readE(2), output->readE(0) );

//const Axis* thetaAxis2 = 0;
//TS_ASSERT_THROWS_NOTHING( thetaAxis2 = output2->getAxis(1) );
//TS_ASSERT( thetaAxis2->isNumeric() );
//TS_ASSERT_EQUALS( thetaAxis2->unit()->caption(), "Scattering angle" );
//TS_ASSERT_EQUALS( thetaAxis2->unit()->label(), "degrees" );
//TS_ASSERT_DELTA( (*thetaAxis2)(0), 0.0000, 0.0001 );
//TS_ASSERT_DELTA( (*thetaAxis2)(1), 1.1458, 0.0001 );
//TS_ASSERT_DELTA( (*thetaAxis2)(2), 2.2906, 0.0001 );
//// Check axis is correct length
//TS_ASSERT_THROWS( (*thetaAxis2)(3), Mantid::Kernel::Exception::IndexError );
const Axis* thetaAxis2 = 0;
TS_ASSERT_THROWS_NOTHING( thetaAxis2 = output2->getAxis(1) );
TS_ASSERT( thetaAxis2->isNumeric() );
TS_ASSERT_EQUALS( thetaAxis2->unit()->caption(), "Scattering angle" );
TS_ASSERT_EQUALS( thetaAxis2->unit()->label(), "degrees" );
TS_ASSERT_DELTA( (*thetaAxis2)(0), 0.0000, 0.0001 );
TS_ASSERT_DELTA( (*thetaAxis2)(1), 1.1458, 0.0001 );

// Check axis is correct length
TS_ASSERT_THROWS( (*thetaAxis2)(3), Mantid::Kernel::Exception::IndexError );

//// Data should be swapped over
//TS_ASSERT_EQUALS( input2->readX(0), output2->readX(2) );
//TS_ASSERT_EQUALS( input2->readY(0), output2->readY(2) );
//TS_ASSERT_EQUALS( input2->readE(0), output2->readE(2) );
//TS_ASSERT_EQUALS( input2->readX(1), output2->readX(1) );
//TS_ASSERT_EQUALS( input2->readY(1), output2->readY(1) );
//TS_ASSERT_EQUALS( input2->readE(1), output2->readE(1) );
//TS_ASSERT_EQUALS( input2->readX(2), output2->readX(0) );
//TS_ASSERT_EQUALS( input2->readY(2), output2->readY(0) );
//TS_ASSERT_EQUALS( input2->readE(2), output2->readE(0) );
TS_ASSERT_EQUALS( input2->readX(0), output2->readX(2) );
TS_ASSERT_EQUALS( input2->readY(0), output2->readY(2) );
TS_ASSERT_EQUALS( input2->readE(0), output2->readE(2) );
TS_ASSERT_EQUALS( input2->readX(1), output2->readX(1) );
TS_ASSERT_EQUALS( input2->readY(1), output2->readY(1) );
TS_ASSERT_EQUALS( input2->readE(1), output2->readE(1) );

//Clean up
AnalysisDataService::Instance().remove(inputWS);
AnalysisDataService::Instance().remove(outputWS);
//AnalysisDataService::Instance().remove(inputWS2);
//AnalysisDataService::Instance().remove(outputWS2);
AnalysisDataService::Instance().remove(inputWS2);
AnalysisDataService::Instance().remove(outputWS2);
}

void est_Target_ElasticQ_Throws_When_No_Efixed_Provided_And_Not_In_Workspace()
void test_Target_ElasticQ_Throws_When_No_Efixed_Provided_And_Not_In_Workspace()
{
std::string inputWS("inWS");
const std::string outputWS("outWS");
Expand All @@ -188,7 +182,7 @@ class ConvertSpectrumAxis2Test : public CxxTest::TestSuite
TS_ASSERT( !conv.isExecuted() );
}

void est_Target_ElasticQ_Uses_Algorithm_EFixed_Value_If_Provided()
void test_Target_ElasticQ_Uses_Algorithm_EFixed_Value_If_Provided()
{
std::string inputWS("inWS");
const std::string outputWS("outWS");
Expand All @@ -203,16 +197,14 @@ class ConvertSpectrumAxis2Test : public CxxTest::TestSuite
TS_ASSERT_THROWS_NOTHING( conv.setPropertyValue("InputWorkspace",inputWS) );
TS_ASSERT_THROWS_NOTHING( conv.setPropertyValue("OutputWorkspace",outputWS) );
TS_ASSERT_THROWS_NOTHING( conv.setPropertyValue("Target",target) );

//TS_ASSERT( != EMPTY_DBL);
}

void estTargetElasticQ() // The new version of this alg follows the standard for the naming of units.
void test_Target_ElasticQ_Returns_Correct_Value_When_EFixed_Is_Provided()
{
std::string inputWS("inWS");
const std::string outputWS("outWS");

do_algorithm_run("ElasticQ", inputWS, outputWS);
do_algorithm_run("ElasticQ", inputWS, outputWS, false);

MatrixWorkspace_const_sptr input,output;
TS_ASSERT_THROWS_NOTHING( input = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(inputWS) );
Expand All @@ -224,15 +216,12 @@ class ConvertSpectrumAxis2Test : public CxxTest::TestSuite
TS_ASSERT( qAxis->isNumeric() );
TS_ASSERT_EQUALS( qAxis->unit()->unitID(), "MomentumTransfer");

double eFixed;
double constant = (4 * M_PI * sqrt(2 * Mantid::PhysicalConstants::NeutronMass * eFixed ))/Mantid::PhysicalConstants::h;

TS_ASSERT_DELTA( (*qAxis)(0), 0.0000, 0.0001 );
TS_ASSERT_DELTA( (*qAxis)(1), constant * 0.0200, 0.0001 );
TS_ASSERT_DELTA( (*qAxis)(2), constant * 0.0400, 0.0001 );
TS_ASSERT_DELTA( (*qAxis)(1), 6.941e9, 1.0000e6 );
TS_ASSERT_DELTA( (*qAxis)(2), 1.387e10, 1.0000e7 );

// Check axis is correct length
TS_ASSERT_THROWS( (*qAxis)(2), Mantid::Kernel::Exception::IndexError );
TS_ASSERT_THROWS( (*qAxis)(3), Mantid::Kernel::Exception::IndexError );

TS_ASSERT_EQUALS( input->readX(0), output->readX(0) );
TS_ASSERT_EQUALS( input->readY(0), output->readY(0) );
Expand All @@ -249,12 +238,12 @@ class ConvertSpectrumAxis2Test : public CxxTest::TestSuite
AnalysisDataService::Instance().remove(outputWS);
}

void estTargetElasticQSquared() // The new version of this alg follows the standard for the naming of units.
void test_Target_ElasticQSquared_Returns_Correct_Value_When_EFixed_Is_Provided()
{
std::string inputWS("inWS");
const std::string outputWS("outWS");

do_algorithm_run("ElasticQSquared", inputWS, outputWS);
do_algorithm_run("ElasticQSquared", inputWS, outputWS, false);

MatrixWorkspace_const_sptr input,output;
TS_ASSERT_THROWS_NOTHING( input = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(inputWS) );
Expand All @@ -265,16 +254,13 @@ class ConvertSpectrumAxis2Test : public CxxTest::TestSuite
TS_ASSERT_THROWS_NOTHING( q2Axis = output->getAxis(1) );
TS_ASSERT( q2Axis->isNumeric() );
TS_ASSERT_EQUALS( q2Axis->unit()->unitID(), "QSquared");

double eFixed;
double constant = (32 * pow(M_PI, 2) * Mantid::PhysicalConstants::NeutronMass * eFixed )/pow(Mantid::PhysicalConstants::h, 2);


TS_ASSERT_DELTA( (*q2Axis)(0), 0.0000, 0.0001 );
TS_ASSERT_DELTA( (*q2Axis)(1), constant * 0.0004, 0.0001 );
TS_ASSERT_DELTA( (*q2Axis)(2), constant * 0.0016, 0.0001 );
TS_ASSERT_DELTA( (*q2Axis)(1), 4.817e19, 1.0000e16 );
TS_ASSERT_DELTA( (*q2Axis)(2), 1.924e20, 1.0000e17 );

// Check axis is correct length
TS_ASSERT_THROWS( (*q2Axis)(2), Mantid::Kernel::Exception::IndexError );
TS_ASSERT_THROWS( (*q2Axis)(3), Mantid::Kernel::Exception::IndexError );

TS_ASSERT_EQUALS( input->readX(0), output->readX(0) );
TS_ASSERT_EQUALS( input->readY(0), output->readY(0) );
Expand Down

0 comments on commit 66017e5

Please sign in to comment.