FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

writeFuns.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "writeFuns.H"
00027 
00028 #if defined(__mips) && !defined(__SICORTEX__)
00029 #include <standards.h>
00030 #include <sys/endian.h>
00031 #endif
00032 
00033 #if defined(LITTLE_ENDIAN) \
00034  || defined(_LITTLE_ENDIAN) \
00035  || defined(__LITTLE_ENDIAN)
00036 #   define LITTLEENDIAN 1
00037 #elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN)
00038 #   undef LITTLEENDIAN
00039 #else
00040 #   error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined."
00041 #   error "Please add to compilation options"
00042 #endif
00043 
00044 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00045 
00046 void Foam::writeFuns::swapWord(label& word32)
00047 {
00048     char* mem =  reinterpret_cast<char*>(&word32);
00049 
00050     char a = mem[0];
00051     mem[0] = mem[3];
00052     mem[3] = a;
00053 
00054     a = mem[1];
00055     mem[1] = mem[2];
00056     mem[2] = a;
00057 }
00058 
00059 
00060 void Foam::writeFuns::swapWords(const label nWords, label* words32)
00061 {
00062     for (label i = 0; i < nWords; i++)
00063     {
00064         swapWord(words32[i]);
00065     }
00066 }
00067 
00068 
00069 void Foam::writeFuns::write
00070 (
00071     std::ostream& os,
00072     const bool binary,
00073     List<floatScalar>& fField
00074 )
00075 {
00076     if (binary)
00077     {
00078 #       ifdef LITTLEENDIAN
00079         swapWords(fField.size(),  reinterpret_cast<label*>(fField.begin()));
00080 #       endif
00081 
00082         os.write
00083         (
00084             reinterpret_cast<char*>(fField.begin()),
00085             fField.size()*sizeof(float)
00086         );
00087 
00088         os << std::endl;
00089     }
00090     else
00091     {
00092         forAll(fField, i)
00093         {
00094             os << fField[i] << ' ';
00095 
00096             if (i > 0 && (i % 10) == 0)
00097             {
00098                 os << std::endl;
00099             }
00100         }
00101         os << std::endl;
00102     }
00103 }
00104 
00105 
00106 void Foam::writeFuns::write
00107 (
00108     std::ostream& os,
00109     const bool binary,
00110     DynamicList<floatScalar>& fField
00111 )
00112 {
00113     List<floatScalar>& fld = fField.shrink();
00114 
00115     write(os, binary, fld);
00116 }
00117 
00118 
00119 void Foam::writeFuns::write
00120 (
00121     std::ostream& os,
00122     const bool binary,
00123     labelList& elems
00124 )
00125 {
00126     if (binary)
00127     {
00128 #       ifdef LITTLEENDIAN
00129         swapWords(elems.size(),  reinterpret_cast<label*>(elems.begin()));
00130 #       endif
00131         os.write
00132         (
00133             reinterpret_cast<char*>(elems.begin()),
00134             elems.size()*sizeof(label)
00135         );
00136 
00137         os << std::endl;
00138     }
00139     else
00140     {
00141         forAll(elems, i)
00142         {
00143             os << elems[i] << ' ';
00144 
00145             if (i > 0 && (i % 10) == 0)
00146             {
00147                 os << std::endl;
00148             }
00149         }
00150         os << std::endl;
00151     }
00152 }
00153 
00154 
00155 void Foam::writeFuns::write
00156 (
00157     std::ostream& os,
00158     const bool binary,
00159     DynamicList<label>& elems
00160 )
00161 {
00162     labelList& fld = elems.shrink();
00163 
00164     write(os, binary, fld);
00165 }
00166 
00167 
00168 // Store vector in dest.
00169 void Foam::writeFuns::insert(const point& pt, DynamicList<floatScalar>& dest)
00170 {
00171     dest.append(float(pt.x()));
00172     dest.append(float(pt.y()));
00173     dest.append(float(pt.z()));
00174 }
00175 
00176 
00177 // Store labelList in dest.
00178 void Foam::writeFuns::insert(const labelList& source, DynamicList<label>& dest)
00179 {
00180     forAll(source, i)
00181     {
00182         dest.append(source[i]);
00183     }
00184 }
00185 
00186 
00187 // Store scalarField in dest
00188 void Foam::writeFuns::insert
00189 (
00190     const List<scalar>& source,
00191     DynamicList<floatScalar>& dest
00192 )
00193 {
00194     forAll(source, i)
00195     {
00196         dest.append(float(source[i]));
00197     }
00198 }
00199 
00200 
00201 // Store scalarField (indexed through map) in dest
00202 void Foam::writeFuns::insert
00203 (
00204     const labelList& map,
00205     const List<scalar>& source,
00206     DynamicList<floatScalar>& dest
00207 )
00208 {
00209     forAll(map, i)
00210     {
00211         dest.append(float(source[map[i]]));
00212     }
00213 }
00214 
00215 
00216 void Foam::writeFuns::insert
00217 (
00218     const List<point>& source,
00219     DynamicList<floatScalar>& dest
00220 )
00221 {
00222     forAll(source, i)
00223     {
00224        insert(source[i], dest);
00225     }
00226 }
00227 
00228 void Foam::writeFuns::insert
00229 (
00230     const labelList& map,
00231     const List<point>& source,
00232     DynamicList<floatScalar>& dest
00233 )
00234 {
00235     forAll(map, i)
00236     {
00237        insert(source[map[i]], dest);
00238     }
00239 }
00240 
00241 
00242 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines