summaryrefslogtreecommitdiff
path: root/src/refinement/grid2D.hh
blob: 99a25633813ca91c921a6d3724e29dc63f8e8563 (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
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
/*  This file is part of the OpenLB library
 *
 *  Copyright (C) 2019 Adrian Kummerländer
 *  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.
*/

#ifndef REFINEMENT_GRID_2D_HH
#define REFINEMENT_GRID_2D_HH

#include "grid2D.h"
#include "coupler2D.hh"

#include "communication/heuristicLoadBalancer.h"

namespace olb {


template <typename T, template<typename> class DESCRIPTOR>
std::unique_ptr<Grid2D<T,DESCRIPTOR>> Grid2D<T,DESCRIPTOR>::make(
                                     IndicatorF2D<T>& domainF,
                                     int resolution, T tau, int re)
{
  return std::unique_ptr<Grid2D<T,DESCRIPTOR>>(
           new Grid2D<T,DESCRIPTOR>(domainF, resolution, tau, re)
         );
}

template <typename T, template<typename> class DESCRIPTOR>
Grid2D<T,DESCRIPTOR>::Grid2D(IndicatorF2D<T>& domainF, int resolution, T tau, int re):
  _converter(new UnitConverterFromResolutionAndRelaxationTime<T,DESCRIPTOR>(
               resolution, // resolution: number of voxels per charPhysL
               tau,        // latticeRelaxationTime: relaxation time, has to be greater than 0.5!
               T{1},       // charPhysLength: reference length of simulation geometry
               T{1},       // charPhysVelocity: maximal/highest expected velocity during simulation in __m / s__
               T{1./re},   // physViscosity: physical kinematic viscosity in __m^2 / s__
               T{1},       // physDensity: physical density in __kg / m^3__
               T{1})),
  _cuboids(new CuboidGeometry2D<T>(
             domainF,
             _converter->getConversionFactorLength(),
#ifdef PARALLEL_MODE_MPI
             singleton::mpi().getSize()
#else
             1
#endif
             )),
  _balancer(new HeuristicLoadBalancer<T>(
              *_cuboids)),
  _geometry(new SuperGeometry2D<T>(
              *_cuboids,
              *_balancer,
              2)),
  _lattice(new SuperLattice2D<T,DESCRIPTOR>(
             *_geometry))
{
  _converter->print();
}

template <typename T, template<typename> class DESCRIPTOR>
UnitConverter<T,DESCRIPTOR>& Grid2D<T,DESCRIPTOR>::getConverter()
{
  return *_converter;
}

template <typename T, template<typename> class DESCRIPTOR>
CuboidGeometry2D<T>& Grid2D<T,DESCRIPTOR>::getCuboidGeometry()
{
  return *_cuboids;
}

template <typename T, template<typename> class DESCRIPTOR>
LoadBalancer<T>& Grid2D<T,DESCRIPTOR>::getLoadBalancer()
{
  return *_balancer;
}

template <typename T, template<typename> class DESCRIPTOR>
SuperGeometry2D<T>& Grid2D<T,DESCRIPTOR>::getSuperGeometry()
{
  return *_geometry;
}

template <typename T, template<typename> class DESCRIPTOR>
SuperLattice2D<T,DESCRIPTOR>& Grid2D<T,DESCRIPTOR>::getSuperLattice()
{
  return *_lattice;
}

template <typename T, template<typename> class DESCRIPTOR>
void Grid2D<T,DESCRIPTOR>::collideAndStream()
{
  for ( auto& fineCoupler : _fineCouplers ) {
    fineCoupler->store();
  }

  this->getSuperLattice().collideAndStream();

  for ( auto& fineGrid : _fineGrids ) {
    fineGrid->collideAndStream();
  }

  for ( auto& fineCoupler : _fineCouplers ) {
    fineCoupler->interpolate();
    fineCoupler->couple();
  }

  for ( auto& fineGrid : _fineGrids ) {
    fineGrid->collideAndStream();
  }

  for ( auto& fineCoupler : _fineCouplers ) {
    fineCoupler->store();
    fineCoupler->couple();
  }

  for ( auto& coarseCoupler : _coarseCouplers ) {
    coarseCoupler->couple();
  }
}

template <typename T, template<typename> class DESCRIPTOR>
FineCoupler2D<T,DESCRIPTOR>& Grid2D<T,DESCRIPTOR>::addFineCoupling(
  Grid2D<T,DESCRIPTOR>& fineGrid, Vector<T,2> origin, Vector<T,2> extend)
{
  _fineCouplers.emplace_back(
    new FineCoupler2D<T,DESCRIPTOR>(
      *this, fineGrid, origin, extend));
  return *_fineCouplers.back();
}

template <typename T, template<typename> class DESCRIPTOR>
CoarseCoupler2D<T,DESCRIPTOR>& Grid2D<T,DESCRIPTOR>::addCoarseCoupling(
  Grid2D<T,DESCRIPTOR>& fineGrid, Vector<T,2> origin, Vector<T,2> extend)
{
  _coarseCouplers.emplace_back(
    new CoarseCoupler2D<T,DESCRIPTOR>(
      *this, fineGrid, origin, extend));
  return *_coarseCouplers.back();
}

template <typename T, template<typename> class DESCRIPTOR>
Grid2D<T,DESCRIPTOR>& Grid2D<T,DESCRIPTOR>::refine(IndicatorF2D<T>& domainF)
{
  _fineGrids.emplace_back(
    new Grid2D<T,DESCRIPTOR>(
      domainF,
      2*getConverter().getResolution(),
      2*getConverter().getLatticeRelaxationTime() - 0.5,
      getConverter().getReynoldsNumber()
    ));
  return *_fineGrids.back();
}

template <typename T, template<typename> class DESCRIPTOR>
Vector<T,2> Grid2D<T,DESCRIPTOR>::alignOriginToGrid(Vector<T,2> physR) const
{
  Vector<int,3> latticeR{};
  _cuboids->getLatticeR(physR, latticeR);
  return _cuboids->getPhysR(latticeR.toStdVector());
}

template <typename T, template<typename> class DESCRIPTOR>
Vector<T,2> Grid2D<T,DESCRIPTOR>::alignExtendToGrid(Vector<T,2> extend) const
{
  const T deltaX = _converter->getPhysDeltaX();
  return floor(extend / deltaX) * deltaX;
}

template <typename T, template<typename> class DESCRIPTOR>
Grid2D<T,DESCRIPTOR>& Grid2D<T,DESCRIPTOR>::refine(Vector<T,2> origin, Vector<T,2> extend)
{
  IndicatorCuboid2D<T> fineCuboid(extend, origin);
  auto& fineGrid = refine(fineCuboid);

  const Vector<T,2> extendX = {extend[0],0};
  const Vector<T,2> extendY = {0,extend[1]};

  addFineCoupling(fineGrid, origin,           extendY);
  addFineCoupling(fineGrid, origin + extendX, extendY);
  addFineCoupling(fineGrid, origin + extendY, extendX);
  addFineCoupling(fineGrid, origin,           extendX);

  const T coarseDeltaX = getConverter().getPhysDeltaX();
  const Vector<T,2> innerOrigin = origin + coarseDeltaX;
  const Vector<T,2> innerExtendX = extendX - Vector<T,2> {2*coarseDeltaX,0};
  const Vector<T,2> innerExtendY = extendY - Vector<T,2> {0,2*coarseDeltaX};

  addCoarseCoupling(fineGrid, innerOrigin,                innerExtendY);
  addCoarseCoupling(fineGrid, innerOrigin + innerExtendX, innerExtendY);
  addCoarseCoupling(fineGrid, innerOrigin + innerExtendY, innerExtendX);
  addCoarseCoupling(fineGrid, innerOrigin,                innerExtendX);

  return fineGrid;
}


}

#endif