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 
28 #if defined(__mips) && !defined(__SICORTEX__)
29 #include <standards.h>
30 #include <sys/endian.h>
31 #endif
32 
33 #if defined(LITTLE_ENDIAN) \
34  || defined(_LITTLE_ENDIAN) \
35  || defined(__LITTLE_ENDIAN)
36 # define LITTLEENDIAN 1
37 #elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN)
38 # undef LITTLEENDIAN
39 #else
40 # error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined."
41 # error "Please add to compilation options"
42 #endif
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 void Foam::writeFuns::swapWord(label& word32)
47 {
48  char* mem = reinterpret_cast<char*>(&word32);
49 
50  char a = mem[0];
51  mem[0] = mem[3];
52  mem[3] = a;
53 
54  a = mem[1];
55  mem[1] = mem[2];
56  mem[2] = a;
57 }
58 
59 
60 void Foam::writeFuns::swapWords(const label nWords, label* words32)
61 {
62  for (label i = 0; i < nWords; i++)
63  {
64  swapWord(words32[i]);
65  }
66 }
67 
68 
70 (
71  std::ostream& os,
72  const bool binary,
73  List<floatScalar>& fField
74 )
75 {
76  if (binary)
77  {
78 # ifdef LITTLEENDIAN
79  swapWords(fField.size(), reinterpret_cast<label*>(fField.begin()));
80 # endif
81 
82  os.write
83  (
84  reinterpret_cast<char*>(fField.begin()),
85  fField.size()*sizeof(float)
86  );
87 
88  os << std::endl;
89  }
90  else
91  {
92  forAll(fField, i)
93  {
94  os << fField[i] << ' ';
95 
96  if (i > 0 && (i % 10) == 0)
97  {
98  os << std::endl;
99  }
100  }
101  os << std::endl;
102  }
103 }
104 
105 
107 (
108  std::ostream& os,
109  const bool binary,
111 )
112 {
113  List<floatScalar>& fld = fField.shrink();
114 
115  write(os, binary, fld);
116 }
117 
118 
120 (
121  std::ostream& os,
122  const bool binary,
123  labelList& elems
124 )
125 {
126  if (binary)
127  {
128 # ifdef LITTLEENDIAN
129  swapWords(elems.size(), reinterpret_cast<label*>(elems.begin()));
130 # endif
131  os.write
132  (
133  reinterpret_cast<char*>(elems.begin()),
134  elems.size()*sizeof(label)
135  );
136 
137  os << std::endl;
138  }
139  else
140  {
141  forAll(elems, i)
142  {
143  os << elems[i] << ' ';
144 
145  if (i > 0 && (i % 10) == 0)
146  {
147  os << std::endl;
148  }
149  }
150  os << std::endl;
151  }
152 }
153 
154 
156 (
157  std::ostream& os,
158  const bool binary,
159  DynamicList<label>& elems
160 )
161 {
162  labelList& fld = elems.shrink();
163 
164  write(os, binary, fld);
165 }
166 
167 
168 // Store vector in dest.
170 {
171  dest.append(float(pt.x()));
172  dest.append(float(pt.y()));
173  dest.append(float(pt.z()));
174 }
175 
176 
177 // Store labelList in dest.
178 void Foam::writeFuns::insert(const labelList& source, DynamicList<label>& dest)
179 {
180  forAll(source, i)
181  {
182  dest.append(source[i]);
183  }
184 }
185 
186 
187 // Store scalarField in dest
189 (
190  const List<scalar>& source,
192 )
193 {
194  forAll(source, i)
195  {
196  dest.append(float(source[i]));
197  }
198 }
199 
200 
201 // Store scalarField (indexed through map) in dest
203 (
204  const labelList& map,
205  const List<scalar>& source,
207 )
208 {
209  forAll(map, i)
210  {
211  dest.append(float(source[map[i]]));
212  }
213 }
214 
215 
217 (
218  const List<point>& source,
220 )
221 {
222  forAll(source, i)
223  {
224  insert(source[i], dest);
225  }
226 }
227 
229 (
230  const labelList& map,
231  const List<point>& source,
233 )
234 {
235  forAll(map, i)
236  {
237  insert(source[map[i]], dest);
238  }
239 }
240 
241 
242 // ************************ vim: set sw=4 sts=4 et: ************************ //