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-2022 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  }
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  {
149  (
150  IOobject
151  (
152  "points",
153  t.timeName(),
154  surfMesh::meshSubDir,
155  t,
156  IOobjectOption::NO_READ,
157  IOobjectOption::NO_WRITE,
158  IOobjectOption::NO_REGISTER
159  )
160  );
161 
162  OFstream os(objectDir/io.name(), t.writeStreamOption());
163 
164  io.writeHeader(os);
165 
166  os << this->points();
167 
168  IOobject::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  IOobjectOption::NO_READ,
183  IOobjectOption::NO_WRITE,
184  IOobjectOption::NO_REGISTER
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  IOobject::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  IOobjectOption::NO_READ,
216  IOobjectOption::NO_WRITE,
217  IOobjectOption::NO_REGISTER
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  IOobject::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>
256 inline Foam::label Foam::MeshedSurfaceProxy<Face>::nTriangles() const
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 // ************************************************************************* //
label nTriangles() const
Count number of triangles.
A class for handling file names.
Definition: fileName.H:72
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
fileName timePath() const
Return current time path = path/timeName.
Definition: Time.H:520
vectorIOField pointIOField
pointIOField is a vectorIOField.
Definition: pointIOField.H:38
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
IOstreamOption writeStreamOption() const noexcept
Get write stream option (format, compression, version)
Definition: TimeI.H:116
A simple container for options an IOstream can normally have.
static bool canWriteType(const word &fileType, bool verbose=false)
Can this file format type be written via MeshedSurfaceProxy?
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Various functions to operate on Lists.
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.
word ext() const
Return file name extension (part after last .)
Definition: wordI.H:171
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:860
Traits class for faces.
Definition: faceTraits.H:44
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
CompactIOList< face, label > faceCompactIOList
Compact IO for a List of face.
Definition: faceIOList.H:35
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:614
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
const pointField & points
A class for handling words, derived from Foam::string.
Definition: word.H:63
#define DebugInFunction
Report an information message using Foam::Info.
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.
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition: HashSet.H:73
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
OBJstream os(runTime.globalPath()/outputName)
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:75
labelList f(nPoints)
List< label > sortedToc(const UList< bool > &bools)
Return the (sorted) values corresponding to &#39;true&#39; entries.
Definition: BitOps.C:195
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
static wordHashSet writeTypes()
The file format types that can be written via MeshedSurfaceProxy.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225