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-2021 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 "OFstream.H"
30 #include "OSspecific.H"
31 #include "IOmanip.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  {
47  for (direction i=0; i < pTraits<Type>::nComponents; ++i)
48  {
49  os << ' ' << component(val, i);
50  }
51  }
52 
53  // Write x/y/z header. Must be called first
54  static inline void writeHeaderXYZ(Ostream& os)
55  {
56  os << "# x y z";
57  }
58 
59  // Write area header
60  static inline void writeHeaderArea(Ostream& os)
61  {
62  os << " area_x area_y area_z";
63  }
64 
65  template<class Type>
66  static inline void writeHeader(Ostream& os, const word& fieldName)
67  {
68  os << " " << fieldName;
69  }
70 
71  template<class Type>
72  void writeHeaderComponents(Ostream& os, const word& fieldName)
73  {
74  os << ' ';
75  for (direction i=0; i < pTraits<Type>::nComponents; ++i)
76  {
77  os << ' ' << fieldName << '_' << Type::componentNames[i];
78  }
79  }
80 
81  template<>
82  void writeHeader<vector>(Ostream& os, const word& fieldName)
83  {
84  writeHeaderComponents<vector>(os, fieldName);
85  }
86 
87  template<>
88  void writeHeader<sphericalTensor>(Ostream& os, const word& fieldName)
89  {
90  writeHeaderComponents<sphericalTensor>(os, fieldName);
91  }
92 
93  template<>
94  void writeHeader<symmTensor>(Ostream& os, const word& fieldName)
95  {
96  writeHeaderComponents<symmTensor>(os, fieldName);
97  }
98 
99  template<>
100  void writeHeader<tensor>(Ostream& os, const word& fieldName)
101  {
102  writeHeaderComponents<tensor>(os, fieldName);
103  }
104 
105 } // End namespace Foam
106 
107 
108 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
109 
110 template<class Type>
111 Foam::fileName Foam::surfaceWriters::rawWriter::writeTemplate
112 (
113  const word& fieldName,
114  const Field<Type>& localValues
115 )
116 {
117  checkOpen();
118 
119  // Field: rootdir/<TIME>/<field>_surfaceName.raw
120 
121  fileName outputFile = outputPath_.path();
122  if (useTimeDir() && !timeName().empty())
123  {
124  // Splice in time-directory
125  outputFile /= timeName();
126  }
127 
128  // Append <field>_surfaceName.raw
129  outputFile /= fieldName + '_' + outputPath_.name();
130  outputFile.ext("raw");
131 
132 
133  // Output scaling for the variable, but not for integer types.
134  // could also solve with clever templating
135 
136  const scalar varScale =
137  (
138  std::is_integral<Type>::value
139  ? scalar(1)
140  : fieldScale_.getOrDefault<scalar>(fieldName, 1)
141  );
142 
143  if (verbose_)
144  {
145  Info<< "Writing field " << fieldName;
146  if (!equal(varScale, 1))
147  {
148  Info<< " (scaling " << varScale << ')';
149  }
150  Info<< " to " << outputFile << endl;
151  }
152 
153 
154  // Implicit geometry merge()
155  tmp<Field<Type>> tfield = mergeField(localValues) * varScale;
156 
157  const meshedSurf& surf = surface();
158 
159  if (Pstream::master() || !parallel_)
160  {
161  const auto& values = tfield();
162  const pointField& points = surf.points();
163  const faceList& faces = surf.faces();
164  const bool withFaceNormal = (writeNormal_ && !this->isPointData());
165 
166  if (!isDir(outputFile.path()))
167  {
168  mkDir(outputFile.path());
169  }
170 
171  OFstream os(outputFile, streamOpt_);
172  os.precision(precision_);
173 
174  // Header
175  {
176  os << "# " << fieldName;
177  if (this->isPointData())
178  {
179  os << " POINT_DATA ";
180  }
181  else
182  {
183  os << " FACE_DATA ";
184  }
185  os << values.size() << nl;
186 
188  writeHeader<Type>(os, fieldName);
189  if (withFaceNormal)
190  {
192  }
193  os << nl;
194  }
195 
196 
197  if (this->isPointData())
198  {
199  // Node values
200  forAll(values, elemi)
201  {
202  writePoint(os, points[elemi]*geometryScale_);
203  writeData(os, values[elemi]);
204  os << nl;
205  }
206  }
207  else
208  {
209  // Face values
210  forAll(values, elemi)
211  {
212  const face& f = faces[elemi];
213 
214  writePoint(os, f.centre(points)*geometryScale_);
215  writeData(os, values[elemi]);
216  if (withFaceNormal)
217  {
218  os << ' ';
219  writePoint(os, f.areaNormal(points)*geometryScale_);
220  }
221  os << nl;
222  }
223  }
224  }
225 
226  wroteGeom_ = true;
227  return outputFile;
228 }
229 
230 
231 // ************************************************************************* //
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::component
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Definition: FieldFieldFunctions.C:44
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::writeHeader< sphericalTensor >
void writeHeader< sphericalTensor >(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:88
Foam::fileName::path
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::writeHeaderArea
static void writeHeaderArea(Ostream &os)
Definition: rawSurfaceWriterImpl.C:60
Foam::meshedSurf::faces
virtual const faceList & faces() const =0
The faces used for the surface.
writeData
const bool writeData(pdfDictionary.get< bool >("writeData"))
Foam::meshedSurf
Abstract definition of a meshed surface defined by faces and points.
Definition: meshedSurf.H:49
Foam::writeHeader< vector >
void writeHeader< vector >(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:82
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:66
Foam::writeHeaderXYZ
static void writeHeaderXYZ(Ostream &os)
Definition: rawSurfaceWriterImpl.C:54
Foam::MatrixTools::equal
bool equal(const Matrix< Form1, Type > &A, const Matrix< Form2, Type > &B, const bool verbose=false, const label maxDiffs=10, const scalar relTol=1e-5, const scalar absTol=1e-8)
Compare matrix elements for absolute or relative equality.
Definition: MatrixTools.C:34
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
OFstream.H
Foam::writePoint
static void writePoint(Ostream &os, const point &p)
Definition: rawSurfaceWriterImpl.C:38
Foam::writeHeader< tensor >
void writeHeader< tensor >(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:100
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::writeHeaderComponents
void writeHeaderComponents(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:72
IOmanip.H
Istream and Ostream manipulators taking arguments.
Foam::meshedSurf::points
virtual const pointField & points() const =0
The points used for the surface.
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::writeData
static void writeData(Ostream &os, const Type &val)
Definition: rawSurfaceWriterImpl.C:45
os
OBJstream os(runTime.globalPath()/outputName)
Foam::fileName::ext
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:218
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::nl
constexpr char nl
Definition: Ostream.H:404
f
labelList f(nPoints)
Foam::Vector< scalar >
Foam::List< face >
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:72
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
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::writeHeader< symmTensor >
void writeHeader< symmTensor >(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:94
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