ForceModel.h 32 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
//======================================================================================================================
//
//  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 ForceModel.h
//! \ingroup lbm
//! \author Florian Schornbaum <florian.schornbaum@fau.de>
//
//======================================================================================================================

#pragma once

#include "core/DataTypes.h"
#include "core/math/Vector3.h"
#include "core/math/Matrix3.h"
#include "core/mpi/BufferSizeTrait.h"
#include "core/mpi/RecvBuffer.h"
#include "core/mpi/SendBuffer.h"
#include "domain_decomposition/IBlock.h"
#include "domain_decomposition/StructuredBlockStorage.h"
#include "field/adaptors/VectorFieldAccessor.h"
#include "lbm/lattice_model/CollisionModel.h"
#include "lbm/lattice_model/EquilibriumDistribution.h"

36
37
#include <type_traits>

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


namespace walberla {
namespace lbm {
namespace force_model {



//**********************************************************************************************************************
/*!
*   \section docForceModel Force Model for the LBM
*
*   Every force model must implement the following concept:
*
*    1. Every force model must have a "tag". See force_model::None_tag and class force_model::None. If you write your
*       own force model and none of the existing tags fit, just define your own empty struct and use it as tag.
*    2. Every force model must define a type "DirectionIndependentTerms_T" which must be returned by function
*       "directionIndependentTerms" (see 9) and which is passed to function "forceTerm" (see 10) during the collision
*       step. [If your force model does not have or does not need to calculate any common, direction-independent terms,
*       you can use the empty struct force_model::NoDirectionIndependentTerms - see class force_model::None as an example]
*    3. static const bool shiftMacVel: specifies whether or not during the evaluation of the macroscopic velocity there
*                                      is a shift by F/2
*    4. static const bool shiftEquVel: specifies whether or not during the evaluation of the equilibrium velocity there
*                                      is a shift by F/2
*    5. static const bool constant: true, if the body force is constant for all cells throughout the entire simulation
*                                   (false otherwise)
*    6. void configure( IBlock & block, StructuredBlockStorage & sbs ): this function is called when a force model
*                                                                       instance is assigned to a block - can be used to
*                                                                       scale stored values to a different grid level
*    7. const Vector3<real_t> force() const: must return the body force (this function must only exist if constant == true)
*    8. const Vector3<real_t> force( const cell_idx_t x, const cell_idx_t y, const cell_idx_t z ) const:
*        -> must return the force for block local cell (x,y,z) - this function must also exist for constant forces!
*    9. template< typename LatticeModel_T >
*       DirectionIndependentTerms_T directionIndependentTerms( const cell_idx_t x, const cell_idx_t y, const cell_idx_t z,
*                                                              const Vector3<real_t> & velocity, const real_t rho,
*                                                              const real_t omega ) const:
*        -> can be used to pre-calculate common, direction-independent terms that are needed for every lattice direction
*           of cell (x,y,z)
*           Parameters: (x,y,z) = the cell that is currently processed, velocity/rho = the velocity and density of the
*                       equilibrium distribution, omega = relaxation parameter that corresponds to the lattice viscosity
*   10. template< typename LatticeModel_T >
*       real_t forceTerm( const cell_idx_t x, const cell_idx_t y, const cell_idx_t z,
*                         const Vector3<real_t> & velocity, const real_t rho,
*                         const DirectionIndependentTerms_T & commonTerms,
*                         const real_t w, const real_t cx, const real_t cy, const real_t cz, const real_t omega ) const:
*        -> must evaluate the force term of the LBM collision step - called from the LB compute kernel for every lattice
*           direction of cell (x,y,z)
*           Parameters: (x,y,z) = the cell that is currently processed, velocity/rho = the velocity and density of the
*                       equilibrium distribution, commonTerms = direction-independent terms calculated by function
*                       "directionIndependentTerms" (see 9), w = lattice model weighting factor, (cx,cy,cz) = lattice
*                       direction, omega = relaxation parameter that corresponds to the lattice viscosity
*   11. bool setConstantBodyForceIfPossible( const Vector3<real_t> & force, const uint_t level = uint_t(0) ):
*        -> Must return true if a constant force can be set. In that case, the force passed in must be set.
*           'level' is the grid level 'force' corresponds to.
*
*   ATTENTION: Both force functions (7+8) and the functions calculating common terms and the force term (9+10) may be
*              called in parallel by multiple threads and therefore must be thread-safe!
*
*   For an example see classes SimpleConstant or LuoField.
*/
//**********************************************************************************************************************

// For documentation about lattice models and how to assemble a lattice model that consists of
// a collision and a force model see documentation in LatticeModelBase.h

struct None_tag {};
struct Simple_tag {}; // Refers to a simple force model which introduces the following additional force term in the
                      // collision process: 3 * (lattice_weight)_i * (e)_i * (force)_i [often: (force)_i = rho * acceleration]
                      // Should only be used with constant forces!
                      // Shifts the macroscopic velocity by F/2, but does not change the equilibrium velocity.
struct EDM_tag {};    // Refers to the exact difference method by Kupershtokh
                      // Shifts the macroscopic velocity by F/2, but does not change the equilibrium velocity.
struct Luo_tag {};    // Refers to the force model by Luo which introduces a more complex force term in the collision process:
                      // F_i = w_i * [ (c_i - u) / (c_s^2) + (c_i * (c_i * u)) / (c_s^4) ] * F
                      // Shifts the macroscopic velocity by F/2, but does not change the equilibrium velocity.
struct Guo_tag {};    // Refers to the force model by Guo which introduces a more complex force term in the collision process:
                      // F_i = w_i * [ 1 - 1 / (2 * tau) ] * [ (c_i - u) / (c_s^2) + (c_i * (c_i * u)) / (c_s^4) ] * F
                      // Adapts the calculation of the macroscopic velocity as well as the equilibrium velocity (both shifted by F/2)!

struct Correction_tag{}; // refers to correction terms proposed in Jonas Latt's thesis


/// Used as return type if there are no common, direction-independent terms for the force term
struct NoDirectionIndependentTerms {};


/// Returns the body force for level 'targetLevel' given the body force 'force_level' on level 'level'
inline Vector3< real_t > levelDependentBodyForce( const uint_t targetLevel, const Vector3< real_t > & force_level, const uint_t level )
{
   const real_t powTwoTarget = real_c( uint_t(1) << targetLevel );
   const real_t powTwoLevel  = real_c( uint_t(1) << level );
   return force_level * ( powTwoLevel / powTwoTarget );
}

/// Returns the acceleration for level 'targetLevel' given the acceleration 'acceleration_level' on level 'level'
inline Vector3< real_t > levelDependentAcceleration( const uint_t targetLevel, const Vector3< real_t > & acceleration_level, const uint_t level )
{
   const real_t powTwoTarget = real_c( uint_t(1) << targetLevel );
   const real_t powTwoLevel  = real_c( uint_t(1) << level );
   return acceleration_level * ( powTwoLevel / powTwoTarget );
}



class None
{
public:

146
147
   using tag = None_tag;
   using DirectionIndependentTerms_T = NoDirectionIndependentTerms;
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

