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 include the required modules.
#include <cmath>
#include <iostream>
#include <memory>
#include <vector>
We define some parameters such as dimensionality and level of the regular sparse grid.
const size_t dim = 2;
const size_t n = 4;
const size_t p = 3;
const bool hasBoundary = true;
return std::sin(7.0 * x[0] - 3.0) * std::cos(5.0 * x[1] - 5.0);
};
base::ScalarFunction & f
Definition AugmentedLagrangian.cpp:75
A class to store one-dimensional data.
Definition DataVector.hpp:25
void setVerbosity(int level)
Definition Printer.hpp:154
static Printer & getInstance()
Definition Printer.cpp:43
int main()
Definition densityMultiplication.cpp:22
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.
Potentially hetereogeneous basis on full grids, i.e., a dim-dimensional vector of 1D bases of type sg...
Definition HeterogeneousBasis.hpp:24
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.
hasBoundary);
Class for representing a collection of full grids together with one scalar coefficient per full grid.
Definition CombinationGrid.hpp:25
static CombinationGrid fromRegularSparse(size_t dim, level_t n, const HeterogeneousBasis &basis, bool hasBoundary=true)
Factory method to create a CombinationGrid corresponding to the combination technique for a regular s...
Definition CombinationGrid.cpp:29
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.
for (size_t k = 0; k < gridStorage.getSize(); k++) {
gridStorage.getPoint(k).getStandardCoordinates(x);
}
Generic hash table based storage of grid points.
Definition HashGridStorage.hpp:42
void combinePoints(base::GridStorage &gridStorage) const
Combine the grid points of all full grids of this combination grid and store the grid points in an ex...
Definition CombinationGrid.cpp:97
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.
std::vector<sgpp::base::DataVector> values;
void distributeValuesToFullGrids(const base::GridStorage &gridStorage, const base::DataVector &values, std::vector< base::DataVector > &result) const
Distribute values given on the combined grid to the full grids contained in this combination grid.
Definition CombinationGrid.cpp:200
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.
std::vector<sgpp::base::DataVector> surpluses(values);
std::vector<std::unique_ptr<sgpp::combigrid::OperationPole>> opPole;
basis, opPole);
opHier.apply(surpluses);
static void fromHeterogenerousBasis(const HeterogeneousBasis &basis, std::vector< std::unique_ptr< OperationPole > > &operation)
Factory method to create a vector of unique_ptr to OperationPoleHierarchisationGeneral objects from a...
Definition OperationPoleHierarchisationGeneral.cpp:23
Operation for applying 1D OperationPole operators on all poles of all full grids of some combination ...
Definition OperationUPCombinationGrid.hpp:22
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 and print the value.
x.assign({0.12, 0.34});
std::cout << "Value of test function at [" << x[0] << " " << x[1]
<<
"]: " <<
f(x) <<
"\n";
{
const double y = opEval.eval(surpluses, x);
std::cout << "Value of combined sparse grid interpolant at [" << x[0] << " "
<< x[1] << "]: " << y << "\n";
}
Operation for evaluating a combination grid function (linear combination of linear combinations of fu...
Definition OperationEvalCombinationGrid.hpp:23
Finally, we do the same for one full grid of the combination grid: We evaluate 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.
const size_t fullGridIndex = 1;
std::cout << "Level of selected full grid with index " << fullGridIndex
<< ": [" << l[0] << " " << l[1] << "]\n";
{
const double y = opEval.eval(surpluses[fullGridIndex], x);
std::cout << "Value of full grid interpolant at [" << x[0] << " " << x[1]
<< "]: " << y << "\n";
}
return 0;
}
const std::vector< FullGrid > & getFullGrids() const
Definition CombinationGrid.cpp:229
Full grid essentially represented by its level and a HeterogeneousBasis.
Definition FullGrid.hpp:22
const LevelVector & getLevel() const
Definition FullGrid.hpp:86
Operation for evaluating a full grid function (linear combination of full grid basis functions).
Definition OperationEvalFullGrid.hpp:20
std::vector< level_t > LevelVector
level multi-index
Definition LevelIndexTypes.hpp:20
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)\).