File: example_sparse_grids_01.cpp

package info (click to toggle)
tasmanian 8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,852 kB
  • sloc: cpp: 34,523; python: 7,039; f90: 5,080; makefile: 224; sh: 64; ansic: 8
file content (88 lines) | stat: -rw-r--r-- 2,718 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

#include "Tasmanian.hpp"

using namespace std;

/*!
 * \internal
 * \file example_sparse_grids_01.cpp
 * \brief Examples for the Tasmanian Sparse Grid module.
 * \author Miroslav Stoyanov
 * \ingroup TasmanianSGExamples
 *
 * Tasmanian Sparse Grids Example 1.
 * \endinternal
 */

/*!
 * \ingroup TasmanianSGExamples
 * \addtogroup TasmanianSGExamples1 Tasmanian Sparse Grids module, example 1
 *
 * \par Example 1
 * Compute \f$ \int_{-1}^{+1} \int_{-1}^{+1} \exp(-x^2) \cos(y) dy dx \f$
 * using a sparse grid with Clenshaw-Curtis nodes and weights.
 */

/*!
 * \ingroup TasmanianSGExamples1
 * \brief Sparse Grids Example 1: integrate a simple function over a canonical domain.
 *
 * \snippet SparseGrids/Examples/example_sparse_grids_01.cpp SG_Example_01 example
 */
void sparse_grids_example_01(){
#ifndef __TASMANIAN_DOXYGEN_SKIP
//! [SG_Example_01 example]
#endif

    cout << "\n---------------------------------------------------------------------------------------------------\n";
    cout << std::scientific; cout.precision(17);
    cout << "Example 1:  integrate f(x,y) = exp(-x^2) * cos(y),\n"
         << "            using clenshaw-curtis nodes and grid of type level\n";

    int dimension = 2;
    int level = 6;

    TasGrid::TasmanianSparseGrid grid;
    grid.makeGlobalGrid(dimension, 0, level, TasGrid::type_level, TasGrid::rule_clenshawcurtis);
    std::vector<double> points  = grid.getPoints();
    std::vector<double> weights = grid.getQuadratureWeights();
    int num_points = grid.getNumPoints(); // also equal to weights.size()

    double I = 0.0;
    for(int i=0; i<num_points; i++){
        double x = points[i*dimension];
        double y = points[i*dimension+1];
        I += weights[i] * std::exp(-x*x) * std::cos(y);
    }

    double exact = 2.513723354063905e+00;
    double E = std::abs(exact - I);
    cout <<   "      at level: " << level
         << "\n      the grid has: " << num_points
         << "\n      integral: " << I
         << "\n         error: " << E << "\n\n";

    level = 7;

    grid.makeGlobalGrid(dimension, 0, level, TasGrid::type_level, TasGrid::rule_clenshawcurtis);
    points = grid.getPoints();
    weights = grid.getQuadratureWeights();
    num_points = grid.getNumPoints();

    I = 0.0;
    for(int i=0; i<num_points; i++){
        double x = points[i*dimension];
        double y = points[i*dimension+1];
        I += weights[i] * std::exp(-x*x) * std::cos(y);
    }

    E = std::abs(exact - I);
    cout <<   "      at level: " << level
         << "\n      the grid has: " << num_points
         << "\n      integral: " << I
         << "\n         error: " << E << endl;

#ifndef __TASMANIAN_DOXYGEN_SKIP
//! [SG_Example_01 example]
#endif
}