OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "writeFuns.H"
00030 
00031 #if defined(__mips) && !defined(__SICORTEX__)
00032 #include <standards.h>
00033 #include <sys/endian.h>
00034 #endif
00035 
00036 #if defined(LITTLE_ENDIAN) \
00037  || defined(_LITTLE_ENDIAN) \
00038  || defined(__LITTLE_ENDIAN)
00039 #   define LITTLEENDIAN 1
00040 #elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN)
00041 #   undef LITTLEENDIAN
00042 #else
00043 #   error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined."
00044 #   error "Please add to compilation options"
00045 #endif
00046 
00047 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00048 
00049 void Foam::writeFuns::swapWord(label& word32)
00050 {
00051     char* mem =  reinterpret_cast<char*>(&word32);
00052 
00053     char a = mem[0];
00054     mem[0] = mem[3];
00055     mem[3] = a;
00056 
00057     a = mem[1];
00058     mem[1] = mem[2];
00059     mem[2] = a;
00060 }
00061 
00062 
00063 void Foam::writeFuns::swapWords(const label nWords, label* words32)
00064 {
00065     for (label i = 0; i < nWords; i++)
00066     {
00067         swapWord(words32[i]);
00068     }
00069 }
00070 
00071 
00072 void Foam::writeFuns::write
00073 (
00074     std::ostream& os,
00075     const bool binary,
00076     List<floatScalar>& fField
00077 )
00078 {
00079     if (binary)
00080     {
00081 #       ifdef LITTLEENDIAN
00082         swapWords(fField.size(),  reinterpret_cast<label*>(fField.begin()));
00083 #       endif
00084 
00085         os.write
00086         (
00087             reinterpret_cast<char*>(fField.begin()),
00088             fField.size()*sizeof(float)
00089         );
00090 
00091         os << std::endl;
00092     }
00093     else
00094     {
00095         forAll(fField, i)
00096         {
00097             os << fField[i] << ' ';
00098 
00099             if (i > 0 && (i % 10) == 0)
00100             {
00101                 os << std::endl;
00102             }
00103         }
00104         os << std::endl;
00105     }
00106 }
00107 
00108 
00109 void Foam::writeFuns::write
00110 (
00111     std::ostream& os,
00112     const bool binary,
00113     DynamicList<floatScalar>& fField
00114 )
00115 {
00116     List<floatScalar>& fld = fField.shrink();
00117 
00118     write(os, binary, fld);
00119 }
00120 
00121 
00122 void Foam::writeFuns::write
00123 (
00124     std::ostream& os,
00125     const bool binary,
00126     labelList& elems
00127 )
00128 {
00129     if (binary)
00130     {
00131 #       ifdef LITTLEENDIAN
00132         swapWords(elems.size(),  reinterpret_cast<label*>(elems.begin()));
00133 #       endif
00134         os.write
00135         (
00136             reinterpret_cast<char*>(elems.begin()),
00137             elems.size()*sizeof(label)
00138         );
00139 
00140         os << std::endl;
00141     }
00142     else
00143     {
00144         forAll(elems, i)
00145         {
00146             os << elems[i] << ' ';
00147 
00148             if (i > 0 && (i % 10) == 0)
00149             {
00150                 os << std::endl;
00151             }
00152         }
00153         os << std::endl;
00154     }
00155 }
00156 
00157 
00158 void Foam::writeFuns::write
00159 (
00160     std::ostream& os,
00161     const bool binary,
00162     DynamicList<label>& elems
00163 )
00164 {
00165     labelList& fld = elems.shrink();
00166 
00167     write(os, binary, fld);
00168 }
00169 
00170 
00171 // Store vector in dest.
00172 void Foam::writeFuns::insert(const point& pt, DynamicList<floatScalar>& dest)
00173 {
00174     dest.append(float(pt.x()));
00175     dest.append(float(pt.y()));
00176     dest.append(float(pt.z()));
00177 }
00178 
00179 
00180 // Store labelList in dest.
00181 void Foam::writeFuns::insert(const labelList& source, DynamicList<label>& dest)
00182 {
00183     forAll(source, i)
00184     {
00185         dest.append(source[i]);
00186     }
00187 }
00188 
00189 
00190 // Store scalarField in dest
00191 void Foam::writeFuns::insert
00192 (
00193     const List<scalar>& source,
00194     DynamicList<floatScalar>& dest
00195 )
00196 {
00197     forAll(source, i)
00198     {
00199         dest.append(float(source[i]));
00200     }
00201 }
00202 
00203 
00204 // Store scalarField (indexed through map) in dest
00205 void Foam::writeFuns::insert
00206 (
00207     const labelList& map,
00208     const List<scalar>& source,
00209     DynamicList<floatScalar>& dest
00210 )
00211 {
00212     forAll(map, i)
00213     {
00214         dest.append(float(source[map[i]]));
00215     }
00216 }
00217 
00218 
00219 void Foam::writeFuns::insert
00220 (
00221     const List<point>& source,
00222     DynamicList<floatScalar>& dest
00223 )
00224 {
00225     forAll(source, i)
00226     {
00227        insert(source[i], dest);
00228     }
00229 }
00230 
00231 void Foam::writeFuns::insert
00232 (
00233     const labelList& map,
00234     const List<point>& source,
00235     DynamicList<floatScalar>& dest
00236 )
00237 {
00238     forAll(map, i)
00239     {
00240        insert(source[map[i]], dest);
00241     }
00242 }
00243 
00244 
00245 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd