File: sparseGridNoBound.cpp

package info (click to toggle)
stopt 5.12%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,860 kB
  • sloc: cpp: 70,456; python: 5,950; makefile: 72; sh: 57
file content (243 lines) | stat: -rw-r--r-- 9,416 bytes parent folder | download | duplicates (3)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright (C) 2016 EDF
// All Rights Reserved
// This code is published under the GNU Lesser General Public License (GNU LGPL)
#include <iostream>
#include <vector>
#include <functional>
#include <Eigen/Dense>
#include "StOpt/core/sparse/sparseGridTypes.h"
#include "StOpt/core/sparse/sparseGridUtils.h"
#include "StOpt/core/sparse/sparseGridCommon.h"
#include "StOpt/core/sparse/GetCoordinateNoBound.h"

namespace StOpt
{

void recursiveSparseConstructionNoBound(Eigen::ArrayXc &p_levelCurrent,
                                        Eigen::ArrayXui &p_positionCurrent,
                                        SparseSet::iterator &p_iterDataStructureCurrent,
                                        const unsigned short  int &p_idim,
                                        SparseSet &p_dataSet,
                                        size_t &p_ipoint)
{
    if (p_iterDataStructureCurrent != p_dataSet.end())
    {
        if (p_idim == 0)
        {
            p_iterDataStructureCurrent->second[p_positionCurrent] = p_ipoint++ ;
            sparse1DConstruction(p_levelCurrent, p_positionCurrent, p_dataSet, p_ipoint);
        }
        else
        {
            recursiveSparseConstructionNoBound(p_levelCurrent, p_positionCurrent, p_iterDataStructureCurrent, p_idim - 1, p_dataSet, p_ipoint);

            char oldLevel = p_levelCurrent(p_idim);
            unsigned int oldPosition = p_positionCurrent(p_idim);

            // child level
            p_levelCurrent(p_idim) += 1;
            SparseSet::iterator iterDataStructureChild = p_dataSet.find(p_levelCurrent);
            // LEFT
            p_positionCurrent(p_idim) = 2 * oldPosition;
            recursiveSparseConstructionNoBound(p_levelCurrent, p_positionCurrent, iterDataStructureChild, p_idim, p_dataSet, p_ipoint);
            // RIGHT
            p_positionCurrent(p_idim) = 2 * oldPosition + 1;
            recursiveSparseConstructionNoBound(p_levelCurrent, p_positionCurrent, iterDataStructureChild, p_idim, p_dataSet, p_ipoint);

            p_positionCurrent(p_idim) = oldPosition;
            p_levelCurrent(p_idim) = oldLevel;
        }
    }
}



void initialSparseConstructionNoBound(const unsigned int &p_levelMax,
                                      const Eigen::ArrayXd &p_alpha,
                                      SparseSet   &p_dataSet,
                                      size_t     &p_ipoint)
{
    if (p_levelMax < 1)
    {
        std::cout << "Level should be above 0 " << std::endl;
        abort();
    }
    // fist level
    Eigen::ArrayXc firstLevel = Eigen::ArrayXc::Constant(p_alpha.size(), static_cast<char>(1));
    double levelCalc = p_alpha.sum();
    // create map
    createLevelsSparse(firstLevel, 0, p_levelMax + p_alpha.size() - 1, p_alpha, p_dataSet, levelCalc);

    // initialize point
    Eigen::ArrayXui positionCurrent = Eigen::ArrayXui::Constant(p_alpha.size(), static_cast<unsigned int>(0));

    // first iterator
    SparseSet::iterator iterDataFirst = p_dataSet.find(firstLevel);
    recursiveSparseConstructionNoBound(firstLevel, positionCurrent, iterDataFirst, p_alpha.size() - 1, p_dataSet, p_ipoint);
}

void initialFullConstructionNoBound(const unsigned int &p_levelMax,
                                    const Eigen::ArrayXd &p_alpha,
                                    SparseSet   &p_dataSet,
                                    size_t     &p_ipoint)
{
    if (p_levelMax < 1)
    {
        std::cout << "Level should be above 0 " << std::endl;
        abort();
    }
    // fist level
    Eigen::ArrayXc firstLevel = Eigen::ArrayXc::Constant(p_alpha.size(), static_cast<char>(1));

    // create map
    createLevelsFull(firstLevel, 0, p_levelMax, p_alpha, p_dataSet);

    // initialize point
    Eigen::ArrayXui positionCurrent = Eigen::ArrayXui::Constant(p_alpha.size(), static_cast<unsigned int>(0));

    // first iterator
    SparseSet::iterator iterDataFirst = p_dataSet.find(firstLevel);
    recursiveSparseConstructionNoBound(firstLevel, positionCurrent, iterDataFirst, p_alpha.size() - 1, p_dataSet, p_ipoint);
}




void createBasisFunctionNoBound(const int &p_levelMax, const Eigen::ArrayXd &p_weight, const int &p_degree, std::vector< std::vector< std::function< double(const double &) > > > &p_functionScal)
{
    int initLevelMax = 0;
    for (int id = 0 ; id < p_weight.size(); ++id)
        initLevelMax = std::max(initLevelMax, static_cast<int>(p_levelMax / p_weight(id)));
    p_functionScal.resize(initLevelMax);

    // first level
    {
        p_functionScal[0].resize(1);
        p_functionScal[0][0] = OneFunction();
    }
    // level two
    {
        p_functionScal[1].resize(2);
        p_functionScal[1][0] =  LinearHatValue(0., 2, 2.);
        p_functionScal[1][1] =  LinearHatValue(1, 2., 2.);
    }
    // nest on level (
    for (int ilevel = 2 ; ilevel < initLevelMax; ++ilevel)
    {
        p_functionScal[ilevel].resize(lastNode[ilevel] + 1);
        {
            // left element
            p_functionScal[ilevel][0] =   LinearHatValue(0., 2. / deltaSparseMesh[ilevel - 1], 2.);
        }
        {
            // right element
            p_functionScal[ilevel][lastNode[ilevel]] =  LinearHatValue(1., 2. / deltaSparseMesh[ilevel - 1], 2.);
        }
        if (p_degree == 1)
        {
            for (size_t iindex = 1; iindex < lastNode[ilevel]; ++iindex)
            {
                double coordNod = GetCoordinateNoBound()(ilevel + 1, iindex);
                p_functionScal[ilevel][iindex] =  LinearHatValue(coordNod, 2. / deltaSparseMesh[ilevel]);
            }
        }
        else if (p_degree == 2)
        {
            for (size_t iindex = 1; iindex < lastNode[ilevel]; ++iindex)
            {
                double coordNod = GetCoordinateNoBound()(ilevel + 1, iindex);
                p_functionScal[ilevel][iindex] =   QuadraticValue(coordNod, 2. / deltaSparseMesh[ilevel]);
            }
        }
        else
        {
            // degree 3
            {
                {
                    double coordNod = GetCoordinateNoBound()(ilevel + 1, 1);
                    p_functionScal[ilevel][1] = QuadraticValue(coordNod, 2. / deltaSparseMesh[ilevel]);
                }
                for (size_t iindex = 2; iindex < lastNode[ilevel] - 1; ++iindex)
                {
                    double coordNod = GetCoordinateNoBound()(ilevel + 1, iindex);
                    int iBaseType = iindex % 2;
                    // cubic.
                    if (iBaseType == 0)
                    {
                        p_functionScal[ilevel][iindex] = CubicLeftValue(coordNod, 2. / deltaSparseMesh[ilevel]);
                    }
                    else
                    {
                        p_functionScal[ilevel][iindex] = CubicRightValue(coordNod, 2. / deltaSparseMesh[ilevel]);
                    }
                }
                {
                    double coordNod = GetCoordinateNoBound()(ilevel + 1, lastNode[ilevel] - 1);
                    p_functionScal[ilevel][lastNode[ilevel] - 1] =   QuadraticValue(coordNod, 2. / deltaSparseMesh[ilevel]);
                }
            }
        }
    }
}

int  sonEvaluationNoBound(const SparseSet   &p_dataSet, const int &p_idim,
                          const int   &p_nbPoint,
                          Eigen::Array< std::array<int, 2 >, Eigen::Dynamic, Eigen::Dynamic > &p_son)
{
    p_son.resize(p_nbPoint, p_idim);
#ifdef _OPENMP
    #pragma omp parallel
#endif
    {
#ifdef _OPENMP
        size_t ithread = omp_get_thread_num();
        size_t nthreads = omp_get_num_threads();
        size_t cnt = 0;
#endif
        for (const auto &fatherLevel : p_dataSet)
        {
#ifdef _OPENMP
            cnt = +1;
            if ((cnt - 1) % nthreads != ithread) continue;
#endif
            Eigen::ArrayXc level = fatherLevel.first;
            Eigen::ArrayXui position(p_idim);
            for (int id = 0; id < p_idim; ++id)
            {
                level(id) += 1;
                SparseSet::const_iterator iterSon = p_dataSet.find(level);
                if (iterSon != p_dataSet.end())
                {
                    for (const auto &iPosition : fatherLevel.second)
                    {
                        position = iPosition.first;
                        int iposPoint = iPosition.second;
                        // left son
                        position(id) *= 2;
                        p_son(iposPoint, id)[0] = iterSon->second.find(position)->second;
                        // right son
                        position(id) += 1;
                        p_son(iposPoint, id)[1] = iterSon->second.find(position)->second;
                    }
                }
                else
                {
                    for (const auto &iPosition : fatherLevel.second)
                    {
                        int iposPoint = iPosition.second;
                        p_son(iposPoint, id)[0] = -1;
                        p_son(iposPoint, id)[1] = -1;
                    }
                }
                level(id) -= 1;
            }
        }
    }
    // root
    Eigen::ArrayXc levelRoot =  Eigen::ArrayXc::Constant(p_idim, 1);
    Eigen::ArrayXui  positionRoot = Eigen::ArrayXui::Constant(p_idim, 0);
    SparseSet::const_iterator iterLevel = p_dataSet.find(levelRoot);
    SparseLevel::const_iterator iterPosition = iterLevel->second.find(positionRoot);
    return  iterPosition->second ;
}
}