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