KernelFieldInterpolator.h 12.2 KB
Newer Older
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
//======================================================================================================================
//
//  This file is part of waLBerla. waLBerla is free software: you can
//  redistribute it and/or modify it under the terms of the GNU General Public
//  License as published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//
//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
//  for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file KernelFieldInterpolator.h
//! \ingroup field
//! \author Christoph Rettinger <christoph.rettinger@fau.de>
//
//======================================================================================================================

#pragma once

#include "core/debug/Debug.h"
#include "core/math/Vector3.h"

#include "domain_decomposition/StructuredBlockStorage.h"

#include "field/FlagField.h"

#include "stencil/D3Q27.h"

#include <numeric>

namespace walberla {
namespace field {

namespace kernelweights
{

// corresponds to the smoothed dirac delta function from Roma et al - An Adaptive Version of the Immersed Boundary Method
// f(r) != 0 for abs(r) < 1.5 -> requires three neighborhood cells
real_t smoothedDeltaFunction( const real_t & r )
{
   real_t rAbs = std::fabs(r);
   if( rAbs <= real_t(0.5) )
   {
      return (real_t(1) + std::sqrt( - real_t(3) * r * r + real_t(1) ) ) * (real_t(1) / real_t(3));
   } else if ( rAbs < real_t(1.5) )
   {
      return (real_t(5) - real_t(3) * rAbs - std::sqrt( - real_t(3) * ( real_t(1) - rAbs ) * ( real_t(1) - rAbs ) + real_t(1) ) ) * ( real_t(1) / real_t(6) );
   } else
   {
      return real_t(0);
   }

}

// X: Lagrangian position, x: Eulerian position (usually cell center), global coordinates
// dx, dy, dz: mesh spacing
real_t kernelWeightFunction( const real_t & X, const real_t & Y, const real_t & Z,
                             const real_t & x, const real_t & y, const real_t & z,
                             const real_t & dx = real_t(1), const real_t & dy = real_t(1), const real_t & dz = real_t(1) )
{
   return smoothedDeltaFunction( ( X - x ) / dx ) * smoothedDeltaFunction( ( Y - y ) / dy ) * smoothedDeltaFunction( ( Z - z ) / dz );
}

// X: Lagrangian position, x: Eulerian position (usually cell center), global coordinates
// dx, dy, dz: mesh spacing
real_t kernelWeightFunction( const Vector3<real_t> & X, const Vector3<real_t> & x,
                             const real_t & dx = real_t(1), const real_t & dy = real_t(1), const real_t & dz = real_t(1) )
{
   return smoothedDeltaFunction( ( X[0] - x[0] ) / dx ) * smoothedDeltaFunction( ( X[1] - x[1] ) / dy ) * smoothedDeltaFunction( ( X[2] - x[2] ) / dz );
}

} // namespace kernelweights


/*! Interpolator for walberla::field::GhostLayerField with kernel strategy
 *
 * \ingroup field
 *
 * This interpolator uses a smoothed dirac kernel function to interpolate values.
 * The applied weights are given in the namespace field::kernelweights.
 * Needs the full neighborhood of the containing cell, i.e. 27 cells.
 * Never construct this interpolator directly, but use the functionality from the FieldInterpolatorCreator.h instead.
 */
template< typename Field_T, typename FlagField_T >
class KernelFieldInterpolator
{
public:

   static const uint_t F_SIZE = Field_T::F_SIZE;

   typedef Field_T                                      BaseField_T;
   typedef typename FlagField_T::flag_t                 flag_t;
   typedef KernelFieldInterpolator<Field_T,FlagField_T> OwnType;

99
   KernelFieldInterpolator( const weak_ptr<StructuredBlockStorage> & blockStorage, const IBlock & block,
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
                            const BaseField_T & baseField, const FlagField_T & flagField,
                            const flag_t & evaluationMask )
   : blockStorage_( blockStorage ), block_( block ), baseField_( baseField ), flagField_( flagField ), evaluationMask_( evaluationMask )
   {
      WALBERLA_ASSERT(baseField.nrOfGhostLayers() > uint_t(0), "field for kernel interpolation needs at least one ghost layer");
   }


   inline bool operator==( const OwnType & other ){ return baseField_ == other.baseField_; }

   template< typename ForwardIterator_T >
   inline void get( const Vector3<real_t> & position, ForwardIterator_T interpolationResultBegin )
   {
      get( position[0], position[1], position[2], interpolationResultBegin );
   }

