MeshedSurfaceProxy.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 "MeshedSurfaceProxy.H"
30 #include "Time.H"
31 #include "ListOps.H"
32 #include "surfMesh.H"
33 #include "OFstream.H"
34 #include "faceTraits.H"
35 
36 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
37 
38 template<class Face>
40 {
41  return wordHashSet(*writefileExtensionMemberFunctionTablePtr_);
42 }
43 
44 
45 template<class Face>
47 (
48  const word& fileType,
49  bool verbose
50 )
51 {
52  return fileFormats::surfaceFormatsCore::checkSupport
53  (
54  writeTypes(),
55  fileType,
56  verbose,
57  "writing"
58  );
59 }
60 
61 
62 template<class Face>
64 (
65  const fileName& name,
66  const MeshedSurfaceProxy& surf,
67  IOstreamOption streamOpt,
68  const dictionary& options
69 )
70 {
71  write(name, name.ext(), surf, streamOpt, options);
72 }
73 
74 
75 template<class Face>
77 (
78  const fileName& name,
79  const word& fileType,
80  const MeshedSurfaceProxy& surf,
81  IOstreamOption streamOpt,
82  const dictionary& options
83 )
84 {
85  if (fileType.empty())
86  {
87  // Handle empty/missing type
88 
89  const word ext(name.ext());
90 
91  if (ext.empty())
92  {
94  << "Cannot determine format from filename" << nl
95  << " " << name << nl
96  << exit(FatalError);
97  }
98 
99  write(name, ext, surf, streamOpt, options);
100  return;
101  }
102 
103 
104  DebugInFunction << "Writing to " << name << nl;
105 
106  auto* mfuncPtr = writefileExtensionMemberFunctionTable(fileType);
107 
108  if (!mfuncPtr)
109  {
111  << "Unknown file type " << fileType << nl << nl
112  << "Valid types:" << nl
113  << flatOutput(writeTypes().sortedToc()) << nl
114  << exit(FatalError);
115  }
116 
117  mfuncPtr(name, surf, streamOpt, options);
118 }
119 
120 
121 template<class Face>
123 (
124  const Time& t,
125  const word& surfName
126 ) const
127 {
128  // the surface name to be used
129  const word name(surfName.size() ? surfName : surfaceRegistry::defaultName);
130 
131  DebugInFunction << "Writing to " << name << endl;
132 
133 
134  // The local location
135  const fileName objectDir
136  (
137  t.timePath()/surfaceRegistry::prefix/name/surfMesh::meshSubDir
138  );
139 
140  if (!isDir(objectDir))
141  {
142  mkDir(objectDir);
143  }
144 
145 
146  // Write surfMesh/points
147  {
148  pointIOField io
149  (
150  IOobject
151  (
152  "points",
153  t.timeName(),
154  surfMesh::meshSubDir,
155  t,
156  IOobject::NO_READ,
157  IOobject::NO_WRITE,
158  false
159  )
160  );
161 
162  OFstream os(objectDir/io.name(), t.writeStreamOption());
163 
164  io.writeHeader(os);
165 
166  os << this->points();
167 
168  io.writeEndDivider(os);
169  }
170 
171 
172  // Write surfMesh/faces
173  {
175  (
176  IOobject
177  (
178  "faces",
179  t.timeName(),
180  surfMesh::meshSubDir,
181  t,
182  IOobject::NO_READ,
183  IOobject::NO_WRITE,
184  false
185  )
186  );
187 
188  OFstream os(objectDir/io.name(), t.writeStreamOption());
189 
190  io.writeHeader(os);
191 
192  if (this->useFaceMap())
193  {
194  os << UIndirectList<Face>(this->surfFaces(), this->faceMap());
195  }
196  else
197  {
198  os << this->surfFaces();
199  }
200 
201  io.writeEndDivider(os);
202  }
203 
204 
205  // Write surfMesh/surfZones
206  {
207  surfZoneIOList io
208  (
209  IOobject
210  (
211  "surfZones",
212  t.timeName(),
213  surfMesh::meshSubDir,
214  t,
215  IOobject::NO_READ,
216  IOobject::NO_WRITE,
217  false
218  )
219  );
220 
221  // Write as ASCII-only
222  OFstream os(objectDir/io.name());
223 
224  io.writeHeader(os);
225 
226  os << this->surfZones();
227 
228  io.writeEndDivider(os);
229  }
230 }
231 
232 
233 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
234 
235 template<class Face>
237 (
238  const pointField& pointLst,
239  const UList<Face>& faceLst,
240  const UList<surfZone>& zoneLst,
241  const labelUList& faceMap,
242  const labelUList& faceIdsLst
243 )
244 :
245  points_(pointLst),
246  faces_(faceLst),
247  zones_(zoneLst),
248  faceMap_(faceMap),
249  faceIds_(faceIdsLst)
250 {}
251 
252 
253 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
254 
255 template<class Face>
257 {
259  {
260  return this->size();
261  }
262 
263  label nTri = 0;
264  for (const auto& f : faces_)
265  {
266  nTri += f.nTriangles();
267  }
268 
269  return nTri;
270 }
271 
272 
273 // ************************************************************************* //
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::MeshedSurfaceProxy::MeshedSurfaceProxy
MeshedSurfaceProxy(const pointField &pointLst, const UList< Face > &faceLst, const UList< surfZone > &zoneLst=UList< surfZone >::null(), const labelUList &faceMap=labelUList::null(), const labelUList &faceIdLst=labelUList::null())
Construct from component references.
Definition: MeshedSurfaceProxy.C:237
MeshedSurfaceProxy.H
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeTopological.C:94
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::MeshedSurfaceProxy::nTriangles
label nTriangles() const
Count number of triangles.
Definition: MeshedSurfaceProxy.C:256
Foam::IOobject::writeEndDivider
static Ostream & writeEndDivider(Ostream &os)
Write the standard end file divider.
Definition: IOobjectWriteHeader.C:142
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
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::HashSet< word, Hash< word > >
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:82
OFstream.H
Foam::faceTraits
Traits class for faces.
Definition: faceTraits.H:50
Foam::surfZoneIOList
IOobject for a surfZoneList.
Definition: surfZoneIOList.H:53
Foam::Field< vector >
DebugInFunction
#define DebugInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:388
Foam::Time::timePath
fileName timePath() const
Return current time path.
Definition: Time.H:375
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::CompactIOList< face, label >
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:123
os
OBJstream os(runTime.globalPath()/outputName)
surfMesh.H
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Time.H
Foam::MeshedSurfaceProxy::writeTypes
static wordHashSet writeTypes()
The file format types that can be written via MeshedSurfaceProxy.
Definition: MeshedSurfaceProxy.C:39
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
f
labelList f(nPoints)
Foam::UList< Face >
points
const pointField & points
Definition: gmvOutputHeader.H:1
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::wordHashSet
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition: HashSet.H:77
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::IOobject::writeHeader
bool writeHeader(Ostream &os) const
Write header with current type()
Definition: IOobjectWriteHeader.C:277
ListOps.H
Various functions to operate on Lists.
faceTraits.H
Foam::mkDir
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:507
Foam::MeshedSurfaceProxy::canWriteType
static bool canWriteType(const word &fileType, bool verbose=false)
Can this file format type be written via MeshedSurfaceProxy?
Definition: MeshedSurfaceProxy.C:47
Foam::isDir
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:643