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-2020 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);
43  addToRunTimeSelectionTable(surfaceWriter, foamWriter, wordDict);
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  IOstream::formatEnum("format", options, IOstream::ASCII),
66  IOstream::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}
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 
124  if (Pstream::master() || !parallel_)
125  {
126  const pointField& points = surf.points();
127  const faceList& faces = surf.faces();
128 
129  if (!isDir(surfaceDir))
130  {
131  mkDir(surfaceDir);
132  }
133 
134  // Points
135  OFstream(surfaceDir/"points", streamOpt_)() << points;
136 
137  // Faces
138  OFstream(surfaceDir/"faces", streamOpt_)() << faces;
139 
140  // Face centers.
141  // Not really necessary but very handy when reusing as inputs
142  // for e.g. timeVaryingMapped bc.
143  pointField faceCentres(faces.size(), Zero);
144 
145  forAll(faces, facei)
146  {
147  faceCentres[facei] = faces[facei].centre(points);
148  }
149 
150  OFstream(surfaceDir/"faceCentres", streamOpt_)() << faceCentres;
151  }
152 
153  wroteGeom_ = true;
154  return surfaceDir;
155 }
156 
157 
158 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
159 
160 template<class Type>
161 Foam::fileName Foam::surfaceWriters::foamWriter::writeTemplate
162 (
163  const word& fieldName,
164  const Field<Type>& localValues
165 )
166 {
167  // Separate geometry
168  if (!wroteGeom_)
169  {
170  write();
171  }
172 
173  checkOpen();
174 
175  // Geometry should already have been written
176  // Values to separate directory (e.g. "scalarField/p")
177 
178  // Field: rootdir/<TIME>/surfaceName/fieldType/field
179  //?? -> or rootdir/surfaceName/fieldType/<TIME>/field
180 
181  fileName surfaceDir = outputPath_;
182  if (useTimeDir() && !timeName().empty())
183  {
184  // Splice in time-directory
185  surfaceDir = outputPath_.path() / timeName() / outputPath_.name();
186  }
187 
188  const fileName outputFile
189  (
190  surfaceDir
192  / fieldName
193  );
194 
195  if (verbose_)
196  {
197  Info<< "Writing field " << fieldName << " to " << surfaceDir << endl;
198  }
199 
200 
201  // geometry merge() implicit
202  tmp<Field<Type>> tfield = mergeField(localValues);
203 
204  if (Pstream::master())
205  {
206  if (!isDir(outputFile.path()))
207  {
208  mkDir(outputFile.path());
209  }
210 
211  // Write field
212  OFstream(outputFile, streamOpt_)() << tfield();
213  }
214 
215  wroteGeom_ = true;
216  return outputFile;
217 }
218 
219 
220 // Field writing methods
222 
223 
224 // ************************************************************************* //
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::surfaceWriters::foamWriter::write
virtual fileName write()
Write surface geometry to file.
Definition: foamSurfaceWriter.C:102
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::surfaceWriter
Base class for surface writers.
Definition: surfaceWriter.H:111
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::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
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::surfaceWriters::defineTypeName
defineTypeName(boundaryDataWriter)
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
surfaceWriterMethods.H
Convenience macros for instantiating surfaceWriter methods.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::surfaceWriters::foamWriter
A surfaceWriter for OpenFOAM surfaces.
Definition: foamSurfaceWriter.H:114
OFstream.H
Foam::surfaceWriters::foamWriter::foamWriter
foamWriter()
Default construct.
Definition: foamSurfaceWriter.C:50
Foam::Field< vector >
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
foamSurfaceWriter.H
Foam::meshedSurf::points
virtual const pointField & points() const =0
The points used for the surface.
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::IOstreamOption::compressionEnum
static compressionType compressionEnum(const word &compName, const compressionType deflt=compressionType::UNCOMPRESSED)
The compression enum corresponding to the string.
Definition: IOstreamOption.C:92
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::FieldBase::typeName
static const char *const typeName
Typename for Field.
Definition: FieldBase.H:59
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:87
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:439
Foam::surfaceWriters::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surfaceWriter, boundaryDataWriter, word)
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::IOstreamOption::formatEnum
static streamFormat formatEnum(const word &formatName, const streamFormat deflt=streamFormat::ASCII)
Definition: IOstreamOption.C:53
Foam::List< face >
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:54
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:35
defineSurfaceWriterWriteFields
defineSurfaceWriterWriteFields(Foam::surfaceWriters::foamWriter)
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::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