FreeFOAM The Cross-Platform CFD Toolkit
writeFuns.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 "writeFuns.H"
27 #include "vtkTopo.H"
28 
29 #if defined(__mips) && !defined(__SICORTEX__)
30 #include <standards.h>
31 #include <sys/endian.h>
32 #endif
33 
34 // MacOSX
35 #ifdef __DARWIN_BYTE_ORDER
36 #if __DARWIN_BYTE_ORDER==__DARWIN_BIG_ENDIAN
37 #undef LITTLE_ENDIAN
38 #else
39 #undef BIG_ENDIAN
40 #endif
41 #endif
42 
43 #if defined(LITTLE_ENDIAN) \
44  || defined(_LITTLE_ENDIAN) \
45  || defined(__LITTLE_ENDIAN)
46 # define LITTLEENDIAN 1
47 #elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN)
48 # undef LITTLEENDIAN
49 #else
50 # error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined."
51 # error "Please add to compilation options"
52 #endif
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 void Foam::writeFuns::swapWord(label& word32)
57 {
58  char* mem = reinterpret_cast<char*>(&word32);
59 
60  char a = mem[0];
61  mem[0] = mem[3];
62  mem[3] = a;
63 
64  a = mem[1];
65  mem[1] = mem[2];
66  mem[2] = a;
67 }
68 
69 
70 void Foam::writeFuns::swapWords(const label nWords, label* words32)
71 {
72  for (label i = 0; i < nWords; i++)
73  {
74  swapWord(words32[i]);
75  }
76 }
77 
78 
80 (
81  std::ostream& os,
82  const bool binary,
83  List<floatScalar>& fField
84 )
85 {
86  if (binary)
87  {
88 # ifdef LITTLEENDIAN
89  swapWords(fField.size(), reinterpret_cast<label*>(fField.begin()));
90 # endif
91  os.write
92  (
93  reinterpret_cast<char*>(fField.begin()),
94  fField.size()*sizeof(float)
95  );
96 
97  os << std::endl;
98  }
99  else
100  {
101  forAll(fField, i)
102  {
103  os << fField[i] << ' ';
104 
105  if (i > 0 && (i % 10) == 0)
106  {
107  os << std::endl;
108  }
109  }
110  os << std::endl;
111  }
112 }
113 
114 
116 (
117  std::ostream& os,
118  const bool binary,
119  DynamicList<floatScalar>& fField
120 )
121 {
122  List<floatScalar>& fld = fField.shrink();
123 
124  write(os, binary, fld);
125 }
126 
127 
129 (
130  std::ostream& os,
131  const bool binary,
132  labelList& elems
133 )
134 {
135  if (binary)
136  {
137 # ifdef LITTLEENDIAN
138  swapWords(elems.size(), reinterpret_cast<label*>(elems.begin()));
139 # endif
140  os.write
141  (
142  reinterpret_cast<char*>(elems.begin()),
143  elems.size()*sizeof(label)
144  );
145 
146  os << std::endl;
147  }
148  else
149  {
150  forAll(elems, i)
151  {
152  os << elems[i] << ' ';
153 
154  if (i > 0 && (i % 10) == 0)
155  {
156  os << std::endl;
157  }
158  }
159  os << std::endl;
160  }
161 }
162 
163 
165 (
166  std::ostream& os,
167  const bool binary,
168  DynamicList<label>& elems
169 )
170 {
171  labelList& fld = elems.shrink();
172 
173  write(os, binary, fld);
174 }
175 
176 
178 (
179  std::ostream& os,
180  const bool binary,
181  const string& name
182 )
183 {
184  os << "# vtk DataFile Version 2.0" << std::endl
185  << name << std::endl;
186 
187  if (binary)
188  {
189  os << "BINARY" << std::endl;
190  }
191  else
192  {
193  os << "ASCII" << std::endl;
194  }
195 }
196 
197 
199 (
200  std::ostream& os,
201  const label nCells,
202  const label nFields
203 )
204 {
205  os << "CELL_DATA " << nCells << std::endl
206  << "FIELD attributes " << nFields << std::endl;
207 }
208 
209 
211 (
212  std::ostream& os,
213  const label nPoints,
214  const label nFields
215 )
216 {
217  os << "POINT_DATA " << nPoints << std::endl
218  << "FIELD attributes " << nFields << std::endl;
219 }
220 
221 
222 void Foam::writeFuns::insert(const scalar& pt, DynamicList<floatScalar>& dest)
223 {
224  dest.append(float(pt));
225 }
226 
227 
228 void Foam::writeFuns::insert(const vector& pt, DynamicList<floatScalar>& dest)
229 {
230  for (direction cmpt = 0; cmpt < vector::nComponents; cmpt++)
231  {
232  dest.append(float(pt[cmpt]));
233  }
234 }
235 
236 
238 (
239  const sphericalTensor& pt,
240  DynamicList<floatScalar>& dest
241 )
242 {
243  for (direction cmpt = 0; cmpt < sphericalTensor::nComponents; cmpt++)
244  {
245  dest.append(float(pt[cmpt]));
246  }
247 }
248 
249 
251 (
252  const symmTensor& src,
253  DynamicList<floatScalar>& dest
254 )
255 {
256  dest.append(float(src.xx()));
257  dest.append(float(src.yy()));
258  dest.append(float(src.zz()));
259  dest.append(float(src.xy()));
260  dest.append(float(src.yz()));
261  dest.append(float(src.xz()));
262 }
263 
264 
265 void Foam::writeFuns::insert(const tensor& pt, DynamicList<floatScalar>& dest)
266 {
267  for (direction cmpt = 0; cmpt < tensor::nComponents; cmpt++)
268  {
269  dest.append(float(pt[cmpt]));
270  }
271 }
272 
273 
274 void Foam::writeFuns::insert(const labelList& source, DynamicList<label>& dest)
275 {
276  forAll(source, i)
277  {
278  dest.append(source[i]);
279  }
280 }
281 
282 
283 // ************************ vim: set sw=4 sts=4 et: ************************ //