libode
Easy to compile, fast ODE integrators as C++ classes
Loading...
Searching...
No Matches
ode_sdirk_43.cc
Go to the documentation of this file.
1
2
3#include "ode_sdirk_43.h"
4
5namespace ode {
6
7void NewtonSDIRK43::f_Newton (double *x, double *y) {
8
9 (void)x;
10 unsigned long i;
11 int m;
12 double dt = *dt_;
13
14 //evaluate solution based on current k values
15 for (i=0; i<neq_; i++) {
16 //diagonal term
17 soltemp_[i] = sol_[i] + dt*gam*k_[ik_][i];
18 //terms below the diagonal
19 for (m=0; m<ik_; m++) soltemp_[i] += dt*a[ik_][m]*k_[m][i];
20 }
22 //evaluate the Newton function
23 for (i=0; i<neq_; i++) y[i] = k_[ik_][i] - ftemp_[i];
24}
25
26void NewtonSDIRK43::J_Newton (double *x, double **J) {
27
28 (void)x;
29 unsigned long i,j;
30 int m;
31 double dt = *dt_;
32
33 //evaluate Jacobian with current k values if needed
34 if ( !get_modified() ) {
35 for (i=0; i<neq_; i++) {
36 //diagonal term
37 soltemp_[i] = sol_[i] + dt*gam*k_[ik_][i];
38 //terms below the diagonal
39 for (m=0; m<ik_; m++) soltemp_[i] += dt*a[ik_][m]*k_[m][i];
40 }
42 }
43 //evaluate the Newton system's Jacobian
44 for (i=0; i<neq_; i++) {
45 for (j=0; j<neq_; j++) {
46 J[i][j] = -dt*gam*Jac_[i][j];
47 }
48 J[i][i] += 1.0;
49 }
50}
51
52//------------------------------------------------------------------------------
53
54OdeSDIRK43::OdeSDIRK43 (unsigned long neq) :
55 OdeEmbedded (neq, true, 3),
56 OdeRK (neq, 5) {
57
58 method_ = "SDIRK43";
59
60 int nk = 5;
61 a = new double*[nk];
62 for (int i=0; i<nk; i++) a[i] = new double[nk];
63 b = new double[nk];
64 d = new double[nk];
65
66 gam = 1.0/4;
67
68 a[1][0] = 1.0/2;
69 a[2][0] = 17.0/50; a[2][1] = -1.0/25;
70 a[3][0] = 371.0/1360; a[3][1] = -137.0/2720; a[3][2] = 15.0/544;
71 a[4][0] = 25.0/24; a[4][1] = -49.0/48; a[4][2] = 125.0/16; a[4][3] = -85.0/12;
72
73 b[0] = a[4][0]; b[1] = a[4][1]; b[2] = a[4][2]; b[3] = a[4][3]; b[4] = gam;
74 d[0] = 59.0/48; d[1] = -17.0/96; d[2] = 225.0/32; d[3] = -85.0/12;
75
76 newton_ = new NewtonSDIRK43(neq, this);
77 newton_->set_modified(true);
78}
79
81 for (int i=0; i<nk_; i++) delete [] a[i];
82 delete [] a;
83 delete [] b;
84 delete [] d;
85}
86
87void OdeSDIRK43::step_ (double dt) {
88
89 unsigned long i;
90 //compute the ode system's Jacobian if using modified Newton iterations
91 if ( newton_->get_modified() ) ode_jac_(sol_, Jac_);
92 //solve for first k vector
93 for (i=0; i<neq_; i++) k_[0][i] = 0.0;
94 newton_->set_ignore_JLU(false);
95 newton_->set_ik(0);
96 newton_->solve_Newton(k_[0]);
97 //solve for subsequent stage k vectors
98 newton_->set_ignore_JLU(true);
99 for (int j=1; j<nk_; j++) {
100 //use previous stage values as initial guess
101 for (i=0; i<neq_; i++) k_[j][i] = k_[j-1][i];
102 //solve
103 newton_->set_ik(j);
104 newton_->solve_Newton(k_[j]);
105 }
106 //compute solution
107 for (i=0; i<neq_; i++) {
108 solemb_[i] = sol_[i] + dt*(d[0]*k_[0][i]
109 + d[1]*k_[1][i]
110 + d[2]*k_[2][i]
111 + d[3]*k_[3][i]);
112 sol_[i] += dt*(b[0]*k_[0][i]
113 + b[1]*k_[1][i]
114 + b[2]*k_[2][i]
115 + b[3]*k_[3][i]
116 + b[4]*k_[4][i]);
117 }
118}
119
120} // namespace ode
Nonlinear system solver for OdeSDIRK43.
void ode_jac_(double *solin, double **Jout)
wrapper, calls ode_jac() and increments nJac;
Definition ode_base.cc:95
unsigned long neq_
number of equations in the system of ODEs
Definition ode_base.h:327
double ** Jac_
storage for the ODE system's Jacobian matrix, only allocated for the methods that need it
Definition ode_base.h:341
std::string method_
the "name" of the solver/method, as in "Euler" or "RK4"
Definition ode_base.h:319
double * sol_
array for the solution, changing over time
Definition ode_base.h:333
Base clase implementing methods for embedded Runge-Kutta error estimation.
double * solemb_
embedded solution array
double ** Jac_
pointer to the solver's Jacobian matrix
void fun(double *solin, double *fout)
wrapper around system evaluation function
unsigned long neq_
ODE system size.
double * dt_
pointer to time step member
void jac(double *solin, double **Jout)
wrapper around Jacobian evaluation function
double * ftemp_
temporary values for evaluation of Newton function
double * sol_
pointer to the solver's solution vector
double * soltemp_
temporary solution values
void set_modified(bool modified)
sets whether modified Newtion's is being used
Definition ode_newton.h:56
int solve_Newton(double *x)
Solve the system of equations.
Definition ode_newton.cc:84
bool get_modified()
gets whether modified Newtion's is being used
Definition ode_newton.h:45
void set_ignore_JLU(bool ignore_JLU)
sets whether no LU decompositions should be done
Definition ode_newton.h:58
void set_ik(int ik)
sets the index of the k vector being solved for
double ** k_
pointer to the stage slopes of RK methods
double ** a
pointer to tableau coefficients
double gam
diagonal tableau coefficient
int ik_
the index of the k vector being solved for
Provides space for stage slope values, an array of arrays for k values.
Definition ode_rk.h:9
int nk_
number of stages, or k vectors
Definition ode_rk.h:25
double ** k_
stage evaluations
Definition ode_rk.h:23
~OdeSDIRK43()
destructs
OdeSDIRK43(unsigned long neq)
constructs