   static const bool shiftMacVel = false;
   static const bool shiftEquVel = false;

   static const bool constant = true;

   void pack( mpi::SendBuffer & ) const {}
   void unpack( mpi::RecvBuffer & ) {}

   void configure( IBlock & /*block*/, StructuredBlockStorage & /*sbs*/ ) const {}
   
   const Vector3<real_t> force() const { return Vector3<real_t>(); }
   const Vector3<real_t> force( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/ ) const { return Vector3<real_t>(); }
   
   template< typename LatticeModel_T >
   DirectionIndependentTerms_T directionIndependentTerms( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/,
Michael Kuron's avatar
Michael Kuron committed
164
                                                          const Vector3<real_t> & /*velocity*/, const real_t /*rho*/, const real_t /*omega*/, const real_t /*omega_bulk*/, const real_t /*omega_odd*/ ) const
165
166
167
168
169
   { return DirectionIndependentTerms_T(); }

   template< typename LatticeModel_T >
   real_t forceTerm( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/, const Vector3<real_t> & /*velocity*/, const real_t /*rho*/,
                     const DirectionIndependentTerms_T & /*commonTerms*/, const real_t /*w*/,
Michael Kuron's avatar
Michael Kuron committed
170
                     const real_t /*cx*/, const real_t /*cy*/, const real_t /*cz*/, const real_t /*omega*/, const real_t /*omega_bulk*/, const real_t /*omega_odd*/ ) const { return real_t(0); }
171
172
173
174
175
176
177
178
179
180
181
182

