libode
Easy to compile, fast ODE integrators as C++ classes
Loading...
Searching...
No Matches
ode_backward_euler.cc
Go to the documentation of this file.
1
2
4
5namespace ode {
6
7void NewtonBackwardEuler::f_Newton (double *x, double *y) {
8
9 (void)x; //supress unused variable warning
10 unsigned long i;
11 double dt = *dt_; //get time step from solver object
12
13 //evaluate temporary solution based on current k values
14 for (i=0; i<neq_; i++) soltemp_[i] = sol_[i] + dt*k_[0][i];
15 //compute k1 values
17 //evaluate the Newton function's k1 component
18 for (i=0; i<neq_; i++) y[i] = k_[0][i] - ftemp_[i];
19}
20
21void NewtonBackwardEuler::J_Newton (double *x, double **J) {
22
23 (void)x; //supress unused variable warning
24 unsigned long i,j;
25 double dt = *dt_; //get time step from solver object
26
27 if ( get_modified() ) jac(sol_, Jac_);
28
29 if ( !get_modified() ) {
30 //evaluate temporary solution based on current k values for k1
31 for (i=0; i<neq_; i++) soltemp_[i] = sol_[i] + dt*k_[0][i];
32 //evaluate J at soltemp
34 }
35 //evaluate the Newton jacobian
36 for (i=0; i<neq_; i++) {
37 for (j=0; j<neq_; j++) {
38 J[i][j] = -dt*Jac_[i][j];
39 }
40 J[i][i] += 1.0; //subtract from the identity matrix
41 }
42}
43
44//------------------------------------------------------------------------------
45
47 OdeAdaptive (neq, true),
48 OdeIRK (neq, 1) {
49
50 method_ = "BackwardEuler";
51
52 newton_ = new NewtonBackwardEuler(neq, 1, this);
53 newton_->set_modified(true);
54}
55
57
58void OdeBackwardEuler::step_ (double dt) {
59
60 unsigned long i;
61 //set k values to zero
62 for (i=0; i<neq_; i++) k_[0][i] = 0.0;
63 //solve for slopes
64 newton_->solve_Newton(k_[0]);
65 //compute new solution
66 for (i=0; i<neq_; i++) sol_[i] += dt*k_[0][i];
67}
68
69} // namespace ode
Nonlinear system solver for OdeBackwardEuler.
Base class implementing solver functions with adaptive time steps.
OdeBackwardEuler(unsigned long neq)
constructs
unsigned long neq_
number of equations in the system of ODEs
Definition ode_base.h:327
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
Provides a large vector containing the slope values of all stages with pointers to each of the indivi...
Definition ode_irk.h:9
double ** k_
individual k arrays for each stage
Definition ode_irk.h:25
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
double ** k_
pointer to the stage slopes of RK methods