Interpolation¶
This module calculates the basis function weights for values and derivatives for various types of interpolants.
The interpolants include:
- Lagrange (1D) interpolants:
- linear (‘L1’), quadratic (‘L2’), cubic (‘L3’), quartic (‘L4’)
- Hermite (1D) interpolator:
- cubic-Hermite (‘H3’)
- Triangular (2D) interpolator:
- bilinear (‘T11’), biquadratic (‘T22’), bicubic (‘T33), biquartic (‘T44’)
morphic.interpolator.weights() is the main function used for
calculating weights for the values or derivatives for an interpolant.
Multiple interpolants can be combined to create higher
order interpolants.
Examples
The basic usage of morphic.interpolator.weights() requires a
list of interpolants (basis) and a list of point (X) for which
the weights are calculated.
To calculate the weights for a one-dimensional linear lagrange interpolant:
X = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
W = weights(['L1'], X) # values
dWdx = weights(['L1'], X, deriv=[1]) # dW/dx
dWdxdx = weights(['L1'], X, deriv=[2]) # dW/dxdx
Warning
The second derivative for some interpolants may not be implemented.
Similarly, for a cubic-Hermite interpolant:
W = weights(['H3'], X) # values
W = weights(['H3'], X, deriv=[1]) # dW/dx
For two-dimensional interpolants we create a 2D list of points. The
easiest way to create a 2D regular grid of points is to use
numpy.meshgrid()
import numpy x = numpy.arange(0, 1.001, 0.2) y = numpy.arange(0, 1.001, 0.2) X, Y = numpy.meshgrid(x, y) XY = numpy.array([X.reshape(X.size), Y.reshape(Y.size)]).T
Then to calculate the weights for a cubic-quadratic element:
W = weights(['L3', 'L2'], XY) # values
dWdx = weights(['L3', 'L2'], XY, deriv=[1, 0]) # dW/dx
dWdy = weights(['L3', 'L2'], XY, deriv=[0, 1])) # dW/dy
dWdxdy = weights(['L3', 'L2'], XY, deriv=[1, 1])) # dW/dxdy
For the two-dimensional triangular interpolants, the range of values in
the points list (X) must satisfy the follow to stay in the bounds of the
element, 0 <= x <= 1, 0<=y<=1, and x + y <= 1. We can filter
the list of points (X) generated above to satify these constrains by:
XY_tri = numpy.array([xy for xy in XY if xy[0] + xy[1] <= 1.])
Then we can calculate the values and derivatives for a triangular interpolant, in this case, the bi-quartic triangle:
W = weights(['T44'], XY_tri) # values
dWdx = weights(['T44'], XY_tri, deriv=[[1, 0]]) # dW/dx
dWdy = weights(['T44'], XY_tri, deriv=[[0, 1]])) # dW/dy
Interpolants can be combined to create higher-order elements. For example,:
W = weights(['L1', 'L1', 'L3'], X) # bilinear-cubic
W = weights(['H3', 'H3', 'L2'], X) # bicubic-Hermite-quadratic
W = weights(['T11', 'L1'], X) # a prism **Needs testing**
W = weights(['T11', 'L1', 'H3', 'L4', 'T44'], X) # something crazy
Warning
Higher order (>2) elements need to be implemented.
Note
To develop and add more intepolants see the developers section (ToDo).
Calculating Weights¶
-
morphic.interpolator.weights(basis, X, deriv=None)¶ Calculates the interpolant value or derivative weights for points X.
Parameters: - basis (list of strings) – interpolation function in each direction, eg,
['L1', 'L1']for bilinear. - X (list or numpy array (npoints, ndims)) – locations to calculate interpolant weights
- deriv (list of integers) – derivative in each dimension, e.g.,
deriv=[1, 1]
Returns: basis weights (ndims)
Return type: numpy array, size: (npoints, nweights)
>>> import numpy >>> x = numpy.array([[0.13, 0.23], [0.77, 0.06]]) >>> weights(['L1', 'L2'], x, deriv=[0, 1]) array([[-1.8096, -0.2704, 1.8792, 0.2808, -0.0696, -0.0104], [-0.6348, -2.1252, 0.8096, 2.7104, -0.1748, -0.5852]])
- basis (list of strings) – interpolation function in each direction, eg,
Lagrange (1D)¶
Linear Lagrange¶
-
morphic.interpolator.L1(x)¶ Linear lagrange basis function.
Parameters: x (numpy array (npoints)) – points to interpolate Returns: basis weights Return type: numpy array (npoints, 2)
-
morphic.interpolator.L1d1(x)¶ First derivative for the linear lagrange basis function.
Parameters: x (numpy array (npoints)) – points to interpolate Returns: basis weights Return type: numpy array (npoints, 2)
-
morphic.interpolator.L1d1d1(x)¶ Second derivative for the linear lagrange basis function.
Parameters: x (numpy array (npoints)) – points to interpolate Returns: basis weights Return type: numpy array (npoints, 2)
Quadratic Lagrange¶
-
morphic.interpolator.L2(x)¶ Quadratic lagrange basis function.
Parameters: x (numpy array (npoints)) – points to interpolate Returns: basis weights Return type: numpy array(npoints, 3)
-
morphic.interpolator.L2d1(x)¶ First derivative of the quadratic lagrange basis function.
Parameters: x (numpy array (npoints)) – points to interpolate Returns: basis weights Return type: numpy array(npoints, 3)
Cubic Lagrange¶
-
morphic.interpolator.L3(x)¶ Cubic lagrange basis function.
Parameters: x (numpy array (npoints)) – points to interpolate Returns: basis weights Return type: numpy array(npoints, 4)
-
morphic.interpolator.L3d1(x)¶ First derivative of the cubic lagrange basis function.
Parameters: x (numpy array (npoints)) – points to interpolate Returns: basis weights Return type: numpy array(npoints, 4)
Quartic Lagrange¶
-
morphic.interpolator.L4(x)¶ Quartic lagrange basis function.
Parameters: x (numpy array (npoints)) – points to interpolate Returns: basis weights Return type: numpy array(npoints, 5)
-
morphic.interpolator.L4d1(x)¶ First derivative of the quartic lagrange basis function.
Parameters: x (numpy array (npoints)) – points to interpolate Returns: basis weights Return type: numpy array(npoints, 5)
Hermite (1D)¶
Cubic-Hermite¶
-
morphic.interpolator.H3(x)¶ Cubic-Hermite basis function.
Parameters: x (numpy array (npoints)) – points to interpolate Returns: basis weights Return type: numpy array(npoints, 4)
-
morphic.interpolator.H3d1(x)¶ First derivative of the cubic-Hermite basis function.
Parameters: x (numpy array (npoints)) – points to interpolate Returns: basis weights Return type: numpy array(npoints, 4)
Triangular (2D)¶
Linear Triangle¶
-
morphic.interpolator.T11(x)¶ Linear lagrange triangle element.
Parameters: x (numpy array (npoints)) – points to interpolate 0<=x<=1, x1+x2<=1 Returns: basis weights Return type: numpy array(npoints, 3)
Quadratic Triangle¶
-
morphic.interpolator.T22(x)¶ Quadratic lagrange triangle element.
Parameters: x (numpy array (npoints, 2)) – points to interpolate 0<=x<=1, x1+x2<=1 Returns: basis weights Return type: numpy array(npoints, 6)
Cubic Triangle¶
-
morphic.interpolator.T33(x)¶ Cubic lagrange triangle element.
Parameters: x (numpy array (npoints, 2)) – points to interpolate 0<=x<=1, x1+x2<=1 Returns: basis weights Return type: numpy array(npoints, 10)
-
morphic.interpolator.T33d1(x)¶ First derivative in dimension 1 for the cubic lagrange triangle element.
Parameters: x (numpy array(npoints, 2)) – points to interpolate 0<=x<=1, x1+x2<=1 Returns: basis weights Return type: numpy array(npoints, 10)
-
morphic.interpolator.T33d2(x)¶ First derivative in dimension 2 for the cubic lagrange triangle element.
Parameters: x (numpy array(npoints, 2)) – points to interpolate 0<=x<=1, x1+x2<=1 Returns: basis weights Return type: numpy array(npoints, 10)
Quartic Triangle¶
-
morphic.interpolator.T44(x)¶ Quartic lagrange triangle element.
Parameters: x (numpy array(npoints, 2)) – points to interpolate 0<=x<=1, x1+x2<=1 Returns: basis weights Return type: numpy array(npoints, 15)
-
morphic.interpolator.T44d1(x)¶ First derivative in dimension 1 for the quartic lagrange triangle element.
Parameters: x (numpy array(npoints, 2)) – points to interpolate 0<=x<=1, x1+x2<=1 Returns: basis weights Return type: numpy array(npoints, 15)
-
morphic.interpolator.T44d2(x)¶ First derivative in dimension 2 for the quartic lagrange triangle element.
Parameters: x (numpy array(npoints, 2)) – points to interpolate 0<=x1<=1, 0<=x2<=1, x1+x2<=1 Returns: basis weights Return type: numpy array(npoints, 15)