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-------------------------------------------------------------------------------
11License
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"
34
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
37namespace Foam
38{
39namespace surfaceWriters
40{
44}
45}
46
47
48// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49
51:
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 (Pstream::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
161template<class Type>
162Foam::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
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 (Pstream::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// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
static const char *const typeName
Typename for Field.
Definition: FieldBase.H:59
The IOstreamOption is a simple container for options an IOstream can normally have.
Output to file stream, using an OSstream.
Definition: OFstream.H:57
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A class for handling file names.
Definition: fileName.H:76
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
Implements a meshed surface by referencing another meshed surface or faces/points components.
Definition: meshedSurfRef.H:56
virtual const pointField & points() const
The points used for the surface.
virtual const faceList & faces() const
The faces used for the surface.
Abstract definition of a meshed surface defined by faces and points.
Definition: meshedSurf.H:50
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
splitCell * master() const
Definition: splitCell.H:113
Base class for surface writers.
virtual void open(const fileName &outputPath)
Open for output on specified path, using existing surface.
A surfaceWriter for OpenFOAM surfaces.
virtual fileName write()
Write surface geometry to file.
A class for managing temporary objects.
Definition: tmp.H:65
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeName(Type)
Define the typeName.
Definition: className.H:96
const pointField & points
word timeName
Definition: getTimeIndex.H:3
Namespace for OpenFOAM.
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:515
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:651
runTime write()
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
Convenience macros for instantiating surfaceWriter methods.
#define defineSurfaceWriterWriteFields(ThisClass)