-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsie.h
More file actions
372 lines (372 loc) · 9.71 KB
/
sie.h
File metadata and controls
372 lines (372 loc) · 9.71 KB
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
_Pragma("once");
#include<boost/numeric/ublas/matrix.hpp>
template<typename State>class StepperSie
{
private:
using state_type=State;
double xold;
double atol,rtol;
bool dense;
double hdid;
double EPS;
int n;
constexpr static int KMAXX=12,IMAXX=KMAXX+1;
int k_targ;
std::vector<int> nseq;
state_type cost;
boost::numeric::ublas::matrix<double> table;
boost::numeric::ublas::matrix<double> dfdy;
state_type dfdx;
double jac_redo;
bool calcjac;
double theta;
boost::numeric::ublas::matrix<double> a;
int kright;
boost::numeric::ublas::matrix<double> coeff;
boost::numeric::ublas::matrix<double> fsave;
state_type dens;
state_type factrl;
bool first_step=true;
bool forward,reject=false,prev_reject=false;
double errold;
public:
StepperSie(const double atol,const double rtol, bool dens=false);
template<typename D>void try_step(D derivs,state_type&y,double&x,double&hnext);
template<typename D>bool dy(double const x,state_type const&y, const double htot, const int k, state_type¥d,int&ipt,state_type const&scale,D derivs);
void polyextr(const int k, boost::numeric::ublas::matrix<double> &table,state_type&last);
void prepare_dense(state_type const y,const double h,state_type const&ysav,state_type const&scale,const int k);
double dense_out(const int i,const double x,const double h);
};
template<typename State>
StepperSie<State>::StepperSie(const double atoll,const double rtoll, bool dens)
: atol(atoll),rtol(rtoll),dense(dens),nseq(IMAXX),cost(IMAXX),calcjac(false),coeff(IMAXX,IMAXX),factrl(IMAXX)
{
constexpr double costfunc=1.0,costjac=5.0,costlu=1.0,costsolve=1.0;
EPS=std::numeric_limits<double>::epsilon();
jac_redo=std::fmin(1.0e-4,rtol);
theta=2.0*jac_redo;
nseq[0]=2;
nseq[1]=3;
for (int i=2;i<IMAXX;i++)nseq[i]=2*nseq[i-2];
cost[0]=costjac+costlu+nseq[0]*(costfunc+costsolve);
for (int k=0;k<KMAXX;k++)cost[k+1]=cost[k]+(nseq[k+1]-1)*(costfunc+costsolve)+costlu;
double logfact=-std::log10(rtol+atol)*0.6+0.5;
k_targ=std::max(1,std::min(KMAXX-1,int(logfact)));
for (int k=0; k<IMAXX; k++) {
for (int l=0; l<k; l++) {
double ratio=double(nseq[k])/nseq[l];
coeff(k,l)=1.0/(ratio-1.0);
}
}
factrl[0]=1.0;
for (int k=0; k<IMAXX-1; k++)
factrl[k+1]=(k+1)*factrl[k];
}
template<typename State> template <class D>
void StepperSie<State>::try_step(D derivs,state_type&y,double&x,double&hnext)
{
if(first_step)
{
n=y.size();
table.resize(KMAXX,n);
dfdy.resize(n,n);
dfdx.resize(n);
a.resize(n,n);
fsave.resize((IMAXX-1)*(IMAXX+1)/2+2,n);
dens.resize((IMAXX+2)*n);
}
constexpr double STEPFAC1=0.6,STEPFAC2=0.93,STEPFAC3=0.1,STEPFAC4=4.0,STEPFAC5=0.5,KFAC1=0.7,KFAC2=0.9;
int i,k;
double fac,h,hnew,err;
bool firstk;
state_type hopt(IMAXX),work(IMAXX);
state_type ysav(n),yseq(n);
state_type ymid(n),scale(n);
work[0]=1.e30;
h=hnext;
forward = h>0 ? true : false;
ysav=y;
if (reject) {
prev_reject=true;
theta=2.0*jac_redo;
}
for (i=0;i<n;i++)
scale[i]=atol+rtol*std::abs(y[i]);
reject=false;
firstk=true;
hnew=std::abs(h);
compute_jac:
if (theta > jac_redo && !calcjac) {
derivs.second(y,dfdy,x,dfdx);
calcjac=true;
}
while (firstk || reject) {
h = forward ? hnew : -hnew;
firstk=false;
reject=false;
if (std::abs(h) <= std::abs(x)*EPS)
throw("step size underflow in StepperSie");
int ipt=-1;
for (k=0; k<=k_targ+1;k++)
{
bool success=dy(x,ysav,h,k,yseq,ipt,scale,derivs);
if (!success) {
reject=true;
hnew=std::abs(h)*STEPFAC5;
break;
}
if (k == 0)
y=yseq;
else
for (i=0;i<n;i++)
table(k-1,i)=yseq[i];
if (k != 0) {
polyextr(k,table,y);
err=0.0;
for (i=0;i<n;i++) {
scale[i]=atol+rtol*std::abs(ysav[i]);
err+=std::pow((y[i]-table(0,i))/scale[i],2);
}
err=std::sqrt(err/n);
if (err > 1.0/EPS || (k > 1 && err >= errold)) {
reject=true;
hnew=std::abs(h)*STEPFAC5;
break;
}
errold=std::max(4.0*err,1.0);
double expo=1.0/(k+1);
double facmin=std::pow(STEPFAC3,expo);
if (err == 0.0)
fac=1.0/facmin;
else {
fac=STEPFAC2/pow(err/STEPFAC1,expo);
fac=std::fmax(facmin/STEPFAC4,std::fmin(1.0/facmin,fac));
}
hopt[k]=std::abs(h*fac);
work[k]=cost[k]/hopt[k];
if (first_step && err <= 1.0)
break;
if (k == k_targ-1 && !prev_reject && !first_step) {
if (err <= 1.0)
break;
else if (err>nseq[k_targ]*nseq[k_targ+1]*4.0) {
reject=true;
k_targ=k;
if (k_targ>1 && work[k-1]<KFAC1*work[k])
k_targ--;
hnew=hopt[k_targ];
break;
}
}
if (k == k_targ) {
if (err <= 1.0)
break;
else if (err>nseq[k+1]*2.0) {
reject=true;
if (k_targ>1 && work[k-1]<KFAC1*work[k])
k_targ--;
hnew=hopt[k_targ];
break;
}
}
if (k == k_targ+1) {
if (err > 1.0) {
reject=true;
if (k_targ>1 && work[k_targ-1]<KFAC1*work[k_targ])
k_targ--;
hnew=hopt[k_targ];
}
break;
}
}
}
if (reject)
{
prev_reject=true;
if (!calcjac)
{
theta=2.0*jac_redo;
goto compute_jac;
}
}
}
calcjac=false;
if (dense)
prepare_dense(y,h,ysav,scale,k);
xold=x;
x+=h;
hdid=h;
first_step=false;
int kopt;
if (k == 1)
kopt=2;
else if (k <= k_targ) {
kopt=k;
if (work[k-1] < KFAC1*work[k])
kopt=k-1;
else if (work[k] < KFAC2*work[k-1])
kopt=std::min(k+1,KMAXX-1);
} else {
kopt=k-1;
if (k > 2 && work[k-2] < KFAC1*work[k-1])
kopt=k-2;
if (work[k] < KFAC2*work[kopt])
kopt=std::min(k,KMAXX-1);
}
if (prev_reject) {
k_targ=std::min(kopt,k);
hnew=std::fmin(std::abs(h),hopt[k_targ]);
prev_reject=false;
}
else {
if (kopt <= k)
hnew=hopt[kopt];
else {
if (k<k_targ && work[k]<KFAC2*work[k-1])
hnew=hopt[k]*cost[kopt+1]/cost[k];
else
hnew=hopt[k]*cost[kopt]/cost[k];
}
k_targ=kopt;
}
if (forward)
hnext=hnew;
else
hnext=-hnew;
}
#include<mkl_lapacke.h>
class Solve final
{
private:
boost::numeric::ublas::matrix<double>lu;
std::vector<int>pivot;
public:
Solve(boost::numeric::ublas::matrix<double>const&Matrix):lu(Matrix),pivot(Matrix.size1())
{
assert(!::LAPACKE_dgetrf(LAPACK_ROW_MAJOR,lu.size1(),lu.size2(),lu.data().begin(),lu.size1(),pivot.data()));
};
Solve(Solve&&)=delete;
template<typename state_type>void operator()(boost::numeric::ublas::matrix<double>const&Matrix,state_type&right)const&
{
auto const Right(right);
assert(!::LAPACKE_dgetrs(LAPACK_ROW_MAJOR,'N',lu.size1(),1,lu.data().cbegin(),lu.size1(),pivot.data(),right.data(),1));
double ferr,berr;
assert(!::LAPACKE_dgerfs(LAPACK_ROW_MAJOR,'N',lu.size1(),1,Matrix.data().cbegin(),lu.size1(),lu.data().cbegin(),lu.size1(),pivot.data(),Right.data(),1,right.data(),1,std::addressof(ferr),std::addressof(berr)));
};
};
template<typename State> template <class D>
bool StepperSie<State>::dy(double const x,state_type const&y,const double htot,const int k,state_type¥d,int &ipt,state_type const&scale,D derivs)
{
state_type del(n),ytemp(n),dytemp(n);
int nstep=nseq[k];
double h=htot/nstep;
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) a(i,j) = -dfdy(i,j);
a(i,i) += 1.0/h;
}
::Solve solve{a};
double xnew=x+h;
derivs.first(y,del,xnew);
ytemp=y;
solve(a,del);
if (dense && nstep==k+1) {
ipt++;
for (int i=0;i<n;i++)
fsave(ipt,i)=del[i];
}
for (int nn=1;nn<nstep;nn++) {
for (int i=0;i<n;i++)
ytemp[i] += del[i];
xnew += h;
derivs.first(ytemp,yend,xnew);
if (nn ==1 && k<=1) {
double del1=0.0;
for (int i=0;i<n;i++)
del1 += std::pow(del[i]/scale[i],2);
del1=std::sqrt(del1);
derivs.first(ytemp,dytemp,x+h);
for (int i=0;i<n;i++)
del[i]=dytemp[i]-del[i]/h;
solve(a,del);
double del2=0.0;
for (int i=0;i<n;i++)
del2 += std::pow(del[i]/scale[i],2);
del2=std::sqrt(del2);
theta=del2/std::fmax(1.0,del1);
if (theta > 1.0)
return false;
}
del=yend;
solve(a,del);
if (dense && nn >= nstep-k-1) {
ipt++;
for (int i=0;i<n;i++)
fsave(ipt,i)=del[i];
}
}
for (int i=0;i<n;i++)
yend[i]=ytemp[i]+del[i];
return true;
}
template<typename State>
void StepperSie<State>::polyextr(const int k,boost::numeric::ublas::matrix<double>&table,state_type&last) {
int l=last.size();
for (int j=k-1; j>0; j--)
for (int i=0; i<l; i++)
table(j-1,i)=table(j,i)+coeff(k,j)*(table(j,i)-table(j-1,i));
for (int i=0; i<l; i++)
last[i]=table(0,i)+coeff(k,0)*(table(0,i)-last[i]);
}
template<typename State>
void StepperSie<State>::prepare_dense(state_type const y,const double h,state_type const&ysav,state_type const&scale,const int k) {
kright=k;
for (int i=0; i<n; i++) {
dens[i]=ysav[i];
dens[n+i]=y[i];
}
for (int klr=0; klr < kright; klr++) {
if (klr >= 1) {
for (int kk=klr; kk<=k; kk++) {
int lbeg=((kk+3)*kk)/2;
int lend=lbeg-kk+1;
for (int l=lbeg; l>=lend; l--)
for (int i=0; i<n; i++)
fsave(l,i)=fsave(l,i)-fsave(l-1,i);
}
}
for (int kk=klr; kk<=k; kk++) {
double facnj=nseq[kk];
facnj=std::pow(facnj,klr+1)/factrl[klr+1];
int ipt=((kk+3)*kk)/2;
int krn=(kk+2)*n;
for (int i=0; i<n; i++) {
dens[krn+i]=fsave(ipt,i)*facnj;
}
}
for (int j=klr+1; j<=k; j++) {
double dblenj=nseq[j];
for (int l=j; l>=klr+1; l--) {
double factor=dblenj/nseq[l-1]-1.0;
for (int i=0; i<n; i++) {
int krn=(l+2)*n+i;
dens[krn-n]=dens[krn]+(dens[krn]-dens[krn-n])/factor;
}
}
}
}
for (int in=0; in<n; in++) {
for (int j=1; j<=kright+1; j++) {
int ii=n*j+in;
dens[ii]=dens[ii]-dens[ii-n];
}
}
}
template<typename State>
double StepperSie<State>::dense_out(const int i,const double x,const double h)
{
double theta=(x-xold)/h;
int k=kright;
double yinterp=dens[(k+1)*n+i];
for (int j=1; j<=k; j++) yinterp=dens[(k+1-j)*n+i]+yinterp*(theta-1.0);
return dens[i]+yinterp*theta;
}