| 1 |
/* |
| 2 |
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
conditions and the following disclaimer. Redistributions in binary form must reproduce |
| 10 |
the above copyright notice, this list of conditions and the following disclaimer |
| 11 |
in the documentation and/or other materials provided with the distribution. |
| 12 |
|
| 13 |
Neither the name of the Johns Hopkins University nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without specific |
| 15 |
prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 18 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 20 |
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 |
DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
template< unsigned int Dim , class Real > |
| 30 |
template< unsigned int ... FEMSigs , unsigned int PointD > |
| 31 |
template< unsigned int _PointD > |
| 32 |
CumulativeDerivativeValues< double , Dim , _PointD > FEMTree< Dim , Real >::_Evaluator< UIntPack< FEMSigs ... > , PointD >::_values( unsigned int d , const int fIdx[Dim] , const int cIdx[Dim] , const _CenterOffset off[Dim] , bool parentChild ) const |
| 33 |
{ |
| 34 |
double dValues[Dim][_PointD+1]; |
| 35 |
_setDValues< _PointD >( d , fIdx , cIdx , off , parentChild , dValues ); |
| 36 |
return Evaluate< Dim , double , _PointD >( dValues ); |
| 37 |
} |
| 38 |
template< unsigned int Dim , class Real > |
| 39 |
template< unsigned int ... FEMSigs , unsigned int PointD > |
| 40 |
template< unsigned int _PointD > |
| 41 |
CumulativeDerivativeValues< double , Dim , _PointD > FEMTree< Dim , Real >::_Evaluator< UIntPack< FEMSigs ... > , PointD >::_centerValues( unsigned int d , const int fIdx[Dim] , const int cIdx[Dim] , bool parentChild ) const |
| 42 |
{ |
| 43 |
_CenterOffset off[Dim]; |
| 44 |
for( int d=0 ; d<Dim ; d++ ) off[d] = CENTER; |
| 45 |
return _values< _PointD >( d , fIdx , cIdx , off , parentChild ); |
| 46 |
} |
| 47 |
template< unsigned int Dim , class Real > |
| 48 |
template< unsigned int ... FEMSigs , unsigned int PointD > |
| 49 |
template< unsigned int _PointD > |
| 50 |
CumulativeDerivativeValues< double , Dim , _PointD > FEMTree< Dim , Real >::_Evaluator< UIntPack< FEMSigs ... > , PointD >::_cornerValues( unsigned int d , const int fIdx[Dim] , const int cIdx[Dim] , int corner , bool parentChild ) const |
| 51 |
{ |
| 52 |
_CenterOffset off[Dim]; |
| 53 |
for( int d=0 ; d<Dim ; d++ ) off[d] = ( (corner>>d) & 1 ) ? FRONT : BACK; |
| 54 |
return _values< _PointD >( d , fIdx , cIdx , off , parentChild ); |
| 55 |
} |
| 56 |
|
| 57 |
template< unsigned int Dim , class Real > |
| 58 |
template< unsigned int ... FEMSigs , unsigned int PointD > |
| 59 |
void FEMTree< Dim , Real >::_Evaluator< UIntPack< FEMSigs ... > , PointD >::set( LocalDepth maxDepth ) |
| 60 |
{ |
| 61 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportSize ... > CenterSizes; |
| 62 |
static const unsigned int LeftCenterRadii[] = { BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportEnd ... }; |
| 63 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportSize ... > CornerSizes; |
| 64 |
static const unsigned int LeftCornerRadii[] = { BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportEnd ... }; |
| 65 |
typedef UIntPack< ( BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::BCornerSize + 1 ) ... > BCornerSizes; |
| 66 |
static const unsigned int LeftBCornerRadii[] = { BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::BCornerEnd ... }; |
| 67 |
|
| 68 |
if( stencilData ) DeletePointer( stencilData ); |
| 69 |
stencilData = NewPointer< StencilData >( maxDepth+1 ); |
| 70 |
if( evaluators ) DeletePointer( evaluators ); |
| 71 |
evaluators = NewPointer< Evaluators >( maxDepth+1 ); |
| 72 |
if( childEvaluators ) DeletePointer( childEvaluators ); |
| 73 |
childEvaluators = NewPointer< ChildEvaluators >( maxDepth+1 ); |
| 74 |
_setEvaluators( maxDepth ); |
| 75 |
for( int depth=0 ; depth<=maxDepth ; depth++ ) |
| 76 |
{ |
| 77 |
int center = ( 1<<depth )>>1; |
| 78 |
int cIdx[Dim] , fIdx[Dim]; |
| 79 |
for( int d=0 ; d<Dim ; d++ ) cIdx[d] = center; |
| 80 |
|
| 81 |
// First set the stencils for the current depth |
| 82 |
{ |
| 83 |
// The center stencil |
| 84 |
WindowLoop< Dim >::Run |
| 85 |
( |
| 86 |
ZeroUIntPack< Dim >() , CenterSizes() , |
| 87 |
[&]( int d , int i ){ fIdx[d] = center + i - LeftCenterRadii[d]; } , |
| 88 |
[&]( CumulativeDerivativeValues< double , Dim , PointD >& p ){ p = _centerValues( depth , fIdx , cIdx , false ); } , |
| 89 |
stencilData[depth].ccCenterStencil() |
| 90 |
); |
| 91 |
// The corner stencil |
| 92 |
for( int c=0 ; c<(1<<Dim) ; c++ ) |
| 93 |
WindowLoop< Dim >::Run |
| 94 |
( |
| 95 |
ZeroUIntPack< Dim >() , CornerSizes() , |
| 96 |
[&]( int d , int i ){ fIdx[d] = center + i - LeftCornerRadii[d]; } , |
| 97 |
[&]( CumulativeDerivativeValues< double , Dim , PointD >& p ){ p = _cornerValues( depth , fIdx , cIdx , c , false ); } , |
| 98 |
stencilData[depth].ccCornerStencil[c]() |
| 99 |
); |
| 100 |
// The boundary corner stencil |
| 101 |
for( int c=0 ; c<(1<<Dim) ; c++ ) |
| 102 |
WindowLoop< Dim >::Run |
| 103 |
( |
| 104 |
ZeroUIntPack< Dim >() , BCornerSizes() , |
| 105 |
[&]( int d , int i ){ fIdx[d] = center + i - LeftBCornerRadii[d]; } , |
| 106 |
[&]( CumulativeDerivativeValues< double , Dim , PointD >& p ){ p = _cornerValues( depth , fIdx , cIdx , c , false ); } , |
| 107 |
stencilData[depth].ccBCornerStencil[c]() |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
// Now set the stencils for the parents |
| 112 |
for( int c=0 ; c<(1<<Dim) ; c++ ) |
| 113 |
{ |
| 114 |
int cIdx[Dim] , fIdx[Dim]; |
| 115 |
for( int d=0 ; d<Dim ; d++ ) cIdx[d] = center + ( (c>>d) & 1 ); |
| 116 |
|
| 117 |
// The center stencil |
| 118 |
WindowLoop< Dim >::Run |
| 119 |
( |
| 120 |
ZeroUIntPack< Dim >() , CenterSizes() , |
| 121 |
[&]( int d , int i ){ fIdx[d] = center/2 + i - LeftCenterRadii[d]; } , |
| 122 |
[&]( CumulativeDerivativeValues< double , Dim , PointD >& p ){ p = _centerValues( depth , fIdx , cIdx , true ); } , |
| 123 |
stencilData[depth].pcCenterStencils[c]() |
| 124 |
); |
| 125 |
// The corner stencil |
| 126 |
for( int cc=0 ; cc<(1<<Dim) ; cc++ ) |
| 127 |
WindowLoop< Dim >::Run |
| 128 |
( |
| 129 |
ZeroUIntPack< Dim >() , CornerSizes() , |
| 130 |
[&]( int d , int i ){ fIdx[d] = center/2 + i - LeftCornerRadii[d]; } , |
| 131 |
[&]( CumulativeDerivativeValues< double , Dim , PointD >& p ){ p = _cornerValues( depth , fIdx , cIdx , cc , true ); } , |
| 132 |
stencilData[depth].pcCornerStencils[c][cc]() |
| 133 |
); |
| 134 |
// The boundary corner stencil |
| 135 |
for( int cc=0 ; cc<(1<<Dim) ; cc++ ) |
| 136 |
WindowLoop< Dim >::Run |
| 137 |
( |
| 138 |
ZeroUIntPack< Dim >() , BCornerSizes() , |
| 139 |
[&]( int d , int i ){ fIdx[d] = center/2 + i - LeftBCornerRadii[d]; } , |
| 140 |
[&]( CumulativeDerivativeValues< double , Dim , PointD >& p ){ p = _cornerValues( depth , fIdx , cIdx , cc , true ); } , |
| 141 |
stencilData[depth].pcBCornerStencils[c][cc]() |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
if( _pointEvaluator ) delete _pointEvaluator; |
| 146 |
_pointEvaluator = new PointEvaluator< UIntPack< FEMSigs ... > , IsotropicUIntPack< Dim , PointD > >( maxDepth ); |
| 147 |
} |
| 148 |
|
| 149 |
template< unsigned int Dim , class Real > |
| 150 |
template< class V , unsigned int _PointD , unsigned int ... FEMSigs , unsigned int PointD > |
| 151 |
CumulativeDerivativeValues< V , Dim , _PointD > FEMTree< Dim , Real >::_getValues( const ConstPointSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& neighborKey , const FEMTreeNode* node , Point< Real , Dim > p , ConstPointer( V ) solution , ConstPointer( V ) coarseSolution , const _Evaluator< UIntPack< FEMSigs ... > , PointD >& evaluator , int maxDepth ) const |
| 152 |
{ |
| 153 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportSize ... > SupportSizes; |
| 154 |
|
| 155 |
if( IsActiveNode< Dim >( node->children ) && _localDepth( node->children )<=maxDepth ) MK_WARN( "getValue assumes leaf node" ); |
| 156 |
CumulativeDerivativeValues< V , Dim , _PointD > values; |
| 157 |
|
| 158 |
PointEvaluatorState< UIntPack< FEMSigs ... > , IsotropicUIntPack< Dim , _PointD > > state; |
| 159 |
|
| 160 |
#ifdef SHOW_WARNINGS |
| 161 |
#pragma message ( "[WARNING] Nudging evaluation point into the interior" ) |
| 162 |
#endif // SHOW_WARNINGS |
| 163 |
for( int dd=0 ; dd<Dim ; dd++ ) |
| 164 |
{ |
| 165 |
if ( p[dd]==0 ) p[dd] = (Real)(0.+1e-6); |
| 166 |
else if( p[dd]==1 ) p[dd] = (Real)(1.-1e-6); |
| 167 |
} |
| 168 |
auto AddToValues = [&]( const typename FEMTreeNode::template ConstNeighbors< SupportSizes >& neighbors , ConstPointer( V ) coefficients ) |
| 169 |
{ |
| 170 |
ConstPointer( FEMTreeNode * const ) nodes = neighbors.neighbors().data; |
| 171 |
for( unsigned int i=0 ; i<WindowSize< SupportSizes >::Size ; i++ ) if( _isValidFEM1Node( nodes[i] ) ) |
| 172 |
{ |
| 173 |
LocalDepth d ; LocalOffset off ; _localDepthAndOffset( nodes[i] , d , off ); |
| 174 |
CumulativeDerivativeValues< Real , Dim , _PointD > _values = state.template dValues< Real , CumulativeDerivatives< Dim , _PointD > >( off ); |
| 175 |
for( int d=0 ; d<CumulativeDerivatives< Dim , _PointD >::Size ; d++ ) values[d] += coefficients[ nodes[i]->nodeData.nodeIndex ] * _values[d]; |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
LocalDepth depth = _localDepth( node ); |
| 180 |
while( GetGhostFlag< Dim >( node ) ) node = node->parent , depth--; |
| 181 |
|
| 182 |
{ |
| 183 |
evaluator._pointEvaluator->initEvaluationState( p , depth , state ); |
| 184 |
AddToValues( neighborKey.neighbors[ node->depth() ] , solution ); |
| 185 |
if( depth>0 ) |
| 186 |
{ |
| 187 |
evaluator._pointEvaluator->initEvaluationState( p , depth-1 , state ); |
| 188 |
AddToValues( neighborKey.neighbors[ node->parent->depth() ] , coarseSolution ); |
| 189 |
} |
| 190 |
} |
| 191 |
// If there could be finer neighbors whose support overlaps the point |
| 192 |
if( depth<_maxDepth ) |
| 193 |
{ |
| 194 |
typename FEMTreeNode::template ConstNeighbors< SupportSizes > cNeighbors; |
| 195 |
int cIdx = 0; |
| 196 |
Point< Real , Dim > c ; Real w; |
| 197 |
_centerAndWidth( node , c , w ); |
| 198 |
for( int d=0 ; d<Dim ; d++ ) if( p[d]>c[d] ) cIdx |= (1<<d); |
| 199 |
if( neighborKey.getChildNeighbors( cIdx , node->depth() , cNeighbors ) ) |
| 200 |
{ |
| 201 |
evaluator._pointEvaluator->initEvaluationState( p , depth+1 , state ); |
| 202 |
AddToValues( cNeighbors , solution ); |
| 203 |
} |
| 204 |
} |
| 205 |
return values; |
| 206 |
} |
| 207 |
template< unsigned int Dim , class Real > |
| 208 |
template< class V , unsigned int _PointD , unsigned int ... FEMSigs , unsigned int PointD > |
| 209 |
CumulativeDerivativeValues< V , Dim , _PointD > FEMTree< Dim , Real >::_getCenterValues( const ConstPointSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& neighborKey , const FEMTreeNode* node , ConstPointer( V ) solution , ConstPointer( V ) coarseSolution , const _Evaluator< UIntPack< FEMSigs ... > , PointD >& evaluator , int maxDepth , bool isInterior ) const |
| 210 |
{ |
| 211 |
if( IsActiveNode< Dim >( node->children ) && _localDepth( node->children )<=maxDepth ) MK_THROW( "getCenterValues assumes leaf node" ); |
| 212 |
typedef _Evaluator< UIntPack< FEMSigs ... > , PointD > _Evaluator; |
| 213 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportSize ... > SupportSizes; |
| 214 |
static const unsigned int supportSizes[] = { BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportSize ... }; |
| 215 |
|
| 216 |
if( IsActiveNode< Dim >( node->children ) && _localDepth( node->children )<=maxDepth ) MK_THROW( "getCenterValue assumes leaf node" ); |
| 217 |
CumulativeDerivativeValues< V , Dim , _PointD > values; |
| 218 |
|
| 219 |
LocalDepth d ; LocalOffset cIdx; |
| 220 |
_localDepthAndOffset( node , d , cIdx ); |
| 221 |
|
| 222 |
static const int corner = (1<<Dim)-1; |
| 223 |
|
| 224 |
static const CornerLoopData< BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportSize ... > loopData; |
| 225 |
auto AddToValuesInterior = [&] |
| 226 |
( |
| 227 |
unsigned int size , const unsigned int* indices , |
| 228 |
const typename FEMTreeNode::template ConstNeighbors< SupportSizes >& neighbors , |
| 229 |
const typename _Evaluator::CornerStencil& cornerStencil , |
| 230 |
ConstPointer( V ) coefficients |
| 231 |
) |
| 232 |
{ |
| 233 |
ConstPointer( FEMTreeNode * const ) nodes = neighbors.neighbors().data; |
| 234 |
ConstPointer( CumulativeDerivativeValues< double , Dim , PointD > ) _values = cornerStencil().data; |
| 235 |
for( unsigned int i=0 ; i<size ; i++ ) |
| 236 |
{ |
| 237 |
int idx = indices[i]; |
| 238 |
if( IsActiveNode< Dim >( nodes[ idx ] ) ) for( int d=0 ; d<CumulativeDerivatives< Dim , _PointD >::Size ; d++ ) values[d] += coefficients[ nodes[ idx ]->nodeData.nodeIndex ] * (Real)_values[ idx ][d]; |
| 239 |
} |
| 240 |
}; |
| 241 |
auto AddToValuesExterior = [&] |
| 242 |
( |
| 243 |
unsigned int size , const unsigned int* indices , |
| 244 |
LocalDepth d , LocalOffset cIdx , |
| 245 |
const typename FEMTreeNode::template ConstNeighbors< SupportSizes >& neighbors , |
| 246 |
ConstPointer( V ) coefficients , bool parent |
| 247 |
) |
| 248 |
{ |
| 249 |
ConstPointer( FEMTreeNode * const ) nodes = neighbors.neighbors().data; |
| 250 |
for( unsigned int i=0 ; i<size ; i++ ) |
| 251 |
{ |
| 252 |
int idx = indices[i]; |
| 253 |
if( IsActiveNode< Dim >( nodes[ idx ] ) ) |
| 254 |
{ |
| 255 |
LocalDepth _d ; LocalOffset fIdx; |
| 256 |
this->_localDepthAndOffset( nodes[idx] , _d , fIdx ); |
| 257 |
CumulativeDerivativeValues< double , Dim , _PointD > _values = evaluator.template _cornerValues< _PointD >( d , fIdx , cIdx , corner , parent ); |
| 258 |
for( int d=0 ; d<CumulativeDerivatives< Dim , _PointD >::Size ; d++ ) values[d] += coefficients[ nodes[ idx ]->nodeData.nodeIndex ] * (Real)_values[d]; |
| 259 |
} |
| 260 |
} |
| 261 |
}; |
| 262 |
|
| 263 |
if( isInterior ) |
| 264 |
{ |
| 265 |
auto AddToValues = [&] |
| 266 |
( |
| 267 |
const typename FEMTreeNode::template ConstNeighbors< SupportSizes >& neighbors , |
| 268 |
const typename _Evaluator::CenterStencil& centerStencil , |
| 269 |
ConstPointer( V ) coefficients |
| 270 |
) |
| 271 |
{ |
| 272 |
ConstPointer( FEMTreeNode * const ) nodes = neighbors.neighbors().data; |
| 273 |
ConstPointer( CumulativeDerivativeValues< double , Dim , PointD > ) _values = centerStencil.data; |
| 274 |
for( int i=0 ; i<WindowSize< SupportSizes >::Size ; i++ ) if( _isValidFEM1Node( nodes[i] ) ) |
| 275 |
for( int d=0 ; d<CumulativeDerivatives< Dim , _PointD >::Size ; d++ ) values[d] += coefficients[ nodes[i]->nodeData.nodeIndex ] * (Real)_values[i][d]; |
| 276 |
}; |
| 277 |
AddToValues( neighborKey.neighbors[ node->depth() ] , evaluator.stencilData[d].ccCenterStencil , solution ); |
| 278 |
if( d>0 ) |
| 279 |
{ |
| 280 |
int _corner = int( node - node->parent->children ); |
| 281 |
AddToValues( neighborKey.neighbors[ node->parent->depth() ] , evaluator.stencilData[d].pcCenterStencils[_corner] , coarseSolution ); |
| 282 |
} |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
auto AddToValues = [&]( const typename FEMTreeNode::template ConstNeighbors< SupportSizes >& neighbors , ConstPointer( V ) coefficients , bool parentChild ) |
| 287 |
{ |
| 288 |
ConstPointer( FEMTreeNode * const ) nodes = neighbors.neighbors().data; |
| 289 |
for( int i=0 ; i<WindowSize< SupportSizes >::Size ; i++ ) if( _isValidFEM1Node( nodes[i] ) ) |
| 290 |
{ |
| 291 |
LocalDepth _d ; LocalOffset fIdx; |
| 292 |
_localDepthAndOffset( nodes[i] , _d , fIdx ); |
| 293 |
const CumulativeDerivativeValues< double , Dim , _PointD >& _values = evaluator.template _centerValues< _PointD >( d , fIdx , cIdx , parentChild ); |
| 294 |
for( int d=0 ; d<CumulativeDerivatives< Dim , _PointD >::Size ; d++ ) values[d] += coefficients[ nodes[i]->nodeData.nodeIndex ] * (Real)_values[d]; |
| 295 |
} |
| 296 |
}; |
| 297 |
|
| 298 |
AddToValues( neighborKey.neighbors[ node->depth() ] , solution , false ); |
| 299 |
if( d>0 ) AddToValues( neighborKey.neighbors[ node->parent->depth() ] , coarseSolution , true ); |
| 300 |
} |
| 301 |
// If there could be finer neighbors whose support overlaps the point |
| 302 |
if( d<_maxDepth ) |
| 303 |
{ |
| 304 |
typename FEMTreeNode::template ConstNeighbors< SupportSizes > cNeighbors; |
| 305 |
if( neighborKey.getChildNeighbors( 0 , node->depth() , cNeighbors ) ) |
| 306 |
{ |
| 307 |
if( isInterior ) AddToValuesInterior( loopData.ccSize[corner] , loopData.ccIndices[corner] , cNeighbors , evaluator.stencilData[d+1].ccCornerStencil[corner] , solution ); |
| 308 |
else |
| 309 |
{ |
| 310 |
LocalDepth _d=d+1 ; LocalOffset _cIdx; |
| 311 |
for( int d=0 ; d<Dim ; d++ ) _cIdx[d] = cIdx[d]<<1; |
| 312 |
AddToValuesExterior( loopData.ccSize[corner] , loopData.ccIndices[corner] , _d , _cIdx , cNeighbors , solution , false ); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
return values; |
| 317 |
} |
| 318 |
|
| 319 |
template< unsigned int Dim , class Real > |
| 320 |
template< class V , unsigned int _PointD , unsigned int ... FEMSigs , unsigned int PointD > |
| 321 |
CumulativeDerivativeValues< V , Dim , _PointD > FEMTree< Dim , Real >::_getCornerValues( const ConstPointSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& neighborKey , const FEMTreeNode* node , int corner , ConstPointer( V ) solution , ConstPointer( V ) coarseSolution , const _Evaluator< UIntPack< FEMSigs ... > , PointD >& evaluator , int maxDepth , bool isInterior ) const |
| 322 |
{ |
| 323 |
if( IsActiveNode< Dim >( node->children ) && _localDepth( node->children )<=maxDepth ) MK_WARN( "getValue assumes leaf node" ); |
| 324 |
typedef _Evaluator< UIntPack< FEMSigs ... > , PointD > _Evaluator; |
| 325 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportSize ... > SupportSizes; |
| 326 |
static const unsigned int supportSizes[] = { BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportSize ... }; |
| 327 |
|
| 328 |
CumulativeDerivativeValues< V , Dim , _PointD > values; |
| 329 |
LocalDepth d ; LocalOffset cIdx; |
| 330 |
_localDepthAndOffset( node , d , cIdx ); |
| 331 |
|
| 332 |
static const CornerLoopData< BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::SupportSize ... > loopData; |
| 333 |
{ |
| 334 |
auto AddToValuesInterior = [&] |
| 335 |
( |
| 336 |
unsigned int size , const unsigned int* indices , |
| 337 |
const typename FEMTreeNode::template ConstNeighbors< SupportSizes >& neighbors , |
| 338 |
const typename _Evaluator::CornerStencil& cornerStencil , |
| 339 |
ConstPointer( V ) coefficients |
| 340 |
) |
| 341 |
{ |
| 342 |
ConstPointer( FEMTreeNode * const ) nodes = neighbors.neighbors().data; |
| 343 |
ConstPointer( CumulativeDerivativeValues< double , Dim , PointD > ) _values = cornerStencil().data; |
| 344 |
for( unsigned int i=0 ; i<size ; i++ ) |
| 345 |
{ |
| 346 |
int idx = indices[i]; |
| 347 |
if( IsActiveNode< Dim >( nodes[ idx ] ) ) for( int d=0 ; d<CumulativeDerivatives< Dim , _PointD >::Size ; d++ ) values[d] += coefficients[ nodes[ idx ]->nodeData.nodeIndex ] * (Real)_values[ idx ][d]; |
| 348 |
} |
| 349 |
}; |
| 350 |
auto AddToValuesExterior = [&] |
| 351 |
( |
| 352 |
unsigned int size , const unsigned int* indices , |
| 353 |
LocalDepth d , LocalOffset cIdx , |
| 354 |
const typename FEMTreeNode::template ConstNeighbors< SupportSizes >& neighbors , |
| 355 |
ConstPointer( V ) coefficients , bool parent |
| 356 |
) |
| 357 |
{ |
| 358 |
ConstPointer( FEMTreeNode * const ) nodes = neighbors.neighbors().data; |
| 359 |
for( unsigned int i=0 ; i<size ; i++ ) |
| 360 |
{ |
| 361 |
int idx = indices[i]; |
| 362 |
if( IsActiveNode< Dim >( nodes[ idx ] ) ) |
| 363 |
{ |
| 364 |
LocalDepth _d ; LocalOffset fIdx; |
| 365 |
this->_localDepthAndOffset( nodes[idx] , _d , fIdx ); |
| 366 |
CumulativeDerivativeValues< double , Dim , _PointD > _values = evaluator.template _cornerValues< _PointD >( d , fIdx , cIdx , corner , parent ); |
| 367 |
for( int d=0 ; d<CumulativeDerivatives< Dim , _PointD >::Size ; d++ ) values[d] += coefficients[ nodes[ idx ]->nodeData.nodeIndex ] * (Real)_values[d]; |
| 368 |
} |
| 369 |
} |
| 370 |
}; |
| 371 |
if( isInterior ) AddToValuesInterior( loopData.ccSize[corner] , loopData.ccIndices[corner] , neighborKey.neighbors[ node->depth() ] , evaluator.stencilData[d].ccCornerStencil[corner] , solution ); |
| 372 |
else AddToValuesExterior( loopData.ccSize[corner] , loopData.ccIndices[corner] , d , cIdx , neighborKey.neighbors[ node->depth() ] , solution , false ); |
| 373 |
if( d>0 ) |
| 374 |
{ |
| 375 |
int _corner = int( node - node->parent->children ); |
| 376 |
if( isInterior ) AddToValuesInterior( loopData.pcSize[corner][_corner] , loopData.pcIndices[corner][_corner] , neighborKey.neighbors[ node->parent->depth() ] , evaluator.stencilData[d].pcCornerStencils[_corner][corner] , coarseSolution ); |
| 377 |
else AddToValuesExterior( loopData.pcSize[corner][_corner] , loopData.pcIndices[corner][_corner] , d , cIdx , neighborKey.neighbors[ node->parent->depth() ] , coarseSolution , true ); |
| 378 |
} |
| 379 |
// If there could be finer neighbors whose support overlaps the point |
| 380 |
if( d<_maxDepth ) |
| 381 |
{ |
| 382 |
typename FEMTreeNode::template ConstNeighbors< SupportSizes > cNeighbors; |
| 383 |
if( neighborKey.getChildNeighbors( corner , node->depth() , cNeighbors ) ) |
| 384 |
{ |
| 385 |
if( isInterior ) AddToValuesInterior( loopData.ccSize[corner] , loopData.ccIndices[corner] , cNeighbors , evaluator.stencilData[d+1].ccCornerStencil[corner] , solution ); |
| 386 |
else |
| 387 |
{ |
| 388 |
LocalDepth _d=d+1 ; LocalOffset _cIdx; |
| 389 |
for( int d=0 ; d<Dim ; d++ ) _cIdx[d] = (cIdx[d]<<1) | ( (corner&(1<<d)) ? 1 : 0 ); |
| 390 |
AddToValuesExterior( loopData.ccSize[corner] , loopData.ccIndices[corner] , _d , _cIdx , cNeighbors , solution , false ); |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
return values; |
| 395 |
} |
| 396 |
} |
| 397 |
template< unsigned int Dim , class Real > |
| 398 |
template< class V , unsigned int _PointD , unsigned int ... FEMSigs , unsigned int PointD > |
| 399 |
CumulativeDerivativeValues< V , Dim , _PointD > FEMTree< Dim , Real >::_getCornerValues( const ConstCornerSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& neighborKey , const FEMTreeNode* node , int corner , ConstPointer( V ) solution , ConstPointer( V ) coarseSolution , const _Evaluator< UIntPack< FEMSigs ... > , PointD >& evaluator , int maxDepth , bool isInterior ) const |
| 400 |
{ |
| 401 |
typedef _Evaluator< UIntPack< FEMSigs ... > , PointD > _Evaluator; |
| 402 |
|
| 403 |
typedef UIntPack< ( BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::BCornerSize + 1 ) ... > BCornerSizes; |
| 404 |
static const unsigned int bCornerSizes[] = { ( BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::BCornerSize + 1 ) ... }; |
| 405 |
CumulativeDerivativeValues< V , Dim , _PointD > values; |
| 406 |
LocalDepth d ; LocalOffset cIdx; |
| 407 |
_localDepthAndOffset( node , d , cIdx ); |
| 408 |
|
| 409 |
static const CornerLoopData< ( BSplineSupportSizes< FEMSignature< FEMSigs >::Degree >::BCornerSize + 1 ) ... > loopData; |
| 410 |
|
| 411 |
{ |
| 412 |
auto AddToValuesInterior = [&] |
| 413 |
( |
| 414 |
unsigned int size , const unsigned int* indices , |
| 415 |
const typename FEMTreeNode::template ConstNeighbors< BCornerSizes >& neighbors , |
| 416 |
const typename _Evaluator::BCornerStencil& cornerStencil , |
| 417 |
ConstPointer( V ) coefficients |
| 418 |
) |
| 419 |
{ |
| 420 |
ConstPointer( FEMTreeNode * const ) nodes = neighbors.neighbors().data; |
| 421 |
ConstPointer( CumulativeDerivativeValues< double , Dim , PointD > ) _values = cornerStencil().data; |
| 422 |
for( unsigned int i=0 ; i<size ; i++ ) |
| 423 |
{ |
| 424 |
int idx = indices[i]; |
| 425 |
if( IsActiveNode< Dim >( nodes[ idx ] ) ) for( int d=0 ; d<CumulativeDerivatives< Dim , _PointD >::Size ; d++ ) values[d] += coefficients[ nodes[ idx ]->nodeData.nodeIndex ] * (Real)_values[ idx ][d]; |
| 426 |
} |
| 427 |
}; |
| 428 |
auto AddToValuesExterior = [&] |
| 429 |
( |
| 430 |
unsigned int size , const unsigned int* indices , |
| 431 |
LocalDepth d , LocalOffset cIdx , |
| 432 |
const typename FEMTreeNode::template ConstNeighbors< BCornerSizes >& neighbors , |
| 433 |
ConstPointer( V ) coefficients , bool parent |
| 434 |
) |
| 435 |
{ |
| 436 |
ConstPointer( FEMTreeNode * const ) nodes = neighbors.neighbors().data; |
| 437 |
for( unsigned int i=0 ; i<size ; i++ ) |
| 438 |
{ |
| 439 |
int idx = indices[i]; |
| 440 |
if( IsActiveNode< Dim >( nodes[ idx ] ) ) |
| 441 |
{ |
| 442 |
LocalDepth _d ; LocalOffset fIdx; |
| 443 |
_localDepthAndOffset( nodes[idx] , _d , fIdx ); |
| 444 |
CumulativeDerivativeValues< double , Dim , _PointD > _values = evaluator.template _cornerValues< _PointD >( d , fIdx , cIdx , corner , parent ); |
| 445 |
for( int d=0 ; d<CumulativeDerivatives< Dim , _PointD >::Size ; d++ ) values[d] += coefficients[ nodes[idx]->nodeData.nodeIndex ] * (Real)_values[d]; |
| 446 |
} |
| 447 |
} |
| 448 |
}; |
| 449 |
if( isInterior ) AddToValuesInterior( loopData.ccSize[corner] , loopData.ccIndices[corner] , neighborKey.neighbors[ node->depth() ] , evaluator.stencilData[d].ccBCornerStencil[corner] , solution ); |
| 450 |
else AddToValuesExterior( loopData.ccSize[corner] , loopData.ccIndices[corner] , d , cIdx , neighborKey.neighbors[ node->depth() ] , solution , false ); |
| 451 |
if( d>0 ) |
| 452 |
{ |
| 453 |
int _corner = int( node - node->parent->children ); |
| 454 |
if( isInterior ) AddToValuesInterior( loopData.pcSize[corner][_corner] , loopData.pcIndices[corner][_corner] , neighborKey.neighbors[ node->parent->depth() ] , evaluator.stencilData[d].pcBCornerStencils[_corner][corner] , coarseSolution ); |
| 455 |
else AddToValuesExterior( loopData.pcSize[corner][_corner] , loopData.pcIndices[corner][_corner] , d , cIdx , neighborKey.neighbors[ node->parent->depth() ] , coarseSolution , true ); |
| 456 |
} |
| 457 |
// If there could be finer neighbors whose support overlaps the point |
| 458 |
if( d<_maxDepth ) |
| 459 |
{ |
| 460 |
typename FEMTreeNode::template ConstNeighbors< BCornerSizes > cNeighbors; |
| 461 |
if( neighborKey.getChildNeighbors( corner , node->depth() , cNeighbors ) ) |
| 462 |
{ |
| 463 |
if( isInterior ) AddToValuesInterior( loopData.ccSize[corner] , loopData.ccIndices[corner] , cNeighbors , evaluator.stencilData[d+1].ccBCornerStencil[corner] , solution ); |
| 464 |
else |
| 465 |
{ |
| 466 |
LocalDepth _d=d+1 ; LocalOffset _cIdx; |
| 467 |
for( int d=0 ; d<Dim ; d++ ) _cIdx[d] = (cIdx[d]<<1) | ( (corner&(1<<d)) ? 1 : 0 ); |
| 468 |
AddToValuesExterior( loopData.ccSize[corner] , loopData.ccIndices[corner] , _d , _cIdx , cNeighbors , solution , false ); |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
return values; |
| 474 |
} |
| 475 |
//////////////////////////// |
| 476 |
// MultiThreadedEvaluator // |
| 477 |
//////////////////////////// |
| 478 |
template< unsigned int Dim , class Real > |
| 479 |
template< unsigned int ... FEMSigs , unsigned int PointD , typename T > |
| 480 |
FEMTree< Dim , Real >::_MultiThreadedEvaluator< UIntPack< FEMSigs ... > , PointD , T >::_MultiThreadedEvaluator( const FEMTree< Dim , Real >* tree , const DenseNodeData< T , FEMSignatures >& coefficients , int threads ) : _coefficients( coefficients ) , _tree( tree ) |
| 481 |
{ |
| 482 |
tree->_setFEM1ValidityFlags( UIntPack< FEMSigs ... >() ); |
| 483 |
_threads = std::max< int >( 1 , threads ); |
| 484 |
_pointNeighborKeys.resize( _threads ); |
| 485 |
_cornerNeighborKeys.resize( _threads ); |
| 486 |
_coarseCoefficients = _tree->template coarseCoefficients< T >( _coefficients ); |
| 487 |
_evaluator.set( _tree->_maxDepth ); |
| 488 |
for( int t=0 ; t<_pointNeighborKeys.size() ; t++ ) _pointNeighborKeys[t].set( tree->_localToGlobal( _tree->_maxDepth ) ); |
| 489 |
for( int t=0 ; t<_cornerNeighborKeys.size() ; t++ ) _cornerNeighborKeys[t].set( tree->_localToGlobal( _tree->_maxDepth ) ); |
| 490 |
} |
| 491 |
|
| 492 |
template< unsigned int Dim , class Real > |
| 493 |
template< unsigned int ... FEMSigs , unsigned int PointD , typename T > |
| 494 |
template< unsigned int _PointD > |
| 495 |
CumulativeDerivativeValues< T , Dim , _PointD > FEMTree< Dim , Real >::_MultiThreadedEvaluator< UIntPack< FEMSigs ... > , PointD , T >::values( Point< Real , Dim > p , int thread , const FEMTreeNode* node ) |
| 496 |
{ |
| 497 |
if( _PointD>PointD ) MK_THROW( "Evaluating more derivatives than available: " , _PointD , " <= " , PointD ); |
| 498 |
if( !node ) node = _tree->leaf( p ); |
| 499 |
ConstPointSupportKey< FEMDegrees >& nKey = _pointNeighborKeys[thread]; |
| 500 |
nKey.getNeighbors( node ); |
| 501 |
return _tree->template _getValues< T , _PointD >( nKey , node , p , _coefficients() , _coarseCoefficients() , _evaluator , _tree->_maxDepth ); |
| 502 |
} |
| 503 |
|
| 504 |
template< unsigned int Dim , class Real > |
| 505 |
template< unsigned int ... FEMSigs , unsigned int PointD , typename T > |
| 506 |
template< unsigned int _PointD > |
| 507 |
CumulativeDerivativeValues< T , Dim , _PointD > FEMTree< Dim , Real >::_MultiThreadedEvaluator< UIntPack< FEMSigs ... > , PointD , T >::centerValues( const FEMTreeNode* node , int thread ) |
| 508 |
{ |
| 509 |
if( _PointD>PointD ) MK_THROW( "Evaluating more derivatives than available: " , _PointD, " <= " , PointD ); |
| 510 |
ConstPointSupportKey< FEMDegrees >& nKey = _pointNeighborKeys[thread]; |
| 511 |
nKey.getNeighbors( node ); |
| 512 |
LocalDepth d ; LocalOffset off; |
| 513 |
_tree->_localDepthAndOffset( node->parent , d , off ); |
| 514 |
return _tree->template _getCenterValues< T , _PointD >( nKey , node , _coefficients() , _coarseCoefficients() , _evaluator , _tree->_maxDepth , BaseFEMIntegrator::IsInteriorlySupported( UIntPack< FEMSigs ... >() , d , off ) ); |
| 515 |
} |
| 516 |
template< unsigned int Dim , class Real > |
| 517 |
template< unsigned int ... FEMSigs , unsigned int PointD , typename T > |
| 518 |
template< unsigned int _PointD > |
| 519 |
CumulativeDerivativeValues< T , Dim , _PointD > FEMTree< Dim , Real >::_MultiThreadedEvaluator< UIntPack< FEMSigs ... > , PointD , T >::cornerValues( const FEMTreeNode* node , int corner , int thread ) |
| 520 |
{ |
| 521 |
if( _PointD>PointD ) MK_THROW( "Evaluating more derivatives than available: " , _PointD , " <= " , PointD ); |
| 522 |
ConstCornerSupportKey< FEMDegrees >& nKey = _cornerNeighborKeys[thread]; |
| 523 |
nKey.getNeighbors( node ); |
| 524 |
LocalDepth d ; LocalOffset off; |
| 525 |
_tree->_localDepthAndOffset( node->parent , d , off ); |
| 526 |
return _tree->template _getCornerValues< T , _PointD >( nKey , node , corner , _coefficients() , _coarseCoefficients() , _evaluator , _tree->_maxDepth , BaseFEMIntegrator::IsInteriorlySupported( UIntPack< FEMSigs ... >() , d , off ) ); |
| 527 |
} |
| 528 |
|
| 529 |
////////////////////////////////// |
| 530 |
// MultiThreadedSparseEvaluator // |
| 531 |
////////////////////////////////// |
| 532 |
template< unsigned int Dim , class Real > |
| 533 |
template< unsigned int ... FEMSigs , typename T > |
| 534 |
FEMTree< Dim , Real >::MultiThreadedSparseEvaluator< UIntPack< FEMSigs ... > , T >::MultiThreadedSparseEvaluator( const FEMTree< Dim , Real >* tree , const SparseNodeData< T , FEMSignatures >& coefficients , int threads ) : _coefficients( coefficients ) , _tree( tree ) |
| 535 |
{ |
| 536 |
tree->_setFEM1ValidityFlags( UIntPack< FEMSigs ... >() ); |
| 537 |
_threads = std::max< int >( 1 , threads ); |
| 538 |
_pointNeighborKeys.resize( _threads ); |
| 539 |
_pointEvaluator = new typename FEMIntegrator::template PointEvaluator< UIntPack< FEMSigs ... > , ZeroUIntPack< Dim > >( _tree->_maxDepth ); |
| 540 |
for( int t=0 ; t<_pointNeighborKeys.size() ; t++ ) _pointNeighborKeys[t].set( tree->_localToGlobal( _tree->_maxDepth ) ); |
| 541 |
} |
| 542 |
|
| 543 |
template< unsigned int Dim , class Real > |
| 544 |
template< unsigned int ... FEMSigs , typename T > |
| 545 |
void FEMTree< Dim , Real >::MultiThreadedSparseEvaluator< UIntPack< FEMSigs ... > , T >::addValue( Point< Real , Dim > p , T &t , int thread , const FEMTreeNode *node ) |
| 546 |
{ |
| 547 |
if( !node ) node = _tree->leaf( p ); |
| 548 |
ConstPointSupportKey< FEMDegrees >& nKey = _pointNeighborKeys[thread]; |
| 549 |
nKey.getNeighbors( node ); |
| 550 |
_tree->template _addEvaluation< T , SparseNodeData< T , FEMSignatures > , 0 >( _coefficients , p , _tree->_globalToLocal( node->depth() ) , *_pointEvaluator , nKey , t ); |
| 551 |
} |
| 552 |
|
| 553 |
template< unsigned int Dim , class Real > |
| 554 |
template< unsigned int ... FEMSigs , typename T > |
| 555 |
template< typename AccumulationFunctor/*=std::function< void ( const T & , Real s ) > */ > |
| 556 |
void FEMTree< Dim , Real >::MultiThreadedSparseEvaluator< UIntPack< FEMSigs ... > , T >::accumulate( Point< Real , Dim > p , AccumulationFunctor &Accumulate , int thread , const FEMTreeNode *node ) |
| 557 |
{ |
| 558 |
if( !node ) node = _tree->leaf( p ); |
| 559 |
ConstPointSupportKey< FEMDegrees >& nKey = _pointNeighborKeys[thread]; |
| 560 |
nKey.getNeighbors( node ); |
| 561 |
_tree->template _accumulate< T , SparseNodeData< T , FEMSignatures > , 0 , AccumulationFunctor >( _coefficients , p , _tree->_globalToLocal( node->depth() ) , *_pointEvaluator , nKey , Accumulate ); |
| 562 |
} |
| 563 |
|
| 564 |
template< unsigned int Dim , class Real > |
| 565 |
template< class V , class Coefficients , unsigned int D , unsigned int ... DataSigs > |
| 566 |
void FEMTree< Dim , Real >::_addEvaluation( const Coefficients& coefficients , Point< Real , Dim > p , const PointEvaluator< UIntPack< DataSigs ... > , IsotropicUIntPack< Dim , D > >& pointEvaluator , const ConstPointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , V &value ) const |
| 567 |
{ |
| 568 |
auto AF = [&value]( const V &w , Real s ){ value += w * s; }; |
| 569 |
_accumulate< V , Coefficients , D , decltype(AF) , DataSigs ... >( coefficients , p , pointEvaluator , dataKey , AF ); |
| 570 |
} |
| 571 |
|
| 572 |
template< unsigned int Dim , class Real > |
| 573 |
template< class V , class Coefficients , unsigned int D , unsigned int ... DataSigs > |
| 574 |
void FEMTree< Dim , Real >::_addEvaluation( const Coefficients& coefficients , Point< Real , Dim > p , LocalDepth pointDepth , const PointEvaluator< UIntPack< DataSigs ... > , IsotropicUIntPack< Dim , D > >& pointEvaluator , const ConstPointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , V &value ) const |
| 575 |
{ |
| 576 |
auto AF = [&value]( const V &w , Real s ){ value += w * s; }; |
| 577 |
_accumulate< V , Coefficients , D , decltype(AF) , DataSigs... >( coefficients , p , pointDepth , pointEvaluator , dataKey , AF ); |
| 578 |
} |
| 579 |
|
| 580 |
template< unsigned int Dim , class Real > |
| 581 |
template< typename V , typename Coefficients , unsigned int D , typename AccumulationFunctor , unsigned int ... DataSigs > |
| 582 |
void FEMTree< Dim , Real >::_accumulate( const Coefficients& coefficients , Point< Real , Dim > p , const PointEvaluator< UIntPack< DataSigs ... > , IsotropicUIntPack< Dim , D > >& pointEvaluator , const ConstPointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , AccumulationFunctor &Accumulate ) const |
| 583 |
{ |
| 584 |
_accumulate< V , Coefficients , D , AccumulationFunctor , DataSigs ... >( coefficients , p , _globalToLocal( dataKey.depth() ) , pointEvaluator , dataKey , Accumulate ); |
| 585 |
} |
| 586 |
|
| 587 |
template< unsigned int Dim , class Real > |
| 588 |
template< typename V , typename Coefficients , unsigned int D , typename AccumulationFunctor , unsigned int ... DataSigs > |
| 589 |
void FEMTree< Dim , Real >::_accumulate( const Coefficients& coefficients , Point< Real , Dim > p , LocalDepth pointDepth , const PointEvaluator< UIntPack< DataSigs ... > , IsotropicUIntPack< Dim , D > >& pointEvaluator , const ConstPointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , AccumulationFunctor &Accumulate ) const |
| 590 |
{ |
| 591 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > SupportSizes; |
| 592 |
PointEvaluatorState< UIntPack< DataSigs ... > , ZeroUIntPack< Dim > > state; |
| 593 |
unsigned int derivatives[Dim]; |
| 594 |
memset( derivatives , 0 , sizeof( derivatives ) ); |
| 595 |
typedef PointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > > DataKey; |
| 596 |
|
| 597 |
for( int d=_localToGlobal( 0 ) ; d<=_localToGlobal( pointDepth ) ; d++ ) |
| 598 |
{ |
| 599 |
{ |
| 600 |
const FEMTreeNode* node = dataKey.neighbors[d].neighbors.data[ WindowIndex< UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > , UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportEnd ... > >::Index ]; |
| 601 |
if( !node ) MK_THROW( "Point is not centered on a node: " , p , " " , WindowIndex< UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > , UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportEnd ... > >::Index , " @ " , d ); |
| 602 |
pointEvaluator.initEvaluationState( p , _localDepth( node ) , state ); |
| 603 |
} |
| 604 |
double scratch[Dim+1]; |
| 605 |
scratch[0] = 1; |
| 606 |
ConstPointer( FEMTreeNode * const ) nodes = dataKey.neighbors[d].neighbors().data; |
| 607 |
for( int i=0 ; i<WindowSize< SupportSizes >::Size ; i++ ) if( _isValidFEM1Node( nodes[i] ) ) |
| 608 |
{ |
| 609 |
const V* v = coefficients( nodes[i] ); |
| 610 |
if( v ) |
| 611 |
{ |
| 612 |
LocalDepth d ; LocalOffset off ; _localDepthAndOffset( nodes[i] , d , off ); |
| 613 |
Accumulate( *v , (Real)state.value( off , derivatives ) ); |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
template< unsigned int Dim , class Real > |
| 620 |
template< bool XMajor , class V , unsigned int ... DataSigs > |
| 621 |
Pointer( V ) FEMTree< Dim , Real >::regularGridEvaluate( const DenseNodeData< V , UIntPack< DataSigs ... > >& coefficients , int& res , LocalDepth depth , bool primal ) const |
| 622 |
{ |
| 623 |
if( depth<=0 ) depth = _maxDepth; |
| 624 |
Pointer( V ) _coefficients = regularGridUpSample< XMajor >( coefficients , depth ); |
| 625 |
|
| 626 |
const int begin[] = { _BSplineBegin< DataSigs >( depth ) ... }; |
| 627 |
const int end [] = { _BSplineEnd< DataSigs >( depth ) ... }; |
| 628 |
const int dim [] = { ( _BSplineEnd< DataSigs >( depth ) - _BSplineBegin< DataSigs >( depth ) ) ... }; |
| 629 |
|
| 630 |
res = 1<<depth; |
| 631 |
if( primal ) res++; |
| 632 |
size_t cellCount = 1; |
| 633 |
for( int d=0 ; d<Dim ; d++ ) cellCount *= res; |
| 634 |
Pointer( V ) values = NewPointer< V >( cellCount ); |
| 635 |
memset( values , 0 , sizeof(V) * cellCount ); |
| 636 |
|
| 637 |
if( primal ) |
| 638 |
{ |
| 639 |
// evaluate at the cell corners |
| 640 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::CornerSize ... > CornerSizes; |
| 641 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::CornerEnd ... > CornerEnds; |
| 642 |
|
| 643 |
EvaluationData::CornerEvaluator* evaluators[] = { ( new typename BSplineEvaluationData< DataSigs >::template CornerEvaluator< 0 >::Evaluator() ) ... }; |
| 644 |
for( int d=0 ; d<Dim ; d++ ) evaluators[d]->set( depth ); |
| 645 |
// Compute the offest from coefficient index to voxel index and the value of the stencil (if the voxel is interior) |
| 646 |
StaticWindow< long long , UIntPack< ( BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::CornerSize ? BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::CornerSize : 1 ) ... > > offsets; |
| 647 |
StaticWindow< double , UIntPack< ( BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::CornerSize ? BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::CornerSize : 1 ) ... > > cornerValues; |
| 648 |
int dimMultiplier[Dim]; |
| 649 |
if( XMajor ) |
| 650 |
{ |
| 651 |
dimMultiplier[0] = 1; |
| 652 |
for( int d=1 ; d<Dim ; d++ ) dimMultiplier[d] = dimMultiplier[d-1] * dim[d-1]; |
| 653 |
} |
| 654 |
else |
| 655 |
{ |
| 656 |
dimMultiplier[Dim-1] = 1; |
| 657 |
for( int d=Dim-2 ; d>=0 ; d-- ) dimMultiplier[d] = dimMultiplier[d+1] * dim[d+1]; |
| 658 |
} |
| 659 |
|
| 660 |
{ |
| 661 |
int center = ( 1<<depth )>>1; |
| 662 |
long long offset[Dim+1] ; offset[0] = 0; |
| 663 |
double upValue[Dim+1] ; upValue[0] = 1; |
| 664 |
WindowLoop< Dim >::Run |
| 665 |
( |
| 666 |
ZeroUIntPack< Dim >() , CornerSizes() , |
| 667 |
[&]( int d , int i ){ offset[d+1] = offset[d] + ( i - (int)CornerEnds::Values[d] - begin[d] ) * dimMultiplier[d] ; upValue[d+1] = upValue[d] * evaluators[d]->value( center + i - (int)CornerEnds::Values[d] , center , false ); } , |
| 668 |
[&]( long long& offsetValue , double& cornerValue ){ offsetValue = offset[Dim] , cornerValue = upValue[Dim]; } , |
| 669 |
offsets() , cornerValues() |
| 670 |
); |
| 671 |
} |
| 672 |
ThreadPool::ParallelFor( 0 , cellCount , [&]( unsigned int , size_t c ) |
| 673 |
{ |
| 674 |
V& value = values[c]; |
| 675 |
int idx[Dim]; |
| 676 |
{ |
| 677 |
size_t _c = c; |
| 678 |
if( XMajor ) for( int d=0 ; d<Dim ; d++ ) idx[ d] = _c % res , _c /= res; |
| 679 |
else for( int d=0 ; d<Dim ; d++ ) idx[Dim-1-d] = _c % res , _c /= res; |
| 680 |
} |
| 681 |
long long ii = 0; |
| 682 |
for( int d=0 ; d<Dim ; d++ ) ii += idx[d] * dimMultiplier[d]; |
| 683 |
|
| 684 |
bool isInterior = true; |
| 685 |
for( int d=0 ; d<Dim ; d++ ) if( ( idx[d] - (int)CornerEnds::Values[d] )<begin[d] || ( idx[d] - (int)CornerEnds::Values[d] + (int)CornerSizes::Values[d] )>=end[d] ) isInterior = false; |
| 686 |
|
| 687 |
if( isInterior ) |
| 688 |
{ |
| 689 |
#ifdef SHOW_WARNINGS |
| 690 |
#pragma message( "[WARNING] This should be modified to support 0-degree elements" ) |
| 691 |
#endif // SHOW_WARNINGS |
| 692 |
ConstPointer( long long ) offsetValues = offsets().data; |
| 693 |
ConstPointer( double ) _cornerValues = cornerValues().data; |
| 694 |
for( int i=0 ; i<WindowSize< CornerSizes >::Size ; i++ ) value += _coefficients[ offsetValues[i]+ii ] * (Real)_cornerValues[i]; |
| 695 |
} |
| 696 |
else |
| 697 |
{ |
| 698 |
double upValues[Dim+1] ; upValues[0] = 1; // Accumulates the product of the weights |
| 699 |
bool isValid[Dim+1] ; isValid[0] = true; |
| 700 |
WindowLoop< Dim >::Run |
| 701 |
( |
| 702 |
ZeroUIntPack< Dim >() , CornerSizes() , |
| 703 |
[&]( int d , int i ) |
| 704 |
{ |
| 705 |
int ii = idx[d] + i - (int)CornerEnds::Values[d]; |
| 706 |
if( ii>=begin[d] && ii<end[d] ) |
| 707 |
{ |
| 708 |
upValues[d+1] = upValues[d] * evaluators[d]->value( ii , idx[d] , false ); |
| 709 |
isValid[d+1] = isValid[d]; |
| 710 |
} |
| 711 |
else isValid[d+1] = false; |
| 712 |
} , |
| 713 |
[&]( long long offsetValue ){ if( isValid[Dim] ) value += _coefficients[ offsetValue + ii ] * (Real)upValues[Dim]; } , |
| 714 |
offsets() |
| 715 |
); |
| 716 |
} |
| 717 |
} |
| 718 |
); |
| 719 |
for( int d=0 ; d<Dim ; d++ ) delete evaluators[d]; |
| 720 |
} |
| 721 |
else |
| 722 |
{ |
| 723 |
// evaluate at the cell centers |
| 724 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > SupportSizes; |
| 725 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportEnd ... > SupportEnds; |
| 726 |
|
| 727 |
EvaluationData::CenterEvaluator* evaluators[] = { ( new typename BSplineEvaluationData< DataSigs >::template CenterEvaluator< 0 >::Evaluator() ) ... }; |
| 728 |
for( int d=0 ; d<Dim ; d++ ) evaluators[d]->set( depth ); |
| 729 |
// Compute the offest from coefficient index to voxel index and the value of the stencil (if the voxel is interior) |
| 730 |
StaticWindow< long long , UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > > offsets; |
| 731 |
StaticWindow< double , UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > > centerValues; |
| 732 |
|
| 733 |
int dimMultiplier[Dim]; |
| 734 |
if( XMajor ) |
| 735 |
{ |
| 736 |
dimMultiplier[0] = 1; |
| 737 |
for( int d=1 ; d<Dim ; d++ ) dimMultiplier[d] = dimMultiplier[d-1] * dim[d-1]; |
| 738 |
} |
| 739 |
else |
| 740 |
{ |
| 741 |
dimMultiplier[Dim-1] = 1; |
| 742 |
for( int d=Dim-2 ; d>=0 ; d-- ) dimMultiplier[d] = dimMultiplier[d+1] * dim[d+1]; |
| 743 |
} |
| 744 |
|
| 745 |
{ |
| 746 |
int center = ( 1<<depth )>>1; |
| 747 |
long long offset[Dim+1] ; offset[0] = 0; |
| 748 |
double upValue[Dim+1] ; upValue[0] = 1; |
| 749 |
WindowLoop< Dim >::Run |
| 750 |
( |
| 751 |
ZeroUIntPack< Dim >() , SupportSizes() , |
| 752 |
[&]( int d , int i ){ offset[d+1] = offset[d] + ( i - (int)SupportEnds::Values[d] - begin[d] ) * dimMultiplier[d] ; upValue[d+1] = upValue[d] * evaluators[d]->value( center + i - (int)SupportEnds::Values[d] , center , false ); } , |
| 753 |
[&]( long long& offsetValue , double& centerValue ){ offsetValue = offset[Dim] , centerValue = upValue[Dim]; } , |
| 754 |
offsets() , centerValues() |
| 755 |
); |
| 756 |
} |
| 757 |
ThreadPool::ParallelFor( 0 , cellCount , [&]( unsigned int , size_t c ) |
| 758 |
{ |
| 759 |
V& value = values[c]; |
| 760 |
int idx[Dim]; |
| 761 |
{ |
| 762 |
size_t _c = c; |
| 763 |
if( XMajor ) for( int d=0 ; d<Dim ; d++ ) idx[ d] = _c % res , _c /= res; |
| 764 |
else for( int d=0 ; d<Dim ; d++ ) idx[Dim-1-d] = _c % res , _c /= res; |
| 765 |
} |
| 766 |
long long ii = 0; |
| 767 |
for( int d=0 ; d<Dim ; d++ ) ii += idx[d] * dimMultiplier[d]; |
| 768 |
|
| 769 |
bool isInterior = true; |
| 770 |
for( int d=0 ; d<Dim ; d++ ) if( ( idx[d] - (int)SupportEnds::Values[d] )<begin[d] || ( idx[d] - (int)SupportEnds::Values[d] + (int)SupportSizes::Values[d] )>=end[d] ) isInterior = false; |
| 771 |
|
| 772 |
if( isInterior ) |
| 773 |
{ |
| 774 |
ConstPointer( long long ) offsetValues = offsets().data; |
| 775 |
ConstPointer( double ) _centerValues = centerValues().data; |
| 776 |
for( int i=0 ; i<WindowSize< SupportSizes >::Size ; i++ ) value += _coefficients[ offsetValues[i] + ii ] * (Real)_centerValues[i]; |
| 777 |
} |
| 778 |
else |
| 779 |
{ |
| 780 |
double upValues[Dim+1] ; upValues[0] = 1; // Accumulates the product of the weights |
| 781 |
bool isValid[Dim+1] ; isValid[0] = true; |
| 782 |
WindowLoop< Dim >::Run |
| 783 |
( |
| 784 |
ZeroUIntPack< Dim >() , SupportSizes() , |
| 785 |
[&]( int d , int i ) |
| 786 |
{ |
| 787 |
int ii = idx[d] + i - (int)SupportEnds::Values[d]; |
| 788 |
if( ii>=begin[d] && ii<end[d] ) |
| 789 |
{ |
| 790 |
upValues[d+1] = upValues[d] * evaluators[d]->value( ii , idx[d] , false ); |
| 791 |
isValid[d+1] = isValid[d]; |
| 792 |
} |
| 793 |
else isValid[d+1] = false; |
| 794 |
} , |
| 795 |
[&]( long long offsetValue ){ if( isValid[Dim] ) value += _coefficients[ offsetValue + ii ] * (Real)upValues[Dim]; } , |
| 796 |
offsets() |
| 797 |
); |
| 798 |
} |
| 799 |
} |
| 800 |
); |
| 801 |
for( int d=0 ; d<Dim ; d++ ) delete evaluators[d]; |
| 802 |
} |
| 803 |
DeletePointer( _coefficients ); |
| 804 |
|
| 805 |
return values; |
| 806 |
} |
| 807 |
|
| 808 |
template< unsigned int Dim , class Real > |
| 809 |
template< bool XMajor , class V , unsigned int ... DataSigs > |
| 810 |
Pointer( V ) FEMTree< Dim , Real >::regularGridEvaluate( const DenseNodeData< V , UIntPack< DataSigs ... > >& coefficients , const unsigned int begin[Dim] , const unsigned int end[Dim] , unsigned int res[Dim] , bool primal ) const |
| 811 |
{ |
| 812 |
LocalDepth depth = _maxDepth; |
| 813 |
Pointer( V ) _coefficients = regularGridUpSample< XMajor >( coefficients , depth ); |
| 814 |
|
| 815 |
const int _begin[] = { _BSplineBegin< DataSigs >( depth ) ... }; |
| 816 |
const int _end [] = { _BSplineEnd< DataSigs >( depth ) ... }; |
| 817 |
const int _dim [] = { ( _BSplineEnd< DataSigs >( depth ) - _BSplineBegin< DataSigs >( depth ) ) ... }; |
| 818 |
|
| 819 |
size_t cellCount = 1; |
| 820 |
for( unsigned int d=0 ; d<Dim ; d++ ) res[d] = primal ? ( end[d]-begin[d]+1 ) : ( end[d]-begin[d] ); |
| 821 |
for( unsigned int d=0 ; d<Dim ; d++ ) cellCount *= res[d]; |
| 822 |
|
| 823 |
Pointer( V ) values = NewPointer< V >( cellCount ); |
| 824 |
memset( values , 0 , sizeof(V) * cellCount ); |
| 825 |
|
| 826 |
if( primal ) |
| 827 |
{ |
| 828 |
// evaluate at the cell corners |
| 829 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::CornerSize ... > CornerSizes; |
| 830 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::CornerEnd ... > CornerEnds; |
| 831 |
|
| 832 |
EvaluationData::CornerEvaluator* evaluators[] = { ( new typename BSplineEvaluationData< DataSigs >::template CornerEvaluator< 0 >::Evaluator() ) ... }; |
| 833 |
for( int d=0 ; d<Dim ; d++ ) evaluators[d]->set( depth ); |
| 834 |
// Compute the offest from coefficient index to voxel index and the value of the stencil (if the voxel is interior) |
| 835 |
StaticWindow< long long , UIntPack< ( BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::CornerSize ? BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::CornerSize : 1 ) ... > > offsets; |
| 836 |
StaticWindow< double , UIntPack< ( BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::CornerSize ? BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::CornerSize : 1 ) ... > > cornerValues; |
| 837 |
int dimMultiplier[Dim]; |
| 838 |
if( XMajor ) |
| 839 |
{ |
| 840 |
dimMultiplier[0] = 1; |
| 841 |
for( int d=1 ; d<Dim ; d++ ) dimMultiplier[d] = dimMultiplier[d-1] * _dim[d-1]; |
| 842 |
} |
| 843 |
else |
| 844 |
{ |
| 845 |
dimMultiplier[Dim-1] = 1; |
| 846 |
for( int d=Dim-2 ; d>=0 ; d-- ) dimMultiplier[d] = dimMultiplier[d+1] * _dim[d+1]; |
| 847 |
} |
| 848 |
|
| 849 |
{ |
| 850 |
int center = ( 1<<depth )>>1; |
| 851 |
long long offset[Dim+1] ; offset[0] = 0; |
| 852 |
double upValue[Dim+1] ; upValue[0] = 1; |
| 853 |
WindowLoop< Dim >::Run |
| 854 |
( |
| 855 |
ZeroUIntPack< Dim >() , CornerSizes() , |
| 856 |
[&]( int d , int i ){ offset[d+1] = offset[d] + ( i - (int)CornerEnds::Values[d] - _begin[d] ) * dimMultiplier[d] ; upValue[d+1] = upValue[d] * evaluators[d]->value( center + i - (int)CornerEnds::Values[d] , center , false ); } , |
| 857 |
[&]( long long& offsetValue , double& cornerValue ){ offsetValue = offset[Dim] , cornerValue = upValue[Dim]; } , |
| 858 |
offsets() , cornerValues() |
| 859 |
); |
| 860 |
} |
| 861 |
ThreadPool::ParallelFor( 0 , cellCount , [&]( unsigned int , size_t c ) |
| 862 |
{ |
| 863 |
V &value = values[c]; |
| 864 |
int idx[Dim]; |
| 865 |
{ |
| 866 |
size_t _c = c; |
| 867 |
if( XMajor ) for( int d=0 ; d<Dim ; d++ ) idx[ d] = begin[d] + _c % res[d] , _c /= res[d]; |
| 868 |
else for( int d=0 ; d<Dim ; d++ ) idx[Dim-1-d] = begin[d] + _c % res[d] , _c /= res[d]; |
| 869 |
} |
| 870 |
long long ii = 0; |
| 871 |
for( int d=0 ; d<Dim ; d++ ) ii += idx[d] * dimMultiplier[d]; |
| 872 |
|
| 873 |
bool isInterior = true; |
| 874 |
for( int d=0 ; d<Dim ; d++ ) if( ( idx[d] - (int)CornerEnds::Values[d] )<_begin[d] || ( idx[d] - (int)CornerEnds::Values[d] + (int)CornerSizes::Values[d] )>=_end[d] ) isInterior = false; |
| 875 |
|
| 876 |
if( isInterior ) |
| 877 |
{ |
| 878 |
#ifdef SHOW_WARNINGS |
| 879 |
#pragma message( "[WARNING] This should be modified to support 0-degree elements" ) |
| 880 |
#endif // SHOW_WARNINGS |
| 881 |
ConstPointer( long long ) offsetValues = offsets().data; |
| 882 |
ConstPointer( double ) _cornerValues = cornerValues().data; |
| 883 |
for( int i=0 ; i<WindowSize< CornerSizes >::Size ; i++ ) value += _coefficients[ offsetValues[i]+ii ] * (Real)_cornerValues[i]; |
| 884 |
} |
| 885 |
else |
| 886 |
{ |
| 887 |
double upValues[Dim+1] ; upValues[0] = 1; // Accumulates the product of the weights |
| 888 |
bool isValid[Dim+1] ; isValid[0] = true; |
| 889 |
WindowLoop< Dim >::Run |
| 890 |
( |
| 891 |
ZeroUIntPack< Dim >() , CornerSizes() , |
| 892 |
[&]( int d , int i ) |
| 893 |
{ |
| 894 |
int ii = idx[d] + i - (int)CornerEnds::Values[d]; |
| 895 |
if( ii>=_begin[d] && ii<_end[d] ) |
| 896 |
{ |
| 897 |
upValues[d+1] = upValues[d] * evaluators[d]->value( ii , idx[d] , false ); |
| 898 |
isValid[d+1] = isValid[d]; |
| 899 |
} |
| 900 |
else isValid[d+1] = false; |
| 901 |
} , |
| 902 |
[&]( long long offsetValue ){ if( isValid[Dim] ) value += _coefficients[ offsetValue + ii ] * (Real)upValues[Dim]; } , |
| 903 |
offsets() |
| 904 |
); |
| 905 |
} |
| 906 |
} |
| 907 |
); |
| 908 |
for( int d=0 ; d<Dim ; d++ ) delete evaluators[d]; |
| 909 |
} |
| 910 |
else |
| 911 |
{ |
| 912 |
// evaluate at the cell centers |
| 913 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > SupportSizes; |
| 914 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportEnd ... > SupportEnds; |
| 915 |
|
| 916 |
EvaluationData::CenterEvaluator* evaluators[] = { ( new typename BSplineEvaluationData< DataSigs >::template CenterEvaluator< 0 >::Evaluator() ) ... }; |
| 917 |
for( int d=0 ; d<Dim ; d++ ) evaluators[d]->set( depth ); |
| 918 |
// Compute the offest from coefficient index to voxel index and the value of the stencil (if the voxel is interior) |
| 919 |
StaticWindow< long long , UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > > offsets; |
| 920 |
StaticWindow< double , UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > > centerValues; |
| 921 |
|
| 922 |
int dimMultiplier[Dim]; |
| 923 |
if( XMajor ) |
| 924 |
{ |
| 925 |
dimMultiplier[0] = 1; |
| 926 |
for( int d=1 ; d<Dim ; d++ ) dimMultiplier[d] = dimMultiplier[d-1] * _dim[d-1]; |
| 927 |
} |
| 928 |
else |
| 929 |
{ |
| 930 |
dimMultiplier[Dim-1] = 1; |
| 931 |
for( int d=Dim-2 ; d>=0 ; d-- ) dimMultiplier[d] = dimMultiplier[d+1] * _dim[d+1]; |
| 932 |
} |
| 933 |
|
| 934 |
{ |
| 935 |
int center = ( 1<<depth )>>1; |
| 936 |
long long offset[Dim+1] ; offset[0] = 0; |
| 937 |
double upValue[Dim+1] ; upValue[0] = 1; |
| 938 |
WindowLoop< Dim >::Run |
| 939 |
( |
| 940 |
ZeroUIntPack< Dim >() , SupportSizes() , |
| 941 |
[&]( int d , int i ){ offset[d+1] = offset[d] + ( i - (int)SupportEnds::Values[d] - _begin[d] ) * dimMultiplier[d] ; upValue[d+1] = upValue[d] * evaluators[d]->value( center + i - (int)SupportEnds::Values[d] , center , false ); } , |
| 942 |
[&]( long long& offsetValue , double& centerValue ){ offsetValue = offset[Dim] , centerValue = upValue[Dim]; } , |
| 943 |
offsets() , centerValues() |
| 944 |
); |
| 945 |
} |
| 946 |
ThreadPool::ParallelFor( 0 , cellCount , [&]( unsigned int , size_t c ) |
| 947 |
{ |
| 948 |
V &value = values[c]; |
| 949 |
int idx[Dim]; |
| 950 |
{ |
| 951 |
size_t _c = c; |
| 952 |
if( XMajor ) for( int d=0 ; d<Dim ; d++ ) idx[ d] = begin[d] + _c % res[d] , _c /= res[d]; |
| 953 |
else for( int d=0 ; d<Dim ; d++ ) idx[Dim-1-d] = begin[d] + _c % res[d] , _c /= res[d]; |
| 954 |
} |
| 955 |
long long ii = 0; |
| 956 |
for( int d=0 ; d<Dim ; d++ ) ii += idx[d] * dimMultiplier[d]; |
| 957 |
|
| 958 |
bool isInterior = true; |
| 959 |
for( int d=0 ; d<Dim ; d++ ) if( ( idx[d] - (int)SupportEnds::Values[d] )<_begin[d] || ( idx[d] - (int)SupportEnds::Values[d] + (int)SupportSizes::Values[d] )>=_end[d] ) isInterior = false; |
| 960 |
|
| 961 |
if( isInterior ) |
| 962 |
{ |
| 963 |
ConstPointer( long long ) offsetValues = offsets().data; |
| 964 |
ConstPointer( double ) _centerValues = centerValues().data; |
| 965 |
for( int i=0 ; i<WindowSize< SupportSizes >::Size ; i++ ) value += _coefficients[ offsetValues[i] + ii ] * (Real)_centerValues[i]; |
| 966 |
} |
| 967 |
else |
| 968 |
{ |
| 969 |
double upValues[Dim+1] ; upValues[0] = 1; // Accumulates the product of the weights |
| 970 |
bool isValid[Dim+1] ; isValid[0] = true; |
| 971 |
WindowLoop< Dim >::Run |
| 972 |
( |
| 973 |
ZeroUIntPack< Dim >() , SupportSizes() , |
| 974 |
[&]( int d , int i ) |
| 975 |
{ |
| 976 |
int ii = idx[d] + i - (int)SupportEnds::Values[d]; |
| 977 |
if( ii>=_begin[d] && ii<_end[d] ) |
| 978 |
{ |
| 979 |
upValues[d+1] = upValues[d] * evaluators[d]->value( ii , idx[d] , false ); |
| 980 |
isValid[d+1] = isValid[d]; |
| 981 |
} |
| 982 |
else isValid[d+1] = false; |
| 983 |
} , |
| 984 |
[&]( long long offsetValue ){ if( isValid[Dim] ) value += _coefficients[ offsetValue + ii ] * (Real)upValues[Dim]; } , |
| 985 |
offsets() |
| 986 |
); |
| 987 |
} |
| 988 |
} |
| 989 |
); |
| 990 |
for( int d=0 ; d<Dim ; d++ ) delete evaluators[d]; |
| 991 |
} |
| 992 |
DeletePointer( _coefficients ); |
| 993 |
|
| 994 |
return values; |
| 995 |
} |
| 996 |
|
| 997 |
template< unsigned int Dim , class Real > |
| 998 |
template< bool XMajor , class V , unsigned int ... DataSigs > |
| 999 |
Pointer( V ) FEMTree< Dim , Real >::regularGridUpSample( const DenseNodeData< V , UIntPack< DataSigs ... > >& coefficients , LocalDepth depth ) const |
| 1000 |
{ |
| 1001 |
if( depth<=0 ) depth = _maxDepth; |
| 1002 |
int begin[Dim] , end[Dim]; |
| 1003 |
FEMIntegrator::BSplineBegin( UIntPack< DataSigs ... >() , depth , begin ); |
| 1004 |
FEMIntegrator::BSplineEnd ( UIntPack< DataSigs ... >() , depth , end ); |
| 1005 |
return regularGridUpSample< XMajor >( coefficients , begin , end , depth ); |
| 1006 |
} |
| 1007 |
template< unsigned int Dim , class Real > |
| 1008 |
template< bool XMajor , class V , unsigned int ... DataSigs > |
| 1009 |
Pointer( V ) FEMTree< Dim , Real >::regularGridUpSample( const DenseNodeData< V , UIntPack< DataSigs ... > >& coefficients , const int begin[Dim] , const int end[Dim] , LocalDepth depth ) const |
| 1010 |
{ |
| 1011 |
if( depth<=0 ) depth = _maxDepth; |
| 1012 |
|
| 1013 |
static const int DownSampleStart[][sizeof...(DataSigs)] = { { BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::DownSampleStart[0] ... } , { BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::DownSampleStart[1] ... } }; |
| 1014 |
static const int DownSampleEnd [][sizeof...(DataSigs)] = { { BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::DownSampleEnd [0] ... } , { BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::DownSampleEnd [1] ... } }; |
| 1015 |
|
| 1016 |
struct GridDimensions |
| 1017 |
{ |
| 1018 |
int begin[Dim] , end[Dim] , dim[Dim]; |
| 1019 |
int dimMultiplier[Dim]; |
| 1020 |
GridDimensions( void ){ } |
| 1021 |
GridDimensions( const int b[Dim] , const int e[Dim] ) |
| 1022 |
{ |
| 1023 |
memcpy( begin , b , sizeof(begin) ); |
| 1024 |
memcpy( end , e , sizeof(end) ); |
| 1025 |
for( int d=0 ; d<Dim ; d++ ) dim[d] = end[d] - begin[d]; |
| 1026 |
if( XMajor ) |
| 1027 |
{ |
| 1028 |
dimMultiplier[0] = 1; |
| 1029 |
for( int d=1 ; d<Dim ; d++ ) dimMultiplier[d] = dimMultiplier[d-1] * dim[d-1]; |
| 1030 |
} |
| 1031 |
else |
| 1032 |
{ |
| 1033 |
dimMultiplier[Dim-1] = 1; |
| 1034 |
for( int d=Dim-2 ; d>=0 ; d-- ) dimMultiplier[d] = dimMultiplier[d+1] * dim[d+1]; |
| 1035 |
} |
| 1036 |
} |
| 1037 |
}; |
| 1038 |
|
| 1039 |
auto SetCoarseGridDimensions = []( const GridDimensions& fine , GridDimensions& coarse , int lowDepth ) |
| 1040 |
{ |
| 1041 |
int begin[Dim] , end[Dim]; |
| 1042 |
FEMIntegrator::BSplineBegin( UIntPack< DataSigs ... >() , lowDepth , begin ); |
| 1043 |
FEMIntegrator::BSplineEnd ( UIntPack< DataSigs ... >() , lowDepth , end ); |
| 1044 |
for( int d=0 ; d<Dim ; d++ ) |
| 1045 |
{ |
| 1046 |
coarse.begin[d] = std::max< int >( begin[d] , (fine.begin[d]>>1) + DownSampleStart[fine.begin[d]&1][d] ); |
| 1047 |
coarse.end [d] = std::min< int >( end [d] , (fine.end [d]>>1) + DownSampleEnd [fine.end [d]&1][d]+1 ); |
| 1048 |
coarse.dim [d] = coarse.end[d] - coarse.begin[d]; |
| 1049 |
} |
| 1050 |
if( XMajor ) |
| 1051 |
{ |
| 1052 |
coarse.dimMultiplier[0] = 1; |
| 1053 |
for( int d=1 ; d<Dim ; d++ ) coarse.dimMultiplier[d] = coarse.dimMultiplier[d-1] * coarse.dim[d-1]; |
| 1054 |
} |
| 1055 |
else |
| 1056 |
{ |
| 1057 |
coarse.dimMultiplier[Dim-1] = 1; |
| 1058 |
for( int d=Dim-2 ; d>=0 ; d-- ) coarse.dimMultiplier[d] = coarse.dimMultiplier[d+1] * coarse.dim[d+1]; |
| 1059 |
} |
| 1060 |
}; |
| 1061 |
auto InBounds = []( const LocalOffset& off , const GridDimensions& gDim ) |
| 1062 |
{ |
| 1063 |
for( int d=0 ; d<Dim ; d++ ) if( off[d]<gDim.begin[d] || off[d]>=gDim.end[d] ) return false; |
| 1064 |
return true; |
| 1065 |
}; |
| 1066 |
|
| 1067 |
std::vector< GridDimensions > gridDimensions( depth+1 ); |
| 1068 |
gridDimensions[depth] = GridDimensions( begin , end ); |
| 1069 |
for( int d=depth ; d>0 ; d-- ) SetCoarseGridDimensions( gridDimensions[d] , gridDimensions[d-1] , d-1 ); |
| 1070 |
|
| 1071 |
// Initialize the coefficients at the coarsest level |
| 1072 |
Pointer( V ) upSampledCoefficients = NullPointer( V ); |
| 1073 |
{ |
| 1074 |
LocalDepth _depth = 0; |
| 1075 |
size_t count = 1; |
| 1076 |
for( int dd=0 ; dd<Dim ; dd++ ) count *= gridDimensions[_depth].dim[dd]; |
| 1077 |
upSampledCoefficients = NewPointer< V >( count ); |
| 1078 |
memset( upSampledCoefficients , 0 , sizeof( V ) * count ); |
| 1079 |
ThreadPool::ParallelFor( _sNodesBegin(_depth) , _sNodesEnd(_depth) , [&]( unsigned int , size_t i ) |
| 1080 |
{ |
| 1081 |
if( !_outOfBounds( UIntPack< DataSigs ... >() , _sNodes.treeNodes[i] ) ) |
| 1082 |
{ |
| 1083 |
LocalDepth _d ; LocalOffset _off; |
| 1084 |
_localDepthAndOffset( _sNodes.treeNodes[i] , _d , _off ); |
| 1085 |
if( InBounds( _off , gridDimensions[_depth] ) ) |
| 1086 |
{ |
| 1087 |
size_t idx = 0; |
| 1088 |
for( int d=0 ; d<Dim ; d++ ) idx += gridDimensions[_depth].dimMultiplier[d] * ( _off[d] - gridDimensions[_depth].begin[d] ); |
| 1089 |
upSampledCoefficients[idx] = coefficients[i]; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
} |
| 1093 |
); |
| 1094 |
} |
| 1095 |
// Up-sample and add in the existing coefficients |
| 1096 |
for( LocalDepth _depth=1 ; _depth<=depth ; _depth++ ) |
| 1097 |
{ |
| 1098 |
size_t count = 1; |
| 1099 |
for( int d=0 ; d<Dim ; d++ ) count *= gridDimensions[_depth].dim[d]; |
| 1100 |
Pointer( V ) _coefficients = NewPointer< V >( count ); |
| 1101 |
memset( _coefficients , 0 , sizeof( V ) * count ); |
| 1102 |
if( _depth<=_maxDepth ) |
| 1103 |
ThreadPool::ParallelFor( _sNodesBegin(_depth) , _sNodesEnd(_depth) , [&]( unsigned int , size_t i ) |
| 1104 |
{ |
| 1105 |
if( !_outOfBounds( UIntPack< DataSigs ... >() , _sNodes.treeNodes[i] ) ) |
| 1106 |
{ |
| 1107 |
LocalDepth _d ; LocalOffset _off; |
| 1108 |
_localDepthAndOffset( _sNodes.treeNodes[i] , _d , _off ); |
| 1109 |
if( InBounds( _off , gridDimensions[_depth] ) ) |
| 1110 |
{ |
| 1111 |
size_t idx = 0; |
| 1112 |
for( int d=0 ; d<Dim ; d++ ) idx += gridDimensions[_depth].dimMultiplier[d] * ( _off[d] - gridDimensions[_depth].begin[d] ); |
| 1113 |
_coefficients[idx] = coefficients[i]; |
| 1114 |
} |
| 1115 |
} |
| 1116 |
} |
| 1117 |
); |
| 1118 |
_RegularGridUpSample< XMajor >( UIntPack< DataSigs ... >() , gridDimensions[_depth-1].begin , gridDimensions[_depth-1].end , gridDimensions[_depth].begin , gridDimensions[_depth].end , _depth , ( ConstPointer(V) )upSampledCoefficients , _coefficients ); |
| 1119 |
DeletePointer( upSampledCoefficients ); |
| 1120 |
upSampledCoefficients = _coefficients; |
| 1121 |
} |
| 1122 |
return upSampledCoefficients; |
| 1123 |
} |
| 1124 |
template< unsigned int Dim , class Real > |
| 1125 |
template< class V , unsigned int ... DataSigs > |
| 1126 |
V FEMTree< Dim , Real >::average( const DenseNodeData< V , UIntPack< DataSigs ... > >& coefficients ) const |
| 1127 |
{ |
| 1128 |
Real begin[Dim] , end[Dim]; |
| 1129 |
for( int d=0 ; d<Dim ; d++ ) begin[d] = (Real)0. , end[d] = (Real)1.; |
| 1130 |
return average( coefficients , begin , end ); |
| 1131 |
} |
| 1132 |
template< unsigned int Dim , class Real > |
| 1133 |
template< class V , unsigned int ... DataSigs > |
| 1134 |
V FEMTree< Dim , Real >::average( const DenseNodeData< V , UIntPack< DataSigs ... > >& coefficients , const Real begin[Dim] , const Real end[Dim] ) const |
| 1135 |
{ |
| 1136 |
_setFEM1ValidityFlags( UIntPack< DataSigs ... >() ); |
| 1137 |
std::vector< V > avgs( ThreadPool::NumThreads() ); |
| 1138 |
for( int i=0 ; i<avgs.size() ; i++ ) avgs[i] = {}; |
| 1139 |
double _begin[Dim] , _end[Dim]; |
| 1140 |
for( int d=0 ; d<Dim ; d++ ) _begin[d] = begin[d] , _end[d] = end[d]; |
| 1141 |
for( int d=0 ; d<=_maxDepth ; d++ ) |
| 1142 |
{ |
| 1143 |
int center = ( 1<<d )>>1; |
| 1144 |
int off[Dim]; |
| 1145 |
double __begin[Dim] , __end[Dim]; |
| 1146 |
for( int dd=0 ; dd<Dim ; dd++ ) off[dd] = center , __begin[dd] = 0 , __end[dd] = 1; |
| 1147 |
double integral = FEMIntegrator::Integral( UIntPack< DataSigs ... >() , d , off , __begin , __end ); |
| 1148 |
ThreadPool::ParallelFor( _sNodesBegin(d) , _sNodesEnd(d) , [&]( unsigned int thread , size_t i ) |
| 1149 |
{ |
| 1150 |
if( _isValidFEM1Node( _sNodes.treeNodes[i] ) ) |
| 1151 |
{ |
| 1152 |
int d , off[Dim]; |
| 1153 |
_localDepthAndOffset( _sNodes.treeNodes[i] , d , off ); |
| 1154 |
if( BaseFEMIntegrator::IsInteriorlySupported( UIntPack< FEMSignature< DataSigs >::Degree ... >() , d , off , _begin , _end ) ) avgs[ thread ] += (V)( coefficients[i] * (Real)integral ); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
); |
| 1158 |
} |
| 1159 |
V avg = {}; |
| 1160 |
for( int i=0 ; i<avgs.size() ; i++ ) avg += avgs[i]; |
| 1161 |
Real scale = (Real)1.; |
| 1162 |
for( int d=0 ; d<Dim ; d++ ) scale *= end[d] - begin[d]; |
| 1163 |
return avg / scale; |
| 1164 |
} |
| 1165 |
|
| 1166 |
template< unsigned int Dim , class Real > |
| 1167 |
template< unsigned int PointD , unsigned int ... FEMSigs > |
| 1168 |
SparseNodeData< CumulativeDerivativeValues< Real , Dim , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > FEMTree< Dim , Real >::leafValues( const DenseNodeData< Real , UIntPack< FEMSigs ... > >& coefficients , int maxDepth ) const |
| 1169 |
{ |
| 1170 |
if( maxDepth<0 ) maxDepth = _maxDepth; |
| 1171 |
_setFEM1ValidityFlags( UIntPack< FEMSigs ... >() ); |
| 1172 |
SparseNodeData< CumulativeDerivativeValues< Real , Dim , PointD > , IsotropicUIntPack< Dim , FEMTrivialSignature > > values; |
| 1173 |
DenseNodeData< Real , UIntPack< FEMSigs ... > > _coefficients = coarseCoefficients< Real >( coefficients ); |
| 1174 |
_Evaluator< UIntPack< FEMSigs ... > , PointD > evaluator; |
| 1175 |
evaluator.set( maxDepth ); |
| 1176 |
for( LocalDepth d=maxDepth ; d>=0 ; d-- ) |
| 1177 |
{ |
| 1178 |
std::vector< ConstPointSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > > > neighborKeys( ThreadPool::NumThreads() ); |
| 1179 |
for( size_t i=0 ; i<neighborKeys.size() ; i++ ) neighborKeys[i].set( _localToGlobal( d ) ); |
| 1180 |
ThreadPool::ParallelFor( _sNodesBegin(d) , _sNodesEnd(d) , [&]( unsigned int thread , size_t i ) |
| 1181 |
{ |
| 1182 |
if( _isValidSpaceNode( _sNodes.treeNodes[i] ) ) |
| 1183 |
{ |
| 1184 |
ConstPointSupportKey< UIntPack< FEMSignature< FEMSigs >::Degree ... > >& neighborKey = neighborKeys[ thread ]; |
| 1185 |
FEMTreeNode* node = _sNodes.treeNodes[i]; |
| 1186 |
if( !IsActiveNode< Dim >( node->children ) || d==maxDepth ) |
| 1187 |
{ |
| 1188 |
neighborKey.getNeighbors( node ); |
| 1189 |
bool isInterior = _isInteriorlySupported( UIntPack< FEMSignature< FEMSigs >::Degree ... >() , node->parent ); |
| 1190 |
values[ node ] = _getCenterValues< Real , PointD >( neighborKey , node , coefficients() , _coefficients() , evaluator , maxDepth , isInterior ); |
| 1191 |
} |
| 1192 |
} |
| 1193 |
} |
| 1194 |
); |
| 1195 |
} |
| 1196 |
return values; |
| 1197 |
} |