SG++
performance.cpp

This example can be found under combigrid/examples/performance.cpp.

// Copyright (C) 2008-today The SG++ project
// This file is part of the SG++ project. For conditions of distribution and
// use, please see the copyright notice provided with SG++ or at
// sgpp.sparsegrids.org
#include <cmath>
#include <iostream>
#include <vector>
double f(sgpp::base::DataVector const &x) {
double prod = 1.0;
for (size_t dim = 0; dim < x.getSize(); ++dim) {
prod *= exp(-x[dim] * x[dim]);
}
return prod;
}
void testEfficientPCE() {
size_t d = 5;
config.polyParameters.alpha_ = 10.0;
config.polyParameters.beta_ = 5.0;
auto functionBasis = std::make_shared<sgpp::combigrid::OrthogonalPolynomialBasis1D>(config);
for (size_t q = 2; q < 8; ++q) {
// interpolate on adaptively refined grid
// auto op = sgpp::combigrid::CombigridOperation::createLinearLejaPolynomialInterpolation(d,
// func);
d, func);
stopwatch.start();
op->getLevelManager()->addRegularLevels(q);
// op->getLevelManager()->addLevelsAdaptiveParallel(1000, 4);
stopwatch.log();
// compute the variance
// initialize the surrogate model
config.basisFunction = functionBasis;
stopwatch.start();
double mean = pce->mean();
double variance = pce->variance();
sgpp::base::DataVector sobol_indices;
sgpp::base::DataVector total_sobol_indices;
// pce.getComponentSobolIndices(sobol_indices);
// pce.getTotalSobolIndices(total_sobol_indices);
std::cout << "Time: " << stopwatch.elapsedSeconds() / static_cast<double>(op->numGridPoints())
<< std::endl;
std::cout << "---------------------------------------------------------" << std::endl;
std::cout << "#gp = " << op->getLevelManager()->numGridPoints() << std::endl;
std::cout << "E(u) = " << mean << std::endl;
std::cout << "Var(u) = " << variance << std::endl;
// std::cout << "Sobol indices = " << sobol_indices.toString() << std::endl;
// std::cout << "Sum Sobol indices = " << sobol_indices.sum() << std::endl;
// std::cout << "Total Sobol indices = " << total_sobol_indices.toString() << std::endl;
std::cout << "---------------------------------------------------------" << std::endl;
}
}
void testFullGridPCE(sgpp::combigrid::FullGridSummationStrategyType summationType) {
size_t d = 5;
config.polyParameters.alpha_ = 10.0;
config.polyParameters.beta_ = 5.0;
auto functionBasis = std::make_shared<sgpp::combigrid::OrthogonalPolynomialBasis1D>(config);
std::vector<std::shared_ptr<sgpp::combigrid::AbstractInfiniteFunctionBasis1D>> functionBases(
d, functionBasis);
for (size_t q = 2; q < 8; ++q) {
// interpolate on adaptively refined grid
// std::vector<std::shared_ptr<sgpp::combigrid::AbstractPointHierarchy>> pointHierarchies(
// d, sgpp::combigrid::CombiHierarchies::linearLeja());
std::vector<std::shared_ptr<sgpp::combigrid::AbstractPointHierarchy>> pointHierarchies(
std::shared_ptr<sgpp::combigrid::AbstractCombigridStorage> storage =
std::make_shared<sgpp::combigrid::CombigridTreeStorage>(pointHierarchies, true, func);
auto levelManager = std::make_shared<sgpp::combigrid::AveragingLevelManager>();
auto op = std::make_shared<sgpp::combigrid::CombigridTensorOperation>(
pointHierarchies, tensorEvaluators, levelManager, storage, summationType);
stopwatch.start();
levelManager->addRegularLevels(q);
auto result = op->getResult();
// levelManager->addLevelsAdaptiveParallel(1000, 4);
std::cout << "Time: "
<< stopwatch.elapsedSeconds() / static_cast<double>(levelManager->numGridPoints())
<< std::endl;
std::cout << "---------------------------------------------------------" << std::endl;
std::cout << "#gp = " << levelManager->numGridPoints() << std::endl;
std::cout << "E(u) = " << result.get(sgpp::combigrid::MultiIndex(d, 0)) << "\n";
std::cout << "Var(u) = " << std::pow(result.norm(), 2) << "\n";
std::cout << "---------------------------------------------------------" << std::endl;
}
}
void oldSpeedtest() {
size_t dim = 6;
auto op =
sgpp::base::DataVector parameter(std::vector<double>{0.1, 0.2, 0.3, 0.4, 0.5, 0.6});
{
op->evaluate(10, parameter);
sw.log();
std::cout << "Number of grid points: " << op->numGridPoints() << "\n";
}
{
for (size_t i = 0; i < 10; ++i) {
op->evaluate(10, parameter);
}
sw.log();
std::cout << "Number of grid points: " << op->numGridPoints() << "\n";
}
}
int main() {
// testEfficientPCE();
return 0;
}