Loading...
Searching...
No Matches
voloctree.hpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit 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 * bitpit 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 bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#ifndef __BITPIT_VOLOCTREE_HPP__
26#define __BITPIT_VOLOCTREE_HPP__
27
28#include <vector>
29#include <unordered_map>
30#include <unordered_set>
31
32#include "bitpit_PABLO.hpp"
33#include "bitpit_patchkernel.hpp"
34
35namespace bitpit {
36
37class VolOctree : public VolumeKernel {
38
39public:
42
43 struct OctantInfo {
44 OctantInfo() : id(0), internal(true) {};
45 OctantInfo(uint32_t _id, bool _internal) : id(_id), internal(_internal) {};
46
47 bool operator==(const OctantInfo &other) const
48 {
49 return (id == other.id && internal == other.internal);
50 }
51
52 uint32_t id;
53 bool internal;
54 };
55
57 {
58 // We can just use the id for the hash, beacuse only two different
59 // octants can be assigned to the same id: the internal octant and
60 // the ghost octant. It's not worth including the ghost flag in the
61 // hash.
62 std::size_t operator()(const OctantInfo& k) const
63 {
64 return std::hash<uint32_t>()(k.id);
65 }
66 };
67
68#if BITPIT_ENABLE_MPI==1
69 VolOctree(MPI_Comm communicator, std::size_t haloSize = 1);
70 VolOctree(int dimension, const std::array<double, 3> &origin, double length, double dh, MPI_Comm communicator, std::size_t haloSize = 1);
71 VolOctree(int id, int dimension, const std::array<double, 3> &origin, double length, double dh, MPI_Comm communicator, std::size_t haloSize = 1);
72 VolOctree(std::istream &stream, MPI_Comm communicator, std::size_t haloSize = 1);
73#else
74 VolOctree();
75 VolOctree(int dimension, const std::array<double, 3> &origin, double length, double dh);
76 VolOctree(int id, int dimension, const std::array<double, 3> &origin, double length, double dh);
77 VolOctree(std::istream &stream);
78#endif
79 VolOctree(std::unique_ptr<PabloUniform> &&tree, std::unique_ptr<PabloUniform> *adopter = nullptr);
80 VolOctree(int id, std::unique_ptr<PabloUniform> &&tree, std::unique_ptr<PabloUniform> *adopter = nullptr);
81 VolOctree(VolOctree &&other) = default;
82
83 ~VolOctree();
84
85 VolOctree & operator=(const VolOctree &other);
86 VolOctree & operator=(VolOctree &&other) = default;
87
88 std::unique_ptr<PatchKernel> clone() const override;
89
90 void reset() override;
91 void setDimension(int dimension) override;
92
93 void settleAdaptionMarkers() override;
94
95 double evalCellVolume(long id) const override;
96 double evalCellSize(long id) const override;
97 std::array<double, 3> evalCellCentroid(long id) const override;
98
99 void simulateCellUpdate(long id, adaption::Marker marker, std::vector<Cell> *virtualCells, PiercedVector<Vertex, long> *virtualVertices) const override;
100
101 double evalInterfaceArea(long id) const override;
102 std::array<double, 3> evalInterfaceNormal(long id) const override;
103
104 OctantInfo getCellOctant(long id) const;
105 int getCellLevel(long id) const;
106 int getCellFamilySplitLocalVertex(long id) const;
107
108 long getOctantId(const OctantInfo &octantInfo) const;
109 Octant * getOctantPointer(const OctantInfo &octantInfo);
110 const Octant * getOctantPointer(const OctantInfo &octantInfo) const;
111
113 const PabloUniform & getTree() const;
114 void setTreeAdopter(std::unique_ptr<PabloUniform> *entruster);
115
116 bool isPointInside(const std::array<double, 3> &point) const override;
117 bool isPointInside(long id, const std::array<double, 3> &point) const override;
118 long locatePoint(const std::array<double, 3> &point) const override;
119
120 std::array<double, 3> getOrigin() const;
121 void setOrigin(const std::array<double, 3> &origin);
122 void translate(const std::array<double, 3> &translation) override;
123 double getLength() const;
124 void setLength(double length);
125 void scale(const std::array<double, 3> &scaling, const std::array<double, 3> &center) override;
126
127protected:
128 VolOctree(const VolOctree &other);
129
130#if BITPIT_ENABLE_MPI==1
131 void setCommunicator(MPI_Comm communicator) override;
132#endif
133
134 void _updateAdjacencies() override;
135
136 std::vector<adaption::Info> _adaptionPrepare(bool trackAdaption) override;
137 std::vector<adaption::Info> _adaptionAlter(bool trackAdaption) override;
138 void _adaptionCleanup() override;
139 bool _markCellForRefinement(long id) override;
140 bool _markCellForCoarsening(long id) override;
141 bool _resetCellAdaptionMarker(long id) override;
142 adaption::Marker _getCellAdaptionMarker(long id) override;
143 bool _enableCellBalancing(long id, bool enabled) override;
144 void _setTol(double tolerance) override;
145 void _resetTol() override;
146
147 int _getDumpVersion() const override;
148 void _dump(std::ostream &stream) const override;
149 void _restore(std::istream &stream) override;
150
151 long _getCellNativeIndex(long id) const override;
152
153 void _findCellNeighs(long id, const std::vector<long> *blackList, std::vector<long> *neighs) const override;
154 void _findCellEdgeNeighs(long id, int edge, const std::vector<long> *blackList, std::vector<long> *neighs) const override;
155 void _findCellVertexNeighs(long id, int vertex, const std::vector<long> *blackList, std::vector<long> *neighs) const override;
156
157#if BITPIT_ENABLE_MPI==1
158 std::size_t _getMaxHaloSize() override;
159 void _setHaloSize(std::size_t haloSize) override;
160
161 std::vector<adaption::Info> _partitioningPrepare(const std::unordered_map<long, double> &cellWeights, double defaultWeight, bool trackPartitioning) override;
162 std::vector<adaption::Info> _partitioningAlter(bool trackPartitioning) override;
163 void _partitioningCleanup() override;
164
165 std::vector<long> _findGhostCellExchangeSources(int rank) override;
166#endif
167
168 int findAdjoinNeighFace(const Cell &cell, int cellFace, const Cell &neigh) const override;
169 bool isSameFace(const Cell &cell_A, int face_A, const Cell &cell_B, int face_B) const override;
170
171private:
173
174 typedef std::bitset<72> OctantHash;
175
176 struct RenumberInfo {
177 RenumberInfo()
178 : cellId(Cell::NULL_ID), newTreeId(0)
179 {
180 };
181
182 RenumberInfo(long _cellId, uint32_t _newTreeId)
183 : cellId(_cellId), newTreeId(_newTreeId)
184 {
185 };
186
187 long cellId;
188 uint32_t newTreeId;
189 };
190
191 struct DeleteInfo {
192 DeleteInfo()
193 : cellId(Element::NULL_ID), trigger(adaption::TYPE_UNKNOWN), rank(-1)
194 {
195 };
196
197 DeleteInfo(long _cellId, adaption::Type _trigger, int _rank = -1)
198 : cellId(_cellId), trigger(_trigger), rank(_rank)
199 {
200 };
201
202 long cellId;
203 adaption::Type trigger;
204 int rank;
205 };
206
207 struct FaceInfo {
208 FaceInfo() : id(Element::NULL_ID), face(-1) {};
209 FaceInfo(long _id, int _face) : id(_id), face(_face) {};
210
211 bool operator==(const FaceInfo &other) const
212 {
213 return (id == other.id && face == other.face);
214 }
215
216 long id;
217 int face;
218 };
219
220 struct FaceInfoHasher
221 {
222 // We can just use the id for the hash, because a cell can have only
223 // a limited amount of faces. It's not worth including the face index
224 // in the hash.
225 std::size_t operator()(const FaceInfo& k) const
226 {
227 return std::hash<long>()(k.id);
228 }
229 };
230
231 typedef std::unordered_set<FaceInfo, FaceInfoHasher> FaceInfoSet;
232
233 typedef std::unordered_map<uint64_t, long> StitchInfo;
234
235 std::vector<std::vector<int>> m_octantLocalFacesOnVertex;
236 std::vector<std::vector<int>> m_octantLocalFacesOnEdge;
237 std::vector<std::vector<int>> m_octantLocalEdgesOnVertex;
238
239 const ReferenceElementInfo *m_cellTypeInfo;
240 const ReferenceElementInfo *m_interfaceTypeInfo;
241
242 std::unordered_map<long, uint32_t, Element::IdHasher> m_cellToOctant;
243 std::unordered_map<long, uint32_t, Element::IdHasher> m_cellToGhost;
244 std::unordered_map<uint32_t, long> m_octantToCell;
245 std::unordered_map<uint32_t, long> m_ghostToCell;
246
247 std::unique_ptr<PabloUniform> m_tree;
248 std::unique_ptr<PabloUniform> *m_treeAdopter;
249
250 std::unique_ptr<std::vector<double>> m_partitioningOctantWeights;
251
252 void initialize();
253
254 void setBoundingBox();
255
256 void __reset(bool resetTree);
257 void __setDimension(int dimension);
258
259 bool setMarker(long id, int8_t value);
260
261 OctantHash evaluateOctantHash(const OctantInfo &octantInfo);
262
263 StitchInfo deleteCells(const std::vector<DeleteInfo> &deletedOctants);
264 void renumberCells(const std::vector<RenumberInfo> &renumberedOctants);
265 std::vector<long> importCells(const std::vector<OctantInfo> &octantTreeIds, StitchInfo &stitchInfo, std::istream *stream = nullptr);
266
267 std::vector<adaption::Info> sync(bool trackChanges);
268
269 void findOctantCodimensionNeighs(const OctantInfo &octantInfo, int index, int codimension,
270 const std::vector<long> *blackList, std::vector<long> *neighs) const;
271
272 void computePartitioningOctantWeights(const std::unordered_map<long, double> &cellWeights, double defaultWeight);
273 void clearPartitioningOctantWeights();
274
275#if BITPIT_ENABLE_MPI==1
276 void initializeTree(std::unique_ptr<PabloUniform> *adopter, std::size_t haloSize);
277 void initializeTreePartitioning();
278 void initializeTreeHaloSize(std::size_t haloSize);
279#else
280 void initializeTree(std::unique_ptr<PabloUniform> *adopter);
281#endif
282};
283
284}
285
286#endif
The Cell class defines the cells.
Definition cell.hpp:42
Octant class definition.
Definition Octant.hpp:89
PABLO Uniform is an example of user class derived from ParaTree to map ParaTree in a uniform (square/...
long locatePoint(double x, double y, double z) const
void setBoundingBox(const std::array< double, 3 > &minPoint, const std::array< double, 3 > &maxPoint)
Metafunction for generating a pierced vector.
The VolOctree defines a Octree patch.
Definition voloctree.hpp:37
void _updateAdjacencies() override
void setDimension(int dimension) override
int getCellLevel(long id) const
long locatePoint(const std::array< double, 3 > &point) const override
std::vector< adaption::Info > _adaptionPrepare(bool trackAdaption) override
std::array< double, 3 > evalInterfaceNormal(long id) const override
double evalCellSize(long id) const override
void _setTol(double tolerance) override
void _findCellEdgeNeighs(long id, int edge, const std::vector< long > *blackList, std::vector< long > *neighs) const override
void scale(const std::array< double, 3 > &scaling, const std::array< double, 3 > &center) override
long _getCellNativeIndex(long id) const override
void _restore(std::istream &stream) override
std::array< double, 3 > getOrigin() const
std::vector< long > _findGhostCellExchangeSources(int rank) override
void translate(const std::array< double, 3 > &translation) override
std::vector< adaption::Info > _adaptionAlter(bool trackAdaption) override
adaption::Marker _getCellAdaptionMarker(long id) override
double evalCellVolume(long id) const override
bool isPointInside(const std::array< double, 3 > &point) const override
void settleAdaptionMarkers() override
OctantInfo getCellOctant(long id) const
Octant * getOctantPointer(const OctantInfo &octantInfo)
void reset() override
void simulateCellUpdate(long id, adaption::Marker marker, std::vector< Cell > *virtualCells, PiercedVector< Vertex, long > *virtualVertices) const override
void _setHaloSize(std::size_t haloSize) override
std::size_t _getMaxHaloSize() override
double getLength() const
std::vector< adaption::Info > _partitioningAlter(bool trackPartitioning) override
void _partitioningCleanup() override
VolOctree(MPI_Comm communicator, std::size_t haloSize=1)
Definition voloctree.cpp:76
double evalInterfaceArea(long id) const override
bool _enableCellBalancing(long id, bool enabled) override
std::vector< adaption::Info > _partitioningPrepare(const std::unordered_map< long, double > &cellWeights, double defaultWeight, bool trackPartitioning) override
void setCommunicator(MPI_Comm communicator) override
int getCellFamilySplitLocalVertex(long id) const
int _getDumpVersion() const override
void _findCellVertexNeighs(long id, int vertex, const std::vector< long > *blackList, std::vector< long > *neighs) const override
void setTreeAdopter(std::unique_ptr< PabloUniform > *entruster)
void _resetTol() override
bool _resetCellAdaptionMarker(long id) override
PabloUniform & getTree()
Gets a reference to the octree associated with the patch.
VolOctree & operator=(const VolOctree &other)
void setOrigin(const std::array< double, 3 > &origin)
std::array< double, 3 > evalCellCentroid(long id) const override
std::unique_ptr< PatchKernel > clone() const override
bool isSameFace(const Cell &cell_A, int face_A, const Cell &cell_B, int face_B) const override
void _adaptionCleanup() override
void _dump(std::ostream &stream) const override
bool _markCellForRefinement(long id) override
void setLength(double length)
void _findCellNeighs(long id, const std::vector< long > *blackList, std::vector< long > *neighs) const override
long getOctantId(const OctantInfo &octantInfo) const
int findAdjoinNeighFace(const Cell &cell, int cellFace, const Cell &neigh) const override
bool _markCellForCoarsening(long id) override
The VolumeKernel class provides an interface for defining volume patches.
bool isPointInside(double x, double y, double z) const
--- layout: doxygen_footer ---