This thorn provides the CCTK_InterpLocalUniform() API for processor-local interpolation of N-dimensional data arrays. The data arrays (in general there may be many of them) must be defined on a uniformly spaced grid.
This thorn provides several variants of Lagrange and Hermite interpolation. At present the Lagrange interpolation operators support orders 1 through 6 for 1-D interpolation, and 1 through 4 for 2-D and 3-D interpolation. The Hermite interpolation operators support orders 2, 3, and 4 for 1-D interpolation, and 2 and 3 for 2-D and 3-D interpolation.
This thorn supports a number of interpolation options, including parameter-table entries to control the handling of grid boundaries and out-of-range points, non-contiguous input arrays, taking derivatives as part of the interpolation, and querying the Jacobian of the interpolation operation.
This interpolator provides:
Lagrange polynomial interpolation of orders 1–6 for 1-D interpolation and 1–4 for 2-D and 3-D interpolation. This interpolator actually provides two slightly different flavors of Lagrange interpolation, tensor product and maximum degree, registered under the operator names
"Lagrange polynomial interpolation (tensor product)" "Lagrange polynomial interpolation (maximum degree)"
Section 2.5 of this thorn guide explains the two variants in detail, but for most purposes the tensor-product variant is preferable. For convenience this is also registered under the operator name
"Lagrange polynomial interpolation"
and this is probably the best interpolation operator for general-purpose use.1
Hermite polynomial interpolation of orders 2-4 for 1-D interpolation, and 2–3 for 2-D and 3-D interpolation, registered under the operator name
"Hermite polynomial interpolation"
The code allows arbitrarily-shaped interpolation molecules, but at the moment only hypercube-shaped molecules are implemented. It would be easy to add additional orders and/or dimensions if desired.
This interpolator supports a number of options specified by a parameter table (a Cactus key-value table, manipulated by the Util_Table*() APIs). Note that all the interpolator options described here apply only to the current interpolator call: there is no visible state kept inside the interpolator from one call to another.
This interpolator was written by Jonathan Thornburg in winter 2001–2002. Between then and July 2003 it lived in the LocalInterp thorn in the CactusBase arrangement, but in July 2003 it was moved to the (new) AEILocalInterp thorn in the AEIThorns arrangement so it could stay GPL (Cactus policies forbid GPL code in the CactusBase arrangement).
Within Cactus, each interpolator has a character-string name; this is mapped to a Cactus interpolator handle by CCTK_InterpHandle(). For any given interpolator handle, there may be a separate interpolator defined for each of the interpolation APIs (both the processor-local ones provided by this thorn, and the global ones provided by driver-specific interpolator thorns such as PUGHInterp).
Terminology for interpolation seems to differ a bit from one author to another. Here we refer to the point-centered interpolation of a set of input arrays (defining data on a uniformly or nonuniformly spaced grid of data points) to a set of interpolation points (specified by a corresponding set of interpolation coordinates), with the results being stored in a corresponding set of output arrays. Alternatively, we may refer to cell-centered interpolation, using a grid of data cells and a set of interpolation cells. (This latter terminology is common in hydrodynamics interpolators.)
At present all the interpolators do polynomial interpolation, and allow the interpolation of multiple input arrays (to the same set of interpolation points) in a single interpolator call, using the basic algorithm:
for each interpolation point { choose an interpolation molecule position somewhere near the interpolation point for each output array { compute an interpolating polynomial which approximates the input data at the molecule points output = polynomial(interpolation point) } }
In the future, we may add other interpolators where the choice of molecule is data-dependent (and thus may vary from one input array to the next), and/or where the entire input grid is used in each interpolation.
We define the order of the interpolation to be the order of the fitted polynomial. That is, in our terminology linear interpolation is order 1, quadratic is order 2, cubic is order 3, etc. An order \(n\) interpolator thus has \(O\big ((\Delta x)^{n+1})\big )\) interpolation errors for generic smooth input data. Section 2.5 explains how the interpolating polynomial is defined for multi-dimensional interpolation.
Because the interpolating polynomial generally changes if the interpolation point moves from one grid cell to another, unless something special is done the interpolating function isn’t smooth, i.e. its 1st derivative is generically discontinuous, with \(O(\Delta x^n)\) jump discontinuities each time the interpolating polynomial changes. Correspondingly, the interpolation error is generically a “bump function” which is zero at each grid point and rises to a local maximum in each grid cell.2 This is the case, for example, for tensor-product Lagrange polynomial interpolation.
[For maximum-degree Lagrange polynomial interpolation, the interpolation error may not be zero at the grid points, and the interpolant itself may not be continuous there. Section 2.5 explains this in detail.]
This thorn also provides Hermite polynomial interpolation, which guarantees a smooth (\(C^1\)) interpolant and (for smooth input data) interpolation error. Unfortunately, this comes at the cost of a larger molecule size than the same-order Lagrange interplator, and a much larger interpolation error if the interpolation molecules are off-centered. (By default, the Hermite interpolator doesn’t allow off-centered molecules, though this can be changed via the boundary_off_centering_tolerance[] and excision_off_centering_tolerance[] parameter-table entries.)
For most purposes Lagrange polynomial interpolation is better; only use Hermite polynomial interpolation if you need a smooth interpolant.
As described in the Function Reference section of the Cactus User’s Guide, interp_coords, input_arrays, and output_arrays are actually all pointers to arrays of void * pointers, since we support a number of different Cactus data types. Internally, the interpolator casts these void * pointers to CCTK_REAL * or whatever the correct Cactus data types are. But when describing how the interpolator accesses the various arrays, for simplicity we usually gloss over this casting, i.e. we pretend that interp_coords, input_arrays, and output_arrays are pointers to arrays of CCTK_REAL * pointers. (This may become clearer once you read the next example.)
We use pt, in, and out as generic 0-origin integer subscripts into the arrays of interpolation points, input arrays, and output arrays respectively. We use (i,j,k) as a generic N_dims-vector of integer subscripts into the input array input_arrays[in]. (Thus (i,j,k) space refers to the grid of data points.) We usually only write array subscripting expressions for the 3-D case; the restrictions/generalizations to other dimensions should be obvious.
For example, for 3-D interpolation, the (x,y,z) coordinates of a typical interpolation point are given by
x = interp_coords[0][pt] y = interp_coords[1][pt] z = interp_coords[2][pt]
(Notice here that as described above, we’ve implicitly taken interp_coords to have the C type
const CCTK_REAL* interp_coords[], glossing over the casting from its actual C type of
const void* interp_coords[].)
We take axis to be a 0-origin integer specifying a coordinate axis (dimension), i.e. 0 for \(x\), 1 for \(y\), 2 for \(z\), …. We take ibndry to be a 0-origin integer specifying the combination of a coordinate axis (dimension) and a minimum/maximum “end” of the grid; these are enumerated in the order \(x_{\min }\), \(x_{\max }\), \(y_{\min }\), \(y_{\max }\), \(z_{\min }\), \(z_{\max }\), ….
When describing Jacobians and domains of dependence, it’s useful to introduce the notion of molecule position, a nominal reference point for the interpolation molecule in (i,j,k) coordinate space. (For example, the molecule position might just be the (i,j,k) coordinates of the molecule’s center.) We also introduce molecule coordinates (mi,mj,jk), which are just (i,j,k) coordinates relative to the molecule position. We use m as a generic molecule coordinate. Thus (in notation which should be obvious) a generic molecule operation can be written \begin {equation} \verb |output| = \sum _{\tt m} \verb |coeff[posn+m]| \times \verb |input[posn+m]| \end {equation} Note that there is no requirement that the output be semantically located at the position posn! (This may become clearer once you look at the example in section 2.9.2.) However, by convention we do assume that \(\verb |m|=0\) is always a valid m coordinate; this simplifies pointer arithmetic.
When describing various entries in the parameter table in, we use const qualifiers on table entries to indicate that the interpolator treats them as const variables/arrays, i.e. it promises not to change them. In contrast, table entries which are not shown as const are considered read-write by the interpolator; it typically uses them to return outputs back to the caller.
The CCTK_InterpLocalUniform() interpolation API (described in detail in the “Function Reference” appendix of the Cactus Users’ Guide) includes a parameter table (a Cactus key-value table, manipulated by the Util_Table*() APIs) whose table handle is passed to the interpolator. This can be used to specify a number of options to the interpolator; it can also be used by the interpolator to return additional information back to the caller.
The only mandatory parameter for this interpolator is the interpolation order:
const CCTK_INT order;
As noted in section 1.2, in our terminology linear interpolation is order 1, quadratic is order 2, cubic is order 3, etc.
If no grid boundaries or excised points are nearby, the interpolator centers the molecules around the interpolation point as much as possible. Table 1 gives the molecule size and details of the centering policy for each interpolation operator and order.
Molecule | |||
Interpolation Operator | Order | Size | MoleculeCentering Policy |
"Lagrange polynomial interpolation" | 1 | 2-point | |
"Lagrange polynomial interpolation" | 2 | 3-point | |
"Lagrange polynomial interpolation" | 3 | 4-point | |
"Lagrange polynomial interpolation" | 4 | 5-point | |
"Lagrange polynomial interpolation" | 5 | 6-point | |
"Lagrange polynomial interpolation" | 6 | 7-point | |
"Hermite polynomial interpolation" | 2 | 4-point | |
"Hermite polynomial interpolation" | 3 | 6-point | |
"Hermite polynomial interpolation" | 4 | 6-point | |
If an interpolation point is too near a grid boundary and/or an excised region the interpolator can either
off-center the interpolation molecule, or refuse to interpolate that point (returning a
CCTK_INTERP_ERROR_POINT_OUTSIDE3
or CCTK_INTERP_ERROR_POINT_EXCISED error code, and possibly also printing a warning message). This behavior
is controlled by the (optional) parameter-table entries
const CCTK_INT N_boundary_points_to_omit[2*N_dims] const CCTK_REAL boundary_off_centering_tolerance[2*N_dims] const CCTK_REAL boundary_extrapolation_tolerance[2*N_dims] const CCTK_REAL excision_off_centering_tolerance[2*N_dims] # not implemented yet const CCTK_REAL excision_extrapolation_tolerance[2*N_dims] # not implemented yet
The elements of these arrays are matched up with the grid boundaries in the order
\([x_{\min }, x_{\max }, y_{\min }, y_{\max }, z_{\min }, z_{\max }, \dots ]\). (For excision_*_tolerance[] the minimum/maximum are interpreted as the corresponding coordinate
decreasing/increasing when moving out of the active region of the grid into the excised region.) We use ibndry
as a generic 0-origin index into these arrays.
For any given grid boundary ibndry, the interpolator doesn’t use input data from the
N_boundary_points_to_omit[ibndry] grid points closest to the boundary. In other words, these points are
effectively omitted from the input grid. For example:
Setting this parameter to an array of all 0s means the interpolator may use data from all grid points. This is the default if this parameter isn’t specified.
Setting this parameter to an array of all 1s means the interpolator may not use input data from the outermost row/plane of grid points on any face.
More generally, this option may be useful for controlling the extent to which the interpolator uses input data from ghost and/or symmetry zones.
The (optional) parameter-table entries
const CCTK_REAL boundary_off_centering_tolerance[2*N_dims] const CCTK_REAL boundary_extrapolation_tolerance[2*N_dims] const CCTK_REAL excision_off_centering_tolerance[2*N_dims] # not implemented yet const CCTK_REAL excision_extrapolation_tolerance[2*N_dims] # not implemented yet
control the local interpolator’s willingness to off-center (change away from the default centering) its interpolation molecules near boundaries and excised points.
To describe how these parameters work, it’s useful to define two regions in the data coordinate space:
The valid-data region
is the entire grid minus any ommited-on-boundary or excised points; to make this a region we take the Lego-block bounding box of the non-excised grid points.
The default-centering region
is that region in interpolation-point space where the interpolator can use the default molecule centering (described in table 1), i.e. where the default-centering molecules require data only from the data-valid region.
Figure 1 shows an example of these regions.
Near a boundary or excised regions, the interpolator will off-center its molecules (if and) only if the interpolation point is both
within (\(\le \)) *_off_centering_tolerance[ibndry] grid spacings of the default-centering region, and
within (\(\le \)) *_extrapolation_tolerance[ibndry] grid spacings of the valid-data region.
where we use “*” to denote boundary or excision as appropriate.
There are four cases for these parameters:
\(\text {\tt *\!\_off\_centering\_tolerance[ibdnry]} = 0.0\) and \(\text {\tt *\!\_extrapolation\_tolerance[ibndry]} = 0.0\):
No off-centering is allowed: the interpolator reports an error (CCTK_ERROR_INTERP_POINT_OUTSIDE
or CCTK_ERROR_INTERP_POINT_EXCISED return code as appropriate) if any interpolation point is
outside the default-centering region.
\(\text {\tt *\!\_off\_centering\_tolerance[ibdnry]} > 0.0\) and \(\text {\tt *\!\_extrapolation\_tolerance[ibndry]} = 0.0\):
The interpolator allows interpolation points to be up to (\(\le \)) *_off_centering_tolerance[ibndry]
grid spacings outside the default-centering region in this direction. If an interpolation point is
beyond this limit in any direction, then the interpolator reports an error.
\(\text {\tt *\!\_off\_centering\_tolerance[ibdnry]} = \infty \)4 and \(\text {\tt *\!\_extrapolation\_tolerance[ibndry]} > 0.0\):
The
interpolator allows interpolation points to be up to (\(\le \)) *_extrapolation_tolerance[ibndry] grid
spacings outside the valid-data region in this direction. If an interpolation point is beyond this limit
in any direction, then the interpolator reports an error.
\(\text {\tt *\!\_off\_centering\_tolerance[ibdnry]} = 0.0\) and \(\text {\tt *\!\_extrapolation\_tolerance[ibndry]} > 0.0\):
In practice the default-centering region is always a (normally proper) subset of the valid-data region,
so this case is nonsensical: the positive value for *_extrapolation_tolerance[ibndry] has no
effect because of the \(\verb |*_off_centering_tolerance[ibndry]| = 0.0\) setting. The interpolator gives a warning for this case.
If any of these table entries aren’t specified, the defaults are
boundary_off_centering_tolerance[ibndry] = 999.0 # effectively infinity boundary_extrapolation_tolerance[ibndry] = 1.0e-10
In other words, the interpolation points may be anywhere within the valid-data region or up to \(10^{-10}\) grid spacing outside it. (The \(10^{-10}\) “fudge factor” helps to avoid spurious errors in case floating-point roundoff moves an interpolation point which was supposed to be just on the boundary of the valid-data region, slightly outside it.)
[In the near future these defaults may be changed: For Lagrange polynomial interpolation the new defaults would be
boundary_off_centering_tolerance[ibndry] = 999.0 # effectively infinity boundary_extrapolation_tolerance[ibndry] = 1.0e-10
while for Hermite polynomial interpolation they would be
boundary_off_centering_tolerance[ibndry] = 1.0e-10 boundary_extrapolation_tolerance[ibndry] = 0.0
This would leave Lagrange interpolation unchanged, while for Hermite interpolation the defaults would forbid any significant off-centering of the interpolation molecules.]
By default, AEILocalInterp prints a Cactus level 1 warning message for each interpolation point which causes it to return a CCTK_INTERP_ERROR_POINT_OUTSIDE or CCTK_INTERP_ERROR_POINT_EXCISED error code. If the key suppress_warnings is present in the parameter table (it may have any datatype and value), then these messages are not printed.
By default, an interpolator just returns a single result, either 0 for success or some (negative) error code. If there are multiple interpolation points causing errors, an interpolator should return 0 for success, or the error code for the first point in error (the one with the smallest pt).
However, sometimes you would like to know the status of the interpolation for each point. The following (optional) parameter-table entries may be used to request this, and to query if the interpolator supports this:
/* * To query whether the interpolator supports per-point status, set this * to a NULL pointer. To actually request per-point status, set this to * a non-NULL pointer pointing to a buffer of N_interp_points CCTK_INTs * into which the interpolator should store the per-point status. The status * for point pt is defined to be the result which CCTK_InterpLocalUnifom() * would return if that were the only point being interpolated. */ CCTK_POINTER per_point_status;
If the interpolator supports returning per-point status, and if the key per_point_status is present in the parameter table, then the interpolator will set
CCTK_INT error_point_status;
in the parameter table. If \(\verb |per_point_status| = \verb |NULL|\), the interpolator will set error_point_status to 0. Otherwise, the interpolator will set error_point_status to the negative of the number of points for which the per-point status is non-zero.
In multiple dimensions, there are two plausible definitions for the generic interpolating polynomial of a given order (degree) \(n\). For convenience I’ll describe these for the 2-D case, but the generalization to any number of dimensions should be obvious:
The generic “tensor product” polynomial of degree \(n\) is \begin {equation} f(x,y) = \sum _{{0 \le i \le n} \atop {0 \le j \le n}} a_{ij} x^i y^j \label {AEIThorns/AEILocalInterp/eqn-polynomial-2d-TP} \end {equation}
The generic “maximum degree” polynomial of degree \(n\) is \begin {equation} g(x,y) = \sum _{0 \le i+j \le n} a_{ij} x^i y^j \label {AEIThorns/AEILocalInterp/eqn-polynomial-2d-MD} \end {equation}
Because it has \((n+1)^2\) coefficients, the tensor-product polynomial \(f\) can (and does) pass through all the \((n+1)^2\) input data points in a square molecule of size \(n+1\). This implies that the interpolation error vanishes at the input grid points, and that the overall interpolating function is continuous (up to floating-point roundoff errors). However, \(f\) does have the slightly peculiar property of having terms up to \(x^n y^n\) despite being of “degree” \(n\). (For example, the “linear” interpolant for \(n=1\) would have \(xy\) terms, even though those are formally quadratic in the independent variables \(x\) and \(y\).)
In contrast, the maximum-degree polynomial \(g\) has only \(\frac {1}{2} (n+1)(n+2)\) coefficients, so for generic input data (i.e. input data which isn’t actually sampled from a polynomial of the form \((\ref {AEIThorns/AEILocalInterp/eqn-polynomial-2d-MD})\)) \(g\) can’t pass through all the \((n+1)^2\) input data points in a square molecule of size \(n+1\). The interpolator actually does a least-squares fit of the polymomial \(g\) to the input data in the molecule, so in general the molecule won’t pass through any of the data points! Moreover, each time the interpolation point crosses a grid square (for odd \(n\)) or the center lines of a grid square (for even \(n\)), the set of points used in the molecule changes (cf. section 2.2), so the interpolant generally has a jump discontinuity! For these reasons, the tensor-product choice \((\ref {AEIThorns/AEILocalInterp/eqn-polynomial-2d-TP})\) is generally preferable.
An interpolator may support different families/shapes of interpolation molecules. Hypercube-shaped molecules are the simplest and most common case, but one could also imagine (say) octagon-shaped molecules in 2-D, or some generalization of this in higher numbers of dimensions.
The (optional) parameter
/* set with Util_TableSetString() */ const char molecule_family[];
may be used to set or query the molecule family.
If this key is present in the parameter table, the interpolator sets the molecule family/shape based on the value specified. If this key isn’t present in the parameter table, then the interpolator sets it to the molecule family being used.
At present only hypercubed-shaped molecules are implemented; these are specified by molecule_family set to "cube".
Sometimes the input “arrays” used by the interpolator might not be contiguous in memory. For example, we might want to do 2-D interpolation within a plane of a 3-D grid array, but the plane might or might not be contiguous in memory. (Another example would be that the input arrays might be members of a compact group.)
The following (optional) parameter-table entries specify non-contiguous input arrays:
const CCTK_INT input_array_offsets[N_input_arrays]; /* the next 3 table entries are shared by all input arrays */ const CCTK_INT input_array_strides [N_dims]; const CCTK_INT input_array_min_subscripts[N_dims]; const CCTK_INT input_array_max_subscripts[N_dims];
In general, the interpolator accesses the input using the generic subscripting expression
input_array[in][offset + i*stride_i + j*stride_j + k*stride_k]
where
offset = input_array_offsets[in] (stride_i,stride_j,stride_k) = input_array_strides[]
and where (i,j,k) run from input_array_min_subscripts[] to input_array_max_subscripts[] inclusive (n.b. this is an inclusive range, i.e. \(\verb |min| \le \verb |(i,j,k)| \le \verb |max|\)).
The defaults are that each input array is contiguous in memory, i.e. input_array_offsets[] = 0, stride determined from input_array_dims[] in the usual Fortran manner, input_array_min_subscripts[] = all 0s, and input_array_max_subscripts[] = input_array_dims[]-1. If the stride and max subscript are both specified explicitly, then the explicit input_array_dims[] argument to CCTK_InterpLocalUniform() is ignored.
If we view the input data as being samples of a smooth function, then instead of estimating values of that function at the interpolation points, the interpolator can instead or additionally estimate values of various derivatives of that function at the interpolation points. (We don’t currently implement it, but one could also imagine interpolating more general molecule operations such as Laplacians.)
The following (optional) parameter-table entries are used to specify this:
const CCTK_INT operand_indices[N_output_arrays]; const CCTK_INT operation_codes[N_output_arrays];
The semantics here are that
output_array[out] = op(input_array[operand_indices[out]])
where op is an operator specified by the operation_codes[out] value as described below.
Note that operand_indices[] doesn’t directly name the inputs, but rather gives their (0-origin) subscripts in the list of inputs. This allows for a more efficient implementation in the (common) case where some of the input arrays have many different operations applied to them. (It’s most efficient to group all operations on a given input array together in the operand_indices and operation_codes arrays, as in the example in section 5.2.)
Negative operation_codes[out] values are reserved for future use. An operation_codes[out] value which is \(\ge 0\) is taken as a decimal integer encoding a coordinate partial derivative: each decimal digit means to take the coordinate partial derivative along that (1-origin) axis; the order of the digits in a number is ignored. Table 2 summarizes these resulting derivative codes.
operation_codes[out] | What it means |
0 | interpolate the input array itself (no derivative) |
1 | interpolate \(\partial \big / \partial x^1\) of the input array |
2 | interpolate \(\partial \big / \partial x^2\) of the input array |
3 | interpolate \(\partial \big / \partial x^3\) of the input array |
12 or 21 | interpolate \(\partial ^2 \big / \partial x^1 \partial x^2\) of the input array |
13 or 31 | interpolate \(\partial ^2 \big / \partial x^1 \partial x^3\) of the input array |
23 or 32 | interpolate \(\partial ^2 \big / \partial x^2 \partial x^3\) of the input array |
11 | interpolate \(\partial ^2 \big / \partial (x^1)^2\) of the input array |
22 | interpolate \(\partial ^2 \big / \partial (x^2)^2\) of the input array |
33 | interpolate \(\partial ^2 \big / \partial (x^3)^2\) of the input array |
At present we do not have any #defines for the operation codes in the Cactus header files. However, you can avoid most of the software-engineering problems of having “magic numbers” for the operation codes, by using the macro
#define DERIV(op) op
to mark all such operation_codes[] values in your code. There’s an example of this in section 5.2.
The Jacobian of the interpolator is defined as \begin {equation} \frac {\partial \hbox {\tt output\_array[out][pt]}} {\partial \hbox {\tt input\_array[in][(i,j,k)]}} \label {AEIThorns/AEILocalInterp/eqn-Jacobian} \end {equation} We may want to know the Jacobian itself, and/or “just” the set of (i,j,k) for which this is nonzero (i.e. the Jacobian’s sparsity structure, or equivalently the domain of dependence of the output result, or equivalently the interpolation molecule size and shape) for a given out, in, and pt.5
The complexity of doing this depends (strongly!) on the structure of the Jacobian, and in particular on the answers to the following questions:
What molecule family is being used?
Does the interpolation molecule size and/or shape depend on where the interpolation points are in the grid?6
If this interpolator supports computing derivatives as described in section 2.8, does the interpolation molecule size and/or shape depend on operation_codes[]?
Does the interpolation molecule size and/or shape depend on the actual floating-point values being interpolated? (Examples of this might include ENO (essentially nonoscillatory) and/or TVD (total-variation diminishing) interpolators for hydrodynamics calculations.)
Do the actual floating-point values of the Jacobian depend on the actual floating-point values being interpolated? Equivalently, is the interpolation nonlinear?
Because the different cases differ so much in the amount of information required to describe the Jacobian, it’s hard to define a single API which covers all cases without burdening the simpler cases with excessive complexity. Instead, the interpolator defines a Jacobian-structure–query API to determine which case applies, together with (conceptually) several different APIs for the different cases. (At the moment only a single case is implemented.)
The following parameter-table entries may be used to query which of the different Jacobian-structure cases applies:
The parameter molecule_family may may be used to query what molecule family is being used. This is described in detail in section 2.6.
If the interpolation molecule size and/or shape vary with the interpolation coordinates, the interpolator sets the parameter
CCTK_INT MSS_is_fn_of_interp_coords;
to 1. Otherwise (i.e. if the interpolation molecule size and shape are independent of the interpolation coordinates) it should set this parameter to 0.
If the interpolator supports computing derivatives as described in section 2.8, and if the interpolation molecule’s size and/or shape varies with operation_codes[], the interpolator sets the parameter
CCTK_INT MSS_is_fn_of_which_operation;
to 1. Otherwise (i.e. if the interpolator doesn’t support computing derivatives, or if the interpolator does support computing derivatives but the interpolation molecule size and shape are independent of the operation_code[] values), the interpolator sets this parameter to 0. Note that this query tests whether the molecule size and/or shape depend on operation_codes[] in general, independent of whether there are in fact any distinct values (or even any values at all) passed in operation_codes[] in this particular interpolator call. In other words, this is a query about the basic design of the interpolator, not about this particular call.
If the interpolation molecule’s size and/or shape varies with the actual floating-point values of the input arrays, the interpolator sets the parameter
CCTK_INT MSS_is_fn_of_input_array_values;
to 1. Otherwise (i.e. if the interpolation molecule size and shape are independent of the input array values; this is a necessary, but not sufficient, condition for the interpolation to be linear), the interpolator sets this parameter to 0.
If the actual floating-point values of the Jacobian \((\ref {AEIThorns/AEILocalInterp/eqn-Jacobian})\) (for a given out, in, and pt) depend on the actual floating-point values of the input arrays (i.e. if the interpolation is nonlinear), the interpolator sets the parameter
CCTK_INT Jacobian_is_fn_of_input_array_values;
to 1. Otherwise (i.e. if the interpolation is linear) the interpolator sets this parameter to 0.
The current implementation always sets
MSS_is_fn_of_interp_coords = 0 MSS_is_fn_of_which_operation = 0 MSS_is_fn_of_input_array_values = 0 Jacobian_is_fn_of_input_array_values = 0
The simplest case (and the only one for which we have defined an API at present) is when the molecules are hypercube-shaped and of (typically small) fixed size, independent of the interpolation coordinates and the actual floating-point values in the input arrays (though presumably depending on the interpolation order and on operation_code). In other words, this case applies if (and only if) the Jacobian structure information described in section 2.9.1 returns
MSS_is_fn_of_interp_coords = 0 MSS_is_fn_of_which_operation = 0 MSS_is_fn_of_input_array_values = 0 Jacobian_is_fn_of_input_array_values = 0
(These are precisely the values set by the current implementation.) In the rest of this section we describe the query API for this case.
The following parameters may be used to query the molecule size:
CCTK_INT const molecule_min_m[N_dims]; CCTK_INT const molecule_max_m[N_dims];
The semantics of these are that if both of these keys are present (the values and datatypes don’t matter), then the interpolator will (re)set the values to give the (inclusive) minimum and maximum m molecule coordinates. (Note that either both of these keys should be present, or neither should be present. This simplifies the logic in the interpolator slightly.)
The following parameter may be used to query the molecule positions:
CCTK_INT* const molecule_positions[N_dims];
The semantics of this is that the caller should set molecule_positions[] to an array of N_dims pointers to (caller-supplied) arrays of N_interp_points CCTK_INTs each. If this key exists, then the interpolator will store the molecule positions in the pointed-to arrays.
The following parameters may be used to query the Jacobian \((\ref {AEIThorns/AEILocalInterp/eqn-Jacobian})\) itself:
CCTK_REAL* const Jacobian_pointer[N_output_arrays]; const CCTK_INT Jacobian_offset [N_output_arrays]; # optional, default=all 0 /* the next 3 table entries are shared by all Jacobians */ const CCTK_INT Jacobian_interp_point_stride; const CCTK_INT Jacobian_m_strides[N_dims]; const CCTK_INT Jacobian_part_stride; # optional, default=1
If Jacobian_pointer is present in the table, then Jacobian_interp_point_stride and Jacobian_m_strides must also be present. For each out where Jacobian_pointer[out] != NULL,7 the interpolator would then store the Jacobian \((\ref {AEIThorns/AEILocalInterp/eqn-Jacobian})\) in
Jacobian_pointer[out][offset + pt*Jacobian_interp_point_stride + mi*stride_i + mj*stride_j + mk*stride_k + part*Jacobian_part_stride]
where
offset = Jacobian_offset[out] (stride_i,stride_j,stride_k) = Jacobian_m_strides[]
and where part is 0 for real values and the real parts of complex values, and 1 for the imaginary parts of complex values.
By appropriately setting the various stride parameters, this allows a fairly wide variety of possible storage layouts for the Jacobian.
An example may help to clarify this: Suppose we have a 1-D grid with 11 grid points, with integer subscripts 0 through 10 inclusive, and interpolation coordinates given by coord_origin = 0.0 and coord_delta = 0.1. Suppose further that we’re doing Lagrange polynomial interpolation, with order = 2 and hence (by table 1) using 3-point molecules. Finally, suppose that we query the Jacobian molecule positions for the N_interp_points=14 interpolation points 0.0, 0.04, 0.06, 0.10, 0.14, 0.16, 0.20, 0.80, 0.84, 0.86, 0.90, 0.94, 0.96, 1.00. Then the queries might return
interp_molecule_min_m = -1 interp_molecule_max_m = +1 /* interp_x molecule */ interp_molecule_positions[0][ 0] = 1 /* 0.00 [0.0, 0.1, 0.2] */ interp_molecule_positions[0][ 1] = 1 /* 0.04 [0.0, 0.1, 0.2] */ interp_molecule_positions[0][ 2] = 1 /* 0.06 [0.0, 0.1, 0.2] */ interp_molecule_positions[0][ 3] = 1 /* 0.10 [0.0, 0.1, 0.2] */ interp_molecule_positions[0][ 4] = 1 /* 0.14 [0.0, 0.1, 0.2] */ interp_molecule_positions[0][ 5] = 2 /* 0.16 [0.1, 0.2, 0.3] */ interp_molecule_positions[0][ 6] = 2 /* 0.20 [0.1, 0.2, 0.3] */ interp_molecule_positions[0][ 7] = 8 /* 0.80 [0.7, 0.8, 0.9] */ interp_molecule_positions[0][ 8] = 8 /* 0.84 [0.7, 0.8, 0.9] */ interp_molecule_positions[0][ 9] = 9 /* 0.86 [0.8, 0.9, 1.0] */ interp_molecule_positions[0][10] = 9 /* 0.90 [0.8, 0.9, 1.0] */ interp_molecule_positions[0][11] = 9 /* 0.94 [0.8, 0.9, 1.0] */ interp_molecule_positions[0][12] = 9 /* 0.96 [0.8, 0.9, 1.0] */ interp_molecule_positions[0][13] = 9 /* 1.00 [0.8, 0.9, 1.0] */
The way the generalized polynomial interpolator is implemented it’s easy to also do Savitzky-Golay smoothing.8 This is best described by way of an example: Suppose we’re doing 1-D cubic interpolation, using (by table 1) 4-point molecules. In other words, at each interpolation point we use a cubic interpolation polynomial fitted to 4 surrounding data points. For Savitzky-Golay smoothing, we would instead least-squares fit a cubic polynomial to some larger number of surrounding data points. This combines interpolation with smoothing, so there’s less amplification of noise in the input data in the interpolation outputs.
The optional input
const CCTK_INT smoothing;
specifies how much (how many points) to enlarge the interpolation molecule for this. The default is 0 (no smoothing). 1 would mean to enlarge the molecule by 1 point (e.g. to use a 5-point molecule instead of the usual 4-point one for cubic interpolation). 2 would mean to enlarge by 2 points, (e.g. to use a 6-point molecule for cubic interpolation). Etc etc.
Note that in \(>1\) dimension, the maximum-degree Lagrange interpolation already uses more data points than the number of free parameters in the interpolating polynomials, i.e. it already does some Savitzky-Golay smoothing. For example, in 2-D a generic cubic polynomial \(f(x,y) = \sum _{i+j \le 3} c_{ij} x^i y^j\) has 10 free parameters \(c_{ij}\), which we least-squares fit to the 16 data points in the \(4 \times 4\) molecule.
Savitzky-Golay smoothing is basically free apart from the increase in the molecule size, e.g. a smoothing=2 cubic interpolation has exactly the same cost as any other 6-point–molecule interpolation.
The current implementation has all the framework for smoothing, but only the smoothing=0 case is implemented at the moment.
Setting the optional parameter
const CCTK_INT debug;
to a positive value turns on various debug printing inside the interpolator. This is intended for debugging the interpolator itself; you need to look at the interpolator source code to interpret the output.
This thorn has a Boolean parameter log_interp_coords. If this is set to true, this thorn will write a detailed log file giving the grid information and interpolation coordinates for each interpolator call. Each processor will write a separate log file, with the file name determined via
snprintf(buffer, length, "AEILocalInterp.proc%d.log" CCTK_MyProc(NULL))
Each file begins with a header comment describing the file format in detail. After the header comment, there is one line in the file for each call on CCTK_InterpLocalUniform() which is handled by this thorn (with log_interp_coords set to true).
Note that these log files are typically quite large, and writing them may significantly slow the simulation – you should probably only use this option for debugging purposes.
Sometimes you may want to call the interpolator, but not actually do any interpolation for some or all of the arrays:
you may be doing a query
you may be just checking values in the parameter table
you may want to interpolate varying subsets of some set of arrays
you may have no interpolation points, yet still want to do the interpolator call for other reasons
In such cases there are several possible ways you can supress some or all of the interpolations:
Set \(\text {\tt N\_interp\_points} = 0\):
This supresses all interpolation. In this case interp_coords may also be NULL if desired.
Set \(\text {\tt N\_input\_arrays} = 0\) and \(\text {\tt N\_output\_arrays} = 0\):
This suppresses all interpolation. However, note that some parameter-table entries like
operand_indices and operation_codes are specified as being arrays whose size depends on
N_output_arrays, and it’s an error for these table entries to be present with the wrong size (in
this case, with any nonzero size).
Set \(\text {\tt output\_arrays[out]} = \text {\tt NULL}\):
This supresses the interpolation for this particular output array. This is probably the cleanest way
of selectively turning individual arrays on and off. If \(\verb |output_arrays[out]| = \verb |NULL|\) and the input arrays aren’t needed for any
queries you’re doing, then it’s useful to also set the corresponding \(\verb |input_arrays[in]| = \verb |NULL|\), to supress the interpolator
fetching data (which in this case will never be used) from the input arrays.
Note that the combination \(\verb |input_arrays[in]| = \verb |NULL|\) and \(\verb |output_arrays[out]| \ne \verb |NULL|\) is an error: conceptually you’re asking for an interpolation (the output array pointer is non-NULL) but not supplying any input data (the input array pointer is NULL).
This interpolator’s basic design is to use separate specialized code for each combination of
(N_dims, molecule_family, order, smoothing)
i.e. in practice for each distinct choice of interpolation molecule. Maple is used to generate all the interpolation coefficients. The C preprocessor is then used to generate all the specialized code from a single master “template” file. The template code uses #ifdefs to handle lower dimensions with no extra overhead, e.g. 1-D/2-D interpolation is basically just as efficient as in a purpose-built 1-D/2-D interpolator, with no extra overhead imposed by the interpolator also supporting higher-dimensional interpolation.
The Maple code which generates the interpolation coefficients is quite general-purpose, and can handle an arbitrary dimensionality and molecule size/shape. Generating new coefficients can be rather time-consuming, though, e.g. the current coefficients for 3-D for orders 1-4 take about 8 cpu minutes to generate using Maple 7 on a 1.7 GHz P4.
Note that when compiling the code in the directory src/GeneralizedPolynomial-Uniform of this thorn, you may get compiler warnings about casts discarding const qualifiers from pointers in (the #include-ed file) template.c. Don’t worry – the code is actually ok.9 You may also get compiler warnings about unused variables in this same file; again don’t worry, the code is ok.
See the README file in the source code directory AEILocalInterp/src/ and in its subdirectories for further details on the implementation.
Here’s a simple example in C, of interpolating a CCTK_REAL and a CCTK_COMPLEX \(10 \times 20\) 2-D array, at 5 interpolation points, using cubic interpolation.
Note: Since C allows arrays to be initialized only if the initializer values are compile-time constants, we have to declare the interp_coords[], input_arrays[], and output_arrays[] arrays as non-const, and set their values with ordinary (run-time) assignment statements. In C++, there’s no restriction on initializer values, so we could declare the arrays const and initialize them as part of their declarations.
#define N_DIMS 2 #define N_INTERP_POINTS 5 #define N_INPUT_ARRAYS 2 #define N_OUTPUT_ARRAYS 2 /* (x,y) coordinates of data grid points */ #define X_ORIGIN ... #define X_DELTA ... #define Y_ORIGIN ... #define Y_DELTA ... const CCTK_REAL origin[N_DIMS] = { X_ORIGIN, Y_ORIGIN }; const CCTK_REAL delta [N_DIMS] = { X_DELTA, Y_DELTA }; /* (x,y) coordinates of interpolation points */ const CCTK_REAL interp_x[N_INTERP_POINTS]; const CCTK_REAL interp_y[N_INTERP_POINTS]; const void* interp_coords[N_DIMS]; /* see note above */ /* input arrays */ /* ... note Cactus uses Fortran storage ordering, i.e. X is contiguous */ #define NX 10 #define NY 20 const CCTK_REAL input_real [NY][NX]; const CCTK_COMPLEX input_complex[NY][NX]; const CCTK_INT input_array_dims[N_DIMS] = { NX, NY }; const CCTK_INT input_array_type_codes[N_INPUT_ARRAYS] = { CCTK_VARIABLE_REAL, CCTK_VARIABLE_COMPLEX }; const void* input_arrays[N_INPUT_ARRAYS]; /* see note above */ /* output arrays */ CCTK_REAL output_real [N_INTERP_POINTS]; CCTK_COMPLEX output_complex[N_INTERP_POINTS]; const CCTK_INT output_array_type_codes[N_OUTPUT_ARRAYS] = { CCTK_VARIABLE_REAL, CCTK_VARIABLE_COMPLEX }; void* output_arrays[N_OUTPUT_ARRAYS]; /* see note above */ int operator_handle, param_table_handle; operator_handle = CCTK_InterpHandle("my interpolation operator"); if (operator_handle < 0) CCTK_WARN(-1, "can’t get interpolation handle!"); param_table_handle = Util_TableCreateFromString("order=3"); if (param_table_handle < 0) CCTK_WARN(-1, "can’t create parameter table!"); /* initialize the rest of the parameter arrays */ interp_coords[0] = (const void *) interp_x; interp_coords[1] = (const void *) interp_y; input_arrays [0] = (const void *) input_real; input_arrays [1] = (const void *) input_complex; output_arrays[0] = ( void *) output_real; output_arrays[1] = ( void *) output_complex; /* do the actual interpolation, and check for error returns */ if (CCTK_InterpLocalUniform(N_DIMS, operator_handle, param_table_handle, origin, delta, N_INTERP_POINTS, CCTK_VARIABLE_REAL, interp_coords, N_INPUT_ARRAYS, input_array_dims, input_array_type_codes, input_arrays, N_OUTPUT_ARRAYS, output_array_type_codes, output_arrays) < 0) CCTK_WARN(-1, "error return from interpolator!");
In this example we interpolate the 3-metric and its 1st derivatives at a set of interpolation points on a 2-sphere.
Note: Since we’re not using C++, we again declare the pointer arrays non-const, and “initialize” them with ordinary (run-time) assignment statements. However, in this example, for greater clarity we place these assignment statements right after the declarations. Since C allows declarations only at the start of a { } block, not in the middle of a block, we nest the rest of the program in extra blocks (with the { } indented 2 spaces to distinguish them from “normal” { } pairs) to allow for further declarations. The reader will have to decide for herself whether this style is more or less ugly than separating the declarations and initializations of the pointer arrays.
#define N_DIMS 3 /* interpolation points */ #define N_INTERP_POINTS 1000 const CCTK_REAL interp_x[N_INTERP_POINTS], interp_y[N_INTERP_POINTS], interp_z[N_INTERP_POINTS]; const void* interp_coords[N_DIMS]; /* see note above */ interp_coords[0] = (const void *) interp_x; interp_coords[1] = (const void *) interp_y; interp_coords[2] = (const void *) interp_z; /* dimensions of the data grid */ #define NX 30 #define NY 40 #define NZ 50 { /* input arrays */ /* n.b. we use Fortran storage order: X is contiguous, Z least so */ #define N_INPUT_ARRAYS 6 const CCTK_REAL gxx_3D[NZ][NY][NX], gxy_3D[NZ][NY][NX], gxz_3D[NZ][NY][NX], gyy_3D[NZ][NY][NX], gyz_3D[NZ][NY][NX], gzz_3D[NZ][NY][NX]; const CCTK_INT input_array_dims[N_DIMS] = {NX, NY, NZ}; const CCTK_INT input_array_type_codes[N_INPUT_ARRAYS] = { CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL }; const void* input_arrays[N_INPUT_ARRAYS]; /* see note above */ input_arrays[0] = (const void *) gxx_3D; input_arrays[1] = (const void *) gxy_3D; input_arrays[2] = (const void *) gxz_3D; input_arrays[3] = (const void *) gyy_3D; input_arrays[4] = (const void *) gyz_3D; input_arrays[5] = (const void *) gzz_3D; { /* output arrays */ /* (for best efficiency we group all operations on a given input together) */ #define N_OUTPUT_ARRAYS 24 CCTK_REAL gxx[N_INTERP_POINTS], dx_gxx[N_INTERP_POINTS], dy_gxx[N_INTERP_POINTS], dz_gxx[N_INTERP_POINTS], gxy[N_INTERP_POINTS], dx_gxy[N_INTERP_POINTS], dy_gxy[N_INTERP_POINTS], dz_gxy[N_INTERP_POINTS], gxz[N_INTERP_POINTS], dx_gxz[N_INTERP_POINTS], dy_gxz[N_INTERP_POINTS], dz_gxz[N_INTERP_POINTS], gyy[N_INTERP_POINTS], dx_gyy[N_INTERP_POINTS], dy_gyy[N_INTERP_POINTS], dz_gyy[N_INTERP_POINTS], gyz[N_INTERP_POINTS], dx_gyz[N_INTERP_POINTS], dy_gyz[N_INTERP_POINTS], dz_gyz[N_INTERP_POINTS], gzz[N_INTERP_POINTS], dx_gzz[N_INTERP_POINTS], dy_gzz[N_INTERP_POINTS], dz_gzz[N_INTERP_POINTS]; const CCTK_INT output_array_type_codes[N_OUTPUT_ARRAYS] = { CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL }; void* output_arrays[N_OUTPUT_ARRAYS]; /* see note above */ output_arrays[ 0] = (void *) gxx; output_arrays[ 1] = (void *) dx_gxx; output_arrays[ 2] = (void *) dy_gxx; output_arrays[ 3] = (void *) dz_gxx; output_arrays[ 4] = (void *) gxy; output_arrays[ 5] = (void *) dx_gxy; output_arrays[ 6] = (void *) dy_gxy; output_arrays[ 7] = (void *) dz_gxy; output_arrays[ 8] = (void *) gxz; output_arrays[ 9] = (void *) dx_gxz; output_arrays[10] = (void *) dy_gxz; output_arrays[11] = (void *) dz_gxz; output_arrays[12] = (void *) gyy; output_arrays[13] = (void *) dx_gyy; output_arrays[14] = (void *) dy_gyy; output_arrays[15] = (void *) dz_gyy; output_arrays[16] = (void *) gyz; output_arrays[17] = (void *) dx_gyz; output_arrays[18] = (void *) dy_gyz; output_arrays[19] = (void *) dz_gyz; output_arrays[20] = (void *) gzz; output_arrays[21] = (void *) dx_gzz; output_arrays[22] = (void *) dy_gzz; output_arrays[23] = (void *) dz_gzz; { /* integer codes to specify the derivatives */ const CCTK_INT operand_indices[N_OUTPUT_ARRAYS] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 }; #define DERIV(x) x const CCTK_INT operation_codes[N_OUTPUT_ARRAYS] = { DERIV(0), DERIV(1), DERIV(2), DERIV(3), DERIV(0), DERIV(1), DERIV(2), DERIV(3), DERIV(0), DERIV(1), DERIV(2), DERIV(3), DERIV(0), DERIV(1), DERIV(2), DERIV(3), DERIV(0), DERIV(1), DERIV(2), DERIV(3), DERIV(0), DERIV(1), DERIV(2), DERIV(3) }; int operator_handle, param_table_handle; operator_handle = CCTK_InterpHandle("my interpolation operator"); if (operator_handle < 0) CCTK_WARN(-1, "can’t get interpolation handle!"); param_table_handle = Util_TableCreate(UTIL_TABLE_FLAGS_DEFAULT); if (param_table_handle < 0) CCTK_WARN(-1, "can’t create parameter table!"); if (Util_TableSetInt(param_table_handle, 3, "order") < 0) CCTK_WARN(-1, "can’t set order in parameter table!"); if (Util_TableSetIntArray(param_table_handle, N_OUTPUT_ARRAYS, operand_indices, "operand_indices") < 0) CCTK_WARN(-1, "can’t set operand_indices array in parameter table!"); if (Util_TableSetIntArray(param_table_handle, N_OUTPUT_ARRAYS, operation_codes, "operation_codes") < 0) CCTK_WARN(-1, "can’t set operation_codes array in parameter table!"); if (CCTK_InterpLocalUniform(N_DIMS, operator_handle, param_table_handle, origin, delta, N_INTERP_POINTS, CCTK_VARIABLE_REAL, interp_coords, N_INPUT_ARRAYS, input_array_dims, input_array_type_codes, input_arrays, N_OUTPUT_ARRAYS, output_array_type_codes, output_arrays) < 0) CCTK_WARN(-1, "error return from interpolator!"); } } }
Thanks to Thomas Radke for our ongoing collaboration on Cactus interpolation, to Ian Hawke and Erik Schnetter for helpful comments on the documentation, to Tom Goodale and Thomas Radke for many useful design discussions, to Erik Schnetter for bug reports, and to all the Cactus crew for a great infrastructure!
log_interp_coords | Scope: private | BOOLEAN |
Description: should we log the grid min(delta)max and the interpolation coordinates for each call
on the interpolator?
| ||
Default: false | ||
Implements:
aeilocalinterp
This section lists all the variables which are assigned storage by thorn Numerical/AEILocalInterp. Storage can either last for the duration of the run (Always means that if this thorn is activated storage will be assigned, Conditional means that if this thorn is activated storage will be assigned for the duration of the run if some condition is met), or can be turned on for the duration of a schedule function.
NONE
CCTK_STARTUP
aeilocalinterp_u_startup
register cctk_interplocaluniform() interpolation operators
After: | driver_startup | |
Language: | c | |
Type: | function | |