surfMesh.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-2018 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 "surfMesh.H"
27 #include "MeshedSurfaceProxy.H"
28 
29 #include "Time.H"
30 #include "OSspecific.H"
31 #include "MeshedSurface.H"
32 #include "demandDrivenData.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 defineTypeNameAndDebug(surfMesh, 0);
39 }
40 
41 
42 Foam::word Foam::surfMesh::meshSubDir = "surfMesh";
43 
44 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
45 
46 // void Foam::surfMesh::oneZone()
47 // {
48 // word zoneName;
49 //
50 // surfZoneList& zones = Allocator::storedIOZones();
51 // if (zones.size())
52 // {
53 // zoneName = zones[0].name();
54 // }
55 // if (zoneName.empty())
56 // {
57 // zoneName = "zone0";
58 // }
59 //
60 // // set single default zone
61 // zones.setSize(1);
62 // zones[0] = surfZone
63 // (
64 // zoneName,
65 // nFaces(), // zone size
66 // 0, // zone start
67 // 0 // zone index
68 // );
69 // }
70 
71 
72 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
73 
75 {
76  // Assign the reference to the points (this is truly ugly)
77  reinterpret_cast<SubField<point>&>
78  (
79  const_cast<Field<point>&>(MeshReference::points())
80  ) = reinterpret_cast<SubField<point>&>(this->storedPoints());
81 }
82 
83 
85 {
86  // Assign the reference to the faces
87  shallowCopy(this->storedFaces());
88 }
89 
90 
92 {
93  this->updatePointsRef();
94  this->updateFacesRef();
95 }
96 
97 
98 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
99 
100 Foam::surfMesh::surfMesh(const IOobject& io, const word& surfName)
101 :
102  surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
103  Allocator
104  (
105  IOobject
106  (
107  "points",
108  time().findInstance(meshDir(), "points"),
109  meshSubDir,
110  *this,
111  IOobject::MUST_READ,
112  IOobject::NO_WRITE
113  ),
114  IOobject
115  (
116  "faces",
117  time().findInstance(meshDir(), "faces"),
118  meshSubDir,
119  *this,
120  IOobject::MUST_READ,
121  IOobject::NO_WRITE
122  ),
123  IOobject
124  (
125  "surfZones",
126  time().findInstance(meshDir(), "surfZones"),
127  meshSubDir,
128  *this,
129  IOobject::MUST_READ,
130  IOobject::NO_WRITE
131  )
132  ),
133  MeshReference(this->storedIOFaces(), this->storedIOPoints())
134 {}
135 
136 
137 Foam::surfMesh::surfMesh
138 (
139  const IOobject& io,
140  const Xfer<pointField>& pointLst,
141  const Xfer<faceList>& faceLst,
142  const word& surfName
143 )
144 :
145  surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
146  Allocator
147  (
148  IOobject
149  (
150  "points",
151  instance(),
152  meshSubDir,
153  *this,
156  ),
157  pointLst,
158  IOobject
159  (
160  "faces",
161  instance(),
162  meshSubDir,
163  *this,
166  ),
167  faceLst,
168  IOobject
169  (
170  "surfZones",
171  instance(),
172  meshSubDir,
173  *this,
176  ),
178  ),
179  MeshReference(this->storedIOFaces(), this->storedIOPoints())
180 {}
181 
182 
183 Foam::surfMesh::surfMesh
184 (
185  const IOobject& io,
186  const Xfer<MeshedSurface<face>>& surf,
187  const word& surfName
188 )
189 :
190  surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
191  Allocator
192  (
193  IOobject
194  (
195  "points",
196  instance(),
197  meshSubDir,
198  *this,
201  ),
202  pointField(),
203  IOobject
204  (
205  "faces",
206  instance(),
207  meshSubDir,
208  *this,
211  ),
212  faceList(),
213  IOobject
214  (
215  "surfZones",
216  instance(),
217  meshSubDir,
218  *this,
221  ),
222  surfZoneList()
223  ),
224  MeshReference(this->storedIOFaces(), this->storedIOPoints())
225 {
226  if (debug)
227  {
228  Info<<"IOobject: " << io.path() << nl
229  <<" name: " << io.name()
230  <<" instance: " << io.instance()
231  <<" local: " << io.local()
232  <<" dbDir: " << io.db().dbDir() << endl;
233  Info<<"creating surfMesh at instance " << instance() << endl;
234  Info<<"timeName: " << instance() << endl;
235  }
236 
237  // We can also send Xfer<..>::null just to initialize without allocating
238  if (notNull(surf))
239  {
240  transfer(surf());
241  }
242 }
243 
244 
245 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
246 
248 {
249  // clearOut();
250  // resetMotion();
251 }
252 
253 
254 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
255 
257 (
258  const Xfer<pointField>& points,
259  const Xfer<faceList>& faces,
260  const Xfer<surfZoneList>& zones,
261  const bool validate
262 )
263 {
264  // Clear addressing.
266 
267  Allocator::reset(points, faces, zones);
268  this->updateRefs();
269 
270  if (validate)
271  {
272  checkZones();
273  }
274 }
275 
276 
278 (
279  MeshedSurface<face>& surf
280 )
281 {
282  // Clear addressing.
284 
285  this->storedIOPoints().transfer(surf.storedPoints());
286  this->storedIOFaces().transfer(surf.storedFaces());
287  this->storedIOZones().transfer(surf.storedZones());
288 
289  this->updateRefs();
290 }
291 
292 
294 {
296 
297  xf().storedPoints().transfer(this->storedPoints());
298  xf().storedFaces().transfer(this->storedFaces());
299  xf().storedZones().transfer(this->storedZones());
300 
301  // is this needed?
302  this->updateRefs();
303 
304  // Clear addressing.
306 
307  return xf;
308 }
309 
310 
312 {
313  return dbDir()/meshSubDir;
314 }
315 
316 
318 {
319  return this->storedIOPoints().instance();
320 }
321 
322 
324 {
325  return this->storedIOFaces().instance();
326 }
327 
328 
330 {
331  return this->points().size();
332 }
333 
334 
336 {
337  return this->faces().size();
338 }
339 
340 
342 {
343  return this->storedIOPoints();
344 }
345 
346 
348 {
349  return this->storedIOFaces();
350 }
351 
352 
354 {
355  // extra safety, ensure we have at some zones
356  // and they cover all the faces - fix start silently
358 
359  if (zones.size() <= 1)
360  {
361  removeZones();
362  }
363  else
364  {
365  label count = 0;
366  forAll(zones, zoneI)
367  {
368  zones[zoneI].start() = count;
369  count += zones[zoneI].size();
370  }
371 
372  if (count < nFaces())
373  {
375  << "more faces " << nFaces() << " than zones " << count
376  << " ... extending final zone"
377  << endl;
378 
379  zones.last().size() += count - nFaces();
380  }
381  else if (count > size())
382  {
384  << "more zones " << count << " than faces " << nFaces()
385  << exit(FatalError);
386  }
387  }
388 }
389 
390 
391 // Add boundary patches. Constructor helper
393 (
394  const surfZoneList& srfZones,
395  const bool validate
396 )
397 {
399 
400  forAll(zones, zoneI)
401  {
402  zones[zoneI] = surfZone(srfZones[zoneI], zoneI);
403  }
404 
405  if (validate)
406  {
407  checkZones();
408  }
409 }
410 
411 
412 // Remove all files and some subdirs (eg, sets)
413 void Foam::surfMesh::removeFiles(const fileName& instanceDir) const
414 {
415  fileName meshFilesPath = db().path()/instanceDir/meshSubDir;
416 
417  rm(meshFilesPath/"points");
418  rm(meshFilesPath/"faces");
419  rm(meshFilesPath/"surfZones");
420 }
421 
423 {
425 }
426 
427 
428 void Foam::surfMesh::write(const fileName& name, const surfMesh& surf)
429 {
431  (
432  surf.points(),
433  surf.faces(),
434  surf.surfZones()
435  ).write(name);
436 }
437 
438 
440 {
441  write(name, *this);
442 }
443 
444 
445 // ************************************************************************* //
virtual void updateRefs()
Update references to storedPoints/storedFaces.
Definition: surfMesh.C:91
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: surfMesh.C:323
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
virtual label size() const
Return number of faces.
Definition: surfMesh.H:210
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
const word & name() const
Return name.
Definition: IOobject.H:297
faceCompactIOList & storedIOFaces()
Non-const access to the faces.
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
virtual void updatePointsRef()
Update references to storedPoints.
Definition: surfMesh.C:74
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
surfZoneList & storedZones()
Non-const access to the zones.
Definition: surfMesh.H:123
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: surfMesh.C:317
A surface zone on a MeshedSurface.
Definition: surfZone.H:62
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
List< face > faceList
Definition: faceListFwd.H:43
faceList & storedFaces()
Non-const access to the faces.
Definition: surfMesh.H:117
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
void addZones(const List< surfZone > &, const bool validate=true)
Add surface zones.
Definition: surfMesh.C:393
pointIOField & storedIOPoints()
Non-const access to the points.
surfZoneList & storedZones()
Non-const access to the zones.
static void write(const fileName &, const surfMesh &)
Write to file.
Definition: surfMesh.C:428
fileName path() const
Return complete path.
Definition: IOobject.C:397
Wraps the normal objectRegistry with a local instance for surfaces.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
Xfer< MeshedSurface< face > > xfer()
Transfer contents to the Xfer container as a MeshedSurface.
Definition: surfMesh.C:293
const pointField & points
List< Face > & storedFaces()
Non-const access to the faces.
static word meshSubDir
Return the mesh sub-directory name (normally "surfMesh")
Definition: surfMesh.H:152
A class for handling words, derived from string.
Definition: word.H:59
virtual const faceList & faces() const
Return faces.
Definition: surfMesh.C:347
const fileName & local() const
Definition: IOobject.H:402
pointField & storedPoints()
Non-const access to global points.
virtual void updateFacesRef()
Update references to storedFaces.
Definition: surfMesh.C:84
surfZoneIOList & storedIOZones()
Non-const access to the zones.
void removeZones()
Update the mesh corresponding to given map.
Definition: surfMeshClear.C:32
virtual label nPoints() const
Return the number of raw points.
Definition: surfMesh.C:329
A helper class for storing points, faces and zones with IO capabilities.
A surface mesh consisting of general polygon faces.
Definition: surfMesh.H:55
static const char nl
Definition: Ostream.H:265
defineTypeNameAndDebug(combustionModel, 0)
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:73
void removeFiles() const
Remove all files from mesh instance()
Definition: surfMesh.C:422
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
List< surfZone > surfZoneList
Definition: surfZoneList.H:45
bool notNull(const T &t)
Return true if t is not a reference to the nullObject of type T.
Definition: nullObjectI.H:46
Template functions to aid in the implementation of demand driven data.
const fileName & instance() const
Definition: IOobject.H:392
void transfer(MeshedSurface< face > &)
Transfer the contents of the argument and annul the argument.
Definition: surfMesh.C:278
#define WarningInFunction
Report a warning using Foam::Warning.
virtual const surfZoneList & surfZones() const
Return surface zones.
Definition: surfMesh.H:223
void reset(const Xfer< pointField > &points, const Xfer< faceList > &faces, const Xfer< surfZoneList > &zones)
Reset primitive data (points, faces and zones)
pointField & storedPoints()
Non-const access to global points.
Definition: surfMesh.H:111
messageStream Info
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to the time.
virtual label nFaces() const
Return the number of raw faces.
Definition: surfMesh.C:335
void checkZones()
Check the surface zone definitions.
Definition: surfMesh.C:353
virtual const pointField & points() const
Return points.
Definition: surfMesh.C:341
void resetPrimitives(const Xfer< pointField > &points, const Xfer< faceList > &faces, const Xfer< surfZoneList > &zones, const bool validate=true)
Reset mesh primitive data.
Definition: surfMesh.C:257
virtual ~surfMesh()
Destructor.
Definition: surfMesh.C:247
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:1008
T & last()
Return the last element of the list.
Definition: UListI.H:128
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:361
fileName meshDir() const
Return the local mesh directory (dbDir()/meshSubDir)
Definition: surfMesh.C:311
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
Namespace for OpenFOAM.