   bool setConstantBodyForceIfPossible( const Vector3<real_t> &, const uint_t = uint_t(0) ) { return false; }
};



/// For the incompressible LBM, in lattice units, the body force is equal to the acceleration, since
/// bodyForce_lattice = density_lattice * acceleration_lattice and density_lattice is implicitly set to 1
class SimpleConstant
{
public:

183
184
   using tag = Simple_tag;
   using DirectionIndependentTerms_T = NoDirectionIndependentTerms;
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

   static const bool shiftMacVel = true;
   static const bool shiftEquVel = false;

   static const bool constant = true;

   SimpleConstant( const Vector3< real_t > & bodyForce, const uint_t level = uint_t(0) ) :
      bodyForce_( bodyForce ), level_( level ) {}
   SimpleConstant( const real_t vx, const real_t vy, const real_t vz, const uint_t level = uint_t(0) ) :
      bodyForce_(vx,vy,vz), level_( level ) {}

   void pack( mpi::SendBuffer & buffer ) const { buffer << bodyForce_ << level_; }
   void unpack( mpi::RecvBuffer & buffer ) { buffer >> bodyForce_ >> level_; }

   void configure( IBlock & block, StructuredBlockStorage & sbs )
   {
      const uint_t level = level_;
      level_ = sbs.getLevel( block );
      bodyForce_ = levelDependentBodyForce( level_, bodyForce_, level );
   }

   /// "force_level" is the level that corresponds to "acceleration"
   void reset( const Vector3< real_t > & bodyForce, const uint_t force_level = uint_t(0) )
   {
      bodyForce_ = levelDependentBodyForce( level_, bodyForce, force_level );
   }

   const Vector3< real_t > & force() const { return bodyForce_; }
   const Vector3< real_t > & force( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/ ) const { return bodyForce_; }
   
   template< typename LatticeModel_T >
   DirectionIndependentTerms_T directionIndependentTerms( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/,
Michael Kuron's avatar
Michael Kuron committed
217
                                                          const Vector3<real_t> & /*velocity*/, const real_t /*rho*/, const real_t /*omega*/, const real_t /*omega_bulk*/, const real_t /*omega_odd*/ ) const
218
219
220
221
222
   { return DirectionIndependentTerms_T(); }

   template< typename LatticeModel_T >
   real_t forceTerm( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/, const Vector3<real_t> & /*velocity*/, const real_t /*rho*/,
                     const DirectionIndependentTerms_T & /*commonTerms*/, const real_t w,
Michael Kuron's avatar
Michael Kuron committed
223
                     const real_t cx, const real_t cy, const real_t cz, const real_t /*omega*/, const real_t /*omega_bulk*/, const real_t /*omega_odd*/ ) const
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
   {
      return real_t(3.0) * w * ( cx * bodyForce_[0] + cy * bodyForce_[1] + cz * bodyForce_[2] );
   }
   
   /// "force_level" is the level that corresponds to "acceleration"
   bool setConstantBodyForceIfPossible( const Vector3< real_t > & bodyForce, const uint_t force_level = uint_t(0) )
   {
      reset( bodyForce, force_level );
      return true;
   }

private:

   Vector3< real_t > bodyForce_;

   uint_t level_;
};



/// \cite kupershtokh2003calculations, \cite kupershtokh2004incorporating
template< typename ForceField_T > // ForceField_T: Field with fSize = 1 and data type = Vector3< real_t >
class EDMField
{
private:

   template< typename LatticeModel_T, class Enable = void >
   struct DirectionIndependentTerm
   {
      static real_t get( const real_t ) { return real_t(0); }
   };
   template< typename LatticeModel_T >
256
   struct DirectionIndependentTerm< LatticeModel_T, typename std::enable_if< LatticeModel_T::compressible >::type >
257
258
259
260
261
262
263
264
265
266
267
268
269
   {
      static real_t get( const real_t rho ) { return real_t(1) / rho; }
   };

   template< typename LatticeModel_T, class Enable = void >
   struct ShiftedVelocity
   {
      static Vector3<real_t> get( const Vector3<real_t> & velocity, const Vector3<real_t> & force, const real_t )
      {
         return velocity + force;
      }
   };
   template< typename LatticeModel_T >
270
   struct ShiftedVelocity< LatticeModel_T, typename std::enable_if< LatticeModel_T::compressible >::type >
271
272
273
274
275
276
277
278
279
   {
      static Vector3<real_t> get( const Vector3<real_t> & velocity, const Vector3<real_t> & force, const real_t rhoInv )
      {
         return velocity + force * rhoInv;
      }
   };

public:

280
281
282
   using tag = EDM_tag;
   using DirectionIndependentTerms_T = real_t;
   using ForceField = ForceField_T;
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

   static const bool shiftMacVel = true;
   static const bool shiftEquVel = false;

   static const bool constant = false;

   EDMField( const BlockDataID & forceFieldId ) :
      forceFieldId_( forceFieldId ), forceField_( NULL ) {}

   void pack( mpi::SendBuffer & buffer ) const { buffer << forceFieldId_; }
   void unpack( mpi::RecvBuffer & buffer ) { buffer >> forceFieldId_; }

   void configure( IBlock & block, StructuredBlockStorage & /*sbs*/ )
   {
      forceField_ = block.getData< ForceField_T >( forceFieldId_ );
   }

   typename field::VectorFieldAccessor<ForceField_T>::vector_or_constRefVector force( const cell_idx_t x, const cell_idx_t y, const cell_idx_t z ) const
   {
      return field::VectorFieldAccessor<ForceField_T>::get( forceField_, x,y,z);
   }

   template< typename LatticeModel_T >
   real_t directionIndependentTerms( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/,
Michael Kuron's avatar
Michael Kuron committed
307
                                     const Vector3<real_t> & /*velocity*/, const real_t rho, const real_t /*omega*/, const real_t /*omega_bulk*/, const real_t /*omega_odd*/ ) const
308
309
310
311
312
313
314
   {
      return DirectionIndependentTerm< LatticeModel_T >::get( rho );
   }

   template< typename LatticeModel_T >
   real_t forceTerm( const cell_idx_t x, const cell_idx_t y, const cell_idx_t z, const Vector3<real_t> & velocity, const real_t rho,
                     const real_t commonTerm, const real_t w,
Michael Kuron's avatar
Michael Kuron committed
315
                     const real_t cx, const real_t cy, const real_t cz, const real_t /*omega*/, const real_t /*omega_bulk*/, const real_t /*omega_odd*/ ) const
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
   {
      const auto shiftedVelocity = ShiftedVelocity< LatticeModel_T >::get( velocity, force(x,y,z), commonTerm );

      return EquilibriumDistribution< LatticeModel_T >::get( cx, cy, cz, w, shiftedVelocity, rho )
               - EquilibriumDistribution< LatticeModel_T >::get( cx, cy, cz, w, velocity, rho );
   }

   bool setConstantBodyForceIfPossible( const Vector3< real_t > &, const uint_t = uint_t(0) ) { return false; }

private:

   BlockDataID forceFieldId_;
   ForceField_T * forceField_;
};



/// For the incompressible LBM, in lattice units, the body force is equal to the acceleration, since
/// bodyForce_lattice = density_lattice * acceleration_lattice and density_lattice is implicitly set to 1.
/// \cite luo1993lattice, \cite shan1994simulation
class LuoConstant
{
public:

340
341
   using tag = Luo_tag;
   using DirectionIndependentTerms_T = NoDirectionIndependentTerms;
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

   static const bool shiftMacVel = true;
   static const bool shiftEquVel = false;

   static const bool constant = true;

   LuoConstant( const Vector3< real_t > & bodyForce, const uint_t level = uint_t(0) ) :
      bodyForce_( bodyForce ), level_( level ) {}
   LuoConstant( const real_t vx, const real_t vy, const real_t vz, const uint_t level = uint_t(0) ) :
      bodyForce_(vx,vy,vz), level_( level ) {}

   void pack( mpi::SendBuffer & buffer ) const { buffer << bodyForce_ << level_; }
   void unpack( mpi::RecvBuffer & buffer ) { buffer >> bodyForce_ >> level_; }

   void configure( IBlock & block, StructuredBlockStorage & sbs )
   {
      const uint_t level = level_;
      level_ = sbs.getLevel( block );
      bodyForce_ = levelDependentBodyForce( level_, bodyForce_, level );
   }

   /// "force_level" is the level that corresponds to "bodyForce"
   void reset( const Vector3< real_t > & bodyForce, const uint_t force_level = uint_t(0) )
   {
      bodyForce_ = levelDependentBodyForce( level_, bodyForce, force_level );
   }

