/
DynamicKuboToyabe.cpp
346 lines (293 loc) · 9.74 KB
/
DynamicKuboToyabe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidCurveFitting/Functions/DynamicKuboToyabe.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/Jacobian.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidKernel/PhysicalConstants.h"
#include <iomanip>
#include <sstream>
#include <vector>
namespace Mantid::CurveFitting::Functions {
using namespace API;
using namespace CurveFitting;
using namespace Kernel;
DECLARE_FUNCTION(DynamicKuboToyabe)
void DynamicKuboToyabe::init() {
declareAttribute("BinWidth", Attribute(m_eps));
declareParameter("Asym", 0.2, "Amplitude at time 0");
declareParameter("Delta", 0.2, "Local field");
declareParameter("Field", 0.0, "External field");
declareParameter("Nu", 0.0, "Hopping rate");
}
//--------------------------------------------------------------------------------------------------------------------------------------
// From Numerical Recipes
// Midpoint method
double midpnt(double func(const double, const double, const double), const double a, const double b, const int n,
const double g, const double w0) {
// quote & modified from numerical recipe 2nd edtion (page147)
static double s;
if (n == 1) {
s = (b - a) * func(0.5 * (a + b), g, w0);
return (s);
} else {
double x, tnm, sum, del, ddel;
int it, j;
for (it = 1, j = 1; j < n - 1; j++)
it *= 3;
tnm = it;
del = (b - a) / (3 * tnm);
ddel = del + del;
x = a + 0.5 * del;
sum = 0.0;
for (j = 1; j <= it; j++) {
sum += func(x, g, w0);
x += ddel;
sum += func(x, g, w0);
x += del;
}
s = (s + (b - a) * sum / tnm) / 3.0;
return s;
}
}
// Polynomial interpolation
void polint(double xa[], const double ya[], int n, double x, double &y, double &dy) {
int i, m, ns = 1;
double dif;
dif = fabs(x - xa[1]);
std::vector<double> c(n + 1);
std::vector<double> d(n + 1);
for (i = 1; i <= n; i++) {
double dift;
if ((dift = fabs(x - xa[i])) < dif) {
ns = i;
dif = dift;
}
c[i] = ya[i];
d[i] = ya[i];
}
y = ya[ns--];
for (m = 1; m < n; m++) {
for (i = 1; i <= n - m; i++) {
double den, ho, hp, w;
ho = xa[i] - x;
hp = xa[i + m] - x;
w = c[i + 1] - d[i];
if ((den = ho - hp) == 0.0) { // error message!!!
throw std::runtime_error("Error in routin polint");
}
den = w / den;
d[i] = hp * den;
c[i] = ho * den;
}
y += (dy = (2 * (ns) < (n - m) ? c[ns + 1] : d[ns--]));
}
}
// Integration
double integral(double func(const double, const double, const double), const double a, const double b, const double g,
const double w0) {
const int JMAX = 14;
const int JMAXP = JMAX + 1;
const int K = 5;
int j;
double ss, dss;
double h[JMAXP + 1], s[JMAXP];
h[1] = 1.0;
for (j = 1; j <= JMAX; j++) {
s[j] = midpnt(func, a, b, j, g, w0);
if (j >= K) {
polint(&h[j - K], &s[j - K], K, 0.0, ss, dss);
if (fabs(dss) <= fabs(ss))
return ss;
}
h[j + 1] = h[j] / 9.0;
}
throw std::runtime_error("Too many steps in routine integrate");
}
// End of Numerical Recipes routines
//--------------------------------------------------------------------------------------------------------------------------------------
// f1: function to integrate
double f1(const double x, const double G, const double w0) {
// G = Delta in doc
// x = dummy time variable
return (exp(-G * G * x * x / 2) * sin(w0 * x));
}
// Static Zero Field Kubo Toyabe relaxation function
// Also called Lorentzian Kubo-Toyabe
double ZFKT(const double x, const double G) {
// q = Delta^2 t^2 in doc
const double q = G * G * x * x;
return (0.3333333333 + 0.6666666667 * exp(-0.5 * q) * (1 - q));
}
// Static non-zero field Kubo Toyabe relaxation function
double HKT(const double x, const double G, const double F) {
// q = Delta^2 t^2 in doc
const double q = G * G * x * x;
// Muon gyromagnetic ratio * 2 * PI
const double gm = 2 * M_PI * PhysicalConstants::MuonGyromagneticRatio;
// w = omega_0 in doc
double w;
if (F > 2 * G) {
// Use F
w = gm * F;
} else {
// Use G
w = gm * 2 * G;
}
// r = Delta^2/omega_0^2
const double r = G * G / w / w;
double ig;
if (x > 0 && r > 0) {
// Compute integral
ig = integral(f1, 0.0, x, G, w);
} else {
// Integral is 0
ig = 0;
}
const double ktb = (1 - 2 * r * (1 - exp(-q / 2) * cos(w * x)) + 2 * r * r * w * ig);
if (F > 2 * G) {
// longitudinal Gaussian field
return ktb;
} else {
//
const double kz = ZFKT(x, G);
return kz + F / 2 / G * (ktb - kz);
}
}
// Dynamic Kubo-Toyabe
double DynamicKuboToyabe::getDKT(double t, double G, double F, double v, double eps) const {
const auto tsmax = static_cast<int>(std::ceil(32.768 / eps));
static double oldG = -1., oldV = -1., oldF = -1., oldEps = -1.;
const auto maxTsmax = static_cast<int>(std::ceil(32.768 / m_minEps));
static std::vector<double> gStat(maxTsmax), gDyn(maxTsmax);
if ((G != oldG) || (v != oldV) || (F != oldF) || (eps != oldEps)) {
// If G or v or F or eps have changed with respect to the
// previous call, we need to re-do the computations
if (G != oldG || (F != oldF)) {
// But we only need to
// re-compute gStat if G or F have changed
// Generate static Kubo-Toyabe
if (F == 0) {
for (int k = 0; k < tsmax; k++) {
gStat[k] = ZFKT(k * eps, G);
}
} else {
for (int k = 0; k < tsmax; k++) {
gStat[k] = HKT(k * eps, G, F);
}
}
// Store new G value
oldG = G;
// Store new F value
oldF = F;
}
// Store new v value
oldV = v;
// Store new eps value
oldEps = eps;
double hop = v * eps;
// Generate dynamic Kubo Toyabe
for (int k = 0; k < tsmax; k++) {
double y = gStat[k];
// do integration
for (int j = k - 1; j > 0; j--) {
y = y * (1 - hop) + hop * gDyn[k - j] * gStat[j];
}
gDyn[k] = y;
}
}
// Interpolate table
// If beyond end, extrapolate
auto x = int(fabs(t) / eps);
if (x > tsmax - 2)
x = tsmax - 2;
double xe = (fabs(t) / eps) - x;
return gDyn[x] * (1 - xe) + xe * gDyn[x + 1];
}
// Dynamic Kubo Toyabe function
void DynamicKuboToyabe::function1D(double *out, const double *xValues, const size_t nData) const {
const double &A = getParameter("Asym");
const double &G = fabs(getParameter("Delta"));
const double &F = fabs(getParameter("Field"));
const double &v = fabs(getParameter("Nu"));
// Zero hopping rate
if (v == 0.0) {
// Zero external field
if (F == 0.0) {
for (size_t i = 0; i < nData; i++) {
out[i] = A * ZFKT(xValues[i], G);
}
}
// Non-zero external field
else {
for (size_t i = 0; i < nData; i++) {
out[i] = A * HKT(xValues[i], G, F);
}
}
}
// Non-zero hopping rate
else {
for (size_t i = 0; i < nData; i++) {
out[i] = A * getDKT(xValues[i], G, F, v, m_eps);
}
}
}
//----------------------------------------------------------------------------------------------
/** Constructor
*/
DynamicKuboToyabe::DynamicKuboToyabe() : m_eps(0.05), m_minEps(0.001), m_maxEps(0.1) {}
//----------------------------------------------------------------------------------------------
/** Function to calculate derivative numerically
*/
void DynamicKuboToyabe::functionDeriv(const API::FunctionDomain &domain, API::Jacobian &jacobian) {
calNumericalDeriv(domain, jacobian);
}
//----------------------------------------------------------------------------------------------
/** Function to calculate derivative analytically
*/
void DynamicKuboToyabe::functionDeriv1D(API::Jacobian * /*jacobian*/, const double * /*xValues*/,
const size_t /*nData*/) {
throw Mantid::Kernel::Exception::NotImplementedError("functionDeriv1D is not implemented for DynamicKuboToyabe.");
}
//----------------------------------------------------------------------------------------------
/** Set new value of the i-th parameter
* @param i :: parameter index
* @param value :: new value
*/
void DynamicKuboToyabe::setActiveParameter(size_t i, double value) { setParameter(i, fabs(value), false); }
//----------------------------------------------------------------------------------------------
/** Set Attribute
* @param attName :: The attribute name. If it is not "eps" exception is thrown.
* @param att :: A double attribute containing a new positive value.
*/
void DynamicKuboToyabe::setAttribute(const std::string &attName, const API::IFunction::Attribute &att) {
if (attName == "BinWidth") {
double newVal = att.asDouble();
if (newVal < 0) {
clearAllParameters();
throw std::invalid_argument("DKT: Attribute BinWidth cannot be negative.");
} else if (newVal < m_minEps) {
clearAllParameters();
std::stringstream ss;
ss << "DKT: Attribute BinWidth too small (BinWidth < " << std::setprecision(3) << m_minEps << ")";
throw std::invalid_argument(ss.str());
} else if (newVal > m_maxEps) {
clearAllParameters();
std::stringstream ss;
ss << "DKT: Attribute BinWidth too large (BinWidth > " << std::setprecision(3) << m_maxEps << ")";
throw std::invalid_argument(ss.str());
}
if (!nParams()) {
init();
}
m_eps = newVal;
IFunction::setAttribute(attName, Attribute(m_eps));
} else {
throw std::invalid_argument("DynamicKuboToyabe: Unknown attribute " + attName);
}
}
} // namespace Mantid::CurveFitting::Functions