-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCOST.h
407 lines (358 loc) · 11.8 KB
/
CCOST.h
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
#include <iostream>
#include <fstream>
#include <random>
#include <stdlib.h>
#include <vector>
#include <cmath>
void ccost_integration(std::vector<std::vector<double> > &v_out, double v[], double eta1[], const double par[9],
const double intepar, const double noise_par[4], const int seed, const long long points, const int N){
double v1[4*N];
//store initial conditions (constant history) in the solution array
for(int jj=0; jj<N; jj++){
v1[4*jj] = v[4*jj];
v1[4*jj + 1] = v[4*jj + 1];
v1[4*jj + 2] = v[4*jj + 2];
v1[4*jj + 3] = v[4*jj + 3];
v_out[jj][0] = v[4*jj];// + v[4*jj +2];
}
/*Generate instance of object Normaldev_BM which generates normal deviates (Gaussian distribution) with mean mu and variance sigma
and seed(Uses Box-Muller Transformation)*/
double htauc = noise_par[0], sqrtdht = noise_par[1];
double mu = noise_par[2], sigma = noise_par[3];
std::random_device rd;
std::mt19937 generator(rd());
std::normal_distribution<double> dist1(mu,sigma);
//parameters
double R1 = par[0];
double R2 = par[1];
double L1 = par[2];
double L2 = par[3];
double C1 = par[4];
double C2 = par[5];
double a = par[6];
double b = par[7];
double lam = par[8];
//Integration Parameter
double h=intepar;
double randn[2*N];
//Euler-Maruyama method
int j=0;
//uncoupled case
if(lam == 0){
for (int ii=0; ii<points-1; ii++){
//Generate normal deviates and store in array randn
for(int rr = 0; rr < 2*N; rr++){
randn[rr] = dist1(generator);
dist1.reset(); //forces eta to be uncorrelated.
}//*/
for(int i = 0; i < N; i++){
//Oscillator Array
v[4*i] = v1[4*i] + h*v1[4*i+1];
v[4*i+1] = v1[4*i+1] + h*(1/L1*((a-3*b*pow(v1[4*i]+v1[4*i+2],2))
*(v1[4*i+1]+v1[4*i+3])-(R1*v1[4*i+1]+v1[4*i]/C1))) + eta1[2*i];
v[4*i+2] = v1[4*i+2] + h*v1[4*i+3];
v[4*i+3] = v1[4*i+3] + h*(1/L2*((a-3*b*pow(v1[4*i]+v1[4*i+2],2))
*(v1[4*i+1]+v1[4*i+3])-(R2*v1[4*i+3]+v1[4*i+2]/C2))) + eta1[2*i+1];
//soln update
v_out[i][ii+1] = v[4*i] + v[4*i + 2];
//update colored noise array "eta"
eta1[2*i] += -htauc*eta1[2*i] + sqrtdht*randn[2*i];
eta1[2*i+1] += -htauc*eta1[2*i+1] + sqrtdht*randn[2*i+1];
}
for(int i=0;i<4*N;i++) v1[i]=v[i];
}
}
//coupled case
if(lam!=0){
for (int ii=0; ii<points-1; ii++){
//Generate normal deviates and store in array randn
for(int rr = 0; rr < 2*N; rr++){
randn[rr] = dist1(generator);
dist1.reset(); //forces eta to be uncorrelated.
}//*/
for(int i = 0; i < N; i++){
j = (i+1)%N;
//Oscillator Array
v[4*i] = v1[4*i] + h*v1[4*i+1];
v[4*i+1] = v1[4*i+1] + h*(1/L1*((a-3*b*pow(v1[4*i]+v1[4*i+2]-lam*(v1[4*j]+v1[4*j+2]),2))
*(v1[4*i+1]+v1[4*i+3]-lam*(v1[4*j+1]+v1[4*j+3]))-(R1*v1[4*i+1]+v1[4*i]/C1)))+ eta1[2*i];
v[4*i+2] = v1[4*i+2] + h*v1[4*i+3];
v[4*i+3] = v1[4*i+3] + h*(1/L2*((a-3*b*pow(v1[4*i]+v1[4*i+2]-lam*(v1[4*j]+v1[4*j+2]),2))
*(v1[4*i+1]+v1[4*i+3]-lam*(v1[4*j+1]+v1[4*j+3]))-(R2*v1[4*i+3]+v1[4*i+2]/C2)))+ eta1[2*i+1];
//soln update
v_out[i][ii+1] = v[4*i];// + v[4*i + 2];
//update colored noise array "eta"
eta1[2*i] += -htauc*eta1[2*i] + sqrtdht*randn[2*i];
eta1[2*i+1] += -htauc*eta1[2*i+1] + sqrtdht*randn[2*i+1];
}
for(int i=0;i<4*N;i++){
if(std::isnan(v1[i])) {
std::cout<<"Failed to Converge"<<std::endl;
return;
}
v1[i]=v[i];
}
}
}
}
//distance modulus
double dmod(double a, double b){
double temp;
if(fmod(a,b)<fmod(b,a)) temp = fmod(a,b);
else temp = fmod (b,a);
return temp;
}
double vmax(std::vector<std::vector<double> > &vec){
int n = vec[0].size();
double k = vec[0][0];
for(int i = 0; i<n; i++){
double temp = *std::min_element(std::begin(vec[i]),std::end(vec[i]));
if(temp > k) k = temp;
}
return k;
}
double dmin(const double vec[], const int N){//Non-zero min
double k = vec[0];
for (int i = 0; i<N; i++)
{
if( k > vec[i] && std::abs(vec[i])>1e-18) k = vec[i];
}
return k;
}
double dmax(const double vec[], const int N){
double k = vec[0];
for (int i = 0; i<N; i++)
{
if( k < vec[i]) k = vec[i];
}
return k;
}
double vmin(std::vector<std::vector<double> > &vec){
int n = vec[0].size();
double k = vec[0][0];
for(int i = 0; i<n; i++){
double temp = *std::min_element(std::begin(vec[i]),std::end(vec[i]));
if(temp < k) k = temp;
}
return k;
}
void ccost_trans(double v[], double v_trans[], double eta1[], double par[9],double intepar,double noise_par[4],int seed, long long points, const int N){
double v1[4*N];
//store initial conditions (constant history) in the solution array
for(int jj=0; jj<4*N; jj++){
v1[jj] = v_trans[jj];
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//noise parameters
//EM-Integration constants
double htauc = noise_par[0], sqrtdht = noise_par[1];
//Generate instance of object Normaldev_BM which generates normal deviates (Gaussian distribution) with mean mu and variance sigma
//and seed seed(Uses Box-Muller Transformation)
double mu = noise_par[2], sigma = noise_par[3];
std::random_device rd;
std::mt19937 generator(rd());
std::normal_distribution<double> dist1(0,1);
//parameters
double R1 = par[0];
double R2 = par[1];
double L1 = par[2];
double L2 = par[3];
double C1 = par[4];
double C2 = par[5];
double a = par[6];
double b = par[7];
double lam = par[8];
//Integration Parameter
double h=intepar;
double randn[2*N];
//Euler-Maruyama method
//uncoupled case
if(lam == 0){
for(long long int ii=0;ii<points;ii++){
//Generate normal deviates and store in array randn
for(int rr = 0; rr < 2*N; rr++){
randn[rr] = dist1(generator);
dist1.reset(); //forces eta to be uncorrelated.
}//*/
for(int i = 0; i < N; i++){
//Oscillator Array
v[4*i] = v1[4*i] + h*v1[4*i+1];
v[4*i+1] = v1[4*i+1] + h*(1/L1*((a-3*b*pow(v1[4*i]+v1[4*i+2],2))
*(v1[4*i+1]+v1[4*i+3])-(R1*v1[4*i+1]+v1[4*i]/C1))) + eta1[2*i];
v[4*i+2] = v1[4*i+2] + h*v1[4*i+3];
v[4*i+3] = v1[4*i+3] + h*(1/L2*((a-3*b*pow(v1[4*i]+v1[4*i+2],2))
*(v1[4*i+1]+v1[4*i+3])-(R2*v1[4*i+3]+v1[4*i+2]/C2))) + eta1[2*i+1];
//update colored noise array "eta"
eta1[2*i] += -htauc*eta1[2*i] + sqrtdht*randn[2*i];
eta1[2*i+1] += -htauc*eta1[2*i+1] + sqrtdht*randn[2*i+1];
}
for(int i=0;i<4*N;i++) v1[i]=v[i];
}
}
if(lam != 0){
for(long long int ii=0;ii<points;ii++){
//Generate normal deviates and store in array randn
for(int i = 0; i < N; i++){
int j = (i+1)%N;
randn[2*i] = dist1(generator);
randn[2*i + 1] = dist1(generator);
dist1.reset(); //forces eta to be uncorrelated.*/
//Oscillator Array
v[4*i] = v1[4*i] + h*v1[4*i+1];
v[4*i+1] = v1[4*i+1] + h*(1/L1*((a-3*b*pow(v1[4*i]+v1[4*i+2]-lam*(v1[4*j]+v1[4*j+2]),2))
*(v1[4*i+1]+v1[4*i+3]-lam*(v1[4*j+1]+v1[4*j+3]))-(R1*v1[4*i+1]+v1[4*i]/C1))) + eta1[2*i];
v[4*i+2] = v1[4*i+2] + h*v1[4*i+3];
v[4*i+3] = v1[4*i+3] + h*(1/L2*((a-3*b*pow(v1[4*i]+v1[4*i+2]-lam*(v1[4*j]+v1[4*j+2]),2))
*(v1[4*i+1]+v1[4*i+3]-lam*(v1[4*j+1]+v1[4*j+3]))-(R2*v1[4*i+3]+v1[4*i+2]/C2))) + eta1[2*i+1];
//update colored noise array "eta"
eta1[2*i] += -htauc*eta1[2*i] + sqrtdht*randn[2*i];
eta1[2*i+1] += -htauc*eta1[2*i+1] + sqrtdht*randn[2*i+1];
}
for(int i=0;i<4*N;i++){
v1[i]=v[i];
if(std::isnan(v1[i])) {
std::cout<<"Failed to Converge"<<std::endl;
return;
}
}
}
}
}
double vave(double v[],int N){
double sum=0.0;
double ave=0.0;
for(int ii=0;ii<N;ii++){
if(std::isnan(v[ii])==0 && v[ii] > 1e-17) sum += v[ii];
}
ave = sum/N;
return ave;
}
void vsum(std::vector<std::vector<double> > &v, std::vector<double> &u, const long long points, const int N){
for (int ii=0;ii<N;ii++){
for(int jj=0;jj<points;jj++){
u[jj]+=v[ii][jj];
}
}
}
void averagesignal(std::vector<std::vector<double>> &v,std::vector<double> &u,const long long points,const int N){
vsum(v,u,points,N);
for(int ii = 0; ii<points ;ii++) u[ii] /= N;
}
double vstd(std::vector<double> &v, const double mu){
int N = v.size();
double sigma = 0;
if(N>1){
double delta;
sigma = 0;
for(int ii=0; ii<N;ii++){
delta=abs(v[ii]-mu);
sigma+=delta;
}
sigma = sqrt(sigma/(N-1));
}
return sigma;
}
void phasedrift_kernel(std::vector<double> &t, std::vector<double> &v,
double& phase_error, double tol,const long long points){
std::vector<double> p(points, 0.0);
double slope;
double a0;
double a1;
double a2;
double A, B, C;
double temp1, temp2;
int num1=0;
double u0, u1, u2;
//Locating times such that x(t)=0
for(int ii=1; ii<points-1;ii++){
u1 = 0.0, u2 = 0.0, u0 = 0.0;
u1 = v[ii];
u2 = v[ii+1];
u0 = v[ii-1];
if((u1<0.e0) && (0.e0<u2)){
//Quadratic Interpolation
a0 = u0/((t[ii-1]-t[ii])*(t[ii-1]-t[ii+1]));
a1 = u1/((t[ii]-t[ii-1])*(t[ii]-t[ii+1]));
a2 = u2/((t[ii+1]-t[ii])*(t[ii+1]-t[ii-1]));
A = a0 + a1 + a2;
B = -((t[ii]+t[ii+1])*a0 + (t[ii-1]+t[ii+1])*a1 + (t[ii-1]+t[ii])*a2);
C = a0*t[ii]*t[ii+1] + a1*t[ii-1]*t[ii+1] + a2*t[ii-1]*t[ii];
temp1 = (-B - sqrt(pow(B,2)-4*A*C))/(2*A);
temp2 = (-B + sqrt(pow(B,2)-4*A*C))/(2*A);
if(temp1 < t[ii+1] && temp1 > t[ii-1]){
p[ii] = temp1;
}
else if(temp2 < t[ii+1] && temp2 > t[ii-1]){
p[ii] = temp2;
}
else{
slope = (u2-u0)/(t[ii+1]-t[ii-1]);
p[ii]=t[ii]-u1/slope;//linear interpolation */
} //*/
num1++;
}
}
if(num1==0) {
std::cout<<"Not Detecting Zeros"<<std::endl;
return;
}
//remove the zero vaules in the p vector
std::vector<double> tp(num1,0.0);
int kk=0;
for(int ii=1;ii<points-1;ii++){
if(p[ii]!=0){
tp[kk]=p[ii];
kk++;
}
}
//remove p
p.clear();
p.shrink_to_fit();
std::vector<double> period(num1,0.0);
double temp;
int num;
for(int ii=0;ii<num1-2;ii++){
temp = tp[ii+1]-tp[ii];
if(tp[ii+2]<1e-12) temp = 0;
period[ii]= temp;
if(period[ii]>0){
num++;
}
}
//remove tp
tp.clear();
tp.shrink_to_fit();
double T = 0.0;
std::vector<double> phasetemp(num,0.0);
//Eliminating Zeros from period vector
kk=0;
for(int ii=0;ii<num1;ii++){
if(std::abs(period[ii])>0.0) {
phasetemp[kk]=period[ii];
T += period[ii];
kk++;
}
}
period.clear();
period.shrink_to_fit();
T /= (kk);
phase_error = 0.0;
for(int ii = 0; ii < kk; ii++){
double delta;
delta = std::fabs(phasetemp[ii] - T);
phase_error += delta;
}
phase_error /= (kk-1);
}
double phasedrift(std::vector<double> &t, std::vector<std::vector<double>> &v, const double tol, const long long points,const int N){
double phase = 0.0;
double phase_temp[N];
std::vector<double> u(points, 0.0);
for(int ii = 0; ii< N; ii++){
u = v[ii];
phasedrift_kernel(t,u,phase_temp[ii],tol,points);
phase += phase_temp[ii];
}
phase /= N;
return phase;
}