foamSurfaceWriter.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) 2015-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 "foamSurfaceWriter.H"
30 #include "OFstream.H"
31 #include "OSspecific.H"
32 #include "surfaceWriterMethods.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 namespace surfaceWriters
40 {
41  defineTypeName(foamWriter);
42  addToRunTimeSelectionTable(surfaceWriter, foamWriter, word);
44 }
45 }
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
51 :
52  surfaceWriter(),
53  streamOpt_()
54 {}
55 
56 
58 (
59  const dictionary& options
60 )
61 :
62  surfaceWriter(options),
63  streamOpt_
64  (
65  IOstreamOption::formatEnum("format", options, IOstreamOption::ASCII),
66  IOstreamOption::compressionEnum("compression", options)
67  )
68 {}
69 
70 
72 (
73  const meshedSurf& surf,
74  const fileName& outputPath,
75  bool parallel,
76  const dictionary& options
77 )
78 :
79  foamWriter(options)
80 {
81  open(surf, outputPath, parallel);
82 }
83 
84 
86 (
87  const pointField& points,
88  const faceList& faces,
89  const fileName& outputPath,
90  bool parallel,
91  const dictionary& options
92 )
93 :
94  foamWriter(options)
95 {
96  open(points, faces, outputPath, parallel);
97 }
98 
99 
100 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
101 
103 {
104  checkOpen();
105 
106  // Geometry:
107  // - rootdir/<TIME>/surfaceName/{points,faces,faceCentres}
108 
109  fileName surfaceDir = outputPath_;
110  if (useTimeDir() && !timeName().empty())
111  {
112  // Splice in time-directory
113  surfaceDir = outputPath_.path() / timeName() / outputPath_.name();
114  }
115 
116  if (verbose_)
117  {
118  Info<< "Writing geometry to " << surfaceDir << endl;
119  }
120 
121 
122  // const meshedSurf& surf = surface();
123  const meshedSurfRef& surf = adjustSurface();
124 
125  if (UPstream::master() || !parallel_)
126  {
127  const pointField& points = surf.points();
128  const faceList& faces = surf.faces();
129 
130  if (!isDir(surfaceDir))
131  {
132  mkDir(surfaceDir);
133  }
134 
135  // Points
136  OFstream(surfaceDir/"points", streamOpt_)() << points;
137 
138  // Faces
139  OFstream(surfaceDir/"faces", streamOpt_)() << faces;
140 
141  // Face centers.
142  // Not really necessary but very handy when reusing as inputs
143  // for e.g. timeVaryingMapped bc.
144  pointField faceCentres(faces.size(), Zero);
145 
146  forAll(faces, facei)
147  {
148  faceCentres[facei] = faces[facei].centre(points);
149  }
150 
151  OFstream(surfaceDir/"faceCentres", streamOpt_)() << faceCentres;
152  }
153 
154  wroteGeom_ = true;
155  return surfaceDir;
156 }
157 
158 
159 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
160 
161 template<class Type>
162 Foam::fileName Foam::surfaceWriters::foamWriter::writeTemplate
163 (
164  const word& fieldName,
165  const Field<Type>& localValues
166 )
167 {
168  // Separate geometry
169  if (!wroteGeom_)
170  {
171  write();
172  }
173 
174  checkOpen();
175 
176  // Geometry should already have been written
177  // Values to separate directory (e.g. "scalarField/p")
178 
179  // Field: rootdir/<TIME>/surfaceName/fieldType/field
180  //?? -> or rootdir/surfaceName/fieldType/<TIME>/field
181 
182  fileName surfaceDir = outputPath_;
183  if (useTimeDir() && !timeName().empty())
184  {
185  // Splice in time-directory
186  surfaceDir = outputPath_.path() / timeName() / outputPath_.name();
187  }
188 
189  const fileName outputFile
190  (
191  surfaceDir
192  / (word(pTraits<Type>::typeName) + FieldBase::typeName)
193  / fieldName
194  );
195 
196 
197  // Implicit geometry merge()
198  tmp<Field<Type>> tfield = adjustField(fieldName, mergeField(localValues));
199 
200  if (verbose_)
201  {
202  Info<< " to " << surfaceDir << endl;
203  }
204 
205 
206  if (UPstream::master())
207  {
208  if (!isDir(outputFile.path()))
209  {
210  mkDir(outputFile.path());
211  }
212 
213  // Write field
214  OFstream(outputFile, streamOpt_)() << tfield();
215  }
216 
217  wroteGeom_ = true;
218  return outputFile;
219 }
220 
221 
222 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
223 
224 // Field writing methods
226 
227 
228 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:72
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
A simple container for options an IOstream can normally have.
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:169
Macros for easy insertion into run-time selection tables.
Abstract definition of a meshed surface defined by faces and points.
Definition: meshedSurf.H:43
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:860
static const char *const typeName
Typename for Field.
Definition: Field.H:86
List< face > faceList
List of faces.
Definition: faceListFwd.H:39
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
word timeName
Definition: getTimeIndex.H:3
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:38
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
const pointField & points
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual fileName write()
Write surface geometry to file.
Convenience macros for instantiating surfaceWriter methods.
A surfaceWriter for OpenFOAM surfaces.
defineTypeName(abaqusWriter)
defineSurfaceWriterWriteFields(Foam::surfaceWriters::foamWriter)
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
messageStream Info
Information stream (stdout output on master, null elsewhere)
Base class for surface writers.
addToRunTimeSelectionTable(surfaceWriter, abaqusWriter, word)
Namespace for OpenFOAM.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127