Doxygen 1.9.1
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.1
Tasmanian Sparse Grids module, example 8
Collaboration diagram for Tasmanian Sparse Grids module, example 8:

Functions

void sparse_grids_example_08 ()
 Sparse Grids Example 8: local polynomial rules. More...
 

Detailed Description

Example 8
Different local polynomial rules.

Function Documentation

◆ sparse_grids_example_08()

void sparse_grids_example_08 ( )

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; // using two inputs for all tests
// using random points to test the error
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);
// computes the error between the gird surrogate model and the actual model
// using the test points, finds the largest absolute error
auto get_error = [&](TasGrid::TasmanianSparseGrid const &grid,
std::function<void(double const x[], double y[], size_t)> model)->
double{
std::vector<double> grid_result;
grid.evaluateBatch(test_points, grid_result);
double err = 0.0;
for(int i=0; i<num_test_points; i++){
double model_result; // using only one output
model(&test_points[i*num_inputs], &model_result, 0);
err = std::max(err, std::abs(grid_result[i] - model_result));
}
return err;
};
// test 1: using smooth function
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; // at order 1 localp and semilocalp are identical
auto grid_localp = TasGrid::makeLocalPolynomialGrid(num_inputs, 1, 7, order,
auto grid_semilocalp = TasGrid::makeLocalPolynomialGrid(num_inputs, 1, 7, order,
TasGrid::loadNeededValues(smooth_model, grid_localp, 4);
TasGrid::loadNeededValues(smooth_model, grid_semilocalp, 4);
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";
// test 1: using model with zero-boundary conditions
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]); };
auto grid_localp0 = TasGrid::makeLocalPolynomialGrid(num_inputs, 1, 6, order,
// the true value indicates overwrite of the currently loaded model
TasGrid::loadNeededValues<TasGrid::mode_parallel, true>(zero_model, grid_localp, 4);
TasGrid::loadNeededValues(zero_model, grid_localp0, 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