Interpolating matrix data to mesh

Ivan,

The error you get here is part of the class definition. The variables are self._x and self._y.

values[:] = griddata((self._x,self._y),self._f[:,self._t],(x[0],x[1]))

The problem with griddata is it returns an array of interpolated values at least from my understanding and you would be calling a whole new interpolation for every point in your data. I realize now that what my previous code also suffers from this:

class ExpressionFromScipyFunction(UserExpression):
    def __init__(self, x, y, f, t, **kwargs):
        self._f = f
        self._x = x
        self._y = y
        self._t = t
        UserExpression.__init__(self, **kwargs)
    def eval(self, values, x):
        values[:] = bisplev(x[0], x[1], self.interp, dx = 0, dy = 0)
    def UpdateInterp(self):
        self.interp = bisplrep(self._x, self._y, self._f[:,self._t])
    def values_shape(self):
        return (1,)

Consider trying this, where you call UpdateInterp after instantiating the class.

expression = ExpressionFromScipyFunction(x, y, T, 0, element=V.ufl_element())
expression.UpdateInterp()
expression_out = interpolate(expression, V)
expression._t = 1 # Next Time Step

For me this speeds up the interpolation incredibly since it’s not remaking the interpolation everytime. I can’t speak for griddata but consider trying this approach.

1 Like