File: sparseGridUtils.h

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 (345 lines) | stat: -rw-r--r-- 10,614 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright (C) 2016 EDF
// All Rights Reserved
// This code is published under the GNU Lesser General Public License (GNU LGPL)
#ifndef SPARSEGRIDUTILS_H
#define SPARSEGRIDUTILS_H
#include <array>
#include "StOpt/core/utils/comparisonUtils.h"

/** \file  sparseGridUtils.h
 *  \brief utilitarian for sparse grids : permits to avoid calculations
 *  \author Xavier Warin
 */
namespace StOpt
{
/// \defgroup sparseUtils Utilitary for sparse grids
/// \brief Utilitarian for sparse grids
///@{
extern std::array<double, 23> deltaSparseMesh ; ///< size or semi size of the mesh by sparse grids
extern std::array<unsigned int, 21> lastNode;  ///< node of last node associated to each level
extern std::array<int, 4> iNodeToFunc ; ///< helper for cubic hierarchization or dehierarchization

/// \brief  for left and right Hierarchization, give weight for parent for cubic Hierarchization or Dehierarchization
///@{
extern std::array<double, 2> weightParent;
extern std::array<double, 2> weightGrandParent;
extern std::array<double, 2> weightQuadraticParent;
///@}


/// \brief function used to get genericity of sparse algorithms
///        Either  the input is an Eigen::ArrayXd and the return is a double
///        or it is an Eigen::ArrayXXd and the return is an Eigen::ArrayXd
class DoubleOrArray
{
public:

    /// \brief First operator()
    /// \param  p_x       array used to get values
    /// \param  p_point   index in the array
    /// \return  p_x(p_point)
    inline double operator()(const Eigen::ArrayXd &p_x, const int &p_point) const
    {
        return p_x(p_point);
    }
    /// \brief Second operator ()
    /// \param p_x  array used to get values
    /// \param  p_point  index in the array
    /// \return  p_x(p_point)
    inline Eigen::ArrayXd operator()(const Eigen::ArrayXXd &p_x, const int &p_point) const
    {
        return p_x.col(p_point) ;
    }

    /// \brief Zero affectation
    /// \param p_x vector to assign
    ///
    inline void zero(double &p_x, const Eigen::ArrayXd &) const
    {
        p_x = 0;
    }

    /// \brief Zero affectation for array
    /// \param p_x vector to assign
    /// \param  p_ut  utilitarian to get size of object
    inline void zero(Eigen::ArrayXd &p_x, const Eigen::ArrayXXd &p_ut) const
    {
        p_x = Eigen::ArrayXd::Zero(p_ut.rows());
    }


    /// \brief affectation operator
    /// \param p_x  array used to get values
    /// \param p_point  index in the array
    /// \param p_affect value to affect
    inline void affect(Eigen::ArrayXd &p_x, const int &p_point, const double &p_affect)  const
    {
        p_x(p_point) = p_affect;
    }

    /// \brief second affectation operator
    /// \param p_x  array used to get values
    /// \param p_point  index in the array
    /// \param p_affect value to affect
    inline void affect(Eigen::ArrayXXd &p_x, const int &p_point, const Eigen::ArrayXd   &p_affect)  const
    {
        p_x.col(p_point) = p_affect;
    }

    /// \brief resize operator
    /// \param p_x  array to work on
    /// \param p_size size to impose to the array
    inline void resize(Eigen::ArrayXd &p_x, int p_size)
    {
        p_x.conservativeResize(p_size);
    }

    /// \brief resize operator
    /// \param p_x  array to work on
    /// \param p_size size of the array
    inline void resize(Eigen::ArrayXXd &p_x, int p_size)
    {
        int nRows = p_x.rows();
        p_x.conservativeResize(nRows, p_size);
    }
};


/// \brief Evaluate linear function basis
///        Basis on \f$ [ xMilde -1./unDx, xMilde+1./unDx]\f$
///        Hat function taking value 1 in xMilde, 0 in \f$ xMilde -1./unDx, xMilde+1./unDx \f$
///        Evaluation for \f$  x \in [ xMilde -1./unDx, xMilde+1./unDx] \f$
class LinearHatValue
{

    double m_xMilde; ///<  coordinate of the middle of the mesh
    double m_unDx ; ///< one over size of mesh
    double m_scale ; ///< scaling factor
public :

    /// \brief Constructor
    /// \param p_xMidle coordinate of the middle of the mesh
    /// \param p_unDx   one over size of mesh
    LinearHatValue(const double &p_xMidle, const double &p_unDx) : m_xMilde(p_xMidle), m_unDx(p_unDx), m_scale(1.) {}

    /// \brief Constructor
    /// \param p_xMidle coordinate of the middle of the mesh
    /// \param p_unDx   one over size of mesh
    /// \param p_scale   scaling factor
    LinearHatValue(const double &p_xMidle, const double &p_unDx, const double &p_scale) : m_xMilde(p_xMidle), m_unDx(p_unDx), m_scale(p_scale) {}

    /// \brief operator ()
    /// \param p_x  Point coordinate where the function is evaluated
    inline double operator()(const double &p_x) const
    {
        assert(isLesserOrEqual(std::fabs(m_xMilde - p_x)*m_unDx, 1.));
        return m_scale * (1. - std::fabs(m_xMilde - p_x) * m_unDx);
    }
};


/// \brief Evaluate quadratic function basis
///        Basis on \f$ [ xMilde -1./unDx, xMilde+1./unDx]\f$
///        Evaluation for \f$  x \in [ xMilde -1./unDx, xMilde+1./unDx] \f$
class QuadraticValue
{
    double m_xMilde; ///<  coordinate of the middle of the mesh
    double m_unDx ; ///< one over size of mesh
public :