   template< typename ForwardIterator_T >
   inline void get( const real_t & x, const real_t & y, const real_t & z, ForwardIterator_T interpolationResultBegin )
   {
119

120
121
122
      WALBERLA_ASSERT(block_.getAABB().contains(x,y,z),
                      "Interpolation position <" << x << ", " << y << ", " << z << "> is not contained inside the block of this interpolator with AABB " << block_.getAABB() << " !");

123
124
125
126
      WALBERLA_CHECK( !blockStorage_.expired() );
      auto blockStorage = blockStorage_.lock();

      Cell centerCell = blockStorage->getBlockLocalCell( block_, x, y, z );
127

128
129
130
      const real_t dx = blockStorage->dx( blockStorage->getLevel( block_ ) );
      const real_t dy = blockStorage->dy( blockStorage->getLevel( block_ ) );
      const real_t dz = blockStorage->dz( blockStorage->getLevel( block_ ) );
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

      const uint_t neighborhoodSize = uint_t(1);

      CellInterval cellNeighborhood( centerCell[0] - cell_idx_c(neighborhoodSize), centerCell[1] - cell_idx_c(neighborhoodSize), centerCell[2] - cell_idx_c(neighborhoodSize),
                                     centerCell[0] + cell_idx_c(neighborhoodSize), centerCell[1] + cell_idx_c(neighborhoodSize), centerCell[2] + cell_idx_c(neighborhoodSize) );

      const uint_t kernelSizeOneDirection = uint_t(2) * neighborhoodSize + uint_t(1);
      // store the calculated weighting factors of the available cells, i.e. cells flagged by the evaluation mask
      std::vector<real_t> weights( kernelSizeOneDirection * kernelSizeOneDirection * kernelSizeOneDirection, real_t(0) );

      // store the calculated weighting factors of the UNavailable cells, i.e. cells not included in the evaluation mask
      std::vector<real_t> weightsUnavailable( kernelSizeOneDirection * kernelSizeOneDirection * kernelSizeOneDirection, real_t(0) );

      cell_idx_t cellIdx0x = cellNeighborhood.xMin();
      cell_idx_t cellIdx0y = cellNeighborhood.yMin();
      cell_idx_t cellIdx0z = cellNeighborhood.zMin();
      uint_t nx = kernelSizeOneDirection;
      uint_t nxy = kernelSizeOneDirection * kernelSizeOneDirection;
149
      Vector3<real_t> cellCenter0 = blockStorage->getBlockLocalCellCenter( block_, Cell(cellIdx0x, cellIdx0y, cellIdx0z) ); // = cell in neighborhood with smallest x-, y-, and z-indices
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

      // calculate kernel weights of all cells in neighborhood
      for( uint_t k = uint_t(0); k < kernelSizeOneDirection; ++k)
      {
         for( uint_t j = uint_t(0); j < kernelSizeOneDirection; ++j)
         {
            for( uint_t i = uint_t(0); i < kernelSizeOneDirection; ++i)
            {
               Vector3<real_t> curCellCenter( cellCenter0[0] + real_c(i) * dx, cellCenter0[1] + real_c(j) * dy, cellCenter0[2] + real_c(k) * dz);
               if( flagField_.isPartOfMaskSet( cellIdx0x + cell_idx_c(i), cellIdx0y + cell_idx_c(j), cellIdx0z + cell_idx_c(k), evaluationMask_ ) )
               {
                  weights[k*nxy+j*nx+i] = kernelweights::kernelWeightFunction( x, y, z, curCellCenter[0], curCellCenter[1], curCellCenter[2], dx, dy, dz );
               } 
               else
               {
                  weightsUnavailable[k*nxy+j*nx+i] = kernelweights::kernelWeightFunction( x, y, z, curCellCenter[0], curCellCenter[1], curCellCenter[2], dx, dy, dz );
               }
            }
         }
      }

/*
      // zero entries in weights array (and non-zero entries in weightsUnavailable) indicate non-fluid nodes
      // weight correction to incorporate extrapolation for kernel interpolation, center element can not be redistributed
      // without this part, the kernel interpolator can not extrapolate values
      // this is based on experimental findings by the author and general correctness is not ensured, thus it is commented out
      for( auto stenIt = stencil::D3Q27::beginNoCenter(); stenIt != stencil::D3Q27::end(); ++stenIt )
      {
         int cx = stenIt.cx();
         int cy = stenIt.cy();
         int cz = stenIt.cz();
         int i = cx + 1;
         int j = cy + 1;
         int k = cz + 1;

         if( weightsUnavailable[k*nxy+j*nx+i] > real_t(0) )
         {
            // check if we can distribute the non-fluid weight to nearby fluid weights that lie in one line inside the neighborhood
            // it should generally not matter in which direction it is distributed
            // check x direction
            if( weights[k*nxy+j*nx+i-cx] > real_t(0) && weights[k*nxy+j*nx+i-2*cx] > real_t(0) )
            {
               weights[k*nxy+j*nx+i-cx] += real_t(2)*weightsUnavailable[k*nxy+j*nx+i];
               weights[k*nxy+j*nx+i-2*cx] -= weightsUnavailable[k*nxy+j*nx+i];
               weightsUnavailable[k*nxy+j*nx+i] = real_t(0);
               continue;
            }
            // check y direction
            if( weights[k*nxy+(j-cy)*nx+i] > real_t(0) && weights[k*nxy+(j-2*cy)*nx+i] > real_t(0) )
            {
               weights[k*nxy+(j-cy)*nx+i] += real_t(2)*weightsUnavailable[k*nxy+j*nx+i];
               weights[k*nxy+(j-2*cy)*nx+i] -= weightsUnavailable[k*nxy+j*nx+i];
               weightsUnavailable[k*nxy+j*nx+i] = real_t(0);
               continue;
            }
            // check z direction
            if( weights[(k-cz)*nxy+j*nx+i] > real_t(0) && weights[(k-2*cz)*nxy+j*nx+i] > real_t(0) )
            {
               weights[(k-cz)*nxy+j*nx+i] += real_t(2)*weightsUnavailable[k*nxy+j*nx+i];
               weights[(k-2*cz)*nxy+j*nx+i] -= weightsUnavailable[k*nxy+j*nx+i];
               weightsUnavailable[k*nxy+j*nx+i] = real_t(0);
               continue;
            }
         }
      }
*/
      // scale available weights by the total amount of unavailable weights such that afterwards sum over all weights is 1
      const real_t sumOfWeightsUnavailable = std::accumulate(weightsUnavailable.begin(), weightsUnavailable.end(), real_t(0) );
      const real_t sumOfWeights = std::accumulate(weights.begin(), weights.end(), real_t(0) );

      // check if at least one cell was available, to prevent division by 0
      if( sumOfWeights <= real_t(0) )
         return;

      const real_t scalingFactor = real_t(1) + sumOfWeightsUnavailable / sumOfWeights;

      for( auto weightIt = weights.begin(); weightIt != weights.end(); ++weightIt )
      {
         *weightIt *= scalingFactor;
      }

      // interpolate value to interpolation position using the previously calculated weights
      // values to not be included have a weight of 0
      for( uint_t k = uint_t(0); k < kernelSizeOneDirection; ++k)
      {
         for( uint_t j = uint_t(0); j < kernelSizeOneDirection; ++j)
         {
            for( uint_t i = uint_t(0); i < kernelSizeOneDirection; ++i)
            {
               if ( weights[k*nxy+j*nx+i] > real_t(0) )
               {
                  Cell curCell( cellIdx0x + cell_idx_c(i), cellIdx0y + cell_idx_c(j), cellIdx0z + cell_idx_c(k) );
                  addWeightedCellValue( interpolationResultBegin, curCell, weights[k*nxy+j*nx+i] );
               }
            }
         }
      }
   }

private:

   template< typename ForwardIterator_T >
   void addWeightedCellValue( ForwardIterator_T interpolationResultBegin, const Cell & curCell, const real_t & weighting )
   {
      for( uint_t f = uint_t(0); f < F_SIZE; ++f )
      {
         WALBERLA_ASSERT( !math::isnan(baseField_( curCell, f)), "NaN found in component " << f << " when interpolating from cell " << curCell );

         *interpolationResultBegin += weighting * baseField_( curCell, f);
         ++interpolationResultBegin;
      }
   }

263
   weak_ptr<StructuredBlockStorage> blockStorage_;
264
265
266
267
268
269
270
271
272
273
   const IBlock & block_;
   const BaseField_T & baseField_;
   const FlagField_T & flagField_;
   flag_t evaluationMask_;

};


} // namespace field
} // namespace walberla