I was interested in integrating a vector field (i.e finding a streamline) for a given initial point using the scipy.integrate
library. Since the vector field is a numpy.ndarray
object, defined on a computational grid, the values in between the grid points have to be interpolated. Do any of the integrators handle this? That is, if I were to for instance attempt the following
import numpy as np
import scipy.integrate as sc
vx = np.random.randn(10,10)
vy = np.random.randn(10,10)
def f(x,t):
return [vx[x[0],x[1]], vy[x[0],x[1]]] # which obviously does not work if x[i] is a float
p0 = (0.5,0.5)
dt = 0.1
t0 = 0
t1 = 1
t = np.arange(t0,t1+dt,dt)
sc.odeint(f,p0,t)
Edit :
I need to return the interpolated values of the vector field of the surrounding grid points :
def f(x,t):
im1 = int(np.floor(x[0]))
ip1 = int(np.ceil(x[1]))
jm1 = int(np.floor(x[0]))
jp1 = int(np.ceil(x[1]))
if (im1 == ip1) and (jm1 == jp1):
return [vx[x[0],x[1]], vy[x[0],x[1]]]
else:
points = (im1,jm1),(ip1,jm1),(im1,jp1),(ip1,jp1)
values_x = vx[im1,jm1],vx[ip1,jm1],vx[im1,jp1],vx[ip1,jp1]
values_y = vy[im1,jm1],vy[ip1,jm1],vy[im1,jp1],vy[ip1,jp1]
return interpolated_values(points,values_x,values_y) # how ?
The last return statement is just some pseudo code. But this is basically what I am looking for.
Edit :
The scipy.interpolate.griddata function seem to be the way to go. Is it possible to incorporate it inside the function it self ? Something in the lines of this :
def f(x,t):
return [scipy.interpolate.griddata(x,vx),scipy.interpolate.griddata(x,vy)]