   const Vector3< real_t > & force() const { return bodyForce_; }
   const Vector3< real_t > & force( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/ ) const { return bodyForce_; }
   
   template< typename LatticeModel_T >
   DirectionIndependentTerms_T directionIndependentTerms( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/,
Michael Kuron's avatar
Michael Kuron committed
374
                                                          const Vector3<real_t> & /*velocity*/, const real_t /*rho*/, const real_t /*omega*/, const real_t /*omega_bulk*/, const real_t /*omega_odd*/ ) const
375
376
377
378
379
   { return DirectionIndependentTerms_T(); }

   template< typename LatticeModel_T >
   real_t forceTerm( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/, const Vector3<real_t> & velocity, const real_t /*rho*/,
                     const DirectionIndependentTerms_T & /*commonTerms*/, const real_t w,
Michael Kuron's avatar
Michael Kuron committed
380
                     const real_t cx, const real_t cy, const real_t cz, const real_t /*omega*/, const real_t /*omega_bulk*/, const real_t /*omega_odd*/ ) const
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
   {
      const Vector3<real_t> c( cx, cy, cz );
      return real_t(3) * w * ( ( c - velocity + ( real_t(3) * ( c * velocity ) * c ) ) * bodyForce_ );
   }
   
   /// "force_level" is the level that corresponds to "bodyForce"
   bool setConstantBodyForceIfPossible( const Vector3< real_t > & bodyForce, const uint_t force_level = uint_t(0) )
   {
      reset( bodyForce, force_level );
      return true;
   }

private:

   Vector3< real_t > bodyForce_;

   uint_t level_;
};



template< typename ForceField_T > // ForceField_T: Field with fSize = 1 and data type = Vector3< real_t >
class LuoField
{
public:

407
408
409
   using tag = Luo_tag;
   using DirectionIndependentTerms_T = NoDirectionIndependentTerms;
   using ForceField = ForceField_T;
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433

   static const bool shiftMacVel = true;
   static const bool shiftEquVel = false;

   static const bool constant = false;

   LuoField( const BlockDataID & forceFieldId ) :
      forceFieldId_( forceFieldId ), forceField_( NULL ) {}

   void pack( mpi::SendBuffer & buffer ) const { buffer << forceFieldId_; }
   void unpack( mpi::RecvBuffer & buffer ) { buffer >> forceFieldId_; }

   void configure( IBlock & block, StructuredBlockStorage & /*sbs*/ )
   {
      forceField_ = block.getData< ForceField_T >( forceFieldId_ );
   }

   typename field::VectorFieldAccessor<ForceField_T>::vector_or_constRefVector force( const cell_idx_t x, const cell_idx_t y, const cell_idx_t z ) const
   {
      return field::VectorFieldAccessor<ForceField_T>::get( forceField_, x,y,z);
   }
   
   template< typename LatticeModel_T >
   DirectionIndependentTerms_T directionIndependentTerms( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/,
Michael Kuron's avatar
Michael Kuron committed
434
                                                          const Vector3<real_t> & /*velocity*/, const real_t /*rho*/, const real_t /*omega*/, const real_t /*omega_bulk*/, const real_t /*omega_odd*/ ) const
435
436
437
438
439
   { return DirectionIndependentTerms_T(); }

   template< typename LatticeModel_T >
   real_t forceTerm( const cell_idx_t x, const cell_idx_t y, const cell_idx_t z, const Vector3<real_t> & velocity, const real_t /*rho*/,
                     const DirectionIndependentTerms_T & /*commonTerms*/, const real_t w,
Michael Kuron's avatar
Michael Kuron committed
440
                     const real_t cx, const real_t cy, const real_t cz, const real_t /*omega*/, const real_t /*omega_bulk*/, const real_t /*omega_odd*/ ) const
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
   {
      const Vector3<real_t> c( cx, cy, cz );
      return real_t(3) * w * ( ( c - velocity + ( real_t(3) * ( c * velocity ) * c ) ) * force(x,y,z) );
   }

   bool setConstantBodyForceIfPossible( const Vector3< real_t > &, const uint_t = uint_t(0) ) { return false; }

private:

   BlockDataID forceFieldId_;
   ForceField_T * forceField_;
};



/// For the incompressible LBM, in lattice units, the body force is equal to the acceleration, since
/// bodyForce_lattice = density_lattice * acceleration_lattice and density_lattice is implicitly set to 1.
/// \cite guo2002discrete, \cite schiller2008thermal
class GuoConstant
{
public:

463
464
   using tag = Guo_tag;
   using DirectionIndependentTerms_T = Matrix3<real_t>;
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496

   static const bool shiftMacVel = true;
   static const bool shiftEquVel = true;

   static const bool constant = true;

   GuoConstant( const Vector3< real_t > & bodyForce, const uint_t level = uint_t(0) ) :
      bodyForce_( bodyForce ), level_( level ) {}
   GuoConstant( const real_t vx, const real_t vy, const real_t vz, const uint_t level = uint_t(0) ) :
      bodyForce_(vx,vy,vz), level_( level ) {}

   void pack( mpi::SendBuffer & buffer ) const { buffer << bodyForce_ << level_; }
   void unpack( mpi::RecvBuffer & buffer ) { buffer >> bodyForce_ >> level_; }

   void configure( IBlock & block, StructuredBlockStorage & sbs )
   {
      const uint_t level = level_;
      level_ = sbs.getLevel( block );
      bodyForce_ = levelDependentBodyForce( level_, bodyForce_, level );
   }

   /// "force_level" is the level that corresponds to "bodyForce"
   void reset( const Vector3< real_t > & bodyForce, const uint_t force_level = uint_t(0) )
   {
      bodyForce_ = levelDependentBodyForce( level_, bodyForce, force_level );
   }

   const Vector3< real_t > & force() const { return bodyForce_; }
   const Vector3< real_t > & force( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/ ) const { return bodyForce_; }
   
   template< typename LatticeModel_T >
   DirectionIndependentTerms_T directionIndependentTerms( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/,
Michael Kuron's avatar
Michael Kuron committed
497
                                                          const Vector3<real_t> & velocity, const real_t /*rho*/, const real_t omega, const real_t omega_bulk, const real_t /*omega_odd*/ ) const
498
   {
Michael Kuron's avatar
Michael Kuron committed
499
      if (std::is_same< typename LatticeModel_T::CollisionModel::tag, collision_model::MRT_tag >::value)
500
      {
501
         const real_t one_over_d  = real_t(1) / real_t(LatticeModel_T::Stencil::D);
502
503
504
505
      
         const auto common = Matrix3<real_t>::makeDiagonalMatrix( velocity * bodyForce_ );
         return (tensorProduct( velocity, bodyForce_ ) +
                 tensorProduct( bodyForce_, velocity ) -
506
507
                 common * (real_t(2)*one_over_d) ) * real_t(0.5) * ( real_t(2) - omega )
                + common * ( one_over_d * ( real_t(2) - omega_bulk ) );
508
509
510
      }
      else
      {
Michael Kuron's avatar
Michael Kuron committed
511
         WALBERLA_ASSERT_FLOAT_EQUAL( omega, omega_bulk );
512
513
514
515
516
517
518
         return DirectionIndependentTerms_T();
      }
   }

   template< typename LatticeModel_T >
   real_t forceTerm( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/, const Vector3<real_t> & velocity, const real_t /*rho*/,
                     const DirectionIndependentTerms_T & commonTerms, const real_t w,
Michael Kuron's avatar
Michael Kuron committed
519
                     const real_t cx, const real_t cy, const real_t cz, const real_t omega, const real_t /*omega_bulk*/, const real_t omega_odd ) const
520
521
   {
      const Vector3<real_t> c( cx, cy, cz );
Michael Kuron's avatar
Michael Kuron committed
522
      if (std::is_same< typename LatticeModel_T::CollisionModel::tag, collision_model::MRT_tag >::value)
523
524
525
526
527
528
      {
         const real_t one_third  = real_t(1) / real_t(3);
      
         const real_t common = (commonTerms * ( tensorProduct(c,c) - Matrix3<real_t>::makeDiagonalMatrix(one_third) )).trace();
         return real_t(3.0) * w * ( bodyForce_ * c + real_t(1.5) * common);
      }
Michael Kuron's avatar
Michael Kuron committed
529
530
531
532
533
534
      else if (std::is_same< typename LatticeModel_T::CollisionModel::tag, collision_model::TRT_tag >::value)
      {
         return real_t(3.0) * w * ( ( real_t(1) - real_t(0.5) * omega ) *
                                    ( ( c - velocity + ( real_t(3) * ( c * velocity ) * c ) ) * bodyForce_ ) +
                                    ( omega - omega_odd ) * real_t(0.5) * (c * bodyForce_) );
      }
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
      else
      {
         return real_t(3.0) * w * ( real_t(1) - real_t(0.5) * omega ) *
                                  ( ( c - velocity + ( real_t(3) * ( c * velocity ) * c ) ) * bodyForce_ );
      }
   }
   
