-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re #5851. Added the tests. Moved the validator to Kernel.
- Loading branch information
1 parent
5c48103
commit 96240db
Showing
7 changed files
with
409 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
262 changes: 262 additions & 0 deletions
262
Code/Mantid/Framework/CurveFitting/test/DampingMinimizerTest.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
#ifndef CURVEFITTING_DAMPINGTEST_H_ | ||
#define CURVEFITTING_DAMPINGTEST_H_ | ||
|
||
#include <cxxtest/TestSuite.h> | ||
|
||
#include "MantidCurveFitting/CostFuncLeastSquares.h" | ||
#include "MantidCurveFitting/DampingMinimizer.h" | ||
#include "MantidCurveFitting/UserFunction.h" | ||
#include "MantidAPI/FunctionDomain1D.h" | ||
#include "MantidAPI/FunctionValues.h" | ||
#include "MantidCurveFitting/BoundaryConstraint.h" | ||
|
||
#include <sstream> | ||
|
||
using namespace Mantid; | ||
using namespace Mantid::CurveFitting; | ||
using namespace Mantid::API; | ||
|
||
class DampingMinimizerTest : public CxxTest::TestSuite | ||
{ | ||
public: | ||
|
||
void test_Gaussian() | ||
{ | ||
API::FunctionDomain1D_sptr domain(new API::FunctionDomain1DVector(0.0,10.0,20)); | ||
API::FunctionValues mockData(*domain); | ||
UserFunction dataMaker; | ||
dataMaker.setAttributeValue("Formula","a*x+b+h*exp(-s*x^2)"); | ||
dataMaker.setParameter("a",1.1); | ||
dataMaker.setParameter("b",2.2); | ||
dataMaker.setParameter("h",3.3); | ||
dataMaker.setParameter("s",0.2); | ||
dataMaker.function(*domain,mockData); | ||
|
||
API::FunctionValues_sptr values(new API::FunctionValues(*domain)); | ||
values->setFitDataFromCalculated(mockData); | ||
values->setFitWeights(1.0); | ||
|
||
boost::shared_ptr<UserFunction> fun(new UserFunction); | ||
fun->setAttributeValue("Formula","a*x+b+h*exp(-s*x^2)"); | ||
fun->setParameter("a",1.); | ||
fun->setParameter("b",2.); | ||
fun->setParameter("h",3.); | ||
fun->setParameter("s",0.1); | ||
|
||
boost::shared_ptr<CostFuncLeastSquares> costFun(new CostFuncLeastSquares); | ||
costFun->setFittingFunction(fun,domain,values); | ||
|
||
DampingMinimizer s; | ||
s.initialize(costFun); | ||
TS_ASSERT( s.existsProperty("Damping") ); | ||
double damping = s.getProperty("Damping"); | ||
TS_ASSERT_EQUALS( damping, 0.0 ); | ||
|
||
TS_ASSERT(s.minimize()); | ||
TS_ASSERT_DELTA(costFun->val(),0.0,0.0001); | ||
TS_ASSERT_DELTA(fun->getParameter("a"),1.1,0.001); | ||
TS_ASSERT_DELTA(fun->getParameter("b"),2.2,0.001); | ||
TS_ASSERT_DELTA(fun->getParameter("h"),3.3,0.001); | ||
TS_ASSERT_DELTA(fun->getParameter("s"),0.2,0.001); | ||
TS_ASSERT_EQUALS(s.getError(),"success"); | ||
} | ||
|
||
void test_Gaussian_with_damping() | ||
{ | ||
API::FunctionDomain1D_sptr domain(new API::FunctionDomain1DVector(0.0,10.0,20)); | ||
API::FunctionValues mockData(*domain); | ||
UserFunction dataMaker; | ||
dataMaker.setAttributeValue("Formula","a*x+b+h*exp(-s*x^2)"); | ||
dataMaker.setParameter("a",1.1); | ||
dataMaker.setParameter("b",2.2); | ||
dataMaker.setParameter("h",3.3); | ||
dataMaker.setParameter("s",0.2); | ||
dataMaker.function(*domain,mockData); | ||
|
||
API::FunctionValues_sptr values(new API::FunctionValues(*domain)); | ||
values->setFitDataFromCalculated(mockData); | ||
values->setFitWeights(1.0); | ||
|
||
boost::shared_ptr<UserFunction> fun(new UserFunction); | ||
fun->setAttributeValue("Formula","a*x+b+h*exp(-s*x^2)"); | ||
fun->setParameter("a",1.); | ||
fun->setParameter("b",2.); | ||
fun->setParameter("h",3.); | ||
fun->setParameter("s",0.1); | ||
|
||
boost::shared_ptr<CostFuncLeastSquares> costFun(new CostFuncLeastSquares); | ||
costFun->setFittingFunction(fun,domain,values); | ||
|
||
DampingMinimizer s; | ||
s.initialize(costFun); | ||
s.setProperty( "Damping", 100.0 ); | ||
double damping = s.getProperty("Damping"); | ||
TS_ASSERT_EQUALS( damping, 100.0 ); | ||
|
||
TS_ASSERT(s.minimize()); | ||
TS_ASSERT_DELTA(costFun->val(),0.0,0.0002); | ||
TS_ASSERT_DELTA(fun->getParameter("a"),1.0973,0.001); | ||
TS_ASSERT_DELTA(fun->getParameter("b"),2.2200,0.001); | ||
TS_ASSERT_DELTA(fun->getParameter("h"),3.2795,0.001); | ||
TS_ASSERT_DELTA(fun->getParameter("s"),0.2014,0.001); | ||
TS_ASSERT_EQUALS(s.getError(),"success"); | ||
} | ||
|
||
void test_Gaussian_fixed() | ||
{ | ||
API::FunctionDomain1D_sptr domain(new API::FunctionDomain1DVector(0.0,10.0,20)); | ||
API::FunctionValues mockData(*domain); | ||
UserFunction dataMaker; | ||
dataMaker.setAttributeValue("Formula","a*x+b+h*exp(-s*x^2)"); | ||
dataMaker.setParameter("a",1.1); | ||
dataMaker.setParameter("b",2.2); | ||
dataMaker.setParameter("h",3.3); | ||
dataMaker.setParameter("s",0.2); | ||
dataMaker.function(*domain,mockData); | ||
|
||
API::FunctionValues_sptr values(new API::FunctionValues(*domain)); | ||
values->setFitDataFromCalculated(mockData); | ||
values->setFitWeights(1.0); | ||
|
||
boost::shared_ptr<UserFunction> fun(new UserFunction); | ||
fun->setAttributeValue("Formula","a*x+b+h*exp(-s*x^2)"); | ||
fun->setParameter("a",1.); | ||
fun->setParameter("b",2.); | ||
fun->setParameter("h",3.); | ||
fun->setParameter("s",0.1); | ||
fun->fix(0); | ||
|
||
boost::shared_ptr<CostFuncLeastSquares> costFun(new CostFuncLeastSquares); | ||
costFun->setFittingFunction(fun,domain,values); | ||
TS_ASSERT_EQUALS(costFun->nParams(),3); | ||
|
||
DampingMinimizer s; | ||
s.initialize(costFun); | ||
TS_ASSERT(s.minimize()); | ||
TS_ASSERT_DELTA(costFun->val(),0.2,0.01); | ||
TS_ASSERT_DELTA(fun->getParameter("a"),1.,0.000001); | ||
TS_ASSERT_DELTA(fun->getParameter("b"),2.90,0.01); | ||
TS_ASSERT_DELTA(fun->getParameter("h"),2.67,0.01); | ||
TS_ASSERT_DELTA(fun->getParameter("s"),0.27,0.01); | ||
TS_ASSERT_EQUALS(s.getError(),"success"); | ||
} | ||
|
||
|
||
void test_Gaussian_tied() | ||
{ | ||
API::FunctionDomain1D_sptr domain(new API::FunctionDomain1DVector(0.0,10.0,20)); | ||
API::FunctionValues mockData(*domain); | ||
UserFunction dataMaker; | ||
dataMaker.setAttributeValue("Formula","a*x+b+h*exp(-s*x^2)"); | ||
dataMaker.setParameter("a",1.1); | ||
dataMaker.setParameter("b",2.2); | ||
dataMaker.setParameter("h",3.3); | ||
dataMaker.setParameter("s",0.2); | ||
dataMaker.function(*domain,mockData); | ||
|
||
API::FunctionValues_sptr values(new API::FunctionValues(*domain)); | ||
values->setFitDataFromCalculated(mockData); | ||
values->setFitWeights(1.0); | ||
|
||
boost::shared_ptr<UserFunction> fun(new UserFunction); | ||
fun->setAttributeValue("Formula","a*x+b+h*exp(-s*x^2)"); | ||
fun->setParameter("a",1.); | ||
fun->setParameter("b",2.); | ||
fun->setParameter("h",3.); | ||
fun->setParameter("s",0.1); | ||
fun->tie("a","1"); | ||
|
||
boost::shared_ptr<CostFuncLeastSquares> costFun(new CostFuncLeastSquares); | ||
costFun->setFittingFunction(fun,domain,values); | ||
TS_ASSERT_EQUALS(costFun->nParams(),3); | ||
|
||
DampingMinimizer s; | ||
s.initialize(costFun); | ||
TS_ASSERT(s.minimize()); | ||
TS_ASSERT_DELTA(costFun->val(),0.2,0.01); | ||
TS_ASSERT_DELTA(fun->getParameter("a"),1.,0.000001); | ||
TS_ASSERT_DELTA(fun->getParameter("b"),2.90,0.01); | ||
TS_ASSERT_DELTA(fun->getParameter("h"),2.67,0.01); | ||
TS_ASSERT_DELTA(fun->getParameter("s"),0.27,0.01); | ||
TS_ASSERT_EQUALS(s.getError(),"success"); | ||
} | ||
|
||
void test_Gaussian_tied_with_formula() | ||
{ | ||
API::FunctionDomain1D_sptr domain(new API::FunctionDomain1DVector(0.0,10.0,20)); | ||
API::FunctionValues mockData(*domain); | ||
UserFunction dataMaker; | ||
dataMaker.setAttributeValue("Formula","a*x+b+h*exp(-s*x^2)"); | ||
dataMaker.setParameter("a",1.1); | ||
dataMaker.setParameter("b",2.2); | ||
dataMaker.setParameter("h",3.3); | ||
dataMaker.setParameter("s",0.2); | ||
dataMaker.function(*domain,mockData); | ||
|
||
API::FunctionValues_sptr values(new API::FunctionValues(*domain)); | ||
values->setFitDataFromCalculated(mockData); | ||
values->setFitWeights(1.0); | ||
|
||
boost::shared_ptr<UserFunction> fun(new UserFunction); | ||
fun->setAttributeValue("Formula","a*x+b+h*exp(-s*x^2)"); | ||
fun->setParameter("a",1.); | ||
fun->setParameter("b",2.); | ||
fun->setParameter("h",3.); | ||
fun->setParameter("s",0.1); | ||
fun->tie("b","2*a+0.1"); | ||
|
||
boost::shared_ptr<CostFuncLeastSquares> costFun(new CostFuncLeastSquares); | ||
costFun->setFittingFunction(fun,domain,values); | ||
TS_ASSERT_EQUALS(costFun->nParams(),3); | ||
|
||
DampingMinimizer s; | ||
s.initialize(costFun); | ||
TS_ASSERT(s.minimize()); | ||
TS_ASSERT_DELTA(costFun->val(),0.002,0.01); | ||
double a = fun->getParameter("a"); | ||
TS_ASSERT_DELTA(a,1.0895,0.01); | ||
TS_ASSERT_DELTA(fun->getParameter("b"),2*a+0.1,0.0001); | ||
TS_ASSERT_DELTA(fun->getParameter("h"),3.23,0.01); | ||
TS_ASSERT_DELTA(fun->getParameter("s"),0.207,0.001); | ||
TS_ASSERT_EQUALS(s.getError(),"success"); | ||
} | ||
|
||
void test_Linear_constrained() | ||
{ | ||
API::FunctionDomain1D_sptr domain(new API::FunctionDomain1DVector(0.0,10.0,20)); | ||
API::FunctionValues mockData(*domain); | ||
UserFunction dataMaker; | ||
dataMaker.setAttributeValue("Formula","a*x+b"); | ||
dataMaker.setParameter("a",1.1); | ||
dataMaker.setParameter("b",2.2); | ||
dataMaker.function(*domain,mockData); | ||
|
||
API::FunctionValues_sptr values(new API::FunctionValues(*domain)); | ||
values->setFitDataFromCalculated(mockData); | ||
values->setFitWeights(1.0); | ||
|
||
boost::shared_ptr<UserFunction> fun(new UserFunction); | ||
fun->setAttributeValue("Formula","a*x+b"); | ||
fun->setParameter("a",1.); | ||
fun->setParameter("b",2.); | ||
|
||
BoundaryConstraint* constraint = new BoundaryConstraint(fun.get(),"a",0,0.5); | ||
|
||
fun->addConstraint(constraint); | ||
|
||
boost::shared_ptr<CostFuncLeastSquares> costFun(new CostFuncLeastSquares); | ||
costFun->setFittingFunction(fun,domain,values); | ||
TS_ASSERT_EQUALS(costFun->nParams(),2); | ||
|
||
DampingMinimizer s; | ||
s.initialize(costFun); | ||
TS_ASSERT(s.minimize()); | ||
|
||
TS_ASSERT_DELTA( fun->getParameter("a"), 0.5, 0.1 ); | ||
TS_ASSERT_DELTA( fun->getParameter("b"), 5.0, 0.1 ); | ||
TS_ASSERT_EQUALS( s.getError(), "success" ); | ||
} | ||
|
||
}; | ||
|
||
#endif /*CURVEFITTING_DAMPINGTEST_H_*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.