LBIBCell
 All Classes Functions Variables Friends Pages
CDESolverD2Q4.cpp
1 /* Copyright (c) 2013 David Sichau <mail"at"sichau"dot"eu>
2  * 2013-2015 Simon Tanaka <tanakas"at"gmx"dot"ch>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 #include <LbmLib/include/nodes/BoundaryNode.hpp>
23 #include <LbmLib/include/nodes/PhysicalNode.hpp>
24 #include <LbmLib/include/solver/BoundaryAbstractSolver.hpp>
25 #include <LbmLib/include/solver/CDESolver/CDESolverD2Q4.hpp>
26 
27 #include <UtilLib/include/Exception.hpp>
28 #include <UtilLib/include/Log.hpp>
29 #include <algorithm>
30 #include <cassert>
31 #include <numeric>
32 #include <string>
33 namespace LbmLib {
34 namespace solver {
35 
36 
38  // init with C = 0
39  collide();
40 }
41 
42 void CDESolverD2Q4::writeSolver(std::ostream* const stream) {
43  (*stream) << physicalNode_->getXPos() << '\t' << physicalNode_->getYPos();
44  for (auto d : distributions_) {
45  (*stream) << '\t' << d;
46  }
47  (*stream) << '\n';
48 }
49 
50 void CDESolverD2Q4::loadSolver(std::stringstream* const stream) {
51  int x, y;
52  (*stream) >> x >> y;
53  assert(physicalNode_->getXPos() == x && "The position does not match");
54  assert(physicalNode_->getYPos() == y && "The position does not match");
55  for (auto d : cdeDirIter_) {
56  (*stream) >> distributions_[d];
57  }
58 }
59 
60 double& CDESolverD2Q4::accessDistribution(const Direction& dir) {
61  assert(dir > T && dir < NE);
62  return distributions_[dir];
63 }
64 
65 void CDESolverD2Q4::rescaleDistributions(const double factor) {
66  for (auto &it: this->distributions_) {
67  it *= factor;
68  }
69 }
70 
71 double CDESolverD2Q4::getC() const {
72  return std::accumulate(distributions_.begin(), distributions_.end(), 0.0);
73 }
74 
76  assert(physicalNode_ != nullptr);
77  assert(distributions_[0] == 0.0);
78  // Calculate the rho
79  const double C = getC();
80 
81  // calculate the speeds
82  const double u = physicalNode_->getFluidSolver().getVelocity().x;
83  const double v = physicalNode_->getFluidSolver().getVelocity().y;
84 
85  const double w = C / 4.0;
86  const double tauI = 1.0/ getTau();
87 
88  double temp[5];
89  temp[E] = w * (1.0 + 2.0 * u);
90  temp[N] = w * (1.0 + 2.0 * v);
91  temp[W] = w * (1.0 + 2.0 * (-u));
92  temp[S] = w * (1.0 + 2.0 * (-v));
93 
94  for (auto d : cdeDirIter_) {
95  double tempD = distributions_[d];
96  // compute non equilibirum
97  // make relaxation
98  distributions_[d] = tempD - tempD * tauI + temp[d] * tauI;
99  }
100 
101 
102  // preparation for advect step
103  localSwap();
104 }
105 
106 double CDESolverD2Q4::calculateEquilibrium(const Direction& dir) {
107  const double C = getC();
108  // calculate the speeds
109  const double u = physicalNode_->getFluidSolver().getVelocity().x;
110  const double v = physicalNode_->getFluidSolver().getVelocity().y;
111 
112  const double w = C / 4.0;
113 
114  switch (dir) {
115  case E:
116  return w * (1.0 + 2.0 * u);
117  break;
118  case N:
119  return w * (1.0 + 2.0 * v);
120  break;
121  case W:
122  return w * (1.0 + 2.0 * (-u));
123  break;
124  case S:
125  return w * (1.0 + 2.0 * (-v));
126  break;
127  default:
128  assert(
129  false &&
130  "you want to get a inverse direction of Direction that does not exist");
131  }
132  return 0;
133 }
134 
136  assert(physicalNode_ != nullptr);
137  std::swap(distributions_[getInverseDirection(W)],
140  std::swap(distributions_[getInverseDirection(S)],
143 }
144 
145 void CDESolverD2Q4::localSwap() {
146  std::swap(distributions_[E], distributions_[W]);
147  std::swap(distributions_[N], distributions_[S]);
148 }
149 
151  double sumC = 0.0;
152  int counter = 0;
153  for (auto d : cdeDirIter_) {
154  // if it has no boundary neighbour and the neighbour is in the same domain then get the concentration
155  if ((this->physicalNode_->getBoundaryNeighbour(d) == nullptr) &&
158  getDomainIdentifier()) ) {
160  solverID_).getC();
161  counter++;
162  }
163  }
164  if (counter == 0) {
165  std::array<Direction, 4> dir {{NE, NW, SW, SE}
166  };
167  for (auto d : dir) {
168  // we need to check the diagonals as it does not work in the other directions
169  if (this->physicalNode_->getDomainIdentifier() ==
171  getDomainIdentifier()) {
172  sumC +=
174  solverID_).getC();
175  counter++;
176  }
177  }
178  LOG(UtilLib::logINFO) <<
179  "the default initialisation failed. Therefore the node was reinitialised from the diagonal directions";
180  }
181  if (counter == 0) {
183  "The cde solver failed to reinitialise the node, this might be due to a stange geometry");
184  }
185  sumC /= static_cast<double>(counter);
186  for (auto d : cdeDirIter_) {
187  distributions_[d] = sumC / 4.0;
188  }
189  this->collide();
190 }
191 
192 const std::string CDESolverD2Q4::name = "CDESolverD2Q4";
193 
194 
195 CDEDirectionsIteratorD2Q4 const CDESolverD2Q4::cdeDirIter_ =
197 
198 CDESolverD2Q4::CDESolverD2Q4() : BaseCDESolver(),
199  distributions_(std::array<double,
200  5> {{0.0, 0.0, 0.0, 0.0, 0.0}
201  }
202  )
203 {}
204 }
205 } // end namespace
virtual double calculateEquilibrium(const Direction &dir)
calculateEquilibrium calculates the equilibirum for direction dir
virtual void rescaleDistributions(const double factor)
Rescales all distributions by a factor.
The Base class for all CDESolver implementations This classes uses the recursive template idiom to au...
virtual void reinitialise()
reinitialise this solver as the corresponding physical node has switched domain
size_t solverID_
solverID_ The ID of the solver instance. Coincides with the index in the vector PhysicalNode::cdeSolv...
PhysicalNode * getPhysicalNeighbour(const Direction &d) const
getPhysicalNeighbour Getter method to access the Physical Neighbour
const nodes::PhysicalNode * physicalNode_
physicalNode_ The physical Node which owns this solver
virtual double & accessDistribution(const Direction &dir)
accessDistribution Access to the distribution
T x
x the value in x direction
Definition: Field.hpp:50
virtual double & accessDistribution(const Direction &dir)=0
accessDistribution Access to the distribution
unsigned int getDomainIdentifier() const
getter for the Domain Identifier of this node
virtual void advect()
advect The advect step of the LBM
virtual double getC() const =0
getC Calculates the concentration on this node
const Field< double > & getVelocity() const
getVelocity Returns the current velocity of the fluid
double getTau() const
getTau Getter method for the tau parameter
const solver::FluidSolver & getFluidSolver() const
getFluidSolver Const getter method for the fluid Solver
int getXPos() const
getXPos Getter for the X position
virtual double getC() const
getC Calculates the concentration on this node
BoundaryNode * getBoundaryNeighbour(const Direction &d) const
getBoundaryNeighbour Getter method to access the Boundary Neighbour
virtual void initSolver()
initSolver Use this to initalise the solver
int getYPos() const
getYPos Getter for the Y position
virtual void collide()
collide The collision step of the LBM
solver::CDEAbstractSolver & getCDESolver(size_t id) const
getCDESolver Getter method for the cde Solver
virtual void loadSolver(std::stringstream *const stream)
loads the solver from the stream
virtual void writeSolver(std::ostream *const stream)
writes the solver to the stream
T y
y the value in y direction
Definition: Field.hpp:54
The CDEDirectionsIteratorD2Q4 class Provides methods to handle the Directions. Use the Function Direc...
Definition: Direction.hpp:91