FreeFOAM The Cross-Platform CFD Toolkit
dxSurfaceWriter.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 "dxSurfaceWriter.H"
27 
28 #include <OpenFOAM/OFstream.H>
29 #include <OpenFOAM/OSspecific.H>
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 template<class Type>
35 (
36  Ostream& os,
37  const pointField& points,
38  const faceList& faces
39 )
40 {
41  // Write vertex coordinates
42 
43  os << "# The irregular positions" << nl
44  << "object 1 class array type float rank 1 shape 3 items "
45  << points.size() << " data follows" << nl;
46 
47  forAll(points, pointI)
48  {
49  const point& pt = points[pointI];
50 
51  os << float(pt.x()) << ' ' << float(pt.y()) << ' ' << float(pt.z())
52  << nl;
53  }
54  os << nl;
55 
56  // Write triangles
57 
58  os << "# The irregular connections (triangles)" << nl
59  << "object 2 class array type int rank 1 shape 3 items "
60  << faces.size() << " data follows" << nl;
61 
62  forAll(faces, faceI)
63  {
64  const face& f = faces[faceI];
65 
66  if (f.size() != 3)
67  {
69  (
70  "writeGeometry(Ostream&, const pointField&, const faceList&)"
71  ) << "Face " << faceI << " vertices " << f
72  << " is not a triangle."
73  << exit(FatalError);
74  }
75 
76  os << f[0] << ' ' << f[1] << ' ' << f[2] << nl;
77  }
78  os << "attribute \"element type\" string \"triangles\"" << nl
79  << "attribute \"ref\" string \"positions\"" << nl << nl;
80 }
81 
82 
83 namespace Foam
84 {
85  // Write scalarField in DX format
86  template<>
88  (
89  Ostream& os,
90  const Field<scalar>& values
91  )
92  {
93  // Write data
94  os << "object 3 class array type float rank 0 items "
95  << values.size() << " data follows" << nl;
96 
97  forAll(values, elemI)
98  {
99  os << float(values[elemI]) << nl;
100  }
101  }
102 
103 
104  // Write vectorField in DX format
105  template<>
107  (
108  Ostream& os,
109  const Field<vector>& values
110  )
111  {
112  // Write data
113  os << "object 3 class array type float rank 1 shape 3 items "
114  << values.size() << " data follows" << nl;
115 
116  forAll(values, elemI)
117  {
118  os << float(values[elemI].x()) << ' '
119  << float(values[elemI].y()) << ' '
120  << float(values[elemI].z()) << nl;
121  }
122  }
123 
124 
125  // Write sphericalTensorField in DX format
126  template<>
128  (
129  Ostream& os,
130  const Field<sphericalTensor>& values
131  )
132  {
133  // Write data
134  os << "object 3 class array type float rank 0 items "
135  << values.size() << " data follows" << nl;
136 
137  forAll(values, elemI)
138  {
139  os << float(values[elemI][0]) << nl;
140  }
141  }
142 
143 
144  // Write symmTensorField in DX format
145  template<>
147  (
148  Ostream& os,
149  const Field<symmTensor>& values
150  )
151  {
152  // Write data
153  os << "object 3 class array type float rank 2 shape 3 items "
154  << values.size() << " data follows" << nl;
155 
156  forAll(values, elemI)
157  {
158  const symmTensor& t = values[elemI];
159 
160  os << float(t.xx()) << ' ' << float(t.xy()) << ' ' << float(t.xz())
161  << float(t.xy()) << ' ' << float(t.yy()) << ' ' << float(t.yz())
162  << float(t.xz()) << ' ' << float(t.yz()) << ' ' << float(t.zz())
163  << nl;
164  }
165  }
166 
167 
168  // Write tensorField in DX format
169  template<>
171  (
172  Ostream& os,
173  const Field<tensor>& values
174  )
175  {
176  // Write data
177  os << "object 3 class array type float rank 2 shape 3 items "
178  << values.size() << " data follows" << nl;
179 
180  forAll(values, elemI)
181  {
182  const tensor& t = values[elemI];
183 
184  os << float(t.xx()) << ' ' << float(t.xy()) << ' ' << float(t.xz())
185  << float(t.yx()) << ' ' << float(t.yy()) << ' ' << float(t.yz())
186  << float(t.zx()) << ' ' << float(t.zy()) << ' ' << float(t.zz())
187  << nl;
188  }
189  }
190 }
191 
192 // Write tensorField in DX format
193 template<class Type>
195 (
196  Ostream& os,
197  const Field<Type>& values
198 )
199 {
200  // Write data
201  os << "object 3 class array type float rank 0 items "
202  << values.size() << " data follows" << nl;
203 
204  forAll(values, elemI)
205  {
206  os << float(0.0) << nl;
207  }
208 }
209 
210 
211 // Write trailer in DX format
212 template<class Type>
214 {
215  os << "# the field, with three components: \"positions\","
216  << " \"connections\", and \"data\"" << nl
217  << "object \"irregular positions irregular "
218  << "connections\" class field"
219  << nl
220  << "component \"positions\" value 1" << nl
221  << "component \"connections\" value 2" << nl
222  << "component \"data\" value 3" << nl;
223 }
224 
225 
226 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
227 
228 template<class Type>
230 :
231  surfaceWriter<Type>()
232 {}
233 
234 
235 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
236 
237 template<class Type>
239 {}
240 
241 
242 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
243 
244 template<class Type>
246 (
247  const fileName& outputDir,
248  const fileName& surfaceName,
249  const pointField& points,
250  const faceList& faces,
251  const fileName& fieldName,
252  const Field<Type>& values,
253  const bool verbose
254 ) const
255 {
256  if (!isDir(outputDir))
257  {
258  mkDir(outputDir);
259  }
260 
261  OFstream os
262  (
263  outputDir/fieldName + '_' + surfaceName + ".dx"
264  );
265 
266  if (verbose)
267  {
268  Info<< "Writing field " << fieldName << " to " << os.name() << endl;
269  }
270 
271  writeGeometry(os, points, faces);
272 
273  writeData(os, values);
274 
275  if (values.size() == points.size())
276  {
277  os << nl << "attribute \"dep\" string \"positions\""
278  << nl << nl;
279  }
280  else
281  {
282  os << nl << "attribute \"dep\" string \"connections\""
283  << nl << nl;
284  }
285 
286  writeTrailer(os);
287 
288  os << "end" << nl;
289 }
290 
291 
292 // ************************ vim: set sw=4 sts=4 et: ************************ //