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