Skip to content

Commit

Permalink
Update test fro DiffRotDiscreteCircle, clang format
Browse files Browse the repository at this point in the history
Refs #11447
  • Loading branch information
DanNixon committed Apr 2, 2015
1 parent caafb11 commit a3867d6
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 52 deletions.
24 changes: 18 additions & 6 deletions Code/Mantid/Framework/CurveFitting/src/DiffRotDiscreteCircle.cpp
Expand Up @@ -99,7 +99,11 @@ void InelasticDiffRotDiscreteCircle::function1D(double *out,
const double S = getParameter("Shift");

double Q;
if (getAttribute("Q").asDouble() == EMPTY_DBL() && m_qValueCache.size() > 0) {
if (getAttribute("Q").asDouble() == EMPTY_DBL()) {
if (m_qValueCache.size() == 0) {
throw std::runtime_error(
"No Q attribute provided and cannot retrieve from worksapce.");
}
const int specIdx = getAttribute("WorkspaceIndex").asInt();
Q = m_qValueCache[specIdx];

Expand Down Expand Up @@ -165,18 +169,26 @@ void InelasticDiffRotDiscreteCircle::setWorkspace(
IDetector_const_sptr det;
try {
det = workspace->getDetector(idx);
} catch(Kernel::Exception::NotFoundError &) {
}
catch (Kernel::Exception::NotFoundError &) {
m_qValueCache.clear();
g_log.information("Cannot populate Q values from workspace");
break;
}

double efixed = workspace->getEFixed(det);
double usignTheta = workspace->detectorTwoTheta(det) / 2.0;
try {
double efixed = workspace->getEFixed(det);
double usignTheta = workspace->detectorTwoTheta(det) / 2.0;

double q = Mantid::Kernel::UnitConversion::run(usignTheta, efixed);
double q = Mantid::Kernel::UnitConversion::run(usignTheta, efixed);

m_qValueCache.push_back(q);
m_qValueCache.push_back(q);
}
catch (std::runtime_error &) {
m_qValueCache.clear();
g_log.information("Cannot populate Q values from workspace");
return;
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions Code/Mantid/Framework/CurveFitting/src/DiffSphere.cpp
Expand Up @@ -210,7 +210,8 @@ void InelasticDiffSphere::function1D(double *out, const double *xValues,
double Q;
if (getAttribute("Q").asDouble() == EMPTY_DBL()) {
if (m_qValueCache.size() == 0) {
throw std::runtime_error("No Q attribute provided and cannot retrieve from worksapce.");
throw std::runtime_error(
"No Q attribute provided and cannot retrieve from worksapce.");
}

const int specIdx = getAttribute("WorkspaceIndex").asInt();
Expand Down Expand Up @@ -276,23 +277,22 @@ InelasticDiffSphere::setWorkspace(boost::shared_ptr<const API::Workspace> ws) {
IDetector_const_sptr det;
try {
det = workspace->getDetector(idx);
} catch(Exception::NotFoundError &) {
}
catch (Exception::NotFoundError &) {
m_qValueCache.clear();
g_log.information("Cannot populate Q values from workspace");
break;
}

try
{
try {
double efixed = workspace->getEFixed(det);
double usignTheta = workspace->detectorTwoTheta(det) / 2.0;

double q = Mantid::Kernel::UnitConversion::run(usignTheta, efixed);

m_qValueCache.push_back(q);
}
catch(std::runtime_error &)
{
catch (std::runtime_error &) {
m_qValueCache.clear();
g_log.information("Cannot populate Q values from workspace");
return;
Expand Down
116 changes: 76 additions & 40 deletions Code/Mantid/Framework/CurveFitting/test/DiffRotDiscreteCircleTest.h
@@ -1,20 +1,23 @@
#ifndef DIFFROTDISCRETECIRCLETEST_H_
#define DIFFROTDISCRETECIRCLETEST_H_

#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/AlgorithmFactory.h"
#include "MantidCurveFitting/Convolution.h"
#include "MantidCurveFitting/DiffRotDiscreteCircle.h"
#include "MantidCurveFitting/Gaussian.h"
#include "MantidCurveFitting/Fit.h"
#include "MantidGeometry/Instrument/ReferenceFrame.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"

#include <cmath>

#include <cxxtest/TestSuite.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/shared_ptr.hpp>

#include "MantidCurveFitting/Gaussian.h"
#include "MantidCurveFitting/DiffRotDiscreteCircle.h"
#include "MantidCurveFitting/Convolution.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include "MantidCurveFitting/Fit.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/AlgorithmFactory.h"

class DiffRotDiscreteCircleTest : public CxxTest::TestSuite
{

Expand Down Expand Up @@ -76,13 +79,25 @@ class DiffRotDiscreteCircleTest : public CxxTest::TestSuite
} // testDiffRotDiscreteCircleElastic


void testDiffRotDiscreteCircleInelastic()
void testDiffRotDiscreteCircleInelasticWithQParam()
{
runDiffRotDiscreteCircleInelasticTest(0.0, 0.20092);
}


void testDiffRotDiscreteCircleInelasticWithWSIndex()
{
runDiffRotDiscreteCircleInelasticTest(0.0);
}


void testDiffRotDiscreteCircleInelasticWithShift()
void testDiffRotDiscreteCircleInelasticWithShiftWithQParam()
{
runDiffRotDiscreteCircleInelasticTest(0.5, 0.20092);
}


void testDiffRotDiscreteCircleInelasticWithShiftWithWSIndex()
{
runDiffRotDiscreteCircleInelasticTest(0.5);
}
Expand Down Expand Up @@ -136,7 +151,6 @@ class DiffRotDiscreteCircleTest : public CxxTest::TestSuite
const double chi_squared = fitalg.getProperty("OutputChi2overDoF");
TS_ASSERT_LESS_THAN( chi_squared, 1e-12 );
//std::cout << "\nchi_squared = " << chi_squared << "\n"; // only for debugging purposes

}


Expand Down Expand Up @@ -248,13 +262,12 @@ class DiffRotDiscreteCircleTest : public CxxTest::TestSuite
TS_ASSERT_DELTA( fitalg_structure_factor -> getParameter( "Decay" ), 7.567, 7.567 * 0.05 ); // allow for a small percent variation
//std::cout << "\nGOAL: Intensity = 47.014, Radius = 1.567, Decay = 7.567\n"; // only for debugging purposes
//std::cout << "OPTIMIZED: Intensity = " << fitalg_structure_factor->getParameter("Intensity") << " Radius = " << fitalg_structure_factor->getParameter("Radius") << " Decay = " << fitalg_structure_factor->getParameter("Decay") << "\n"; // only for debugging purposes

} // testDiffRotDiscreteCircle


private:
/// Fit the convolution of the inelastic part with a Gaussian resolution function
void runDiffRotDiscreteCircleInelasticTest(const double S)
void runDiffRotDiscreteCircleInelasticTest(const double S, const double Q = Mantid::EMPTY_DBL())
{
/* Note: it turns out that parameters Intensity and Radius are highly covariant, so that more than one minimum exists.
* Thus, I tied parameter Radius. This is OK since one usually knows the radius of the circle of the jumping diffusion
Expand All @@ -263,14 +276,18 @@ class DiffRotDiscreteCircleTest : public CxxTest::TestSuite
const double R(1.567);
const double tao(7.567);

double simQ = Q;
if (Q == Mantid::EMPTY_DBL())
simQ = 0.20092;

// initialize the fitting function in a Fit algorithm
// Parameter units are assumed in micro-eV, Angstroms, Angstroms**(-1), and nano-seconds. Intensities have arbitrary units
std::ostringstream function_stream;
function_stream << "(composite=Convolution,FixResolution=true,NumDeriv=true;"
<< "name=Gaussian,Height=1.0,PeakCentre=0.0,Sigma=20.0,"
<< "ties=(Height=1.0,PeakCentre=0.0,Sigma=20.0);"
<< "name=InelasticDiffRotDiscreteCircle,N=3,Q=0.5,"
<< "Intensity=" << I
<< "name=InelasticDiffRotDiscreteCircle,N=3,Q=" << simQ
<< ",Intensity=" << I
<< ",Radius=" << R
<< ",Decay=" << tao
<< ",Shift=" << S << ")";
Expand All @@ -292,8 +309,12 @@ class DiffRotDiscreteCircleTest : public CxxTest::TestSuite
function_stream << "(composite=Convolution,FixResolution=true,NumDeriv=true;"
<< "name=Gaussian,Height=1.0,PeakCentre=0.0,Sigma=20.0,"
<< "ties=(Height=1.0,PeakCentre=0.0,Sigma=20.0);"
<< "name=InelasticDiffRotDiscreteCircle,N=3,Q=0.5,"
<< "Intensity=10.0,Radius=1.567,Decay=20.0"
<< "name=InelasticDiffRotDiscreteCircle,N=3";

if (Q != Mantid::EMPTY_DBL())
function_stream << ",Q=" << Q;

function_stream << ",Intensity=10.0,Radius=1.567,Decay=20.0"
<< ",ties=(Radius=" << R << "))";
fitalg.setProperty( "Function", function_stream.str() );
fitalg.setProperty( "InputWorkspace", data_workspace );
Expand Down Expand Up @@ -323,7 +344,6 @@ class DiffRotDiscreteCircleTest : public CxxTest::TestSuite
TS_ASSERT_DELTA( fitalg_structure_factor -> getParameter( "Shift" ), S, 0.00001 ); // allow for a small percent variation
//std::cout << "\nGOAL: Intensity = 47.014, Radius = 1.567, Decay = 7.567\n"; // only for debugging purposes
//std::cout << "OPTIMIZED: Intensity = " << fitalg_structure_factor->getParameter("Intensity") << " Radius = " << fitalg_structure_factor->getParameter("Radius") << " Decay = " << fitalg_structure_factor->getParameter("Decay") << "\n"; // only for debugging purposes

} // runDiffRotDiscreteCircleInelasticTest


Expand Down Expand Up @@ -362,7 +382,6 @@ class DiffRotDiscreteCircleTest : public CxxTest::TestSuite

// some cleaning
Mantid::API::AnalysisDataService::Instance().remove( temp_ws -> getName() );

}


Expand Down Expand Up @@ -420,6 +439,9 @@ class DiffRotDiscreteCircleTest : public CxxTest::TestSuite
// create a data workspace using a Fit algorithm
Mantid::DataObjects::Workspace2D_sptr generateWorkspaceFromFitAlgorithm( Mantid::CurveFitting::Fit & fitalg )
{
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;

// initialize some frequency values centered around zero. Will work as dataX
const size_t M = 1001;
double dataX[ M ];
Expand All @@ -432,8 +454,41 @@ class DiffRotDiscreteCircleTest : public CxxTest::TestSuite
Mantid::API::IFunction_sptr fitalg_function = fitalg.getProperty( "Function" );
fitalg_function -> function( dataXview, dataYvalues );

// create the workspace
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1, M );
// Create the workspace
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1, M);

// Create the instrument
boost::shared_ptr<Instrument> inst(new Instrument("BASIS"));
inst->setReferenceFrame(boost::shared_ptr<ReferenceFrame>(new ReferenceFrame(Y, Z, Left, "")));

// Add the source position
ObjComponent *source = new ObjComponent("moderator", ComponentCreationHelper::createSphere(0.1, V3D(0,0,0), "1"), inst.get());
source->setPos(V3D(0.0, 0.0, -84.0));
inst->add(source);
inst->markAsSource(source);

// Add the sample position
ObjComponent *sample = new ObjComponent("samplePos", ComponentCreationHelper::createSphere(0.1, V3D(0,0,0), "1"), inst.get());
inst->setPos(0.0, 0.0, 0.0);
inst->add(sample);
inst->markAsSamplePos(sample);

// Add a detector
Object_sptr pixelShape = ComponentCreationHelper::createCappedCylinder(
0.05, 0.02, V3D(0.0, 0.0, 0.0), V3D(0., 1.0, 0.), "tube");
Detector *det = new Detector("pixel-1", 1, pixelShape, inst.get()); // ID 5 is a valid detector for BASIS
det->setPos(0.942677, 0.0171308, 4.63343); // Position of first detector on BASIS
inst->add(det);
inst->markAsDetector(det);

// Set the instrument and spec-det mapping
ws->setInstrument(inst);
ws->getSpectrum(0)->addDetectorID(det->getID());

// Set emergy mode and fixed energy
ws->mutableRun().addLogData(new Mantid::Kernel::PropertyWithValue<std::string>("deltaE-mode", "Indirect"));
ws->setEFixed(det->getID(), 2.08275); // EFixed of first detector on BASIS

double fractional_error = 0.01; // error taken as a percent of the signal
for( size_t i = 0; i < M; i++ )
{
Expand All @@ -449,23 +504,4 @@ class DiffRotDiscreteCircleTest : public CxxTest::TestSuite

};




















#endif /* DIFFROTDISCRETECIRCLETEST_H_ */

0 comments on commit a3867d6

Please sign in to comment.