25#ifndef PARTICLE_COMMUNICATION_UTILITIES_H
26#define PARTICLE_COMMUNICATION_UTILITIES_H
33template<
typename T,
unsigned D>
36 std::map<
int,std::vector<int>>& neighbourhood,
39 for (
int iC=0; iC<cuboidDecomposition.
size(); ++iC){
40 neighbourhood[iC].clear();
42 for (
int jC : neighbours) {
43 neighbourhood[iC].emplace_back(jC);
51 std::map<
int,std::vector<int>>& neighbourhood,
55 bool consistent =
true;
56 for (
auto cuboidNeighbourPair : neighbourhood){
58 int iC = cuboidNeighbourPair.first;
59 std::vector<int>& neighbours = cuboidNeighbourPair.second;
61 for (
int iCN : neighbours){
63 std::vector<int>& neighboursNeighbours = neighbourhood[iCN];
66 for (
int iCNN : neighboursNeighbours ){
77 std::cout <<
"iC " << iC <<
" not found in list of neighbour iC "
82 neighbourhood[iCN].push_back(iC);
96namespace communication {
100template<
typename T,
unsigned D,
bool verbose=false>
107 if constexpr(verbose){
108 std::cout << globiC <<
": " << neighbors << std::endl;
112 for (
int iC : neighbors) {
119template<
typename T,
unsigned D,
bool verbose=false>
121 SuperStructure<T,D>& superStructure,
int rank, std::map<
int,std::vector<int>> neighbourhood )
123 std::set<int> rankNeighbours;
125#ifdef PARALLEL_MODE_MPI
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));
138 if constexpr(verbose){
139 std::cout << rank <<
": " << rankNeighbours << std::endl;
143 return rankNeighbours;
148template<
typename T,
unsigned D,
bool verbose=false>
150 SuperStructure<T,D>& superStructure,
int rank,
const std::vector<std::set<int>>& neighbourhood )
152 std::map<int,std::vector<int>> neighbourhoodMap;
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);
162 superStructure, rank, neighbourhoodMap );
166template<
typename T,
unsigned D,
bool verbose=false>
172 superStructure, rank, neighbourhood);
177template<
typename T,
unsigned D,
bool verbose=false>
181 std::set<int> rankNeighbours;
183#ifdef PARALLEL_MODE_MPI
189 std::map<int,std::vector<int>> neighbourhood;
193 constexpr bool correctFaultyNeighbourhood =
true;
198 superStructure, rank, neighbourhood );
201 return rankNeighbours;
205template<
typename T,
unsigned D,
bool verbose=false>
215template<
typename T,
typename PARTICLETYPE>
217 std::uint8_t* bufferiCandParticle )
222 const std::vector<unsigned int> index{0};
225 communicatable.serialize(index, bufferiCandParticle);
227 return communicatable.size(index);
230template<
typename T,
typename PARTICLETYPE>
233 std::uint8_t* bufferiCandParticle )
236 std::size_t serialSizeField =
238 std::uint8_t* bufferRawParticle = &bufferiCandParticle[serialSizeField];
243template<
typename T,
typename PARTICLETYPE>
245 std::uint8_t* bufferRaw )
249 const std::vector<unsigned int> index{0};
252 communicatable.deserialize(index, bufferRaw);
255 return communicatable.size(index);
259template<
typename T,
typename PARTICLETYPE>
262 std::uint8_t* bufferiCandParticle )
265 std::size_t serialSizeField =
267 std::uint8_t* bufferRawParticle = &bufferiCandParticle[serialSizeField];
277template<
typename T,
typename PARTICLETYPE>
280 constexpr unsigned D = PARTICLETYPE::d;
283 const T physDeltaX = cuboidDecomposition.getMotherCuboid().getDeltaR();
285 T maxCircumRadius{0};
290 maxCircumRadius =
util::max(maxCircumRadius,
293#ifdef PARALLEL_MODE_MPI
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]);
305#ifdef PARALLEL_MODE_MPI
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.");
321 return min + (position -
max);
327 return max - (min - position);
337 else if(position > max) {
348template<
typename T,
unsigned D>
351 const T physDeltaX = cuboidDecomposition.
getMotherCuboid().getDeltaR();
352 return (cuboidDecomposition.
getMotherCuboid().getOrigin() - 0.5 * physDeltaX);
357template<
typename T,
unsigned D>
361 const T physDeltaX = cuboidDecomposition.
getMotherCuboid().getDeltaR();
362 return min + physDeltaX * cuboidDecomposition.
getMotherCuboid().getExtent();
366template<
typename T,
unsigned D>
375 for(
unsigned iD=0; iD<D; ++iD) {
377 position[iD], max[iD], min[iD]);
384template<
typename T,
unsigned D>
391 if (
auto iC_ = cuboidDecomposition.
getC(newPosition)) {
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.
std::size_t deserialize(const std::uint8_t *buffer)
Deserialize data.
std::size_t serialize(std::uint8_t *buffer) const
Serialize data.
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.
T movePositionToEnd(const T position, const T max, const T min)
std::size_t serializeIcDest(int globiCdest, std::uint8_t *bufferiCandParticle)
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.
std::vector< int > getNeighborCuboids(CuboidDecomposition< T, D > &cuboidDecomposition, unsigned offset, int globiC)
Get neighbour cuboids.
std::size_t deserializeIcDest(int &globiCdest, std::uint8_t *bufferRaw)
void checkCuboidSizes(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem)
Vector< T, D > getCuboidMin(const CuboidDecomposition< T, D > &cuboidDecomposition)
Returns minimal coordinate of domain for periodic particle boundaries.
std::set< int > getNeighbourRanksFromCuboidNeighbourhood(SuperStructure< T, D > &superStructure, int rank, std::map< int, std::vector< int > > neighbourhood)
Get neighbour ranks.
Vector< T, D > getCuboidMax(const CuboidDecomposition< T, D > &cuboidDecomposition, const PhysR< T, D > &min)
Returns maximal coordinate of domain for periodic particle boundaries.
void forParticlesInSuperParticleSystem(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem, F f)
T movePositionToStart(const T position, const T max, const T min)
void deserializeIcDestAndParticle(int &globiCdest, Particle< T, PARTICLETYPE > &particle, std::uint8_t *bufferiCandParticle)
T applyPeriodicityToPosition(const bool isPeriodic, T position, const T max, const T min)
void serializeIcDestAndParticle(int globiCdest, Particle< T, PARTICLETYPE > &particle, std::uint8_t *bufferiCandParticle)
T evalCircumRadius(T const contactDetectionDistance, T const circumRadius, T const epsilon)
constexpr bool isPeriodic(const Vector< bool, D > &periodic)
Top level namespace for all of OpenLB.
constexpr T max(const ScalarVector< T, D, IMPL > &v)
void evaluateCuboidDecompositionNeighbourhood(CuboidDecomposition< T, D > &cuboidDecomposition, std::map< int, std::vector< int > > &neighbourhood, int offset)
Evaluate complete neighbourhood and store in std::map.
bool checkCuboidNeighbourhoodConsistency(std::map< int, std::vector< int > > &neighbourhood, bool correct=false, bool verbose=false)
Consistency check for neighbour retrieval.
#define OLB_ASSERT(COND, MESSAGE)