Skip to content

Commit

Permalink
refs #8060 Unit test for MullerAnsatz
Browse files Browse the repository at this point in the history
  • Loading branch information
abuts committed Apr 29, 2014
1 parent 2b3951c commit ffb3892
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 48 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/MDAlgorithms/CMakeLists.txt
Expand Up @@ -199,6 +199,7 @@ set ( TEST_FILES
MinusMDTest.h
ModeratorChopperResolutionTest.h
MultiplyMDTest.h
MullerAnsatzTest.h
NotMDTest.h
OrMDTest.h
PlusMDTest.h
Expand Down
@@ -1,5 +1,5 @@
#ifndef MANTID_MDALGORITHMS_STRONTIUM122_H_
#define MANTID_MDALGORITHMS_STRONTIUM122_H_
#ifndef MANTID_MDALGORITHMS_MULLERANSATZ_H_
#define MANTID_MDALGORITHMS_MULLERANSATZ_H_
/**
Copyright © 2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
Expand Down Expand Up @@ -34,6 +34,7 @@ namespace Mantid
*/
class DLLExport MullerAnsatz : public ForegroundModel
{
public:
enum ChainDirection
{
Along_a,
Expand Down
Expand Up @@ -17,7 +17,7 @@ namespace Mantid
struct AnsatzParameters
{
/*
p(1) A, the intensity scale factor in the Mueller Ansatz formalism.
! p(1) A, the intensity scale factor in the Mueller Ansatz formalism.
! p(2) J (maximum of lower bound is at pi*J/2)
!
! p(19) = 1,2,3 for chains along a*, b*, c* respectively
Expand All @@ -28,11 +28,10 @@ namespace Mantid
! Default is isotropic form factor
*/
/// Enumerate parameter positions
enum {Ampliture,J_bound};
/// Enumerate parameter positions and the Number of parameters
enum {Ampliture,J_coupling,NPARAMS};


/// Number of parameters
enum{NPARAMS = 2};
/// Parameter names, same order as above
static char * PAR_NAMES[NPARAMS];
/// N attrs
Expand All @@ -42,7 +41,7 @@ namespace Mantid

};

char* AnsatzParameters::PAR_NAMES[AnsatzParameters::NPARAMS] = {"Amplitude","J_boundLimit"};
char* AnsatzParameters::PAR_NAMES[AnsatzParameters::NPARAMS] = {"Amplitude","J_coupling"};
char* AnsatzParameters::ATTR_NAMES[AnsatzParameters::NATTS] = {"IonName","ChainDirection","MagneticFFDirection"};
//static
/**
Expand All @@ -51,20 +50,22 @@ namespace Mantid
void MullerAnsatz::init()
{
// Default form factor. Can be overridden with the FormFactorIon attribute
//setFormFactorIon("Fe2");
//setFormFactorIon("Cu2");

// Declare parameters that participate in fitting
for(unsigned int i = 0; i < AnsatzParameters::NPARAMS; ++i)
{
declareParameter(AnsatzParameters::PAR_NAMES[i], 0.0);
}

// Declare fixed attributes
declareAttribute(AnsatzParameters::ATTR_NAMES[0], API::IFunction::Attribute("Fe"));
for(unsigned int i = 1; i < AnsatzParameters::NATTS; ++i)
{
declareAttribute(AnsatzParameters::ATTR_NAMES[i], API::IFunction::Attribute(int(1)));
}
// Declare fixed attributes defaults
auto CoIon = API::IFunction::Attribute("Cu2");
declareAttribute(AnsatzParameters::ATTR_NAMES[0], CoIon);
declareAttribute(AnsatzParameters::ATTR_NAMES[1], API::IFunction::Attribute(int(Along_c)));
declareAttribute(AnsatzParameters::ATTR_NAMES[2], API::IFunction::Attribute(int(Isotropic)));

setFormFactorIon(CoIon.asString());

}

/**
Expand Down Expand Up @@ -134,7 +135,7 @@ namespace Mantid
const double qx(point[0]), qy(point[1]), qz(point[2]), eps(point[3]);
const double qsqr = qx*qx + qy*qy + qz*qz;
const double Amplitude = getCurrentParameterValue(AnsatzParameters::Ampliture);
const double J_bound = getCurrentParameterValue(AnsatzParameters::J_bound);
const double J_coupling = getCurrentParameterValue(AnsatzParameters::J_coupling);

// const double epssqr = eps*eps;

Expand All @@ -158,14 +159,14 @@ namespace Mantid
}
case(Along_c):
{
qchain = qk;
qchain = ql;
break;
}
default:
qchain = ql;
}
double wl = M_PI_2*J_bound*std::fabs(sin(TWO_PI*qchain));
double wu = M_PI*J_bound*std::fabs(sin(M_PI*qchain));
double wl = M_PI_2*J_coupling*std::fabs(sin(TWO_PI*qchain));
double wu = M_PI*J_coupling*std::fabs(sin(M_PI*qchain));
if (eps > (wl+FLT_EPSILON) && eps <= wu)
{
// Orientation of the hole orbital
Expand Down Expand Up @@ -205,35 +206,6 @@ namespace Mantid
else
return 0;

/*
iorient = nint(p(20))
qsqr = qx**2 + qy**2 + qz**2
if (iorient .eq. 1) then
cos_beta_sqr = (qh*arlu(1))**2 / qsqr
elseif (iorient .eq. 2) then
cos_beta_sqr = (qk*arlu(2))**2 / qsqr
elseif (iorient .eq. 3) then
cos_beta_sqr = (ql*arlu(3))**2 / qsqr
endif
! Get spectral weight
wl = piby2*p(2)*abs(sin(twopi*qchain))
wu = pi*p(2)*abs(sin(pi*qchain))
if (eps .gt. (wl+small) .and. eps .le. wu) then
if (iorient .ge. 1 .and. iorient .le. 3) then
weight = p(1)*(sigma_mag/pi)* (bose(eps,temp)/eps) *
& (amff_cu3d(qsqr,cos_beta_sqr))**2 / sqrt(eps**2-wl**2)
else
weight = p(1)*(sigma_mag/pi)* (bose(eps,temp)/eps) * (form_table(qsqr))**2 / sqrt(eps**2-wl**2)
endif
else
weight = 0.0d0
endif
*/



return weight;
Expand Down
114 changes: 114 additions & 0 deletions Code/Mantid/Framework/MDAlgorithms/test/MullerAnsatzTest.h
@@ -0,0 +1,114 @@
#ifndef MANTID_MDALGORITHMS_MULLERANSATZTEST_H_
#define MANTID_MDALGORITHMS_MULLERANSATZTEST_H_

#include "MantidMDAlgorithms/Quantification/Models/MullerAnsatz.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MDFittingTestHelpers.h"

#include <cxxtest/TestSuite.h>

class Strontium122Test : public CxxTest::TestSuite
{
private:
class FakeFGModelFitFunction :
public virtual Mantid::API::IFunctionMD, public virtual Mantid::API::ParamFunction
{
public:
FakeFGModelFitFunction(Mantid::MDAlgorithms::ForegroundModel & fgModel)
{
fgModel.setFunctionUnderMinimization(*this);
const size_t nparams = fgModel.nParams();
for(size_t i = 0; i < nparams; ++i)
{
this->declareParameter(fgModel.parameterName(i), fgModel.getInitialParameterValue(i),
fgModel.parameterDescription(i));
}

setParameter("Amplitude", 0.7);
setParameter("J_coupling", 2.1);
}

/// Returns the number of attributes associated with the function
virtual size_t nAttributes()const{return 2;}
/// Returns a list of attribute names
virtual std::vector<std::string> getAttributeNames()const
{
auto names = std::vector<std::string>(3);
names[0] = "IonName";
names[1] = "ChainDirection";
names[2] = "MagneticFFDirection";
return names;
}


std::string name() const { return "FakeFGModelFitFunction"; }
double functionMD(const Mantid::API::IMDIterator&) const
{
return 0.0;
}
};

public:

void test_Initialized_Model_Has_two_Parameters()
{
using Mantid::MDAlgorithms::MullerAnsatz;
MullerAnsatz Cu2p;

TS_ASSERT_EQUALS(Cu2p.nParams(), 0);
Cu2p.initialize();
TS_ASSERT_EQUALS(Cu2p.nParams(), 2);
}

void test_MANS_Has_DefaultIon_As_Cu2()
{
using Mantid::MDAlgorithms::MullerAnsatz;
using Mantid::MDAlgorithms::ForegroundModel;
MullerAnsatz Cu2Default;
Cu2Default.initialize();
Cu2Default.setAttributeValue("Amplitude",0.67);
Cu2Default.setAttributeValue("J_coupling", 2.1);
double valueWithDefault = calculateTestModelWeight(Cu2Default);
TS_ASSERT_DELTA(0.016787062635810316, valueWithDefault, 1e-10); // Check the absolute value is correct

MullerAnsatz Cu2Def;
Cu2Def.initialize();
Cu2Def.setAttributeValue("IonName", "Cu2");
Cu2Def.setAttributeValue("ChainDirection", MullerAnsatz::Along_c);
Cu2Def.setAttributeValue("MagneticFFDirection", MullerAnsatz::Isotropic);

// Same test but set the ion to check they match
double valueWithAttrSet = calculateTestModelWeight(Cu2Def);

TS_ASSERT_DELTA(valueWithDefault, valueWithAttrSet, 1e-10);


}

private:

double calculateTestModelWeight(Mantid::MDAlgorithms::MullerAnsatz & model)
{
using Mantid::MDAlgorithms::ForegroundModel;
FakeFGModelFitFunction fakeFitFunction(model); // Use fit function to access current fit values

const double qx(7.7), qy(6.5), qz(4.3), deltaE(3.3);
const double qOmega[4] = {qx, qy, qz, deltaE};
Mantid::API::ExperimentInfo experimentDescr;
auto lattice = new Mantid::Geometry::OrientedLattice(5.51,12.298,5.57);
Mantid::Kernel::V3D uVec(9.800000e-03,9.996000e-01,9.700000e-03),
vVec(-3.460000e-02,-4.580000e-02,9.992000e-01);
lattice->setUFromVectors(uVec, vVec);

experimentDescr.mutableSample().setOrientedLattice(lattice);
experimentDescr.mutableRun().addProperty("temperature_log", 6.0);

double weight(-1.0);
const ForegroundModel & MulFunction = model; // scatteringIntensity is private concrete model
TS_ASSERT_THROWS_NOTHING( weight = MulFunction.scatteringIntensity(experimentDescr, std::vector<double>(qOmega, qOmega + 4)) );
return weight;
}

};

#endif // MANTID_MDALGORITHMS_STRONTIUM122_H_

0 comments on commit ffb3892

Please sign in to comment.