Mantid
Loading...
Searching...
No Matches
ThermalNeutronDtoTOFFunction.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10#include "MantidKernel/System.h"
11#include <cmath>
12#include <gsl/gsl_sf_erf.h>
13
14using namespace Mantid::API;
15
16using namespace std;
17
19
20using namespace CurveFitting;
21
22//----------------------------------------------------------------------------------------------
23DECLARE_FUNCTION(ThermalNeutronDtoTOFFunction)
24
25
29
31 declareParameter("Dtt1", 1.0); // 0
32 declareParameter("Dtt1t", 1.0); // 1
33 declareParameter("Dtt2t", 1.0); // 2
34 declareParameter("Zero", 0.0); // 3
35 declareParameter("Zerot", 0.0); // 4
36
37 declareParameter("Width", 1.0); // 5
38 declareParameter("Tcross", 1.0); // 6
39}
40
44void ThermalNeutronDtoTOFFunction::function1D(double *out, const double *xValues, const size_t nData) const {
45 double dtt1 = getParameter("Dtt1");
46 double dtt1t = getParameter("Dtt1t");
47 double dtt2t = getParameter("Dtt2t");
48 double zero = getParameter("Zero");
49 double zerot = getParameter("Zerot");
50 double width = getParameter("Width");
51 double tcross = getParameter("Tcross");
52
53 for (size_t i = 0; i < nData; ++i) {
54 // out[i] = corefunction(xValues[i], dtt1, dtt1t, dtt2t, zero, zerot, width,
55 // tcross);
56 out[i] = calThermalNeutronTOF(xValues[i], dtt1, dtt1t, dtt2t, zero, zerot, width, tcross);
57 }
58}
59
60//------------------------------------------------------------------------------------------------
64void ThermalNeutronDtoTOFFunction::function1D(vector<double> &out, const vector<double> &xValues) const {
65 double dtt1 = getParameter(0);
66 double dtt1t = getParameter(1);
67 double dtt2t = getParameter(2);
68 double zero = getParameter(3);
69 double zerot = getParameter(4);
70 double width = getParameter(5);
71 double tcross = getParameter(6);
72
73 size_t nData = out.size();
74
75 for (size_t i = 0; i < nData; ++i) {
76 out[i] = calThermalNeutronTOF(xValues[i], dtt1, dtt1t, dtt2t, zero, zerot, width, tcross);
77 }
78}
79
80void ThermalNeutronDtoTOFFunction::functionDeriv1D(Jacobian *out, const double *xValues, const size_t nData) {
81 // 1. Get hold all parameters
82 const double dtt1 = getParameter("Dtt1");
83 const double dtt1t = getParameter("Dtt1t");
84 const double dtt2t = getParameter("Dtt2t");
85 const double zero = getParameter("Zero");
86 const double zerot = getParameter("Zerot");
87 const double width = getParameter("Width");
88 const double tcross = getParameter("Tcross");
89
90 // 2. Calcualtion
91 for (size_t i = 0; i < nData; ++i) {
92 // a) Some calcualtion
93 double x = xValues[i];
94 double n = 0.5 * gsl_sf_erfc(width * (tcross - 1 / x));
95 double u = width * (tcross - 1 / x);
96
97 double deriv_dtt1 = n * x;
98 double deriv_dtt1t = (1 - n) * x;
99 double deriv_dtt2t = (n - 1) / x;
100 double deriv_zero = n;
101 double deriv_zerot = (1 - n);
102 double deriv_width =
103 -(zero + dtt1 * x - zerot - dtt1t * x + dtt2t / x) * exp(-u * u) / sqrt(M_PI) * (tcross - 1 / x);
104 double deriv_tcross = -(zero + dtt1 * x - zerot - dtt1t * x + dtt2t / x) * exp(-u * u) / sqrt(M_PI) * width;
105
106 // b) Set
107 out->set(i, 0, deriv_dtt1);
108 out->set(i, 1, deriv_dtt1t);
109 out->set(i, 2, deriv_dtt2t);
110 out->set(i, 3, deriv_zero);
111 out->set(i, 4, deriv_zerot);
112 out->set(i, 5, deriv_width);
113 out->set(i, 6, deriv_tcross);
114 }
115}
116
119void ThermalNeutronDtoTOFFunction::functionDerivLocal(API::Jacobian * /*unused*/, const double * /*unused*/,
120 const size_t /*unused*/) {
121 throw Mantid::Kernel::Exception::NotImplementedError("functionDerivLocal is not implemented for "
122 "ThermalNeutronDtoTOFFunction.");
123}
124
125} // 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.
void functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) override
Derivative.
void function1D(double *out, const double *xValues, const size_t nData) const override
Override.
void functionDerivLocal(API::Jacobian *, const double *, const size_t)
Derivative.
Marks code as not implemented yet.
Definition: Exception.h:138
double calThermalNeutronTOF(double dh, double dtt1, double dtt1t, double dtt2t, double zero, double zerot, double width, double tcross)
Calcualte TOF from d-spacing value for thermal neutron.
STL namespace.