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-2020 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 each component
38  template<class Type>
39  static inline void writeData(Ostream& os, const Type& val)
40  {
41  for (direction i=0; i < pTraits<Type>::nComponents; ++i)
42  {
43  os << ' ' << component(val, i);
44  }
45  os << nl;
46  }
47 
48  template<class Type>
49  static inline void writeHeader(Ostream& os, const word& fieldName) {}
50 
51  template<>
52  void writeHeader<label>(Ostream& os, const word& fieldName)
53  {
54  os << "# x y z"
55  << " " << fieldName << nl;
56  }
57 
58  template<>
59  void writeHeader<scalar>(Ostream& os, const word& fieldName)
60  {
61  os << "# x y z"
62  << " " << fieldName << nl;
63  }
64 
65  template<>
66  void writeHeader<vector>(Ostream& os, const word& fieldName)
67  {
68  os << "# x y z"
69  << " " << fieldName << "_x"
70  << " " << fieldName << "_y"
71  << " " << fieldName << "_z"
72  << nl;
73  }
74 
75  template<>
76  void writeHeader<sphericalTensor>(Ostream& os, const word& fieldName)
77  {
78  os << "# x y z"
79  << " " << fieldName << "_ii" << nl;
80  }
81 
82  template<>
83  void writeHeader<symmTensor>(Ostream& os, const word& fieldName)
84  {
85  // This may not be quite right (not sure what people might need)
86  os << "# xx xy xz yy yz zz";
87  for (direction i=0; i < pTraits<symmTensor>::nComponents; ++i)
88  {
89  os << " " << fieldName << '_' << int(i);
90  }
91  os << nl;
92  }
93 
94  template<>
95  void writeHeader<tensor>(Ostream& os, const word& fieldName)
96  {
97  // This may not be quite right (not sure what people might need)
98  os << "# xx xy xz yx yy yz zx zy zz";
99  for (direction i=0; i < pTraits<tensor>::nComponents; ++i)
100  {
101  os << " " << fieldName << '_' << int(i);
102  }
103  os << nl;
104  }
105 
106 } // End namespace Foam
107 
108 
109 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
110 
111 template<class Type>
112 Foam::fileName Foam::surfaceWriters::rawWriter::writeTemplate
113 (
114  const word& fieldName,
115  const Field<Type>& localValues
116 )
117 {
118  checkOpen();
119 
120  // Field: rootdir/<TIME>/<field>_surfaceName.raw
121 
122  fileName outputFile = outputPath_.path();
123  if (useTimeDir() && !timeName().empty())
124  {
125  // Splice in time-directory
126  outputFile /= timeName();
127  }
128 
129  // Append <field>_surfaceName.raw
130  outputFile /= fieldName + '_' + outputPath_.name();
131  outputFile.ext("raw");
132 
133 
134  // Output scaling for the variable, but not for integer types.
135  // could also solve with clever templating
136 
137  const scalar varScale =
138  (
139  std::is_integral<Type>::value
140  ? scalar(1)
141  : fieldScale_.getOrDefault<scalar>(fieldName, 1)
142  );
143 
144  if (verbose_)
145  {
146  Info<< "Writing field " << fieldName;
147  if (!equal(varScale, 1))
148  {
149  Info<< " (scaling " << varScale << ')';
150  }
151  Info<< " to " << outputFile << endl;
152  }
153 
154 
155  // Implicit geometry merge()
156  tmp<Field<Type>> tfield = mergeField(localValues) * varScale;
157 
158  const meshedSurf& surf = surface();
159 
160  if (Pstream::master() || !parallel_)
161  {
162  const auto& values = tfield();
163  const pointField& points = surf.points();
164  const faceList& faces = surf.faces();
165 
166  if (!isDir(outputFile.path()))
167  {
168  mkDir(outputFile.path());
169  }
170 
171  OFstream os(outputFile, streamOpt_);
172 
173  // Header
174  {
175  os << "# " << fieldName;
176  if (this->isPointData())
177  {
178  os << " POINT_DATA ";
179  }
180  else
181  {
182  os << " FACE_DATA ";
183  }
184  os << values.size() << nl;
185 
186  // # x y z field
187  writeHeader<Type>(os, fieldName);
188  }
189 
190 
191  if (this->isPointData())
192  {
193  // Node values
194  forAll(values, elemi)
195  {
196  writePoint(os, points[elemi]*geometryScale_);
197  writeData(os, values[elemi]);
198  }
199  }
200  else
201  {
202  // Face values
203  forAll(values, elemi)
204  {
205  writePoint(os, faces[elemi].centre(points)*geometryScale_);
206  writeData(os, values[elemi]);
207  }
208  }
209  }
210 
211  wroteGeom_ = true;
212  return outputFile;
213 }
214 
215 
216 // ************************************************************************* //
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
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:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::writeHeader< sphericalTensor >
void writeHeader< sphericalTensor >(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:76
Foam::fileName::path
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:186
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::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:66
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:49
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::writeHeader< tensor >
void writeHeader< tensor >(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:95
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::writeHeader< scalar >
void writeHeader< scalar >(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:59
IOmanip.H
Istream and Ostream manipulators taking arguments.
Foam::writeHeader< label >
void writeHeader< label >(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:52
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:39
Foam::fileName::ext
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:228
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:385
Foam::List< face >
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::direction
uint8_t direction
Definition: direction.H:52
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:83
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