rawSurfaceWriter.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "rawSurfaceWriter.H"
27 #include "OFstream.H"
28 #include "OSspecific.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  makeSurfaceWriterType(rawSurfaceWriter);
36  addToRunTimeSelectionTable(surfaceWriter, rawSurfaceWriter, wordDict);
37 }
38 
39 
40 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
41 
42 inline void Foam::rawSurfaceWriter::writeLocation
43 (
44  Ostream& os,
45  const pointField& points,
46  const label pointi
47 )
48 {
49  const point& pt = points[pointi];
50  os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << ' ';
51 }
52 
53 
54 inline void Foam::rawSurfaceWriter::writeLocation
55 (
56  Ostream& os,
57  const pointField& points,
58  const faceList& faces,
59  const label facei
60 )
61 {
62  const point& ct = faces[facei].centre(points);
63  os << ct.x() << ' ' << ct.y() << ' ' << ct.z() << ' ';
64 }
65 
66 
67 namespace Foam
68 {
69  template<>
70  void Foam::rawSurfaceWriter::writeHeader
71  (
72  Ostream& os,
73  const word& fieldName,
74  const Field<scalar>& values
75  )
76  {
77  os << values.size() << nl
78  << "# x y z " << fieldName << nl;
79  }
80 
81 
82  template<>
83  void Foam::rawSurfaceWriter::writeHeader
84  (
85  Ostream& os,
86  const word& fieldName,
87  const Field<vector>& values
88  )
89  {
90  os << values.size() << nl
91  << "# x y z "
92  << fieldName << "_x "
93  << fieldName << "_y "
94  << fieldName << "_z "
95  << endl;
96  }
97 
98 
99  template<>
100  void Foam::rawSurfaceWriter::writeHeader
101  (
102  Ostream& os,
103  const word& fieldName,
104  const Field<sphericalTensor>& values
105  )
106  {
107  os << values.size() << nl
108  << "# ii "
109  << fieldName << "_ii" << nl;
110  }
111 
112 
113  template<>
114  void Foam::rawSurfaceWriter::writeHeader
115  (
116  Ostream& os,
117  const word& fieldName,
118  const Field<symmTensor>& values
119  )
120  {
121  os << values.size() << nl
122  << "# xx xy xz yy yz ";
123  for (int i=0; i<6; ++i)
124  {
125  os << fieldName << "_" << i << " ";
126  }
127  os << endl;
128  }
129 
130 
131  template<>
132  void Foam::rawSurfaceWriter::writeHeader
133  (
134  Ostream& os,
135  const word& fieldName,
136  const Field<tensor>& values
137  )
138  {
139  os << values.size() << nl
140  << "# xx xy xz yx yy yz zx zy zz";
141  for (int i=0; i<9; ++i)
142  {
143  os << fieldName << "_" << i << " ";
144  }
145  os << nl;
146  }
147 
148 
149  template<>
150  inline void Foam::rawSurfaceWriter::writeData
151  (
152  Ostream& os,
153  const scalar& v
154  )
155  {
156  os << v << nl;
157  }
158 
159 
160  template<>
161  inline void Foam::rawSurfaceWriter::writeData
162  (
163  Ostream& os,
164  const vector& v
165  )
166  {
167  os << v[0] << ' ' << v[1] << ' ' << v[2] << nl;
168  }
169 
170 
171  template<>
172  inline void Foam::rawSurfaceWriter::writeData
173  (
174  Ostream& os,
175  const sphericalTensor& v
176  )
177  {
178  os << v[0] << nl;
179  }
180 
181 
182  template<>
183  inline void Foam::rawSurfaceWriter::writeData
184  (
185  Ostream& os,
186  const symmTensor& v
187  )
188  {
189  os << v[0] << ' ' << v[1] << ' ' << v[2] << ' '
190  << v[3] << ' ' << v[4] << ' ' << v[5] << nl;
191  }
192 
193 
194  template<>
195  inline void Foam::rawSurfaceWriter::writeData
196  (
197  Ostream& os,
198  const tensor& v
199  )
200  {
201  os << v[0] << ' ' << v[1] << ' ' << v[2] << ' '
202  << v[3] << ' ' << v[4] << ' ' << v[5] << ' '
203  << v[6] << ' ' << v[7] << ' ' << v[8] << nl;
204  }
205 
206 }
207 
208 
209 template<class Type>
210 void Foam::rawSurfaceWriter::Write
211 (
212  const fileName& outputDir,
213  const fileName& surfaceName,
214  const pointField& points,
215  const faceList& faces,
216  const word& fieldName,
217  const Field<Type>& values,
218  const bool isNodeValues
219 ) const
220 {
221  if (!isDir(outputDir))
222  {
223  mkDir(outputDir);
224  }
225 
226  OFstream os
227  (
228  outputDir/fieldName + '_' + surfaceName + ".raw",
231  writeCompression_
232  );
233 
234  if (debug)
235  {
236  Info<< "Writing field " << fieldName << " to " << os.name() << endl;
237  }
238 
239  // Header
240  os << "# " << fieldName;
241  if (isNodeValues)
242  {
243  os << " POINT_DATA ";
244  }
245  else
246  {
247  os << " FACE_DATA ";
248  }
249 
250  // Header
251  writeHeader(os, fieldName, values);
252 
253  // Values
254  if (isNodeValues)
255  {
256  forAll(values, elemI)
257  {
258  writeLocation(os, points, elemI);
259  writeData(os, values[elemI]);
260  }
261  }
262  else
263  {
264  forAll(values, elemI)
265  {
266  writeLocation(os, points, faces, elemI);
267  writeData(os, values[elemI]);
268  }
269  }
270 }
271 
272 
273 
274 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
275 
277 (
278  const IOstream::streamFormat writeFormat
279 )
280 :
281  surfaceWriter(writeFormat),
282  writeCompression_(IOstream::UNCOMPRESSED)
283 {}
284 
285 
287 :
288  surfaceWriter(optDict),
289  writeCompression_(IOstream::UNCOMPRESSED)
290 {
291  if (optDict.found("compression"))
292  {
293  writeCompression_ =
294  IOstream::compressionEnum(optDict.lookup("compression"));
295  }
296 }
297 
298 
299 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
300 
302 {}
303 
304 
305 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
306 
308 (
309  const fileName& outputDir,
310  const fileName& surfaceName,
311  const pointField& points,
312  const faceList& faces
313 ) const
314 {
315  if (!isDir(outputDir))
316  {
317  mkDir(outputDir);
318  }
319 
320  OFstream os
321  (
322  outputDir/surfaceName + ".raw",
325  writeCompression_
326  );
327 
328  if (debug)
329  {
330  Info<< "Writing geometry to " << os.name() << endl;
331  }
332 
333 
334  // Header
335  os << "# geometry NO_DATA " << faces.size() << nl
336  << "# x y z" << nl;
337 
338  // Write faces centres
339  forAll(faces, elemI)
340  {
341  writeLocation(os, points, faces, elemI);
342  os << nl;
343  }
344 
345  os << nl;
346 }
347 
348 
349 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350 
351 // Create write methods
353 
354 
355 // ************************************************************************* //
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:667
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A surfaceWriter for raw output.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
static compressionType compressionEnum(const word &)
Return compression of given compression name.
Definition: IOstream.C:61
A class for handling file names.
Definition: fileName.H:79
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
virtual void write(const fileName &outputDir, const fileName &surfaceName, const pointField &points, const faceList &faces) const
Write single surface geometry to file.
Output to file stream.
Definition: OFstream.H:82
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
List< face > faceList
Definition: faceListFwd.H:43
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
const fileName & name() const
Return the name of the stream.
Definition: OFstream.H:120
Templated 3D SphericalTensor derived from VectorSpace adding construction from 1 component, element access using th ii() member function and the inner-product (dot-product) and outer-product operators.
const Vector< Cmpt > & centre(const Foam::List< Vector< Cmpt >> &) const
Return *this (used for point which is a typedef to Vector<scalar>.
Definition: VectorI.H:116
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:42
virtual ~rawSurfaceWriter()
Destructor.
void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Write header.
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
A class for handling words, derived from string.
Definition: word.H:59
Convenience macros for instantiating writer methods for surfaceWriter classes.
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
defineSurfaceWriterWriteFields(nastranSurfaceWriter)
const Cmpt & x() const
Definition: VectorI.H:75
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
static const char nl
Definition: Ostream.H:260
const bool writeData(readBool(pdfDictionary.lookup("writeData")))
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:71
vector point
Point is a vector.
Definition: point.H:41
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
makeSurfaceWriterType(ensightSurfaceWriter)
messageStream Info
rawSurfaceWriter(const IOstream::streamFormat writeFormat)
Construct given write format.
Base class for surface writers.
Definition: surfaceWriter.H:54
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:812