edgeMesh.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "edgeMesh.H"
29 #include "ListOps.H"
30 #include "EdgeMap.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
39 }
40 
41 
43 {
44  return wordHashSet(*fileExtensionConstructorTablePtr_);
45 }
46 
47 
49 {
50  return wordHashSet(*writefileExtensionMemberFunctionTablePtr_);
51 }
52 
53 
54 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
55 
57 (
58  const word& ext,
59  const bool verbose
60 )
61 {
62  return checkSupport
63  (
64  readTypes(),
65  ext,
66  verbose,
67  "reading"
68  );
69 }
70 
71 
73 (
74  const word& ext,
75  const bool verbose
76 )
77 {
78  return checkSupport
79  (
80  writeTypes(),
81  ext,
82  verbose,
83  "writing"
84  );
85 }
86 
87 
89 (
90  const fileName& name,
91  const bool verbose
92 )
93 {
94  word ext = name.ext();
95  if (ext == "gz")
96  {
97  ext = name.lessExt().ext();
98  }
99  return canReadType(ext, verbose);
100 }
101 
102 
103 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
104 
105 void Foam::edgeMesh::calcPointEdges() const
106 {
107  if (pointEdgesPtr_.valid())
108  {
110  << "pointEdges already calculated." << abort(FatalError);
111  }
112 
113  pointEdgesPtr_.reset(new labelListList(points_.size()));
114  labelListList& pointEdges = pointEdgesPtr_();
115 
116  invertManyToMany(pointEdges.size(), edges_, pointEdges);
117 }
118 
119 
120 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
121 
123 :
124  fileFormats::edgeMeshFormatsCore(),
125  points_(0),
126  edges_(0),
127  pointEdgesPtr_(nullptr)
128 {}
129 
130 
132 (
133  const pointField& points,
134  const edgeList& edges
135 )
136 :
137  fileFormats::edgeMeshFormatsCore(),
138  points_(points),
139  edges_(edges),
140  pointEdgesPtr_(nullptr)
141 {}
142 
143 
145 (
146  pointField&& pointLst,
147  edgeList&& edgeLst
148 )
149 :
150  fileFormats::edgeMeshFormatsCore(),
151  points_(move(pointLst)),
152  edges_(move(edgeLst)),
153  pointEdgesPtr_(nullptr)
154 {}
155 
156 
157 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
158 
160 {}
161 
162 
163 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
164 
166 {
167  points_.clear();
168  edges_.clear();
169  pointEdgesPtr_.clear();
170 }
171 
172 
174 (
175  pointField&& pointLst,
176  edgeList&& edgeLst
177 )
178 {
179  // Take over new primitive data.
180  // Optimised to avoid overwriting data at all
181  if (notNull(pointLst))
182  {
183  points_.transfer(pointLst);
184  }
185 
186  if (notNull(edgeLst))
187  {
188  edges_.transfer(edgeLst);
189 
190  // connectivity likely changed
191  pointEdgesPtr_.clear();
192  }
193 }
194 
195 
197 {
198  points_.transfer(mesh.points_);
199  edges_.transfer(mesh.edges_);
200  pointEdgesPtr_ = mesh.pointEdgesPtr_;
201 }
202 
203 
205 {
206  edgeRegion.setSize(edges_.size());
207  edgeRegion = -1;
208 
209  label startEdgeI = 0;
210  label currentRegion = 0;
211 
212  while (true)
213  {
214  while (startEdgeI < edges_.size() && edgeRegion[startEdgeI] != -1)
215  {
216  startEdgeI++;
217  }
218 
219  if (startEdgeI == edges_.size())
220  {
221  break;
222  }
223 
224  // Found edge that has not yet been assigned a region.
225  // Mark connected region with currentRegion starting at startEdgeI.
226 
227  edgeRegion[startEdgeI] = currentRegion;
228  labelList edgesToVisit(1, startEdgeI);
229 
230  while (edgesToVisit.size())
231  {
232  // neighbours of current edgesToVisit
233  DynamicList<label> newEdgesToVisit(edgesToVisit.size());
234 
235  // Mark all point connected edges with current region.
236  forAll(edgesToVisit, i)
237  {
238  label edgeI = edgesToVisit[i];
239 
240  // Mark connected edges
241  const edge& e = edges_[edgeI];
242 
243  forAll(e, fp)
244  {
245  const labelList& pEdges = pointEdges()[e[fp]];
246 
247  forAll(pEdges, pEdgeI)
248  {
249  label nbrEdgeI = pEdges[pEdgeI];
250 
251  if (edgeRegion[nbrEdgeI] == -1)
252  {
253  edgeRegion[nbrEdgeI] = currentRegion;
254  newEdgesToVisit.append(nbrEdgeI);
255  }
256  }
257  }
258  }
259 
260  edgesToVisit.transfer(newEdgesToVisit);
261  }
262 
263  currentRegion++;
264  }
265  return currentRegion;
266 }
267 
268 
269 void Foam::edgeMesh::scalePoints(const scalar scaleFactor)
270 {
271  // avoid bad scaling
272  if (scaleFactor > 0 && scaleFactor != 1.0)
273  {
274  points_ *= scaleFactor;
275  }
276 }
277 
278 
280 {
281  EdgeMap<label> existingEdges(2*edges_.size());
282 
283  label curEdgeI = 0;
284  forAll(edges_, edgeI)
285  {
286  const edge& e = edges_[edgeI];
287 
288  if (existingEdges.insert(e, curEdgeI))
289  {
290  curEdgeI++;
291  }
292  }
293 
294  if (debug)
295  {
296  Info<< "Merging duplicate edges: "
297  << edges_.size() - existingEdges.size()
298  << " edges will be deleted." << endl;
299  }
300 
301  edges_.setSize(existingEdges.size());
302 
303  forAllConstIter(EdgeMap<label>, existingEdges, iter)
304  {
305  edges_[iter()] = iter.key();
306  }
307 
308  // connectivity changed
309  pointEdgesPtr_.clear();
310 }
311 
312 
313 // ************************************************************************* //
Various functions to operate on Lists.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
Macros for easy insertion into member function selection tables.
Macros for easy insertion into run-time selection tables.
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
A HashTable with keys but without contents.
Definition: HashSet.H:62
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void setSize(const label)
Reset size of List.
Definition: List.C:281
Points connected by edges.
Definition: edgeMesh.H:72
virtual void scalePoints(const scalar)
Scale points. A non-positive factor is ignored.
Definition: edgeMesh.C:269
virtual void mergeEdges()
Merge duplicate edges.
Definition: edgeMesh.C:279
virtual ~edgeMesh()
Destructor.
Definition: edgeMesh.C:159
virtual void reset(pointField &&points, edgeList &&edges)
Reset primitive data (points, edges)
Definition: edgeMesh.C:174
static wordHashSet writeTypes()
Definition: edgeMesh.C:48
edgeMesh()
Construct null.
Definition: edgeMesh.C:122
void transfer(edgeMesh &)
Transfer the contents of the argument and annul the argument.
Definition: edgeMesh.C:196
static bool canWriteType(const word &ext, const bool verbose=false)
Can we write this file format type?
Definition: edgeMesh.C:73
label regions(labelList &edgeRegion) const
Find connected regions. Set region number per edge.
Definition: edgeMesh.C:204
static bool canRead(const fileName &, const bool verbose=false)
Can we read this file format?
Definition: edgeMesh.C:89
static bool canReadType(const word &ext, const bool verbose=false)
Can we read this file format?
Definition: edgeMesh.C:57
virtual void clear()
Clear all storage.
Definition: edgeMesh.C:165
static wordHashSet readTypes()
Definition: edgeMesh.C:42
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:61
A class for handling file names.
Definition: fileName.H:82
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
const pointField & points
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
Namespace for OpenFOAM.
const doubleScalar e
Definition: doubleScalar.H:106
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Info
bool notNull(const T &t)
Return true if t is not a reference to the nullObject of type T.
Definition: nullObjectI.H:64
List< labelList > labelListList
A List of labelList.
Definition: labelList.H:57
defineTypeNameAndDebug(combustionModel, 0)
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:210
error FatalError
defineMemberFunctionSelectionTable(edgeMesh, write, fileExtension)
void invertManyToMany(const label len, const UList< InList > &, List< OutList > &)
Invert many-to-many.