libode
Easy to compile, fast ODE integrators as C++ classes
Loading...
Searching...
No Matches
ode_grk4a.cc
Go to the documentation of this file.
1
2
3#include "ode_grk4a.h"
4
5namespace ode {
6
7OdeGRK4A::OdeGRK4A (unsigned long neq) :
8 OdeEmbedded (neq, true, 3),
9 OdeRosenbrock (neq, 4) {
10
11 method_ = "GRK4A";
12 //main diagonal, gam_ii
13 gam = 0.395;
14 //all other gamma values
15 gam21 = -0.767672395484;
16 gam31 = -0.851675323742; gam32 = 0.522967289188;
17 gam41 = 0.288463109545; gam42 = 0.0880214273381; gam43 = -0.337389840627;
18 //reduced gamma values
19 g21 = gam21/gam;
20 g31 = gam31/gam; g32 = gam32/gam;
21 g41 = gam41/gam; g42 = gam42/gam; g43 = gam43/gam;
22 //alpha values
23 alp21 = 0.438;
24 alp31 = 0.796920457938; alp32 = 0.0730795420615;
25 //k weights for 4th and 3rd order solutions
26 b1 = 0.199293275701; b2 = 0.482645235674; b3 = 0.0680614886256; b4 = 0.25;
27 d1 = 0.346325833758; d2 = 0.285693175712; d3 = 0.367980990530;
28
29}
30
31void OdeGRK4A::step_ (double dt) {
32
33 unsigned long i;
34
35 //------------------------------------------------------------------
36 //first evaluate the Jacobian, do arithmetic, and LU factorize it
37
39 prep_jac(Jac_, neq_, dt, p_);
40
41 //------------------------------------------------------------------
42 //stage 1
43
44 ode_fun_(sol_, k_[0]);
45 for (i=0; i<neq_; i++) rhs_[i] = dt*k_[0][i];
47
48 //------------------------------------------------------------------
49 //stage 2
50
51 //temporary solution vector for evaluating ode_fun
52 for (i=0; i<neq_; i++) soltemp_[i] = sol_[i] + alp21*k_[0][i];
53 //put new slope values into k vector temporarily
54 ode_fun_(soltemp_, k_[1]);
55 //compute right hand side of matrix equation
56 for (i=0; i<neq_; i++) rhs_[i] = dt*k_[1][i] + g21*k_[0][i];
57 //solve the matrix equation
59 //recover the actual k values
60 for (i=0; i<neq_; i++) k_[1][i] -= g21*k_[0][i];
61
62 //------------------------------------------------------------------
63 //stage 3
64
65 //temporary solution vector for evaluating ode_fun
66 for (i=0; i<neq_; i++) soltemp_[i] = sol_[i] + alp31*k_[0][i]
67 + alp32*k_[1][i];
68 //put new slope values into k vector temporarily
69 ode_fun_(soltemp_, k_[2]);
70 //compute right hand side of matrix equation
71 for (i=0; i<neq_; i++) rhs_[i] = dt*k_[2][i] + g31*k_[0][i]
72 + g32*k_[1][i];
73 //solve the matrix equation
75 //recover the actual k values
76 for (i=0; i<neq_; i++) k_[2][i] -= g31*k_[0][i] + g32*k_[1][i];
77
78 //------------------------------------------------------------------
79 //stage 4
80
81 //temporary solution vector for evaluating ode_fun
82 for (i=0; i<neq_; i++) soltemp_[i] = sol_[i] + alp31*k_[0][i]
83 + alp32*k_[1][i];
84 //put new slope values into k vector temporarily
85 ode_fun_(soltemp_, k_[3]);
86 //compute right hand side of matrix equation
87 for (i=0; i<neq_; i++) rhs_[i] = dt*k_[3][i] + g41*k_[0][i]
88 + g42*k_[1][i]
89 + g43*k_[2][i];
90 //solve the matrix equation
92 //recover the actual k values
93 for (i=0; i<neq_; i++) k_[3][i] -= g41*k_[0][i] + g42*k_[1][i] + g43*k_[2][i];
94
95 //------------------------------------------------------------------
96 //solution and embedded solution
97
98 for (i=0; i<neq_; i++) {
99 solemb_[i] = sol_[i] + (d1*k_[0][i]
100 + d2*k_[1][i]
101 + d3*k_[2][i]);
102 sol_[i] = sol_[i] + (b1*k_[0][i]
103 + b2*k_[1][i]
104 + b3*k_[2][i]
105 + b4*k_[3][i]);
106 }
107}
108
109} // namespace ode
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
void ode_fun_(double *solin, double *fout)
wrapper, calls ode_fun() and increases the neval counter by one
Definition ode_base.cc:87
Base clase implementing methods for embedded Runge-Kutta error estimation.
double * solemb_
embedded solution array
OdeGRK4A(unsigned long neq)
constructs
Definition ode_grk4a.cc:7
Base class for Rosenbrock methods.
void prep_jac(double **Jac, unsigned long n, double dt, int *p)
do necessary arithmetic with the Jacobian then lu factor it
int * p_
permutation array for LU factorization
double ** k_
stage derivatives
double * soltemp_
temporary sol vector
double * rhs_
right hand side of matrix equations
double gam
parameter multipying Jacobian or single diagonal gamma
void ode_solve_LU(double **LU, int *p, double *b, int n, double *out)
Solves a matrix equation where the matrix has already be crout LU decomposed.
Definition ode_linalg.cc:76