surfMesh.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-2016 OpenFOAM Foundation
9  Copyright (C) 2016-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 "surfMesh.H"
30 #include "meshedSurf.H"
31 #include "MeshedSurfaceProxy.H"
32 
33 #include "Time.H"
34 #include "OSspecific.H"
35 #include "MeshedSurface.H"
36 
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41  defineTypeNameAndDebug(surfMesh, 0);
42 }
43 
45 
46 
47 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48 
49 // void Foam::surfMesh::oneZone()
50 // {
51 // word zoneName;
52 //
53 // if (surfZones_.size())
54 // {
55 // zoneName = surfZones_[0].name();
56 // }
57 // if (zoneName.empty())
58 // {
59 // zoneName = "zone0";
60 // }
61 //
62 // // Set single default zone with nFaces
63 // surfZones_.resize(1);
64 // surfZones_[0] = surfZone(zoneName, nFaces());
65 // }
66 
67 
68 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
69 
71 {
72  // Synchronize UList reference to the faces
73  static_cast<MeshReference&>(*this).shallowCopy
74  (
75  this->storedFaces()
76  );
77 }
78 
79 
80 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
81 
82 Foam::surfMesh::surfMesh(const IOobject& io)
83 :
84  surfMesh(io, io.name())
85 {}
86 
87 
88 Foam::surfMesh::surfMesh(const IOobject& io, const word& surfName)
89 :
90  surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
91  Allocator
92  (
93  IOobject
94  (
95  "points",
96  time().findInstance(meshDir(), "points"),
97  meshSubDir,
98  *this,
99  IOobject::MUST_READ,
100  IOobject::NO_WRITE
101  ),
102  IOobject
103  (
104  "faces",
105  time().findInstance(meshDir(), "faces"),
106  meshSubDir,
107  *this,
108  IOobject::MUST_READ,
109  IOobject::NO_WRITE
110  )
111  ),
112  MeshReference(this->storedIOFaces(), this->storedIOPoints()),
113 
114  surfZones_
115  (
116  IOobject
117  (
118  "surfZones",
119  time().findInstance(meshDir(), "surfZones"),
120  meshSubDir,
121  *this,
122  IOobject::READ_IF_PRESENT,
123  IOobject::NO_WRITE
124  )
125  )
126 {}
127 
128 
129 Foam::surfMesh::surfMesh
130 (
131  const word& surfName,
132  const objectRegistry& obr
133 )
134 :
135  surfaceRegistry(obr, surfName),
136  Allocator
137  (
138  IOobject
139  (
140  "points",
141  instance(),
142  meshSubDir,
143  *this,
146  ),
147  IOobject
148  (
149  "faces",
150  instance(),
151  meshSubDir,
152  *this,
155  )
156  ),
157  MeshReference(this->storedIOFaces(), this->storedIOPoints()),
158 
159  surfZones_
160  (
161  IOobject
162  (
163  "surfZones",
164  instance(),
165  meshSubDir,
166  *this,
169  )
170  )
171 {}
172 
173 
174 Foam::surfMesh::surfMesh
175 (
176  const IOobject& io,
177  const MeshedSurface<face>& surf,
178  const word& surfName
179 )
180 :
181  surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
182  Allocator
183  (
184  IOobject
185  (
186  "points",
187  instance(),
188  meshSubDir,
189  *this,
191  io.writeOpt()
192  ),
193  IOobject
194  (
195  "faces",
196  instance(),
197  meshSubDir,
198  *this,
200  io.writeOpt()
201  )
202  ),
203  MeshReference(this->storedIOFaces(), this->storedIOPoints()),
204 
205  surfZones_
206  (
207  IOobject
208  (
209  "surfZones",
210  instance(),
211  meshSubDir,
212  *this,
214  io.writeOpt()
215  )
216  )
217 {
218  DebugInfo
219  <<"IOobject: " << io.path() << nl
220  <<" name: " << io.name()
221  <<" instance: " << io.instance()
222  <<" local: " << io.local()
223  <<" dbDir: " << io.db().dbDir() << nl
224  <<"creating surfMesh at instance " << instance() << endl;
225 
226  copySurface(surf);
227 }
228 
229 
230 Foam::surfMesh::surfMesh
231 (
232  const IOobject& io,
233  MeshedSurface<face>&& surf,
234  const word& surfName
235 )
236 :
237  surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
238  Allocator
239  (
240  IOobject
241  (
242  "points",
243  instance(),
244  meshSubDir,
245  *this,
246  io.readOpt(),
247  io.writeOpt()
248  ),
249  IOobject
250  (
251  "faces",
252  instance(),
253  meshSubDir,
254  *this,
255  io.readOpt(),
256  io.writeOpt()
257  )
258  ),
259  MeshReference(this->storedIOFaces(), this->storedIOPoints()),
260 
261  surfZones_
262  (
263  IOobject
264  (
265  "surfZones",
266  instance(),
267  meshSubDir,
268  *this,
269  io.readOpt(),
270  io.writeOpt()
271  )
272  )
273 {
274  DebugInfo
275  <<"IOobject: " << io.path() << nl
276  <<" name: " << io.name()
277  <<" instance: " << io.instance()
278  <<" local: " << io.local()
279  <<" dbDir: " << io.db().dbDir() << nl
280  <<"creating surfMesh at instance " << instance() << nl
281  <<"timeName: " << instance() << endl;
282 
283  transfer(surf);
284 }
285 
286 
287 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
288 
290 {
291  clearOut(); // Clear addressing
292 }
293 
294 
295 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
296 
298 (
299  const pointField& points,
300  const faceList& faces,
301  bool validate
302 )
303 {
304  clearOut(); // Clear addressing
305 
306  if
307  (
308  this->nPoints() != points.size()
309  || this->nFaces() != faces.size()
310  )
311  {
312  // Geometry changed
313  clearFields();
314  }
315 
316  this->storedIOPoints() = points;
317  this->storedIOFaces() = faces;
318  surfZones_.clear();
319 
320  this->updateRefs();
321 
322  // No zones
323 }
324 
325 
327 (
328  const meshedSurf& surf,
329  bool validate
330 )
331 {
332  clearOut(); // Clear addressing
333 
334  if
335  (
336  this->nPoints() != surf.points().size()
337  || this->nFaces() != surf.faces().size()
338  )
339  {
340  // Geometry changed
341  clearFields();
342  }
343 
344  this->storedIOPoints() = surf.points();
345  this->storedIOFaces() = surf.faces();
346  surfZones_.clear();
347 
348  this->updateRefs();
349 
350  // No zones
351 }
352 
353 
355 (
356  const MeshedSurface<face>& surf,
357  bool validate
358 )
359 {
360  clearOut(); // Clear addressing
361 
362  if
363  (
364  this->nPoints() != surf.points().size()
365  || this->nFaces() != surf.surfFaces().size()
366  )
367  {
368  // Geometry changed
369  clearFields();
370  }
371 
372  this->storedIOPoints() = surf.points();
373  this->storedIOFaces() = surf.surfFaces();
374  surfZones_ = surf.surfZones();
375 
376  this->updateRefs();
377 
378  if (validate)
379  {
380  checkZones();
381  }
382 }
383 
384 
386 (
387  MeshedSurface<face>& surf,
388  bool validate
389 )
390 {
391  clearOut(); // Clear addressing
392 
393  this->storedPoints().transfer(surf.storedPoints());
394  this->storedFaces().transfer(surf.storedFaces());
395  this->storedZones().transfer(surf.storedZones());
396 
397  this->updateRefs();
398 
399  if (validate)
400  {
401  checkZones();
402  }
403 }
404 
405 
408 {
409  clearOut(); // Clear addressing
410  clearFields();
411 
412  // Start with an empty geometry
413  auto aptr = autoPtr<MeshedSurface<face>>::New();
414 
415  // Transfer in content
416  aptr->storedPoints().transfer(this->storedPoints());
417  aptr->storedFaces().transfer(this->storedFaces());
418  aptr->storedZones().transfer(this->storedZones());
419 
420  this->updateRefs(); // This may not be needed...
421 
422  return aptr;
423 }
424 
425 
427 {
428  return dbDir()/meshSubDir;
429 }
430 
431 
433 {
434  return this->storedIOPoints().instance();
435 }
436 
437 
439 {
440  return this->storedIOFaces().instance();
441 }
442 
443 
444 Foam::label Foam::surfMesh::nPoints() const
445 {
446  return this->points().size();
447 }
448 
449 
450 Foam::label Foam::surfMesh::nFaces() const
451 {
452  return this->faces().size();
453 }
454 
455 
457 {
458  return this->storedIOPoints();
459 }
460 
461 
463 {
464  return this->storedIOFaces();
465 }
466 
467 
469 {
470  // Extra safety, ensure we have at some zones
471  // and they cover all the faces - fix start silently
472 
473  if (surfZones_.size() <= 1)
474  {
475  removeZones();
476  return;
477  }
478 
479  label count = 0;
480  for (surfZone& zn : surfZones_)
481  {
482  zn.start() = count;
483  count += zn.size();
484  }
485 
486  if (count < nFaces())
487  {
489  << "More faces " << nFaces() << " than zones " << count
490  << " ... extending final zone"
491  << endl;
492 
493  surfZones_.last().size() += count - nFaces();
494  }
495  else if (size() < count)
496  {
498  << "More zones " << count << " than faces " << nFaces()
499  << exit(FatalError);
500  }
501 }
502 
503 
504 // Add boundary patches. Constructor helper
506 (
507  const surfZoneList& zones,
508  const bool validate
509 )
510 {
511  removeZones();
512 
513  forAll(surfZones_, zonei)
514  {
515  surfZones_[zonei] = surfZone(zones[zonei], zonei);
516  }
517 
518  if (validate)
519  {
520  checkZones();
521  }
522 }
523 
524 
525 // Remove all files and some subdirs (eg, sets)
526 void Foam::surfMesh::removeFiles(const fileName& instanceDir) const
527 {
528  fileName meshFilesPath = db().path()/instanceDir/meshSubDir;
529 
530  rm(meshFilesPath/"points");
531  rm(meshFilesPath/"faces");
532  rm(meshFilesPath/"surfZones");
533 }
534 
535 
537 {
538  removeFiles(instance());
539 }
540 
541 
543 (
544  const fileName& name,
545  IOstreamOption streamOpt,
546  const dictionary& options
547 ) const
548 {
549  write(name, name.ext(), streamOpt, options);
550 }
551 
552 
554 (
555  const fileName& name,
556  const word& fileType,
557  IOstreamOption streamOpt,
558  const dictionary& options
559 ) const
560 {
562  (
563  this->points(),
564  this->faces(),
565  this->surfZones()
566  ).write(name, fileType, streamOpt, options);
567 }
568 
569 
570 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
Foam::surfMesh::pointsInstance
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: surfMesh.C:432
Foam::Detail::MeshedSurfaceIOAllocator
A helper class for storing points and faces with IO capabilities.
Definition: MeshedSurfaceIOAllocator.H:54
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
MeshedSurfaceProxy.H
Foam::MeshedSurface::surfZones
const surfZoneList & surfZones() const
Const access to the surface zones.
Definition: MeshedSurface.H:427
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:70
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::MeshedSurface::storedFaces
List< Face > & storedFaces()
Non-const access to the faces.
Definition: MeshedSurface.H:171
Foam::surfaceRegistry
Wraps the normal objectRegistry with a local instance for surfaces.
Definition: surfaceRegistry.H:51
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::fileName::path
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:186
Foam::MeshedSurface::storedPoints
pointField & storedPoints()
Non-const access to global points.
Definition: MeshedSurface.H:165
Foam::surfMesh::storedFaces
faceList & storedFaces()
Non-const access to the faces.
Definition: surfMesh.H:123
Foam::surfMesh::updateRefs
void updateRefs()
Update point/face references.
Definition: surfMesh.C:70
Foam::IOobject::instance
const fileName & instance() const
Definition: IOobjectI.H:191
validate
thermo validate(args.executable(), "h")
Foam::meshedSurf::faces
virtual const faceList & faces() const =0
The faces used for the surface.
Foam::meshedSurf
Abstract definition of a meshed surface defined by faces and points.
Definition: meshedSurf.H:49
Foam::surfMesh::write
void write(const fileName &name, IOstreamOption streamOpt=IOstreamOption(), const dictionary &options=dictionary::null) const
Write to file, choosing writer based on its extension.
Definition: surfMesh.C:543
Foam::MeshedSurface::surfFaces
const List< Face > & surfFaces() const
Return const access to the faces.
Definition: MeshedSurface.H:411
Foam::surfMesh
A surface mesh consisting of general polygon faces that has IO capabilities and a registry for storin...
Definition: surfMesh.H:63
Foam::rm
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:994
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::word::ext
word ext() const
Return file name extension (part after last .)
Definition: word.C:126
Foam::MeshedSurfaceProxy::write
static void write(const fileName &name, const MeshedSurfaceProxy &surf, IOstreamOption streamOpt=IOstreamOption(), const dictionary &options=dictionary::null)
Write to file, select based on its extension.
Definition: MeshedSurfaceProxy.C:64
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:82
Foam::surfMesh::meshSubDir
static word meshSubDir
Return the mesh sub-directory name (normally "surfMesh")
Definition: surfMesh.H:153
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::IOobject::db
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:457
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::objectRegistry::dbDir
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to the time.
Definition: objectRegistry.H:198
Foam::MeshedSurface::storedZones
surfZoneList & storedZones()
Non-const access to the zones.
Definition: MeshedSurface.H:183
Foam::IOobject::local
const fileName & local() const
Definition: IOobjectI.H:203
Foam::Field< vector >
Foam::IOobject::writeOpt
writeOption writeOpt() const
The write option.
Definition: IOobjectI.H:177
Foam::surfMesh::addZones
void addZones(const surfZoneList &zones, bool validate=true)
Add surface zones, optionally validating the zone coverage.
Definition: surfMesh.C:506
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::surfMesh::faces
virtual const faceList & faces() const
Return faces.
Definition: surfMesh.C:462
Foam::meshedSurf::points
virtual const pointField & points() const =0
The points used for the surface.
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
surfMesh.H
Foam::surfMesh::nPoints
virtual label nPoints() const
Return the number of raw points.
Definition: surfMesh.C:444
Foam::surfMesh::removeFiles
void removeFiles() const
Remove all files from mesh instance()
Definition: surfMesh.C:536
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::surfMesh::transfer
void transfer(MeshedSurface< face > &surf, bool validate=false)
Transfer the contents of the argument and annul the argument.
Definition: surfMesh.C:386
Time.H
Foam::UList::shallowCopy
void shallowCopy(const UList< T > &list)
Copy the pointer held by the given UList.
Definition: UListI.H:219
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::surfMesh::~surfMesh
virtual ~surfMesh()
Destructor.
Definition: surfMesh.C:289
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::surfMesh::points
virtual const pointField & points() const
Return points.
Definition: surfMesh.C:456
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:359
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::surfMesh::meshDir
fileName meshDir() const
Return the local mesh directory (dbDir()/meshSubDir)
Definition: surfMesh.C:426
Foam::List< face >
Foam::surfZone
A surface zone on a MeshedSurface.
Definition: surfZone.H:56
Foam::IOobject::readOpt
readOption readOpt() const
The read option.
Definition: IOobjectI.H:165
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:115
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:35
Foam::surfMesh::releaseGeom
autoPtr< MeshedSurface< face > > releaseGeom()
Release the geometry and return as a MeshedSurface<face>.
Definition: surfMesh.C:407
meshedSurf.H
Foam::MeshedSurface< face >
Foam::IOobject::NO_READ
Definition: IOobject.H:123
Foam::surfMesh::checkZones
void checkZones()
Check the surface zone definitions.
Definition: surfMesh.C:468
Foam::MeshedSurface::size
label size() const
The surface size is the number of faces.
Definition: MeshedSurface.H:405
Foam::IOobject::path
fileName path() const
The complete path.
Definition: IOobject.C:481
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::surfMesh::nFaces
virtual label nFaces() const
Return the number of raw faces.
Definition: surfMesh.C:450
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:303
Foam::surfMesh::copySurface
void copySurface(const pointField &points, const faceList &faces, bool validate=false)
Update with new contents.
Definition: surfMesh.C:298
Foam::surfMesh::facesInstance
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: surfMesh.C:438
MeshedSurface.H
Foam::PrimitivePatch<::Foam::UList< face >, const pointField & >