summaryrefslogtreecommitdiff
path: root/src/dynamics/mrtHelpers.h
blob: 96ef7553f956431f2dfbcd1a287c9b699f44157d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*  This file is part of the OpenLB library
 *
 *  Copyright (C) 2006, 2007, 2013 Jonas Latt, Mathias J. Krause, Geng Liu
 *  E-mail contact: info@openlb.net
 *  The most recent release of OpenLB can be downloaded at
 *  <http://www.openlb.net/>
 *
 *  This program 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 2
 *  of the License, or (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA  02110-1301, USA.
*/

/** \file
 * Helper functions for the implementation of LB dynamics. This file is all
 * about efficiency. The generic template code is specialized for commonly
 * used Lattices, so that a maximum performance can be taken out of each
 * case.
 */
#ifndef MRT_HELPERS_H
#define MRT_HELPERS_H

#include "lbHelpers.h"
#include "mrtLatticeDescriptors.h"

namespace olb {

 

/// All helper functions are inside this structure
template<typename T, typename DESCRIPTOR>
struct mrtHelpers {
  static_assert(
    std::is_same<typename DESCRIPTOR::category_tag, descriptors::tag::MRT>::value,
    "DESCRIPTOR is tagged as MRT");

  /// Computation of equilibrium distribution (in momenta space)
  static T equilibrium( int iPop, T rho, const T u[DESCRIPTOR::d],
                        const T uSqr )
  {
    T equ = T();
    for (int jPop = 0; jPop < DESCRIPTOR::q; ++jPop) {
      equ += descriptors::m<T,DESCRIPTOR>(iPop,jPop) *
             (lbHelpers<T,DESCRIPTOR>::equilibrium(jPop,rho,u,uSqr) +
              descriptors::t<T,DESCRIPTOR>(jPop));
    }

    return equ;
  }

  /// Computation of all equilibrium distribution (in momenta space)
  static void computeEquilibrium( T momentaEq[DESCRIPTOR::q],
                                  T rho, const T u[DESCRIPTOR::d],
                                  const T uSqr )
  {
    for (int iPop = 0; iPop < DESCRIPTOR::q; ++iPop) {
      momentaEq[iPop] = T();
      for (int jPop = 0; jPop < DESCRIPTOR::q; ++jPop) {
        momentaEq[iPop] += descriptors::m<T,DESCRIPTOR>(iPop,jPop) *
                           (lbHelpers<T,DESCRIPTOR>::equilibrium(jPop,rho,u,uSqr) +
                            descriptors::t<T,DESCRIPTOR>(jPop));
      }
    }
  }

  static void computeMomenta(T momenta[DESCRIPTOR::q], Cell<T,DESCRIPTOR> &cell)
  {
    for (int iPop = 0; iPop < DESCRIPTOR::q; ++iPop) {
      momenta[iPop] = T();
      for (int jPop = 0; jPop < DESCRIPTOR::q; ++jPop) {
        momenta[iPop] += descriptors::m<T,DESCRIPTOR>(iPop,jPop) *
                         (cell[jPop] + descriptors::t<T,DESCRIPTOR>(jPop));
      }
    }
  }

  /// MRT collision step
  static T mrtCollision( Cell<T,DESCRIPTOR>& cell,
                         T rho, const T u[DESCRIPTOR::d],
                         T invM_S[DESCRIPTOR::q][DESCRIPTOR::q])
  {
    T uSqr = util::normSqr<T,DESCRIPTOR::d>(u);
    T momenta[DESCRIPTOR::q];
    T momentaEq[DESCRIPTOR::q];

    computeMomenta(momenta,cell);
    computeEquilibrium(momentaEq,rho,u,uSqr);

    for (int iPop=0; iPop < DESCRIPTOR::q; ++iPop) {
      T collisionTerm = T();
      for (int jPop = 0; jPop < DESCRIPTOR::q; ++jPop) {
        collisionTerm += invM_S[iPop][jPop] *
                         (momenta[jPop] - momentaEq[jPop]);
      }
      cell[iPop] -= collisionTerm;
    }

    return uSqr;
  }

  /// MRT SGS collision step
  static T mrtSGSCollision( Cell<T,DESCRIPTOR>& cell,
                            T rho, const T u[DESCRIPTOR::d],
                            T omega,
                            T invM_S_SGS[DESCRIPTOR::q][DESCRIPTOR::q])
  {
    T uSqr = util::normSqr<T,DESCRIPTOR::d>(u);
    T momenta[DESCRIPTOR::q];
    T momentaEq[DESCRIPTOR::q];


    computeMomenta(momenta,cell);
    computeEquilibrium(momentaEq,rho,u,uSqr);

    for (int iPop=0; iPop < DESCRIPTOR::q; ++iPop) {
      T collisionTerm = T();
      for (int jPop = 0; jPop < DESCRIPTOR::q; ++jPop) {
        collisionTerm += invM_S_SGS[iPop][jPop] *
                         (momenta[jPop] - momentaEq[jPop]);
      }
      cell[iPop] -= collisionTerm;
    }

    return uSqr;
  }


  /// Ladd-Verberg-I body force model for MRT
  /// A.Ladd, R. Verberg, DESCRIPTOR-Boltzmann simulations of particle-fluid suspensions, Journal of Statistical Physics 104(2001)
  static void addExternalForce( Cell<T,DESCRIPTOR>& cell,
                                T rho,
                                const T u[DESCRIPTOR::d],
                                T invM_S[DESCRIPTOR::q][DESCRIPTOR::q])
  {
    T* force = cell.template getFieldPointer<descriptors::FORCE>();
    T f_u = T();
    for (int iD=0; iD < DESCRIPTOR::d; ++iD) {
      f_u += force[iD]*u[iD];
    }

    for (int iPop=0; iPop < DESCRIPTOR::q; ++iPop) {
      T c_u = T();
      T c_f = T();
      for (int iD=0; iD < DESCRIPTOR::d; ++iD) {
        c_u += descriptors::c<DESCRIPTOR>(iPop,iD)*u[iD];
        c_f += descriptors::c<DESCRIPTOR>(iPop,iD)*force[iD];
      }
      T f1 = descriptors::t<T,DESCRIPTOR>(iPop)*rho*c_f*descriptors::invCs2<T,DESCRIPTOR>();
      T f2 = descriptors::t<T,DESCRIPTOR>(iPop)*rho*(c_u*c_f*descriptors::invCs2<T,DESCRIPTOR>()-f_u)*descriptors::invCs2<T,DESCRIPTOR>();

      T invMsM = T();
      for (int jPop=0; jPop < DESCRIPTOR::q; ++jPop) {
        invMsM += invM_S[iPop][jPop]*descriptors::m<T,DESCRIPTOR>(jPop,iPop);
      }
      cell[iPop] += f1 + f2 - invMsM*f2/2.;
    }

    return;
  }

};  // struct mrtHelpers

}  // namespace olb

// The specialized code is directly included. That is because we never want
// it to be precompiled so that in both the precompiled and the
// "include-everything" version, the compiler can apply all the
// optimizations it wants.
#include "mrtHelpers2D.h"
#include "mrtHelpers3D.h"

#endif