LBIBCell
 All Classes Functions Variables Friends Pages
tutorial_01_CDESolverD2Q5_SIGNAL.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_01_CDESolverD2Q5_SIGNAL.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 deltaT = 1.0;
40 const unsigned int SWITCHOFF_TIME = 5000;
41 const double SIGNAL_decay = 0.00001;
42 const double SIGNAL_production = 0.0001;
43 const double SIGNAL_initalcondition = 1.0;
44 }
45 
47  if (this->physicalNode_->getDomainIdentifier() == 0) {
48  for (auto d : cdeDirIter_) {
49  distributions_[d] = 0.0;
50  }
51  }
52  else {
53  for (auto d : cdeDirIter_) {
54  distributions_[d] = SIGNAL_initalcondition/5.0;
55  }
56  }
57 }
58 
59 void tutorial_01_CDESolverD2Q5_SIGNAL::writeSolver(std::ostream* const stream) {
60  (*stream) << physicalNode_->getXPos() << '\t' << physicalNode_->getYPos();
61  for (auto d : distributions_) {
62  (*stream) << '\t' << d;
63  }
64  (*stream) << '\n';
65 }
66 
67 void tutorial_01_CDESolverD2Q5_SIGNAL::loadSolver(std::stringstream* const stream) {
68  int x, y;
69  (*stream) >> x >> y;
70  assert(physicalNode_->getXPos() == x && "The position does not match");
71  assert(physicalNode_->getYPos() == y && "The position does not match");
72  for (auto d : cdeDirIter_) {
73  (*stream) >> distributions_[d];
74  }
75 }
76 
78  assert(dir > T && dir < NE);
79  return distributions_[dir];
80 }
81 
83  for (auto &it: this->distributions_) {
84  it *= factor;
85  }
86 }
87 
89  return std::accumulate(distributions_.begin(), distributions_.end(), 0.0);
90 }
91 
93 {
94  if ((this->physicalNode_->getDomainIdentifier() == 1) && (Parameters.getCurrentIteration()<SWITCHOFF_TIME)) {
95  return SIGNAL_production;
96  }
97  else {
98  return -SIGNAL_decay*this->getC();
99  }
100 }
101 
103  assert(physicalNode_ != nullptr);
104  // get the local concentration:
105  const double C = getC();
106 
107  // get the local velocity:
108  const double ux = physicalNode_->getFluidSolver().getVelocity().x;
109  const double uy = physicalNode_->getFluidSolver().getVelocity().y;
110 
111  const double w0 = C / 3.0;
112  const double w1 = C / 6.0;
113  const double tauI = 1.0 / getTau();
114 
115  // temporary computations
116  double temp[5];
117  temp[T] = w0;
118  temp[E] = w1 * (1.0 + ux * 3.0);
119  temp[N] = w1 * (1.0 + uy * 3.0);
120  temp[W] = w1 * (1.0 + (-ux) * 3.0);
121  temp[S] = w1 * (1.0 + (-uy) * 3.0);
122 
123  const double reactionTerm = this->reaction();
124  const double reactionTerm_ZERO = deltaT*reactionTerm / 3.0; // for the ZERO distribution
125  const double reactionTerm_OTHERS = deltaT*reactionTerm / 6.0; // for all the other distributions
126 
127  distributions_[T] = distributions_[T] - distributions_[T] * tauI + temp[T] * tauI + reactionTerm_ZERO;
128  distributions_[E] = distributions_[E] - distributions_[E] * tauI + temp[E] * tauI + reactionTerm_OTHERS;
129  distributions_[N] = distributions_[N] - distributions_[N] * tauI + temp[N] * tauI + reactionTerm_OTHERS;
130  distributions_[W] = distributions_[W] - distributions_[W] * tauI + temp[W] * tauI + reactionTerm_OTHERS;
131  distributions_[S] = distributions_[S] - distributions_[S] * tauI + temp[S] * tauI + reactionTerm_OTHERS;
132 
133  // preparation for advect step: this is necessary
134  localSwap();
135 }
136 
138  const double C = getC();
139  // calculate the speeds
140  const double u = physicalNode_->getFluidSolver().getVelocity().x;
141  const double v = physicalNode_->getFluidSolver().getVelocity().y;
142 
143  const double w1 = C / 6.0;
144 
145  switch (dir) {
146  case T:
147  return C / 3.0;
148  break;
149  case E:
150  return w1 * (1.0 + u * 3.0);
151  break;
152  case N:
153  return w1 * (1.0 + v * 3.0);
154  break;
155  case W:
156  return w1 * (1.0 + (-u) * 3.0);
157  break;
158  case S:
159  return w1 * (1.0 + (-v) * 3.0);
160  break;
161  default:
162  assert(
163  false &&
164  "you want to get a inverse direction of a Direction that does not exist");
165  }
166  return 0;
167 }
168 
170  assert(physicalNode_ != nullptr);
171  std::swap(distributions_[getInverseDirection(W)],
174  std::swap(distributions_[getInverseDirection(S)],
177 }
178 
179 void tutorial_01_CDESolverD2Q5_SIGNAL::localSwap() {
180  std::swap(distributions_[E], distributions_[W]);
181  std::swap(distributions_[N], distributions_[S]);
182 }
183 
185  double sumC = 0.0;
186  int counter = 0;
187  const unsigned int myNodeID = this->physicalNode_->getDomainIdentifier();
188 
189  // accumulate the concenctration of appropriate (tbd) neighbors.
190  // first, try {E, N, W, S}:
191  std::array<Direction, 4> dir1 {{E, N, W, S}};
192  for (auto d : dir1) {
193  // if it has no boundary neighbour and the neighbour is in the same domain then get the concentration
194  if ((this->physicalNode_->getBoundaryNeighbour(d) == nullptr) &&
195  (myNodeID ==
198  solverID_).getC();
199  counter++;
200  }
201  }
202  // if not successful, try {NE,NW,SW,SE}:
203  if (counter == 0) {
204  std::array<Direction, 4> dir2 {{NE, NW, SW, SE}};
205  for (auto d : dir2) {
206  // we need to check the diagonals as it does not work in the other directions
207  if ( (this->physicalNode_->getBoundaryNeighbour(d) == nullptr) &&
208  (myNodeID == this->physicalNode_->getPhysicalNeighbour(d)->getDomainIdentifier())) {
209  sumC +=
211  solverID_).getC();
212  counter++;
213  }
214  }
215  if (counter != 0) {
216  std::stringstream message;
217  message << std::setprecision(12);
218  message << "Default initialisation on PhysicalNode ";
219  message << "("<< this->physicalNode_->getXPos()<<","<<this->physicalNode_->getYPos() <<")";
220  message << " failed. Therefore the node was reinitialised from the diagonal directions";
221  LOG(UtilLib::logINFO) << message.str().c_str();
222  }
223 
224  }
225 
226  // if still fails: initialize with neighbors, even if they are in an other domain:
227  if (counter == 0) {
228  std::stringstream message;
229  message << std::setprecision(12)
230  << "Initialization on PhysicalNode "
231  << "("<< this->physicalNode_->getXPos()<<","<<this->physicalNode_->getYPos() <<")"
232  << " failed at time "
233  << Parameters.getCurrentIteration()
234  << ". Therefore the node was initialized with all neighbors {E,N,W,S}, ignoring their domainID."
235  << " CurrentNodeID="<<this->physicalNode_->getDomainIdentifier();
236  for (auto d : dir1) {
237  // if it has no boundary neighbour and the neighbour is in the same domain then get the concentration
239  solverID_).getC();
240  counter++;
241  }
242  LOG(UtilLib::logINFO) << message.str().c_str();
243  }
244 
245  sumC /= static_cast<double>(counter);
246  for (auto d : cdeDirIter_) {
247  this->distributions_[d] = sumC / 5.0;
248  }
249  this->collide();
250 }
251 
252 const std::string tutorial_01_CDESolverD2Q5_SIGNAL::name = "tutorial_01_CDESolverD2Q5_SIGNAL";
253 
254 
255 CDEDirectionsIteratorD2Q5 const tutorial_01_CDESolverD2Q5_SIGNAL::cdeDirIter_ =
257 
258 tutorial_01_CDESolverD2Q5_SIGNAL::tutorial_01_CDESolverD2Q5_SIGNAL() : BaseCDESolver(),
259  distributions_(std::array<double,
260  5> {
261  {0.0, 0.0, 0.0,
262  0.0, 0.0}
263  }
264 
265  )
266 {}
267 }
268 } // end namespace
269 
virtual void loadSolver(std::stringstream *const stream)
loads the solver from the stream
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
virtual double & accessDistribution(const Direction &dir)
accessDistribution Access to the distribution
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
The CDEDirectionsIteratorD2Q5 class Provides methods to handle the Directions. Use the Function Direc...
Definition: Direction.hpp:126
virtual void rescaleDistributions(const double factor)
Rescales all distributions by a factor.
T x
x the value in x direction
Definition: Field.hpp:50
const double reaction(void) const
reaction The reaction term of the tutorial_01_CDESolverD2Q5_SIGNAL solver is implemented here...
virtual void collide()
collide The collision step of the LBM
virtual double & accessDistribution(const Direction &dir)=0
accessDistribution Access to the distribution
virtual void reinitialise()
reinitialise this solver as the corresponding physical node has switched domain
unsigned int getDomainIdentifier() const
getter for the Domain Identifier of this node
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 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
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