initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / utilities / mesh / manipulation / setSet / writeFuns.C
blob44bf3ecd81060d1a2469f61943acbcea7f0332bc
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
27 \*---------------------------------------------------------------------------*/
29 #include "writeFuns.H"
31 #if defined(__mips) && !defined(__SICORTEX__)
32 #include <standards.h>
33 #include <sys/endian.h>
34 #endif
36 #if defined(LITTLE_ENDIAN) \
37  || defined(_LITTLE_ENDIAN) \
38  || defined(__LITTLE_ENDIAN)
39 #   define LITTLEENDIAN 1
40 #elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN)
41 #   undef LITTLEENDIAN
42 #else
43 #   error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined."
44 #   error "Please add to compilation options"
45 #endif
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 void Foam::writeFuns::swapWord(label& word32)
51     char* mem =  reinterpret_cast<char*>(&word32);
53     char a = mem[0];
54     mem[0] = mem[3];
55     mem[3] = a;
57     a = mem[1];
58     mem[1] = mem[2];
59     mem[2] = a;
63 void Foam::writeFuns::swapWords(const label nWords, label* words32)
65     for (label i = 0; i < nWords; i++)
66     {
67         swapWord(words32[i]);
68     }
72 void Foam::writeFuns::write
74     std::ostream& os,
75     const bool binary,
76     List<floatScalar>& fField
79     if (binary)
80     {
81 #       ifdef LITTLEENDIAN
82         swapWords(fField.size(),  reinterpret_cast<label*>(fField.begin()));
83 #       endif
85         os.write
86         (
87             reinterpret_cast<char*>(fField.begin()),
88             fField.size()*sizeof(float)
89         );
91         os << std::endl;
92     }
93     else
94     {
95         forAll(fField, i)
96         {
97             os << fField[i] << ' ';
99             if (i > 0 && (i % 10) == 0)
100             {
101                 os << std::endl;
102             }
103         }
104         os << std::endl;
105     }
109 void Foam::writeFuns::write
111     std::ostream& os,
112     const bool binary,
113     DynamicList<floatScalar>& fField
116     List<floatScalar>& fld = fField.shrink();
118     write(os, binary, fld);
122 void Foam::writeFuns::write
124     std::ostream& os,
125     const bool binary,
126     labelList& elems
129     if (binary)
130     {
131 #       ifdef LITTLEENDIAN
132         swapWords(elems.size(),  reinterpret_cast<label*>(elems.begin()));
133 #       endif
134         os.write
135         (
136             reinterpret_cast<char*>(elems.begin()),
137             elems.size()*sizeof(label)
138         );
140         os << std::endl;
141     }
142     else
143     {
144         forAll(elems, i)
145         {
146             os << elems[i] << ' ';
148             if (i > 0 && (i % 10) == 0)
149             {
150                 os << std::endl;
151             }
152         }
153         os << std::endl;
154     }
158 void Foam::writeFuns::write
160     std::ostream& os,
161     const bool binary,
162     DynamicList<label>& elems
165     labelList& fld = elems.shrink();
167     write(os, binary, fld);
171 // Store vector in dest.
172 void Foam::writeFuns::insert(const point& pt, DynamicList<floatScalar>& dest)
174     dest.append(float(pt.x()));
175     dest.append(float(pt.y()));
176     dest.append(float(pt.z()));
180 // Store labelList in dest.
181 void Foam::writeFuns::insert(const labelList& source, DynamicList<label>& dest)
183     forAll(source, i)
184     {
185         dest.append(source[i]);
186     }
190 // Store scalarField in dest
191 void Foam::writeFuns::insert
193     const List<scalar>& source,
194     DynamicList<floatScalar>& dest
197     forAll(source, i)
198     {
199         dest.append(float(source[i]));
200     }
204 // Store scalarField (indexed through map) in dest
205 void Foam::writeFuns::insert
207     const labelList& map,
208     const List<scalar>& source,
209     DynamicList<floatScalar>& dest
212     forAll(map, i)
213     {
214         dest.append(float(source[map[i]]));
215     }
219 void Foam::writeFuns::insert
221     const List<point>& source,
222     DynamicList<floatScalar>& dest
225     forAll(source, i)
226     {
227        insert(source[i], dest);
228     }
231 void Foam::writeFuns::insert
233     const labelList& map,
234     const List<point>& source,
235     DynamicList<floatScalar>& dest
238     forAll(map, i)
239     {
240        insert(source[map[i]], dest);
241     }
245 // ************************************************************************* //