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-2021 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 
138 (
139  const IOobject& io,
140  pointField&& pointLst,
141  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  move(pointLst),
158  IOobject
159  (
160  "faces",
161  instance(),
162  meshSubDir,
163  *this,
166  ),
167  move(faceLst),
168  IOobject
169  (
170  "surfZones",
171  instance(),
172  meshSubDir,
173  *this,
176  ),
177  surfZoneList()
178  ),
179  MeshReference(this->storedIOFaces(), this->storedIOPoints())
180 {}
181 
182 
184 (
185  const IOobject& io,
186  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(false) << 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 null just to initialise 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  pointField&& points,
259  faceList&& faces,
260  surfZoneList&& zones,
261  const bool validate
262 )
263 {
264  // Clear addressing.
266 
267  Allocator::reset(move(points), move(faces), move(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 {
295  return dbDir()/meshSubDir;
296 }
297 
298 
300 {
301  return this->storedIOPoints().instance();
302 }
303 
304 
306 {
307  return this->storedIOFaces().instance();
308 }
309 
310 
312 {
313  return this->points().size();
314 }
315 
316 
318 {
319  return this->faces().size();
320 }
321 
322 
324 {
325  return this->storedIOPoints();
326 }
327 
328 
330 {
331  return this->storedIOFaces();
332 }
333 
334 
336 {
337  // extra safety, ensure we have at some zones
338  // and they cover all the faces - fix start silently
340 
341  if (zones.size() <= 1)
342  {
343  removeZones();
344  }
345  else
346  {
347  label count = 0;
348  forAll(zones, zoneI)
349  {
350  zones[zoneI].start() = count;
351  count += zones[zoneI].size();
352  }
353 
354  if (count < nFaces())
355  {
357  << "more faces " << nFaces() << " than zones " << count
358  << " ... extending final zone"
359  << endl;
360 
361  zones.last().size() += count - nFaces();
362  }
363  else if (count > size())
364  {
366  << "more zones " << count << " than faces " << nFaces()
367  << exit(FatalError);
368  }
369  }
370 }
371 
372 
373 // Add boundary patches. Constructor helper
375 (
376  const surfZoneList& srfZones,
377  const bool validate
378 )
379 {
381 
382  forAll(zones, zoneI)
383  {
384  zones[zoneI] = surfZone(srfZones[zoneI], zoneI);
385  }
386 
387  if (validate)
388  {
389  checkZones();
390  }
391 }
392 
393 
394 // Remove all files and some subdirs (eg, sets)
395 void Foam::surfMesh::removeFiles(const fileName& instanceDir) const
396 {
397  fileName meshFilesPath = db().path(instanceDir, meshSubDir);
398 
399  rm(meshFilesPath/"points");
400  rm(meshFilesPath/"faces");
401  rm(meshFilesPath/"surfZones");
402 }
403 
405 {
407 }
408 
409 
410 void Foam::surfMesh::write(const fileName& name, const surfMesh& surf)
411 {
413  (
414  surf.points(),
415  surf.faces(),
416  surf.surfZones()
417  ).write(name);
418 }
419 
420 
422 {
423  write(name, *this);
424 }
425 
426 
427 // ************************************************************************* //
virtual void updateRefs()
Update references to storedPoints/storedFaces.
Definition: surfMesh.C:91
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: surfMesh.C:305
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
virtual label size() const
Return number of faces.
Definition: surfMesh.H:198
const word & name() const
Return name.
Definition: IOobject.H:315
faceCompactIOList & storedIOFaces()
Non-const access to the faces.
A class for handling file names.
Definition: fileName.H:79
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:306
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: surfMesh.C:299
surfaceRegistry(const objectRegistry &, const word &surfName="")
Construct for the given objectRegistry and named surface.
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:164
List< face > faceList
Definition: faceListFwd.H:43
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
void addZones(const List< surfZone > &, const bool validate=true)
Add surface zones.
Definition: surfMesh.C:375
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
fileName path(const word &instance, const fileName &local="") const
Return complete path with alternative instance and local.
Pre-declare related SubField type.
Definition: Field.H:60
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:410
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
const pointField & points
List< Face > & storedFaces()
Non-const access to the faces.
Pre-declare SubField and related Field type.
Definition: Field.H:56
static word meshSubDir
Return the mesh sub-directory name (normally "surfMesh")
Definition: surfMesh.H:137
A class for handling words, derived from string.
Definition: word.H:59
virtual const faceList & faces() const
Return faces.
Definition: surfMesh.C:329
const fileName & local() const
Definition: IOobject.H:409
pointField & storedPoints()
Non-const access to global points.
fileName path(const bool global) const
Return complete path including the processor sub-directory.
Definition: IOobject.C:380
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:311
A helper class for storing points, faces and zones with IO capabilities.
surfMesh(const IOobject &, const word &surfName="")
Construct from IOobject, with alternative surface name.
Definition: surfMesh.C:100
A surface mesh consisting of general polygon faces.
Definition: surfMesh.H:55
static const char nl
Definition: Ostream.H:260
defineTypeNameAndDebug(combustionModel, 0)
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:73
void reset(pointField &&points, faceList &&faces, surfZoneList &&zones)
Reset primitive data (points, faces and zones)
void removeFiles() const
Remove all files from mesh instance()
Definition: surfMesh.C:404
void resetPrimitives(pointField &&points, faceList &&faces, surfZoneList &&zones, const bool validate=true)
Reset mesh primitive data.
Definition: surfMesh.C:257
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:52
Template functions to aid in the implementation of demand driven data.
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:211
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:317
void checkZones()
Check the surface zone definitions.
Definition: surfMesh.C:335
fileName & instance() const
Return the instance directory, constant, system, <time> etc.
Definition: IOobject.C:355
virtual const pointField & points() const
Return points.
Definition: surfMesh.C:323
virtual ~surfMesh()
Destructor.
Definition: surfMesh.C:247
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:1017
T & last()
Return the last element of the list.
Definition: UListI.H:128
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
fileName meshDir() const
Return the local mesh directory (dbDir()/meshSubDir)
Definition: surfMesh.C:293
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:98
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
Namespace for OpenFOAM.