MaMiCo 1.2
Loading...
Searching...
No Matches
FDCouetteSolver.h
1#ifndef _MOLECULARDYNAMICS_COUPLING_SOLVERS_FINITEDIFFERENCE_H_
2#define _MOLECULARDYNAMICS_COUPLING_SOLVERS_FINITEDIFFERENCE_H_
3
4#include "coupling/solvers/NumericalSolver.h"
5#include "tarch/la/Vector.h"
6#include <cmath>
7
8namespace coupling {
9namespace solvers {
11}
12} // namespace coupling
13
21public:
35 FiniteDifferenceSolver(const double channelheight, tarch::la::Vector<3, double> wallVelocity, const double kinVisc, const double dx, const double dt,
36 const int plotEveryTimestep, const std::string filestem, const tarch::la::Vector<3, unsigned int> processes)
37 : coupling::solvers::NumericalSolver(channelheight, dx, dt, kinVisc, plotEveryTimestep, filestem, processes), _omega(dt * kinVisc / (dx * dx)),
38 _wallVelocity(wallVelocity) {
39 if (_processes[0] > 1 || _processes[1] > 1 || _processes[2] > 1) {
40 _processes[0] = 1;
41 _processes[1] = 1;
42 _processes[2] = 1;
43 std::cout << "The FiniteDifferenceSolver was requested in a parallel manner. It will be run sequentially, since it doesn't support parallel runs. "
44 << std::endl;
45 }
46 // return if required
47 if (skipRank()) {
48 return;
49 }
50 _velold = new double[3 * (_domainSizeX + 2) * (_domainSizeY + 2) * (_domainSizeZ + 2)];
51#if (COUPLING_MD_DEBUG == COUPLING_MD_YES)
52 std::cout << "Domain size=" << _domainSizeX << "," << _domainSizeY << "," << _domainSizeZ << std::endl;
53 std::cout << "tau=" << 1.0 / _omega << std::endl;
54 std::cout << "wallVelocity=" << _wallVelocity << std::endl;
55 for (int z = 0; z < _domainSizeZ + 2; z++) {
56 for (int y = 0; y < _domainSizeY + 2; y++) {
57 for (int x = 0; x < _domainSizeX + 2; x++) {
58 std::cout << x << "," << y << "," << z << "FLAG=" << _flag[get(x, y, z)] << std::endl;
59 }
60 }
61 }
62#endif
63 // check pointers
64 if ((!_vel) || (!_density) || (!_flag)) {
65 std::cout << "ERROR FiniteDifferenceSolver: nullptr!" << std::endl;
66 exit(EXIT_FAILURE);
67 }
68 }
69
72 if (_velold) {
73 delete[] _velold;
74 _velold = nullptr;
75 }
76 }
77
80 void advance(double dt) override {
81 if (skipRank()) {
82 return;
83 }
84 const int timesteps = floor(dt / _dt + 0.5);
85 if (fabs(timesteps * _dt - dt) / _dt > 1.0e-8) {
86 std::cout << "ERROR FiniteDifferenceSolver::advance(): time steps and dt do not match!" << std::endl;
87 exit(EXIT_FAILURE);
88 }
89 for (int i = 0; i < timesteps; i++) {
91 update();
92 plot();
93 _counter++;
94 }
95 }
96
102#if (COUPLING_MD_DEBUG == COUPLING_MD_YES)
103 if ((pos[0] < domainOffset[0]) || (pos[0] > domainOffset[0] + _domainSizeX * _dx) || (pos[1] < domainOffset[1]) ||
104 (pos[1] > domainOffset[1] + _domainSizeY * _dx) || (pos[2] < domainOffset[2]) || (pos[2] > domainOffset[2] + _domainSizeZ * _dx)) {
105 std::cout << "ERROR FiniteDifferenceSolver::getVelocity(): Position " << pos << " out of range!" << std::endl;
106 exit(EXIT_FAILURE);
107 }
108#endif
109 // compute index for respective cell (_dx+... for ghost cells); use coords
110 // to store local cell coordinates
112 for (unsigned int d = 0; d < 3; d++) {
113 coords[d] = (unsigned int)((_dx + pos[d] - domainOffset[d]) / _dx);
114 }
115 const int index = get(coords[0], coords[1], coords[2]);
117 // extract velocity
118 for (int d = 0; d < 3; d++) {
119 vel[d] = _vel[3 * index + d];
120 }
121 return vel;
122 }
123
126 virtual void setWallVelocity(const tarch::la::Vector<3, double> wallVelocity) override { _wallVelocity = wallVelocity; }
127
131 double getDensity(tarch::la::Vector<3, double> pos) const override {
134#if (COUPLING_MD_DEBUG == COUPLING_MD_YES)
135 if ((pos[0] < domainOffset[0]) || (pos[0] > domainOffset[0] + _domainSizeX * _dx) || (pos[1] < domainOffset[1]) ||
136 (pos[1] > domainOffset[1] + _domainSizeY * _dx) || (pos[2] < domainOffset[2]) || (pos[2] > domainOffset[2] + _domainSizeZ * _dx)) {
137 std::cout << "ERROR FiniteDifferenceSolver::getDensity(): Position " << pos << " out of range!" << std::endl;
138 exit(EXIT_FAILURE);
139 }
140#endif
141 for (unsigned int d = 0; d < 3; d++) {
142 coords[d] = (unsigned int)((_dx + pos[d] - domainOffset[d]) / _dx);
143 }
144 const int index = get(coords[0], coords[1], coords[2]);
145 return _density[index];
146 }
147
153
155 if (skipRank()) {
156 return;
157 }
158 for (auto pair : md2macroBuffer) {
159 I01 idx;
161 std::tie(couplingCell, idx) = pair;
162 // determine cell index of this cell in continuum domain
163 tarch::la::Vector<3, unsigned int> globalCellCoords{I01{idx}.get()};
164 globalCellCoords[0] = (globalCellCoords[0] + _offset[0]) - _coords[0] * _avgDomainSizeX;
165 globalCellCoords[1] = (globalCellCoords[1] + _offset[1]) - _coords[1] * _avgDomainSizeY;
166 globalCellCoords[2] = (globalCellCoords[2] + _offset[2]) - _coords[2] * _avgDomainSizeZ;
167#if (COUPLING_MD_DEBUG == COUPLING_MD_YES)
168 std::cout << "Process coords: " << _coords << ": GlobalCellCoords for index ";
169 std::cout << I01{idx} << ": " << globalCellCoords << std::endl;
170#endif
171 const int index = get(globalCellCoords[0], globalCellCoords[1], globalCellCoords[2]);
172#if (COUPLING_MD_DEBUG == COUPLING_MD_YES)
173 if (_flag[index] != MD_BOUNDARY) {
174 std::cout << "ERROR FiniteDifferenceSolver::setMDBoundaryValues(): Cell " << index << " is no MD boundary cell!" << std::endl;
175 exit(EXIT_FAILURE);
176 }
177#endif
178 // set velocity value in MD boundary cell (before streaming); the boundary velocities are interpolated between the neighbouring and this cell. This
179 // interpolation is valid for FLUID-MD_BOUNDARY neighbouring relations only. determine local velocity received from MaMiCo and convert it to LB units;
180 // store the velocity in _vel
181 tarch::la::Vector<3, double> localVel((1.0 / couplingCell->getMacroscopicMass()) * couplingCell->getMacroscopicMomentum());
182 for (unsigned int d = 0; d < 3; d++) {
183 _vel[3 * index + d] = localVel[d];
184 }
185 }
186 }
187
188private:
195#pragma omp parallel for
196 for (int i = 0; i < _yO; i++) {
197 _vel[3 * i] = 2 * _wallVelocity[0] - _vel[3 * (i + _yO)];
198 }
199 }
200
202 void update() {
203 double* swap = _velold;
204 _velold = _vel;
205 _vel = swap;
206#pragma omp parallel for
207 for (int i = _yO; i < (_domainSizeX + 2) * (_domainSizeY + 2) * (_domainSizeZ + 2) - _yO; i++) {
208 if (_flag[i] == FLUID) {
209 _vel[3 * i] = _omega * (_velold[3 * (i - _yO)] - 2 * _velold[3 * i] + _velold[3 * (i + _yO)]) + _velold[3 * i];
210 }
211 }
212 }
213
215 const double _omega;
219 double* _velold{nullptr};
220};
221#endif
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
implements a simple one-dimensional finite-diffference solver for the Couette flow.
Definition FDCouetteSolver.h:20
void setBeyondWall()
updates the velocities at boundaries
Definition FDCouetteSolver.h:194
virtual void setWallVelocity(const tarch::la::Vector< 3, double > wallVelocity) override
changes the velocity at the moving wall (z=0)
Definition FDCouetteSolver.h:126
double * _velold
the velocity field from the last time step
Definition FDCouetteSolver.h:219
const double _omega
factor for the finite difference stencil = dt*kinVisc/(dx*dx)
Definition FDCouetteSolver.h:215
void setMDBoundaryValues(coupling::datastructures::FlexibleCellContainer< 3 > &md2macroBuffer) override
applies the values received from the MD-solver within the conntinuum solver
Definition FDCouetteSolver.h:154
FiniteDifferenceSolver(const double channelheight, tarch::la::Vector< 3, double > wallVelocity, const double kinVisc, const double dx, const double dt, const int plotEveryTimestep, const std::string filestem, const tarch::la::Vector< 3, unsigned int > processes)
a simple constructor
Definition FDCouetteSolver.h:35
tarch::la::Vector< 3, double > _wallVelocity
velocity of moving wall of Couette flow
Definition FDCouetteSolver.h:217
~FiniteDifferenceSolver()
a simple destructor
Definition FDCouetteSolver.h:71
void advance(double dt) override
advances one time step dt in time and triggers vtk plot if required
Definition FDCouetteSolver.h:80
tarch::la::Vector< 3, double > getVelocity(tarch::la::Vector< 3, double > pos) const override
gets the velocity at a given position
Definition FDCouetteSolver.h:100
void update()
update the velocity in the cells which are flagged as FLUID
Definition FDCouetteSolver.h:202
double getDensity(tarch::la::Vector< 3, double > pos) const override
returns density at a certain position
Definition FDCouetteSolver.h:131
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
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
const int _avgDomainSizeZ
avg. domain size in MPI-parallel simulation in z-direction
Definition NumericalSolver.h:564
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
@ MD_BOUNDARY
a cell on the boundary to md
Definition NumericalSolver.h:524
@ FLUID
a normal fluid cell
Definition NumericalSolver.h:520
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
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:25
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