37 : model(
model_), u(model.size()), f(model.size())
39 model.initialize(t,u);
118 : model(
model_), u(model.size()), w(model.size()), k1(model.size()), k2(model.size())
123 model.initialize(t,u);
142 model.f(t+c2*dt,w,k2);
212 : model(
model_), u(model.size()), w(model.size()), k1(model.size()), k2(model.size())
218 model.initialize(t,u);
237 model.f(t+c2*dt,w,k2);
308 : model(
model_), u(model.size()), w(model.size()), k1(model.size()),
309 k2(model.size()), k3(model.size())
318 model.initialize(t,u);
337 model.f(t+c2*dt,w,k2);
342 model.f(t+c3*dt,w,k3);
412 : model(
model_), u(model.size()), w(model.size()), k1(model.size()),
413 k2(model.size()), k3(model.size())
423 model.initialize(t,u);
442 model.f(t+c2*dt,w,k2);
448 model.f(t+c3*dt,w,k3);
519 : model(
model_), u(model.size()), w(model.size()), k1(model.size()),
520 k2(model.size()), k3(model.size()), k4(model.size())
532 model.initialize(t,u);
551 model.f(t+c2*dt,w,k2);
556 model.f(t+c3*dt,w,k3);
561 model.f(t+c4*dt,w,k4);
605 time_type c2,c3,c4,a21,a32,a43,b1,b2,b3,b4;
629 : model(
model_), u(model.size()), w(model.size()), ww(model.size()), k1(model.size()),
630 k2(model.size()), k3(model.size()), k4(model.size()), k5(model.size()), k6(model.size()),
631 steps(0), rejected(0)
678 model.initialize(t,u);
705 model.f(t+c2*dt,w,k2);
711 model.f(t+c3*dt,w,k3);
718 model.f(t+c4*dt,w,k4);
726 model.f(t+c5*dt,w,k5);
735 model.f(t+c6*dt,w,k6);
771 if (dt>dt_min)
step();
802 std::cout <<
"RE: steps=" << steps <<
" rejected=" << rejected << std::endl;
810 time_type a21,a31,a32,a41,a42,a43,a51,a52,a53,a54,a61,a62,a63,a64,a65;
824 template<
class M,
class S>
840 wlow(model.size()), whigh(model.size()), ww(model.size()),
841 steps(0), rejected(0)
843 model.initialize(t,u);
875 solver.set_state(t,u);
878 wlow = solver.get_state();
881 solver.set_state(t,u);
885 whigh = solver.get_state();
890 time_type error(norm(ww)/(pow(
H,1.0+solver.get_order())*(1.0-1.0/two_power_m)));
901 u /= two_power_m-1.0;
908 if (dt>dt_min)
step();
933 return solver.get_order()+1;
939 std::cout <<
"RE: steps=" << steps <<
" rejected=" << rejected << std::endl;
962 template<
class M,
class S>
967 class NonlinearProblem
978 typename M::time_type
tnew_,
typename M::time_type
dt_)
983 std::size_t size ()
const
1000 model.f_x(tnew,x,
result);
1005 void set_tnew_dt (
typename M::time_type
tnew_,
typename M::time_type
dt_)
1014 typename M::time_type tnew;
1015 typename M::time_type dt;
1030 : verbosity(0), model(
model_), newton(
newton_), u(model.size()), unew(model.size())
1032 model.initialize(t,u);
1052 std::cout <<
"IE: step" <<
" t=" << t <<
" dt=" << dt << std::endl;
1053 NonlinearProblem
nlp(model,u,t+dt,dt);
1059 newton.solve(
nlp,unew);
1060 if (newton.has_converged())
1066 dt = std::min(2.0*dt,dtmax);
1068 std::cout <<
"IE: increasing time step to " << dt << std::endl;
1076 HDNUM_ERROR(
"time step too small in implicit Euler");
1082 nlp.set_tnew_dt(t+dt,dt);
1083 if (verbosity>0) std::cout <<
"IE: reducing time step to " << dt << std::endl;
1152 template<
class M,
class S>
1175 if(
method.find(
"Implicit Euler") != std::string::npos){
1183 else if(
method.find(
"Alexander") != std::string::npos){
1186 butcher[0][0] = alpha;
1187 butcher[0][1] = alpha;
1190 butcher[1][1] = 1. - alpha;
1191 butcher[1][2] = alpha;
1193 butcher[2][1] = 1. - alpha;
1194 butcher[2][2] = alpha;
1198 else if(
method.find(
"Crouzieux") != std::string::npos){
1201 butcher[0][0] = 0.5 + beta;
1202 butcher[0][1] = 0.5 + beta;
1204 butcher[1][0] = 0.5 - beta;
1205 butcher[1][1] = -1. / sqrt(3);
1206 butcher[1][2] = 0.5 + beta;
1208 butcher[2][1] = 0.5;
1209 butcher[2][2] = 0.5;
1213 else if(
method.find(
"Midpoint Rule") != std::string::npos){
1215 butcher[0][0] = 0.5;
1216 butcher[0][1] = 0.5;
1221 else if(
method.find(
"Fractional Step Theta") != std::string::npos){
1226 butcher[1][0] =
theta;
1227 butcher[1][1] = beta *
theta;
1228 butcher[1][2] = alpha *
theta;
1230 butcher[2][0] = 1.-
theta;
1231 butcher[2][1] = beta *
theta;
1232 butcher[2][2] = alpha * (1.-
theta);
1233 butcher[2][3] = alpha *
theta;
1236 butcher[3][1] = beta *
theta;
1237 butcher[3][2] = alpha * (1.-
theta);
1238 butcher[3][3] = (alpha + beta) *
theta;
1239 butcher[3][4] = alpha *
theta;
1241 butcher[4][1] = beta *
theta;
1242 butcher[4][2] = alpha * (1.-
theta);
1243 butcher[4][3] = (alpha + beta) *
theta;
1244 butcher[4][4] = alpha *
theta;
1249 HDNUM_ERROR(
"Order not available for Runge Kutta solver.");
1253 static int initOrder(
const std::string
method)
1255 if(
method.find(
"Implicit Euler") != std::string::npos){
1258 else if(method.find(
"Alexander") != std::string::npos){
1261 else if(method.find(
"Crouzieux") != std::string::npos){
1264 else if(method.find(
"Midpoint Rule") != std::string::npos){
1267 else if(method.find(
"Fractional Step Theta") != std::string::npos){
1271 HDNUM_ERROR(
"Order not available for Runge Kutta solver.");
1278 class NonlinearProblem
1282 typedef typename M::size_type size_type;
1285 typedef typename M::number_type number_type;
1288 NonlinearProblem (
const M& model_,
const Vector<number_type>& yold_,
1289 typename M::time_type told_,
typename M::time_type dt_,
1291 const std::vector< Vector<number_type> > & k_)
1292 : model(model_), yold(yold_), told(told_),
1293 dt(dt_), butcher(butcher_), rk_step(rk_step_), k_old(model.size(),0)
1295 for(
int i=0; i<rk_step; ++i)
1296 k_old.
update(butcher[rk_step][1+i] * dt, k_[i]);
1300 std::size_t size ()
const
1302 return model.size();
1306 void F (
const Vector<number_type>& x, Vector<number_type>& result)
const
1310 Vector<number_type> current_z(x);
1311 current_z.update(1.,yold);
1313 const number_type tnew = told + butcher[rk_step][0] * dt;
1315 Vector<number_type> current_k(model.size(),0.);
1316 model.f(tnew,current_z,current_k);
1317 result.
update(butcher[rk_step][rk_step+1] * dt, current_k);
1323 void F_x (
const Vector<number_type>& x, DenseMatrix<number_type>& result)
const
1325 const number_type tnew = told + butcher[rk_step][0] * dt;
1327 Vector<number_type> current_z(x);
1328 current_z.update(1.,yold);
1330 model.f_x(tnew,current_z,result);
1332 result *= dt * butcher[rk_step][rk_step+1];
1334 for (size_type i=0; i<model.size(); i++) result[i][i] -= number_type(1.0);
1337 void set_told_dt (
typename M::time_type told_,
typename M::time_type dt_)
1345 const Vector<number_type>& yold;
1346 typename M::time_type told;
1347 typename M::time_type dt;
1350 Vector<number_type> k_old;
1359 u(model.size()), order(
order_)
1361 model.initialize(t,u);
1368 : verbosity(0), butcher(initTableau(
method)), model(
model_), newton(
newton_), u(model.size()),
1371 model.initialize(t,u);
1397 std::cout <<
"DIRK: step to" <<
" t+dt=" << t+dt <<
" dt=" << dt << std::endl;
1401 bool converged =
true;
1404 std::vector< Vector<number_type> >
k;
1407 std::cout <<
"DIRK: step nr "<<
i << std::endl;
1415 NonlinearProblem
nlp(model,u,t,dt,butcher,
i,
k);
1419 converged = converged && newton.has_converged();
1434 std::cout <<
"DIRK finished"<< std::endl;
1443 dt = std::min(2.0*dt,dtmax);
1445 std::cout <<
"DIRK: increasing time step to " << dt << std::endl;
1453 HDNUM_ERROR(
"time step too small in implicit Euler");
1459 if (verbosity>0) std::cout <<
"DIRK: reducing time step to " << dt << std::endl;
1520 template<
class T,
class N>
1521 inline void gnuplot (
const std::string&
fname,
const std::vector<T> t,
const std::vector<
Vector<N> > u)
1523 std::fstream f(
fname.c_str(),std::ios::out);
1524 for (
typename std::vector<T>::size_type n=0; n<t.size(); n++)
1526 f << std::scientific << std::showpoint
1527 << std::setprecision(16) << t[n];
1529 f <<
" " << std::scientific << std::showpoint
1530 << std::setprecision(u[n].precision()) << u[n][
i];
1537 template<
class T,
class N>
1538 inline void gnuplot (
const std::string&
fname,
const std::vector<T> t,
const std::vector<
Vector<N> > u,
const std::vector<T> dt)
1540 std::fstream f(
fname.c_str(),std::ios::out);
1541 for (
typename std::vector<T>::size_type n=0; n<t.size(); n++)
1543 f << std::scientific << std::showpoint
1544 << std::setprecision(16) << t[n];
1546 f <<
" " << std::scientific << std::showpoint
1547 << std::setprecision(u[n].precision()) << u[n][
i];
1548 f <<
" " << std::scientific << std::showpoint
1549 << std::setprecision(16) << dt[n];
Implementation of a general Diagonal Implicit Runge-Kutta method.
Definition ode.hh:1154
M::number_type number_type
export number_type
Definition ode.hh:1164
void step()
do one step
Definition ode.hh:1389
void set_dt(time_type dt_)
set time step for subsequent steps
Definition ode.hh:1377
time_type get_time() const
get current time
Definition ode.hh:1484
DIRK(const M &model_, const S &newton_, const ButcherTableau &butcher_, const int order_)
Definition ode.hh:1357
DenseMatrix< number_type > ButcherTableau
the type of a Butcher tableau
Definition ode.hh:1167
void get_info() const
print some information
Definition ode.hh:1502
bool get_error() const
get current state
Definition ode.hh:1465
void set_state(time_type t_, const Vector< number_type > &u_)
set current state
Definition ode.hh:1471
M::size_type size_type
export size_type
Definition ode.hh:1158
M::time_type time_type
export time_type
Definition ode.hh:1161
time_type get_dt() const
get dt used in last step (i.e. to compute current state)
Definition ode.hh:1490
void set_verbosity(size_type verbosity_)
set verbosity level
Definition ode.hh:1383
DIRK(const M &model_, const S &newton_, const std::string method)
Definition ode.hh:1367
size_type get_order() const
return consistency order of the method
Definition ode.hh:1496
const Vector< number_type > & get_state() const
get current state
Definition ode.hh:1478
Class with mathematical matrix operations.
Definition densematrix.hh:33
void update(const REAL s, const DenseMatrix &B)
Scaled update of a Matrix.
Definition densematrix.hh:489
size_t colsize() const
get number of columns of the matrix
Definition densematrix.hh:153
Explicit Euler method as an example for an ODE solver.
Definition ode.hh:24
time_type get_time() const
get current time
Definition ode.hh:71
EE(const M &model_)
constructor stores reference to the model
Definition ode.hh:36
M::number_type number_type
export number_type
Definition ode.hh:33
M::size_type size_type
export size_type
Definition ode.hh:27
void step()
do one step
Definition ode.hh:50
M::time_type time_type
export time_type
Definition ode.hh:30
void set_state(time_type t_, const Vector< number_type > &u_)
set current state
Definition ode.hh:58
void set_dt(time_type dt_)
set time step for subsequent steps
Definition ode.hh:44
const Vector< number_type > & get_state() const
get current state
Definition ode.hh:65
size_type get_order() const
return consistency order of the method
Definition ode.hh:83
time_type get_dt() const
get dt used in last step (i.e. to compute current state)
Definition ode.hh:77
Heun method (order 2 with 2 stages)
Definition ode.hh:199
M::size_type size_type
export size_type
Definition ode.hh:202
void set_dt(time_type dt_)
set time step for subsequent steps
Definition ode.hh:223
const Vector< number_type > & get_state() const
get current state
Definition ode.hh:253
size_type get_order() const
return consistency order of the method
Definition ode.hh:271
time_type get_dt() const
get dt used in last step (i.e. to compute current state)
Definition ode.hh:265
void step()
do one step
Definition ode.hh:229
M::time_type time_type
export time_type
Definition ode.hh:205
void set_state(time_type t_, const Vector< number_type > &u_)
set current state
Definition ode.hh:246
time_type get_time() const
get current time
Definition ode.hh:259
Heun2(const M &model_)
constructor stores reference to the model
Definition ode.hh:211
M::number_type number_type
export number_type
Definition ode.hh:208
Heun method (order 3 with 3 stages)
Definition ode.hh:295
const Vector< number_type > & get_state() const
get current state
Definition ode.hh:358
void set_dt(time_type dt_)
set time step for subsequent steps
Definition ode.hh:323
M::time_type time_type
export time_type
Definition ode.hh:301
M::number_type number_type
export number_type
Definition ode.hh:304
time_type get_dt() const
get dt used in last step (i.e. to compute current state)
Definition ode.hh:370
void set_state(time_type t_, const Vector< number_type > &u_)
set current state
Definition ode.hh:351
size_type get_order() const
return consistency order of the method
Definition ode.hh:376
Heun3(const M &model_)
constructor stores reference to the model
Definition ode.hh:307
M::size_type size_type
export size_type
Definition ode.hh:298
time_type get_time() const
get current time
Definition ode.hh:364
void step()
do one step
Definition ode.hh:329
Implicit Euler using Newton's method to solve nonlinear system.
Definition ode.hh:964
M::number_type number_type
export number_type
Definition ode.hh:1026
IE(const M &model_, const S &newton_)
constructor stores reference to the model
Definition ode.hh:1029
void set_dt(time_type dt_)
set time step for subsequent steps
Definition ode.hh:1037
time_type get_dt() const
get dt used in last step (i.e. to compute current state)
Definition ode.hh:1114
void set_verbosity(size_type verbosity_)
set verbosity level
Definition ode.hh:1043
M::time_type time_type
export time_type
Definition ode.hh:1023
bool get_error() const
get current state
Definition ode.hh:1089
const Vector< number_type > & get_state() const
get current state
Definition ode.hh:1102
void get_info() const
print some information
Definition ode.hh:1126
void step()
do one step
Definition ode.hh:1049
void set_state(time_type t_, const Vector< number_type > &u_)
set current state
Definition ode.hh:1095
size_type get_order() const
return consistency order of the method
Definition ode.hh:1120
time_type get_time() const
get current time
Definition ode.hh:1108
M::size_type size_type
export size_type
Definition ode.hh:1020
Kutta method (order 3 with 3 stages)
Definition ode.hh:399
M::time_type time_type
export time_type
Definition ode.hh:405
M::number_type number_type
export number_type
Definition ode.hh:408
const Vector< number_type > & get_state() const
get current state
Definition ode.hh:465
void set_state(time_type t_, const Vector< number_type > &u_)
set current state
Definition ode.hh:458
time_type get_dt() const
get dt used in last step (i.e. to compute current state)
Definition ode.hh:477
void step()
do one step
Definition ode.hh:434
size_type get_order() const
return consistency order of the method
Definition ode.hh:483
M::size_type size_type
export size_type
Definition ode.hh:402
time_type get_time() const
get current time
Definition ode.hh:471
void set_dt(time_type dt_)
set time step for subsequent steps
Definition ode.hh:428
Kutta3(const M &model_)
constructor stores reference to the model
Definition ode.hh:411
Modified Euler method (order 2 with 2 stages)
Definition ode.hh:105
void step()
do one step
Definition ode.hh:134
M::time_type time_type
export time_type
Definition ode.hh:111
size_type get_order() const
return consistency order of the method
Definition ode.hh:175
void set_state(time_type t_, const Vector< number_type > &u_)
set current state
Definition ode.hh:150
const Vector< number_type > & get_state() const
get current state
Definition ode.hh:157
time_type get_dt() const
get dt used in last step (i.e. to compute current state)
Definition ode.hh:169
M::size_type size_type
export size_type
Definition ode.hh:108
M::number_type number_type
export number_type
Definition ode.hh:114
ModifiedEuler(const M &model_)
constructor stores reference to the model
Definition ode.hh:117
void set_dt(time_type dt_)
set time step for subsequent steps
Definition ode.hh:128
time_type get_time() const
get current time
Definition ode.hh:163
Adaptive one-step method using Richardson extrapolation.
Definition ode.hh:826
M::number_type number_type
export number_type
Definition ode.hh:835
void get_info() const
print some information
Definition ode.hh:937
RE(const M &model_, S &solver_)
constructor stores reference to the model
Definition ode.hh:838
const Vector< number_type > & get_state() const
get current state
Definition ode.hh:913
void step()
do one step
Definition ode.hh:868
time_type get_dt() const
get dt used in last step (i.e. to compute current state)
Definition ode.hh:925
void set_TOL(time_type TOL_)
set tolerance for adaptive computation
Definition ode.hh:862
size_type get_order() const
return consistency order of the method
Definition ode.hh:931
void set_dt(time_type dt_)
set time step for subsequent steps
Definition ode.hh:856
time_type get_time() const
get current time
Definition ode.hh:919
M::size_type size_type
export size_type
Definition ode.hh:829
M::time_type time_type
export time_type
Definition ode.hh:832
Adaptive Runge-Kutta-Fehlberg method.
Definition ode.hh:616
size_type get_order() const
return consistency order of the method
Definition ode.hh:794
M::time_type time_type
export time_type
Definition ode.hh:622
const Vector< number_type > & get_state() const
get current state
Definition ode.hh:776
time_type get_time() const
get current time
Definition ode.hh:782
time_type get_dt() const
get dt used in last step (i.e. to compute current state)
Definition ode.hh:788
void get_info() const
print some information
Definition ode.hh:800
RKF45(const M &model_)
constructor stores reference to the model
Definition ode.hh:628
M::size_type size_type
export size_type
Definition ode.hh:619
void set_dt(time_type dt_)
set time step for subsequent steps
Definition ode.hh:683
void step()
do one step
Definition ode.hh:695
void set_TOL(time_type TOL_)
set tolerance for adaptive computation
Definition ode.hh:689
M::number_type number_type
export number_type
Definition ode.hh:625
classical Runge-Kutta method (order 4 with 4 stages)
Definition ode.hh:506
void set_dt(time_type dt_)
set time step for subsequent steps
Definition ode.hh:537
void set_state(time_type t_, const Vector< number_type > &u_)
set current state
Definition ode.hh:572
size_type get_order() const
return consistency order of the method
Definition ode.hh:597
void step()
do one step
Definition ode.hh:543
M::size_type size_type
export size_type
Definition ode.hh:509
RungeKutta4(const M &model_)
constructor stores reference to the model
Definition ode.hh:518
time_type get_dt() const
get dt used in last step (i.e. to compute current state)
Definition ode.hh:591
const Vector< number_type > & get_state() const
get current state
Definition ode.hh:579
M::number_type number_type
export number_type
Definition ode.hh:515
M::time_type time_type
export time_type
Definition ode.hh:512
time_type get_time() const
get current time
Definition ode.hh:585
std::size_t size_type
Type used for array indices.
Definition vector.hh:34
Vector & update(const REAL alpha, const Vector &y)
Update vector by addition of a scaled vector (x += a y )
Definition vector.hh:201
Newton's method with line search.