OpenLB 1.8.1
Loading...
Searching...
No Matches
utilities.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2022-2023 Nicolas Hafen, Jan E. Marquardt, Mathias J. Krause
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24
25#ifndef PARTICLE_COMMUNICATION_UTILITIES_H
26#define PARTICLE_COMMUNICATION_UTILITIES_H
27
29
30namespace olb {
31
33template<typename T, unsigned D>
35 CuboidDecomposition<T,D>& cuboidDecomposition,
36 std::map<int,std::vector<int>>& neighbourhood,
37 int offset )
38{
39 for (int iC=0; iC<cuboidDecomposition.size(); ++iC){
40 neighbourhood[iC].clear();
41 auto neighbours = cuboidDecomposition.getNeighborhood(iC, offset);
42 for (int jC : neighbours) {
43 neighbourhood[iC].emplace_back(jC);
44 }
45 }
46}
47
51 std::map<int,std::vector<int>>& neighbourhood,
52 bool correct = false,
53 bool verbose = false)
54{
55 bool consistent = true;
56 for ( auto cuboidNeighbourPair : neighbourhood){
57 //Retrieve iC and neighbours
58 int iC = cuboidNeighbourPair.first;
59 std::vector<int>& neighbours = cuboidNeighbourPair.second;
60 //Loop over neighbours
61 for (int iCN : neighbours){
62 //Retreive neighbour's neighbours
63 std::vector<int>& neighboursNeighbours = neighbourhood[iCN];
64 bool iCfound = false;
65 //Loop over neighbour's neighbours
66 for (int iCNN : neighboursNeighbours ){
67 if (iCNN == iC){
68 iCfound=true;
69 break;
70 }
71 }
72 if (!iCfound){
73 //Set consistency boolean to false
74 consistent = false;
75 //Output, if desired
76 if (verbose){
77 std::cout << "iC " << iC << " not found in list of neighbour iC "
78 << iCN << std::endl;
79 }
80 //Correct, if desired
81 if (correct){
82 neighbourhood[iCN].push_back(iC);
83 }
84 }
85 }
86 }
87 //Return whether consistent
88 return consistent;
89}
90
91
92
93
94namespace particles {
95
96namespace communication {
97
98
100template<typename T, unsigned D, bool verbose=false>
101std::vector<int> getNeighborCuboids(
102 CuboidDecomposition<T,D>& cuboidDecomposition, unsigned offset, int globiC )
103{
104 auto neighbors = cuboidDecomposition.getNeighborhood(globiC, offset);
105
106 //Print, if desired
107 if constexpr(verbose){
108 std::cout << globiC << ": " << neighbors << std::endl;
109 }
110
111 std::vector<int> ns;
112 for (int iC : neighbors) {
113 ns.emplace_back(iC);
114 }
115 return ns;
116}
117
119template<typename T, unsigned D, bool verbose=false>
121 SuperStructure<T,D>& superStructure, int rank, std::map<int,std::vector<int>> neighbourhood )
122{
123 std::set<int> rankNeighbours;
124
125#ifdef PARALLEL_MODE_MPI
126 auto& loadBalancer = superStructure.getLoadBalancer();
127
128 for ( auto cuboidNeighbourPair : neighbourhood){
129 int globiC = cuboidNeighbourPair.first;
130 if (loadBalancer.rank(globiC) == rank) {
131 std::vector<int>& neighbours = cuboidNeighbourPair.second;
132 for (int globiCN : neighbours ){
133 rankNeighbours.insert( loadBalancer.rank(globiCN));
134 }
135 }
136 }
137
138 if constexpr(verbose){
139 std::cout << rank << ": " << rankNeighbours << std::endl;
140 }
141#endif
142
143 return rankNeighbours;
144}
145
146
148template<typename T, unsigned D, bool verbose=false>
150 SuperStructure<T,D>& superStructure, int rank, const std::vector<std::set<int>>& neighbourhood )
151{
152 std::map<int,std::vector<int>> neighbourhoodMap;
153
154 for(unsigned i=0; i<neighbourhood.size(); ++i) {
155 neighbourhoodMap[i] = std::vector<int>();
156 for(int iC : neighbourhood[i]) {
157 neighbourhoodMap[i].push_back(iC);
158 }
159 }
160
162 superStructure, rank, neighbourhoodMap );
163}
164
166template<typename T, unsigned D, bool verbose=false>
168 SuperStructure<T,D>& superStructure, const std::vector<std::set<int>>& neighbourhood )
169{
170 const int rank = singleton::mpi().getRank();
172 superStructure, rank, neighbourhood);
173}
174
175
177template<typename T, unsigned D, bool verbose=false>
178std::set<int> getNeighbourRanks(
179 SuperStructure<T,D>& superStructure, unsigned offset, int rank )
180{
181 std::set<int> rankNeighbours;
182
183#ifdef PARALLEL_MODE_MPI
184 auto& loadBalancer = superStructure.getLoadBalancer();
185 auto& cuboidDecomposition = superStructure.getCuboidDecomposition();
186
188 // 1. sample complete neighbourhood
189 std::map<int,std::vector<int>> neighbourhood;
190 evaluateCuboidDecompositionNeighbourhood(cuboidDecomposition, neighbourhood, offset);
191
192 //2. check consistency
193 constexpr bool correctFaultyNeighbourhood = true;
194 checkCuboidNeighbourhoodConsistency( neighbourhood, correctFaultyNeighbourhood );
195
196 //3. evaluate relevant ranks
198 superStructure, rank, neighbourhood );
199#endif
200
201 return rankNeighbours;
202}
203
205template<typename T, unsigned D, bool verbose=false>
206std::set<int> getNeighbourRanks(
207 SuperStructure<T,D>& superStructure, unsigned offset)
208{
209 //Retrive parallization infos
210 const int rank = singleton::mpi().getRank();
211 return getNeighbourRanks<T,D,false>(superStructure, offset, rank);
212}
213
214
215template<typename T, typename PARTICLETYPE>
216std::size_t serializeIcDest( int globiCdest,
217 std::uint8_t* bufferiCandParticle )
218{
219 //Define iC field as FieldArrayD with single entry
221 fieldiC.setField(0, globiCdest);
222 const std::vector<unsigned int> index{0};
223 auto communicatable = ConcreteCommunicatable(fieldiC);
224 //Serialize iC
225 communicatable.serialize(index, bufferiCandParticle);
226 //Return communicatable size
227 return communicatable.size(index);
228}
229
230template<typename T, typename PARTICLETYPE>
231void serializeIcDestAndParticle( int globiCdest,
232 Particle<T,PARTICLETYPE>& particle,
233 std::uint8_t* bufferiCandParticle )
234{
235 //Serialize iC and retrieve particle buffer
236 std::size_t serialSizeField =
237 serializeIcDest<T,PARTICLETYPE>( globiCdest, bufferiCandParticle );
238 std::uint8_t* bufferRawParticle = &bufferiCandParticle[serialSizeField];
239 //Serialize particle
240 particle.serialize(bufferRawParticle);
241}
242
243template<typename T, typename PARTICLETYPE>
244std::size_t deserializeIcDest( int& globiCdest,
245 std::uint8_t* bufferRaw )
246{
247 //Define iC field as FieldArrayD with single entry
249 const std::vector<unsigned int> index{0};
250 auto communicatable = ConcreteCommunicatable(fieldiC);
251 //Deserialice iC
252 communicatable.deserialize(index, bufferRaw);
253 globiCdest = fieldiC.getField(0);
254 //Return communicatable size
255 return communicatable.size(index);
256}
257
258
259template<typename T, typename PARTICLETYPE>
260void deserializeIcDestAndParticle( int& globiCdest,
261 Particle<T,PARTICLETYPE>& particle,
262 std::uint8_t* bufferiCandParticle )
263{
264 //Deserialize iC and retrieve particle buffer
265 std::size_t serialSizeField =
266 deserializeIcDest<T,PARTICLETYPE>(globiCdest,bufferiCandParticle);
267 std::uint8_t* bufferRawParticle = &bufferiCandParticle[serialSizeField];
268 //Deserialize particle
269 particle.deserialize(bufferRawParticle);
270}
271
272
273/*
274 * The function provides a check if the cuboids fit the constraints from the
275 * implementation of the particle parallization
276 */
277template<typename T, typename PARTICLETYPE>
279{
280 constexpr unsigned D = PARTICLETYPE::d;
281 auto& cuboidDecomposition = sParticleSystem.getSuperStructure().getCuboidDecomposition();
282
283 const T physDeltaX = cuboidDecomposition.getMotherCuboid().getDeltaR();
284
285 T maxCircumRadius{0};
287 sParticleSystem,
288 [&](Particle<T, PARTICLETYPE>& particle,
289 ParticleSystem<T, PARTICLETYPE>& particleSystem, int globiC) {
290 maxCircumRadius = util::max(maxCircumRadius,
291 particles::contact::evalCircumRadius(particle, physDeltaX));
292 });
293#ifdef PARALLEL_MODE_MPI
294 singleton::mpi().reduceAndBcast(maxCircumRadius, MPI_MAX,
295 singleton::mpi().bossId());
296#endif
297
298 int minCuboidExtent{std::numeric_limits<int>::max()};
299 for (int iC=0; iC<cuboidDecomposition.size(); ++iC){
300 for(unsigned iD=0; iD<D; ++iD){
301 minCuboidExtent = util::min(minCuboidExtent,
302 cuboidDecomposition.get(iC).getExtent()[iD]);
303 }
304 }
305#ifdef PARALLEL_MODE_MPI
306 singleton::mpi().reduceAndBcast(minCuboidExtent, MPI_MIN,
307 singleton::mpi().bossId());
308#endif
309
310 // It's enough that one rank checks for an error
311 if (singleton::mpi().getRank() == 0) {
312 OLB_ASSERT(minCuboidExtent*physDeltaX > maxCircumRadius,
313 "At least one cuboid too small so that the currently implemented "
314 "particle parallelization strategy does not work.");
315 }
316}
317
318template<typename T>
319T movePositionToStart(const T position, const T max, const T min)
320{
321 return min + (position - max);
322}
323
324template<typename T>
325T movePositionToEnd(const T position, const T max, const T min)
326{
327 return max - (min - position);
328}
329
330template<typename T>
331T applyPeriodicityToPosition(const bool isPeriodic, T position, const T max, const T min)
332{
333 if(isPeriodic) {
334 if(position < min) {
335 position = movePositionToEnd(position, max, min);
336 }
337 else if(position > max) {
338 position = movePositionToStart(position, max, min);
339 }
340 }
341 return position;
342}
343
344// The following functions use an offset of 0.5 * physDeltaX, because there is
345// a distance of 1 * physDeltaX between the maximal coordinate and minimal coordinate
346
348template<typename T, unsigned D>
350{
351 const T physDeltaX = cuboidDecomposition.getMotherCuboid().getDeltaR();
352 return (cuboidDecomposition.getMotherCuboid().getOrigin() - 0.5 * physDeltaX);
353
354}
355
357template<typename T, unsigned D>
359 const PhysR<T,D>& min)
360{
361 const T physDeltaX = cuboidDecomposition.getMotherCuboid().getDeltaR();
362 return min + physDeltaX * cuboidDecomposition.getMotherCuboid().getExtent();
363}
364
366template<typename T, unsigned D>
368 const CuboidDecomposition<T,D>& cuboidDecomposition,
369 const Vector<bool,D>& periodicity,
370 PhysR<T,D> position)
371{
372 const PhysR<T,D> min = getCuboidMin<T,D>(cuboidDecomposition);
373 const PhysR<T,D> max = getCuboidMax<T,D>(cuboidDecomposition, min);
374
375 for(unsigned iD=0; iD<D; ++iD) {
376 position[iD] = applyPeriodicityToPosition(periodicity[iD],
377 position[iD], max[iD], min[iD]);
378 }
379
380 return position;
381}
382
384template<typename T, unsigned D>
385bool getCuboid(const CuboidDecomposition<T,D>& cuboidDecomposition,
386 const Vector<bool,D>& periodicity,
387 const PhysR<T,D>& position, int& iC)
388{
389 PhysR<T,D> newPosition(
390 applyPeriodicityToPosition(cuboidDecomposition, periodicity, position));
391 if (auto iC_ = cuboidDecomposition.getC(newPosition)) {
392 iC = *iC_;
393 return true;
394 }
395 return false;
396}
397
398} //namespace communication
399
400} //namespace particles
401
402} //namespace olb
403
404
405#endif
Decomposition of a physical volume into a set of disjoint cuboids.
int size() const
Returns number of cuboids in decomposition.
std::set< int > getNeighborhood(int iCglob, int overlap=0) const
Returns set of neighbors to cuboid iCglob within overlap.
std::optional< int > getC(Vector< T, D > physR, int padding=0) const
Returns ID of cuboid containing physR within padding.
const Cuboid< T, D > & getMotherCuboid() const
Returns the smallest cuboid that includes all cuboids of the structure.
SoA storage for instances of a single FIELD.
auto getField(std::size_t iCell) const
Return copy of FIELD data for cell iCell.
void setField(std::size_t iCell, const FieldD< T, DESCRIPTOR, FIELD > &v)
Set FIELD data at cell iCell.
LoadBalancer< T > & getLoadBalancer()
Read and write access to the load balancer.
CuboidDecomposition< T, D > & getCuboidDecomposition()
Read and write access to cuboid geometry.
Plain old scalar vector.
std::size_t deserialize(const std::uint8_t *buffer)
Deserialize data.
Definition particle.h:146
std::size_t serialize(std::uint8_t *buffer) const
Serialize data.
Definition particle.h:141
SuperStructure< T, PARTICLETYPE::d > & getSuperStructure()
void reduceAndBcast(T &reductVal, MPI_Op op, int root=0, MPI_Comm comm=MPI_COMM_WORLD)
Reduction operation, followed by a broadcast.
int getRank() const
Returns the process ID.
std::set< int > getNeighbourRanks(SuperStructure< T, D > &superStructure, unsigned offset, int rank)
Get neighbour ranks.
Definition utilities.h:178
T movePositionToEnd(const T position, const T max, const T min)
Definition utilities.h:325
std::size_t serializeIcDest(int globiCdest, std::uint8_t *bufferiCandParticle)
Definition utilities.h:216
bool getCuboid(const CuboidDecomposition< T, D > &cuboidDecomposition, const Vector< bool, D > &periodicity, const PhysR< T, D > &position, int &iC)
Function returns true if cuboid was found and gives iC.
Definition utilities.h:385
std::vector< int > getNeighborCuboids(CuboidDecomposition< T, D > &cuboidDecomposition, unsigned offset, int globiC)
Get neighbour cuboids.
Definition utilities.h:101
std::size_t deserializeIcDest(int &globiCdest, std::uint8_t *bufferRaw)
Definition utilities.h:244
void checkCuboidSizes(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem)
Definition utilities.h:278
Vector< T, D > getCuboidMin(const CuboidDecomposition< T, D > &cuboidDecomposition)
Returns minimal coordinate of domain for periodic particle boundaries.
Definition utilities.h:349
std::set< int > getNeighbourRanksFromCuboidNeighbourhood(SuperStructure< T, D > &superStructure, int rank, std::map< int, std::vector< int > > neighbourhood)
Get neighbour ranks.
Definition utilities.h:120
Vector< T, D > getCuboidMax(const CuboidDecomposition< T, D > &cuboidDecomposition, const PhysR< T, D > &min)
Returns maximal coordinate of domain for periodic particle boundaries.
Definition utilities.h:358
void forParticlesInSuperParticleSystem(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem, F f)
T movePositionToStart(const T position, const T max, const T min)
Definition utilities.h:319
void deserializeIcDestAndParticle(int &globiCdest, Particle< T, PARTICLETYPE > &particle, std::uint8_t *bufferiCandParticle)
Definition utilities.h:260
T applyPeriodicityToPosition(const bool isPeriodic, T position, const T max, const T min)
Definition utilities.h:331
void serializeIcDestAndParticle(int globiCdest, Particle< T, PARTICLETYPE > &particle, std::uint8_t *bufferiCandParticle)
Definition utilities.h:231
T evalCircumRadius(T const contactDetectionDistance, T const circumRadius, T const epsilon)
constexpr bool isPeriodic(const Vector< bool, D > &periodic)
MpiManager & mpi()
Expr min(Expr a, Expr b)
Definition expr.cpp:249
Expr max(Expr a, Expr b)
Definition expr.cpp:245
Top level namespace for all of OpenLB.
constexpr T max(const ScalarVector< T, D, IMPL > &v)
Definition vector.h:466
void evaluateCuboidDecompositionNeighbourhood(CuboidDecomposition< T, D > &cuboidDecomposition, std::map< int, std::vector< int > > &neighbourhood, int offset)
Evaluate complete neighbourhood and store in std::map.
Definition utilities.h:34
bool checkCuboidNeighbourhoodConsistency(std::map< int, std::vector< int > > &neighbourhood, bool correct=false, bool verbose=false)
Consistency check for neighbour retrieval.
Definition utilities.h:50
#define OLB_ASSERT(COND, MESSAGE)
Definition olbDebug.h:45