Skip to main content

Scientific Computing

Solving ODEs

ODE Solvers -... []

This material has been adapted from material by Joe Pitt-Francis from the "Scientific Computing" module of the SABS R³ Center for Doctoral Training.

This material has been adapted from material by Joe Pitt-Francis from the "Scientific Computing" module of the SABS R³ Center for Doctoral Training.

Creative Commons License
This course material was developed as part of UNIVERSE-HPC, which is funded through the SPF ExCALIBUR programme under grant number EP/W035731/1

This course material was developed as part of UNIVERSE-HPC, which is funded through the SPF ExCALIBUR programme under grant number EP/W035731/1

Creative Commons License

ODE Solvers - PM Exercises

In these exercises we will use Python to solve a variety of Ordinary Differential Equations (ODEs), including initial and boundary value problems. Prior to attempting the exercises you should be familiar with how to solve first order ODEs using separable solutions and integrating factors. You should also know how solve a second order ODE with constant coefficients and how to reduce a general second order ODE to a system of coupled first order equations. If you are not familiar with this material then speak to a module leader or demonstrator who will give you a tutorial. Advanced exercises are marked with a star (\star); you should attempt these only if you have time and have completed the other exercises.

Motivation

Ordinary differential equations are used very frequently in modelling biological and physiological processes. Most commonly they are used to model the way in which quantities of interest (such as concentrations of drugs, viral load, or population densities) change as a function of time. The earlier exercises below are revision of the kinds of ODEs you may have encountered at A level or as an undergraduate. The later exercises are taken from real models of chemical and biological systems.

The intentions behind this exercise are:

  • To give you a clear goal to meet

  • To give you a chance to revise or learn a few things. Specifically:

    • The abstract base class pattern;

    • Unit testing with unittest;

    • Ordinary differential equation solver implementations.

A nice feature of this assignment is that you are provided with a testing infrastructure which gives an implicit specification of the code you are required to write. This means that when you've written code such that all the tests pass then you will know that you have completed the exercise.

Initial value problems

Solve the following initial value problems by hand:

  1. dydx=x2\dfrac{\textrm{d} y}{\textrm{d} x} = x^{2},   \; subject to y(0)=1y(0)=1.

  2. dxdt=t2x\dfrac{\textrm{d} x}{\textrm{d} t} = \dfrac{t^{2}}{x},   \; subject to x(0)=1x(0)=1.

  3. dydx+yx=1\dfrac{\textrm{d} y}{\textrm{d} x} + \dfrac{y}{x}=1,   \; subject to y(1)=0y(1)=0.

  4. dy1dx=y2\dfrac{\textrm{d} y_{1}}{\textrm{d} x} = -y_{2} and dy2dx=y1\dfrac{\textrm{d} y_{2}}{\textrm{d} x} = y_{1},   \; subject to y1(0)=1y_{1}(0)=1 and y2(0)=0y_{2}(0)=0.

  5. d2ydx2+3dydx4y=0\dfrac{\textrm{d}^{2} y}{\textrm{d} x^{2}} +3\dfrac{\textrm{d} y}{\textrm{d} x} -4y=0,   \; subject to y(0)=1y(0)=1 and y(0)=0y'(0)=0.

Solve each of the above initial value problems numerically using your best Python ODE solver and compare with the analytic solutions. Note that in order to solve the last problem numerically you will need to reformulate the equation as a system of first order ODEs. See if, for some of your answers, you can make a figure similar to those below, made using model solutions.

Solution for question 2. Solution for question 4.

Boundary value problems

Use scipy.integrate.solve_bvp to solve the boundary value problem

d2ydx2+3dydx4y=0,\begin{aligned} \dfrac{\textrm{d}^{2} y}{\textrm{d} x^{2}} + 3 \dfrac{\textrm{d} y}{\textrm{d} x} -4y=0, \end{aligned}

subject to the boundary conditions y(0)=1y(0)=1 and y(1)=1y(1)=1.

Solve the same boundary value problem, but now with the boundary conditions y(0)=0y'(0) = 0 and y(1)=1y(1)=1.

Chemical reaction systems

Mathematical models of simple chemical or biochemical reaction mechanisms often take the form of non-linear systems of ordinary differential equations (derived using the standard chemical laws of mass action). Often the various reactions making up the system happen on very different time scales leading to a stiff system. An example is Robertson's chemical reaction model, in which the concentrations of three reacting chemical species evolve according to the system of equations

dy1dx=0.04y1+10000y2y3,dy2dx=0.04y110000y2y330000000y22,dy3dx=30000000y22,\begin{aligned} \dfrac{\textrm{d} y_{1}}{\textrm{d} x} &= -0.04y_1 + 10000 y_2 y_3, \\ \dfrac{\textrm{d} y_{2}}{\textrm{d} x} &=0.04y_1 - 10000 y_2 y_3 - 30000000y_2^2,\\ \dfrac{\textrm{d} y_{3}}{\textrm{d} x} &= 30000000y_2^2, \end{aligned}

with initial conditions

y1(0)=1,  y2(0)=0,  y3(0)=0.\begin{aligned} y_1(0) = 1, \; y_2(0) = 0, \; y_3 (0) = 0. \end{aligned}

