LBIBCell
 All Classes Functions Variables Friends Pages
CDESolverD2Q5BMP.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/CDESolverD2Q5BMP.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  for (auto d : cdeDirIter_) {
47  distributions_[d] = 0.0;
48  }
49  deltaT = 1.0;
50 }
51 
52 void CDESolverD2Q5BMP::writeSolver(std::ostream* const stream) {
53  (*stream) << physicalNode_->getXPos() << '\t' << physicalNode_->getYPos();
54  for (auto d : distributions_) {
55  (*stream) << '\t' << d;
56  }
57  (*stream) << '\n';
58 }
59 
60 void CDESolverD2Q5BMP::loadSolver(std::stringstream* const stream) {
61  int x, y;
62  (*stream) >> x >> y;
63  assert(physicalNode_->getXPos() == x && "The position does not match");
64  assert(physicalNode_->getYPos() == y && "The position does not match");
65  for (auto d : cdeDirIter_) {
66  (*stream) >> distributions_[d];
67  }
68 }
69 
70 double& CDESolverD2Q5BMP::accessDistribution(const Direction& dir) {
71  assert(dir > T && dir < NE);
72  return distributions_[dir];
73 }
74 
75 void CDESolverD2Q5BMP::rescaleDistributions(const double factor) {
76  for (auto &it: this->distributions_) {
77  it *= factor;
78  }
79 }
80 
81 double CDESolverD2Q5BMP::getC() const {
82  return std::accumulate(distributions_.begin(), distributions_.end(), 0.0);
83 }
84 
86  assert(physicalNode_ != nullptr);
87  // Calculate the rho
88  const double C = getC();
89 
90 
91  // calculate the speeds
92  const double ux = physicalNode_->getFluidSolver().getVelocity().x;
93  const double uy = physicalNode_->getFluidSolver().getVelocity().y;
94 
95  const double w0 = C / 3.0;
96  const double w1 = C / 6.0;
97  const double tauI = 1.0 / getTau();
98 
99 
100  double temp[5];
101  temp[T] = w0;
102  temp[E] = w1 * (1.0 + ux * 3.0);
103  temp[N] = w1 * (1.0 + uy * 3.0);
104  temp[W] = w1 * (1.0 + (-ux) * 3.0);
105  temp[S] = w1 * (1.0 + (-uy) * 3.0);
106 
107  const double HH =
108  physicalNode_->getCDESolverSlow("CDESolverD2Q5HH").getC();
109  const double BMPdecay = 0.001;
110  const double BMPproduction = 0.00001;
111  const double k_hill = 0.01;
112  const double n_hill = 4;
113 
114  double reaktionTerm = deltaT*(-BMPdecay*C) / 3.0;
115  double reaktionTermR = deltaT*(-BMPdecay*C) / 6.0;
116 
117  if (this->physicalNode_->getCellType() == 2) { // if it is and epithelial cell
118  reaktionTerm = deltaT * BMPproduction / 3.0 * std::pow(HH,n_hill)/(std::pow(k_hill,n_hill)+std::pow(HH,n_hill));
119  reaktionTermR = deltaT * BMPproduction / 6.0 * std::pow(HH,n_hill)/(std::pow(k_hill,n_hill)+std::pow(HH,n_hill));
120  }
121  distributions_[T] = distributions_[T] - distributions_[T] * tauI + temp[T] *
122  tauI + reaktionTerm;
123  distributions_[E] = distributions_[E] - distributions_[E] * tauI + temp[E] *
124  tauI + reaktionTermR;
125  distributions_[N] = distributions_[N] - distributions_[N] * tauI + temp[N] *
126  tauI + reaktionTermR;
127  distributions_[W] = distributions_[W] - distributions_[W] * tauI + temp[W] *
128  tauI + reaktionTermR;
129  distributions_[S] = distributions_[S] - distributions_[S] * tauI + temp[S] *
130  tauI + reaktionTermR;
131  // for (auto d : cdeDirIter_) {
132  // double tempD = distributions_[d];
133  // // compute non equilibirum
134  // // make relaxation
135  // distributions_[d] = tempD - tempD * tauI + temp[d] * tauI +
136  // reaktionTerm;
137  // }
138 
139 
140  // preparation for advect step
141  localSwap();
142 }
143 
144 double CDESolverD2Q5BMP::calculateEquilibrium(const Direction& dir) {
145  const double C = getC();
146  // calculate the speeds
147  const double u = physicalNode_->getFluidSolver().getVelocity().x;
148  const double v = physicalNode_->getFluidSolver().getVelocity().y;
149 
150  const double w1 = C / 6.0;
151 
152  switch (dir) {
153  case T:
154  return C / 3.0;
155  break;
156  case E:
157  return w1 * (1.0 + u * 3.0);
158  break;
159  case N:
160  return w1 * (1.0 + v * 3.0);
161  break;
162  case W:
163  return w1 * (1.0 + (-u) * 3.0);
164  break;
165  case S:
166  return w1 * (1.0 + (-v) * 3.0);
167  break;
168  default:
169  assert(
170  false &&
171  "you want to get a inverse direction of a Direction that does not exist");
172  }
173  return 0;
174 }
175 
177  assert(physicalNode_ != nullptr);
178  std::swap(distributions_[getInverseDirection(W)],
181  std::swap(distributions_[getInverseDirection(S)],
184 }
185 
186 void CDESolverD2Q5BMP::localSwap() {
187  std::swap(distributions_[E], distributions_[W]);
188  std::swap(distributions_[N], distributions_[S]);
189 }
190 
192  double sumC = 0.0;
193  int counter = 0;
194  const unsigned int myNodeID = this->physicalNode_->getDomainIdentifier();
195 
196  // accumulate the concenctration of appropriate (tbd) neighbors.
197  // first, try {E, N, W, S}:
198  std::array<Direction, 4> dir1 {{E, N, W, S}};
199  for (auto d : dir1) {
200  // if it has no boundary neighbour and the neighbour is in the same domain then get the concentration
201  if ((this->physicalNode_->getBoundaryNeighbour(d) == nullptr) &&
202  (myNodeID ==
205  solverID_).getC();
206  counter++;
207  }
208  }
209  // if not successful, try {NE,NW,SW,SE}:
210  if (counter == 0) {
211  std::array<Direction, 4> dir2 {{NE, NW, SW, SE}};
212  for (auto d : dir2) {
213  // we need to check the diagonals as it does not work in the other directions
214  if ( (this->physicalNode_->getBoundaryNeighbour(d) == nullptr) &&
215  (myNodeID == this->physicalNode_->getPhysicalNeighbour(d)->getDomainIdentifier())) {
216  sumC +=
218  solverID_).getC();
219  counter++;
220  }
221  }
222  if (counter != 0) {
223  std::stringstream message;
224  message << std::setprecision(12);
225  message << "Default initialisation on PhysicalNode ";
226  message << "("<< this->physicalNode_->getXPos()<<","<<this->physicalNode_->getYPos() <<")";
227  message << " failed. Therefore the node was reinitialised from the diagonal directions";
228  LOG(UtilLib::logINFO) << message.str().c_str();
229  }
230 
231  }
232 
233  // if still fails: initialize with neighbors, even if they are in an other domain:
234  if (counter == 0) {
235  std::stringstream message;
236  message << std::setprecision(12)
237  << "Initialization on PhysicalNode "
238  << "("<< this->physicalNode_->getXPos()<<","<<this->physicalNode_->getYPos() <<")"
239  << " failed at time "
240  << Parameters.getCurrentIteration()
241  << ". Therefore the node was initialized with all neighbors {E,N,W,S}, ignoring their domainID."
242  << " CurrentNodeID="<<this->physicalNode_->getDomainIdentifier();
243  for (auto d : dir1) {
244  // if it has no boundary neighbour and the neighbour is in the same domain then get the concentration
246  solverID_).getC();
247  counter++;
248  }
249  LOG(UtilLib::logINFO) << message.str().c_str();
250  }
251 
252  sumC /= static_cast<double>(counter);
253  for (auto d : cdeDirIter_) {
254  this->distributions_[d] = sumC / 5.0;
255  }
256  this->collide();
257 }
258 
259 const std::string CDESolverD2Q5BMP::name = "CDESolverD2Q5BMP";
260 
261 
262 CDEDirectionsIteratorD2Q5 const CDESolverD2Q5BMP::cdeDirIter_ =
264 
265 CDESolverD2Q5BMP::CDESolverD2Q5BMP() : BaseCDESolver(),
266  distributions_(std::array<double,
267  5> {
268  {0.0, 0.0, 0.0,
269  0.0, 0.0}
270  }
271 
272  )
273 {}
274 }
275 } // end namespace
276 
solver::CDEAbstractSolver & getCDESolverSlow(const std::string &name) const
getCDESolverSlow Getter method for the cde Solver
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 advect()
advect The advect 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
unsigned int getDomainIdentifier() const
getter for the Domain Identifier of this node
virtual double getC() const
getC Calculates the concentration on this node
unsigned int getCellType() const
getter for the cell type of this node
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 void reinitialise()
reinitialise this solver as the corresponding physical node has switched domain
virtual void writeSolver(std::ostream *const stream)
writes the solver to the stream
virtual double & accessDistribution(const Direction &dir)
accessDistribution Access to the distribution
BoundaryNode * getBoundaryNeighbour(const Direction &d) const
getBoundaryNeighbour Getter method to access the Boundary Neighbour
int getYPos() const
getYPos Getter for the Y position
virtual void loadSolver(std::stringstream *const stream)
loads the solver from the stream
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 initSolver()
initSolver Use this to initalise the solver
virtual void rescaleDistributions(const double factor)
Rescales all distributions by a factor.
T y
y the value in y direction
Definition: Field.hpp:54