Loading...
Searching...
No Matches
edgeMesh.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2015-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "edgeMesh.H"
30#include "bitSet.H"
31#include "edgeHashes.H"
32#include "mergePoints.H"
33#include "ListOps.H"
37// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38
39namespace Foam
40{
46
47// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
50{
51 return wordHashSet(*fileExtensionConstructorTablePtr_);
52}
53
56{
57 return wordHashSet(*writefileExtensionMemberFunctionTablePtr_);
58}
59
60
61bool Foam::edgeMesh::canReadType(const word& fileType, bool verbose)
62{
63 return checkSupport
64 (
65 readTypes(),
66 fileType,
67 verbose,
68 "reading"
69 );
70}
71
72
73bool Foam::edgeMesh::canWriteType(const word& fileType, bool verbose)
74{
75 return checkSupport
76 (
77 writeTypes(),
78 fileType,
79 verbose,
80 "writing"
81 );
82}
83
84
85bool Foam::edgeMesh::canRead(const fileName& name, bool verbose)
86{
87 const word ext =
88 (
89 name.has_ext("gz")
90 ? name.stem().ext()
91 : name.ext()
92 );
93
94 return canReadType(ext, verbose);
95}
96
97
98// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
99
100void Foam::edgeMesh::calcPointEdges() const
101{
102 if (pointEdgesPtr_)
103 {
105 << "pointEdges already calculated."
106 << abort(FatalError);
107 }
108
109 pointEdgesPtr_.reset(new labelListList(points_.size()));
110 auto& pointEdges = *pointEdgesPtr_;
112 invertManyToMany(pointEdges.size(), edges_, pointEdges);
113}
114
115
116// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
117
120 points_.clear();
121 edges_.clear();
122 pointEdgesPtr_.reset(nullptr);
123}
124
125
127{
128 if (&mesh == this)
129 {
130 return; // Self-transfer is a no-op
131 }
133 points_.transfer(mesh.points_);
134 edges_.transfer(mesh.edges_);
135 pointEdgesPtr_ = std::move(mesh.pointEdgesPtr_);
136}
137
138
139Foam::label Foam::edgeMesh::regions(labelList& edgeRegion) const
140{
141 edgeRegion.setSize(edges_.size());
142 edgeRegion = -1;
143
144 label startEdgeI = 0;
145 label currentRegion = 0;
146
147 while (true)
148 {
149 while (startEdgeI < edges_.size() && edgeRegion[startEdgeI] != -1)
150 {
151 startEdgeI++;
152 }
153
154 if (startEdgeI == edges_.size())
155 {
156 break;
157 }
158
159 // Found edge that has not yet been assigned a region.
160 // Mark connected region with currentRegion starting at startEdgeI.
161
162 edgeRegion[startEdgeI] = currentRegion;
163 labelList edgesToVisit(1, startEdgeI);
164
165 while (edgesToVisit.size())
166 {
167 // neighbours of current edgesToVisit
168 DynamicList<label> newEdgesToVisit(edgesToVisit.size());
169
170 // Mark all point connected edges with current region.
171 forAll(edgesToVisit, i)
172 {
173 label edgeI = edgesToVisit[i];
174
175 // Mark connected edges
176 const edge& e = edges_[edgeI];
177
178 forAll(e, fp)
179 {
180 const labelList& pEdges = pointEdges()[e[fp]];
181
182 forAll(pEdges, pEdgeI)
183 {
184 label nbrEdgeI = pEdges[pEdgeI];
185
186 if (edgeRegion[nbrEdgeI] == -1)
187 {
188 edgeRegion[nbrEdgeI] = currentRegion;
189 newEdgesToVisit.append(nbrEdgeI);
190 }
191 }
192 }
193 }
194
195 edgesToVisit.transfer(newEdgesToVisit);
196 }
197
198 currentRegion++;
199 }
200
201 return currentRegion;
202}
203
204
205void Foam::edgeMesh::scalePoints(const scalar scaleFactor)
206{
207 // avoid bad scaling
208 if (scaleFactor > VSMALL && !equal(scaleFactor, 1))
209 {
210 points_ *= scaleFactor;
211 }
212}
213
214
215void Foam::edgeMesh::mergePoints(const scalar mergeDist)
216{
217 labelList pointMap;
218
219 label nChanged = Foam::inplaceMergePoints
220 (
221 points_,
222 mergeDist,
223 false,
224 pointMap
225 );
226
227 if (nChanged)
228 {
229 pointEdgesPtr_.reset(nullptr); // connectivity change
230
231 for (edge& e : edges_)
232 {
233 e = edge(pointMap, e);
235 }
236
237 this->mergeEdges();
238}
239
240
242{
243 edgeHashSet uniqEdges(2*edges_.size());
244 bitSet pointIsUsed(points_.size());
245
246 label nUniqEdges = 0;
247 label nUniqPoints = 0;
248 forAll(edges_, edgeI)
249 {
250 const edge& e = edges_[edgeI];
251
252 // Remove degenerate and repeated edges
253 // - reordering (e[0] < e[1]) is not really necessary
254 if (e[0] != e[1] && uniqEdges.insert(e))
255 {
256 if (nUniqEdges != edgeI)
257 {
258 edges_[nUniqEdges] = e;
259 }
260 edges_[nUniqEdges].sort();
261 ++nUniqEdges;
262
263 if (pointIsUsed.set(e[0]))
264 {
265 ++nUniqPoints;
266 }
267 if (pointIsUsed.set(e[1]))
268 {
269 ++nUniqPoints;
270 }
271 }
272 }
273
274 if (debug)
275 {
276 Info<< "Merging duplicate edges: "
277 << (edges_.size() - nUniqEdges)
278 << " edges will be deleted, "
279 << (points_.size() - nUniqPoints)
280 << " unused points will be removed." << endl;
281 }
282
283 if (nUniqEdges < edges_.size())
284 {
285 pointEdgesPtr_.reset(nullptr); // connectivity change
286 edges_.setSize(nUniqEdges); // truncate
287 }
288
289 if (nUniqPoints < points_.size())
290 {
291 pointEdgesPtr_.reset(nullptr); // connectivity change
292
293 // build a oldToNew point-map and rewrite the points.
294 // We can do this simultaneously since the point order is unchanged
295 // and we are only effectively eliminating some entries.
296 labelList pointMap(points_.size(), -1);
297
298 label newId = 0;
299 forAll(pointMap, pointi)
300 {
301 if (pointIsUsed.test(pointi))
302 {
303 pointMap[pointi] = newId;
304
305 if (newId < pointi)
306 {
307 // copy down
308 points_[newId] = points_[pointi];
309 }
310 ++newId;
311 }
312 }
313 points_.setSize(newId);
314
315 // Renumber edges - already sorted (above)
316 for (edge& e : edges_)
317 {
318 e = edge(pointMap, e);
319 }
320 }
321}
322
323
324// ************************************************************************* //
Various functions to operate on Lists.
Macros for easy insertion into member function selection tables.
Macros for easy insertion into run-time selection tables.
writer write("magLe", fld)
void setSize(const label n)
Alias for resize().
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
void append(const T &val)
Copy append an element to the end of this list.
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition HashSet.H:229
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition List.C:347
void append(const T &val)
Append an element at the end of the list.
Definition List.H:497
void setSize(label n)
Alias for resize().
Definition List.H:536
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition bitSet.H:61
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition bitSetI.H:502
bool test(const label pos) const
Test for True value at specified position, never auto-vivify entries.
Definition bitSet.H:334
Mesh data needed to do the Finite Area discretisation.
Definition edgeFaMesh.H:50
virtual void mergeEdges()
Merge duplicate edges and eliminate unused points.
Definition edgeMesh.C:234
const labelListList & pointEdges() const
Return edges.
Definition edgeMeshI.H:104
static bool canWriteType(const word &fileType, bool verbose=false)
Can we write this file format type?
Definition edgeMesh.C:66
static wordHashSet writeTypes()
Summary of supported write file types.
Definition edgeMesh.C:48
virtual void mergePoints(const scalar mergeDist)
Geometric merge points (points within mergeDist) prior to.
Definition edgeMesh.C:208
edgeMesh(const faMesh &mesh)
Construct finite-area edge mesh faMesh reference.
Definition edgeFaMesh.H:58
static bool canReadType(const word &fileType, bool verbose=false)
Can we read this file format?
Definition edgeMesh.C:54
void transfer(edgeMesh &mesh)
Transfer the contents of the argument and annul the argument.
Definition edgeMesh.C:119
label regions(labelList &edgeRegion) const
Find connected regions. Set region number per edge.
Definition edgeMesh.C:132
static bool canRead(const fileName &name, bool verbose=false)
Can we read this file format?
Definition edgeMesh.C:78
virtual void clear()
Clear all storage.
Definition edgeMesh.C:111
static void write(const fileName &name, const edgeMesh &mesh, IOstreamOption streamOpt=IOstreamOption(), const dictionary &options=dictionary::null)
Write to file (format implicit in the extension).
Definition edgeMeshIO.C:105
virtual void scalePoints(const scalar scaleFactor)
Scale points. A non-positive factor is ignored.
Definition edgeMesh.C:198
static wordHashSet readTypes()
Summary of supported read file types.
Definition edgeMesh.C:42
An edge is a list of two vertex labels. This can correspond to a directed graph edge or an edge on a ...
Definition edge.H:62
static bool checkSupport(const wordHashSet &available, const word &fileType, const bool verbose=false, const char *functionName=nullptr)
Verbose checking of fileType in the list of available types.
A class for handling file names.
Definition fileName.H:75
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
dynamicFvMesh & mesh
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
#define defineMemberFunctionSelectionTable(baseType, funcName, argNames)
Define run-time selection table.
Geometric merging of points. See below.
Namespace for handling debugging switches.
Definition debug.C:45
Namespace for OpenFOAM.
void invertManyToMany(const label len, const UList< InputIntListType > &input, List< OutputIntListType > &output)
Invert many-to-many.
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition HashSet.H:80
List< labelList > labelListList
List of labelList.
Definition labelList.H:38
HashSet< edge, Hash< edge > > edgeHashSet
A HashSet with edge for its key. Hashing (and ==) on an edge is symmetric.
Definition edgeHashes.H:48
List< label > labelList
A List of labels.
Definition List.H:62
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition label.H:180
messageStream Info
Information stream (stdout output on master, null elsewhere).
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
errorManip< error > abort(error &err)
Definition errorManip.H:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
label inplaceMergePoints(PointList &points, const scalar mergeTol, const bool verbose, labelList &pointToUnique)
Inplace merge points, preserving the original point order. All points closer/equal mergeTol are to be...
#define defineRunTimeSelectionTable(baseType, argNames)
Define run-time selection table.
volScalarField & e
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299