Loading...
Searching...
No Matches
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"
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37namespace Foam
38{
39namespace surfaceWriters
40{
48// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49
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)
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)
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
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
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
225defineSurfaceWriterWriteFields(Foam::surfaceWriters::foamWriter);
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 Field.H:93
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
A simple container for options an IOstream can normally have.
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A class for handling file names.
Definition fileName.H:75
static std::string path(const std::string &str)
Return directory path name (part before last /).
Definition fileNameI.H:169
Implements a meshed surface by referencing another meshed surface or faces/points components.
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:44
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
Base class for surface writers.
surfaceWriter()
Default construct.
virtual void open(const fileName &outputPath)
Open for output on specified path, using existing surface.
bool wroteGeom_
Track if geometry has been written since the last open.
bool useTimeDir() const noexcept
Should a time directory be spliced into the output path?
void checkOpen() const
Verify that the outputPath_ has been set or FatalError.
bool parallel_
Writing in parallel (via master).
bool empty() const
The surface to write is empty if the global number of faces is zero.
tmp< Field< label > > adjustField(const word &fieldName, const tmp< Field< label > > &tfield) const
bool verbose_
Additional output verbosity.
const meshedSurfRef & adjustSurface() const
Merge surfaces (if not upToDate) and return merged (parallel) or regular surface (non-parallel) and a...
tmp< Field< label > > mergeField(const Field< label > &fld) const
fileName outputPath_
The full output directory and file (surface) name.
A surfaceWriter for OpenFOAM surfaces.
virtual fileName write()
Write surface geometry to file.
A class for managing temporary objects.
Definition tmp.H:75
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeName(Type)
Define the typeName.
Definition className.H:113
const pointField & points
word timeName
Definition getTimeIndex.H:3
Namespace for surface writers.
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 POSIX.C:616
messageStream Info
Information stream (stdout output on master, null elsewhere).
List< face > faceList
List of faces.
Definition faceListFwd.H:41
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
vectorField pointField
pointField is a vectorField.
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition POSIX.C:862
runTime write()
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299
Convenience macros for instantiating surfaceWriter methods.
#define defineSurfaceWriterWriteFields(ThisClass)