   /// "force_level" is the level that corresponds to "bodyForce"
   bool setConstantBodyForceIfPossible( const Vector3< real_t > & bodyForce, const uint_t force_level = uint_t(0) )
   {
      reset( bodyForce, force_level );
      return true;
   }

private:

   Vector3< real_t > bodyForce_;

   uint_t level_;
};



template< typename ForceField_T > // ForceField_T: Field with fSize = 1 and data type = Vector3< real_t >
class GuoField
{
public:

563
564
565
   using tag = Guo_tag;
   using DirectionIndependentTerms_T = Matrix3<real_t>;
   using ForceField = ForceField_T;
566
567
568
569
570
571
572

   static const bool shiftMacVel = true;
   static const bool shiftEquVel = true;

   static const bool constant = false;

   GuoField( const BlockDataID & forceFieldId ) :
Sebastian Eibl's avatar
Sebastian Eibl committed
573
      forceFieldId_( forceFieldId ), forceField_( nullptr ) {}
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589

   void pack( mpi::SendBuffer & buffer ) const { buffer << forceFieldId_; }
   void unpack( mpi::RecvBuffer & buffer ) { buffer >> forceFieldId_; }

   void configure( IBlock & block, StructuredBlockStorage & /*sbs*/ )
   {
      forceField_ = block.getData< ForceField_T >( forceFieldId_ );
   }

   typename field::VectorFieldAccessor<ForceField_T>::vector_or_constRefVector force( const cell_idx_t x, const cell_idx_t y, const cell_idx_t z ) const
   {
      return field::VectorFieldAccessor<ForceField_T>::get( forceField_, x,y,z);
   }

   template< typename LatticeModel_T >
   DirectionIndependentTerms_T directionIndependentTerms( const cell_idx_t x, const cell_idx_t y, const cell_idx_t z,
Michael Kuron's avatar
Michael Kuron committed
590
                                                          const Vector3<real_t> & velocity, const real_t /*rho*/, const real_t omega, const real_t omega_bulk, const real_t /*omega_odd*/ ) const
591
   {
Michael Kuron's avatar
Michael Kuron committed
592
      if (std::is_same< typename LatticeModel_T::CollisionModel::tag, collision_model::MRT_tag >::value)
593
      {
594
         const real_t one_over_d  = real_t(1) / real_t(LatticeModel_T::Stencil::D);
595
596
597
598
      
         const auto common = Matrix3<real_t>::makeDiagonalMatrix( velocity * force(x,y,z) );
         return (tensorProduct( velocity, force(x,y,z) ) +
                 tensorProduct( force(x,y,z), velocity ) -
599
600
                 common * (real_t(2)*one_over_d) ) * real_t(0.5) * ( real_t(2) - omega )
                + common * ( one_over_d * ( real_t(2) - omega_bulk ) );
601
602
603
604
605
606
607
608
609
610
611
      }
      else
      {
         WALBERLA_ASSERT_FLOAT_EQUAL( omega, omega_bulk );
         return DirectionIndependentTerms_T();
      }
   }

