Mantid
Loading...
Searching...
No Matches
MagneticOrderParameter.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2021 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
12
13#include <cmath>
14
16
17using namespace CurveFitting;
18
19using namespace Kernel;
20
21using namespace API;
22
23DECLARE_FUNCTION(MagneticOrderParameter)
24
26 declareParameter("A0", 1.0, "amplitude");
27 declareParameter("Alpha", 2.0, "balance parameter");
28 declareParameter("Beta", 0.5, "critical exponent");
29 declareParameter("CriticalTemp", 1.0, "critical temperature");
30}
31
32void MagneticOrderParameter::function1D(double *out, const double *xValues, const size_t nData) const {
33 const double Amp = getParameter("A0");
34 const double Alpha = getParameter("Alpha");
35 const double Beta = getParameter("Beta");
36 const double Tc = getParameter("CriticalTemp");
37
38 for (size_t i = 0; i < nData; i++) {
39 double formula = Amp * pow((1 - pow(xValues[i] / Tc, Alpha)), Beta);
40 // check whether the formula has returned an nan and if so return 0 instead
41 if (!std::isfinite(formula)) {
42 formula = 0;
43 }
44 out[i] = formula;
45 }
46}
47
48void MagneticOrderParameter::functionDeriv1D(Jacobian *out, const double *xValues, const size_t nData) {
49 const double Amp = getParameter("A0");
50 const double Alpha = getParameter("Alpha");
51 const double Beta = getParameter("Beta");
52 const double Tc = getParameter("CriticalTemp");
53
54 for (size_t i = 0; i < nData; i++) {
55 double xCalc = (xValues[i] / Tc);
56 double xCalcAlpha = pow(xCalc, Alpha);
57
58 double diffAmp = pow((1 - xCalcAlpha), Beta);
59 // if diffAmp is NaN or inf we should use 0 instead.
60 if (!std::isfinite(diffAmp)) {
61 diffAmp = 0;
62 }
63
64 double diffAmpMin = pow((1 - xCalcAlpha), Beta - 1);
65 // if diffAmpMin is NaN or inf we should use 0 instead.
66 if (!std::isfinite(diffAmpMin)) {
67 diffAmpMin = 0;
68 }
69
70 double diffAlpha = -Amp * Beta * xCalcAlpha * log(xCalc) * diffAmpMin;
71 double diffBeta = Amp * diffAmp * log(1 - xCalcAlpha);
72 // we can only take a log of a positive number. If thats not possible the diffBeta should be 0.
73 if ((1 - xCalcAlpha) <= 0.0) {
74 diffBeta = 0;
75 }
76 double diffTc = (Amp * Alpha * Beta * xCalcAlpha * diffAmpMin) / Tc;
77
78 out->set(i, 0, diffAmp);
79 out->set(i, 1, diffAlpha);
80 out->set(i, 2, diffBeta);
81 out->set(i, 3, diffTc);
82 }
83}
84
85} // namespace Mantid::CurveFitting::Functions
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
Represents the Jacobian in IFitFunction::functionDeriv.
Definition: Jacobian.h:22
virtual void set(size_t iY, size_t iP, double value)=0
Set a value to a Jacobian matrix element.
double getParameter(size_t i) const override
Get i-th parameter.
Provide Magnetic Order Paramtere fit function interface to IFunction.
void function1D(double *out, const double *xValues, const size_t nData) const override
Function you want to fit to.
void functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) override
Derivatives of function with respect to active parameters.