MimmoObject.hpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo is free software: you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License v3 (LGPL)
13  * as published by the Free Software Foundation.
14  *
15  * mimmo is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23 \*---------------------------------------------------------------------------*/
24 #ifndef __MIMMOOBJECT_HPP__
25 #define __MIMMOOBJECT_HPP__
26 
27 #include "mimmoTypeDef.hpp"
28 #include "MimmoSharedPointer.hpp"
29 #include <bitpit_volunstructured.hpp>
30 #include <bitpit_surfunstructured.hpp>
31 #include <bitpit_SA.hpp>
32 #include <surface_skd_tree.hpp>
33 #include <volume_skd_tree.hpp>
34 #if MIMMO_ENABLE_MPI==1
35 # include <mpi.h>
36 #endif
37 
38 namespace mimmo{
39 
45 class MimmoSurfUnstructured: public bitpit::SurfUnstructured{
46 public:
47  // Constructors
48 #if MIMMO_ENABLE_MPI
49  MimmoSurfUnstructured(MPI_Comm communicator);
50  MimmoSurfUnstructured(int patch_dim, MPI_Comm communicator);
51  MimmoSurfUnstructured(int id, int patch_dim, MPI_Comm communicator);
52  MimmoSurfUnstructured(std::istream &stream, MPI_Comm communicator);
53 #else
55  MimmoSurfUnstructured(int patch_dim);
56  MimmoSurfUnstructured(int id, int patch_dim);
57  MimmoSurfUnstructured(std::istream &stream);
58 #endif
59  virtual ~MimmoSurfUnstructured();
60  // Clone
61  std::unique_ptr<bitpit::PatchKernel> clone() const override;
62 
63 protected:
65  MimmoSurfUnstructured(const MimmoSurfUnstructured & other) = default;
66 };
67 
73 class MimmoVolUnstructured: public bitpit::VolUnstructured{
74 public:
75  // Constructors
76 #if MIMMO_ENABLE_MPI
77  MimmoVolUnstructured(MPI_Comm communicator);
78  MimmoVolUnstructured(int dimension, MPI_Comm communicator);
79  MimmoVolUnstructured(int id, int dimension, MPI_Comm communicator);
80 #else
82  MimmoVolUnstructured(int dimension);
83  MimmoVolUnstructured(int id, int dimension);
84 #endif
85  virtual ~MimmoVolUnstructured();
86  // Clone
87  std::unique_ptr<bitpit::PatchKernel> clone() const override;
88 
89 protected:
91  MimmoVolUnstructured(const MimmoVolUnstructured &) = default;
92 };
93 
99 class MimmoPointCloud: public bitpit::SurfUnstructured{
100 public:
101  // Constructors
102 #if MIMMO_ENABLE_MPI
103  MimmoPointCloud(MPI_Comm communicator);
104  MimmoPointCloud(int id, MPI_Comm communicator);
105 #else
106  MimmoPointCloud();
107  MimmoPointCloud(int id);
108 #endif
109  virtual ~MimmoPointCloud();
110  // Clone
111  std::unique_ptr<bitpit::PatchKernel> clone() const override;
112 
113 
114 protected:
116  MimmoPointCloud(const MimmoPointCloud &) = default;
117 };
118 
123 enum class SyncStatus: long {
124  NOTSUPPORTED = -1,
125  NONE = 0,
126  UNSYNC = 1,
127  SYNC = 2
128 };
129 
144 
145 private:
146  std::unique_ptr<bitpit::PatchKernel> m_patch;
147  bitpit::PatchKernel * m_extpatch;
148  bool m_internalPatch;
150  double m_tolerance;
152 protected:
153 //members
154  int m_type;
155  std::unordered_set<long> m_pidsType;
156  std::unordered_map<long, std::string> m_pidsTypeWNames;
157  std::unique_ptr<bitpit::PatchSkdTree> m_skdTree;
158  std::unique_ptr<bitpit::KdTree<3,bitpit::Vertex,long> > m_kdTree;
164  bitpit::Logger* m_log;
167  int m_nprocs;
168  int m_rank;
170 #if MIMMO_ENABLE_MPI
171  MPI_Comm m_communicator;
172  long m_ninteriorvertices = 0;
173  long m_nglobalvertices = 0;
174  std::vector<long> m_rankinteriorvertices;
175  long m_globaloffset;
176  std::unordered_map<long, long> m_pointConsecutiveId;
177  SyncStatus m_pointGhostExchangeInfoSync;
178 #endif
179 
180  bitpit::PatchNumberingInfo m_patchInfo;
185  std::unordered_map<long, std::unordered_set<long> > m_pointConnectivity;
188 public:
189  MimmoObject(int type = 1, bool isParallel = MIMMO_ENABLE_MPI);
190  MimmoObject(int type, dvecarr3E & vertex, livector2D * connectivity = nullptr, bool isParallel = MIMMO_ENABLE_MPI);
191  MimmoObject(int type, bitpit::PatchKernel* geometry);
192  MimmoObject(int type, std::unique_ptr<bitpit::PatchKernel> & geometry);
193  ~MimmoObject();
194 
195  bitpit::Logger& getLog();
196 
197  bool isEmpty();
198  bool isSkdTreeSupported();
199  int getType();
200  long getNVertices();
201  long getNCells();
202  long getNVertices()const;
203  long getNCells()const;
204  long getNInternals()const;
205  long getNInternalVertices();
206 #if MIMMO_ENABLE_MPI
207  long getNGlobalVertices();
208  long getNGlobalCells();
209  long getPointGlobalCountOffset();
210 #endif
211  dvecarr3E getVerticesCoords(lilimap* mapDataInv = nullptr);
212  const darray3E & getVertexCoords(long i) const;
213  bitpit::PiercedVector<bitpit::Vertex> & getVertices();
214  const bitpit::PiercedVector<bitpit::Vertex> & getVertices() const ;
215 
218  livector1D getCellConnectivity(long id) const ;
219  bitpit::PiercedVector<bitpit::Cell> & getCells();
220  const bitpit::PiercedVector<bitpit::Cell> & getCells() const;
221 
222  bitpit::PiercedVector<bitpit::Interface> & getInterfaces();
223  const bitpit::PiercedVector<bitpit::Interface> & getInterfaces() const;
224 
225  livector1D getCellsIds(bool internalsOnly = false);
226  livector1D getVerticesIds(bool internalsOnly = false);
227  bitpit::PatchKernel* getPatch();
228  const bitpit::PatchKernel* getPatch() const;
229  std::unordered_set<long> & getPIDTypeList();
230  std::unordered_map<long, std::string> & getPIDTypeListWNames();
231  const std::unordered_set<long> & getPIDTypeList() const;
232  const std::unordered_map<long, std::string> & getPIDTypeListWNames() const;
234  std::unordered_map<long, long> getPID();
235 
236  bitpit::PatchSkdTree* getSkdTree();
237  bitpit::KdTree<3, bitpit::Vertex, long> * getKdTree();
238  bitpit::PatchNumberingInfo* getPatchInfo();
243 
244  double getTolerance();
245 
246  int getRank() const;
247  int getProcessorCount() const;
248 
249  bool isPointInterior(long id);
250 #if MIMMO_ENABLE_MPI
251  const MPI_Comm & getCommunicator() const;
252  const std::unordered_map<int, std::vector<long>> & getPointGhostExchangeSources() const;
253  const std::unordered_map<int, std::vector<long>> & getPointGhostExchangeTargets() const;
254  SyncStatus getPointGhostExchangeInfoSyncStatus();
255  void updatePointGhostExchangeInfo();
256  void resetPointGhostExchangeInfo();
257  SyncStatus cleanParallelSkdTreeSync();
258  SyncStatus cleanParallelKdTreeSync();
259  SyncStatus cleanParallelAdjacenciesSync();
260  SyncStatus cleanParallelInterfacesSync();
261  SyncStatus cleanParallelPointConnectivitySync();
262  SyncStatus cleanParallelInfoSync();
263  SyncStatus cleanParallelBoundingBoxSync();
264  SyncStatus cleanParallelPointGhostExchangeInfoSync();
265  void cleanAllParallelSync();
266  bool isDistributed();
267  void deleteOrphanGhostCells();
268 #endif
269  bool isParallel();
270 
271  void setTolerance(double tol);
272 
273  long addVertex(const darray3E & vertex, const long idtag = bitpit::Vertex::NULL_ID);
274  long addVertex(const bitpit::Vertex & vertex, const long idtag = bitpit::Vertex::NULL_ID);
275  bool modifyVertex(const darray3E & vertex, const long & id);
276 
277  long addConnectedCell(const livector1D & locConn, bitpit::ElementType type, int rank = -1);
278  long addConnectedCell(const livector1D & locConn, bitpit::ElementType type, long idtag, int rank = -1);
279  long addConnectedCell(const livector1D & locConn, bitpit::ElementType type, long PID, long idtag, int rank = -1);
280  long addCell(bitpit::Cell & cell, int rank = -1);
281  long addCell(bitpit::Cell & cell, long idtag, int rank = -1);
282 
283  void setPID(livector1D );
284  void setPID(std::unordered_map<long, long> );
285  void setPIDCell(long, long);
286  bool setPIDName(long, const std::string &);
287  void resyncPID();
288 
290  void swap(MimmoObject & ) noexcept;
291 
292  bool cleanGeometry();
293  void setUnsyncAll();
294 
296  livector1D getCellFromVertexList(const livector1D &vertList, bool strict = true);
297  livector1D getInterfaceFromCellList(const livector1D &cellList, bool all = true);
298  livector1D getInterfaceFromVertexList(const livector1D &vertList, bool strict, bool border);
299 
300  std::unordered_map<long, std::set<int> > extractBoundaryFaceCellID(bool ghost = false);
301  livector1D extractBoundaryCellID(bool ghost = false);
302  livector1D extractBoundaryCellID(std::unordered_map<long, std::set<int> > & map);
303  livector1D extractBoundaryVertexID(std::unordered_map<long, std::set<int> > & map);
304  livector1D extractBoundaryVertexID(bool ghost = false);
305  livector1D extractBoundaryInterfaceID(std::unordered_map<long, std::set<int> > & map);
306  livector1D extractBoundaryInterfaceID(bool ghost= false);
308 
309  std::unordered_map<long, std::set<int> > getBorderFaceCells();
310  livector1D getBorderCells(std::unordered_map<long, std::set<int> > & map);
312  livector1D getBorderVertices(std::unordered_map<long, std::set<int> > & map);
314 
315  livector1D extractPIDCells(long, bool squeeze = true);
316  livector1D extractPIDCells(livector1D, bool squeeze = true);
317 
318  std::map<long, livector1D> extractPIDSubdivision();
319 
320  lilimap getMapData(bool withghosts=false);
321  lilimap getMapDataInv(bool withghosts=true);
322  lilimap getMapCell(bool withghosts=true);
323  lilimap getMapCellInv(bool withghosts=true);
324 
325  void getBoundingBox(std::array<double,3> & pmin, std::array<double,3> & pmax, bool global = true);
326  void buildSkdTree(std::size_t value = 1);
327  void buildKdTree();
328  void buildPatchInfo();
329  void updateAdjacencies();
330  void updateInterfaces();
331  void destroyAdjacencies();
332  void destroyInterfaces();
333  void cleanPatchInfo();
334  void resetPatch();
335  void cleanKdTree();
336  void cleanSkdTree();
337  void cleanBoundingBox();
338 
339  void update();
340 
343  bool isClosedLoop();
344 
345  std::vector<std::vector<long> > decomposeLoop();
346 
347  bitpit::ElementType desumeElement(const livector1D &);
348 
349  void dump(std::ostream & stream);
350  void restore(std::istream & stream);
351 
352  void evalCellVolumes(bitpit::PiercedVector<double> &);
353  void evalCellAspectRatio(bitpit::PiercedVector<double> &);
354 
355  double evalCellVolume(const long & id);
356  double evalCellAspectRatio(const long & id);
357 
358  std::array<double,3> evalCellCentroid(const long & id);
359  std::array<double,3> evalInterfaceCentroid(const long & id);
360  double evalInterfaceArea(const long & id);
361  std::array<double,3> evalInterfaceNormal(const long & id);
362 
364  const double & maxdist,
365  livector1D * seedList = nullptr);
366  bitpit::PiercedVector<double> getCellsNarrowBandToExtSurfaceWDist(MimmoObject & surface,
367  const double & maxdist,
368  livector1D * seedList = nullptr);
369 
371  const double & maxdist,
372  livector1D * seedList = nullptr);
373  bitpit::PiercedVector<double> getVerticesNarrowBandToExtSurfaceWDist(MimmoObject & surface,
374  const double & maxdist,
375  livector1D * seedList = nullptr);
376 
377 
378  std::unordered_map<long,long> getInverseConnectivity();
379  std::set<long> findVertexVertexOneRing(const long &, const long & );
380 
381  void buildPointConnectivity();
382  void cleanPointConnectivity();
383  std::unordered_set<long> & getPointConnectivity(const long & id);
385 
386  void triangulate();
387 
388  void degradeDegenerateElements(bitpit::PiercedVector<bitpit::Cell>* degradedDeletedCells = nullptr, bitpit::PiercedVector<bitpit::Vertex>* collapsedVertices = nullptr);
389 
390 
391 protected:
392  void initializeLogger();
393  void reset(int type, bool isParallel = MIMMO_ENABLE_MPI);
394 
395  std::unordered_set<int> elementsMap(bitpit::PatchKernel & obj);
396 
397 #if MIMMO_ENABLE_MPI
398  void initializeMPI();
399  void initializeCommunicator(bool isParallel = true);
400 #endif
401 
402 private:
403  //make copy constructor and assignment private and not accessible (use properly clone method for copy)
404  MimmoObject(const MimmoObject & other);
405  MimmoObject & operator=(MimmoObject other);
406 
407  bool checkCellConnCoherence(const bitpit::ElementType & type, const livector1D & conn_);
408 
414  struct VertexPositionLess
415  {
419  VertexPositionLess(const MimmoObject &object)
420  : m_object(object)
421  {
422  }
424  virtual ~VertexPositionLess() = default;
425 
435  bool operator()(const long &id_1, const long &id_2) const
436  {
437  std::array<double, 3> vertex_1 = m_object.getVertexCoords(id_1);
438  std::array<double, 3> vertex_2 = m_object.getVertexCoords(id_2);
439 
440  for (int k = 0; k < 3; ++k) {
441  if (std::abs(vertex_1[k] - vertex_2[k]) <= 1.0E-12) {
442  continue;
443  }
444  return vertex_1[k] < vertex_2[k];
445  }
446 
447  // If we are here the two vertices coincide. It's not
448  // possible to define an order for the two vertices.
449  std::ostringstream stream;
450  stream << "It was not possible to define an order for vertices " << id_1 << " and " << id_2 << ". ";
451  stream << "The two vertices have the same coordinates.";
452  throw std::runtime_error (stream.str());
453  }
454 
455  const MimmoObject &m_object;
456  };
457 
458 };
459 
460 };
461 
462 #endif /* __MIMMOOBJECT_HPP__ */
int getProcessorCount() const
livector2D getConnectivity()
std::vector< livector1D > livector2D
livector1D extractBoundaryVertexID(std::unordered_map< long, std::set< int > > &map)
SyncStatus m_AdjSync
std::unordered_map< long, std::set< int > > getBorderFaceCells()
std::vector< std::vector< long > > decomposeLoop()
std::set< long > findVertexVertexOneRing(const long &, const long &)
double evalInterfaceArea(const long &id)
SyncStatus
Define status of mimmo object structures like adjacencies, interfaces, etc.
bitpit::PiercedVector< bitpit::Vertex > & getVertices()
bitpit::PiercedVector< double > getCellsNarrowBandToExtSurfaceWDist(MimmoObject &surface, const double &maxdist, livector1D *seedList=nullptr)
SyncStatus m_boundingBoxSync
MimmoObject is the basic geometry container for mimmo library.
Custom derivation of bitpit::VolUnstructured class.
Definition: MimmoObject.hpp:73
SyncStatus m_pointConnectivitySync
bitpit::Logger * m_log
livector1D getBorderCells()
bitpit::PiercedVector< bitpit::Interface > & getInterfaces()
std::unordered_set< long > m_pidsType
SyncStatus getKdTreeSyncStatus()
dvecarr3E getVerticesCoords(lilimap *mapDataInv=nullptr)
std::vector< darray3E > dvecarr3E
bool setPIDName(long, const std::string &)
livector1D getVertexFromCellList(const livector1D &cellList)
void setPIDCell(long, long)
void swap(MimmoObject &) noexcept
void dump(std::ostream &stream)
MimmoObject(int type=1, bool isParallel=0)
std::vector< long > livector1D
long getNInternals() const
lilimap getMapCell(bool withghosts=true)
bool isPointInterior(long id)
long addConnectedCell(const livector1D &locConn, bitpit::ElementType type, int rank=-1)
livector1D getCellsIds(bool internalsOnly=false)
SyncStatus m_infoSync
livector1D getCellFromVertexList(const livector1D &vertList, bool strict=true)
std::unique_ptr< bitpit::PatchKernel > clone() const override
long addVertex(const darray3E &vertex, const long idtag=bitpit::Vertex::NULL_ID)
bitpit::PatchNumberingInfo * getPatchInfo()
livector1D extractBoundaryInterfaceID(std::unordered_map< long, std::set< int > > &map)
bitpit::PiercedVector< bitpit::Cell > & getCells()
void setTolerance(double tol)
bool modifyVertex(const darray3E &vertex, const long &id)
lilimap getMapCellInv(bool withghosts=true)
void setPID(livector1D)
SyncStatus getInfoSyncStatus()
std::unique_ptr< bitpit::PatchSkdTree > m_skdTree
livector2D getCompactConnectivity(lilimap &mapDataInv)
void evalCellAspectRatio(bitpit::PiercedVector< double > &)
std::unique_ptr< bitpit::KdTree< 3, bitpit::Vertex, long > > m_kdTree
livector1D getCellsNarrowBandToExtSurface(MimmoObject &surface, const double &maxdist, livector1D *seedList=nullptr)
std::unordered_map< long, long > getInverseConnectivity()
bitpit::PatchNumberingInfo m_patchInfo
SyncStatus getAdjacenciesSyncStatus()
livector1D extractPIDCells(long, bool squeeze=true)
bitpit::KdTree< 3, bitpit::Vertex, long > * getKdTree()
livector1D getInterfaceFromVertexList(const livector1D &vertList, bool strict, bool border)
bitpit::ElementType desumeElement(const livector1D &)
std::unique_ptr< bitpit::PatchKernel > clone() const override
std::array< double, 3 > evalInterfaceNormal(const long &id)
std::unordered_map< long, std::string > & getPIDTypeListWNames()
std::unordered_map< long, std::set< int > > extractBoundaryFaceCellID(bool ghost=false)
std::unordered_map< long int, long int > lilimap
Custom derivation of bitpit::SurfUnstructured class, for Point Cloud handling only.
Definition: MimmoObject.hpp:99
SyncStatus getInterfacesSyncStatus()
std::unordered_set< int > elementsMap(bitpit::PatchKernel &obj)
bitpit::PatchSkdTree * getSkdTree()
livector1D getBorderVertices()
void getBoundingBox(std::array< double, 3 > &pmin, std::array< double, 3 > &pmax, bool global=true)
bitpit::Logger & getLog()
std::array< double, 3 > evalCellCentroid(const long &id)
livector1D getInterfaceFromCellList(const livector1D &cellList, bool all=true)
void degradeDegenerateElements(bitpit::PiercedVector< bitpit::Cell > *degradedDeletedCells=nullptr, bitpit::PiercedVector< bitpit::Vertex > *collapsedVertices=nullptr)
void restore(std::istream &stream)
std::array< double, 3 > evalInterfaceCentroid(const long &id)
MimmoSharedPointer< MimmoObject > clone() const
std::unordered_map< long, std::string > m_pidsTypeWNames
void buildSkdTree(std::size_t value=1)
SyncStatus getBoundingBoxSyncStatus()
std::array< double, 3 > darray3E
livector1D getCompactPID()
std::unordered_set< long > & getPIDTypeList()
std::map< long, livector1D > extractPIDSubdivision()
livector1D getCellConnectivity(long id) const
SyncStatus getSkdTreeSyncStatus()
std::unordered_set< long > & getPointConnectivity(const long &id)
long addCell(bitpit::Cell &cell, int rank=-1)
livector1D getVerticesIds(bool internalsOnly=false)
void evalCellVolumes(bitpit::PiercedVector< double > &)
SyncStatus getPointConnectivitySyncStatus()
MimmoSharedPointer is a custom implementation of shared pointer.
std::unique_ptr< bitpit::PatchKernel > clone() const override
livector1D getVerticesNarrowBandToExtSurface(MimmoObject &surface, const double &maxdist, livector1D *seedList=nullptr)
lilimap getMapData(bool withghosts=false)
livector1D extractBoundaryCellID(bool ghost=false)
Custom derivation of bitpit::SurfUnstructured class.
Definition: MimmoObject.hpp:45
std::unordered_map< long, std::unordered_set< long > > m_pointConnectivity
MimmoSharedPointer< MimmoObject > extractBoundaryMesh()
std::unordered_map< long, long > getPID()
bitpit::PiercedVector< double > getVerticesNarrowBandToExtSurfaceWDist(MimmoObject &surface, const double &maxdist, livector1D *seedList=nullptr)
void reset(int type, bool isParallel=0)
double evalCellVolume(const long &id)
lilimap getMapDataInv(bool withghosts=true)
SyncStatus m_kdTreeSync
const darray3E & getVertexCoords(long i) const
SyncStatus m_IntSync
SyncStatus m_skdTreeSync
bitpit::PatchKernel * getPatch()