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

Functions

void sparse_grids_example_11 ()
 Sparse Grids Example 11: Unstructured data. More...
 

Detailed Description

Example 11
Constructing a grid from unstructured data.

Function Documentation

◆ sparse_grids_example_11()

void sparse_grids_example_11 ( )

Sparse Grids Example 11: Unstructured data.

Sparse grid approximation (or surrogates) can be constructed from a set of points not necessarily aligned to the points on the grid, using a least-squares fit.

  • the fit will not interpolate the data, i.e., there will be a difference between the surrogate and the data at each point
  • the overall accuracy will be lower and more points are needed to reach the same accuracy as in the structured case, the fit should be used when the model cannot be sampled exactly at the sparse grid points
  • solving the fit requires the solution to a system of linear equations and uses significant amount of flops and memory, at the minimum BLAS must be enabled but GPU acceleration is preferable with the MAGMA library which provides advanced out-of-core methods
#endif
cout << "\n---------------------------------------------------------------------------------------------------\n";
cout << std::scientific; cout.precision(4);
cout << "Example 11: construction using unstructured data\n\n";
int const num_inputs = 2;
// 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;
};
// using a simple model
auto model = [](double const x[], double y[], size_t)->
void{ y[0] = std::exp(-x[0]*x[0] -x[1]-x[1]); };
// generate random data for the inputs, and compute the corresponding outputs
int const num_data_points = 2000;
std::vector<double> data_input(num_inputs * num_data_points);
std::vector<double> data_output(num_data_points);
for(auto &d : data_input) d = domain(park_miller);
for(int i=0; i<num_data_points; i++) model(&data_input[i * num_inputs], &data_output[i], 0);
// check if capability is available
cout << "Skipping example 11, BLAS, CUDA, or MAGMA acceleration required.\n";
return;
}
// accel_cpu_blas: works on the CPU and can utilize all available RAM
// accel_gpu_cuda: works on the GPU but it is restricted to the case
// where the data can fit in GPU memory
// accel_gpu_magma: works out-of-core, the data is stored in CPU RAM
// while computations are still done on the GPU
// constructs the grid, depending on the amount of data data,
// the side of the grid and the enabled acceleration
// this can take significant amount of time
TasGrid::loadUnstructuredDataL2(data_input, data_output, 1.E-4, grid);
cout << "Using construction from unstructured (random) data\n";
cout << " approximatino error = " << get_error(grid, model) << "\n\n";
#ifndef __TASMANIAN_DOXYGEN_SKIP
The master-class that represents an instance of a Tasmanian sparse grid.
Definition: TasmanianSparseGrid.hpp:293
static bool isAccelerationAvailable(TypeAcceleration acc)
Returns whether a specific mode can be enabled.
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.
void enableAcceleration(TypeAcceleration acc)
Change the current acceleration mode to the one specified.
@ rule_clenshawcurtis
Classic nested rule using Chebyshev nodes with very low Lebesgue constant.
Definition: tsgEnumerates.hpp:289
@ accel_cpu_blas
Default (if available), uses both BLAS and LAPACK libraries.
Definition: tsgEnumerates.hpp:555
@ accel_gpu_magma
Same the CUDA option but uses the UTK MAGMA library for the linear algebra operations.
Definition: tsgEnumerates.hpp:563
@ accel_gpu_cuda
Similar to the cuBLAS option but also uses a set of Tasmanian custom GPU kernels.
Definition: tsgEnumerates.hpp:561
@ type_level
Ignoring the polynomial space, use rules with index .
Definition: tsgEnumerates.hpp:209
void loadUnstructuredDataL2(double const data_points[], int num_data, double const model_values[], double tolerance, TasmanianSparseGrid &grid)
Construct a sparse grid surrogate using a least-squares fit.
Definition: tsgLoadUnstructuredPoints.hpp:282
TasmanianSparseGrid makeGlobalGrid(int dimensions, int outputs, int depth, TypeDepth type, TypeOneDRule rule, std::vector< int > const &anisotropic_weights=std::vector< int >(), double alpha=0.0, double beta=0.0, const char *custom_filename=nullptr, std::vector< int > const &level_limits=std::vector< int >())
Factory method, creates a new grid and calls TasmanianSparseGrid::makeGlobalGrid().
Definition: TasmanianSparseGrid.hpp:2272