Note: your mileage may vary with this question, because with more modern versions of scipy it becomes increasingly hard to stop the integration scheme being "clever" and using a sophisticated scheme earlier.

  1. Read the documentation for scipy.integrate.ode. Solve this system using the Dormand & Prince dopri solver, which is a high-order Runge-Kutta solver, until t=100t=100 in steps of Δt=1\Delta t=1. (Note that the interface is a bit more difficult to set up than odeint but an example is given with the documentation.) What warning do you get from the solver? Can you change any of the dopri parameters to get rid of warnings? How long does the integrator take to solve the system?

  2. If you are still getting warnings for the dopri then you will have a hint to help you select a better scipy.integrate.ode method. Switch to such a method. Again work to remove all warnings.

  3. (\star) Explain what is happening mathematically and chemically.

  4. Repeat parts (a)--(b) for the system

    dy1dx=0.04y1+y2y3,dy2dx=0.04y1y2y330y22,dy3dx=30y22,\begin{aligned} \dfrac{\textrm{d} y_{1}}{\textrm{d} x} &= -0.04y_1 + y_2 y_3, \\ \dfrac{\textrm{d} y_{2}}{\textrm{d} x} &=0.04y_1 - y_2 y_3 - 30y_2^2,\\ \dfrac{\textrm{d} y_{3}}{\textrm{d} x} &= 30y_2^2, \end{aligned}

    with the same initial conditions. Which solver is faster now?
    Which solver gives you warnings now?

  5. Which solver should you use in which situation?

  6. (\star) If you are feeling brave then assess how one of the fixed time-step solvers you have written yourself measures up to the solver you used in part (b). There are 3 species so you may want to extend the functionality to cope with y3y_3. Use the solution from part (b) as a reference to measure your error. How small do you need to make your time-step to get within a particular error?

When Zombies Attack!

(\star)Inspired by a famous SIR model for epidemics there is a SZR model for zombie invasion. The paper describing this model is available to download from https://mysite.science.uottawa.ca/rsmith43/Zombies.pdf which includes modelling code in Matlab.

A more advanced model included in the paper, which includes latent infection and quarantine, is known as the SIZRQ model. This model is defined by

dSdt=ΠβSZδS,dIdt=βSZρIδIκI,dZdt=ρI+ζRαSZσZ,dRdt=δS+δI+αSZζR+γQ,dQdt=κI+σZγQ,\begin{aligned} \dfrac{\textrm{d} S}{\textrm{d} t} &= \Pi - \beta SZ - \delta S, \\ \dfrac{\textrm{d} I}{\textrm{d} t} &= \beta S Z -\rho I - \delta I - \kappa I, \\ \dfrac{\textrm{d} Z}{\textrm{d} t} &= \rho I + \zeta R - \alpha S Z -\sigma Z, \\ \dfrac{\textrm{d} R}{\textrm{d} t} &= \delta S + \delta I + \alpha SZ -\zeta R + \gamma Q, \\ \dfrac{\textrm{d} Q}{\textrm{d} t} &= \kappa I +\sigma Z - \gamma Q, \end{aligned}

with initial conditions

S(0)=500,  I(0)=0,  Z(0)=0,  R(0)=0,  Q(0)=0\begin{aligned} S(0) = 500, \; I(0) = 0, \; Z(0) = 0, \; R(0) = 0, \; Q(0) = 0 \end{aligned}

and parameter values

Π=0,  α=0.005,  β=0.0095,  ζ=0.1,  δ=0.0001,ρ=0.5,  κ=0.1,  σ=0.01,  γ=0.01.\begin{aligned} \Pi = 0, \; \alpha = 0.005, \; & \beta = 0.0095, \; \zeta = 0.1, \; \delta = 0.0001, \\ \rho = 0.5, \; & \kappa = 0.1, \; \sigma = 0.01, \; \gamma = 0.01. \end{aligned}
  1. Solve this system of equations numerically.

  2. How realistic is this type of model? Can you think of any improvements to the model?

Simple events

(\star)ODE-based models of biological systems often include specific events, which may occur either at predetermined times or when a given condition on the values of the state variables or their derivatives is met. The following model describes a zombie outbreak in which there is periodic culling (Spoiler alert. It looks like we all die whatever we do. If you think that's depressing then you might have to read this followup paper). of the zombie population:

dSdt=ΠβSZδS,  ttn,dZdt=βSZ+ζRαSZ,ttn,dRdt=δS+αSZζR,ttn,Z=kZ,t=tn.\begin{aligned} \dfrac{\textrm{d} S}{\textrm{d} t} &= \Pi - \beta SZ - \delta S, & \; t &\ne t_{n} ,\\ \dfrac{\textrm{d} Z}{\textrm{d} t} &= \beta SZ + \zeta R - \alpha S Z, & t &\ne t_{n}, \\ \dfrac{\textrm{d} R}{\textrm{d} t} &= \delta S + \alpha SZ -\zeta R, & t &\ne t_{n}, \\ \triangle Z &= -kZ, & t &= t_{n}. \end{aligned}

Here k(0,1]k\in(0,1] is the kill ratio, and the last equation represents periodic culling of the zombie population.

Solve this model subject to initial conditions

S(0)=500,  Z(0)=0,  and  R(0)=0,\begin{aligned} S(0) = 500, \; Z(0) = 0, \; \text{and} \; R(0) = 0, \end{aligned}

and with parameter values

Π=0,  α=0.005,  β=0.0095,  ζ=0.1,  δ=0.0001,  k=0.25,\begin{aligned} \Pi = 0, \; \alpha = 0.005, \; \beta = 0.0095, \; \zeta = 0.1, \; \delta = 0.0001, \; k = 0.25, \end{aligned}

and culling every 10 units of time (i.e. t1=10t_1=10, t2=20t_2=20, etc.). Hint: you will need to loop over time intervals to solve this model.