   template< typename LatticeModel_T >
   real_t forceTerm( const cell_idx_t x, const cell_idx_t y, const cell_idx_t z, const Vector3<real_t> & velocity, const real_t /*rho*/,
                     const DirectionIndependentTerms_T & commonTerms, const real_t w,
Michael Kuron's avatar
Michael Kuron committed
612
                     const real_t cx, const real_t cy, const real_t cz, const real_t omega, const real_t /*omega_bulk*/, const real_t omega_odd ) const
613
614
   {
      const Vector3<real_t> c( cx, cy, cz );
Michael Kuron's avatar
Michael Kuron committed
615
      if (std::is_same< typename LatticeModel_T::CollisionModel::tag, collision_model::MRT_tag >::value)
616
617
618
619
620
621
      {
         const real_t one_third  = real_t(1) / real_t(3);
      
         const real_t common = (commonTerms * ( tensorProduct(c,c) - Matrix3<real_t>::makeDiagonalMatrix(one_third) )).trace();
         return real_t(3.0) * w * ( force(x,y,z) * c + real_t(1.5) * common);
      }
Michael Kuron's avatar
Michael Kuron committed
622
623
624
625
626
627
      else if (std::is_same< typename LatticeModel_T::CollisionModel::tag, collision_model::TRT_tag >::value)
      {
         return real_t(3.0) * w * ( ( real_t(1) - real_t(0.5) * omega ) *
                                    ( ( c - velocity + ( real_t(3) * ( c * velocity ) * c ) ) * force(x,y,z) ) +
                                    ( omega - omega_odd ) * real_t(0.5) * (c * force(x,y,z)) );
      }
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
      else
      {
         return real_t(3.0) * w * ( real_t(1) - real_t(0.5) * omega ) *
                                  ( ( c - velocity + ( real_t(3) * ( c * velocity ) * c ) ) * force(x,y,z) );
      }
   }

   bool setConstantBodyForceIfPossible( const Vector3< real_t > &, const uint_t = uint_t(0) ) { return false; }

private:

   BlockDataID forceFieldId_;
   ForceField_T * forceField_;
};



/// \cite latt2007hydrodynamic
template< typename MomentumDensityField_T >
class Correction
{
public:

651
652
   using tag = Correction_tag;
   using DirectionIndependentTerms_T = Vector3<real_t>;
653
654
655
656
657
658
659

   static const bool shiftMacVel = false;
   static const bool shiftEquVel = false;

   static const bool constant = true;

   Correction( const BlockDataID & previousRhoVelocityId ) :
Sebastian Eibl's avatar
Sebastian Eibl committed
660
      force_( real_t(0) ), previousRhoVelocityId_( previousRhoVelocityId ), previousRhoVelocity_(nullptr) {}
661
662
663
664
665
666
667
668
669
670
671
672
673
674

   void pack( mpi::SendBuffer & buffer ) const { buffer << force_ << previousRhoVelocityId_; }
   void unpack( mpi::RecvBuffer & buffer ) { buffer >> force_ >> previousRhoVelocityId_; }

   void configure( IBlock & block, StructuredBlockStorage & /*sbs*/ )
   {
      previousRhoVelocity_ = block.getData< MomentumDensityField_T >( previousRhoVelocityId_ );
   }

   const Vector3< real_t > & force() const { return force_; }
   const Vector3< real_t > & force( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/ ) const { return force_; }
   
   template< typename LatticeModel_T >
   Vector3<real_t> directionIndependentTerms( const cell_idx_t x, const cell_idx_t y, const cell_idx_t z,
Michael Kuron's avatar
Michael Kuron committed
675
                                              const Vector3<real_t> & velocity, const real_t rho, const real_t omega, const real_t /*omega_bulk*/, const real_t /*omega_odd*/ ) const
676
677
678
679
680
681
682
683
684
685
   {
      const auto rhoVelocity = rho * velocity;
      Vector3<real_t> & previousRhoVelocity = previousRhoVelocity_->get(x,y,z);
      Vector3<real_t> result = ( real_c(3) - real_c(1.5) * omega ) * ( rhoVelocity - previousRhoVelocity );
      previousRhoVelocity = rhoVelocity;
      return result;
   }

   template< typename LatticeModel_T >
   real_t forceTerm( const cell_idx_t /*x*/, const cell_idx_t /*y*/, const cell_idx_t /*z*/, const Vector3<real_t> & /*velocity*/, const real_t /*rho*/,
Michael Kuron's avatar
Michael Kuron committed
686
                     const Vector3<real_t> & commonTerm, const real_t w, const real_t cx, const real_t cy, const real_t cz, const real_t /*omega*/, const real_t /*omega_bulk*/, const real_t /*omega_odd*/ ) const
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
   {
      return w * ( cx * commonTerm[0] + cy * commonTerm[1] + cz * commonTerm[2] );
   }
   
   bool setConstantBodyForceIfPossible( const Vector3<real_t> &, const uint_t = uint_t(0) ) { return false; }

private:

   Vector3< real_t > force_;

   BlockDataID previousRhoVelocityId_;
   MomentumDensityField_T * previousRhoVelocity_;
};



} // namespace force_model
} // namespace lbm
} // namespace walberla