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

dxSurfaceWriter.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 "dxSurfaceWriter.H"
00027 
00028 #include <OpenFOAM/OFstream.H>
00029 #include <OpenFOAM/OSspecific.H>
00030 
00031 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00032 
00033 template<class Type>
00034 void Foam::dxSurfaceWriter<Type>::writeGeometry
00035 (
00036     Ostream& os,
00037     const pointField& points,
00038     const faceList& faces
00039 )
00040 {
00041     // Write vertex coordinates
00042 
00043     os  << "# The irregular positions" << nl
00044         << "object 1 class array type float rank 1 shape 3 items "
00045         << points.size() << " data follows" << nl;
00046 
00047     forAll(points, pointI)
00048     {
00049         const point& pt = points[pointI];
00050 
00051         os  << float(pt.x()) << ' ' << float(pt.y()) << ' ' << float(pt.z())
00052             << nl;
00053     }
00054     os  << nl;
00055 
00056     // Write triangles
00057 
00058     os  << "# The irregular connections (triangles)" << nl
00059         << "object 2 class array type int rank 1 shape 3 items "
00060         << faces.size() << " data follows" << nl;
00061 
00062     forAll(faces, faceI)
00063     {
00064         const face& f = faces[faceI];
00065 
00066         if (f.size() != 3)
00067         {
00068             FatalErrorIn
00069             (
00070                 "writeGeometry(Ostream&, const pointField&, const faceList&)"
00071             )   << "Face " << faceI << " vertices " << f
00072                 << " is not a triangle."
00073                 << exit(FatalError);
00074         }
00075 
00076         os << f[0] << ' ' << f[1] << ' ' << f[2] << nl;
00077     }
00078     os << "attribute \"element type\" string \"triangles\"" << nl
00079        << "attribute \"ref\" string \"positions\"" << nl << nl;
00080 }
00081 
00082 
00083 namespace Foam
00084 {
00085     // Write scalarField in DX format
00086     template<>
00087     void Foam::dxSurfaceWriter<Foam::scalar>::writeData
00088     (
00089         Ostream& os,
00090         const Field<scalar>& values
00091     )
00092     {
00093         // Write data
00094         os  << "object 3 class array type float rank 0 items "
00095             << values.size() << " data follows" << nl;
00096 
00097         forAll(values, elemI)
00098         {
00099             os << float(values[elemI]) << nl;
00100         }
00101     }
00102 
00103 
00104     // Write vectorField in DX format
00105     template<>
00106     void Foam::dxSurfaceWriter<Foam::vector>::writeData
00107     (
00108         Ostream& os,
00109         const Field<vector>& values
00110     )
00111     {
00112         // Write data
00113         os  << "object 3 class array type float rank 1 shape 3 items "
00114             << values.size() << " data follows" << nl;
00115 
00116         forAll(values, elemI)
00117         {
00118             os  << float(values[elemI].x()) << ' '
00119                 << float(values[elemI].y()) << ' '
00120                 << float(values[elemI].z()) << nl;
00121         }
00122     }
00123 
00124 
00125     // Write sphericalTensorField in DX format
00126     template<>
00127     void Foam::dxSurfaceWriter<Foam::sphericalTensor>::writeData
00128     (
00129         Ostream& os,
00130         const Field<sphericalTensor>& values
00131     )
00132     {
00133         // Write data
00134         os  << "object 3 class array type float rank 0 items "
00135             << values.size() << " data follows" << nl;
00136 
00137         forAll(values, elemI)
00138         {
00139             os << float(values[elemI][0]) << nl;
00140         }
00141     }
00142 
00143 
00144     // Write symmTensorField in DX format
00145     template<>
00146     void Foam::dxSurfaceWriter<Foam::symmTensor>::writeData
00147     (
00148         Ostream& os,
00149         const Field<symmTensor>& values
00150     )
00151     {
00152         // Write data
00153         os  << "object 3 class array type float rank 2 shape 3 items "
00154             << values.size() << " data follows" << nl;
00155 
00156         forAll(values, elemI)
00157         {
00158             const symmTensor& t = values[elemI];
00159 
00160             os  << float(t.xx()) << ' ' << float(t.xy()) << ' ' << float(t.xz())
00161                 << float(t.xy()) << ' ' << float(t.yy()) << ' ' << float(t.yz())
00162                 << float(t.xz()) << ' ' << float(t.yz()) << ' ' << float(t.zz())
00163                 << nl;
00164         }
00165     }
00166 
00167 
00168     // Write tensorField in DX format
00169     template<>
00170     void Foam::dxSurfaceWriter<Foam::tensor>::writeData
00171     (
00172         Ostream& os,
00173         const Field<tensor>& values
00174     )
00175     {
00176         // Write data
00177         os  << "object 3 class array type float rank 2 shape 3 items "
00178             << values.size() << " data follows" << nl;
00179 
00180         forAll(values, elemI)
00181         {
00182             const tensor& t = values[elemI];
00183 
00184             os  << float(t.xx()) << ' ' << float(t.xy()) << ' ' << float(t.xz())
00185                 << float(t.yx()) << ' ' << float(t.yy()) << ' ' << float(t.yz())
00186                 << float(t.zx()) << ' ' << float(t.zy()) << ' ' << float(t.zz())
00187                 << nl;
00188         }
00189     }
00190 }
00191 
00192 // Write tensorField in DX format
00193 template<class Type>
00194 void Foam::dxSurfaceWriter<Type>::writeData
00195 (
00196     Ostream& os,
00197     const Field<Type>& values
00198 )
00199 {
00200     // Write data
00201     os  << "object 3 class array type float rank 0 items "
00202         << values.size() << " data follows" << nl;
00203 
00204     forAll(values, elemI)
00205     {
00206         os << float(0.0) << nl;
00207     }
00208 }
00209 
00210 
00211 // Write trailer in DX format
00212 template<class Type>
00213 void Foam::dxSurfaceWriter<Type>::writeTrailer(Ostream& os)
00214 {
00215     os  << "# the field, with three components: \"positions\","
00216         << " \"connections\", and \"data\"" << nl
00217         << "object \"irregular positions irregular "
00218         << "connections\" class field"
00219         << nl
00220         << "component \"positions\" value 1" << nl
00221         << "component \"connections\" value 2" << nl
00222         << "component \"data\" value 3" << nl;
00223 }
00224 
00225 
00226 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00227 
00228 template<class Type>
00229 Foam::dxSurfaceWriter<Type>::dxSurfaceWriter()
00230 :
00231     surfaceWriter<Type>()
00232 {}
00233 
00234 
00235 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00236 
00237 template<class Type>
00238 Foam::dxSurfaceWriter<Type>::~dxSurfaceWriter()
00239 {}
00240 
00241 
00242 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00243 
00244 template<class Type>
00245 void Foam::dxSurfaceWriter<Type>::write
00246 (
00247     const fileName& outputDir,
00248     const fileName& surfaceName,
00249     const pointField& points,
00250     const faceList& faces,
00251     const fileName& fieldName,
00252     const Field<Type>& values,
00253     const bool verbose
00254 ) const
00255 {
00256     if (!isDir(outputDir))
00257     {
00258         mkDir(outputDir);
00259     }
00260 
00261     OFstream os
00262     (
00263         outputDir/fieldName + '_' + surfaceName + ".dx"
00264     );
00265 
00266     if (verbose)
00267     {
00268         Info<< "Writing field " << fieldName << " to " << os.name() << endl;
00269     }
00270 
00271     writeGeometry(os, points, faces);
00272 
00273     writeData(os, values);
00274 
00275     if (values.size() == points.size())
00276     {
00277         os  << nl << "attribute \"dep\" string \"positions\""
00278             << nl << nl;
00279     }
00280     else
00281     {
00282         os  << nl << "attribute \"dep\" string \"connections\""
00283             << nl << nl;
00284     }
00285 
00286     writeTrailer(os);
00287 
00288     os << "end" << nl;
00289 }
00290 
00291 
00292 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines