FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/OFstream.H>
29 #include <OpenFOAM/OSspecific.H>
30 #include <OpenFOAM/IOmanip.H>
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class Type>
36 (
37  const pointField& points,
38  const label pointI,
39  Ostream& os
40 )
41 {
42  const point& pt = points[pointI];
43 
44  os << pt.x() << ' ' << pt.y() << ' ' << pt.z() << ' ';
45 }
46 
47 
48 template<class Type>
50 (
51  const pointField& points,
52  const faceList& faces,
53  const label faceI,
54  Ostream& os
55 )
56 {
57  const point& ct = faces[faceI].centre(points);
58 
59  os << ct.x() << ' ' << ct.y() << ' ' << ct.z() << ' ';
60 }
61 
62 
63 // Write scalarField in raw format
64 template<class Type>
66 (
67  const fileName& fieldName,
68  const pointField& points,
69  const faceList& faces,
70  const scalarField& values,
71  Ostream& os
72 )
73 {
74  // header
75  os << "# x y z " << fieldName << endl;
76 
77  // Write data
78  if (values.size() == points.size())
79  {
80  forAll(values, elemI)
81  {
82  writeGeometry(points, elemI, os);
83  os << values[elemI] << nl;
84  }
85  }
86  else
87  {
88  forAll(values, elemI)
89  {
90  writeGeometry(points, faces, elemI, os);
91  os << values[elemI] << nl;
92  }
93  }
94 
95  os << nl;
96 }
97 
98 
99 // Write vectorField in raw format
100 template<class Type>
102 (
103  const fileName& fieldName,
104  const pointField& points,
105  const faceList& faces,
106  const vectorField& values,
107  Ostream& os
108 )
109 {
110  // header
111  os << "# x y z "
112  << fieldName << "_x "
113  << fieldName << "_y "
114  << fieldName << "_z "
115  << endl;
116 
117  // Write data
118  if (values.size() == points.size())
119  {
120  forAll(values, elemI)
121  {
122  writeGeometry(points, elemI, os);
123 
124  const vector& v = values[elemI];
125  os << v[0] << ' ' << v[1] << ' ' << v[2] << nl;
126  }
127  }
128  else
129  {
130  forAll(values, elemI)
131  {
132  writeGeometry(points, faces, elemI, os);
133 
134  const vector& v = values[elemI];
135  os << v[0] << ' ' << v[1] << ' ' << v[2] << nl;
136  }
137  }
138 
139 }
140 
141 
142 // Write sphericalTensorField in raw format
143 template<class Type>
145 (
146  const fileName& fieldName,
147  const pointField& points,
148  const faceList& faces,
149  const sphericalTensorField& values,
150  Ostream& os
151 )
152 {
153  // header
154  os << "# ii ";
155  os << fieldName << "_ii" << endl;
156 
157  // Write data
158  if (values.size() == points.size())
159  {
160  forAll(values, elemI)
161  {
162  writeGeometry(points, elemI, os);
163 
164  const sphericalTensor& v = values[elemI];
165  os << v[0] << nl;
166  }
167  }
168  else
169  {
170  forAll(values, elemI)
171  {
172  writeGeometry(points, faces, elemI, os);
173 
174  const sphericalTensor& v = values[elemI];
175  os << v[0] << nl;
176  }
177  }
178 }
179 
180 
181 // Write symmTensorField in raw format
182 template<class Type>
184 (
185  const fileName& fieldName,
186  const pointField& points,
187  const faceList& faces,
188  const symmTensorField& values,
189  Ostream& os
190 )
191 {
192  // header
193  os << "# xx xy xz yy yz ";
194  for(int i=0; i<6; i++)
195  {
196  os << fieldName << "_" << i << " ";
197  }
198  os << endl;
199 
200  // Write data
201  if (values.size() == points.size())
202  {
203  forAll(values, elemI)
204  {
205  writeGeometry(points, elemI, os);
206 
207  const symmTensor& v = values[elemI];
208 
209  os << v[0] << ' ' << v[1] << ' ' << v[2] << ' '
210  << v[3] << ' ' << v[4] << ' ' << v[5] << ' '
211  << nl;
212  }
213  }
214  else
215  {
216  forAll(values, elemI)
217  {
218  writeGeometry(points, faces, elemI, os);
219 
220  const symmTensor& v = values[elemI];
221 
222  os << v[0] << ' ' << v[1] << ' ' << v[2] << ' '
223  << v[3] << ' ' << v[4] << ' ' << v[5] << ' '
224  << nl;
225  }
226  }
227 }
228 
229 
230 // Write tensorField in raw format
231 template<class Type>
233 (
234  const fileName& fieldName,
235  const pointField& points,
236  const faceList& faces,
237  const tensorField& values,
238  Ostream& os
239 )
240 {
241  // header
242  os << "# xx xy xz yx yy yz zx zy zz";
243  for (int i=0; i<9; ++i)
244  {
245  os << fieldName << "_" << i << " ";
246  }
247  os << endl;
248 
249  // Write data
250  if (values.size() == points.size())
251  {
252  forAll(values, elemI)
253  {
254  writeGeometry(points, elemI, os);
255 
256  const tensor& v = values[elemI];
257  os << v[0] << ' ' << v[1] << ' ' << v[2] << ' '
258  << v[3] << ' ' << v[4] << ' ' << v[5] << ' '
259  << v[6] << ' ' << v[7] << ' ' << v[8] << nl;
260  }
261  }
262  else
263  {
264  forAll(values, elemI)
265  {
266  writeGeometry(points, faces, elemI, os);
267 
268  const tensor& v = values[elemI];
269  os << v[0] << ' ' << v[1] << ' ' << v[2] << ' '
270  << v[3] << ' ' << v[4] << ' ' << v[5] << ' '
271  << v[6] << ' ' << v[7] << ' ' << v[8] << nl;
272  }
273  }
274 }
275 
276 
277 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
278 
279 template<class Type>
281 :
282  surfaceWriter<Type>()
283 {}
284 
285 
286 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
287 
288 template<class Type>
290 {}
291 
292 
293 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
294 
295 template<class Type>
297 (
298  const fileName& outputDir,
299  const fileName& surfaceName,
300  const pointField& points,
301  const faceList& faces,
302  const bool verbose
303 ) const
304 {
305  if (!isDir(outputDir))
306  {
307  mkDir(outputDir);
308  }
309 
310  OFstream os
311  (
312  outputDir/surfaceName + ".raw"
313  );
314 
315  if (verbose)
316  {
317  Info<< "Writing geometry to " << os.name() << endl;
318  }
319 
320 
321  // header
322  os << "# geometry NO_DATA " << faces.size() << nl
323  << "# x y z" << endl;
324 
325  // Write faces
326  forAll(faces, elemI)
327  {
328  writeGeometry(points, faces, elemI, os);
329  os << nl;
330  }
331 
332  os << nl;
333 }
334 
335 
336 namespace Foam
337 {
338  // bool fields aren't supported
339  template<>
341  (
342  const fileName& outputDir,
343  const fileName& surfaceName,
344  const pointField& points,
345  const faceList& faces,
346  const fileName& fieldName,
347  const Field<bool>& values,
348  const bool verbose
349  ) const
350  {}
351 }
352 
353 
354 template<class Type>
356 (
357  const fileName& outputDir,
358  const fileName& surfaceName,
359  const pointField& points,
360  const faceList& faces,
361  const fileName& fieldName,
362  const Field<Type>& values,
363  const bool verbose
364 ) const
365 {
366  if (!isDir(outputDir))
367  {
368  mkDir(outputDir);
369  }
370 
371  OFstream os
372  (
373  outputDir/fieldName + '_' + surfaceName + ".raw"
374  );
375 
376  if (verbose)
377  {
378  Info<< "Writing field " << fieldName << " to " << os.name() << endl;
379  }
380 
381 
382  // header
383  os << "# " << fieldName;
384  if (values.size() == points.size())
385  {
386  os << " POINT_DATA ";
387  }
388  else
389  {
390  os << " FACE_DATA ";
391  }
392 
393  os << values.size() << nl;
394 
395  writeData(fieldName, points, faces, values, os);
396 }
397 
398 
399 // ************************ vim: set sw=4 sts=4 et: ************************ //