polyMeshIO.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-2017 OpenFOAM Foundation
9  Copyright (C) 2015-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 "polyMesh.H"
30 #include "Time.H"
31 #include "cellIOList.H"
32 
33 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34 
36 (
37  const fileName& inst,
38  const IOobject::writeOption wOpt
39 )
40 {
41  DebugInFunction << "Resetting file instance to " << inst << endl;
42 
43  points_.writeOpt(wOpt);
44  points_.instance() = inst;
45 
46  faces_.writeOpt(wOpt);
47  faces_.instance() = inst;
48 
49  owner_.writeOpt(wOpt);
50  owner_.instance() = inst;
51 
52  neighbour_.writeOpt(wOpt);
53  neighbour_.instance() = inst;
54 
55  boundary_.writeOpt(wOpt);
56  boundary_.instance() = inst;
57 
58  pointZones_.writeOpt(wOpt);
59  pointZones_.instance() = inst;
60 
61  faceZones_.writeOpt(wOpt);
62  faceZones_.instance() = inst;
63 
64  cellZones_.writeOpt(wOpt);
65  cellZones_.instance() = inst;
66 
67  if (tetBasePtIsPtr_)
68  {
69  tetBasePtIsPtr_->writeOpt(wOpt);
70  tetBasePtIsPtr_->instance() = inst;
71  }
72 }
73 
74 
76 {
77  DebugInFunction << "Updating mesh based on saved data." << endl;
78 
79  // Find the point and cell instance
80  fileName pointsInst(time().findInstance(meshDir(), "points"));
81  fileName facesInst(time().findInstance(meshDir(), "faces"));
82  //fileName boundaryInst(time().findInstance(meshDir(), "boundary"));
83 
84  if (debug)
85  {
86  Info<< "Faces instance: old = " << facesInstance()
87  << " new = " << facesInst << nl
88  << "Points instance: old = " << pointsInstance()
89  << " new = " << pointsInst << endl;
90  }
91 
92  if (facesInst != facesInstance())
93  {
94  // Topological change
95  if (debug)
96  {
97  Info<< "Topological change" << endl;
98  }
99 
100  clearOut();
101 
102  // Set instance to new instance. Note that points instance can differ
103  // from from faces instance.
104  setInstance(facesInst);
105  points_.instance() = pointsInst;
106 
107  points_ = pointIOField
108  (
109  IOobject
110  (
111  "points",
112  pointsInst,
113  meshSubDir,
114  *this,
117  false
118  )
119  );
120 
121  faces_ = faceCompactIOList
122  (
123  IOobject
124  (
125  "faces",
126  facesInst,
127  meshSubDir,
128  *this,
131  false
132  )
133  );
134 
135  owner_ = labelIOList
136  (
137  IOobject
138  (
139  "owner",
140  facesInst,
141  meshSubDir,
142  *this,
145  false
146  )
147  );
148 
149  neighbour_ = labelIOList
150  (
151  IOobject
152  (
153  "neighbour",
154  facesInst,
155  meshSubDir,
156  *this,
159  false
160  )
161  );
162 
163  // Reset the boundary patches
164  polyBoundaryMesh newBoundary
165  (
166  IOobject
167  (
168  "boundary",
169  facesInst,
170  meshSubDir,
171  *this,
174  false
175  ),
176  *this
177  );
178 
179  // Check that patch types and names are unchanged
180  bool boundaryChanged = false;
181 
182  if (newBoundary.size() != boundary_.size())
183  {
184  boundaryChanged = true;
185  }
186  else
187  {
188  wordList newTypes = newBoundary.types();
189  wordList newNames = newBoundary.names();
190 
191  wordList oldTypes = boundary_.types();
192  wordList oldNames = boundary_.names();
193 
194  forAll(oldTypes, patchi)
195  {
196  if
197  (
198  oldTypes[patchi] != newTypes[patchi]
199  || oldNames[patchi] != newNames[patchi]
200  )
201  {
202  boundaryChanged = true;
203  break;
204  }
205  }
206  }
207 
208  if (boundaryChanged)
209  {
211  << "Number of patches has changed. This may have "
212  << "unexpected consequences. Proceed with care." << endl;
213 
214  boundary_.clear();
215  boundary_.setSize(newBoundary.size());
216 
217  forAll(newBoundary, patchi)
218  {
219  boundary_.set(patchi, newBoundary[patchi].clone(boundary_));
220  }
221  }
222  else
223  {
224  forAll(boundary_, patchi)
225  {
226  boundary_[patchi] = polyPatch
227  (
228  newBoundary[patchi].name(),
229  newBoundary[patchi].size(),
230  newBoundary[patchi].start(),
231  patchi,
232  boundary_,
233  newBoundary[patchi].physicalType(),
234  newBoundary[patchi].inGroups()
235  );
236  }
237  }
238 
239 
240  // Boundary is set so can use initMesh now (uses boundary_ to
241  // determine internal and active faces)
242 
243  if (!owner_.headerClassName().empty())
244  {
245  initMesh();
246  }
247  else
248  {
250  (
251  IOobject
252  (
253  "cells",
254  facesInst,
255  meshSubDir,
256  *this,
259  false
260  )
261  );
262 
263  // Recalculate the owner/neighbour addressing and reset the
264  // primitiveMesh
265  initMesh(cells);
266  }
267 
268 
269  // Even if number of patches stayed same still recalculate boundary
270  // data.
271 
272  // Calculate topology for the patches (processor-processor comms etc.)
273  boundary_.updateMesh();
274 
275  // Calculate the geometry for the patches (transformation tensors etc.)
276  boundary_.calcGeometry();
277 
278  // Derived info
279  bounds_ = boundBox(points_);
280  geometricD_ = Zero;
281  solutionD_ = Zero;
282 
283  // Zones
284  pointZoneMesh newPointZones
285  (
286  IOobject
287  (
288  "pointZones",
289  facesInst,
290  meshSubDir,
291  *this,
294  false
295  ),
296  *this
297  );
298 
299  label oldSize = pointZones_.size();
300 
301  if (newPointZones.size() <= pointZones_.size())
302  {
303  pointZones_.setSize(newPointZones.size());
304  }
305 
306  // Reset existing ones
307  forAll(pointZones_, czI)
308  {
309  pointZones_[czI] = newPointZones[czI];
310  }
311 
312  // Extend with extra ones
313  pointZones_.setSize(newPointZones.size());
314 
315  for (label czI = oldSize; czI < newPointZones.size(); czI++)
316  {
317  pointZones_.set(czI, newPointZones[czI].clone(pointZones_));
318  }
319 
320 
321  faceZoneMesh newFaceZones
322  (
323  IOobject
324  (
325  "faceZones",
326  facesInst,
327  meshSubDir,
328  *this,
331  false
332  ),
333  *this
334  );
335 
336  oldSize = faceZones_.size();
337 
338  if (newFaceZones.size() <= faceZones_.size())
339  {
340  faceZones_.setSize(newFaceZones.size());
341  }
342 
343  // Reset existing ones
344  forAll(faceZones_, fzI)
345  {
346  faceZones_[fzI].resetAddressing
347  (
348  newFaceZones[fzI],
349  newFaceZones[fzI].flipMap()
350  );
351  }
352 
353  // Extend with extra ones
354  faceZones_.setSize(newFaceZones.size());
355 
356  for (label fzI = oldSize; fzI < newFaceZones.size(); fzI++)
357  {
358  faceZones_.set(fzI, newFaceZones[fzI].clone(faceZones_));
359  }
360 
361 
362  cellZoneMesh newCellZones
363  (
364  IOobject
365  (
366  "cellZones",
367  facesInst,
368  meshSubDir,
369  *this,
372  false
373  ),
374  *this
375  );
376 
377  oldSize = cellZones_.size();
378 
379  if (newCellZones.size() <= cellZones_.size())
380  {
381  cellZones_.setSize(newCellZones.size());
382  }
383 
384  // Reset existing ones
385  forAll(cellZones_, czI)
386  {
387  cellZones_[czI] = newCellZones[czI];
388  }
389 
390  // Extend with extra ones
391  cellZones_.setSize(newCellZones.size());
392 
393  for (label czI = oldSize; czI < newCellZones.size(); czI++)
394  {
395  cellZones_.set(czI, newCellZones[czI].clone(cellZones_));
396  }
397 
398  // Re-read tet base points
399  tetBasePtIsPtr_ = readTetBasePtIs();
400 
401 
402  if (boundaryChanged)
403  {
405  }
406  else
407  {
408  return polyMesh::TOPO_CHANGE;
409  }
410  }
411  else if (pointsInst != pointsInstance())
412  {
413  // Points moved
414  if (debug)
415  {
416  Info<< "Point motion" << endl;
417  }
418 
419  pointIOField newPoints
420  (
421  IOobject
422  (
423  "points",
424  pointsInst,
425  meshSubDir,
426  *this,
429  false
430  )
431  );
432 
433  // Re-read tet base points
434  autoPtr<labelIOList> newTetBasePtIsPtr = readTetBasePtIs();
435 
436  // Update all geometry
437  updateGeomPoints(std::move(newPoints), newTetBasePtIsPtr);
438 
439  return polyMesh::POINTS_MOVED;
440  }
441  else
442  {
443  if (debug)
444  {
445  Info<< "No change" << endl;
446  }
447 
448  return polyMesh::UNCHANGED;
449  }
450 }
451 
452 
453 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
Foam::HashTable< regIOobject * >::size
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::polyBoundaryMesh
A polyBoundaryMesh is a polyPatch list with additional search methods and registered IO.
Definition: polyBoundaryMesh.H:63
Foam::polyMesh::POINTS_MOVED
Definition: polyMesh.H:93
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::polyMesh::meshSubDir
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:321
Foam::primitiveMesh::cells
const cellList & cells() const
Definition: primitiveMeshCells.C:138
Foam::polyMesh::facesInstance
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:852
Foam::polyMesh::clearOut
void clearOut()
Clear all geometry and addressing unnecessary for CFD.
Definition: polyMeshClear.C:222
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::PtrList::set
const T * set(const label i) const
Return const pointer to element (can be nullptr),.
Definition: PtrList.H:138
Foam::IOobject::headerClassName
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:83
polyMesh.H
Foam::labelIOList
IOList< label > labelIOList
Label container classes.
Definition: labelIOList.H:44
Foam::polyBoundaryMesh::types
wordList types() const
Return a list of patch types.
Definition: polyBoundaryMesh.C:561
Foam::polyBoundaryMesh::names
wordList names() const
Return a list of patch names.
Definition: polyBoundaryMesh.C:555
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::polyMesh::TOPO_PATCH_CHANGE
Definition: polyMesh.H:95
Foam::polyMesh::pointsInstance
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:846
Foam::IOobject::writeOption
writeOption
Enumeration defining the write options.
Definition: IOobject.H:192
Foam::pointIOField
vectorIOField pointIOField
pointIOField is a vectorIOField.
Definition: pointIOField.H:44
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
DebugInFunction
#define DebugInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:388
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:68
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:187
Foam::PtrList::setSize
void setSize(const label newLen)
Same as resize()
Definition: PtrList.H:151
Foam::ZoneMesh< pointZone, polyMesh >
Foam::CompactIOList
A List of objects of type <T> with automated input and output using a compact storage....
Definition: CompactIOList.H:55
Foam::polyMesh::UNCHANGED
Definition: polyMesh.H:92
Foam::IOobject::clone
autoPtr< IOobject > clone() const
Clone.
Definition: IOobject.H:468
Foam::polyMesh::TOPO_CHANGE
Definition: polyMesh.H:94
Foam::PtrList::clear
void clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition: PtrListI.H:97
Foam::polyMesh::meshDir
fileName meshDir() const
Return the local mesh directory (dbDir()/meshSubDir)
Definition: polyMesh.C:840
Foam::polyMesh::readUpdateState
readUpdateState
Enumeration defining the state of the mesh after a read update.
Definition: polyMesh.H:90
Foam::polyBoundaryMesh::updateMesh
void updateMesh()
Correct polyBoundaryMesh after topology update.
Definition: polyBoundaryMesh.C:1163
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::List< word >
Foam::boundBox
A bounding box defined in terms of min/max extrema points.
Definition: boundBox.H:63
Foam::polyMesh::updateGeomPoints
void updateGeomPoints(pointIOField &&newPoints, autoPtr< labelIOList > &newTetBasePtIsPtr)
Definition: polyMeshClear.C:75
Foam::faceCompactIOList
CompactIOList< face, label > faceCompactIOList
Definition: faceIOList.H:45
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
cellIOList.H
Foam::objectRegistry::time
const Time & time() const noexcept
Return time registry.
Definition: objectRegistry.H:178
Foam::polyMesh::setInstance
void setInstance(const fileName &instance, const IOobject::writeOption wOpt=IOobject::AUTO_WRITE)
Set the instance for mesh files.
Definition: polyMeshIO.C:36
Foam::polyMesh::readUpdate
virtual readUpdateState readUpdate()
Update the mesh based on the mesh files saved in.
Definition: polyMeshIO.C:75
Foam::IOobject::MUST_READ
Definition: IOobject.H:185