-
Notifications
You must be signed in to change notification settings - Fork 16
/
function1D.cpp
427 lines (387 loc) · 16 KB
/
function1D.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2024 Team Dissolve and contributors
#define _USE_MATH_DEFINES
#include "math/function1D.h"
#include "templates/algorithms.h"
#include <math.h>
#include <map>
/*
* One-Dimensional Function Definition
*/
Function1DDefinition::Function1DDefinition(const std::vector<std::string> ¶meterNames,
const Flags<FunctionProperties::FunctionProperty> &properties, Function1DSetup setup,
Function1DXOmega y, Function1DXOmega dYdX, Function1DXOmega yFT,
Function1DOmega norm)
: parameterNames_(parameterNames), properties_(properties), setup_(std::move(setup)), y_(std::move(y)),
dYdX_(std::move(dYdX)), yFT_(std::move(yFT)), normaliser_(std::move(norm))
{
}
// Return number of parameters the function requires
int Function1DDefinition::nParameters() const { return parameterNames_.size(); }
// Return parameter names
const std::vector<std::string> &Function1DDefinition::parameterNames() const { return parameterNames_; }
// Return properties of the function
const Flags<FunctionProperties::FunctionProperty> &Function1DDefinition::properties() const { return properties_; }
// Return function for setup
Function1DSetup Function1DDefinition::setup() const { return setup_; }
// Return function for y value at specified x, omega
Function1DXOmega Function1DDefinition::y() const { return y_; }
// Return function for first derivative of y with respect to x (at specified omega)
Function1DXOmega Function1DDefinition::dYdX() const { return dYdX_; }
// Return function for FT of y value at the specified x, omega
Function1DXOmega Function1DDefinition::yFT() const { return yFT_; }
// Return normalisation function
Function1DOmega Function1DDefinition::normalisation() const { return normaliser_; }
// One-Dimensional Function Definitions
static std::map<Functions1D::Form, Function1DDefinition> functions1D_ = {
// No Function - returns zero
{Functions1D::Form::None,
{{},
{FunctionProperties::FourierTransform, FunctionProperties::Normalisation, FunctionProperties::FirstDerivative},
[](std::vector<double> params) { return params; },
[](double x, double omega, const std::vector<double> ¶ms) { return 0.0; },
[](double x, double omega, const std::vector<double> ¶ms) { return 0.0; },
[](double x, double omega, const std::vector<double> ¶ms) { return 0.0; },
[](double omega, const std::vector<double> ¶ms) { return 0.0; }}},
/*
* Gaussian
*
* Parameters:
* INPUT 0 = fwhm
* CALC 1 = c = fwhm / (2 * sqrt(2 ln 2))
* CALC 2 = 1.0 / c
*/
{Functions1D::Form::Gaussian,
{{"fwhm"},
{FunctionProperties::FourierTransform, FunctionProperties::Normalisation},
[](std::vector<double> params) {
params.push_back(params[0] / (2.0 * sqrt(2.0 * log(2.0))));
params.push_back(1.0 / params[1]);
return params;
},
/*
* ( x * x )
* f(x) = exp ( - --------- )
* ( 2 * c * c )
*/
[](double x, double omega, const std::vector<double> ¶ms) { return exp(-(0.5 * x * x * params[2] * params[2])); },
// First derivative (not defined)
{},
/*
* ( x * x * c * c )
* FT(x) = exp ( - ------------- )
* ( 2 )
*/
[](double x, double omega, const std::vector<double> ¶ms) { return exp(-(0.5 * x * x * params[1] * params[1])); },
/*
* 1
* Norm = ------------
* c sqrt(2 pi)
*/
[](double omega, const std::vector<double> ¶ms) { return params[2] / sqrt(2.0 * M_PI); }}},
/*
* Gaussian with prefactor
*
* Parameters:
* INPUT 0 = A
* INPUT 1 = fwhm
* CALC 2 = c = fwhm / (2 * sqrt(2 ln 2))
* CALC 3 = 1.0 / c
*/
{Functions1D::Form::ScaledGaussian,
{{"A", "fwhm"},
{FunctionProperties::FourierTransform, FunctionProperties::Normalisation},
[](std::vector<double> params) {
params.push_back(params[1] / (2.0 * sqrt(2.0 * log(2.0))));
params.push_back(1.0 / params[2]);
return params;
},
/*
* ( x * x )
* f(x) = A exp ( - --------- )
* ( 2 * c * c )
*/
[](double x, double omega, const std::vector<double> ¶ms) {
return params[0] * exp(-(0.5 * x * x * params[3] * params[3]));
},
// First derivative (not defined)
{},
/*
* ( x * x * c * c )
* FT(x) = A exp ( - ------------- )
* ( 2 )
*/
[](double x, double omega, const std::vector<double> ¶ms) {
return params[0] * exp(-(0.5 * x * x * params[2] * params[2]));
},
/*
* 1
* Norm = --------------
* A c sqrt(2 pi)
*/
[](double omega, const std::vector<double> ¶ms) { return params[3] / (params[0] * sqrt(2.0 * M_PI)); }}},
/*
* Gaussian with omega-dependent fwhm
*
* Parameters:
* INPUT 0 = fwhm
* CALC 1 = c = fwhm / (2 * sqrt(2 ln 2))
* CALC 2 = 1.0 / c
*/
{Functions1D::Form::OmegaDependentGaussian,
{{"fwhm(x)"},
{FunctionProperties::FourierTransform, FunctionProperties::Normalisation},
[](std::vector<double> params) {
params.push_back(params[0] / (2.0 * sqrt(2.0 * log(2.0))));
params.push_back(1.0 / params[1]);
return params;
},
/*
* ( x * x )
* f(x) = exp ( - ---------------- )
* ( 2 * (c*omega)**2 )
*/
[](double x, double omega, const std::vector<double> ¶ms) {
return exp(-(x * x) / (2.0 * (params[1] * omega) * (params[1] * omega)));
},
// First derivative (not defined)
{},
/*
* ( x*x * (c*omega)**2 )
* FT(x) = exp ( - ------------------ )
* ( 2 )
*/
[](double x, double omega, const std::vector<double> ¶ms) {
return exp(-(0.5 * x * x * (params[1] * omega) * (params[1] * omega)));
},
/*
* 1
* Norm = ------------------
* c omega sqrt(2 pi)
*/
[](double omega, const std::vector<double> ¶ms) { return 1.0 / (params[1] * omega * sqrt(2.0 * M_PI)); }}},
/*
* Gaussian with omega-independent and omega-dependent fwhm
*
* Parameters:
* INPUT 0 = fwhm1
* INPUT 1 = fwhm2 (omega-dependent)
* CALC 2 = c1 = fwhm1 / (2 * sqrt(2 ln 2))
* CALC 3 = c2 = fwhm2 / (2 * sqrt(2 ln 2))
* CALC 4 = 1.0 / c1
* CALC 5 = 1.0 / c2
*/
{Functions1D::Form::GaussianC2,
{{"fwhm", "fwhm(x)"},
{FunctionProperties::FourierTransform},
[](std::vector<double> params) {
params.push_back(params[0] / (2.0 * sqrt(2.0 * log(2.0))));
params.push_back(params[1] / (2.0 * sqrt(2.0 * log(2.0))));
params.push_back(1.0 / params[2]);
params.push_back(1.0 / params[3]);
return params;
},
/*
* ( x * x )
* f(x) = exp ( - ---------------------- )
* ( 2 * (c1 + c2*omega)**2 )
*/
[](double x, double omega, const std::vector<double> ¶ms) {
return exp(-(x * x) / (2.0 * (params[2] + params[3] * omega) * (params[2] + params[3] * omega)));
},
// First derivative (not defined)
{},
/*
* ( x * x * (c1 + c2*omega)**2 )
* FT(x) = exp ( - -------------------------- )
* ( 2 )
*/
[](double x, double omega, const std::vector<double> ¶ms) {
return exp(-(0.5 * x * x * (params[2] + params[3] * omega) * (params[2] + params[3] * omega)));
},
/*
* 1
* Norm = --------------------------
* (c1 + c2 omega) sqrt(2 pi)
*/
[](double omega, const std::vector<double> ¶ms) {
return 1.0 / ((params[2] + params[3] * omega) * sqrt(2.0 * M_PI));
}}},
/*
* Lennard-Jones 12-6 Potential
*
* Parameters:
* INPUT 0 = epsilon
* INPUT 1 = sigma
*/
{Functions1D::Form::LennardJones126,
{{"epsilon", "sigma"},
{FunctionProperties::FirstDerivative},
[](std::vector<double> params) { return params; },
/*
* [ ( sigma )**12 ( sigma )**6 ]
* F(x) = 4 * epsilon * [ ( ----- ) - ( ----- ) ]
* [ ( x ) ( x ) ]
*/
[](double x, double omega, const std::vector<double> ¶ms) {
auto sigmar = params[1] / x;
auto sigmar6 = pow(sigmar, 6.0);
auto sigmar12 = sigmar6 * sigmar6;
return 4.0 * params[0] * (sigmar12 - sigmar6);
},
/*
* [ ( sigma**12 ) ( sigma**6 ) ]
* dYdX(x) = -48 * epsilon * [ ( --------- ) - 0.5 * ( -------- ) ]
* [ ( x**13 ) ( x**7 ) ]
*/
[](double x, double omega, const std::vector<double> ¶ms) {
auto sigmar = params[1] / x;
auto sigmar6 = pow(sigmar, 6.0);
return 48.0 * params[0] * sigmar6 * (-sigmar6 + 0.5) / x;
},
{},
{}}},
/*
* Buckingham Potential
*
* Parameters:
* INPUT 0 = A
* INPUT 1 = B
* INPUT 2 = C
*/
{Functions1D::Form::Buckingham,
{{"A", "B", "C"},
{FunctionProperties::FirstDerivative},
[](std::vector<double> params) { return params; },
/* C
* F(x)=A exp(-B * x) - -----
* x**6
*/
[](double x, double A, const std::vector<double> ¶ms) {
auto B = exp(-params[1] * x);
auto C = params[2] * pow(x, 6.0);
return params[0] * B + C;
},
// dy/dx = -B * A exp(-B * x) + 6 * C * x**-7
[](double x, double A, const std::vector<double> ¶ms) {
auto expo = exp(-params[1] * x);
auto C = 6 * params[2] * pow(x, -7.0);
return -params[1] * params[0] * expo + C;
},
{},
{}}}};
// Return enum option info for forms
EnumOptions<Functions1D::Form> Functions1D::forms()
{
return EnumOptions<Functions1D::Form>("Function1D",
{
{Functions1D::Form::None, "None"},
{Functions1D::Form::Gaussian, "Gaussian", 1},
{Functions1D::Form::ScaledGaussian, "ScaledGaussian", 2},
{Functions1D::Form::OmegaDependentGaussian, "OmegaDependentGaussian", 1},
{Functions1D::Form::GaussianC2, "GaussianC2", 2},
{Functions1D::Form::LennardJones126, "LennardJones126", 2},
{Functions1D::Form::Buckingham, "Buckingham", 3},
});
}
// Return parameters for specified form
const std::vector<std::string> &Functions1D::parameters(Form form) { return functions1D_.at(form).parameterNames(); }
// Return nth parameter for the given form
std::string Functions1D::parameter(Form form, int n) { return functions1D_.at(form).parameterNames()[n]; }
// Return index of parameter in the given form
std::optional<int> Functions1D::parameterIndex(Form form, std::string_view name)
{
auto it = std::find(functions1D_.at(form).parameterNames().begin(), functions1D_.at(form).parameterNames().end(), name);
if (it == functions1D_.at(form).parameterNames().end())
return {};
return it - functions1D_.at(form).parameterNames().begin();
}
// Return base function requested
Function1DDefinition Functions1D::functionDefinition1D(Functions1D::Form form) { return functions1D_.at(form); }
// Check function properties against those supplied, returning truth if the function meets all requirements
bool Functions1D::validFunction1DProperties(Functions1D::Form form,
const Flags<FunctionProperties::FunctionProperty> &properties)
{
return (functions1D_.at(form).properties() & properties) == properties;
}
// Return all available functions with properties matching those provided
std::vector<Functions1D::Form> Functions1D::matchingFunction1D(const Flags<FunctionProperties::FunctionProperty> &properties)
{
std::vector<Functions1D::Form> matches;
for (auto n = 0; n < forms().nOptions(); ++n)
if (validFunction1DProperties(forms().enumerationByIndex(n), properties))
matches.push_back(forms().enumerationByIndex(n));
return matches;
}
/*
* One-Dimensional Function Wrapper
*/
Function1DWrapper::Function1DWrapper(Functions1D::Form form, const std::vector<double> ¶ms)
: form_(form), function_(functions1D_.at(form)), parameters_(params)
{
calculateInternalParameters();
}
// Initialise internal function parameters from current base parameters
void Function1DWrapper::calculateInternalParameters() { internalParameters_ = function_.setup()(parameters_); }
// Set function type and parameters
bool Function1DWrapper::setFormAndParameters(Functions1D::Form form, const std::vector<double> ¶ms)
{
form_ = form;
function_ = functions1D_.at(form_);
if (params.size() != function_.nParameters())
return Messenger::error("1D function '{}' requires {} parameters, but {} were given.\n",
Functions1D::forms().keyword(form_), function_.nParameters(), params.size());
parameters_ = params;
calculateInternalParameters();
return true;
}
// Set current functional form
void Function1DWrapper::setForm(Functions1D::Form form)
{
form_ = form;
function_ = functions1D_.at(form_);
calculateInternalParameters();
}
// Return functional form
Functions1D::Form Function1DWrapper::form() const { return form_; }
// Set current function parameters
bool Function1DWrapper::setParameters(const std::vector<double> ¶ms)
{
if (params.size() != function_.nParameters())
return Messenger::error("1D function '{}' requires {} parameters, but {} were given.\n",
Functions1D::forms().keyword(form_), function_.nParameters(), params.size());
parameters_ = params;
calculateInternalParameters();
return true;
}
// Return number of parameters for current function
int Function1DWrapper::nParameters() const { return function_.nParameters(); }
// Return current parameters
const std::vector<double> &Function1DWrapper::parameters() const { return parameters_; }
// Return name of nth parameter
std::string Function1DWrapper::parameterName(int i) const { return function_.parameterNames()[i]; }
// Return parameter summary ("name = value, ...")
std::string Function1DWrapper::parameterSummary() const
{
std::string summary;
for (const auto &[name, p] : zip(function_.parameterNames(), parameters_))
summary += fmt::format("{}{} = {}", summary.empty() ? "" : ", ", name, p);
return summary;
}
// Return y value at specified x, omega
double Function1DWrapper::y(double x, double omega) const { return function_.y()(x, omega, internalParameters_); }
// Return first derivative of y at specified x, omega
double Function1DWrapper::dYdX(double x, double omega) const
{
return function_.dYdX() ? function_.dYdX()(x, omega, internalParameters_) : 0.0;
}
// Return Fourier transformed y value at specified x, omega
double Function1DWrapper::yFT(double x, double omega) const
{
return function_.yFT() ? function_.yFT()(x, omega, internalParameters_) : 0.0;
}
// Return normalisation factor at specified omega
double Function1DWrapper::normalisation(double omega) const
{
return function_.normalisation() ? function_.normalisation()(omega, internalParameters_) : 1.0;
}