![]() |
SG++-Doxygen-Documentation
|
In this example, we use the combigrid module to interpolate a test function on a two-dimensional regular sparse grid with the combination technique and hierarchical B-splines.
First, we import the required modules.
Next, we define a helper function for plotting the resulting functions.
We define some parameters such as dimensionality and level of the regular sparse grid.
The basis functions are defined via an sgpp::combigrid::HeterogeneousBasis object. In contrast to sgpp::base::Basis, this allows for different types of basis functions for the different dimensions. However, for this example, we do not need this flexibility, so we use the same basis function types for both dimensions.
An sgpp::combigrid::CombinationGrid is a collection of full grids (nodal subspaces) together with scalar-valued coefficients. Here, we construct an sgpp::combigrid::CombinationGrid object for a regular sparse grid via the combination technique.
We obtain the grid points of the regular sparse grid by combining the grid points of all full grids that are contained in the combination grid.
We now want to perform an operation on each full grid. For this, we distribute the values of the combined grid (sparse grid) to the full grids. The result is a std::vector
of sgpp::base::DataVector; each DataVector
contains the values at all grid points for one specific full grid.
The operation we want to perform on each full grid is hierarchization. Since the grids are full grids, we can use the unidirectional principle for this, which performs 1D hierarchization on each pole (one-dimensional sub-grid), iterating over all dimensions.
The resulting surpluses are also a std::vector
of sgpp::base::DataVector, separated by full grids. We could combine the full grid surpluses via the combination formula to the sparse grid surpluses via combineSparseGridValues
. However, the operation sgpp::combigrid::OperationEvalCombinationGrid does this automatically.
We evaluate the combined function (combination of all full grid interpolants) at some arbitrary point, print the value, and plot the function.
Finally, we do the same for one full grid of the combination grid: We evaluate and plot the corresponding interpolant. We extract the surpluses from the already calculated vector
of DataVector
. Alternatively, we could also apply sgpp::combigrid::OperationUPFullGrid with opPole to obtain the surpluses for this single full grid.
The example program outputs the following results:
Value of test function at [0.12 0.34]: 0.820974 Value of combined sparse grid interpolant at [0.12 0.34]: 0.774666 Level of selected full grid with index 1: [3 1] Value of full grid interpolant at [0.12 0.34]: 0.564036
We see that the value of the combined sparse grid interpolant at the evaluation point is closer to the actual value of the test function than the value of the chosen full grid interpolant, which corresponds to the full grid of level \((3, 1)\).