rawSurfaceWriterImpl.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-2014 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 "IOmanip.H"
30#include "OFstream.H"
31#include "OSspecific.H"
32
33// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
34
35namespace Foam
36{
37 // Emit x,y,z
38 static inline void writePoint(Ostream& os, const point& p)
39 {
40 os << p.x() << ' ' << p.y() << ' ' << p.z();
41 }
42
43 // Emit each component
44 template<class Type>
45 static inline void writeData(Ostream& os, const Type& val)
46 {
47 for (direction d = 0; d < pTraits<Type>::nComponents; ++d)
48 {
49 os << ' ' << component(val, d);
50 }
51 }
52
53 // Write area header
54 static inline void writeHeaderArea(Ostream& os)
55 {
56 os << " area_x area_y area_z";
57 }
58
59 // Write field name, use named components for VectorSpace
60 template<class Type>
61 static inline void writeHeader(Ostream& os, const word& fieldName)
62 {
63 os << ' ';
64
65 const auto nCmpts(pTraits<Type>::nComponents);
66
67 if (pTraits<Type>::rank || nCmpts > 1)
68 {
69 for (direction d = 0; d < nCmpts; ++d)
70 {
71 os << ' ' << fieldName
73 }
74 }
75 else
76 {
77 os << ' ' << fieldName;
78 }
79 }
80
81} // End namespace Foam
82
83
84// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
85
86template<class Type>
87Foam::fileName Foam::surfaceWriters::rawWriter::writeTemplate
88(
89 const word& fieldName,
90 const Field<Type>& localValues
91)
92{
93 checkOpen();
94
95 // Field: rootdir/<TIME>/<field>_surfaceName.raw
96
97 fileName outputFile = outputPath_.path();
98 if (useTimeDir() && !timeName().empty())
99 {
100 // Splice in time-directory
101 outputFile /= timeName();
102 }
103
104 // Append <field>_surfaceName.raw
105 outputFile /= fieldName + '_' + outputPath_.name();
106 outputFile.ext("raw");
107
108
109 // Implicit geometry merge()
110 tmp<Field<Type>> tfield = adjustField(fieldName, mergeField(localValues));
111
112 if (verbose_)
113 {
114 Info<< " to " << outputFile << endl;
115 }
116
117 // const meshedSurf& surf = surface();
118 const meshedSurfRef& surf = adjustSurface();
119
120 if (Pstream::master() || !parallel_)
121 {
122 const auto& values = tfield();
123 const pointField& points = surf.points();
124 const faceList& faces = surf.faces();
125 const bool withFaceNormal = (writeNormal_ && !this->isPointData());
126
127 if (!isDir(outputFile.path()))
128 {
129 mkDir(outputFile.path());
130 }
131
132 OFstream os(outputFile, streamOpt_);
133 os.precision(precision_);
134
135 // Header
136 {
137 os << "# " << fieldName;
138 if (this->isPointData())
139 {
140 os << " POINT_DATA ";
141 }
142 else
143 {
144 os << " FACE_DATA ";
145 }
146 os << values.size() << nl;
147
148 os << "# x y z";
149 writeHeader<Type>(os, fieldName);
150 if (withFaceNormal)
151 {
153 }
154 os << nl;
155 }
156
157
158 if (this->isPointData())
159 {
160 // Node values
161 forAll(values, elemi)
162 {
163 writePoint(os, points[elemi]);
164 writeData(os, values[elemi]);
165 os << nl;
166 }
167 }
168 else
169 {
170 // Face values
171 forAll(values, elemi)
172 {
173 const face& f = faces[elemi];
174
175 writePoint(os, f.centre(points));
176 writeData(os, values[elemi]);
177 if (withFaceNormal)
178 {
179 os << ' ';
180 writePoint(os, f.areaNormal(points));
181 }
182 os << nl;
183 }
184 }
185 }
186
187 wroteGeom_ = true;
188 return outputFile;
189}
190
191
192// ************************************************************************* //
Istream and Ostream manipulators taking arguments.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Generic templated field type.
Definition: Field.H:82
Output to file stream, using an OSstream.
Definition: OFstream.H:57
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:326
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
A class for handling file names.
Definition: fileName.H:76
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:218
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
static std::string name(const std::string &str)
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:199
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.
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
splitCell * master() const
Definition: splitCell.H:113
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?
bool isPointData() const noexcept
Are the field data to be treated as point data?
const word & timeName() const
The current time value/name.
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
tmp< Field< label > > mergeField(const Field< label > &fld) const
fileName outputPath_
The full output directory and file (surface) name.
A class for managing temporary objects.
Definition: tmp.H:65
A class for handling words, derived from Foam::string.
Definition: word.H:68
volScalarField & p
OBJstream os(runTime.globalPath()/outputName)
const pointField & points
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
static void writePoint(Ostream &os, const point &p)
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
static void writeHeader(Ostream &os, const word &fieldName)
messageStream Info
Information stream (stdout output on master, null elsewhere)
static void writeHeaderArea(Ostream &os)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
uint8_t direction
Definition: direction.H:56
static void writeData(Ostream &os, const Type &val)
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:651
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
labelList f(nPoints)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333