In my model each agent solves a system of ODEs at each tick. I have employed Eulers method (similar to the systems dynamics modeler in NetLogo) to solve these first order ODEs. However, for a stable solution, I am forced to use a very small time step (dt), which means the simulation proceeds very slowly with this method. I´m curious if anyone has advice on a method to solve the ODEs more quickly? I am considering implementing Runge-Kutta (with a larger time step?) as was done here (http://academic.evergreen.edu/m/mcavityd/netlogo/Bouncing_Ball.html). I would also consider using the R extension and using an ODE solver in R. But again, the ODEs are solved by each agent, so I don´t know if this is an efficient method.
I´m hoping someone has a feel for the performance of these methods and could offer some advice. If not, I will try to share what I find out.
In general your idea is correct. For a method of order p
to reach a global error level tol
over an integration interval of length T
you will need a step size in the magnitude range
h=pow(tol/T,1.0/p).
However, not only the discretization error accumulates over the N=T/h
steps, but also the floating point error. This gives a lower bound for useful step sizes of magnitude h=pow(T*mu,1.0/(p+1))
.
Example: For T=1
, mu=1e-15
and tol=1e-6
the Euler method of order 1 would need a step size of about
h=1e-6
and thusN=1e+6
steps and function evaluations. The range of step sizes where reasonable results can be expected is bounded below byh=3e-8
.the improved Euler or Heun method has order 2, which implies a step size
1e-3
,N=1000
steps and2N=2000
function evaluations, the lower bound for useful step sizes is1e-3
.the classical Runge-Kutta method has order 4, which gives a required step size of about
h=3e-2
with aboutN=30
steps and4N=120
function evaluations. The lower bound is1e-3
.
So there is a significant gain to be had by using higher order methods. At the same time the range where step size reduction results in a lower global error also gets significantly narrower for increasing order. But at the same time the achievable accuracy increases. So one has to knowingly care when the point is reached to leave well enough alone.
The implementation of RK4 in the ball example, as in general for the numerical integration of ODE, is for an ODE system x'=f(t,x)
, where x
is the, possibly very large, state vector
A second order ODE (system) is transformed to a first order system by making the velocities members of the state vector. x''=a(x,x')
gets transformed to [x',v']=[v, a(x,v)]
. The big vector of the agent system is then composed of the collection of the pairs [x,v]
or, if desired, as the concatenation of the collection of all x
components and the collection of all v
components.
In an agent based system it is reasonable to store the components of the state vector belonging to the agent as internal variables of the agent. Then the vector operations are performed by iterating over the agent collection and computing the operation tailored to the internal variables.
Taking into consideration that in the LOGO language there are no explicit parameters for function calls, the evaluation of dotx = f(t,x)
needs to first fix the correct values of t
and x
before calling the function evaluation of f
save t0=t, x0=x
evaluate k1 = f_of_t_x
set t=t0+h/2, x=x0+h/2*k1
evaluate k2=f_of_t_x
set x=x0+h/2*k2
evaluate k3=f_of_t_x
set t=t+h, x=x0+h*k3
evaluate k4=f_of_t_x
set x=x0+h/6*(k1+2*(k2+k3)+k4)
来源:https://stackoverflow.com/questions/29564018/solving-odes-in-netlogo-eulers-vs-r-k-vs-r-solver