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 
28 #if defined(__mips)
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(int32_t& 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, int32_t* 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<int32_t*>(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,
110  DynamicList<floatScalar>& fField
111 )
112 {
113  List<floatScalar>& fld = fField.shrink();
114  write(os, binary, fld);
115 }
116 
117 
119 (
120  std::ostream& os,
121  const bool binary,
122  labelList& elems
123 )
124 {
125  if (binary)
126  {
127  #ifdef LITTLEENDIAN
128  swapWords
129  (
130  (sizeof(label)/4)*elems.size(),
131  reinterpret_cast<int32_t*>(elems.begin())
132  );
133  #endif
134  os.write
135  (
136  reinterpret_cast<char*>(elems.begin()),
137  elems.size()*sizeof(label)
138  );
139 
140  os << std::endl;
141  }
142  else
143  {
144  forAll(elems, i)
145  {
146  os << elems[i] << ' ';
147 
148  if (i > 0 && (i % 10) == 0)
149  {
150  os << std::endl;
151  }
152  }
153  os << std::endl;
154  }
155 }
156 
157 
159 (
160  std::ostream& os,
161  const bool binary,
162  DynamicList<label>& elems
163 )
164 {
165  labelList& fld = elems.shrink();
166  write(os, binary, fld);
167 }
168 
169 
170 void Foam::writeFuns::insert(const point& pt, DynamicList<floatScalar>& dest)
171 {
172  dest.append(float(pt.x()));
173  dest.append(float(pt.y()));
174  dest.append(float(pt.z()));
175 }
176 
177 
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 
188 (
189  const List<scalar>& source,
190  DynamicList<floatScalar>& dest
191 )
192 {
193  forAll(source, i)
194  {
195  dest.append(float(source[i]));
196  }
197 }
198 
199 
201 (
202  const labelList& map,
203  const List<scalar>& source,
204  DynamicList<floatScalar>& dest
205 )
206 {
207  forAll(map, i)
208  {
209  dest.append(float(source[map[i]]));
210  }
211 }
212 
213 
215 (
216  const List<point>& source,
217  DynamicList<floatScalar>& dest
218 )
219 {
220  forAll(source, i)
221  {
222  insert(source[i], dest);
223  }
224 }
225 
227 (
228  const labelList& map,
229  const List<point>& source,
230  DynamicList<floatScalar>& dest
231 )
232 {
233  forAll(map, i)
234  {
235  insert(source[map[i]], dest);
236  }
237 }
238 
239 
240 // ************************************************************************* //
#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
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.
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.
vector point
Point is a vector.
Definition: point.H:41