vtkTools.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-2014 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 "vtkTools.H"
27 
28 #if defined(__mips)
29 #include <standards.h>
30 #include <sys/endian.h>
31 #endif
32 
33 // MacOSX
34 #ifdef __DARWIN_BYTE_ORDER
35 #if __DARWIN_BYTE_ORDER==__DARWIN_BIG_ENDIAN
36 #undef LITTLE_ENDIAN
37 #else
38 #undef BIG_ENDIAN
39 #endif
40 #endif
41 
42 #if defined(LITTLE_ENDIAN) \
43  || defined(_LITTLE_ENDIAN) \
44  || defined(__LITTLE_ENDIAN)
45 # define LITTLEENDIAN 1
46 #elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN)
47 # undef LITTLEENDIAN
48 #else
49 # error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined."
50 # error "Please add to compilation options"
51 #endif
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
56 {
57  char* mem = reinterpret_cast<char*>(&word32);
58 
59  char a = mem[0];
60  mem[0] = mem[3];
61  mem[3] = a;
62 
63  a = mem[1];
64  mem[1] = mem[2];
65  mem[2] = a;
66 }
67 
68 
69 void Foam::vtkTools::swapWords(const label nWords, label* words32)
70 {
71  for (label i = 0; i < nWords; i++)
72  {
73  swapWord(words32[i]);
74  }
75 }
76 
77 
79 (
80  std::ostream& os,
81  const bool binary,
82  List<floatScalar>& fField
83 )
84 {
85  if (binary)
86  {
87 # ifdef LITTLEENDIAN
88  swapWords(fField.size(), reinterpret_cast<label*>(fField.begin()));
89 # endif
90  os.write
91  (
92  reinterpret_cast<char*>(fField.begin()),
93  fField.size()*sizeof(float)
94  );
95 
96  os << std::endl;
97  }
98  else
99  {
100  forAll(fField, i)
101  {
102  os << fField[i];
103 
104  if (i > 0 && (i % 10) == 0)
105  {
106  os << std::endl;
107  }
108  else
109  {
110  os << ' ';
111  }
112  }
113  os << std::endl;
114  }
115 }
116 
117 
119 (
120  std::ostream& os,
121  const bool binary,
123 )
124 {
125  List<floatScalar>& fld = fField.shrink();
126 
127  write(os, binary, fld);
128 }
129 
130 
132 (
133  std::ostream& os,
134  const bool binary,
135  labelList& elems
136 )
137 {
138  if (binary)
139  {
140 # ifdef LITTLEENDIAN
141  swapWords(elems.size(), reinterpret_cast<label*>(elems.begin()));
142 # endif
143  os.write
144  (
145  reinterpret_cast<char*>(elems.begin()),
146  elems.size()*sizeof(label)
147  );
148 
149  os << std::endl;
150  }
151  else
152  {
153  forAll(elems, i)
154  {
155  os << elems[i];
156 
157  if (i > 0 && (i % 10) == 0)
158  {
159  os << std::endl;
160  }
161  else
162  {
163  os << ' ';
164  }
165  }
166  os << std::endl;
167  }
168 }
169 
170 
172 (
173  std::ostream& os,
174  const bool binary,
175  DynamicList<label>& elems
176 )
177 {
178  labelList& fld = elems.shrink();
179 
180  write(os, binary, fld);
181 }
182 
183 
185 (
186  std::ostream& os,
187  const bool binary,
188  const std::string& title
189 )
190 {
191  os << "# vtk DataFile Version 2.0" << std::endl
192  << title << std::endl;
193 
194  if (binary)
195  {
196  os << "BINARY" << std::endl;
197  }
198  else
199  {
200  os << "ASCII" << std::endl;
201  }
202 }
203 
204 
206 (
207  std::ostream& os,
208  const label nCells,
209  const label nFields
210 )
211 {
212  os << "CELL_DATA " << nCells << std::endl
213  << "FIELD attributes " << nFields << std::endl;
214 }
215 
216 
218 (
219  std::ostream& os,
220  const label nPoints,
221  const label nFields
222 )
223 {
224  os << "POINT_DATA " << nPoints << std::endl
225  << "FIELD attributes " << nFields << std::endl;
226 }
227 
228 
230 {
231  dest.append(float(src));
232 }
233 
234 
236 {
237  for (direction cmpt = 0; cmpt < vector::nComponents; ++cmpt)
238  {
239  dest.append(float(src[cmpt]));
240  }
241 }
242 
243 
245 (
246  const sphericalTensor& src,
248 )
249 {
250  for (direction cmpt = 0; cmpt < sphericalTensor::nComponents; ++cmpt)
251  {
252  dest.append(float(src[cmpt]));
253  }
254 }
255 
256 
258 (
259  const symmTensor& src,
261 )
262 {
263  dest.append(float(src.xx()));
264  dest.append(float(src.yy()));
265  dest.append(float(src.zz()));
266  dest.append(float(src.xy()));
267  dest.append(float(src.yz()));
268  dest.append(float(src.xz()));
269 }
270 
271 
273 {
274  for (direction cmpt = 0; cmpt < tensor::nComponents; ++cmpt)
275  {
276  dest.append(float(src[cmpt]));
277  }
278 }
279 
280 
282 {
283  dest.append(src);
284 }
285 
286 
287 // ************************************************************************* //
Foam::Tensor
Templated 3D tensor derived from VectorSpace adding construction from 9 components,...
Definition: complexI.H:224
Foam::SymmTensor< scalar >
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
Foam::SymmTensor::zz
const Cmpt & zz() const
Definition: SymmTensorI.H:115
Foam::SymmTensor::xy
const Cmpt & xy() const
Definition: SymmTensorI.H:91
Foam::VectorSpace< Vector< scalar >, scalar, 3 >::nComponents
@ nComponents
Number of components in this vector space.
Definition: VectorSpace.H:88
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::vtkTools::swapWord
static void swapWord(label &word32)
Definition: vtkTools.C:55
Foam::SymmTensor::yz
const Cmpt & yz() const
Definition: SymmTensorI.H:109
Foam::label
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
Foam::SymmTensor::xx
const Cmpt & xx() const
Definition: SymmTensorI.H:85
Foam::vtkTools::writePointDataHeader
static void writePointDataHeader(std::ostream &, const label nPoints, const label nFields)
Definition: vtkTools.C:218
vtkTools.H
Foam::vtkTools::writeHeader
static void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Definition: vtkTools.C:185
Foam::DynamicList::shrink
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:258
fld
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< ' ';}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< ' ';}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< ' ';}gmvFile<< nl;forAll(lagrangianScalarNames, i){ const word &name=lagrangianScalarNames[i];IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Foam::vtkTools::writeCellDataHeader
static void writeCellDataHeader(std::ostream &, const label nCells, const label nFields)
Definition: vtkTools.C:206
Foam::SphericalTensor
Templated 3D SphericalTensor derived from VectorSpace adding construction from 1 component,...
Definition: SphericalTensor.H:51
Foam::DynamicList::append
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Foam::vtkTools::insert
static void insert(const scalar, DynamicList< floatScalar > &)
Definition: vtkTools.C:229
Foam::Vector< scalar >
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::SymmTensor::yy
const Cmpt & yy() const
Definition: SymmTensorI.H:103
Foam::direction
unsigned char direction
Definition: direction.H:43
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::SymmTensor::xz
const Cmpt & xz() const
Definition: SymmTensorI.H:97
Foam::vtkTools::swapWords
static void swapWords(const label nWords, label *words32)
Definition: vtkTools.C:69
write
Tcoeff write()
Foam::vtkTools::write
static void write(std::ostream &, const bool, List< floatScalar > &)
Definition: vtkTools.C:79