LBIBCell
 All Classes Functions Variables Friends Pages
SchnakenbergD2Q4u.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/GlobalSimulationParameters.hpp>
25 #include <LbmLib/include/solver/BoundaryAbstractSolver.hpp>
26 #include <LbmLib/include/solver/CDESolver/SchnakenbergD2Q4u.hpp>
27 
28 #include <UtilLib/include/Exception.hpp>
29 #include <UtilLib/include/Log.hpp>
30 #include <algorithm>
31 #include <cassert>
32 #include <numeric>
33 #include <string>
34 #include <random>
35 namespace LbmLib {
36 namespace solver {
37 namespace {
38 const double gamma = 300.0;
39 const double a = 0.3;
40 double deltaT;
41 }
42 
44  // init with C = 1 + eps where eps is a random number with mean 0 and std 0.01
45  static std::random_device rd;
46  static std::mt19937 gen(rd());
47  static std::uniform_real_distribution<> dis(-0.01, 0.01);
48  double temp = 0.25 * (1.0 + dis(gen));
49  for (auto d : cdeDirIter_) {
50  distributions_[d] = temp;
51  }
52  deltaT = 20.0 / Parameters.getIterations();
53 }
54 
55 void SchnakenbergD2Q4u::writeSolver(std::ostream* const stream) {
56  (*stream) << physicalNode_->getXPos() << '\t' << physicalNode_->getYPos();
57  for (auto d : distributions_) {
58  (*stream) << '\t' << d;
59  }
60  (*stream) << '\n';
61 }
62 
63 void SchnakenbergD2Q4u::loadSolver(std::stringstream* const stream) {
64  int x, y;
65  (*stream) >> x >> y;
66  assert(physicalNode_->getXPos() == x && "The position does not match");
67  assert(physicalNode_->getYPos() == y && "The position does not match");
68  for (auto d : cdeDirIter_) {
69  (*stream) >> distributions_[d];
70  }
71 }
72 
73 double& SchnakenbergD2Q4u::accessDistribution(const Direction& dir) {
74  assert(dir > T && dir < NE);
75  return distributions_[dir];
76 }
77 
78 void SchnakenbergD2Q4u::rescaleDistributions(const double factor) {
79  for (auto &it: this->distributions_) {
80  it *= factor;
81  }
82 }
83 
84 double SchnakenbergD2Q4u::getC() const {
85  return std::accumulate(distributions_.begin(), distributions_.end(), 0.0);
86 }
87 
89  assert(physicalNode_ != nullptr);
90  assert(distributions_[0] == 0.0);
91  // Calculate the rho
92  const double C = getC();
93 
94  // calculate the speeds
95  const double u = physicalNode_->getFluidSolver().getVelocity().x;
96  const double v = physicalNode_->getFluidSolver().getVelocity().y;
97 
98  const double w = C * 0.25;
99 
100  double temp[5];
101  temp[E] = w * (1.0 + 2.0 * u);
102  temp[N] = w * (1.0 + 2.0 * v);
103  temp[W] = w * (1.0 + 2.0 * (-u));
104  temp[S] = w * (1.0 + 2.0 * (-v));
105 
106  const double tauI = 1.0 / getTau();
107  const std::string schnakenbergD2Q4v = "SchnakenbergD2Q4v";
108  const double Cv = physicalNode_->getCDESolverSlow(schnakenbergD2Q4v).getC();
109  const double reaktionTerm = 0.25 * deltaT * gamma * (a - C + C * C * Cv);
110 
111  for (auto d : cdeDirIter_) {
112  double tempD = distributions_[d];
113  // compute non equilibirum
114  // make relaxation
115  distributions_[d] = tempD - tempD * tauI + temp[d] * tauI +
116  reaktionTerm;
117  }
118 
119 
120  // preparation for advect step
121  localSwap();
122 }
123 
124 double SchnakenbergD2Q4u::calculateEquilibrium(const Direction& dir) {
125  const double C = getC();
126  // calculate the speeds
127  const double u = physicalNode_->getFluidSolver().getVelocity().x;
128  const double v = physicalNode_->getFluidSolver().getVelocity().y;
129 
130  const double w = C / 4.0;
131 
132  switch (dir) {
133  case E:
134  return w * (1.0 + 2.0 * u);
135  break;
136  case N:
137  return w * (1.0 + 2.0 * v);
138  break;
139  case W:
140  return w * (1.0 + 2.0 * (-u));
141  break;
142  case S:
143  return w * (1.0 + 2.0 * (-v));
144  break;
145  default:
146  assert(
147  false &&
148  "you want to get a inverse direction of Direction that does not exist");
149  }
150  return 0;
151 }
152 
154  assert(physicalNode_ != nullptr);
155  std::swap(distributions_[getInverseDirection(W)],
158  std::swap(distributions_[getInverseDirection(S)],
161 }
162 
163 void SchnakenbergD2Q4u::localSwap() {
164  std::swap(distributions_[E], distributions_[W]);
165  std::swap(distributions_[N], distributions_[S]);
166 }
167 
169  double sumC = 0.0;
170  int counter = 0;
171  for (auto d : cdeDirIter_) {
172  // if it has no boundary neighbour and the neighbour is in the same domain then get the concentration
173  if ((this->physicalNode_->getBoundaryNeighbour(d) == nullptr) &&
176  getDomainIdentifier()) ) {
178  solverID_).getC();
179  counter++;
180  }
181  }
182  if (counter == 0) {
183  std::array<Direction, 4> dir {{NE, NW, SW, SE}
184  };
185  for (auto d : dir) {
186  // we need to check the diagonals as it does not work in the other directions
187  if (this->physicalNode_->getDomainIdentifier() ==
189  getDomainIdentifier()) {
190  sumC +=
192  solverID_).getC();
193  counter++;
194  }
195  }
196  LOG(UtilLib::logINFO) <<
197  "the default initialisation failed. Therefore the node was reinitialised from the diagonal directions";
198  }
199  if (counter == 0) {
201  "The cde solver failed to reinitialise the node, this might be due to a stange geometry");
202  }
203  sumC /= static_cast<double>(counter);
204  for (auto d : cdeDirIter_) {
205  distributions_[d] = sumC / 4.0;
206  }
207  this->collide();
208 }
209 
210 const std::string SchnakenbergD2Q4u::name = "SchnakenbergD2Q4u";
211 
212 
213 CDEDirectionsIteratorD2Q4 const SchnakenbergD2Q4u::cdeDirIter_ =
215 
216 SchnakenbergD2Q4u::SchnakenbergD2Q4u() : BaseCDESolver(),
217  distributions_(std::array<double,
218  5> {{0.0, 0.0, 0.0,
219  0.0, 0.0}
220  }
221  )
222 {}
223 }
224 } // end namespace
solver::CDEAbstractSolver & getCDESolverSlow(const std::string &name) const
getCDESolverSlow Getter method for the cde Solver
virtual void reinitialise()
reinitialise this solver as the corresponding physical node has switched domain
The Base class for all CDESolver implementations This classes uses the recursive template idiom to au...
virtual void collide()
collide The collision step of the LBM
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
T x
x the value in x direction
Definition: Field.hpp:50
virtual double & accessDistribution(const Direction &dir)=0
accessDistribution Access to the distribution
virtual void initSolver()
initSolver Use this to initalise the solver
unsigned int getDomainIdentifier() const
getter for the Domain Identifier of this node
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
virtual double getC() const =0
getC Calculates the concentration on this node
virtual double calculateEquilibrium(const Direction &dir)
calculateEquilibrium calculates the equilibirum for direction dir
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 void advect()
advect The advect step of the LBM
virtual void rescaleDistributions(const double factor)
Rescales all distributions by a factor.
BoundaryNode * getBoundaryNeighbour(const Direction &d) const
getBoundaryNeighbour Getter method to access the Boundary Neighbour
virtual double getC() const
getC Calculates the concentration on this node
virtual double & accessDistribution(const Direction &dir)
accessDistribution Access to the distribution
int getYPos() const
getYPos Getter for the Y position
solver::CDEAbstractSolver & getCDESolver(size_t id) const
getCDESolver Getter method for the cde Solver
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