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-2015 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 
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<doubleScalar>& fField
84 )
85 {
86  os.setf(ios_base::fixed, ios_base::floatfield);
87  if (binary)
88  {
89 # ifdef LITTLEENDIAN
90  swapWords(fField.size(), reinterpret_cast<label*>(fField.begin()));
91 # endif
92  os.write
93  (
94  reinterpret_cast<char*>(fField.begin()),
95  fField.size()*sizeof(double)
96  );
97 
98  os << std::endl;
99  }
100  else
101  {
102  forAll(fField, i)
103  {
104  os << fField[i];
105 
106  if (i > 0 && (i % 10) == 0)
107  {
108  os << std::endl;
109  }
110  else
111  {
112  os << ' ';
113  }
114  }
115  os << std::endl;
116  }
117 }
118 
119 
121 (
122  std::ostream& os,
123  const bool binary,
125 )
126 {
127  os.setf(ios_base::fixed, ios_base::floatfield);
128  List<doubleScalar>& fld = fField.shrink();
129 
130  write(os, binary, fld);
131 }
132 
133 
135 (
136  std::ostream& os,
137  const bool binary,
138  labelList& elems
139 )
140 {
141  os.setf(ios_base::fixed, ios_base::floatfield);
142  if (binary)
143  {
144 # ifdef LITTLEENDIAN
145  swapWords(elems.size(), reinterpret_cast<label*>(elems.begin()));
146 # endif
147  os.write
148  (
149  reinterpret_cast<char*>(elems.begin()),
150  elems.size()*sizeof(label)
151  );
152 
153  os << std::endl;
154  }
155  else
156  {
157  forAll(elems, i)
158  {
159  os << elems[i];
160 
161  if (i > 0 && (i % 10) == 0)
162  {
163  os << std::endl;
164  }
165  else
166  {
167  os << ' ';
168  }
169  }
170  os << std::endl;
171  }
172 }
173 
174 
176 (
177  std::ostream& os,
178  const bool binary,
179  DynamicList<label>& elems
180 )
181 {
182  os.setf(ios_base::fixed, ios_base::floatfield);
183  labelList& fld = elems.shrink();
184 
185  write(os, binary, fld);
186 }
187 
188 
190 (
191  std::ostream& os,
192  const bool binary,
193  const std::string& title
194 )
195 {
196  os << "# vtk DataFile Version 2.0" << std::endl
197  << title << std::endl;
198 
199  if (binary)
200  {
201  os << "BINARY" << std::endl;
202  }
203  else
204  {
205  os << "ASCII" << std::endl;
206  }
207 }
208 
209 
211 (
212  std::ostream& os,
213  const label nCells,
214  const label nFields
215 )
216 {
217  os << "CELL_DATA " << nCells << std::endl
218  << "FIELD attributes " << nFields << std::endl;
219 }
220 
221 
223 (
224  std::ostream& os,
225  const label nPoints,
226  const label nFields
227 )
228 {
229  os << "POINT_DATA " << nPoints << std::endl
230  << "FIELD attributes " << nFields << std::endl;
231 }
232 
233 
235 {
236  dest.append(double(src));
237 }
238 
239 
241 {
242  for (direction cmpt = 0; cmpt < vector::nComponents; ++cmpt)
243  {
244  dest.append(double(src[cmpt]));
245  }
246 }
247 
248 
250 (
251  const sphericalTensor& src,
253 )
254 {
255  for (direction cmpt = 0; cmpt < sphericalTensor::nComponents; ++cmpt)
256  {
257  dest.append(double(src[cmpt]));
258  }
259 }
260 
261 
263 (
264  const symmTensor& src,
266 )
267 {
268  dest.append(double(src.xx()));
269  dest.append(double(src.yy()));
270  dest.append(double(src.zz()));
271  dest.append(double(src.xy()));
272  dest.append(double(src.yz()));
273  dest.append(double(src.xz()));
274 }
275 
276 
278 {
279  for (direction cmpt = 0; cmpt < tensor::nComponents; ++cmpt)
280  {
281  dest.append(double(src[cmpt]));
282  }
283 }
284 
285 
287 {
288  dest.append(src);
289 }
290 
291 
292 // ************************************************************************* //
Foam::Tensor
Templated 3D tensor derived from VectorSpace adding construction from 9 components,...
Definition: complexI.H:224
Foam::SymmTensor< scalar >
writeFuns.H
Foam::writeFuns::writeCellDataHeader
static void writeCellDataHeader(std::ostream &, const label nCells, const label nFields)
Definition: writeFuns.C:211
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
vtkTopo.H
Foam::writeFuns::writePointDataHeader
static void writePointDataHeader(std::ostream &, const label nPoints, const label nFields)
Definition: writeFuns.C:223
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::writeFuns::writeHeader
static void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Definition: writeFuns.C:190
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::writeFuns::write
static void write(std::ostream &, const bool, DynamicList< floatScalar > &)
Write floats ascii or binary.
Definition: writeFuns.C:107
Foam::writeFuns::swapWord
static void swapWord(int32_t &word32)
Swap halves of word.
Definition: writeFuns.C:46
Foam::DynamicList::shrink
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:258
Foam::fixed
IOstream & fixed(IOstream &io)
Definition: IOstream.H:576
Foam::writeFuns::swapWords
static void swapWords(const label nWords, int32_t *words32)
Swap halves of word.
Definition: writeFuns.C:60
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::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::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::writeFuns::insert
static void insert(const point &, DynamicList< floatScalar > &dest)
Append point to given DynamicList.
Definition: writeFuns.C:170
write
Tcoeff write()