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-2021 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 
468 void Foam::surfMesh::checkZones(const bool verbose)
469 {
470  auto& zones = this->storedZones();
471 
472  if (zones.size() <= 1)
473  {
474  removeZones();
475  return;
476  }
477 
478  // Check that zones cover all faces exactly
479  // - fix each start silently
480 
481  bool zonesTooBig(false);
482 
483  const label maxCount = this->nFaces();
484 
485  label start = 0;
486  for (surfZone& zn : zones)
487  {
488  zn.start() = start;
489  start += zn.size();
490  if (start > maxCount)
491  {
492  zonesTooBig = true; // Zones exceed what is required
493  zn.size() = (maxCount - zn.start());
494  start = (zn.start() + zn.size());
495  }
496  }
497 
498  if (!zones.empty())
499  {
500  surfZone& zn = zones.last();
501 
502  if ((zn.start() + zn.size()) < maxCount)
503  {
504  // Zones address less than expected - extend final zone
505  zn.size() += maxCount - zn.start();
506 
507  if (verbose)
508  {
510  << "Surface has more faces " << maxCount
511  << " than zone addressing ... extending final zone" << nl;
512  }
513  }
514  else if (zonesTooBig)
515  {
516  if (verbose)
517  {
519  << "Surface has more zone addressing than faces "
520  << maxCount
521  << " ... trucated/resized accordingly" << nl;
522  }
523  }
524  }
525 }
526 
527 
528 // Add boundary patches. Constructor helper
530 (
531  const surfZoneList& zones,
532  const bool validate
533 )
534 {
535  removeZones();
536 
537  forAll(surfZones_, zonei)
538  {
539  surfZones_[zonei] = surfZone(zones[zonei], zonei);
540  }
541 
542  if (validate)
543  {
544  checkZones();
545  }
546 }
547 
548 
549 // Remove all files and some subdirs (eg, sets)
550 void Foam::surfMesh::removeFiles(const fileName& instanceDir) const
551 {
552  fileName meshFilesPath = db().path()/instanceDir/meshSubDir;
553 
554  rm(meshFilesPath/"points");
555  rm(meshFilesPath/"faces");
556  rm(meshFilesPath/"surfZones");
557 }
558 
559 
561 {
562  removeFiles(instance());
563 }
564 
565 
567 (
568  const fileName& name,
569  IOstreamOption streamOpt,
570  const dictionary& options
571 ) const
572 {
573  write(name, name.ext(), streamOpt, options);
574 }
575 
576 
578 (
579  const fileName& name,
580  const word& fileType,
581  IOstreamOption streamOpt,
582  const dictionary& options
583 ) const
584 {
586  (
587  this->points(),
588  this->faces(),
589  this->surfZones()
590  ).write(name, fileType, streamOpt, options);
591 }
592 
593 
594 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
Foam::surfZone::start
label start() const
The start label of this zone in the face list.
Definition: surfZone.H:128
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:169
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:429
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::MeshedSurface::storedFaces
List< Face > & storedFaces()
Non-const access to the faces.
Definition: MeshedSurface.H:173
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:73
Foam::IOobject::instance
const fileName & instance() const noexcept
Definition: IOobjectI.H:196
Foam::fileName::path
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
Foam::MeshedSurface::storedPoints
pointField & storedPoints()
Non-const access to global points.
Definition: MeshedSurface.H:167
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::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:567
Foam::MeshedSurface::surfFaces
const List< Face > & surfFaces() const
Return const access to the faces.
Definition: MeshedSurface.H:413
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:1004
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
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::IOobject::writeOpt
writeOption writeOpt() const noexcept
The write option.
Definition: IOobjectI.H:179
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
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:187
Foam::MeshedSurface::storedZones
surfZoneList & storedZones()
Non-const access to the zones.
Definition: MeshedSurface.H:185
Foam::Field< vector >
Foam::IOobject::local
const fileName & local() const noexcept
Definition: IOobjectI.H:208
Foam::surfMesh::addZones
void addZones(const surfZoneList &zones, bool validate=true)
Add surface zones, optionally validating the zone coverage.
Definition: surfMesh.C:530
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::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::surfZone::size
label size() const
The size of this zone in the face list.
Definition: surfZone.H:140
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:560
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IOobject::readOpt
readOption readOpt() const noexcept
The read option.
Definition: IOobjectI.H:164
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::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
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
Foam::surfMesh::checkZones
void checkZones(const bool verbose=true)
Check the surface zone definitions.
Definition: surfMesh.C:468
Time.H
Foam::UList::shallowCopy
void shallowCopy(const UList< T > &list)
Copy the pointer and size held by the given UList.
Definition: UListI.H:272
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
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:382
Foam::nl
constexpr char nl
Definition: Ostream.H:404
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
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:116
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:36
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::surfMesh::releaseGeom
autoPtr< MeshedSurface< face > > releaseGeom()
Release the geometry and return as a MeshedSurface<face>.
Definition: surfMesh.C:407
meshedSurf.H
Foam::stringOps::validate
StringType validate(const std::string &str, const UnaryPredicate &accept, const bool invert=false)
Return a copy of the input string with validated characters.
Definition: stringOpsTemplates.C:71
Foam::MeshedSurface< face >
Foam::IOobject::NO_READ
Definition: IOobject.H:188
Foam::MeshedSurface::size
label size() const
The surface size is the number of faces.
Definition: MeshedSurface.H:407
Foam::IOobject::path
fileName path() const
The complete path.
Definition: IOobject.C:511
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:328
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 & >
Foam::IOobject::db
const objectRegistry & db() const noexcept
Return the local objectRegistry.
Definition: IOobject.C:487