Sparse Grids Example 8: local polynomial rules.
Local polynomial grids use a hierarchy of basis functions with decreasing support, Tasmanian offers several different types of basis each tuned to different types of models. This example demonstrates the efficiency (i.e., error per number of points) of different local polynomial grids when applied to different models.
#endif
cout << "\n---------------------------------------------------------------------------------------------------\n";
cout << std::scientific; cout.precision(4);
cout << "Example 8: interpolate different functions demonstrating the different\n"
<< " local polynomial rules\n\n";
int const num_inputs = 2;
int const num_test_points = 1000;
std::vector<double> test_points(num_test_points * num_inputs);
std::minstd_rand park_miller(42);
std::uniform_real_distribution<double> domain(-1.0, 1.0);
for(auto &t : test_points) t = domain(park_miller);
std::function<void(double const x[], double y[], size_t)> model)->
double{
std::vector<double> grid_result;
double err = 0.0;
for(int i=0; i<num_test_points; i++){
double model_result;
model(&test_points[i*num_inputs], &model_result, 0);
err = std::max(err, std::abs(grid_result[i] - model_result));
}
return err;
};
auto smooth_model = [](double const x[], double y[], size_t)->
void{ y[0] = std::exp(-x[0] * x[0]) * std::cos(x[1]); };
int const order = 2;
cout << "Using smooth model: f(x, y) = exp(-x*x) * cos(y)\n"
<< " rule_localp, points = " << grid_localp.getNumPoints()
<< " error = " << get_error(grid_localp, smooth_model) << "\n"
<< " rule_semilocalp, points = " << grid_semilocalp.getNumPoints()
<< " error = " << get_error(grid_semilocalp, smooth_model) << "\n"
<< " If the model is smooth, rule_semilocalp has an advantage.\n\n";
constexpr double pi = 3.14159265358979323846;
auto zero_model = [=](double const x[], double y[], size_t)->
void{ y[0] = std::cos(0.5 * pi * x[0]) * std::cos(0.5 * pi * x[1]); };
TasGrid::loadNeededValues<TasGrid::mode_parallel, true>(zero_model, grid_localp, 4);
cout << "Using homogeneous model: f(x, y) = cos(pi * x / 2) * cos(pi * y / 2)\n"
<< " rule_localp, points = " << grid_localp.getNumPoints()
<< " error = " << get_error(grid_localp, zero_model) << "\n"
<< " rule_localp0, points = " << grid_localp0.getNumPoints()
<< " error = " << get_error(grid_localp0, zero_model) << "\n"
<< " The rule_localp0 uses basis tuned for models with zero boundary.\n";
#ifndef __TASMANIAN_DOXYGEN_SKIP
The master-class that represents an instance of a Tasmanian sparse grid.
Definition: TasmanianSparseGrid.hpp:293
void evaluateBatch(std::vector< FloatType > const &x, std::vector< FloatType > &y) const
Computes the value of the interpolant (or point-wise approximation) for a batch of points.
@ rule_localp
Nested rule with a hierarchy of uniformly distributed nodes and functions with compact support.
Definition: tsgEnumerates.hpp:362
@ rule_localp0
Variation of rule_localp assuming the model is zero at the domain boundary.
Definition: tsgEnumerates.hpp:364
@ rule_semilocalp
Variation of rule_localp using increased support in exchange for higher order basis (better for smoot...
Definition: tsgEnumerates.hpp:366
void loadNeededValues(std::function< void(double const x[], double y[], size_t thread_id)> model, TasmanianSparseGrid &grid, size_t num_threads)
Loads the current grid with model values, does not perform any refinement.
Definition: tsgLoadNeededValues.hpp:104
TasmanianSparseGrid makeLocalPolynomialGrid(int dimensions, int outputs, int depth, int order=1, TypeOneDRule rule=rule_localp, std::vector< int > const &level_limits=std::vector< int >())
Factory method, creates a new grid and calls TasmanianSparseGrid::makeLocalPolynomialGrid().
Definition: TasmanianSparseGrid.hpp:2301