MaMiCo 1.2
Loading...
Searching...
No Matches
LBCouetteSolver.h
1// Copyright (C) 2015 Technische Universitaet Muenchen
2// This file is part of the Mamico project. For conditions of distribution
3// and use, please see the copyright notice in Mamico's main folder
4#ifndef _MOLECULARDYNAMICS_COUPLING_SOLVERS_LBCOUETTESOLVER_H_
5#define _MOLECULARDYNAMICS_COUPLING_SOLVERS_LBCOUETTESOLVER_H_
6
7namespace coupling {
8namespace solvers {
11} // namespace solvers
12} // namespace coupling
13
14#if defined(_OPENMP)
15#include <omp.h>
16#endif
17#include "coupling/interface/PintableMacroSolver.h"
18#include "coupling/solvers/NumericalSolver.h"
19#include <cmath>
20
22public:
23 LBCouetteSolverState(int size) : _pdf(size, 0) {}
24
25 LBCouetteSolverState(int size, double* pdf) : LBCouetteSolverState(size) { std::copy(pdf, pdf + size, _pdf.data()); }
26
27 std::unique_ptr<State> clone() const override { return std::make_unique<LBCouetteSolverState>(*this); }
28
29 ~LBCouetteSolverState() {}
30
31 int getSizeBytes() const override { return sizeof(double) * _pdf.size(); }
32
33 std::unique_ptr<State> operator+(const State& rhs) override;
34 std::unique_ptr<State> operator-(const State& rhs) override;
35
36 bool operator==(const State& rhs) const override {
37 const LBCouetteSolverState* other = dynamic_cast<const LBCouetteSolverState*>(&rhs);
38 if (other == nullptr)
39 return false;
40 return _pdf == other->_pdf;
41 }
42
43 double* getData() override { return _pdf.data(); }
44 const double* getData() const override { return _pdf.data(); }
45
46 void print(std::ostream& os) const override { os << "<LBCouetteSolverState instance with size " << getSizeBytes() << ">"; }
47
48private:
49 std::vector<double> _pdf;
50};
51
57public:
73 LBCouetteSolver(const double channelheight, tarch::la::Vector<3, double> wallVelocity, const double kinVisc, const double dx, const double dt,
74 const int plotEveryTimestep, const bool plotAverageVelocity, const std::string filestem, const tarch::la::Vector<3, unsigned int> processes,
75 const unsigned int numThreads = 1, const Scenario* scen = nullptr)
76 : coupling::solvers::NumericalSolver(channelheight, dx, dt, kinVisc, plotEveryTimestep, filestem, processes, scen), _mode(Mode::coupling), _dt_pint(dt),
77 _omega(1.0 / (3.0 * (kinVisc * dt / (dx * dx)) + 0.5)), _wallVelocity((dt / dx) * wallVelocity), _plotAverageVelocity(plotAverageVelocity) {
78 // return if required
79 if (skipRank()) {
80 return;
81 }
82 _pdfsize = 19 * (_domainSizeX + 2) * (_domainSizeY + 2) * (_domainSizeZ + 2);
83 _pdf1 = new double[_pdfsize];
84 _pdf2 = new double[_pdfsize];
85#if defined(_OPENMP)
86 omp_set_num_threads(numThreads);
87#endif
88#if (COUPLING_MD_DEBUG == COUPLING_MD_YES)
89 std::cout << "Domain size=" << _domainSizeX << "," << _domainSizeY << "," << _domainSizeZ << std::endl;
90 std::cout << "tau=" << 1.0 / _omega << std::endl;
91 std::cout << "wallVelocity=" << _wallVelocity << std::endl;
92 for (int z = 0; z < _domainSizeZ + 2; z++) {
93 for (int y = 0; y < _domainSizeY + 2; y++) {
94 for (int x = 0; x < _domainSizeX + 2; x++) {
95 std::cout << x << "," << y << "," << z << "FLAG=" << _flag[get(x, y, z)] << std::endl;
96 }
97 }
98 }
99#endif
100 // check pointers
101 if ((_pdf1 == NULL) || (_pdf2 == NULL) || (_vel == NULL) || (_density == NULL) || (_flag == NULL)) {
102 std::cout << "ERROR LBCouetteSolver: NULL ptr!" << std::endl;
103 exit(EXIT_FAILURE);
104 }
105#if (COUPLING_MD_PARALLEL == COUPLING_MD_YES)
106 if ((_sendBufferX == NULL) || (_recvBufferX == NULL) || (_sendBufferY == NULL) || (_recvBufferY == NULL) || (_sendBufferZ == NULL) ||
107 (_recvBufferZ == NULL)) {
108 std::cout << "ERROR LBCouetteSolver: NULL ptr in send/recv!" << std::endl;
109 exit(EXIT_FAILURE);
110 }
111#endif
112// init everything with lattice weights
113#pragma omp parallel for
114 for (int i = 0; i < (_domainSizeX + 2) * (_domainSizeY + 2) * (_domainSizeZ + 2); i++) {
115 for (int q = 0; q < 19; q++) {
116 _pdf1[get(i) * 19 + q] = _W[q];
117 _pdf2[get(i) * 19 + q] = _W[q];
118 }
119 }
120 computeDensityAndVelocityEverywhere();
121 }
122
125 if (_pdf1 != NULL) {
126 delete[] _pdf1;
127 _pdf1 = NULL;
128 }
129 if (_pdf2 != NULL) {
130 delete[] _pdf2;
131 _pdf2 = NULL;
132 }
133 if (_vel != NULL) {
134 delete[] _vel;
135 _vel = NULL;
136 }
137 if (_density != NULL) {
138 delete[] _density;
139 _density = NULL;
140 }
141 if (_flag != NULL) {
142 delete[] _flag;
143 _flag = NULL;
144 }
145#if (COUPLING_MD_PARALLEL == COUPLING_MD_YES)
146 if (_sendBufferX != NULL) {
147 delete[] _sendBufferX;
148 _sendBufferX = NULL;
149 }
150 if (_sendBufferY != NULL) {
151 delete[] _sendBufferY;
152 _sendBufferY = NULL;
153 }
154 if (_sendBufferZ != NULL) {
155 delete[] _sendBufferZ;
156 _sendBufferZ = NULL;
157 }
158 if (_recvBufferX != NULL) {
159 delete[] _recvBufferX;
160 _recvBufferX = NULL;
161 }
162 if (_recvBufferY != NULL) {
163 delete[] _recvBufferY;
164 _recvBufferY = NULL;
165 }
166 if (_recvBufferZ != NULL) {
167 delete[] _recvBufferZ;
168 _recvBufferZ = NULL;
169 }
170#endif
171 }
172
175 void advance(double dt) override {
176 if (skipRank()) {
177 return;
178 }
179 const int timesteps = floor(dt / _dt + 0.5);
180 if (fabs(timesteps * _dt - dt) / _dt > 1.0e-8) {
181 std::cout << "ERROR LBCouetteSolver::advance(): time steps and dt do not match!" << std::endl;
182 exit(EXIT_FAILURE);
183 }
184 for (int i = 0; i < timesteps; i++) {
186 computeDensityAndVelocityEverywhere();
187 plot();
188 plot_avg_vel();
190 communicate(); // exchange between neighbouring MPI subdomains
191 _counter++;
192 }
193 }
194
200 if (skipRank()) {
201 return;
202 }
203#if (COUPLING_MD_ERROR == COUPLING_MD_YES)
204 if (_mode == Mode::supervising) {
205 std::cout << "ERROR LBCouetteSolver setMDBoundaryValues() called in supervising mode" << std::endl;
206 exit(EXIT_FAILURE);
207 }
208#endif
209
210 // loop over all received cells
211 for (auto pair : md2macroBuffer) {
212 I01 idx;
214 std::tie(couplingCell, idx) = pair;
215 // determine cell index of this cell in LB domain
216 tarch::la::Vector<3, unsigned int> globalCellCoords{idx.get()};
217 globalCellCoords[0] = (globalCellCoords[0] + _offset[0]) - _coords[0] * _avgDomainSizeX;
218 globalCellCoords[1] = (globalCellCoords[1] + _offset[1]) - _coords[1] * _avgDomainSizeY;
219 globalCellCoords[2] = (globalCellCoords[2] + _offset[2]) - _coords[2] * _avgDomainSizeZ;
220#if (COUPLING_MD_DEBUG == COUPLING_MD_YES)
221 std::cout << "Process coords: " << _coords << ": GlobalCellCoords for index " << idx << ": " << globalCellCoords << std::endl;
222#endif
223 const int index = get(globalCellCoords[0], globalCellCoords[1], globalCellCoords[2]);
224#if (COUPLING_MD_ERROR == COUPLING_MD_YES)
225 if (_flag[index] != MD_BOUNDARY) {
226 std::cout << "ERROR LBCouetteSolver::setMDBoundaryValues(): Cell " << index << " is no MD boundary cell!" << std::endl;
227 exit(EXIT_FAILURE);
228 }
229#endif
230 // set velocity value and pdfs in MD boundary cell (before streaming); the
231 // boundary velocities are interpolated between the neighbouring and this
232 // cell. This interpolation is valid for FLUID-MD_BOUNDARY neighbouring
233 // relations only. determine local velocity received from MaMiCo and
234 // convert it to LB units; store the velocity in _vel
235 tarch::la::Vector<3, double> localVel((1.0 / couplingCell->getMacroscopicMass()) * (_dt / _dx) * couplingCell->getMacroscopicMomentum());
236 for (unsigned int d = 0; d < 3; d++) {
237 _vel[3 * index + d] = localVel[d];
238 }
239 // loop over all pdfs and set them according to interpolated moving-wall
240 // conditions
241 for (unsigned int q = 0; q < 19; q++) {
242 // index of neighbour cell; only if cell is located inside local domain
243 if (((int)globalCellCoords[0] + _C[q][0] > 0) && ((int)globalCellCoords[0] + _C[q][0] < _domainSizeX + 1) &&
244 ((int)globalCellCoords[1] + _C[q][1] > 0) && ((int)globalCellCoords[1] + _C[q][1] < _domainSizeY + 1) &&
245 ((int)globalCellCoords[2] + _C[q][2] > 0) && ((int)globalCellCoords[2] + _C[q][2] < _domainSizeZ + 1)) {
246 const int nbIndex = get((_C[q][0] + globalCellCoords[0]), (_C[q][1] + globalCellCoords[1]), (_C[q][2] + globalCellCoords[2]));
247 const tarch::la::Vector<3, double> interpolVel(0.5 * (_vel[3 * index] + _vel[3 * nbIndex]), 0.5 * (_vel[3 * index + 1] + _vel[3 * nbIndex + 1]),
248 0.5 * (_vel[3 * index + 2] + _vel[3 * nbIndex + 2]));
249 _pdf1[19 * index + q] =
250 _pdf1[19 * nbIndex + 18 - q] -
251 6.0 * _W[q] * _density[nbIndex] * (_C[18 - q][0] * interpolVel[0] + _C[18 - q][1] * interpolVel[1] + _C[18 - q][2] * interpolVel[2]);
252 }
253 }
254 }
255 }
256
263 // check pos-data for process locality (todo: put this in debug mode in
264 // future releases)
265 if ((pos[0] < domainOffset[0]) || (pos[0] > domainOffset[0] + _domainSizeX * _dx) || (pos[1] < domainOffset[1]) ||
266 (pos[1] > domainOffset[1] + _domainSizeY * _dx) || (pos[2] < domainOffset[2]) || (pos[2] > domainOffset[2] + _domainSizeZ * _dx)) {
267 std::cout << "ERROR LBCouetteSolver::getVelocity(): Position " << pos << " out of range!" << std::endl;
268 std::cout << "domainOffset = " << domainOffset << std::endl;
269 std::cout << "_domainSizeX = " << _domainSizeX << std::endl;
270 std::cout << "_domainSizeY = " << _domainSizeY << std::endl;
271 std::cout << "_domainSizeZ = " << _domainSizeZ << std::endl;
272 std::cout << "_dx = " << _dx << std::endl;
273 exit(EXIT_FAILURE);
274 }
275 // compute index for respective cell (_dx+... for ghost cells); use coords
276 // to store local cell coordinates
277 for (unsigned int d = 0; d < 3; d++) {
278 coords[d] = (unsigned int)((_dx + pos[d] - domainOffset[d]) / _dx);
279 }
280 const int index = get(coords[0], coords[1], coords[2]);
282 // extract and scale velocity to "real"=MD units
283 for (int d = 0; d < 3; d++) {
284 vel[d] = _dx / _dt * _vel[3 * index + d];
285 }
286#if (COUPLING_MD_DEBUG == COUPLING_MD_YES)
287 std::cout << "Position " << pos << " corresponds to cell: " << coords << "; vel=" << vel << std::endl;
288#endif
289 return vel;
290 }
291
295 double getDensity(tarch::la::Vector<3, double> pos) const override {
298 // check pos-data for process locality (todo: put this in debug mode in
299 // future releases)
300 if ((pos[0] < domainOffset[0]) || (pos[0] > domainOffset[0] + _domainSizeX * _dx) || (pos[1] < domainOffset[1]) ||
301 (pos[1] > domainOffset[1] + _domainSizeY * _dx) || (pos[2] < domainOffset[2]) || (pos[2] > domainOffset[2] + _domainSizeZ * _dx)) {
302 std::cout << "ERROR LBCouetteSolver::getDensity(): Position " << pos << " out of range!" << std::endl;
303 exit(EXIT_FAILURE);
304 }
305 // compute index for respective cell (_dx+... for ghost cells); use coords
306 // to store local cell coordinates
307 for (unsigned int d = 0; d < 3; d++) {
308 coords[d] = (unsigned int)((_dx + pos[d] - domainOffset[d]) / _dx);
309 }
310 const int index = get(coords[0], coords[1], coords[2]);
311 return _density[index];
312 }
313
316 virtual void setWallVelocity(const tarch::la::Vector<3, double> wallVelocity) override { _wallVelocity = (_dt / _dx) * wallVelocity; }
317
321
322 std::unique_ptr<State> getState() override {
323 computeDensityAndVelocityEverywhere();
324 return std::make_unique<LBCouetteSolverState>(_pdfsize, _pdf1);
325 }
326
327 void setState(const std::unique_ptr<State>& input, int cycle) override {
328 const LBCouetteSolverState* state = dynamic_cast<const LBCouetteSolverState*>(input.get());
329
330#if (COUPLING_MD_ERROR == COUPLING_MD_YES)
331 if (state == nullptr) {
332 std::cout << "ERROR LBCouetteSolver setState() wrong state type" << std::endl;
333 exit(EXIT_FAILURE);
334 }
335#endif
336
337 std::copy(state->getData(), state->getData() + _pdfsize, _pdf1);
338 computeDensityAndVelocityEverywhere();
339
340 _counter = cycle;
341 }
342
343 std::unique_ptr<State> operator()(const std::unique_ptr<State>& input, int cycle) override {
344 setState(input, cycle);
345
346#if (COUPLING_MD_ERROR == COUPLING_MD_YES)
347 if (_mode != Mode::supervising) {
348 std::cout << "ERROR LBCouetteSolver operator() called but not in supervising mode" << std::endl;
349 exit(EXIT_FAILURE);
350 }
351#endif
352
353 advance(_dt_pint);
354 return getState();
355 }
356
357 Mode getMode() const override { return _mode; }
358
365 std::unique_ptr<PintableMacroSolver> getSupervisor(int num_cycles, double visc_multiplier) const override {
366#if (COUPLING_MD_ERROR == COUPLING_MD_YES)
367 if (_mode == Mode::supervising) {
368 std::cout << "ERROR LBCouetteSolver getSupervisor(): already in supervising mode" << std::endl;
369 exit(EXIT_FAILURE);
370 }
371#endif
372
373 int numThreads = 1;
374#if defined(_OPENMP)
375 numThreads = omp_get_num_threads();
376#endif
377
378 auto res = std::make_unique<LBCouetteSolver>(_channelheight, _wallVelocity * _dx / _dt, _kinVisc * visc_multiplier, _dx, _dt, _plotEveryTimestep, _plotAverageVelocity,
379 _filestem + std::string("_supervising"), _processes, numThreads, _scen);
380
381 res->_mode = Mode::supervising;
382 res->_dt_pint = _dt * num_cycles;
383
384 return res;
385 }
386
387 void print(std::ostream& os) const override {
388 if (_mode == Mode::supervising)
389 os << "<LBCouetteSolver instance in supervising mode >";
390 if (_mode == Mode::coupling)
391 os << "<LBCouetteSolver instance in coupling mode >";
392 }
393
394 double get_avg_vel(const std::unique_ptr<State>& state) const override {
395 double vel[3];
396 double density;
397 double res[3]{0, 0, 0};
398 for (int i = 0; i < _pdfsize; i += 19) {
399 LBCouetteSolver::computeDensityAndVelocity(vel, density, state->getData() + i);
400 res[0] += vel[0];
401 res[1] += vel[1];
402 res[2] += vel[2];
403 }
404 if (_pdfsize > 0) {
405 res[0] /= (_pdfsize / 19);
406 res[1] /= (_pdfsize / 19);
407 res[2] /= (_pdfsize / 19);
408 }
409 return std::sqrt(res[0] * res[0] + res[1] * res[1] + res[2] * res[2]);
410 }
411
412private:
413 Mode _mode;
414 double _dt_pint;
415
416 void computeDensityAndVelocityEverywhere() {
417 if (skipRank())
418 return;
419 for (int z = 1; z < _domainSizeZ + 1; z++) {
420 for (int y = 1; y < _domainSizeY + 1; y++) {
421 for (int x = 1; x < _domainSizeX + 1; x++) {
422 const int index = get(x, y, z);
423 const int pI = 19 * index;
424 double* vel = &_vel[3 * index];
425 computeDensityAndVelocity(vel, _density[index], &_pdf1[pI]);
426 }
427 }
428 }
429 }
430
431 void plot_avg_vel(){
432 if (!_plotAverageVelocity) return;
433
434 int rank = 0;
435#if (COUPLING_MD_PARALLEL == COUPLING_MD_YES)
436 MPI_Comm_rank(coupling::indexing::IndexingService<3>::getInstance().getComm(), &rank);
437#endif
438 std::stringstream ss;
439 ss << _filestem << "_r" << rank;
440 if (_scen != nullptr) {
441 auto ts = _scen->getTimeIntegrationService();
442 if (ts != nullptr) {
443 if (ts->isPintEnabled())
444 ss << "_i" << ts->getInteration();
445 }
446 }
447 ss << ".csv";
448 std::string filename = ss.str();
449 std::ofstream file(filename.c_str(), _counter==0 ? std::ofstream::out : std::ofstream::app);
450 if (!file.is_open()) {
451 std::cout << "ERROR LBCouetteSolver::plot_avg_vel(): Could not open file " << filename << "!" << std::endl;
452 exit(EXIT_FAILURE);
453 }
454
455 if(_counter == 0){
456 file << "coupling_cycle ; avg_vel" << std::endl;
457 }
458
459 std::unique_ptr<State> s = std::make_unique<LBCouetteSolverState>(_pdfsize, _pdf1);
460 double vel = get_avg_vel(s);
461 file << _counter << " ; " << vel << std::endl;
462 file.close();
463 }
464
468
472#pragma omp parallel for
473 for (int z = 1; z < _domainSizeZ + 1; z++) {
474 for (int y = 1; y < _domainSizeY + 1; y++) {
475 for (int x = 1; x < _domainSizeX + 1; x++) {
476 const int index = get(x, y, z);
477 if (_flag[index] == FLUID) {
478 stream(index);
479 collide(index, x, y, z);
480 }
481 }
482 }
483 }
484 // swap fields
485 double* swap = _pdf1;
486 _pdf1 = _pdf2;
487 _pdf2 = swap;
488 }
489
491 void stream(int index) {
492 const int pI = 19 * index;
493 for (int q = 0; q < 9; q++) {
494 const int nb = 19 * (_C[q][0] + _C[q][1] * _xO + _C[q][2] * _yO);
495 _pdf2[pI + q] = _pdf1[pI + q - nb];
496 _pdf2[pI + 18 - q] = _pdf1[pI + 18 - q + nb];
497 }
498 _pdf2[pI + 9] = _pdf1[pI + 9];
499 }
500
502 void collide(int index, int x, int y, int z) {
503 // index of start of cell-local pdfs in AoS
504 const int pI = 19 * index;
505 // compute and store density, velocity
506 double* vel = &_vel[3 * index];
507 computeDensityAndVelocity(vel, _density[index], &_pdf2[pI]);
508 // collide (BGK); always handle pdfs no. q and inv(q)=18-q in one step
509 const double u2 = 1.0 - 1.5 * (vel[0] * vel[0] + vel[1] * vel[1] + vel[2] * vel[2]);
510 // pdf 0,18
511 double cu = -vel[1] - vel[2];
512 int nb = -_xO - _yO;
513 double feq = _W[0] * _density[index] * (u2 + 3.0 * cu + 4.5 * cu * cu);
514 _pdf2[pI] -= _omega * (_pdf2[pI] - feq);
515 boundary(_pdf2, pI, x, y, z, 0, _flag[index + nb], pI + 19 * nb);
516 feq -= 6.0 * _W[0] * _density[index] * cu;
517 _pdf2[pI + 18] -= _omega * (_pdf2[pI + 18] - feq);
518 boundary(_pdf2, pI, x, y, z, 18, _flag[index - nb], pI - 19 * nb);
519 // pdf 1,17
520 cu = -vel[0] - vel[2];
521 nb = -1 - _yO;
522 feq = _W[1] * _density[index] * (u2 + 3.0 * cu + 4.5 * cu * cu);
523 _pdf2[pI + 1] -= _omega * (_pdf2[pI + 1] - feq);
524 boundary(_pdf2, pI, x, y, z, 1, _flag[index + nb], pI + 19 * nb);
525 feq -= 6.0 * _W[1] * _density[index] * cu;
526 _pdf2[pI + 17] -= _omega * (_pdf2[pI + 17] - feq);
527 boundary(_pdf2, pI, x, y, z, 17, _flag[index - nb], pI - 19 * nb);
528 // pdf 2,16
529 cu = -vel[2];
530 nb = -_yO;
531 feq = _W[2] * _density[index] * (u2 + 3.0 * cu + 4.5 * cu * cu);
532 _pdf2[pI + 2] -= _omega * (_pdf2[pI + 2] - feq);
533 boundary(_pdf2, pI, x, y, z, 2, _flag[index + nb], pI + 19 * nb);
534 feq -= 6.0 * _W[2] * _density[index] * cu;
535 _pdf2[pI + 16] -= _omega * (_pdf2[pI + 16] - feq);
536 boundary(_pdf2, pI, x, y, z, 16, _flag[index - nb], pI - 19 * nb);
537 // pdf 3,15
538 cu = vel[0] - vel[2];
539 nb = 1 - _yO;
540 feq = _W[3] * _density[index] * (u2 + 3.0 * cu + 4.5 * cu * cu);
541 _pdf2[pI + 3] -= _omega * (_pdf2[pI + 3] - feq);
542 boundary(_pdf2, pI, x, y, z, 3, _flag[index + nb], pI + 19 * nb);
543 feq -= 6.0 * _W[3] * _density[index] * cu;
544 _pdf2[pI + 15] -= _omega * (_pdf2[pI + 15] - feq);
545 boundary(_pdf2, pI, x, y, z, 15, _flag[index - nb], pI - 19 * nb);
546 // pdf 4,14
547 cu = vel[1] - vel[2];
548 nb = _xO - _yO;
549 feq = _W[4] * _density[index] * (u2 + 3.0 * cu + 4.5 * cu * cu);
550 _pdf2[pI + 4] -= _omega * (_pdf2[pI + 4] - feq);
551 boundary(_pdf2, pI, x, y, z, 4, _flag[index + nb], pI + 19 * nb);
552 feq -= 6.0 * _W[4] * _density[index] * cu;
553 _pdf2[pI + 14] -= _omega * (_pdf2[pI + 14] - feq);
554 boundary(_pdf2, pI, x, y, z, 14, _flag[index - nb], pI - 19 * nb);
555 // pdf 5,13
556 cu = -vel[0] - vel[1];
557 nb = -1 - _xO;
558 feq = _W[5] * _density[index] * (u2 + 3.0 * cu + 4.5 * cu * cu);
559 _pdf2[pI + 5] -= _omega * (_pdf2[pI + 5] - feq);
560 boundary(_pdf2, pI, x, y, z, 5, _flag[index + nb], pI + 19 * nb);
561 feq -= 6.0 * _W[5] * _density[index] * cu;
562 _pdf2[pI + 13] -= _omega * (_pdf2[pI + 13] - feq);
563 boundary(_pdf2, pI, x, y, z, 13, _flag[index - nb], pI - 19 * nb);
564 // pdf 6,12
565 cu = -vel[1];
566 nb = -_xO;
567 feq = _W[6] * _density[index] * (u2 + 3.0 * cu + 4.5 * cu * cu);
568 _pdf2[pI + 6] -= _omega * (_pdf2[pI + 6] - feq);
569 boundary(_pdf2, pI, x, y, z, 6, _flag[index + nb], pI + 19 * nb);
570 feq -= 6.0 * _W[6] * _density[index] * cu;
571 _pdf2[pI + 12] -= _omega * (_pdf2[pI + 12] - feq);
572 boundary(_pdf2, pI, x, y, z, 12, _flag[index - nb], pI - 19 * nb);
573 // pdf 7,11
574 cu = vel[0] - vel[1];
575 nb = 1 - _xO;
576 feq = _W[7] * _density[index] * (u2 + 3.0 * cu + 4.5 * cu * cu);
577 _pdf2[pI + 7] -= _omega * (_pdf2[pI + 7] - feq);
578 boundary(_pdf2, pI, x, y, z, 7, _flag[index + nb], pI + 19 * nb);
579 feq -= 6.0 * _W[7] * _density[index] * cu;
580 _pdf2[pI + 11] -= _omega * (_pdf2[pI + 11] - feq);
581 boundary(_pdf2, pI, x, y, z, 11, _flag[index - nb], pI - 19 * nb);
582 // pdf 8,10
583 cu = -vel[0];
584 nb = -1;
585 feq = _W[8] * _density[index] * (u2 + 3.0 * cu + 4.5 * cu * cu);
586 _pdf2[pI + 8] -= _omega * (_pdf2[pI + 8] - feq);
587 boundary(_pdf2, pI, x, y, z, 8, _flag[index + nb], pI + 19 * nb);
588 feq -= 6.0 * _W[8] * _density[index] * cu;
589 _pdf2[pI + 10] -= _omega * (_pdf2[pI + 10] - feq);
590 boundary(_pdf2, pI, x, y, z, 10, _flag[index - nb], pI - 19 * nb);
591 // pdf 9
592 _pdf2[pI + 9] -= _omega * (_pdf2[pI + 9] - _W[9] * _density[index] * u2);
593 }
594
604 void boundary(double* const pdf, int index, int x, int y, int z, int q, const Flag& flag, int nbIndex) {
605 if (flag != FLUID) {
606 if (flag == NO_SLIP) {
607 // half-way bounce back
608 pdf[nbIndex + 18 - q] = pdf[index + q];
609 } else if (flag == MOVING_WALL) {
610 // half-way bounce back + moving wall acceleration (only x-direction for
611 // wall supported at the moment)
612 pdf[nbIndex + 18 - q] =
613 pdf[index + q] - 6.0 * _W[q] * _density[index / 19] * (_C[q][0] * _wallVelocity[0] + _C[q][1] * _wallVelocity[1] + _C[q][2] * _wallVelocity[2]);
614 } else if (flag == PERIODIC) {
615 // periodic treatment
616 int target[3] = {x, y, z};
617 if (target[0] + _C[q][0] == 0) {
618 target[0] = _domainSizeX + 1;
619 } else if (target[0] + _C[q][0] == _domainSizeX + 1) {
620 target[0] = 0;
621 }
622 if (target[1] + _C[q][1] == 0) {
623 target[1] = _domainSizeY + 1;
624 } else if (target[1] + _C[q][1] == _domainSizeY + 1) {
625 target[1] = 0;
626 }
627 if (target[2] + _C[q][2] == 0) {
628 target[2] = _domainSizeZ + 1;
629 } else if (target[2] + _C[q][2] == _domainSizeZ + 1) {
630 target[2] = 0;
631 }
632 const int periodicNb = target[0] + (_domainSizeX + 2) * (target[1] + (_domainSizeY + 2) * target[2]);
633 pdf[19 * periodicNb + q] = pdf[index + q];
634 }
635 }
636 }
637
642 static void computeDensityAndVelocity(double* const vel, double& density, const double* const pdf) {
643 vel[0] = -(pdf[1] + pdf[5] + pdf[8] + pdf[11] + pdf[15]);
644 density = pdf[3] + pdf[7] + pdf[10] + pdf[13] + pdf[17];
645 vel[1] = (pdf[4] + pdf[11] + pdf[12] + pdf[13] + pdf[18]) - (pdf[0] + pdf[5] + pdf[6] + pdf[7] + pdf[14]);
646 vel[0] = density + vel[0];
647 density = density + pdf[0] + pdf[1] + pdf[2] + pdf[4] + pdf[5] + pdf[6] + pdf[8] + pdf[9] + pdf[11] + pdf[12] + pdf[14] + pdf[15] + pdf[16] + pdf[18];
648 vel[2] = (pdf[14] + pdf[15] + pdf[16] + pdf[17] + pdf[18]) - (pdf[0] + pdf[1] + pdf[2] + pdf[3] + pdf[4]);
649 vel[0] = vel[0] / density;
650 vel[1] = vel[1] / density;
651 vel[2] = vel[2] / density;
652 }
653
668 void communicatePart(double* pdf, double* sendBuffer, double* recvBuffer, NbFlag nbFlagTo, NbFlag nbFlagFrom, tarch::la::Vector<3, int> startSend,
670#if (COUPLING_MD_PARALLEL == COUPLING_MD_YES)
671 // directions that point to LEFT/RIGHT,... -> same ordering as enums!
672 const int directions[6][5] = {{1, 5, 8, 11, 15}, {3, 7, 10, 13, 17}, {4, 11, 12, 13, 18}, {0, 5, 6, 7, 14}, {0, 1, 2, 3, 4}, {14, 15, 16, 17, 18}};
673 MPI_Request requests[2];
674 MPI_Status status[2];
676 tarch::la::Vector<2, int> domainSize;
677 // find out plane coordinates
678 if (nbFlagTo == LEFT || nbFlagTo == RIGHT) {
679 plane[0] = 1;
680 plane[1] = 2;
681 domainSize[0] = _domainSizeY;
682 domainSize[1] = _domainSizeZ;
683 } else if (nbFlagTo == FRONT || nbFlagTo == BACK) {
684 plane[0] = 0;
685 plane[1] = 2;
686 domainSize[0] = _domainSizeX;
687 domainSize[1] = _domainSizeZ;
688 } else if (nbFlagTo == TOP || nbFlagTo == BOTTOM) {
689 plane[0] = 0;
690 plane[1] = 1;
691 domainSize[0] = _domainSizeX;
692 domainSize[1] = _domainSizeY;
693 } else {
694 std::cout << "ERROR LBCouetteSolver::communicatePart: d >2 or d < 0!" << std::endl;
695 exit(EXIT_FAILURE);
696 }
697 // extract data and write to send buffer
699 for (coords[2] = startSend[2]; coords[2] < endSend[2]; coords[2]++) {
700 for (coords[1] = startSend[1]; coords[1] < endSend[1]; coords[1]++) {
701 for (coords[0] = startSend[0]; coords[0] < endSend[0]; coords[0]++) {
702 for (int q = 0; q < 5; q++) {
703 sendBuffer[q + 5 * getParBuf(coords[plane[0]], coords[plane[1]], domainSize[0], domainSize[1])] =
704 pdf[directions[nbFlagTo][q] + 19 * get(coords[0], coords[1], coords[2])];
705 }
706 }
707 }
708 }
709 // send and receive data
710 MPI_Irecv(recvBuffer, (domainSize[0] + 2) * (domainSize[1] + 2) * 5, MPI_DOUBLE, _parallelNeighbours[nbFlagFrom], 1000,
711 coupling::indexing::IndexingService<3>::getInstance().getComm(), &requests[0]);
712 MPI_Isend(sendBuffer, (domainSize[0] + 2) * (domainSize[1] + 2) * 5, MPI_DOUBLE, _parallelNeighbours[nbFlagTo], 1000,
713 coupling::indexing::IndexingService<3>::getInstance().getComm(), &requests[1]);
714 MPI_Waitall(2, requests, status);
715 // write data back to pdf field
716 if (_parallelNeighbours[nbFlagFrom] != MPI_PROC_NULL) {
717 for (coords[2] = startRecv[2]; coords[2] < endRecv[2]; coords[2]++) {
718 for (coords[1] = startRecv[1]; coords[1] < endRecv[1]; coords[1]++) {
719 for (coords[0] = startRecv[0]; coords[0] < endRecv[0]; coords[0]++) {
720 for (int q = 0; q < 5; q++) {
721 if (_flag[get(coords[0], coords[1], coords[2])] == PARALLEL_BOUNDARY) {
722 pdf[directions[nbFlagTo][q] + 19 * get(coords[0], coords[1], coords[2])] =
723 recvBuffer[q + 5 * getParBuf(coords[plane[0]], coords[plane[1]], domainSize[0], domainSize[1])];
724 }
725 }
726 }
727 }
728 }
729 }
730#endif
731 }
732
735 void communicate() {
736#if (COUPLING_MD_PARALLEL == COUPLING_MD_YES)
737 // send from right to left
741 // send from left to right
745 // send from back to front
749 // send from front to back
753 // send from top to bottom
757 // send from bottom to top
761#endif
762 }
763
765 const double _omega;
768 int _pdfsize{0};
770 double* _pdf1{NULL};
772 double* _pdf2{NULL};
774 const int _C[19][3]{{0, -1, -1}, {-1, 0, -1}, {0, 0, -1}, {1, 0, -1}, {0, 1, -1}, {-1, -1, 0}, {0, -1, 0}, {1, -1, 0}, {-1, 0, 0}, {0, 0, 0},
775 {1, 0, 0}, {-1, 1, 0}, {0, 1, 0}, {1, 1, 0}, {0, -1, 1}, {-1, 0, 1}, {0, 0, 1}, {1, 0, 1}, {0, 1, 1}};
776
777 const double _W[19]{1.0 / 36.0, 1.0 / 36.0, 1.0 / 18.0, 1.0 / 36.0, 1.0 / 36.0, 1.0 / 36.0, 1.0 / 18.0, 1.0 / 36.0, 1.0 / 18.0, 1.0 / 3.0,
778 1.0 / 18.0, 1.0 / 36.0, 1.0 / 18.0, 1.0 / 36.0, 1.0 / 36.0, 1.0 / 36.0, 1.0 / 18.0, 1.0 / 36.0, 1.0 / 36.0};
779
781};
782
783#endif // _MOLECULARDYNAMICS_COUPLING_SOLVERS_LBCOUETTESOLVER_H_
Definition Scenario.h:18
defines the cell type with cell-averaged quantities only (no linked cells).
Definition CouplingCell.h:29
const tarch::la::Vector< dim, double > & getMacroscopicMomentum() const
Definition CouplingCell.h:64
const double & getMacroscopicMass() const
Definition CouplingCell.h:58
provides access to coupling cells, which may belong to different indexing domains
Definition FlexibleCellContainer.h:30
value_T get() const
Definition CellIndex.h:138
Definition PintableMacroSolver.h:67
Definition PintableMacroSolver.h:30
Definition LBCouetteSolver.h:21
int getSizeBytes() const override
Definition LBCouetteSolver.h:31
std::unique_ptr< State > operator+(const State &rhs) override
const double * getData() const override
Definition LBCouetteSolver.h:44
void print(std::ostream &os) const override
Definition LBCouetteSolver.h:46
std::unique_ptr< State > operator-(const State &rhs) override
bool operator==(const State &rhs) const override
Definition LBCouetteSolver.h:36
double * getData() override
Definition LBCouetteSolver.h:43
implements a three-dimensional Lattice-Boltzmann Couette flow solver.
Definition LBCouetteSolver.h:56
static void computeDensityAndVelocity(double *const vel, double &density, const double *const pdf)
refers to the LB method; computes density and velocity on pdf
Definition LBCouetteSolver.h:642
std::unique_ptr< State > operator()(const std::unique_ptr< State > &input, int cycle) override
Definition LBCouetteSolver.h:343
std::unique_ptr< PintableMacroSolver > getSupervisor(int num_cycles, double visc_multiplier) const override
Definition LBCouetteSolver.h:365
void communicatePart(double *pdf, double *sendBuffer, double *recvBuffer, NbFlag nbFlagTo, NbFlag nbFlagFrom, tarch::la::Vector< 3, int > startSend, tarch::la::Vector< 3, int > endSend, tarch::la::Vector< 3, int > startRecv, tarch::la::Vector< 3, int > endRecv)
Definition LBCouetteSolver.h:668
std::unique_ptr< State > getState() override
Definition LBCouetteSolver.h:322
double get_avg_vel(const std::unique_ptr< State > &state) const override
Definition LBCouetteSolver.h:394
const double _omega
relaxation frequency
Definition LBCouetteSolver.h:765
const int _C[19][3]
lattice velocities
Definition LBCouetteSolver.h:774
void setMDBoundaryValues(coupling::datastructures::FlexibleCellContainer< 3 > &md2macroBuffer) override
applies the values received from the MD-solver within the conntinuum solver
Definition LBCouetteSolver.h:199
double * _pdf1
partical distribution function field
Definition LBCouetteSolver.h:770
void collide(int index, int x, int y, int z)
Definition LBCouetteSolver.h:502
void boundary(double *const pdf, int index, int x, int y, int z, int q, const Flag &flag, int nbIndex)
takes care of the correct boundary treatment for the LB method
Definition LBCouetteSolver.h:604
Mode getMode() const override
Definition LBCouetteSolver.h:357
const double _W[19]
lattice weights
Definition LBCouetteSolver.h:777
void communicate()
comunicates the boundary field data between the different processes
Definition LBCouetteSolver.h:735
tarch::la::Vector< 3, double > getVelocity(tarch::la::Vector< 3, double > pos) const override
returns velocity at a certain position
Definition LBCouetteSolver.h:260
void advance(double dt) override
advances one time step dt in time and triggers vtk plot if required
Definition LBCouetteSolver.h:175
virtual ~LBCouetteSolver()
a simple destructor
Definition LBCouetteSolver.h:124
virtual void setWallVelocity(const tarch::la::Vector< 3, double > wallVelocity) override
changes the velocity at the moving wall (z=0)
Definition LBCouetteSolver.h:316
tarch::la::Vector< 3, double > _wallVelocity
velocity of moving wall of Couette flow
Definition LBCouetteSolver.h:767
const bool _plotAverageVelocity
enables avg_vel CSV output
Definition LBCouetteSolver.h:780
void collidestream()
collide-stream algorithm for the Lattice-Boltzmann method
Definition LBCouetteSolver.h:471
void setState(const std::unique_ptr< State > &input, int cycle) override
Definition LBCouetteSolver.h:327
void print(std::ostream &os) const override
Definition LBCouetteSolver.h:387
double * _pdf2
partial distribution function field (stores the old time step)
Definition LBCouetteSolver.h:772
LBCouetteSolver(const double channelheight, tarch::la::Vector< 3, double > wallVelocity, const double kinVisc, const double dx, const double dt, const int plotEveryTimestep, const bool plotAverageVelocity, const std::string filestem, const tarch::la::Vector< 3, unsigned int > processes, const unsigned int numThreads=1, const Scenario *scen=nullptr)
a simple constructor
Definition LBCouetteSolver.h:73
void stream(int index)
the stream part of the LB algorithm (from pdf1 to pdf2)
Definition LBCouetteSolver.h:491
double getDensity(tarch::la::Vector< 3, double > pos) const override
returns density at a certain position
Definition LBCouetteSolver.h:295
is a virtual base class for the interface for a numerical fluid solver for the Couette scenario
Definition NumericalSolver.h:33
const int _domainSizeX
domain size in x-direction
Definition NumericalSolver.h:554
double * _recvBufferY
buffer to receive data from from front/back neighbour
Definition NumericalSolver.h:584
double * _sendBufferX
buffer to send data from left/right to right/left neighbour
Definition NumericalSolver.h:578
const double _channelheight
the height and width of the channel in z and y direction
Definition NumericalSolver.h:539
const double _kinVisc
kinematic viscosity of the fluid
Definition NumericalSolver.h:545
int _counter
time step counter
Definition NumericalSolver.h:569
int get(int i) const
returns i and performs checks in debug mode
Definition NumericalSolver.h:360
const int _yO
offset for z-direction
Definition NumericalSolver.h:593
double * _recvBufferZ
buffer to receive data from from top/buttom neighbour
Definition NumericalSolver.h:588
const int _avgDomainSizeZ
avg. domain size in MPI-parallel simulation in z-direction
Definition NumericalSolver.h:564
int getParBuf(int x, int y, int lengthx, int lengthy) const
returns index in 2D parallel buffer with buffer dimensions lengthx+2,lengthy+2. Performs checks in de...
Definition NumericalSolver.h:409
double * _sendBufferY
buffer to send data from front/back to front/back neighbour
Definition NumericalSolver.h:582
const int _xO
offset for y-direction (lexicographic grid ordering)
Definition NumericalSolver.h:591
double * _sendBufferZ
buffer to send data from top/buttom to top/buttom neighbour
Definition NumericalSolver.h:586
NumericalSolver(const double channelheight, const double dx, const double dt, const double kinVisc, const int plotEveryTimestep, const std::string filestem, const tarch::la::Vector< 3, unsigned int > processes, const Scenario *scen=nullptr)
a simple constructor
Definition NumericalSolver.h:46
Flag * _flag
flag field
Definition NumericalSolver.h:575
const int _avgDomainSizeY
avg. domain size in MPI-parallel simulation in y-direction
Definition NumericalSolver.h:562
const tarch::la::Vector< 3, unsigned int > _coords
coordinates of this process (=1,1,1, unless parallel run of the solver )
Definition NumericalSolver.h:567
const int _domainSizeY
domain size in y-direction
Definition NumericalSolver.h:556
const double _dt
time step
Definition NumericalSolver.h:543
tarch::la::Vector< 3, unsigned int > _processes
domain decomposition on MPI rank basis; total number is given by multipling all entries
Definition NumericalSolver.h:548
double * _density
density field
Definition NumericalSolver.h:573
tarch::la::Vector< 6, int > _parallelNeighbours
neighbour ranks
Definition NumericalSolver.h:595
const std::string _filestem
file stem for vtk plot
Definition NumericalSolver.h:552
NbFlag
The flags are used on parallel boundaries to define in which direction the boundary goes.
Definition NumericalSolver.h:530
@ LEFT
a parallel boundary to the left
Definition NumericalSolver.h:531
@ RIGHT
a parallel boundary to the right
Definition NumericalSolver.h:532
@ BOTTOM
a parallel boundary to the bottom
Definition NumericalSolver.h:535
@ FRONT
a parallel boundary to the front
Definition NumericalSolver.h:534
@ TOP
a parallel boundary to the top
Definition NumericalSolver.h:536
@ BACK
a parallel boundary to the back
Definition NumericalSolver.h:533
Flag
for every cell exists a flag entry, upon this is defined how the cell is handled
Definition NumericalSolver.h:519
@ MD_BOUNDARY
a cell on the boundary to md
Definition NumericalSolver.h:524
@ PERIODIC
a cell on a periodic boundary
Definition NumericalSolver.h:523
@ PARALLEL_BOUNDARY
a cell on a inner boundary of a splitted domain in a parallel run
Definition NumericalSolver.h:525
@ NO_SLIP
a cell on the no slip (non-moving) wall
Definition NumericalSolver.h:521
@ FLUID
a normal fluid cell
Definition NumericalSolver.h:520
@ MOVING_WALL
a cell on the moving wall
Definition NumericalSolver.h:522
const int _plotEveryTimestep
number of time steps between vtk plots
Definition NumericalSolver.h:550
double * _vel
velocity field
Definition NumericalSolver.h:571
const int _avgDomainSizeX
avg. domain size in MPI-parallel simulation in x-direction
Definition NumericalSolver.h:560
bool skipRank() const
returns true, if this rank is not of relevance for the LB simulation
Definition NumericalSolver.h:509
double * _recvBufferX
buffer to receive data from from left/right neighbour
Definition NumericalSolver.h:580
tarch::la::Vector< 3, int > _offset
offset of the md domain
Definition NumericalSolver.h:597
const double _dx
mesh size, dx=dy=dz
Definition NumericalSolver.h:541
const int _domainSizeZ
domain size in z-direction
Definition NumericalSolver.h:558
Definition Vector.h:24
all numerical solvers are defined in the namespace, and their interfaces
Definition CouetteSolver.h:14
everything necessary for coupling operations, is defined in here
Definition AdditiveMomentumInsertion.h:15