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-2019 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 #include "demandDrivenData.H"
37 
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 
40 namespace Foam
41 {
42  defineTypeNameAndDebug(surfMesh, 0);
43 }
44 
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
63 // surfZones_.resize(1);
64 // surfZones_[0] = surfZone
65 // (
66 // zoneName,
67 // nFaces(), // zone size
68 // 0, // zone start
69 // 0 // zone index
70 // );
71 // }
72 
73 
74 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
75 
76 Foam::surfMesh::surfMesh(const IOobject& io)
77 :
78  surfMesh(io, io.name())
79 {}
80 
81 
82 Foam::surfMesh::surfMesh(const IOobject& io, const word& surfName)
83 :
84  surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
85  Allocator
86  (
87  IOobject
88  (
89  "points",
90  time().findInstance(meshDir(), "points"),
91  meshSubDir,
92  *this,
93  IOobject::MUST_READ,
94  IOobject::NO_WRITE
95  ),
96  IOobject
97  (
98  "faces",
99  time().findInstance(meshDir(), "faces"),
100  meshSubDir,
101  *this,
102  IOobject::MUST_READ,
103  IOobject::NO_WRITE
104  )
105  ),
106  MeshReference(this->storedIOFaces(), this->storedIOPoints()),
107 
108  surfZones_
109  (
110  IOobject
111  (
112  "surfZones",
113  time().findInstance(meshDir(), "surfZones"),
114  meshSubDir,
115  *this,
116  IOobject::READ_IF_PRESENT,
117  IOobject::NO_WRITE
118  )
119  )
120 {}
121 
122 
123 Foam::surfMesh::surfMesh
124 (
125  const word& surfName,
126  const objectRegistry& obr
127 )
128 :
129  surfaceRegistry(obr, surfName),
130  Allocator
131  (
132  IOobject
133  (
134  "points",
135  instance(),
136  meshSubDir,
137  *this,
140  ),
141  IOobject
142  (
143  "faces",
144  instance(),
145  meshSubDir,
146  *this,
149  )
150  ),
151  MeshReference(this->storedIOFaces(), this->storedIOPoints()),
152 
153  surfZones_
154  (
155  IOobject
156  (
157  "surfZones",
158  instance(),
159  meshSubDir,
160  *this,
163  )
164  )
165 {}
166 
167 
168 Foam::surfMesh::surfMesh
169 (
170  const IOobject& io,
171  const MeshedSurface<face>& surf,
172  const word& surfName
173 )
174 :
175  surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
176  Allocator
177  (
178  IOobject
179  (
180  "points",
181  instance(),
182  meshSubDir,
183  *this,
185  io.writeOpt()
186  ),
187  IOobject
188  (
189  "faces",
190  instance(),
191  meshSubDir,
192  *this,
194  io.writeOpt()
195  )
196  ),
197  MeshReference(this->storedIOFaces(), this->storedIOPoints()),
198 
199  surfZones_
200  (
201  IOobject
202  (
203  "surfZones",
204  instance(),
205  meshSubDir,
206  *this,
208  io.writeOpt()
209  )
210  )
211 {
212  DebugInfo
213  <<"IOobject: " << io.path() << nl
214  <<" name: " << io.name()
215  <<" instance: " << io.instance()
216  <<" local: " << io.local()
217  <<" dbDir: " << io.db().dbDir() << nl
218  <<"creating surfMesh at instance " << instance() << endl;
219 
220  copySurface(surf);
221 }
222 
223 
224 Foam::surfMesh::surfMesh
225 (
226  const IOobject& io,
227  MeshedSurface<face>&& surf,
228  const word& surfName
229 )
230 :
231  surfaceRegistry(io.db(), (surfName.size() ? surfName : io.name())),
232  Allocator
233  (
234  IOobject
235  (
236  "points",
237  instance(),
238  meshSubDir,
239  *this,
240  io.readOpt(),
241  io.writeOpt()
242  ),
243  IOobject
244  (
245  "faces",
246  instance(),
247  meshSubDir,
248  *this,
249  io.readOpt(),
250  io.writeOpt()
251  )
252  ),
253  MeshReference(this->storedIOFaces(), this->storedIOPoints()),
254 
255  surfZones_
256  (
257  IOobject
258  (
259  "surfZones",
260  instance(),
261  meshSubDir,
262  *this,
263  io.readOpt(),
264  io.writeOpt()
265  )
266  )
267 {
268  DebugInfo
269  <<"IOobject: " << io.path() << nl
270  <<" name: " << io.name()
271  <<" instance: " << io.instance()
272  <<" local: " << io.local()
273  <<" dbDir: " << io.db().dbDir() << nl
274  <<"creating surfMesh at instance " << instance() << nl
275  <<"timeName: " << instance() << endl;
276 
277  transfer(surf);
278 }
279 
280 
281 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
282 
284 {
285  clearOut(); // Clear addressing
286 }
287 
288 
289 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
290 
292 {
293  // Assign the reference to the points (quite ugly)
294  // points() are returned as Field but are actually stored as SubField
295  reinterpret_cast<typename MeshReference::PointFieldType&>
296  (
297  const_cast<Field<point>&>(MeshReference::points())
298  ).shallowCopy
299  (
300  this->storedPoints()
301  );
302 }
303 
304 
306 {
307  // Assign the reference to the faces (UList)
308  static_cast<MeshReference&>(*this).shallowCopy
309  (
310  this->storedFaces()
311  );
312 }
313 
314 
316 {
317  this->updatePointsRef();
318  this->updateFacesRef();
319 }
320 
321 
323 (
324  const pointField& points,
325  const faceList& faces,
326  bool validate
327 )
328 {
329  clearOut(); // Clear addressing
330 
331  if
332  (
333  this->nPoints() != points.size()
334  || this->nFaces() != faces.size()
335  )
336  {
337  // Geometry changed
338  clearFields();
339  }
340 
341  this->storedIOPoints() = points;
342  this->storedIOFaces() = faces;
343  surfZones_.clear();
344 
345  this->updateRefs();
346 
347  // No zones
348 }
349 
350 
352 (
353  const meshedSurf& surf,
354  bool validate
355 )
356 {
357  clearOut(); // Clear addressing
358 
359  if
360  (
361  this->nPoints() != surf.points().size()
362  || this->nFaces() != surf.faces().size()
363  )
364  {
365  // Geometry changed
366  clearFields();
367  }
368 
369  this->storedIOPoints() = surf.points();
370  this->storedIOFaces() = surf.faces();
371  surfZones_.clear();
372 
373  this->updateRefs();
374 
375  // No zones
376 }
377 
378 
380 (
381  const MeshedSurface<face>& surf,
382  bool validate
383 )
384 {
385  clearOut(); // Clear addressing
386 
387  if
388  (
389  this->nPoints() != surf.points().size()
390  || this->nFaces() != surf.surfFaces().size()
391  )
392  {
393  // Geometry changed
394  clearFields();
395  }
396 
397  this->storedIOPoints() = surf.points();
398  this->storedIOFaces() = surf.surfFaces();
399  surfZones_ = surf.surfZones();
400 
401  this->updateRefs();
402 
403  if (validate)
404  {
405  checkZones();
406  }
407 }
408 
409 
411 (
412  MeshedSurface<face>& surf,
413  bool validate
414 )
415 {
416  clearOut(); // Clear addressing
417 
418  this->storedPoints().transfer(surf.storedPoints());
419  this->storedFaces().transfer(surf.storedFaces());
420  this->storedZones().transfer(surf.storedZones());
421 
422  this->updateRefs();
423 
424  if (validate)
425  {
426  checkZones();
427  }
428 }
429 
430 
433 {
434  // Start with an empty geometry
435  auto aptr = autoPtr<MeshedSurface<face>>::New();
436 
437  // Transfer in content
438  aptr->storedPoints().transfer(this->storedPoints());
439  aptr->storedFaces().transfer(this->storedFaces());
440  aptr->storedZones().transfer(this->storedZones());
441 
442  this->updateRefs(); // This may not be needed...
443  clearOut(); // Clear addressing.
444  clearFields();
445 
446  return aptr;
447 }
448 
449 
451 {
452  return dbDir()/meshSubDir;
453 }
454 
455 
457 {
458  return this->storedIOPoints().instance();
459 }
460 
461 
463 {
464  return this->storedIOFaces().instance();
465 }
466 
467 
469 {
470  return this->points().size();
471 }
472 
473 
475 {
476  return this->faces().size();
477 }
478 
479 
481 {
482  return this->storedIOPoints();
483 }
484 
485 
487 {
488  return this->storedIOFaces();
489 }
490 
491 
493 {
494  // Extra safety, ensure we have at some zones
495  // and they cover all the faces - fix start silently
496 
497  if (surfZones_.size() <= 1)
498  {
499  removeZones();
500  return;
501  }
502 
503  label count = 0;
504  for (surfZone& zn : surfZones_)
505  {
506  zn.start() = count;
507  count += zn.size();
508  }
509 
510  if (count < nFaces())
511  {
513  << "More faces " << nFaces() << " than zones " << count
514  << " ... extending final zone"
515  << endl;
516 
517  surfZones_.last().size() += count - nFaces();
518  }
519  else if (size() < count)
520  {
522  << "More zones " << count << " than faces " << nFaces()
523  << exit(FatalError);
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  const dictionary& options
570 ) const
571 {
572  write(name, name.ext(), options);
573 }
574 
575 
577 (
578  const fileName& name,
579  const word& ext,
580  const dictionary& options
581 ) const
582 {
584  (
585  this->points(),
586  this->faces(),
587  this->surfZones()
588  ).write(name, ext, options);
589 }
590 
591 
592 // ************************************************************************* //
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:456
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
Foam::surfMesh::updatePointsRef
virtual void updatePointsRef()
Update references to storedPoints.
Definition: surfMesh.C:291
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:370
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:46
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:157
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:151
demandDrivenData.H
Template functions to aid in the implementation of demand driven data.
Foam::surfMesh::updateRefs
virtual void updateRefs()
Update references to storedPoints/storedFaces.
Definition: surfMesh.C:315
Foam::IOobject::instance
const fileName & instance() const
Definition: IOobjectI.H:167
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::MeshedSurface::surfFaces
const List< Face > & surfFaces() const
Return const access to the faces.
Definition: MeshedSurface.H:362
Foam::surfMesh
A surface mesh consisting of general polygon faces.
Definition: surfMesh.H:64
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:337
Foam::word::ext
word ext() const
Return file name extension (part after last .)
Definition: word.C:126
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:78
Foam::surfMesh::meshSubDir
static word meshSubDir
Return the mesh sub-directory name (normally "surfMesh")
Definition: surfMesh.H:164
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::IOobject::db
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:432
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::SubField< point >
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:163
Foam::IOobject::local
const fileName & local() const
Definition: IOobjectI.H:179
Foam::MeshedSurfaceProxy::write
static void write(const fileName &name, const MeshedSurfaceProxy &surf, const dictionary &options)
Write to file, select based on its extension.
Definition: MeshedSurfaceProxy.C:62
Foam::label
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:62
Foam::Field< point >
Foam::IOobject::writeOpt
writeOption writeOpt() const
The write option.
Definition: IOobjectI.H:153
Foam::surfMesh::addZones
void addZones(const surfZoneList &zones, bool validate=true)
Add surface zones, optionally validating the zone coverage.
Definition: surfMesh.C:530
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::surfMesh::faces
virtual const faceList & faces() const
Return faces.
Definition: surfMesh.C:486
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:468
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::surfMesh::updateFacesRef
virtual void updateFacesRef()
Update references to storedFaces.
Definition: surfMesh.C:305
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:411
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:283
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::surfMesh::points
virtual const pointField & points() const
Return points.
Definition: surfMesh.C:480
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:350
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:74
Foam::surfMesh::meshDir
fileName meshDir() const
Return the local mesh directory (dbDir()/meshSubDir)
Definition: surfMesh.C:450
Foam::List< face >
Foam::surfZone
A surface zone on a MeshedSurface.
Definition: surfZone.H:65
Foam::IOobject::readOpt
readOption readOpt() const
The read option.
Definition: IOobjectI.H:141
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:432
meshedSurf.H
Foam::surfMesh::write
void write(const fileName &name, const dictionary &options=dictionary::null) const
Write to file, choosing writer based on its extension.
Definition: surfMesh.C:567
Foam::MeshedSurface< face >
Foam::IOobject::NO_READ
Definition: IOobject.H:123
Foam::surfMesh::checkZones
void checkZones()
Check the surface zone definitions.
Definition: surfMesh.C:492
Foam::MeshedSurface::size
label size() const
The surface size is the number of faces.
Definition: MeshedSurface.H:356
Foam::IOobject::path
fileName path() const
The complete path.
Definition: IOobject.C:456
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::surfMesh::nFaces
virtual label nFaces() const
Return the number of raw faces.
Definition: surfMesh.C:474
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::surfMesh::copySurface
void copySurface(const pointField &points, const faceList &faces, bool validate=false)
Update with new contents.
Definition: surfMesh.C:323
Foam::surfMesh::facesInstance
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: surfMesh.C:462
MeshedSurface.H
Foam::PrimitivePatch< face, ::Foam::UList, ::Foam::SubField< point >, point >