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) 2011-2016 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 "writeFuns.H"
27 #include "vtkTopo.H"
28 
29 #if defined(__mips)
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  else
110  {
111  os << ' ';
112  }
113  }
114  os << std::endl;
115  }
116 }
117 
118 
120 (
121  std::ostream& os,
122  const bool binary,
123  DynamicList<floatScalar>& fField
124 )
125 {
126  List<floatScalar>& fld = fField.shrink();
127 
128  write(os, binary, fld);
129 }
130 
131 
133 (
134  std::ostream& os,
135  const bool binary,
136  labelList& elems
137 )
138 {
139  if (binary)
140  {
141  #ifdef LITTLEENDIAN
142  swapWords(elems.size(), reinterpret_cast<label*>(elems.begin()));
143  #endif
144  os.write
145  (
146  reinterpret_cast<char*>(elems.begin()),
147  elems.size()*sizeof(label)
148  );
149 
150  os << std::endl;
151  }
152  else
153  {
154  forAll(elems, i)
155  {
156  os << elems[i];
157 
158  if (i > 0 && (i % 10) == 0)
159  {
160  os << std::endl;
161  }
162  else
163  {
164  os << ' ';
165  }
166  }
167  os << std::endl;
168  }
169 }
170 
171 
173 (
174  std::ostream& os,
175  const bool binary,
176  DynamicList<label>& elems
177 )
178 {
179  labelList& fld = elems.shrink();
180 
181  write(os, binary, fld);
182 }
183 
184 
186 (
187  std::ostream& os,
188  const bool binary,
189  const std::string& title
190 )
191 {
192  os << "# vtk DataFile Version 2.0" << std::endl
193  << title << std::endl;
194 
195  if (binary)
196  {
197  os << "BINARY" << std::endl;
198  }
199  else
200  {
201  os << "ASCII" << std::endl;
202  }
203 }
204 
205 
207 (
208  std::ostream& os,
209  const label nCells,
210  const label nFields
211 )
212 {
213  os << "CELL_DATA " << nCells << std::endl
214  << "FIELD attributes " << nFields << std::endl;
215 }
216 
217 
219 (
220  std::ostream& os,
221  const label nPoints,
222  const label nFields
223 )
224 {
225  os << "POINT_DATA " << nPoints << std::endl
226  << "FIELD attributes " << nFields << std::endl;
227 }
228 
229 
230 void Foam::writeFuns::insert(const scalar src, DynamicList<floatScalar>& dest)
231 {
232  dest.append(float(src));
233 }
234 
235 
236 void Foam::writeFuns::insert(const vector& src, DynamicList<floatScalar>& dest)
237 {
238  for (direction cmpt = 0; cmpt < vector::nComponents; ++cmpt)
239  {
240  dest.append(float(src[cmpt]));
241  }
242 }
243 
244 
246 (
247  const sphericalTensor& src,
248  DynamicList<floatScalar>& dest
249 )
250 {
251  for (direction cmpt = 0; cmpt < sphericalTensor::nComponents; ++cmpt)
252  {
253  dest.append(float(src[cmpt]));
254  }
255 }
256 
257 
259 (
260  const symmTensor& src,
261  DynamicList<floatScalar>& dest
262 )
263 {
264  dest.append(float(src.xx()));
265  dest.append(float(src.yy()));
266  dest.append(float(src.zz()));
267  dest.append(float(src.xy()));
268  dest.append(float(src.yz()));
269  dest.append(float(src.xz()));
270 }
271 
272 
273 void Foam::writeFuns::insert(const tensor& src, DynamicList<floatScalar>& dest)
274 {
275  for (direction cmpt = 0; cmpt < tensor::nComponents; ++cmpt)
276  {
277  dest.append(float(src[cmpt]));
278  }
279 }
280 
281 
282 void Foam::writeFuns::insert(const labelList& src, DynamicList<label>& dest)
283 {
284  dest.append(src);
285 }
286 
287 
288 // ************************************************************************* //
static void writeCellDataHeader(std::ostream &, const label nCells, const label nFields)
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
uint8_t direction
Definition: direction.H:46
static void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static void insert(const point &, DynamicList< floatScalar > &dest)
Append point to given DynamicList.
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
static const direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:96
SymmTensor< scalar > symmTensor
SymmTensor of scalars.
Definition: symmTensor.H:48
label nPoints
List< label > labelList
A List of labels.
Definition: labelList.H:56
static void write(std::ostream &, const bool, DynamicList< floatScalar > &)
Write floats ascii or binary.
SphericalTensor< scalar > sphericalTensor
SphericalTensor of scalars.
Tensor< scalar > tensor
Tensor of scalars.
Definition: tensor.H:51
static void writePointDataHeader(std::ostream &, const label nPoints, const label nFields)