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-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 "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  // Zones
310  meshPointZones newPointZones
311  (
312  IOobject
313  (
314  "pointZones",
315  facesInst,
316  meshSubDir,
317  *this,
320  false
321  ),
322  *this
323  );
324 
325  label oldSize = pointZones_.size();
326 
327  if (newPointZones.size() <= pointZones_.size())
328  {
329  pointZones_.setSize(newPointZones.size());
330  }
331 
332  // Reset existing ones
333  forAll(pointZones_, czI)
334  {
335  pointZones_[czI] = newPointZones[czI];
336  }
337 
338  // Extend with extra ones
339  pointZones_.setSize(newPointZones.size());
340 
341  for (label czI = oldSize; czI < newPointZones.size(); czI++)
342  {
343  pointZones_.set(czI, newPointZones[czI].clone(pointZones_));
344  }
345 
346  pointZones_.instance() = facesInst;
347 
348 
349  meshFaceZones newFaceZones
350  (
351  IOobject
352  (
353  "faceZones",
354  facesInst,
355  meshSubDir,
356  *this,
359  false
360  ),
361  *this
362  );
363 
364  oldSize = faceZones_.size();
365 
366  if (newFaceZones.size() <= faceZones_.size())
367  {
368  faceZones_.setSize(newFaceZones.size());
369  }
370 
371  // Reset existing ones
372  forAll(faceZones_, fzI)
373  {
374  faceZones_[fzI].resetAddressing
375  (
376  newFaceZones[fzI],
377  newFaceZones[fzI].flipMap()
378  );
379  }
380 
381  // Extend with extra ones
382  faceZones_.setSize(newFaceZones.size());
383 
384  for (label fzI = oldSize; fzI < newFaceZones.size(); fzI++)
385  {
386  faceZones_.set(fzI, newFaceZones[fzI].clone(faceZones_));
387  }
388 
389  faceZones_.instance() = facesInst;
390 
391 
392  meshCellZones newCellZones
393  (
394  IOobject
395  (
396  "cellZones",
397  facesInst,
398  meshSubDir,
399  *this,
402  false
403  ),
404  *this
405  );
406 
407  oldSize = cellZones_.size();
408 
409  if (newCellZones.size() <= cellZones_.size())
410  {
411  cellZones_.setSize(newCellZones.size());
412  }
413 
414  // Reset existing ones
415  forAll(cellZones_, czI)
416  {
417  cellZones_[czI] = newCellZones[czI];
418  }
419 
420  // Extend with extra ones
421  cellZones_.setSize(newCellZones.size());
422 
423  for (label czI = oldSize; czI < newCellZones.size(); czI++)
424  {
425  cellZones_.set(czI, newCellZones[czI].clone(cellZones_));
426  }
427 
428  cellZones_.instance() = facesInst;
429 
430 
431  // Re-read tet base points
432  tetBasePtIsPtr_ = readTetBasePtIs();
433 
434 
435  if (boundaryChanged)
436  {
438  }
439  else
440  {
441  return polyMesh::TOPO_CHANGE;
442  }
443  }
444  else if (pointsInst != pointsInstance())
445  {
446  // Points moved
447  if (debug)
448  {
449  Info<< "Point motion" << endl;
450  }
451 
452  clearGeom();
453 
454 
455  label nOldPoints = points_.size();
456 
457  points_.clear();
458 
459  pointIOField newPoints
460  (
461  IOobject
462  (
463  "points",
464  pointsInst,
465  meshSubDir,
466  *this,
469  false
470  )
471  );
472 
473  if (nOldPoints != 0 && nOldPoints != newPoints.size())
474  {
476  << "Point motion detected but number of points "
477  << newPoints.size() << " in "
478  << newPoints.objectPath() << " does not correspond to "
479  << " current " << nOldPoints
480  << exit(FatalError);
481  }
482 
483  points_.transfer(newPoints);
484 
485  points_.instance() = pointsInst;
486 
487  // Re-read tet base points
488  autoPtr<labelIOList> newTetBasePtIsPtr = readTetBasePtIs();
489  if (newTetBasePtIsPtr.valid())
490  {
491  tetBasePtIsPtr_ = newTetBasePtIsPtr;
492  }
493 
494  // Calculate the geometry for the patches (transformation tensors etc.)
495  boundary_.calcGeometry();
496 
497  // Derived info
498  bounds_ = boundBox(points_);
499 
500  // Rotation can cause direction vector to change
501  geometricD_ = Zero;
502  solutionD_ = Zero;
503 
504  return polyMesh::POINTS_MOVED;
505  }
506  else
507  {
508  if (debug)
509  {
510  Info<< "No change" << endl;
511  }
512 
513  return polyMesh::UNCHANGED;
514  }
515 }
516 
517 
519 (
523  const bool write
524 ) const
525 {
526  if (faces_.writeOpt() == AUTO_WRITE)
527  {
528  auto rmAddressing = [&](const word& name)
529  {
530  const IOobject faceProcAddressingIO
531  (
532  name,
533  facesInstance(),
534  meshSubDir,
535  *this
536  );
537 
538  rm(faceProcAddressingIO.objectPath(false));
539  };
540 
541  if (!Pstream::parRun())
542  {
543  rmAddressing("cellProc");
544  }
545  else
546  {
547  rmAddressing("pointProcAddressing");
548  rmAddressing("faceProcAddressing");
549  rmAddressing("cellProcAddressing");
550  }
551  }
552 
553  const bool written = objectRegistry::writeObject(fmt, ver, cmp, write);
554 
555  const_cast<polyMesh&>(*this).setTopologyWrite(IOobject::NO_WRITE);
556 
557  return written;
558 }
559 
560 
561 // ************************************************************************* //
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
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
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 a 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:91
@ TOPO_PATCH_CHANGE
Definition: polyMesh.H:95
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write=true) const
Write the underlying polyMesh.
Definition: polyMeshIO.C:519
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:159
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
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:251
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
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
static const char nl
Definition: Ostream.H:260
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488