hemocell
Loading...
Searching...
No Matches
leesEdwardsBC.h
Go to the documentation of this file.
1/*
2This file is part of the HemoCell library
3
4HemoCell is developed and maintained by the Computational Science Lab
5in the University of Amsterdam. Any questions or remarks regarding this library
6can be sent to: info@hemocell.eu
7
8When using the HemoCell library in scientific work please cite the
9corresponding paper: https://doi.org/10.3389/fphys.2017.00563
10
11The HemoCell library is free software: you can redistribute it and/or
12modify it under the terms of the GNU Affero General Public License as
13published by the Free Software Foundation, either version 3 of the
14License, or (at your option) any later version.
15
16The library is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU Affero General Public License for more details.
20
21You should have received a copy of the GNU Affero General Public License
22along with this program. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25/*
26Lees-Edwards boundary condition class which makes use of two data processors
27for calculating and writing of the populations according to the Lees-Edwards algorithm.
28
29Populations are read and writen between two 2D arrays (topPopulations & bottomPopulations).
30The memory address for the current Lees-Edwards displacement is shared with the hemoCell class.
31
32The 2D arrays and Lees-Edwards displacement are members of the LeesEdwardsBC object; the data
33processors of all lattice blocks refer to them through pointers. The object must therefore
34live as long as the lattice is iterated, and it is not copyable.
35
36The velocity in the system comes from setting the macroscopic velocity in the boundary nodes
37
38NOTE: currently the Lees-Edwards algorithm works only in one direction (z-direction)
39
40* @author Daan van Ingen
41*/
42
43#ifndef LEESEDWARDSBC
44#define LEESEDWARDSBC
45
46#include "hemocell.h"
47#include "palabos3D.h"
48#include "palabos3D.hh"
49
50namespace hemo {
51
52/* Calculate the streaming of the populations according to the Lees-Edwards algorithm
53 Inherits from data processing functional which handles the subdivision of the code
54 onto individual lattice blocks
55*/
56template <typename T, template <typename U> class Descriptor>
57class LeesEdwardsBCGetPopulations : public plb::BoxProcessingFunctional3D_L<T, Descriptor> {
58public:
59 plint nx; // dimension in x direction
60 plint nz; // dimension in z direction
61 double * LEcurrentDisplacement; // Current Lees-Edwards displacement
62 std::vector<std::vector<std::vector<T>>> * topPopulations; // Data structure containing the top populations
63 std::vector<std::vector<std::vector<T>>> * bottomPopulations; // Data structure containing the bottom populations
64 T topVelocity; // Macroscopic top velocity
65 T bottomVelocity; // Macroscopic bottom velocity
66
68 std::vector<std::vector<std::vector<T>>> * topPopulations,
69 std::vector<std::vector<std::vector<T>>> * bottomPopulations) {
70 this->nx = nx;
71 this->nz = nz;
72 this->LEcurrentDisplacement = LEcurrentDisplacement;
73 this->topVelocity = topVelocity;
74 this->bottomVelocity = bottomVelocity;
75 this->topPopulations = topPopulations;
76 this->bottomPopulations = bottomPopulations;
77 }
78
79 /* Convert Palabos array to vector
80
81 * @param populations Palabos array containing lattice node populations
82 * @return vector containing lattice node populations
83 */
84 std::vector<T> populationsArrayToVector(plb::Array<T, Descriptor<T>::q> populations) {
85 std::vector<T> pop(Descriptor<T>::q);
86 for (plint i = 0; i < Descriptor<T>::q; i++) { pop[i] = populations[i]; }
87 return pop;
88 }
89
90 /* Non-negative modulo (-a % b gives a negative number in C++)
91 */
92 plint modNonNegative(plint a, plint b) { return (a % b + b) % b; }
93
94 /* Process function containg the code to be executed onto the individual lattice block.
95 Set macroscopic velocity
96 Calculate new populations according to the Lees-Edwards algorithm
97
98 * @param domain Domain on which to execute code
99 * @param lattice individual block lattice
100 */
101 virtual void process(plb::Box3D domain, plb::BlockLattice3D<T, Descriptor> & lattice) {
102 plb::Dot3D absoluteOffset = lattice.getLocation(); // Location of this block lattice {x, y, z}
103
104 // The portion of the population streamed from node S1 or S2 is determined by the overlap with the reference node
105 double gfrac = std::fmod((*this->LEcurrentDisplacement), 1.0);
106 plint globalX, globalY;
107 plint globalZ0 = absoluteOffset.z + domain.z0;
108 plint globalZ1 = absoluteOffset.z + domain.z1;
109 plb::Cell<T, Descriptor> curCell;
110 plb::Cell<T, Descriptor> s1Cell;
111 plb::Cell<T, Descriptor> s2Cell;
112
113 // Variables for setting the macroscopic velocity
114 T rhoBar;
115 plb::Array<T, Descriptor<T>::d> j;
116
117 if (globalZ1 == this->nz - 1) // Top
118 {
119 for (plint iX = domain.x0; iX <= domain.x1; ++iX) {
120 globalX = absoluteOffset.x + iX;
121 for (plint iY = domain.y0; iY <= domain.y1; ++iY) {
122 globalY = absoluteOffset.y + iY;
123 curCell = lattice.get(iX, iY, domain.z1);
124
125 // Set macroscopic velocity
126 curCell.getDynamics().computeRhoBarJ(curCell, rhoBar, j);
127 curCell.getDynamics().collideExternal(curCell, rhoBar, plb::Array<T, 3>(this->topVelocity, 0.0, 0.0), T(),
128 lattice.getInternalStatistics());
129 (*this->topPopulations)[globalX][globalY] = populationsArrayToVector(curCell.getRawPopulations());
130
131 // Determine reference nodes coordinates according to the current displacement
132 plint s1 = domain.x0 + (plint)modNonNegative(std::ceil((*this->LEcurrentDisplacement) + globalX), this->nx);
133 plint s2 = domain.x0 + (plint)modNonNegative(std::floor((*this->LEcurrentDisplacement) + globalX), this->nx);
134
135 // Get the nodes
136 s1Cell = lattice.get(s1, iY, domain.z1);
137 s2Cell = lattice.get(s2, iY, domain.z1);
138
139 // Write resulting LE stream to data structure
140 (*this->topPopulations)[globalX][globalY][3] = gfrac * s1Cell[3] + (1 - gfrac) * s2Cell[3];
141 (*this->topPopulations)[globalX][globalY][6] = gfrac * s1Cell[16] + (1 - gfrac) * s2Cell[16];
142 (*this->topPopulations)[globalX][globalY][8] = gfrac * s1Cell[8] + (1 - gfrac) * s2Cell[8];
143 (*this->topPopulations)[globalX][globalY][16] = gfrac * s1Cell[6] + (1 - gfrac) * s2Cell[6];
144 (*this->topPopulations)[globalX][globalY][18] = gfrac * s1Cell[18] + (1 - gfrac) * s2Cell[18];
145 }
146 }
147 }
148 if (globalZ0 == 0) // Bottom
149 {
150 for (plint iX = domain.x0; iX <= domain.x1; ++iX) {
151 globalX = absoluteOffset.x + iX;
152 for (plint iY = domain.y0; iY <= domain.y1; ++iY) {
153 globalY = absoluteOffset.y + iY;
154 curCell = lattice.get(iX, iY, domain.z0);
155
156 // Set macroscopic velocity
157 curCell.getDynamics().computeRhoBarJ(curCell, rhoBar, j);
158 curCell.getDynamics().collideExternal(curCell, rhoBar, plb::Array<T, 3>(this->bottomVelocity, 0.0, 0.0), T(),
159 lattice.getInternalStatistics());
160 (*this->bottomPopulations)[globalX][globalY] = populationsArrayToVector(curCell.getRawPopulations());
161
162 // Determine reference nodes coordinates according to the current displacement
163 plint s1 = domain.x0 + (plint)modNonNegative(std::floor(-(*this->LEcurrentDisplacement) + globalX), this->nx);
164 plint s2 = domain.x0 + (plint)modNonNegative(std::ceil(-(*this->LEcurrentDisplacement) + globalX), this->nx);
165
166 // Get the nodes
167 s1Cell = lattice.get(s1, iY, domain.z0);
168 s2Cell = lattice.get(s2, iY, domain.z0);
169
170 // Write resulting LE stream to data structure
171 (*this->bottomPopulations)[globalX][globalY][7] = gfrac * s1Cell[15] + (1 - gfrac) * s2Cell[15];
172 (*this->bottomPopulations)[globalX][globalY][9] = gfrac * s1Cell[9] + (1 - gfrac) * s2Cell[9];
173 (*this->bottomPopulations)[globalX][globalY][12] = gfrac * s1Cell[12] + (1 - gfrac) * s2Cell[12];
174 (*this->bottomPopulations)[globalX][globalY][15] = gfrac * s1Cell[7] + (1 - gfrac) * s2Cell[7];
175 (*this->bottomPopulations)[globalX][globalY][17] = gfrac * s1Cell[17] + (1 - gfrac) * s2Cell[17];
176 }
177 }
178 }
179 // cout << global::mpi().getRank() << " " << bottomPopulations[5][5][10] << endl;
180 }
181
182 // Clone class to individual lattice blocks
186
187 // Return the type of data that is modified
188 virtual void getTypeOfModification(std::vector<plb::modif::ModifT> & modified) const {
189 modified[0] = plb::modif::nothing;
190 }
191
192 // Only need the bulk of the domain
193 virtual plb::BlockDomain::DomainT appliesTo() const { return plb::BlockDomain::bulk; }
194};
195
196/* Write the streaming of the populations according to the Lees-Edwards algorithm
197 Inherits from data processing functional which handles the subdivision of the code
198 onto individual lattice blocks
199*/
200template <typename T, template <typename U> class Descriptor>
201class LeesEdwardsBCSetPopulations : public plb::BoxProcessingFunctional3D_L<T, Descriptor> {
202public:
203 plint nz; // dimension in z direction
204 std::vector<std::vector<std::vector<T>>> * topPopulations; // Data structure containing the top populations
205 std::vector<std::vector<std::vector<T>>> * bottomPopulations; // Data structure containing the bottom populations
206
207 LeesEdwardsBCSetPopulations(plint nz, std::vector<std::vector<std::vector<T>>> * topPopulations,
208 std::vector<std::vector<std::vector<T>>> * bottomPopulations) {
209 this->nz = nz;
210 this->topPopulations = topPopulations;
211 this->bottomPopulations = bottomPopulations;
212 }
213
214 /* Convert vector to Palabos array
215
216 * @param populations vector containing lattice node populations
217 * @return Palabos array containing lattice node populations
218 */
219 plb::Array<T, Descriptor<T>::q> populationsVectorToArray(std::vector<T> populations) {
220 plb::Array<T, Descriptor<T>::q> pop;
221 for (plint i = 0; i < DESCRIPTOR<T>::q; i++) { pop[i] = populations[i]; }
222 return pop;
223 }
224
225 /* Process function containg the code to be executed onto the individual lattice block.
226 Write new populations to lattice.
227
228 * @param domain Domain on which to execute code
229 * @param lattice individual block lattice
230 */
231 virtual void process(plb::Box3D domain, plb::BlockLattice3D<T, Descriptor> & lattice) {
232 plb::Dot3D absoluteOffset = lattice.getLocation(); // Location of this block lattice {x, y, z}
233
234 plint globalZ0 = absoluteOffset.z + domain.z0;
235 plint globalZ1 = absoluteOffset.z + domain.z1;
236 plint globalX, globalY;
237 plb::Array<T, Descriptor<T>::q> populations;
238
239 if (globalZ1 == this->nz - 1) { // Top
240 for (plint iX = domain.x0; iX <= domain.x1; ++iX) {
241 globalX = absoluteOffset.x + iX;
242 for (plint iY = domain.y0; iY <= domain.y1; ++iY) {
243 globalY = absoluteOffset.y + iY;
244 populations = populationsVectorToArray((*this->topPopulations)[globalX][globalY]);
245 lattice.get(iX, iY, domain.z1).setPopulations(populations);
246 }
247 }
248 }
249 if (globalZ0 == 0) { // Bottom
250 for (plint iX = domain.x0; iX <= domain.x1; ++iX) {
251 globalX = absoluteOffset.x + iX;
252 for (plint iY = domain.y0; iY <= domain.y1; ++iY) {
253 globalY = absoluteOffset.y + iY;
254 populations = populationsVectorToArray((*this->bottomPopulations)[globalX][globalY]);
255 lattice.get(iX, iY, domain.z0).setPopulations(populations);
256 }
257 }
258 }
259 }
260
261 // Clone class to individual lattice blocks
265
266 // Return the type of data that is modified
267 virtual void getTypeOfModification(std::vector<plb::modif::ModifT> & modified) const {
268 modified[0] = plb::modif::staticVariables;
269 }
270
271 // Only need the bulk of the domain
272 virtual plb::BlockDomain::DomainT appliesTo() const { return plb::BlockDomain::bulk; }
273};
274
275
276/* Lees-Edwards boundary conditions base class responsible for the setup and initialization of the data processors
277 */
278template <typename T, template <class U> class Descriptor>
280public:
281 plb::MultiBlockLattice3D<T, Descriptor> & lattice;
282 plint nx; // Dimension in x direction
283 plint ny; // Dimension in y direction
284 plint nz; // Dimension in z direction
285 // int direction; // TODO direction of the LE boundary x = 0, y = 1, z = 2
286 double LEdisplacement; // Displacement per timestep
287 double LEcurrentDisplacement = 0; // Current total displacement
288 T dt; // Timestep size
289 T topVelocity; // Macroscopic velocity top boundary layer
290 T bottomVelocity; // Macroscopic velocity bottom boundary layer
291 plint dataProcessorLevel; // level of operation of the Lees-Edwards data processors
292
293 std::vector<std::vector<std::vector<T>>> topPopulations; // Data structure containing the top populations
294 std::vector<std::vector<std::vector<T>>> bottomPopulations; // Data structure containing the bottom populations
295
296 LeesEdwardsBC(plb::MultiBlockLattice3D<T, Descriptor> & lattice, T shearRate, T dt,
297 double ** hemoCellLEcurrentDisplacement, plint dataProcessorLevel = 1)
298 : lattice(lattice) {
299 this->nx = lattice.getNx();
300 this->ny = lattice.getNy();
301 this->nz = lattice.getNz();
302 this->dt = dt;
303 this->LEdisplacement = shearRate * dt;
304 T vHalf = (nz - 1) * shearRate * 0.5;
305 this->topVelocity = -vHalf;
306 this->bottomVelocity = vHalf;
307 this->dataProcessorLevel = dataProcessorLevel;
308 *hemoCellLEcurrentDisplacement =
309 &LEcurrentDisplacement; // Let the hemocell LE displacement point to this memory address
310 }
311 // The data processors and HemoCell point into this object.
312 LeesEdwardsBC(const LeesEdwardsBC &) = delete;
314
315 void initialize() {
316 // Uses the default periodicity because otherwise a form of bounceback boundary will be initialized per default
317 this->lattice.periodicity().toggleAll(true);
318
319 this->topPopulations.resize(this->nx, std::vector<std::vector<T>>(this->ny, std::vector<T>(Descriptor<T>::q)));
320 this->bottomPopulations.resize(this->nx, std::vector<std::vector<T>>(this->ny, std::vector<T>(Descriptor<T>::q)));
321
322 // set data processors
323 integrateProcessingFunctional(
324 new LeesEdwardsBCGetPopulations<T, DESCRIPTOR>(this->nx, this->nz, this->topVelocity, this->bottomVelocity,
326 this->lattice.getBoundingBox(), this->lattice, this->dataProcessorLevel);
327
328 integrateProcessingFunctional(
330 this->lattice.getBoundingBox(), this->lattice, this->dataProcessorLevel + 1);
331 }
332
333 /* Update the current Lees-Edwards displacement
334
335 * @param iter current hemocell iteration counter
336 */
337 void updateLECurDisplacement(unsigned int iter) {
338 LEcurrentDisplacement = std::fmod(this->LEdisplacement * iter, (double)this->nx);
339 }
340};
341
342
343}; // namespace hemo
344
345#endif
Definition leesEdwardsBC.h:57
T topVelocity
Definition leesEdwardsBC.h:64
LeesEdwardsBCGetPopulations(plint nx, plint nz, T topVelocity, T bottomVelocity, double *LEcurrentDisplacement, std::vector< std::vector< std::vector< T > > > *topPopulations, std::vector< std::vector< std::vector< T > > > *bottomPopulations)
Definition leesEdwardsBC.h:67
plint nz
Definition leesEdwardsBC.h:60
double * LEcurrentDisplacement
Definition leesEdwardsBC.h:61
plint nx
Definition leesEdwardsBC.h:59
std::vector< std::vector< std::vector< T > > > * topPopulations
Definition leesEdwardsBC.h:62
plint modNonNegative(plint a, plint b)
Definition leesEdwardsBC.h:92
std::vector< std::vector< std::vector< T > > > * bottomPopulations
Definition leesEdwardsBC.h:63
virtual void getTypeOfModification(std::vector< plb::modif::ModifT > &modified) const
Definition leesEdwardsBC.h:188
virtual plb::BlockDomain::DomainT appliesTo() const
Definition leesEdwardsBC.h:193
virtual LeesEdwardsBCGetPopulations< T, Descriptor > * clone() const
Definition leesEdwardsBC.h:183
virtual void process(plb::Box3D domain, plb::BlockLattice3D< T, Descriptor > &lattice)
Definition leesEdwardsBC.h:101
std::vector< T > populationsArrayToVector(plb::Array< T, Descriptor< T >::q > populations)
Definition leesEdwardsBC.h:84
T bottomVelocity
Definition leesEdwardsBC.h:65
Definition leesEdwardsBC.h:201
plint nz
Definition leesEdwardsBC.h:203
LeesEdwardsBCSetPopulations(plint nz, std::vector< std::vector< std::vector< T > > > *topPopulations, std::vector< std::vector< std::vector< T > > > *bottomPopulations)
Definition leesEdwardsBC.h:207
std::vector< std::vector< std::vector< T > > > * topPopulations
Definition leesEdwardsBC.h:204
plb::Array< T, Descriptor< T >::q > populationsVectorToArray(std::vector< T > populations)
Definition leesEdwardsBC.h:219
virtual LeesEdwardsBCSetPopulations< T, Descriptor > * clone() const
Definition leesEdwardsBC.h:262
virtual plb::BlockDomain::DomainT appliesTo() const
Definition leesEdwardsBC.h:272
std::vector< std::vector< std::vector< T > > > * bottomPopulations
Definition leesEdwardsBC.h:205
virtual void getTypeOfModification(std::vector< plb::modif::ModifT > &modified) const
Definition leesEdwardsBC.h:267
virtual void process(plb::Box3D domain, plb::BlockLattice3D< T, Descriptor > &lattice)
Definition leesEdwardsBC.h:231
Definition leesEdwardsBC.h:279
LeesEdwardsBC(const LeesEdwardsBC &)=delete
T dt
Definition leesEdwardsBC.h:288
double LEdisplacement
Definition leesEdwardsBC.h:286
LeesEdwardsBC & operator=(const LeesEdwardsBC &)=delete
T topVelocity
Definition leesEdwardsBC.h:289
plb::MultiBlockLattice3D< T, Descriptor > & lattice
Definition leesEdwardsBC.h:281
std::vector< std::vector< std::vector< T > > > topPopulations
Definition leesEdwardsBC.h:293
plint ny
Definition leesEdwardsBC.h:283
void updateLECurDisplacement(unsigned int iter)
Definition leesEdwardsBC.h:337
T bottomVelocity
Definition leesEdwardsBC.h:290
plint nx
Definition leesEdwardsBC.h:282
LeesEdwardsBC(plb::MultiBlockLattice3D< T, Descriptor > &lattice, T shearRate, T dt, double **hemoCellLEcurrentDisplacement, plint dataProcessorLevel=1)
Definition leesEdwardsBC.h:296
double LEcurrentDisplacement
Definition leesEdwardsBC.h:287
std::vector< std::vector< std::vector< T > > > bottomPopulations
Definition leesEdwardsBC.h:294
plint nz
Definition leesEdwardsBC.h:284
void initialize()
Definition leesEdwardsBC.h:315
plint dataProcessorLevel
Definition leesEdwardsBC.h:291
double T
Definition constant_defaults.h:137
long int plint
Definition constant_defaults.h:149
Definition config.cpp:37