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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "writeFuns.H"
00028 #include "vtkTopo.H"
00029 
00030 #if defined(__mips) && !defined(__SICORTEX__)
00031 #include <standards.h>
00032 #include <sys/endian.h>
00033 #endif
00034 
00035 // MacOSX
00036 #ifdef __DARWIN_BYTE_ORDER
00037 #if __DARWIN_BYTE_ORDER==__DARWIN_BIG_ENDIAN
00038 #undef LITTLE_ENDIAN
00039 #else
00040 #undef BIG_ENDIAN
00041 #endif
00042 #endif
00043 
00044 #if defined(LITTLE_ENDIAN) \
00045  || defined(_LITTLE_ENDIAN) \
00046  || defined(__LITTLE_ENDIAN)
00047 #   define LITTLEENDIAN 1
00048 #elif defined(BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN)
00049 #   undef LITTLEENDIAN
00050 #else
00051 #   error "Cannot find LITTLE_ENDIAN or BIG_ENDIAN symbol defined."
00052 #   error "Please add to compilation options"
00053 #endif
00054 
00055 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00056 
00057 void Foam::writeFuns::swapWord(label& word32)
00058 {
00059     char* mem = reinterpret_cast<char*>(&word32);
00060 
00061     char a = mem[0];
00062     mem[0] = mem[3];
00063     mem[3] = a;
00064 
00065     a = mem[1];
00066     mem[1] = mem[2];
00067     mem[2] = a;
00068 }
00069 
00070 
00071 void Foam::writeFuns::swapWords(const label nWords, label* words32)
00072 {
00073     for (label i = 0; i < nWords; i++)
00074     {
00075         swapWord(words32[i]);
00076     }
00077 }
00078 
00079 
00080 void Foam::writeFuns::write
00081 (
00082     std::ostream& os,
00083     const bool binary,
00084     List<floatScalar>& fField
00085 )
00086 {
00087     if (binary)
00088     {
00089 #       ifdef LITTLEENDIAN
00090         swapWords(fField.size(), reinterpret_cast<label*>(fField.begin()));
00091 #       endif
00092         os.write
00093         (
00094             reinterpret_cast<char*>(fField.begin()),
00095             fField.size()*sizeof(float)
00096         );
00097 
00098         os << std::endl;
00099     }
00100     else
00101     {
00102         forAll(fField, i)
00103         {
00104             os << fField[i] << ' ';
00105 
00106             if (i > 0 && (i % 10) == 0)
00107             {
00108                 os << std::endl;
00109             }
00110         }
00111         os << std::endl;
00112     }
00113 }
00114 
00115 
00116 void Foam::writeFuns::write
00117 (
00118     std::ostream& os,
00119     const bool binary,
00120     DynamicList<floatScalar>& fField
00121 )
00122 {
00123     List<floatScalar>& fld = fField.shrink();
00124 
00125     write(os, binary, fld);
00126 }
00127 
00128 
00129 void Foam::writeFuns::write
00130 (
00131     std::ostream& os,
00132     const bool binary,
00133     labelList& elems
00134 )
00135 {
00136     if (binary)
00137     {
00138 #       ifdef LITTLEENDIAN
00139         swapWords(elems.size(), reinterpret_cast<label*>(elems.begin()));
00140 #       endif
00141         os.write
00142         (
00143             reinterpret_cast<char*>(elems.begin()),
00144             elems.size()*sizeof(label)
00145         );
00146 
00147         os << std::endl;
00148     }
00149     else
00150     {
00151         forAll(elems, i)
00152         {
00153             os << elems[i] << ' ';
00154 
00155             if (i > 0 && (i % 10) == 0)
00156             {
00157                 os << std::endl;
00158             }
00159         }
00160         os << std::endl;
00161     }
00162 }
00163 
00164 
00165 void Foam::writeFuns::write
00166 (
00167     std::ostream& os,
00168     const bool binary,
00169     DynamicList<label>& elems
00170 )
00171 {
00172     labelList& fld = elems.shrink();
00173 
00174     write(os, binary, fld);
00175 }
00176 
00177 
00178 void Foam::writeFuns::writeHeader
00179 (
00180     std::ostream& os,
00181     const bool binary,
00182     const string& name
00183 )
00184 {
00185     os  << "# vtk DataFile Version 2.0" << std::endl
00186         << name << std::endl;
00187 
00188     if (binary)
00189     {
00190         os << "BINARY" << std::endl;
00191     }
00192     else
00193     {
00194         os << "ASCII" << std::endl;
00195     }
00196 }
00197 
00198 
00199 void Foam::writeFuns::writeCellDataHeader
00200 (
00201     std::ostream& os,
00202     const label nCells,
00203     const label nFields
00204 )
00205 {
00206     os  << "CELL_DATA " << nCells << std::endl
00207         << "FIELD attributes " << nFields << std::endl;
00208 }
00209 
00210 
00211 void Foam::writeFuns::writePointDataHeader
00212 (
00213     std::ostream& os,
00214     const label nPoints,
00215     const label nFields
00216 )
00217 {
00218     os  << "POINT_DATA  " << nPoints << std::endl
00219         << "FIELD attributes " << nFields << std::endl;
00220 }
00221 
00222 
00223 void Foam::writeFuns::insert(const scalar& pt, DynamicList<floatScalar>& dest)
00224 {
00225     dest.append(float(pt));
00226 }
00227 
00228 
00229 void Foam::writeFuns::insert(const vector& pt, DynamicList<floatScalar>& dest)
00230 {
00231     for (direction cmpt = 0; cmpt < vector::nComponents; cmpt++)
00232     {
00233         dest.append(float(pt[cmpt]));
00234     }
00235 }
00236 
00237 
00238 void Foam::writeFuns::insert
00239 (
00240     const sphericalTensor& pt,
00241     DynamicList<floatScalar>& dest
00242 )
00243 {
00244     for (direction cmpt = 0; cmpt < sphericalTensor::nComponents; cmpt++)
00245     {
00246         dest.append(float(pt[cmpt]));
00247     }
00248 }
00249 
00250 
00251 void Foam::writeFuns::insert
00252 (
00253     const symmTensor& pt,
00254     DynamicList<floatScalar>& dest
00255 )
00256 {
00257     for (direction cmpt = 0; cmpt < symmTensor::nComponents; cmpt++)
00258     {
00259         dest.append(float(pt[cmpt]));
00260     }
00261 }
00262 
00263 
00264 void Foam::writeFuns::insert(const tensor& pt, DynamicList<floatScalar>& dest)
00265 {
00266     for (direction cmpt = 0; cmpt < tensor::nComponents; cmpt++)
00267     {
00268         dest.append(float(pt[cmpt]));
00269     }
00270 }
00271 
00272 
00273 void Foam::writeFuns::insert(const labelList& source, DynamicList<label>& dest)
00274 {
00275     forAll(source, i)
00276     {
00277         dest.append(source[i]);
00278     }
00279 }
00280 
00281 
00282 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd