porepy.utils.interpolation_tables module

The module contains interpolation tables, intended for use in function evaluations. Specifically, the motivation is to facilitate the parametrization framework described in

Operator-based linearization approach for modeling of multiphase multi-component flow in porous media by Denis Voskov (JCP 2017)

The module contains two classes:

InterpolationTable: Interpolation based on pre-computation of function values.

Essentially this is a cumbersome implementation of scipy interpolation functionality, and the latter is likely to be preferred.

AdaptiveInterpolationTable: Interpolation where function values are computed

and stored on demand. Can give significant computational speedup in cases where function evaluations are costly and only part of the parameter space is accessed during simulation.

Both classes use piecewise linear interpolation of functions, and piecewise constant approximations of derivatives.

class AdaptiveInterpolationTable(dx, base_point=None, function=None, dim=1)[source]

Bases: InterpolationTable

Interpolation table based on adaptive computation of function values.

Function values are interpolated on a Cartesian mesh. The interpolation is done using piecewise linears (for function values) and constants (for derivatives). The domain of interpolation is an Nd box.

The function values are computed on demand, then stored. This can give substantial computational savings in cases where only a part of the parameter space is accessed, and the computation of function values is costly.

Parameters
  • dx (np.ndarray) – Grid resolution in each direction.

  • base_point (Optional[np.ndarray]) – A point in the underlying grid. Used to fix the location of the grid lines.

  • function (Callable) – Function to represent in the table. Should be vectorized (if necessary with a for-loop wrapper) so that multiple coordinates can be evaluated at the same time. If not provided, table values must be assigned via the method ‘assign_values’.

  • dim (int) – Dimension of the range of the function. Values above one have not been much tested, use with care.

assign_values(val, coord, indices=None)[source]

Assign externally computed values to the table.

The user is responsible that the coordinates are nodes in the Cartesian grid underlying this interpolation tabel.

This method can be used, e.g., if the table values are calculated by an external function which it is not desirable to pass to the interpolation table. One use case is when the function is an external library (e.g., a thermodynamic flash calculation) which computes several properties at the same time. In this case it is better to collect all properties externally and pass it to the respective interpolation tables for the individual properties.

If the indices in the underlying Cartesian grid corresponding to the coordinate points are known (e.g., the points were found by the method quadrature_points_from_coordinates), it is strongly recommended that these are also provided in assignment.

Parameters
  • val (ndarray) – Values to assign.

  • coord (ndarray) – Coordinates of points to assign values to.

  • indices (Optional[ndarray]) – Indices of the coordinates in the underlying Grid.

Return type

None

gradient(x, axis)[source]

Perform differentiation on a Cartesian grid by a piecewise constant approximation.

Parameters
  • x (np.ndarray) – Points to evaluate the function. Size dimension of parameter space times number of points.

  • axis (int) – Axis to differentiate along.

Returns

Function values.

Return type

np.ndarray

interpolate(x)[source]

Perform interpolation on a Cartesian grid by a piecewise linear approximation.

If the table has a function, function values in the quadrature points will be computed as needed. If the values are computed externally and fed through the method assign_values(), the user is responsible that all relevant quadrature points have been assigned values.

Parameters

x (np.ndarray) – Points to evaluate the function. Size dimension of parameter space times number of points.

Returns

Function values.

Return type

np.ndarray

quadrature_points_from_coordinates(x, remove_known_points=True)[source]

Obtain the coordinates of quadrature points that are necessary to evalute the table in specified points.

If the table values are calculated by an external computation (i.e., not through self._function), this method can be used to determine which evaluations are needed before interpolation is invoked.

The method also returns indices of the quadrature points in the Cartesian grid underlying the interpolation table. If the quadrature points are used to compute function values which later are fed to this interpolation table using the method ‘assign_values()’, it is strongly recommended that the indices are also passed to the assignment function.

Parameters
  • x (ndarray) – Coordinates of evaluation points.

  • remove_known_points (bool) – If True, only quadrature points that have not previously been assigned values in this table are returned.

Returns

Coordinates of the quadrature points. Indices of the quadrature points in the underlying Cartesian grid.

Return type

tuple[numpy.ndarray, numpy.ndarray]

class InterpolationTable(low, high, npt, function, dim=1)[source]

Bases: object

Interpolation table based on pre-computation of function values.

Function values are interpolated on a Cartesian mesh. The interpolation is done using piecewise linears (for function values) and constants (for derivatives). The domain of interpolation is an Nd box.

The implementation may not be efficient, consider using functions from scipy.interpolate instead.

Parameters
  • low (np.ndarray) – Minimum values of the domain boundary per dimension.

  • high (np.ndarray) – Maximum values of the domain boundary per dimension.

  • npt (np.ndarray) – Number of quadrature points (including endpoints of intervals) per dimension.

  • function (Callable[[np.ndarray], np.ndarray]) – Function to represent in the table. Should be vectorized (if necessary with a for-loop wrapper) so that multiple coordinates can be evaluated at the same time.

  • dim (int) – Dimension of the range of the function.

gradient(x, axis)[source]

Perform differentiation on a Cartesian grid by a piecewise constant approximation.

Parameters
  • x (np.ndarray) – Points to evaluate the function. Size dimension of parameter space times number of points.

  • axis (int) – Axis to differentiate along.

Returns

Function values.

Return type

np.ndarray

interpolate(x)[source]

Perform interpolation on a Cartesian grid by a piecewise linear approximation.

Parameters

x (np.ndarray) – Points to evaluate the function. Size dimension of parameter space times number of points.

Returns

Function values.

Return type

np.ndarray