rawSurfaceWriter.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 | Copyright (C) 2011 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 
28 #include "OFstream.H"
29 #include "OSspecific.H"
30 #include "IOmanip.H"
31 
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  makeSurfaceWriterType(rawSurfaceWriter);
39 }
40 
41 
42 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
43 
44 inline void Foam::rawSurfaceWriter::writeLocation
45 (
46  Ostream& os,
47  const pointField& points,
48  const label pointI
49 )
50 {
51  const point& pt = points[pointI];
52  os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << ' ';
53 }
54 
55 
56 inline void Foam::rawSurfaceWriter::writeLocation
57 (
58  Ostream& os,
59  const pointField& points,
60  const faceList& faces,
61  const label faceI
62 )
63 {
64  const point& ct = faces[faceI].centre(points);
65  os << ct.x() << ' ' << ct.y() << ' ' << ct.z() << ' ';
66 }
67 
68 
69 namespace Foam
70 {
71  template<>
72  void Foam::rawSurfaceWriter::writeHeader
73  (
74  Ostream& os,
75  const word& fieldName,
76  const Field<scalar>& values
77  )
78  {
79  os << values.size() << nl
80  << "# x y z " << fieldName << nl;
81  }
82 
83 
84  template<>
85  void Foam::rawSurfaceWriter::writeHeader
86  (
87  Ostream& os,
88  const word& fieldName,
89  const Field<vector>& values
90  )
91  {
92  os << values.size() << nl
93  << "# x y z "
94  << fieldName << "_x "
95  << fieldName << "_y "
96  << fieldName << "_z "
97  << endl;
98  }
99 
100 
101  template<>
102  void Foam::rawSurfaceWriter::writeHeader
103  (
104  Ostream& os,
105  const word& fieldName,
106  const Field<sphericalTensor>& values
107  )
108  {
109  os << values.size() << nl
110  << "# ii "
111  << fieldName << "_ii" << nl;
112  }
113 
114 
115  template<>
116  void Foam::rawSurfaceWriter::writeHeader
117  (
118  Ostream& os,
119  const word& fieldName,
120  const Field<symmTensor>& values
121  )
122  {
123  os << values.size() << nl
124  << "# xx xy xz yy yz ";
125  for (int i=0; i<6; ++i)
126  {
127  os << fieldName << "_" << i << " ";
128  }
129  os << endl;
130  }
131 
132 
133  template<>
134  void Foam::rawSurfaceWriter::writeHeader
135  (
136  Ostream& os,
137  const word& fieldName,
138  const Field<tensor>& values
139  )
140  {
141  os << values.size() << nl
142  << "# xx xy xz yx yy yz zx zy zz";
143  for (int i=0; i<9; ++i)
144  {
145  os << fieldName << "_" << i << " ";
146  }
147  os << nl;
148  }
149 
150 
151  template<>
152  inline void Foam::rawSurfaceWriter::writeData
153  (
154  Ostream& os,
155  const scalar& v
156  )
157  {
158  os << v << nl;
159  }
160 
161 
162  template<>
163  inline void Foam::rawSurfaceWriter::writeData
164  (
165  Ostream& os,
166  const vector& v
167  )
168  {
169  os << v[0] << ' ' << v[1] << ' ' << v[2] << nl;
170  }
171 
172 
173  template<>
174  inline void Foam::rawSurfaceWriter::writeData
175  (
176  Ostream& os,
177  const sphericalTensor& v
178  )
179  {
180  os << v[0] << nl;
181  }
182 
183 
184  template<>
185  inline void Foam::rawSurfaceWriter::writeData
186  (
187  Ostream& os,
188  const symmTensor& v
189  )
190  {
191  os << v[0] << ' ' << v[1] << ' ' << v[2] << ' '
192  << v[3] << ' ' << v[4] << ' ' << v[5] << nl;
193  }
194 
195 
196  template<>
197  inline void Foam::rawSurfaceWriter::writeData
198  (
199  Ostream& os,
200  const tensor& v
201  )
202  {
203  os << v[0] << ' ' << v[1] << ' ' << v[2] << ' '
204  << v[3] << ' ' << v[4] << ' ' << v[5] << ' '
205  << v[6] << ' ' << v[7] << ' ' << v[8] << nl;
206  }
207 
208 }
209 
210 
211 template<class Type>
212 void Foam::rawSurfaceWriter::writeTemplate
213 (
214  const fileName& outputDir,
215  const fileName& surfaceName,
216  const pointField& points,
217  const faceList& faces,
218  const word& fieldName,
219  const Field<Type>& values,
220  const bool isNodeValues,
221  const bool verbose
222 ) const
223 {
224  if (!isDir(outputDir))
225  {
226  mkDir(outputDir);
227  }
228 
229  OFstream os(outputDir/fieldName + '_' + surfaceName + ".raw");
230 
231  if (verbose)
232  {
233  Info<< "Writing field " << fieldName << " to " << os.name() << endl;
234  }
235 
236  // header
237  os << "# " << fieldName;
238  if (isNodeValues)
239  {
240  os << " POINT_DATA ";
241  }
242  else
243  {
244  os << " FACE_DATA ";
245  }
246 
247  // header
248  writeHeader(os, fieldName, values);
249 
250  // values
251  if (isNodeValues)
252  {
253  forAll(values, elemI)
254  {
255  writeLocation(os, points, elemI);
256  writeData(os, values[elemI]);
257  }
258  }
259  else
260  {
261  forAll(values, elemI)
262  {
263  writeLocation(os, points, faces, elemI);
264  writeData(os, values[elemI]);
265  }
266  }
267 }
268 
269 
270 
271 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
272 
274 :
275  surfaceWriter()
276 {}
277 
278 
279 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
280 
282 {}
283 
284 
285 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
286 
288 (
289  const fileName& outputDir,
290  const fileName& surfaceName,
291  const pointField& points,
292  const faceList& faces,
293  const bool verbose
294 ) const
295 {
296  if (!isDir(outputDir))
297  {
298  mkDir(outputDir);
299  }
300 
301  OFstream os(outputDir/surfaceName + ".raw");
302 
303  if (verbose)
304  {
305  Info<< "Writing geometry to " << os.name() << endl;
306  }
307 
308 
309  // header
310  os << "# geometry NO_DATA " << faces.size() << nl
311  << "# x y z" << nl;
312 
313  // Write faces centres
314  forAll(faces, elemI)
315  {
316  writeLocation(os, points, faces, elemI);
317  os << nl;
318  }
319 
320  os << nl;
321 }
322 
323 
324 // create write methods
326 
327 
328 // ************************************************************************* //
Output to file stream.
Definition: OFstream.H:81
vector point
Point is a vector.
Definition: point.H:41
Templated 3D symmetric tensor derived from VectorSpace adding construction from 6 components...
Definition: SymmTensor.H:53
Convenience macros for instantiating writer methods for surfaceWriter classes.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
A class for handling words, derived from string.
Definition: word.H:59
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
defineSurfaceWriterWriteFields(nastranSurfaceWriter)
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
messageStream Info
List< face > faceList
Definition: faceListFwd.H:43
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.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
Namespace for OpenFOAM.
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
virtual ~rawSurfaceWriter()
Destructor.
const fileName & name() const
Return the name of the stream.
Definition: OFstream.H:118
Base class for surface writers.
Definition: surfaceWriter.H:54
rawSurfaceWriter()
Construct null.
#define forAll(list, i)
Definition: UList.H:421
const Cmpt & x() const
Definition: VectorI.H:65
A surfaceWriter for raw output.
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:106
Istream and Ostream manipulators taking arguments.
const bool writeData(readBool(pdfDictionary.lookup("writeData")))
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:616
virtual void write(const fileName &outputDir, const fileName &surfaceName, const pointField &points, const faceList &faces, const bool verbose=false) const
Write single surface geometry to file.
A class for handling file names.
Definition: fileName.H:69
makeSurfaceWriterType(dxSurfaceWriter)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:420