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 -------------------------------------------------------------------------------
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 "IOmanip.H"
30 #include "OFstream.H"
31 #include "OSspecific.H"
32 
33 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
34 
35 namespace 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  {
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
72  << '_' << pTraits<Type>::componentNames[d];
73  }
74  }
75  else
76  {
77  os << ' ' << fieldName;
78  }
79  }
80 
81 } // End namespace Foam
82 
83 
84 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
85 
86 template<class Type>
87 Foam::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 (UPstream::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 // ************************************************************************* //
uint8_t direction
Definition: direction.H:46
A class for handling file names.
Definition: fileName.H:72
static void writeHeader(Ostream &os, const word &fieldName)
static void writeData(Ostream &os, const Type &val)
bool empty() const
The surface to write is empty if the global number of faces is zero.
void checkOpen() const
Verify that the outputPath_ has been set or FatalError.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
const meshedSurfRef & adjustSurface() const
Merge surfaces (if not upToDate) and return merged (parallel) or regular surface (non-parallel) and a...
::Foam::direction nComponents(const expressions::valueTypeCode) noexcept
The number of components associated with given valueTypeCode.
Definition: exprTraits.C:40
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:169
bool parallel_
Writing in parallel (via master)
bool wroteGeom_
Track if geometry has been written since the last open.
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:211
bool isPointData() const noexcept
Are the field data to be treated as point data?
const word & timeName() const
The current time value/name.
#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
List< face > faceList
List of faces.
Definition: faceListFwd.H:39
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
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
Generic templated field type.
Definition: Field.H:62
A class for handling words, derived from Foam::string.
Definition: word.H:63
tmp< Field< label > > adjustField(const word &fieldName, const tmp< Field< label >> &tfield) const
static void writePoint(Ostream &os, const point &p)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
Istream and Ostream manipulators taking arguments.
tmp< Field< label > > mergeField(const Field< label > &fld) const
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
static std::string name(const std::string &str)
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:192
OBJstream os(runTime.globalPath()/outputName)
labelList f(nPoints)
bool useTimeDir() const noexcept
Should a time directory be spliced into the output path?
fileName outputPath_
The full output directory and file (surface) name.
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
static void writeHeaderArea(Ostream &os)
::Foam::direction rank(const expressions::valueTypeCode) noexcept
The vector-space rank associated with given valueTypeCode.
Definition: exprTraits.C:70
messageStream Info
Information stream (stdout output on master, null elsewhere)
bool verbose_
Additional output verbosity.
volScalarField & p
A class for managing temporary objects.
Definition: HashPtrTable.H:50
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Namespace for OpenFOAM.