-
Notifications
You must be signed in to change notification settings - Fork 0
/
dde.py
81 lines (58 loc) · 2.44 KB
/
dde.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
##!/usr/bin/env python
# REQUIRES PACKAGES Numpy AND Scipy INSTALLED
import numpy as np
import scipy.integrate
import scipy.interpolate
class ddeVar:
"""
special function-like variables for the integration of DDEs
"""
def __init__(self, g, tc=0):
""" g(t) = expression of Y(t) for t<tc """
self.g = g
self.tc = tc
# We must fill the interpolator with 2 points minimum
self.itpr = scipy.interpolate.interp1d(
np.array([tc-1, tc]), # X
np.array([self.g(tc), self.g(tc)]).T, # Y
kind='linear', bounds_error=False,
fill_value = self.g(tc))
def update(self, t, Y):
""" Add one new (ti, yi) to the interpolator """
#print (t, Y)
self.itpr.x = np.hstack([self.itpr.x, [t]])
Y2 = Y if (Y.size==1) else np.array([Y]).T
self.itpr.y = np.hstack([self.itpr.y, Y2])
self.itpr._y = self.itpr._reshape_yi(self.itpr.y)
self.itpr.fill_value = Y
def __call__(self, t=0):
""" Y(t) will return the instance's value at time t """
return (self.g(t) if (t<=self.tc) else self.itpr(t))
class dde(scipy.integrate.ode):
""" Overwrites a few functions of scipy.integrate.ode"""
def __init__(self, f, jac=None):
def f2(t, y, args):
return f(self.Y, t, *args)
scipy.integrate.ode.__init__(self, f2, jac)
#attempt to emulate ode23s from matlab
self.set_integrator('vode', method='bdf', order=15, nsteps=3000)
self.set_f_params(None)
def integrate(self, t, step=0, relax=0):
scipy.integrate.ode.integrate(self, t, step, relax)
self.Y.update(self.t, self.y)
return self.y
def set_initial_value(self, Y):
self.Y = Y #!!! Y will be modified during integration
scipy.integrate.ode.set_initial_value(self, Y(Y.tc), Y.tc)
def ddeint(func, g, tt, fargs=None):
"""
similar to scipy.integrate.odeint. Solves the DDE system
defined by func at the times tt with 'history function' g
and potential additional arguments for the model, fargs
"""
fargs = fargs or []
dde_ = dde(func)
dde_.set_initial_value(ddeVar(g, tt[0]))
dde_.set_f_params(fargs)
return np.array([g(tt[0])]+
[dde_.integrate(dde_.t + dt) for dt in np.diff(tt)])