import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Nx = 8
Ny = 6
x = np.linspace(0,10,Nx)
y = np.linspace(0,5, Ny)
f = np.zeros((Nx,Ny))
for i in range(Nx):
for j in range(Ny):
f[i,j] = np.sin(x[i])*np.cos(y[j])
def LI_1D(x, f, xi, Lgeneric=True):
if Lgeneric:
ilo = int(xi/(x[1]-x[0]))
if ilo < 0:
ilo = 0
elif ilo >= len(x)-1:
ilo = len(x)-2
ihi = ilo + 1
return f[ilo] + (xi - x[ilo]) * (f[ihi]-f[ilo])/(x[ihi]-x[ilo])
else:
return f[0] + (xi - x[0]) * (f[1]-f[0])/(x[1]-x[0])
def LI_2D(x,y, f, xP, yP):
dx = x[1]-x[0]
ilo = int(xP/dx)
if ilo < 0:
ilo = 0
elif ilo >= len(x)-1:
ilo = len(x)-2
ihi = ilo + 1
#---------- interpolate grid to yP
flo = LI_1D(y, f[ilo,:], yP) # f at yP, x[ilo]
fhi = LI_1D(y, f[ihi,:], yP) # f at yP, x[ihi]
#---------- interpolate final x direction
return LI_1D( np.array([x[ilo], x[ihi]]), np.array([flo, fhi]), xP, False)
def LI_3D(x,y,z, f, xP, yP, zP):
dx = x[1]-x[0]
ilo = int(xP/dx)
if ilo < 0:
ilo = 0
elif ilo >= len(x)-1:
ilo = len(x)-2
ihi = ilo + 1
#---------- interpolate grid to yP, zP
flo = LI_2D(y,z, f[ilo,:,:], yP,zP) # f at yP,zP, x[ilo]
fhi = LI_2D(y,z, f[ihi,:,:], yP,zP) # f at yP,zP, x[ihi]
#---------- interpolate final x direction
return LI_1D( np.array([x[ilo], x[ihi]]), np.array([flo, fhi]), xP, False)
def LI_4D(x,y,z,w, f, xP, yP, zP, wP):
dx = x[1]-x[0]
ilo = int(xP/dx)
if ilo < 0:
ilo = 0
elif ilo >= len(x)-1:
ilo = len(x)-2
ihi = ilo + 1
#---------- interpolate grid to yP, zP
flo = LI_3D(y,z,w, f[ilo,:,:,:], yP,zP,wP) # f at yP,zP,wP, x[ilo]
fhi = LI_3D(y,z,w, f[ihi,:,:,:], yP,zP,wP) # f at yP,zP,wP x[ihi]
#---------- interpolate final x direction
return LI_1D( np.array([x[ilo], x[ihi]]), np.array([flo, fhi]), xP, False)
def LI_5D(x,y,z,w,a, f, xP, yP, zP, wP, aP):
dx = x[1]-x[0]
ilo = int(xP/dx)
if ilo < 0:
ilo = 0
elif ilo >= len(x)-1:
ilo = len(x)-2
ihi = ilo + 1
#---------- interpolate grid to yP, zP
flo = LI_4D(y,z,w,a, f[ilo,:,:,:,:], yP,zP,wP,aP) # f at yP,zP,wP,aP x[ilo]
fhi = LI_4D(y,z,w,a, f[ihi,:,:,:,:], yP,zP,wP,aP) # f at yP,zP,wP,aP x[ihi]
#---------- interpolate final x direction
return LI_1D( np.array([x[ilo], x[ihi]]), np.array([flo, fhi]), xP, False)