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-2019 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  if (verbose_)
134  {
135  Info<< "Writing field " << fieldName << " to " << outputFile << endl;
136  }
137 
138 
139  // geometry merge() implicit
140  tmp<Field<Type>> tfield = mergeField(localValues);
141 
142  const meshedSurf& surf = surface();
143 
144  if (Pstream::master() || !parallel_)
145  {
146  const auto& values = tfield();
147  const pointField& points = surf.points();
148  const faceList& faces = surf.faces();
149 
150  if (!isDir(outputFile.path()))
151  {
152  mkDir(outputFile.path());
153  }
154 
155  OFstream os
156  (
157  outputFile,
158  IOstream::ASCII,
159  IOstream::currentVersion,
160  writeCompression_
161  );
162 
163  // Header
164  {
165  os << "# " << fieldName;
166  if (this->isPointData())
167  {
168  os << " POINT_DATA ";
169  }
170  else
171  {
172  os << " FACE_DATA ";
173  }
174  os << values.size() << nl;
175 
176  // # x y z field
177  writeHeader<Type>(os, fieldName);
178  }
179 
180 
181  if (this->isPointData())
182  {
183  // Node values
184  forAll(values, elemi)
185  {
186  writePoint(os, points[elemi]);
187  writeData(os, values[elemi]);
188  }
189  }
190  else
191  {
192  // Face values
193  forAll(values, elemi)
194  {
195  writePoint(os, faces[elemi].centre(points));
196  writeData(os, values[elemi]);
197  }
198  }
199  }
200 
201  wroteGeom_ = true;
202  return outputFile;
203 }
204 
205 
206 // ************************************************************************* //
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
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:59
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:337
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:49
Foam::writePoint
static void writePoint(Foam::Ostream &os, const Foam::point &p)
Definition: rawSurfaceWriter.C:53
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
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:99
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::List< face >
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::direction
uint8_t direction
Definition: direction.H:47
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