    /// \brief Constructor
    /// \param p_xMidle coordinate of the middle of the mesh
    /// \param p_unDx   one over size of mesh
    QuadraticValue(const double &p_xMidle, const double &p_unDx): m_xMilde(p_xMidle), m_unDx(p_unDx) {}

    /// \brief operator ()
    /// \param p_x  Point coordinate where the function is evaluated
    inline double operator()(const double   &p_x) const
    {
        assert(isLesserOrEqual(std::fabs(m_xMilde - p_x)*m_unDx, 1.));
        double xInter = (m_xMilde - p_x) * m_unDx;
        return (1 + xInter) * (1 - xInter);
    }
};


/// \brief Evaluate Cubic function basis (left one)
class CubicLeftValue
{
private :

    double m_unTiers;
    double m_xMilde; ///<  coordinate of the middle of the mesh
    double m_unDx ; ///< one over size of mesh

public :

    /// \brief Constructor
    /// \param p_xMidle coordinate of the middle of the mesh
    /// \param p_unDx   one over size of mesh
    CubicLeftValue(const double &p_xMidle, const double &p_unDx): m_unTiers(1. / 3.), m_xMilde(p_xMidle), m_unDx(p_unDx) {}

    /// \brief operator ()
    /// \param p_x  Point coordinate where the function is evaluated
    inline double operator()(const double &p_x) const
    {
        double xInter = (p_x - m_xMilde) * m_unDx;
        if (std::fabs(xInter) > 1)
            return 0.;
        return (xInter * xInter - 1) * (xInter - 3) * m_unTiers;
    }
};


/// \brief Evaluate Cubic function basis (right one)
class CubicRightValue
{
private :

    double unTiers;
    double m_xMilde; ///<  coordinate of the middle of the mesh
    double m_unDx ; ///< one over size of mesh

public :

    /// \brief Constructor
    /// \param p_xMidle coordinate of the middle of the mesh
    /// \param p_unDx   one over size of mesh
    CubicRightValue(const double &p_xMidle, const double &p_unDx): unTiers(1. / 3.), m_xMilde(p_xMidle), m_unDx(p_unDx) {}

    /// \brief operator ()
    /// \param p_x  Point coordinate where the function is evaluated
    inline double operator()(const double &p_x) const
    {
        double xInter = (p_x - m_xMilde) * m_unDx;
        return (1. - xInter * xInter) * (xInter + 3) * unTiers;
    }
};

/// \brief One function
class OneFunction
{

public :

    OneFunction() {}

    /// \brief return one
    inline double operator()(const double &) const
    {
        return 1;
    }
};


/// \brief Permits to get non direct father of a point for a sparse grid without boundary points
class GetNonDirectFatherNoBound
{
public :
    /// \brief Constructor
    GetNonDirectFatherNoBound() {}

    /// \param p_level             level  (to update)
    /// \param p_position          position (to update)
    void inline operator()(char   &p_level,   unsigned int   &p_position)
    {
        assert(p_level > 2);
        unsigned int  ip = p_position & 1; // 1 if even, 0 otherwise
        unsigned int  father = p_position >> 1;
        p_position = (father >> 1); // grandfather
        p_level -= 2 ;
        // test if son is correct
        while (((p_position << 1) | ip) == father)
        {
            father = p_position;
            p_position = p_position >> 1;
            p_level -= 1 ;
        }
    }
};

/// \brief Permits to get non direct father of a point for a sparse grid with boundary points
class GetNonDirectFatherBound
{
public :
    /// \brief Constructor
    GetNonDirectFatherBound() {}

    /// \param p_level             level  (to update)
    /// \param p_position          position (to update)
    void inline operator()(char   &p_level,   unsigned int   &p_position)
    {
        assert(p_level > 1);
        if (p_position == 0)
        {
            p_level = 1 ;
            p_position = 0;
            return ;
        }
        else if (p_position == lastNode[p_level - 1])
        {
            p_level = 1 ;
            p_position = 2;
            return ;
        }
        unsigned int  ip = p_position & 1; // 1 if even, 0 otherwise
        unsigned int  father = p_position >> 1;
        p_position = (father >> 1); // grandfather
        p_level -= 2 ;
        // test if son is correct
        while (((p_position << 1) | ip) == father)
        {
            father = p_position;
            p_position = p_position >> 1;
            p_level -= 1 ;
        }
        // correct if first level
        if (p_level == 1)
            p_position = 1 ;
    }
};

/// \brief permits to get direct father for sparse grids without boundary points
class GetDirectFatherNoBound
{
public :
    /// \brief Constructor
    GetDirectFatherNoBound() {}

    /// \param p_level                level  (to update)
    /// \param p_position              position (to update)
    void inline operator()(char   &p_level,   unsigned int   &p_position)
    {
        p_level -= 1 ;
        p_position = (p_position >> 1) ;
    }
};

/// \brief permits to get direct father for sparse grids with boundary points
class GetDirectFatherBound
{
public :
    /// \brief Constructor
    GetDirectFatherBound() {}

    /// \param p_level                level  (to update)
    /// \param p_position              position (to update)
    void inline operator()(char   &p_level,   unsigned int   &p_position)
    {
        assert(p_level > 1);
        if (p_level == 2)
        {
            p_level = 1 ;
            p_position = 1 ; // centrer point
        }
        else
        {
            p_level -= 1 ;
            p_position = (p_position >> 1) ;
        }
    }
};
///@}
}

#endif /* SPARSEGRIDUTILS.H */