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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
// Copyright (C) 2016 EDF
// All Rights Reserved
// This code is published under the GNU Lesser General Public License (GNU LGPL)
#ifndef SPARSEGRIDCUBICBOUND_H
#define SPARSEGRIDCUBICBOUND_H
#include <Eigen/Dense>
#include "StOpt/core/sparse/sparseGridTypes.h"
#include "StOpt/core/sparse/SparseGridHierarDehierarBound.h"
/** \file SparseGridCubicBound.h
* \brief cubicSparseBound Regroup hierarchization and dehierarchization for cubic sparse grids eliminating boundary points
* \author Xavier Warin
*/
namespace StOpt
{
/// \defgroup Cubic Hierarchization and Deheriarchization with boundary points
/// \brief Regroup function used in hierarchization and dehierarchization for sparse grids without any points on the boundary and a cubic approximation per mesh
///@{
/// \class Hierar1DCubicBound SparseGridCubicBound.h
/// Hierarchization
class Hierar1DCubicBound: public HierarDehierarBound
{
protected :
/// \brief Hierarchization in given dimension in 1D : general cubic
/// \param p_levelCurrent Current level of the point
/// \param p_positionCurrent Current position of the point
/// \param p_iterLevel Iteration on level
/// \param p_idim Current dimension
/// \param p_leftParentNodalValue Left parent nodal value
/// \param p_rightParentNodalValue Right parent nodal value
/// \param p_parentLinearHierarValue Linear Hierarchical value of parent
/// \param p_grandParentLinearHierarValue Linear Hierarchical value of grandparent
/// \param p_dataSet Data structure with all the points
/// \param p_nodalValues Nodal values
/// \param p_hierarValues Hierarchical values
template< class T, class TT>
void recursive1DHierarchization(Eigen::ArrayXc &p_levelCurrent,
Eigen::ArrayXui &p_positionCurrent,
const SparseSet::const_iterator &p_iterLevel,
const unsigned int &p_idim,
const T &p_leftParentNodalValue,
const T &p_rightParentNodalValue,
const T &p_parentLinearHierarValue,
const T &p_grandParentLinearHierarValue,
const SparseSet &p_dataSet,
const TT &p_nodalValues,
TT &p_hierarValues)
{
if (p_iterLevel == p_dataSet.end())
return ;
SparseLevel::const_iterator iterPosition = p_iterLevel->second.find(p_positionCurrent);
if (iterPosition == p_iterLevel->second.end())
return ;
// position
int iposPoint = iterPosition->second;
T valueMidle = DoubleOrArray()(p_nodalValues, iposPoint);
// hierarchization
T LinearHierar = valueMidle - 0.5 * (p_leftParentNodalValue + p_rightParentNodalValue);
int iBaseType = iNodeToFunc[p_positionCurrent(p_idim) % 4];
// begin by linear
DoubleOrArray().affect(p_hierarValues, iposPoint, LinearHierar + weightParent[iBaseType]*p_parentLinearHierarValue + weightGrandParent[iBaseType]*p_grandParentLinearHierarValue);
char oldLevel = p_levelCurrent(p_idim);
unsigned int oldPosition = p_positionCurrent(p_idim);
// child level
p_levelCurrent(p_idim) += 1;
SparseSet::const_iterator iterLevelChild = p_dataSet.find(p_levelCurrent);
// left
p_positionCurrent(p_idim) = 2 * oldPosition;
recursive1DHierarchization<T, TT>(p_levelCurrent, p_positionCurrent, iterLevelChild, p_idim, p_leftParentNodalValue, valueMidle, LinearHierar, p_parentLinearHierarValue, p_dataSet, p_nodalValues,
p_hierarValues);
// right
p_positionCurrent(p_idim) += 1;
recursive1DHierarchization<T, TT>(p_levelCurrent, p_positionCurrent, iterLevelChild, p_idim, valueMidle, p_rightParentNodalValue, LinearHierar, p_parentLinearHierarValue, p_dataSet, p_nodalValues,
p_hierarValues);
p_positionCurrent(p_idim) = oldPosition;
p_levelCurrent(p_idim) = oldLevel;
}
/// \brief Hierarchization in given dimension in 1D quadratic
/// \param p_levelCurrent Current level of the point
/// \param p_positionCurrent Current position of the point
/// \param p_iterLevel Iteration on level
/// \param p_idim Current dimension
/// \param p_leftParentNodalValue Left parent nodal value
/// \param p_rightParentNodalValue Right parent nodal value
/// \param p_parentLinearHierarValue Linear Hierarchical value of parent
/// \param p_dataSet Data structure with all the points
/// \param p_nodalValues Nodal values
/// \param p_hierarValues Hierarchical values
template< class T, class TT>
void hierarchizationStep2(Eigen::ArrayXc &p_levelCurrent,
Eigen::ArrayXui &p_positionCurrent,
const SparseSet::const_iterator &p_iterLevel,
const unsigned int &p_idim,
const T &p_leftParentNodalValue,
const T &p_rightParentNodalValue,
const T &p_parentLinearHierarValue,
const SparseSet &p_dataSet,
const TT &p_nodalValues,
TT &p_hierarValues)
{
if (p_iterLevel == p_dataSet.end())
return ;
SparseLevel::const_iterator iterPosition = p_iterLevel->second.find(p_positionCurrent);
if (iterPosition == p_iterLevel->second.end())
return ;
// position
int iposPoint = iterPosition->second;
T valueMidle = DoubleOrArray()(p_nodalValues, iposPoint);
// hierarchization
T LinearHierar = valueMidle - 0.5 * (p_leftParentNodalValue + p_rightParentNodalValue);
// quadratic
DoubleOrArray().affect(p_hierarValues, iposPoint, LinearHierar - 0.25 * p_parentLinearHierarValue) ;
char oldLevel = p_levelCurrent(p_idim);
unsigned int oldPosition = p_positionCurrent(p_idim);
// child level
p_levelCurrent(p_idim) += 1;
SparseSet::const_iterator iterLevelChild = p_dataSet.find(p_levelCurrent);
// left
p_positionCurrent(p_idim) = 2 * oldPosition;
recursive1DHierarchization<T, TT>(p_levelCurrent, p_positionCurrent, iterLevelChild, p_idim, p_leftParentNodalValue, valueMidle, LinearHierar, p_parentLinearHierarValue,
p_dataSet, p_nodalValues, p_hierarValues);
// right
p_positionCurrent(p_idim) += 1;
recursive1DHierarchization<T, TT>(p_levelCurrent, p_positionCurrent, iterLevelChild, p_idim, valueMidle, p_rightParentNodalValue, LinearHierar, p_parentLinearHierarValue,
p_dataSet, p_nodalValues, p_hierarValues);
p_positionCurrent(p_idim) = oldPosition;
p_levelCurrent(p_idim) = oldLevel;
}
/// \brief Hierarchization in given dimension in 1D (first step : subtract constant value)
/// \param p_levelCurrent Current level of the point
/// \param p_positionCurrent Current position of the point
/// \param p_iterLevel Iteration on level
/// \param p_idim Current dimension
/// \param p_leftParentNodalValue Left parent nodal value
/// \param p_rightParentNodalValue Right parent nodal value
/// \param p_dataSet Data structure with all the points
/// \param p_nodalValues Nodal values
/// \param p_hierarValues Hierarchical values
template< class T, class TT>
void hierarchizationStep1(Eigen::ArrayXc &p_levelCurrent,
Eigen::ArrayXui &p_positionCurrent,
const SparseSet::const_iterator &p_iterLevel,
const unsigned int &p_idim,
const T &p_leftParentNodalValue,
const T &p_rightParentNodalValue,
const SparseSet &p_dataSet,
const TT &p_nodalValues,
TT &p_hierarValues)
{
if (p_iterLevel == p_dataSet.end())
return ;
SparseLevel::const_iterator iterPosition = p_iterLevel->second.find(p_positionCurrent);
if (iterPosition == p_iterLevel->second.end())
return ;
// position
int iposPoint = iterPosition->second;
T valueMidle = DoubleOrArray()(p_nodalValues, iposPoint);
// hierarchization
T LinearHierar = valueMidle - 0.5 * (p_leftParentNodalValue + p_rightParentNodalValue);
// cubic
DoubleOrArray().affect(p_hierarValues, iposPoint, LinearHierar) ;
char oldLevel = p_levelCurrent(p_idim);
unsigned int oldPosition = p_positionCurrent(p_idim);
// child level
p_levelCurrent(p_idim) += 1;
SparseSet::const_iterator iterLevelChild = p_dataSet.find(p_levelCurrent);
// left
p_positionCurrent(p_idim) = 0;
hierarchizationStep2<T, TT>(p_levelCurrent, p_positionCurrent, iterLevelChild, p_idim, p_leftParentNodalValue, valueMidle, LinearHierar, p_dataSet, p_nodalValues, p_hierarValues);
// right
p_positionCurrent(p_idim) = 1;
hierarchizationStep2<T, TT>(p_levelCurrent, p_positionCurrent, iterLevelChild, p_idim, valueMidle, p_rightParentNodalValue, LinearHierar, p_dataSet, p_nodalValues, p_hierarValues);
p_positionCurrent(p_idim) = oldPosition;
p_levelCurrent(p_idim) = oldLevel;
}
public :
// default
Hierar1DCubicBound() {}
// operator for 1D Hierarchization
/// \brief Hierarchization in given dimension in 1D
/// \param p_levelCurrent Current index of the point
/// \param p_positionCurrent Current level of the point
/// \param p_iterLevel Iterator on current level
/// \param p_idim Current dimension
/// \param p_dataSet Data structure with all the points
/// \param p_nodalValues Nodal values
/// \param p_hierarValues Hierarchical values
template< class T, class TT>
void operator()(Eigen::ArrayXc &p_levelCurrent,
Eigen::ArrayXui &p_positionCurrent,
const SparseSet::const_iterator &p_iterLevel,
const unsigned int &p_idim,
const SparseSet &p_dataSet,
const TT &p_nodalValues,
TT &p_hierarValues)
{
// get back boundary key in the dimension
T leftValue ;
T rightValue ;
Eigen::ArrayXui leftBound(p_positionCurrent) ;
leftBound(p_idim) = 0;
SparseLevel::const_iterator iterLeft = p_iterLevel->second.find(leftBound);
if (iterLeft != p_iterLevel->second.end())
leftValue = DoubleOrArray()(p_nodalValues, iterLeft->second);
Eigen::ArrayXui rightBound(p_positionCurrent);
rightBound(p_idim) = 2;
SparseLevel::const_iterator iterRight = p_iterLevel->second.find(rightBound);
if (iterRight != p_iterLevel->second.end())
rightValue = DoubleOrArray()(p_nodalValues, iterRight->second);
hierarchizationStep1<T, TT>(p_levelCurrent, p_positionCurrent, p_iterLevel, p_idim, leftValue, rightValue,
p_dataSet, p_nodalValues, p_hierarValues);
}
};
/// \class Dehierar1DCubicBound SparseGridCubicBound.h
/// Dehierarchization for cubic with boundary points
class Dehierar1DCubicBound: public HierarDehierarBound
{
protected :
/// \brief Dehierarchization 1D in given dimension (modified bound) (cubic so available after two level)
/// \param p_levelCurrent Current index of the point
/// \param p_positionCurrent Current level of the point
/// \param p_iterLevel Iterator on current level
/// \param p_idim Current dimension
/// \param p_leftParentHierarValue Left parent nodal value
/// \param p_rightParentHierarValue Right parent nodal value
/// \param p_linearHierarParent Linear hierarchical value of parent
/// \param p_linearHierarGrandParent Linear Hierarchical value of parent
/// \param p_dataSet Data structure with all the points
/// \param p_hierarValues Hierarchical values
/// \param p_nodalValues Nodal values
template< class T, class TT>
void recursive1DDehierarchization(Eigen::ArrayXc &p_levelCurrent,
Eigen::ArrayXui &p_positionCurrent,
const SparseSet::const_iterator &p_iterLevel,
const unsigned int &p_idim,
const T &p_leftParentHierarValue,
const T &p_rightParentHierarValue,
const T &p_linearHierarParent,
const T &p_linearHierarGrandParent,
const SparseSet &p_dataSet,
const TT &p_hierarValues,
TT &p_nodalValues)
{
if (p_iterLevel == p_dataSet.end())
return ;
// position
SparseLevel::const_iterator iterPosition = p_iterLevel->second.find(p_positionCurrent);
if (iterPosition == p_iterLevel->second.end())
return ;
// position
int iposPoint = iterPosition->second;
T valueMidle = DoubleOrArray()(p_hierarValues, iposPoint);
// do dehierarchization
// calculation linear hierarchical coefficient
int iBaseType = iNodeToFunc[p_positionCurrent(p_idim) % 4];
T LinearHierarchical = valueMidle - (weightParent[iBaseType] * p_linearHierarParent + weightGrandParent[iBaseType] * p_linearHierarGrandParent);
valueMidle = LinearHierarchical + 0.5 * (p_leftParentHierarValue + p_rightParentHierarValue);
DoubleOrArray().affect(p_nodalValues, iposPoint, valueMidle);
char oldLevel = p_levelCurrent(p_idim);
unsigned int oldPosition = p_positionCurrent(p_idim);
// child level
p_levelCurrent(p_idim) += 1;
SparseSet::const_iterator iterLevelChild = p_dataSet.find(p_levelCurrent);
// left
p_positionCurrent(p_idim) = 2 * oldPosition;
recursive1DDehierarchization<T, TT>(p_levelCurrent, p_positionCurrent, iterLevelChild, p_idim, p_leftParentHierarValue, valueMidle, LinearHierarchical, p_linearHierarParent,
p_dataSet, p_hierarValues, p_nodalValues);
// right
p_positionCurrent(p_idim) += 1;
recursive1DDehierarchization<T, TT>(p_levelCurrent, p_positionCurrent, iterLevelChild, p_idim, valueMidle, p_rightParentHierarValue, LinearHierarchical, p_linearHierarParent,
p_dataSet, p_hierarValues, p_nodalValues);
p_positionCurrent(p_idim) = oldPosition;
p_levelCurrent(p_idim) = oldLevel;
}
/// \brief Dehierarchization 1D in given dimension (modified bound, step 2 quadratic)
/// \param p_levelCurrent Current index of the point
/// \param p_positionCurrent Current level of the point
/// \param p_iterLevel Iterator on current level
/// \param p_idim Current dimension
/// \param p_leftParentHierarValue Left parent nodal value
/// \param p_rightParentHierarValue Right parent nodal value
/// \param p_linearHierarParent Linear hierarchical value of parent
/// \param p_dataSet Data structure with all the points
/// \param p_hierarValues Hierarchical values
/// \param p_nodalValues Nodal values
template< class T, class TT>
void dehierarchizationStep2(Eigen::ArrayXc &p_levelCurrent,
Eigen::ArrayXui &p_positionCurrent,
const SparseSet::const_iterator &p_iterLevel,
const unsigned int &p_idim,
const T &p_leftParentHierarValue,
const T &p_rightParentHierarValue,
const T &p_linearHierarParent,
const SparseSet &p_dataSet,
const TT &p_hierarValues,
TT &p_nodalValues)
{
if (p_iterLevel == p_dataSet.end())
return ;
// position
SparseLevel::const_iterator iterPosition = p_iterLevel->second.find(p_positionCurrent);
if (iterPosition == p_iterLevel->second.end())
return ;
// position
int iposPoint = iterPosition->second;
T valueMidle = DoubleOrArray()(p_hierarValues, iposPoint);
// do dehierarchization
// calculation linear hierarchical coefficient
T LinearHierarchical = valueMidle + 0.25 * p_linearHierarParent;
valueMidle = LinearHierarchical + 0.5 * (p_leftParentHierarValue + p_rightParentHierarValue);
DoubleOrArray().affect(p_nodalValues, iposPoint, valueMidle);
char oldLevel = p_levelCurrent(p_idim);
unsigned int oldPosition = p_positionCurrent(p_idim);
// child level
p_levelCurrent(p_idim) += 1;
SparseSet::const_iterator iterLevelChild = p_dataSet.find(p_levelCurrent);
// left
p_positionCurrent(p_idim) = 2 * oldPosition;
recursive1DDehierarchization<T, TT>(p_levelCurrent, p_positionCurrent, iterLevelChild, p_idim, p_leftParentHierarValue, valueMidle, LinearHierarchical, p_linearHierarParent,
p_dataSet, p_hierarValues, p_nodalValues);
// right
p_positionCurrent(p_idim) += 1;
recursive1DDehierarchization<T, TT>(p_levelCurrent, p_positionCurrent, iterLevelChild, p_idim, valueMidle, p_rightParentHierarValue, LinearHierarchical, p_linearHierarParent,
p_dataSet, p_hierarValues, p_nodalValues);
p_positionCurrent(p_idim) = oldPosition;
p_levelCurrent(p_idim) = oldLevel;
}
/// \brief Dehierarchization 1D in given dimension (modified bound, step 2 quadratic)
/// \param p_levelCurrent Current level of the point
/// \param p_positionCurrent Current position of the point
/// \param p_iterLevel Iteration on level
/// \param p_idim Current dimension
/// \param p_leftParentHierarValue Left parent nodal value
/// \param p_rightParentHierarValue Right parent nodal value
/// \param p_dataSet Data structure with all the points
/// \param p_hierarValues Hierarchical values
/// \param p_nodalValues Nodal values
template< class T, class TT>
void dehierarchizationStep1(Eigen::ArrayXc &p_levelCurrent,
Eigen::ArrayXui &p_positionCurrent,
const SparseSet::const_iterator &p_iterLevel,
const unsigned int &p_idim,
const T &p_leftParentHierarValue,
const T &p_rightParentHierarValue,
const SparseSet &p_dataSet,
const TT &p_hierarValues,
TT &p_nodalValues)
{
if (p_iterLevel == p_dataSet.end())
return ;
// position
SparseLevel::const_iterator iterPosition = p_iterLevel->second.find(p_positionCurrent);
if (iterPosition == p_iterLevel->second.end())
return ;
// position
int iposPoint = iterPosition->second;
T LinearHierarchical = DoubleOrArray()(p_hierarValues, iposPoint);
// do dehierarchization
// calculation linear hierarchical coefficient
T valueMidle = LinearHierarchical + 0.5 * (p_leftParentHierarValue + p_rightParentHierarValue);
DoubleOrArray().affect(p_nodalValues, iposPoint, valueMidle);
char oldLevel = p_levelCurrent(p_idim);
unsigned int oldPosition = p_positionCurrent(p_idim);
// child level
p_levelCurrent(p_idim) += 1;
SparseSet::const_iterator iterLevelChild = p_dataSet.find(p_levelCurrent);
// left
p_positionCurrent(p_idim) = 0;
dehierarchizationStep2<T, TT>(p_levelCurrent, p_positionCurrent, iterLevelChild, p_idim, p_leftParentHierarValue, valueMidle, LinearHierarchical,
p_dataSet, p_hierarValues, p_nodalValues);
// right
p_positionCurrent(p_idim) = 1;
dehierarchizationStep2<T, TT>(p_levelCurrent, p_positionCurrent, iterLevelChild, p_idim, valueMidle, p_rightParentHierarValue, LinearHierarchical,
p_dataSet, p_hierarValues, p_nodalValues);
p_positionCurrent(p_idim) = oldPosition;
p_levelCurrent(p_idim) = oldLevel;
}
public :
Dehierar1DCubicBound() {}
/// \brief Dehierarchization 1D in given dimension
/// \param p_levelCurrent Current level of the point
/// \param p_positionCurrent Current position of the point
/// \param p_iterLevel Iteration on level
/// \param p_idim Current dimension
/// \param p_dataSet Data structure with all the points
/// \param p_hierarValues Hierarchical values
/// \param p_nodalValues Nodal values
template< class T, class TT>
void operator()(Eigen::ArrayXc &p_levelCurrent,
Eigen::ArrayXui &p_positionCurrent,
const SparseSet::const_iterator &p_iterLevel,
const unsigned int &p_idim,
const SparseSet &p_dataSet,
const TT &p_hierarValues,
TT &p_nodalValues)
{
// get back boundary key in the dimension
T leftValue ;
T rightValue ;
Eigen::ArrayXui leftBound(p_positionCurrent) ;
leftBound(p_idim) = 0;
SparseLevel::const_iterator iterLeft = p_iterLevel->second.find(leftBound);
if (iterLeft != p_iterLevel->second.end())
leftValue = DoubleOrArray()(p_hierarValues, iterLeft->second);
Eigen::ArrayXui rightBound(p_positionCurrent);
rightBound(p_idim) = 2;
SparseLevel::const_iterator iterRight = p_iterLevel->second.find(rightBound);
if (iterRight != p_iterLevel->second.end())
rightValue = DoubleOrArray()(p_hierarValues, iterRight->second); // get back boundary key in the dimension
dehierarchizationStep1<T, TT>(p_levelCurrent, p_positionCurrent, p_iterLevel, p_idim, leftValue, rightValue, p_dataSet, p_hierarValues, p_nodalValues);
}
};
///@}
}
#endif /* SPARSEGRIDCUBICNONOUND_H */
|