LBIBCell
 All Classes Functions Variables Friends Pages
CDESolverD2Q5.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/GlobalSimulationParameters.hpp>
23 #include <LbmLib/include/nodes/BoundaryNode.hpp>
24 #include <LbmLib/include/nodes/PhysicalNode.hpp>
25 #include <LbmLib/include/solver/BoundaryAbstractSolver.hpp>
26 #include <LbmLib/include/solver/CDESolver/CDESolverD2Q5.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 namespace LbmLib {
34 namespace solver {
35 
36 
38  distributions_[T] = 1.0;
39  collide();
40 }
41 
42 void CDESolverD2Q5::loadSolver(std::stringstream* const stream) {
43  int x, y;
44  (*stream) >> x >> y;
45  assert(physicalNode_->getXPos() == x && "The position does not match");
46  assert(physicalNode_->getYPos() == y && "The position does not match");
47  for (auto d : cdeDirIter_) {
48  (*stream) >> distributions_[d];
49  }
50 }
51 
52 void CDESolverD2Q5::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 double& CDESolverD2Q5::accessDistribution(const Direction& dir) {
61  assert(dir > T && dir < NE);
62  return distributions_[dir];
63 }
64 
65 void CDESolverD2Q5::rescaleDistributions(const double factor) {
66  for (auto &it: this->distributions_) {
67  it *= factor;
68  }
69 }
70 
71 double CDESolverD2Q5::getC() const {
72  return std::accumulate(distributions_.begin(), distributions_.end(), 0.0);
73 }
74 
76  assert(physicalNode_ != nullptr);
77  // Calculate the rho
78  const double C = getC();
79 
80  // calculate the speeds
81  const double ux = physicalNode_->getFluidSolver().getVelocity().x;
82  const double uy = physicalNode_->getFluidSolver().getVelocity().y;
83  // std::cout<<std::sqrt(ux*ux+uy*uy)<<std::endl;
84  const double w0 = C / 3.0;
85  const double w1 = C / 6.0;
86  const double tauI = 1.0 / getTau();
87 
88 
89  double temp[5];
90  temp[T] = w0;
91  temp[E] = w1 * (1.0 + ux * 3.0);
92  temp[N] = w1 * (1.0 + uy * 3.0);
93  temp[W] = w1 * (1.0 + (-ux) * 3.0);
94  temp[S] = w1 * (1.0 + (-uy) * 3.0);
95 
96 
97  for (auto d : cdeDirIter_) {
98  double tempD = distributions_[d];
99  // compute non equilibirum
100  // make relaxation
101  distributions_[d] = tempD - tempD * tauI + temp[d] * tauI;
102  }
103 
104 
105  // preparation for advect step
106  localSwap();
107 }
108 
109 double CDESolverD2Q5::calculateEquilibrium(const Direction& dir) {
110  const double C = getC();
111  // calculate the speeds
112  const double u = physicalNode_->getFluidSolver().getVelocity().x;
113  const double v = physicalNode_->getFluidSolver().getVelocity().y;
114 
115  const double w1 = C / 6.0;
116 
117  switch (dir) {
118  case T:
119  return C / 3.0;
120  break;
121  case E:
122  return w1 * (1.0 + u * 3.0);
123  break;
124  case N:
125  return w1 * (1.0 + v * 3.0);
126  break;
127  case W:
128  return w1 * (1.0 + (-u) * 3.0);
129  break;
130  case S:
131  return w1 * (1.0 + (-v) * 3.0);
132  break;
133  default:
134  assert(
135  false &&
136  "you want to get a inverse direction of a Direction that does not exist");
137  }
138  return 0;
139 }
140 
142  assert(physicalNode_ != nullptr);
143  std::swap(distributions_[getInverseDirection(W)],
146  std::swap(distributions_[getInverseDirection(S)],
149 }
150 
151 void CDESolverD2Q5::localSwap() {
152  std::swap(distributions_[E], distributions_[W]);
153  std::swap(distributions_[N], distributions_[S]);
154 }
155 
157  double sumC = 0.0;
158  int counter = 0;
159  const unsigned int myNodeID = this->physicalNode_->getDomainIdentifier();
160 
161  // accumulate the concenctration of appropriate (tbd) neighbors.
162  // first, try {E, N, W, S}:
163  std::array<Direction, 4> dir1 {{E, N, W, S}};
164  for (auto d : dir1) {
165  // if it has no boundary neighbour and the neighbour is in the same domain then get the concentration
166  if ((this->physicalNode_->getBoundaryNeighbour(d) == nullptr) &&
167  (myNodeID ==
170  solverID_).getC();
171  counter++;
172  }
173  }
174  // fi not successful, try {NE,NW,SW,SE}:
175  if (counter == 0) {
176  std::array<Direction, 4> dir2 {{NE, NW, SW, SE}};
177  for (auto d : dir2) {
178  // we need to check the diagonals as it does not work in the other directions
179  if ( (this->physicalNode_->getBoundaryNeighbour(d) == nullptr) &&
180  (myNodeID == this->physicalNode_->getPhysicalNeighbour(d)->getDomainIdentifier())) {
181  sumC +=
183  solverID_).getC();
184  counter++;
185  }
186  }
187  if (counter != 0) {
188  std::stringstream message;
189  message << std::setprecision(12);
190  message << "Default initialisation on PhysicalNode ";
191  message << "("<< this->physicalNode_->getXPos()<<","<<this->physicalNode_->getYPos() <<")";
192  message << " failed. Therefore the node was reinitialised from the diagonal directions";
193  LOG(UtilLib::logINFO) << message.str().c_str();
194  }
195 
196  }
197  if (counter == 0) {
198  std::stringstream message;
199  message << std::setprecision(12);
200  message << "Initialization on PhysicalNode ";
201  message << "("<< this->physicalNode_->getXPos()<<","<<this->physicalNode_->getYPos() <<")";
202  message << " failed at time ";
203  message << Parameters.getCurrentIteration();
204  message << ". Therefore the node was NOT INITIALIZED.";
205  message << " CurrentNodeID="<<this->physicalNode_->getDomainIdentifier();
206  message << ". DirectNeighbor(domainID): {";
207 
208  std::array<Direction, 8> dir3 {{E, N, W, S, NE, NW, SW, SE}};
209  for (auto d : dir3) {
210  // if it has no boundary neighbour and the neighbour is in the same domain then get the concentration
211  message << d << "(" << this->physicalNode_->getPhysicalNeighbour(d)->getDomainIdentifier() << ")";
212  if ( (this->physicalNode_->getBoundaryNeighbour(d) != nullptr)) {
213  message <<"<-hasBN";
214  }
215  message << " ";
216  }
217  message << "}";
218 
219 
220  LOG(UtilLib::logINFO) << message.str().c_str();
221  //lbm_fail(message.str().c_str());
222  }
223  sumC /= static_cast<double>(counter);
224  for (auto d : cdeDirIter_) {
225  this->distributions_[d] = sumC / 5.0;
226  }
227  this->collide();
228 }
229 
230 CDESolverD2Q5::CDESolverD2Q5() : BaseCDESolver(),
231  distributions_(std::array<double,
232  5> {{0.0, 0.0, 0.0, 0.0, 0.0}
233  }
234  )
235 {}
236 
237 
238 const std::string CDESolverD2Q5::name = "CDESolverD2Q5";
239 
240 CDEDirectionsIteratorD2Q5 const CDESolverD2Q5::cdeDirIter_ =
241  CDEDirectionsIteratorD2Q5();
242 }
243 } // end namespace
virtual void loadSolver(std::stringstream *const stream)
loads the solver from the file
The Base class for all CDESolver implementations This classes uses the recursive template idiom to au...
virtual double getC() const
getC Calculates the concentration on this node
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
T x
x the value in x direction
Definition: Field.hpp:50
virtual void initSolver()
initSolver Use this to initalise the solver
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 void collide()
collide The collision step of the LBM
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
int getYPos() const
getYPos Getter for the Y position
virtual void reinitialise()
reinitialise this solver iff the corresponding physical node has switched domain
virtual void writeSolver(std::ostream *const stream)
writes the solver to the file
virtual double calculateEquilibrium(const Direction &dir)
calculateEquilibrium calculates the equilibirum for direction dir
solver::CDEAbstractSolver & getCDESolver(size_t id) const
getCDESolver Getter method for the cde Solver
virtual void rescaleDistributions(const double factor)
Rescales all distributions by a factor.
virtual void advect()
advect The advect step of the LBM
virtual double & accessDistribution(const Direction &dir)
accessDistribution Access to the distribution
T y
y the value in y direction
Definition: Field.hpp:54