polyMeshIO.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-2024 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 "polyMesh.H"
27 #include "Time.H"
28 #include "cellIOList.H"
29 #include "OSspecific.H"
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
33 void Foam::polyMesh::setPointsWrite(const Foam::IOobject::writeOption wo)
34 {
35  points_.writeOpt() = wo;
36 
37  if (tetBasePtIsPtr_.valid())
38  {
39  tetBasePtIsPtr_->writeOpt() = wo;
40  }
41 }
42 
43 
44 void Foam::polyMesh::setTopologyWrite(const Foam::IOobject::writeOption wo)
45 {
46  setPointsWrite(wo);
47 
48  faces_.writeOpt() = wo;
49  owner_.writeOpt() = wo;
50  neighbour_.writeOpt() = wo;
51  boundary_.writeOpt() = wo;
52  pointZones_.writeOpt() = wo;
53  faceZones_.writeOpt() = wo;
54  cellZones_.writeOpt() = wo;
55 }
56 
57 
59 {
60  if (debug)
61  {
62  InfoInFunction << "Resetting points instance to " << inst << endl;
63  }
64 
65  points_.instance() = inst;
66  points_.eventNo() = getEvent();
67 
68  if (tetBasePtIsPtr_.valid())
69  {
70  tetBasePtIsPtr_->instance() = inst;
71  tetBasePtIsPtr_().eventNo() = getEvent();
72  }
73 
74  setPointsWrite(IOobject::AUTO_WRITE);
75 }
76 
77 
79 {
80  if (debug)
81  {
82  InfoInFunction << "Resetting topology instance to " << inst << endl;
83  }
84 
85  setPointsInstance(inst);
86 
87  faces_.instance() = inst;
88  owner_.instance() = inst;
89  neighbour_.instance() = inst;
90  boundary_.instance() = inst;
91  pointZones_.instance() = inst;
92  faceZones_.instance() = inst;
93  cellZones_.instance() = inst;
94 
95  setTopologyWrite(IOobject::AUTO_WRITE);
96 }
97 
98 
100 {
101  if (debug)
102  {
103  InfoInFunction << "Updating mesh based on saved data." << endl;
104  }
105 
106  // Find the points and faces instance
107  fileName pointsInst(time().findInstance(meshDir(), "points"));
108  fileName facesInst(time().findInstance(meshDir(), "faces"));
109 
110  if (debug)
111  {
112  Info<< "Faces instance: old = " << facesInstance()
113  << " new = " << facesInst << nl
114  << "Points instance: old = " << pointsInstance()
115  << " new = " << pointsInst << endl;
116  }
117 
118  if (facesInst != facesInstance())
119  {
120  // Topological change
121  if (debug)
122  {
123  Info<< "Topological change" << endl;
124  }
125 
126  clearOut();
127 
128  points_ = pointIOField
129  (
130  IOobject
131  (
132  "points",
133  pointsInst,
134  meshSubDir,
135  *this,
138  false
139  )
140  );
141 
142  points_.instance() = pointsInst;
143 
144  faces_ = faceCompactIOList
145  (
146  IOobject
147  (
148  "faces",
149  facesInst,
150  meshSubDir,
151  *this,
154  false
155  )
156  );
157 
158  faces_.instance() = facesInst;
159 
160  owner_ = labelIOList
161  (
162  IOobject
163  (
164  "owner",
165  facesInst,
166  meshSubDir,
167  *this,
170  false
171  )
172  );
173 
174  owner_.instance() = facesInst;
175 
176  neighbour_ = labelIOList
177  (
178  IOobject
179  (
180  "neighbour",
181  facesInst,
182  meshSubDir,
183  *this,
186  false
187  )
188  );
189 
190  neighbour_.instance() = facesInst;
191 
192  // Reset the boundary patches
193  polyBoundaryMesh newBoundary
194  (
195  IOobject
196  (
197  "boundary",
198  facesInst,
199  meshSubDir,
200  *this,
203  false
204  ),
205  *this
206  );
207 
208  // Check that patch types and names are unchanged
209  bool boundaryChanged = false;
210 
211  if (newBoundary.size() != boundary_.size())
212  {
213  boundaryChanged = true;
214  }
215  else
216  {
217  wordList newTypes = newBoundary.types();
218  wordList newNames = newBoundary.names();
219 
220  wordList oldTypes = boundary_.types();
221  wordList oldNames = boundary_.names();
222 
223  forAll(oldTypes, patchi)
224  {
225  if
226  (
227  oldTypes[patchi] != newTypes[patchi]
228  || oldNames[patchi] != newNames[patchi]
229  )
230  {
231  boundaryChanged = true;
232  break;
233  }
234  }
235  }
236 
237  if (boundaryChanged)
238  {
239  boundary_.clear();
240  boundary_.setSize(newBoundary.size());
241 
242  forAll(newBoundary, patchi)
243  {
244  boundary_.set(patchi, newBoundary[patchi].clone(boundary_));
245  }
246  }
247  else
248  {
249  forAll(boundary_, patchi)
250  {
251  boundary_[patchi] = polyPatch
252  (
253  newBoundary[patchi].name(),
254  newBoundary[patchi].size(),
255  newBoundary[patchi].start(),
256  patchi,
257  boundary_,
258  newBoundary[patchi].type()
259  );
260  }
261  }
262 
263  boundary_.instance() = facesInst;
264 
265 
266  // Boundary is set so can use initMesh now (uses boundary_ to
267  // determine internal and active faces)
268 
269  if (!owner_.headerClassName().empty())
270  {
271  initMesh();
272  }
273  else
274  {
276  (
277  IOobject
278  (
279  "cells",
280  facesInst,
281  meshSubDir,
282  *this,
285  false
286  )
287  );
288 
289  // Recalculate the owner/neighbour addressing and reset the
290  // primitiveMesh
291  initMesh(cells);
292  }
293 
294 
295  // Even if number of patches stayed same still recalculate boundary
296  // data.
297 
298  // Calculate topology for the patches (processor-processor comms etc.)
299  boundary_.topoChange();
300 
301  // Calculate the geometry for the patches (transformation tensors etc.)
302  boundary_.calcGeometry();
303 
304  // Derived info
305  bounds_ = boundBox(points_);
306  geometricD_ = Zero;
307  solutionD_ = Zero;
308 
309  // pointZones
310  {
311  pointZoneList newPointZones
312  (
313  IOobject
314  (
315  "pointZones",
316  facesInst,
317  meshSubDir,
318  *this,
321  false
322  ),
323  *this
324  );
325 
326  pointZones_.swap(newPointZones);
327  pointZones_.instance() = facesInst;
328  }
329 
330  // faceZones
331  {
332  faceZoneList newFaceZones
333  (
334  IOobject
335  (
336  "faceZones",
337  facesInst,
338  meshSubDir,
339  *this,
342  false
343  ),
344  *this
345  );
346 
347  faceZones_.swap(newFaceZones);
348  faceZones_.instance() = facesInst;
349  }
350 
351  // cellZones
352  {
353  cellZoneList newCellZones
354  (
355  IOobject
356  (
357  "cellZones",
358  facesInst,
359  meshSubDir,
360  *this,
363  false
364  ),
365  *this
366  );
367 
368  cellZones_.swap(newCellZones);
369  cellZones_.instance() = facesInst;
370  }
371 
372  // Re-read tet base points
373  tetBasePtIsPtr_ = readTetBasePtIs();
374 
375 
376  if (boundaryChanged)
377  {
379  }
380  else
381  {
382  return polyMesh::TOPO_CHANGE;
383  }
384  }
385  else if (pointsInst != pointsInstance())
386  {
387  // Points moved
388  if (debug)
389  {
390  Info<< "Point motion" << endl;
391  }
392 
393  clearGeom();
394 
395 
396  label nOldPoints = points_.size();
397 
398  points_.clear();
399 
400  pointIOField newPoints
401  (
402  IOobject
403  (
404  "points",
405  pointsInst,
406  meshSubDir,
407  *this,
410  false
411  )
412  );
413 
414  if (nOldPoints != 0 && nOldPoints != newPoints.size())
415  {
417  << "Point motion detected but number of points "
418  << newPoints.size() << " in "
419  << newPoints.objectPath() << " does not correspond to "
420  << " current " << nOldPoints
421  << exit(FatalError);
422  }
423 
424  points_.transfer(newPoints);
425 
426  points_.instance() = pointsInst;
427 
428  // Re-read tet base points
429  autoPtr<labelIOList> newTetBasePtIsPtr = readTetBasePtIs();
430  if (newTetBasePtIsPtr.valid())
431  {
432  tetBasePtIsPtr_ = newTetBasePtIsPtr;
433  }
434 
435  // Calculate the geometry for the patches (transformation tensors etc.)
436  boundary_.calcGeometry();
437 
438  // Derived info
439  bounds_ = boundBox(points_);
440 
441  // Rotation can cause direction vector to change
442  geometricD_ = Zero;
443  solutionD_ = Zero;
444 
445  return polyMesh::POINTS_MOVED;
446  }
447  else
448  {
449  if (debug)
450  {
451  Info<< "No change" << endl;
452  }
453 
454  return polyMesh::UNCHANGED;
455  }
456 }
457 
458 
460 (
464  const bool write
465 ) const
466 {
467  if (faces_.writeOpt() == AUTO_WRITE)
468  {
469  auto rmAddressing = [&](const word& name)
470  {
471  const IOobject faceProcAddressingIO
472  (
473  name,
474  facesInstance(),
475  meshSubDir,
476  *this
477  );
478 
479  rm(faceProcAddressingIO.objectPath(false));
480  };
481 
482  if (!Pstream::parRun())
483  {
484  rmAddressing("cellProc");
485  }
486  else
487  {
488  rmAddressing("pointProcAddressing");
489  rmAddressing("faceProcAddressing");
490  rmAddressing("cellProcAddressing");
491  }
492  }
493 
494  const bool written = objectRegistry::writeObject(fmt, ver, cmp, write);
495 
496  const_cast<polyMesh&>(*this).setTopologyWrite(IOobject::NO_WRITE);
497 
498  return written;
499 }
500 
501 
502 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A List of objects of type <Type> with automated input and output using a compact storage....
A primitive field of type <Type> with automated input and output.
Definition: IOField.H:53
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
writeOption writeOpt() const
Definition: IOobject.H:370
writeOption
Enumeration defining the write options.
Definition: IOobject.H:126
fileName objectPath(const bool global) const
Return complete path + object name including the processor.
Definition: IOobject.H:411
Version number type.
Definition: IOstream.H:97
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set)
Definition: autoPtrI.H:83
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:59
A class for handling file names.
Definition: fileName.H:82
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write) const
Write the objects.
Foam::polyBoundaryMesh.
wordList types() const
Return a list of patch types.
wordList names() const
Return the list of patch names.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
readUpdateState
Enumeration defining the state of the mesh after a read update.
Definition: polyMesh.H:90
@ TOPO_PATCH_CHANGE
Definition: polyMesh.H:94
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write=true) const
Write the underlying polyMesh.
Definition: polyMeshIO.C:460
readUpdateState readUpdate()
Update the mesh based on the mesh files saved in.
Definition: polyMeshIO.C:99
void setPointsInstance(const fileName &)
Set the instance for the points files.
Definition: polyMeshIO.C:58
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:78
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:70
fileName objectPath() const
Return complete path + object name.
Definition: regIOobject.H:158
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
label patchi
const cellShapeList & cells
#define InfoInFunction
Report an information message using Foam::Info.
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
static const zero Zero
Definition: zero.H:97
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
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:1017
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
CompactIOList< face > faceCompactIOList
Definition: faceIOList.H:44
messageStream Info
vectorIOField pointIOField
pointIOField is a vectorIOField.
Definition: pointIOField.H:42
T clone(const T &t)
Definition: List.H:55
IOList< label > labelIOList
Label container classes.
Definition: labelIOList.H:42
error FatalError
static const char nl
Definition: Ostream.H:266
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488