LBIBCell
 All Classes Functions Variables Friends Pages
CDESolverD2Q5HH.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/CDESolverD2Q5HH.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 = 800.0;
40 const double b = 0.9;
41 double deltaT;
42 }
43 
44 
46  if (this->physicalNode_->getDomainIdentifier() == 0) {
47  for (auto d : cdeDirIter_) {
48  distributions_[d] = 0.0;
49  }
50  }
51  else {
52  for (auto d : cdeDirIter_) {
53  distributions_[d] = 0.2;
54  }
55  }
56  deltaT = 1.0;
57 }
58 
59 void CDESolverD2Q5HH::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 CDESolverD2Q5HH::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 
77 double& CDESolverD2Q5HH::accessDistribution(const Direction& dir) {
78  assert(dir > T && dir < NE);
79  return distributions_[dir];
80 }
81 
82 void CDESolverD2Q5HH::rescaleDistributions(const double factor) {
83  for (auto &it: this->distributions_) {
84  it *= factor;
85  }
86 }
87 
88 double CDESolverD2Q5HH::getC() const {
89  return std::accumulate(distributions_.begin(), distributions_.end(), 0.0);
90 }
91 
93  assert(physicalNode_ != nullptr);
94  // Calculate the rho
95  const double C = getC();
96 
97  // calculate the speeds
98  const double ux = physicalNode_->getFluidSolver().getVelocity().x;
99  const double uy = physicalNode_->getFluidSolver().getVelocity().y;
100 
101  const double w0 = C / 3.0;
102  const double w1 = C / 6.0;
103  const double tauI = 1.0 / getTau();
104 
105 
106  double temp[5];
107  temp[T] = w0;
108  temp[E] = w1 * (1.0 + ux * 3.0);
109  temp[N] = w1 * (1.0 + uy * 3.0);
110  temp[W] = w1 * (1.0 + (-ux) * 3.0);
111  temp[S] = w1 * (1.0 + (-uy) * 3.0);
112 
113  const double Cu = 0.0;
114  //physicalNode_->getCDESolverSlow("SchnakenbergD2Q5u").getC();
115 
116  const double HHdecay = 0.00001;
117  const double HHproduction = 0.0001;
118  double reaktionTerm;
119  double reaktionTermR;
120 
121  //if (this->physicalNode_->getCellType() == 1) { // if it is a stem cell
122  if ((this->physicalNode_->getDomainIdentifier()) == 1 && (Parameters.getCurrentIteration()<5000)) {
123  reaktionTerm = deltaT * HHproduction / 3.0;
124  reaktionTermR = deltaT * HHproduction / 6.0;
125  }
126  else {
127  reaktionTerm = deltaT*(-HHdecay*C) / 3.0;
128  reaktionTermR = deltaT*(-HHdecay*C) / 6.0;
129  }
130 
131  distributions_[T] = distributions_[T] - distributions_[T] * tauI + temp[T] *
132  tauI + reaktionTerm;
133  distributions_[E] = distributions_[E] - distributions_[E] * tauI + temp[E] *
134  tauI + reaktionTermR;
135  distributions_[N] = distributions_[N] - distributions_[N] * tauI + temp[N] *
136  tauI + reaktionTermR;
137  distributions_[W] = distributions_[W] - distributions_[W] * tauI + temp[W] *
138  tauI + reaktionTermR;
139  distributions_[S] = distributions_[S] - distributions_[S] * tauI + temp[S] *
140  tauI + reaktionTermR;
141  // for (auto d : cdeDirIter_) {
142  // double tempD = distributions_[d];
143  // // compute non equilibirum
144  // // make relaxation
145  // distributions_[d] = tempD - tempD * tauI + temp[d] * tauI +
146  // reaktionTerm;
147  // }
148 
149 
150  // preparation for advect step
151  localSwap();
152 }
153 
154 double CDESolverD2Q5HH::calculateEquilibrium(const Direction& dir) {
155  const double C = getC();
156  // calculate the speeds
157  const double u = physicalNode_->getFluidSolver().getVelocity().x;
158  const double v = physicalNode_->getFluidSolver().getVelocity().y;
159 
160  const double w1 = C / 6.0;
161 
162  switch (dir) {
163  case T:
164  return C / 3.0;
165  break;
166  case E:
167  return w1 * (1.0 + u * 3.0);
168  break;
169  case N:
170  return w1 * (1.0 + v * 3.0);
171  break;
172  case W:
173  return w1 * (1.0 + (-u) * 3.0);
174  break;
175  case S:
176  return w1 * (1.0 + (-v) * 3.0);
177  break;
178  default:
179  assert(
180  false &&
181  "you want to get a inverse direction of a Direction that does not exist");
182  }
183  return 0;
184 }
185 
187  assert(physicalNode_ != nullptr);
188  std::swap(distributions_[getInverseDirection(W)],
191  std::swap(distributions_[getInverseDirection(S)],
194 }
195 
196 void CDESolverD2Q5HH::localSwap() {
197  std::swap(distributions_[E], distributions_[W]);
198  std::swap(distributions_[N], distributions_[S]);
199 }
200 
202  double sumC = 0.0;
203  int counter = 0;
204  const unsigned int myNodeID = this->physicalNode_->getDomainIdentifier();
205 
206  // accumulate the concenctration of appropriate (tbd) neighbors.
207  // first, try {E, N, W, S}:
208  std::array<Direction, 4> dir1 {{E, N, W, S}};
209  for (auto d : dir1) {
210  // if it has no boundary neighbour and the neighbour is in the same domain then get the concentration
211  if ((this->physicalNode_->getBoundaryNeighbour(d) == nullptr) &&
212  (myNodeID ==
215  solverID_).getC();
216  counter++;
217  }
218  }
219  // if not successful, try {NE,NW,SW,SE}:
220  if (counter == 0) {
221  std::array<Direction, 4> dir2 {{NE, NW, SW, SE}};
222  for (auto d : dir2) {
223  // we need to check the diagonals as it does not work in the other directions
224  if ( (this->physicalNode_->getBoundaryNeighbour(d) == nullptr) &&
225  (myNodeID == this->physicalNode_->getPhysicalNeighbour(d)->getDomainIdentifier())) {
226  sumC +=
228  solverID_).getC();
229  counter++;
230  }
231  }
232  if (counter != 0) {
233  std::stringstream message;
234  message << std::setprecision(12);
235  message << "Default initialisation on PhysicalNode ";
236  message << "("<< this->physicalNode_->getXPos()<<","<<this->physicalNode_->getYPos() <<")";
237  message << " failed. Therefore the node was reinitialised from the diagonal directions";
238  LOG(UtilLib::logINFO) << message.str().c_str();
239  }
240 
241  }
242 
243  // if still fails: initialize with neighbors, even if they are in an other domain:
244  if (counter == 0) {
245  std::stringstream message;
246  message << std::setprecision(12)
247  << "Initialization on PhysicalNode "
248  << "("<< this->physicalNode_->getXPos()<<","<<this->physicalNode_->getYPos() <<")"
249  << " failed at time "
250  << Parameters.getCurrentIteration()
251  << ". Therefore the node was initialized with all neighbors {E,N,W,S}, ignoring their domainID."
252  << " CurrentNodeID="<<this->physicalNode_->getDomainIdentifier();
253  for (auto d : dir1) {
254  // if it has no boundary neighbour and the neighbour is in the same domain then get the concentration
256  solverID_).getC();
257  counter++;
258  }
259  LOG(UtilLib::logINFO) << message.str().c_str();
260  }
261 
262  sumC /= static_cast<double>(counter);
263  for (auto d : cdeDirIter_) {
264  this->distributions_[d] = sumC / 5.0;
265  }
266  this->collide();
267 }
268 
269 const std::string CDESolverD2Q5HH::name = "CDESolverD2Q5HH";
270 
271 
272 CDEDirectionsIteratorD2Q5 const CDESolverD2Q5HH::cdeDirIter_ =
274 
275 CDESolverD2Q5HH::CDESolverD2Q5HH() : BaseCDESolver(),
276  distributions_(std::array<double,
277  5> {
278  {0.0, 0.0, 0.0,
279  0.0, 0.0}
280  }
281 
282  )
283 {}
284 }
285 } // end namespace
286 
virtual void writeSolver(std::ostream *const stream)
writes the solver to the stream
The Base class for all CDESolver implementations This classes uses the recursive template idiom to au...
virtual double & accessDistribution(const Direction &dir)
accessDistribution Access to the distribution
virtual void loadSolver(std::stringstream *const stream)
loads the solver from the stream
size_t solverID_
solverID_ The ID of the solver instance. Coincides with the index in the vector PhysicalNode::cdeSolv...
virtual void collide()
collide The collision step of the LBM
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
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 void advect()
advect The advect step of the LBM
virtual void rescaleDistributions(const double factor)
Rescales all distributions by a factor.
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 calculateEquilibrium(const Direction &dir)
calculateEquilibrium calculates the equilibirum for direction dir
int getYPos() const
getYPos Getter for the Y position
virtual double getC() const
getC Calculates the concentration on this node
solver::CDEAbstractSolver & getCDESolver(size_t id) const
getCDESolver Getter method for the cde Solver
virtual void initSolver()
initSolver Use this to initalise the solver
T y
y the value in y direction
Definition: Field.hpp:54