LBIBCell
 All Classes Functions Variables Friends Pages
tutorial_02_CDESolverD2Q5_L.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/tutorial_02_CDESolverD2Q5_L.hpp>
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 #include <random>
34 #include <iomanip>
35 
36 namespace LbmLib {
37 namespace solver {
38 namespace {
39 const double gamma = 100.0;
40 const double b = 0.9;
41 const double deltaT = 1.0e-4;
42 }
43 
45  static std::random_device rd;
46  static std::mt19937 gen(rd());
47  static std::uniform_real_distribution<> dis(-0.01, 0.01);
48 
49  if (this->physicalNode_->getDomainIdentifier() == 0) {
50  for (auto d : cdeDirIter_) {
51  distributions_[d] = 0.0;
52  }
53  }
54  else {
55  for (auto d : cdeDirIter_) {
56  distributions_[d] = (1.0 + dis(gen))/5.0;
57  }
58  }
59 }
60 
61 void tutorial_02_CDESolverD2Q5_L::writeSolver(std::ostream* const stream) {
62  (*stream) << physicalNode_->getXPos() << '\t' << physicalNode_->getYPos();
63  for (auto d : distributions_) {
64  (*stream) << '\t' << d;
65  }
66  (*stream) << '\n';
67 }
68 
69 void tutorial_02_CDESolverD2Q5_L::loadSolver(std::stringstream* const stream) {
70  int x, y;
71  (*stream) >> x >> y;
72  assert(physicalNode_->getXPos() == x && "The position does not match");
73  assert(physicalNode_->getYPos() == y && "The position does not match");
74  for (auto d : cdeDirIter_) {
75  (*stream) >> distributions_[d];
76  }
77 }
78 
79 double& tutorial_02_CDESolverD2Q5_L::accessDistribution(const Direction& dir) {
80  assert(dir > T && dir < NE);
81  return distributions_[dir];
82 }
83 
85  for (auto &it: this->distributions_) {
86  it *= factor;
87  }
88 }
89 
91  return std::accumulate(distributions_.begin(), distributions_.end(), 0.0);
92 }
93 
95 {
96  const double R = physicalNode_->getCDESolverSlow("tutorial_02_CDESolverD2Q5_R").getC();
97  const double L = this->getC();
98 
99 
100  if (this->physicalNode_->getDomainIdentifier() != 0) {
101  return gamma * (b - R * R * L);
102  }
103  else {
104  return 0.0;
105  }
106 }
107 
109  assert(physicalNode_ != nullptr);
110  // get the local concentration:
111  const double C = getC();
112 
113  // get the local velocity:
114  const double ux = physicalNode_->getFluidSolver().getVelocity().x;
115  const double uy = physicalNode_->getFluidSolver().getVelocity().y;
116 
117  const double w0 = C / 3.0;
118  const double w1 = C / 6.0;
119  const double tauI = 1.0 / getTau();
120 
121  // temporary computations
122  double temp[5];
123  temp[T] = w0;
124  temp[E] = w1 * (1.0 + ux * 3.0);
125  temp[N] = w1 * (1.0 + uy * 3.0);
126  temp[W] = w1 * (1.0 + (-ux) * 3.0);
127  temp[S] = w1 * (1.0 + (-uy) * 3.0);
128 
129  const double reactionTerm = this->reaction();
130  const double reactionTerm_ZERO = deltaT*reactionTerm / 3.0; // for the ZERO distribution
131  const double reactionTerm_OTHERS = deltaT*reactionTerm / 6.0; // for all the other distributions
132 
133  distributions_[T] = distributions_[T] - distributions_[T] * tauI + temp[T] * tauI + reactionTerm_ZERO;
134  distributions_[E] = distributions_[E] - distributions_[E] * tauI + temp[E] * tauI + reactionTerm_OTHERS;
135  distributions_[N] = distributions_[N] - distributions_[N] * tauI + temp[N] * tauI + reactionTerm_OTHERS;
136  distributions_[W] = distributions_[W] - distributions_[W] * tauI + temp[W] * tauI + reactionTerm_OTHERS;
137  distributions_[S] = distributions_[S] - distributions_[S] * tauI + temp[S] * tauI + reactionTerm_OTHERS;
138 
139  // preparation for advect step: this is necessary
140  localSwap();
141 }
142 
144  const double C = getC();
145  // calculate the speeds
146  const double u = physicalNode_->getFluidSolver().getVelocity().x;
147  const double v = physicalNode_->getFluidSolver().getVelocity().y;
148 
149  const double w1 = C / 6.0;
150 
151  switch (dir) {
152  case T:
153  return C / 3.0;
154  break;
155  case E:
156  return w1 * (1.0 + u * 3.0);
157  break;
158  case N:
159  return w1 * (1.0 + v * 3.0);
160  break;
161  case W:
162  return w1 * (1.0 + (-u) * 3.0);
163  break;
164  case S:
165  return w1 * (1.0 + (-v) * 3.0);
166  break;
167  default:
168  assert(
169  false &&
170  "you want to get a inverse direction of a Direction that does not exist");
171  }
172  return 0;
173 }
174 
176  assert(physicalNode_ != nullptr);
177  std::swap(distributions_[getInverseDirection(W)],
180  std::swap(distributions_[getInverseDirection(S)],
183 }
184 
185 void tutorial_02_CDESolverD2Q5_L::localSwap() {
186  std::swap(distributions_[E], distributions_[W]);
187  std::swap(distributions_[N], distributions_[S]);
188 }
189 
191  double sumC = 0.0;
192  int counter = 0;
193  const unsigned int myNodeID = this->physicalNode_->getDomainIdentifier();
194 
195  // accumulate the concenctration of appropriate (tbd) neighbors.
196  // first, try {E, N, W, S}:
197  std::array<Direction, 4> dir1 {{E, N, W, S}};
198  for (auto d : dir1) {
199  // if it has no boundary neighbour and the neighbour is in the same domain then get the concentration
200  if ((this->physicalNode_->getBoundaryNeighbour(d) == nullptr) &&
201  (myNodeID ==
204  solverID_).getC();
205  counter++;
206  }
207  }
208  // if not successful, try {NE,NW,SW,SE}:
209  if (counter == 0) {
210  std::array<Direction, 4> dir2 {{NE, NW, SW, SE}};
211  for (auto d : dir2) {
212  // we need to check the diagonals as it does not work in the other directions
213  if ( (this->physicalNode_->getBoundaryNeighbour(d) == nullptr) &&
214  (myNodeID == this->physicalNode_->getPhysicalNeighbour(d)->getDomainIdentifier())) {
215  sumC +=
217  solverID_).getC();
218  counter++;
219  }
220  }
221  if (counter != 0) {
222  std::stringstream message;
223  message << std::setprecision(12);
224  message << "Default initialisation on PhysicalNode ";
225  message << "("<< this->physicalNode_->getXPos()<<","<<this->physicalNode_->getYPos() <<")";
226  message << " failed. Therefore the node was reinitialised from the diagonal directions";
227  LOG(UtilLib::logINFO) << message.str().c_str();
228  }
229 
230  }
231 
232  // if still fails: initialize with neighbors, even if they are in an other domain:
233  if (counter == 0) {
234  std::stringstream message;
235  message << std::setprecision(12)
236  << "Initialization on PhysicalNode "
237  << "("<< this->physicalNode_->getXPos()<<","<<this->physicalNode_->getYPos() <<")"
238  << " failed at time "
239  << Parameters.getCurrentIteration()
240  << ". Therefore the node was initialized with all neighbors {E,N,W,S}, ignoring their domainID."
241  << " CurrentNodeID="<<this->physicalNode_->getDomainIdentifier();
242  for (auto d : dir1) {
243  // if it has no boundary neighbour and the neighbour is in the same domain then get the concentration
245  solverID_).getC();
246  counter++;
247  }
248  LOG(UtilLib::logINFO) << message.str().c_str();
249  }
250 
251  sumC /= static_cast<double>(counter);
252  for (auto d : cdeDirIter_) {
253  this->distributions_[d] = sumC / 5.0;
254  }
255  this->collide();
256 }
257 
258 const std::string tutorial_02_CDESolverD2Q5_L::name = "tutorial_02_CDESolverD2Q5_L";
259 
260 
261 CDEDirectionsIteratorD2Q5 const tutorial_02_CDESolverD2Q5_L::cdeDirIter_ =
263 
264 tutorial_02_CDESolverD2Q5_L::tutorial_02_CDESolverD2Q5_L() : BaseCDESolver(),
265  distributions_(std::array<double,
266  5> {
267  {0.0, 0.0, 0.0,
268  0.0, 0.0}
269  }
270 
271  )
272 {}
273 }
274 } // end namespace
275 
solver::CDEAbstractSolver & getCDESolverSlow(const std::string &name) const
getCDESolverSlow Getter method for the cde Solver
virtual void initSolver()
initSolver Use this to initalise the solver
The Base class for all CDESolver implementations This classes uses the recursive template idiom to au...
virtual void advect()
advect The advect step of the LBM
size_t solverID_
solverID_ The ID of the solver instance. Coincides with the index in the vector PhysicalNode::cdeSolv...
virtual void loadSolver(std::stringstream *const stream)
loads the solver from the stream
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
The CDEDirectionsIteratorD2Q5 class Provides methods to handle the Directions. Use the Function Direc...
Definition: Direction.hpp:126
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
const double reaction(void) const
reaction The reaction term of the tutorial_02_CDESolverD2Q5_L solver is implemented here...
virtual void rescaleDistributions(const double factor)
Rescales all distributions by a factor.
virtual double getC() const =0
getC Calculates the concentration on this node
virtual void collide()
collide The collision step of the LBM
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 calculateEquilibrium(const Direction &dir)
calculateEquilibrium calculates the equilibirum for direction dir
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 void writeSolver(std::ostream *const stream)
writes the solver to the stream
int getYPos() const
getYPos Getter for the Y position
virtual void reinitialise()
reinitialise this solver as the corresponding physical node has switched domain
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
virtual double & accessDistribution(const Direction &dir)
accessDistribution Access to the distribution