-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.cpp
571 lines (534 loc) · 19.1 KB
/
Function.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#if !defined(_WIN32)
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) || defined(__NetBSD__)
#include <cstdlib>
#else
#include <alloca.h>
#endif
#endif
#include "Function.h"
#include "ATC_Error.h"
#include "LammpsInterface.h"
#include <sstream>
using std::stringstream;
using std::string;
using std::set;
using std::fstream;
namespace ATC {
//====================================================================
// UXT_Function
//===================================================================
UXT_Function::UXT_Function(int /* narg */, double* /* args */) { }
//====================================================================
// UXT_Function_Mgr
//====================================================================
UXT_Function_Mgr * UXT_Function_Mgr::myInstance_ = NULL;
// -----------------------------------------------------------------
// instance()
// -----------------------------------------------------------------
UXT_Function_Mgr * UXT_Function_Mgr::instance()
{
if (myInstance_ == NULL) {
myInstance_ = new UXT_Function_Mgr();
}
return myInstance_;
}
// Destructor
UXT_Function_Mgr::~UXT_Function_Mgr()
{
// Delete all functions created using "new"
set<UXT_Function * >::iterator it;
for (it = pointerSet_.begin(); it != pointerSet_.end(); it++)
if (*it) delete *it;
}
// add user function into the if statement and assign returnFunction to it
UXT_Function* UXT_Function_Mgr::function(string & type, int nargs, double * args)
{
UXT_Function * returnFunction;
if (type=="linear") {
returnFunction = new ScalarLinearFunction(nargs,args);
}
else
throw ATC_Error("Bad user function name");
pointerSet_.insert(returnFunction);
return returnFunction;
}
// add user function into the if statement and assign returnFunction to it
UXT_Function* UXT_Function_Mgr::function(char ** args, int nargs)
{
string type = args[0];
int narg = nargs -1;
#ifdef _WIN32
double *dargs = (double *) _alloca(sizeof(double) * narg);
#else
double *dargs = (double *) alloca(sizeof(double) * narg);
#endif
for (int i = 0; i < narg; ++i) dargs[i] = atof(args[i+1]);
return function(type, narg, dargs);
}
// add constant function
UXT_Function* UXT_Function_Mgr::linear_function(double c0, double c1)
{
double args[2] = {c0,c1};
UXT_Function * returnFunction = new ScalarLinearFunction(2,args);
pointerSet_.insert(returnFunction);
return (returnFunction);
}
UXT_Function* UXT_Function_Mgr::copy_UXT_function(UXT_Function* other)
{
string tag = other->tag();
UXT_Function * returnFunction = NULL;
if (tag=="linear") {
ScalarLinearFunction * other_cast = (ScalarLinearFunction*) other;
returnFunction = new ScalarLinearFunction(*other_cast);
}
pointerSet_.insert(returnFunction);
return returnFunction;
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// ScalarLinearFunction
//--------------------------------------------------------------------
//--------------------------------------------------------------------
ScalarLinearFunction::ScalarLinearFunction(int narg, double* args)
: UXT_Function(narg,args)
{
tag_ = "linear";
c0_ = args[0];
c1_ = args[1];
stringstream ss;
ss << "created function : " << c0_ << " + " << c1_ << "*u";
ATC::LammpsInterface::instance()->print_msg_once(ss.str());
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// XT_Function
//--------------------------------------------------------------------
//--------------------------------------------------------------------
XT_Function::XT_Function(int narg, double* args)
{
if (narg > 5 ) {
x0[0] = args[0];
x0[1] = args[1];
x0[2] = args[2];
mask[0] = args[3];
mask[1] = args[4];
mask[2] = args[5];
}
else {
x0[0] = 0.0;
x0[1] = 0.0;
x0[2] = 0.0;
mask[0] = 0.0;
mask[1] = 0.0;
mask[2] = 0.0;
}
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// XT_Function_Mgr
//--------------------------------------------------------------------
//--------------------------------------------------------------------
XT_Function_Mgr * XT_Function_Mgr::myInstance_ = NULL;
// -----------------------------------------------------------------
// instance()
// -----------------------------------------------------------------
XT_Function_Mgr * XT_Function_Mgr::instance()
{
if (myInstance_ == NULL) {
myInstance_ = new XT_Function_Mgr();
}
return myInstance_;
}
// Destructor
XT_Function_Mgr::~XT_Function_Mgr()
{
// Delete all functions created using "new"
set<XT_Function * >::iterator it;
for (it = pointerSet_.begin(); it != pointerSet_.end(); it++)
if (*it) delete *it;
}
// add user function into the if statement and assign returnFunction to it
XT_Function* XT_Function_Mgr::function(string & type, int nargs, double * args)
{
XT_Function * returnFunction;
if (type=="constant") {
returnFunction = new ConstantFunction(nargs,args);
}
else if (type=="temporal_ramp") {
returnFunction = new TemporalRamp(nargs,args);
}
else if (type=="linear")
returnFunction = new LinearFunction(nargs,args);
else if (type=="piecewise_linear")
returnFunction = new PiecewiseLinearFunction(nargs,args);
else if (type=="linear_temporal_ramp")
returnFunction = new LinearTemporalRamp(nargs,args);
else if (type=="quadratic")
returnFunction = new QuadraticFunction(nargs,args);
else if (type=="sine")
returnFunction = new SineFunction(nargs,args);
else if (type=="gaussian")
returnFunction = new GaussianFunction(nargs,args);
else if (type=="gaussian_temporal_ramp")
returnFunction = new GaussianTemporalRamp(nargs,args);
else if (type=="radial_power")
returnFunction = new RadialPower(nargs,args);
else
throw ATC_Error("Bad user function name");
pointerSet_.insert(returnFunction);
return returnFunction;
}
// add user function into the if statement and assign returnFunction to it
XT_Function* XT_Function_Mgr::function(char ** args, int nargs)
{
string type = args[0];
int narg = nargs -1;
#ifdef _WIN32
double *dargs = (double *) _alloca(sizeof(double) * narg);
#else
double *dargs = (double *) alloca(sizeof(double) * narg);
#endif
for (int i = 0; i < narg; ++i) dargs[i] = atof(args[i+1]);
return function(type, narg, dargs);
}
// add constant function
XT_Function* XT_Function_Mgr::constant_function(double c)
{
XT_Function * returnFunction = new ConstantFunction(c);
pointerSet_.insert(returnFunction);
return (returnFunction);
}
XT_Function* XT_Function_Mgr::copy_XT_function(XT_Function* other)
{
string tag = other->tag();
XT_Function * returnFunction = NULL;
if (tag=="linear") {
LinearFunction * other_cast = (LinearFunction*) other;
returnFunction = new LinearFunction(*other_cast);
}
else if (tag=="piecewise_linear") {
PiecewiseLinearFunction * other_cast = (PiecewiseLinearFunction*) other;
returnFunction = new PiecewiseLinearFunction(*other_cast);
}
else if (tag=="quadratic") {
QuadraticFunction * other_cast = (QuadraticFunction*) other;
returnFunction = new QuadraticFunction(*other_cast);
}
else if (tag=="sine") {
SineFunction * other_cast = (SineFunction*) other;
returnFunction = new SineFunction(*other_cast);
}
else if (tag=="gaussian") {
GaussianFunction * other_cast = (GaussianFunction*) other;
returnFunction = new GaussianFunction(*other_cast);
}
else if (tag=="gaussian_temporal_ramp") {
GaussianTemporalRamp * other_cast = (GaussianTemporalRamp*) other;
returnFunction = new GaussianTemporalRamp(*other_cast);
}
else if (tag=="temporal_ramp") {
TemporalRamp * other_cast = (TemporalRamp*) other;
returnFunction = new TemporalRamp(*other_cast);
}
else if (tag=="radial_power") {
RadialPower * other_cast = (RadialPower*) other;
returnFunction = new RadialPower(*other_cast);
}
pointerSet_.insert(returnFunction);
return returnFunction;
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// ConstantFunction
//--------------------------------------------------------------------
//--------------------------------------------------------------------
ConstantFunction::ConstantFunction(int narg, double* args)
: XT_Function(narg,args),
C0(args[0])
{
tag_ = "constant";
}
//--------------------------------------------------------------------
ConstantFunction::ConstantFunction(double arg)
: XT_Function(1,&arg),
C0(arg)
{
tag_ = "constant";
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// LinearFunction
//--------------------------------------------------------------------
//--------------------------------------------------------------------
LinearFunction::LinearFunction(int narg, double* args)
: XT_Function(narg,args)
{
C0 = args[6];
tag_ = "linear";
stringstream ss;
ss << "created function : " << C0 << " + " << mask[0] << "(x-"<< x0[0] << ")+"<< mask[1] << "(y-"<<x0[1]<<")+"<<mask[2]<<"(z-"<<x0[2] << ")";
ATC::LammpsInterface::instance()->print_msg_once(ss.str());
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// PiecewiseLinearFunction
//--------------------------------------------------------------------
//--------------------------------------------------------------------
PiecewiseLinearFunction::PiecewiseLinearFunction(int narg, double* args)
: XT_Function(narg,args)
{
int i=0, idx = 6, n = (narg-idx)/2;
xi.reset(n);
fi.reset(n);
while (idx < narg) {
xi(i) = args[idx++];
fi(i++) = args[idx++];
}
tag_ = "piecewise_linear";
stringstream ss;
ss << "created piecewise function : " << n << " \n";
for (i = 0; i < n; i++) {
ss << xi(i) << " " << fi(i) << "\n";
}
ATC::LammpsInterface::instance()->print_msg_once(ss.str());
}
double PiecewiseLinearFunction::f(double * x, double /* t */)
{
double s = mask[0]*(x[0]-x0[0])+mask[1]*(x[1]-x0[1])+mask[2]*(x[2]-x0[2]);
int index = xi.index(s);
if (index < 0) return fi(0);
else if (index >= xi.size()-1 ) return fi(xi.size()-1);
else {
double f = fi(index)
+ (fi(index+1)-fi(index))*(s-xi(index))/(xi(index+1)-xi(index));
return f;
}
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// LinearTemporalRamp
//--------------------------------------------------------------------
//--------------------------------------------------------------------
LinearTemporalRamp::LinearTemporalRamp(int narg, double* args)
: XT_Function(narg,args)
{
double mask_final[3];
mask_final[0] = args[6];
mask_final[1] = args[7];
mask_final[2] = args[8];
C0_initial = args[9];
double C0_final = args[10];
double delta_t = args[11];
for (int i = 0; i < 3; i++)
mask_slope[i] = (mask_final[i] - mask[i])/delta_t;
C0_slope = (C0_initial - C0_final)/delta_t;
}
double LinearTemporalRamp::f(double* x, double t) {
double slope[3];
for (int i = 0; i < 3; i++)
slope[i] = mask[i] + mask_slope[i]*t;
double C0 = C0_initial + C0_slope*t;
return slope[0]*(x[0]-x0[0])+slope[1]*(x[1]-x0[1])+slope[2]*(x[2]-x0[2]) + C0;
}
double LinearTemporalRamp::dfdt(double* x, double /* t */) {
return mask_slope[0]*(x[0]-x0[0])+mask_slope[1]*(x[1]-x0[1])+mask_slope[2]*(x[2]-x0[2]) + C0_slope;
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// QuadraticFunction
//--------------------------------------------------------------------
//--------------------------------------------------------------------
QuadraticFunction::QuadraticFunction(int narg, double* args)
: XT_Function(narg,args)
{
C0 = args[6];
C2[0] = args[7];
C2[1] = args[8];
C2[2] = args[9];
C2[3] = args[10];
C2[4] = args[11];
C2[5] = args[12];
tag_ = "quadratic";
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// SineFunction
//--------------------------------------------------------------------
//--------------------------------------------------------------------
SineFunction::SineFunction(int narg, double* args)
: XT_Function(narg,args)
{
C = args[6];
C0 = args[7];
w = args[8];
tag_ = "sine";
stringstream ss;
ss << "Power : " << C << " Penetration : " << w << " SpotRadius : " << C0;
ATC::LammpsInterface::instance()->print_msg_once(ss.str());
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// GaussianFunction
//--------------------------------------------------------------------
//--------------------------------------------------------------------
GaussianFunction::GaussianFunction(int narg, double* args)
: XT_Function(narg,args)
{
tau = args[6];
C = args[7];
C0 = args[8];
tag_ = "gaussian";
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// GaussianTemporalRamp
//--------------------------------------------------------------------
//--------------------------------------------------------------------
GaussianTemporalRamp::GaussianTemporalRamp(int narg, double* args)
: GaussianFunction(narg,args)
{
tau_initial = args[9];
C_initial = args[10];
C0_initial = args[11];
double delta_t = args[12];
tau_slope = (tau - tau_initial)/delta_t;
C_slope = (C - C_initial)/delta_t;
C0_slope = (C0 - C0_initial)/delta_t;
tag_ = "gaussian_temporal_ramp";
}
double GaussianTemporalRamp::f(double* x, double t) {
tau = tau_initial + tau_slope*t;
C = C_initial + C_slope*t;
C0 = C0_initial + C0_slope*t;
return GaussianFunction::f(x,t);
}
double GaussianTemporalRamp::dfdt(double* x, double t) {
tau = tau_initial + tau_slope*t;
C = C_initial + C_slope*t;
C0 = C0_initial + C0_slope*t;
double dfdt = 0.;
dfdt += C_slope*exp(-(mask[0]*(x[0]-x0[0])*(x[0]-x0[0])
+mask[1]*(x[1]-x0[1])*(x[1]-x0[1])
+mask[2]*(x[2]-x0[2])*(x[2]-x0[2]))
/tau/tau);
dfdt += C*exp(2.*tau_slope*(mask[0]*(x[0]-x0[0])*(x[0]-x0[0])
+mask[1]*(x[1]-x0[1])*(x[1]-x0[1])
+mask[2]*(x[2]-x0[2])*(x[2]-x0[2]))
/tau/tau/tau);
dfdt += C0_slope;
return dfdt;
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// TemporalRamp
//--------------------------------------------------------------------
//--------------------------------------------------------------------
TemporalRamp::TemporalRamp(int narg, double* args)
: XT_Function(narg,args)
{
f_initial = args[0];
double f_final = args[1];
double delta_t = args[2];
slope = (f_final - f_initial)/delta_t;
tag_ = "temporal_ramp";
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// RadialPower
//--------------------------------------------------------------------
//--------------------------------------------------------------------
RadialPower::RadialPower(int narg, double* args)
: XT_Function(narg,args)
{
C0 = args[6];
n = args[7];
tag_ = "radial_power";
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// InterpolationFunction
//--------------------------------------------------------------------
//--------------------------------------------------------------------
void InterpolationFunction::initialize(int npts, fstream &fileId, double coef)
{ // read data
npts_ = npts;
xs_.reset(npts);
fs_.reset(npts);
fps_.reset(npts);
double x,f,fp;
int i = 0;
while(fileId.good() && i < npts) {
fileId >> x >> f >> fp;
xs_(i)=x;
fs_(i)=coef*f;
fps_(i)=coef*fp;
i++;
}
// scale tangents
double dx, dx0 = xs_(1)-xs_(0);
for (int i = 0; i < npts_ ; i++) {
if (i == 0) { dx = xs_(1)-xs_(0); }
else if (i+1 == npts_) { dx = xs_(npts_-1)-xs_(npts_-2); }
else { dx= 0.5*(xs_(i+1)-xs_(i-1)); }
if (fabs(dx-dx0) > 1.e-8) throw ATC_Error("InterpolationFunction::initialize non-uniform data spacing not handled currently");
fps_(i) *= dx;
}
// options: calculate / adjust tangents for monotonicity
}
double InterpolationFunction::coordinate(double x,
double & f0, double & fp0, double & f1, double & fp1, double & inv_dx ) const
{
int i0 = xs_.index(x);
int i1 = i0+1;
if (i0 < 0) {
double x0 = xs_(0), x1 = xs_(1);
inv_dx = 1./(x1-x0);
fp0 = fp1 = fps_(0);
f1 = fs_(0);
f0 = fp0*(x-xs_(0))+f1;
return 0;
}
else if (i1 >= npts_) {
double x0 = xs_(npts_-2), x1 = xs_(npts_-1);
inv_dx = 1./(x1-x0);
fp0 = fp1 = fps_(i0);
f0 = fs_(i0);
f1 = fp0*(x-xs_(i0))+f0;
return 1;
}
else {
double x0 = xs_(i0), x1 = xs_(i1);
inv_dx = 1./(x1-x0);
f0 = fs_ (i0); f1 = fs_ (i1);
fp0 = fps_(i0); fp1 = fps_(i1);
double t = (x-x0)*inv_dx;
return t;
}
}
double InterpolationFunction::f(const double x) const
{
double f0,fp0,f1,fp1,inv_dx;
double t = coordinate(x,f0,fp0,f1,fp1,inv_dx);
double t2 = t*t;
double t3 = t*t2;
double h00 = 2*t3 - 3*t2 + 1;
double h10 = t3 - 2*t2 + t;
double h01 =-2*t3 + 3*t2;
double h11 = t3 - t2;
double f = h00 * f0 + h10 * fp0 + h01 * f1 + h11 * fp1;
return f;
}
double InterpolationFunction::dfdt(const double x) const
{
double f0,fp0,f1,fp1,inv_dx;
double t = coordinate(x,f0,fp0,f1,fp1,inv_dx);
double t2 = t*t;
double d00 = 6*t2 - 6*t;
double d10 = 3*t2 - 4*t + 1;
double d01 =-6*t2 + 6*t;
double d11 = 3*t2 - 2*t;
double fp = d00 * f0 + d10 * fp0 + d01 * f1 + d11 * fp1;
fp *= inv_dx;
return fp;
}
}