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

OFSsurfaceFormat.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 "OFSsurfaceFormat.H"
00027 #include <OpenFOAM/IFstream.H>
00028 #include <OpenFOAM/IStringStream.H>
00029 #include <OpenFOAM/ListOps.H>
00030 
00031 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00032 
00033 template<class Face>
00034 Foam::fileFormats::OFSsurfaceFormat<Face>::OFSsurfaceFormat
00035 (
00036     const fileName& filename
00037 )
00038 {
00039     read(filename);
00040 }
00041 
00042 
00043 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00044 
00045 template<class Face>
00046 bool Foam::fileFormats::OFSsurfaceFormat<Face>::read
00047 (
00048     const fileName& filename
00049 )
00050 {
00051     this->clear();
00052 
00053     IFstream is(filename);
00054     if (!is.good())
00055     {
00056         FatalErrorIn
00057         (
00058             "fileFormats::OFSsurfaceFormat::read(const fileName&)"
00059         )
00060             << "Cannot read file " << filename
00061             << exit(FatalError);
00062     }
00063 
00064     // read surfZones:
00065     is >> this->storedZones();
00066 
00067     // read points:
00068     is >> this->storedPoints();
00069 
00070     // must triangulate?
00071     if (MeshedSurface<Face>::isTri())
00072     {
00073         // read faces as 'face' and transcribe to 'triFace'
00074         List<face> faceLst(is);
00075 
00076         MeshedSurface<face> surf
00077         (
00078             xferMove(this->storedPoints()),
00079             xferMove(faceLst),
00080             xferMove(this->storedZones())
00081         );
00082 
00083         this->transcribe(surf);
00084     }
00085     else
00086     {
00087         // read faces directly
00088         is >> this->storedFaces();
00089     }
00090 
00091     return true;
00092 }
00093 
00094 
00095 template<class Face>
00096 bool Foam::fileFormats::OFSsurfaceFormat<Face>::read
00097 (
00098     Istream& is,
00099     pointField& pointLst,
00100     List<Face>& faceLst,
00101     List<surfZone>& zoneLst
00102 )
00103 {
00104     if (!is.good())
00105     {
00106         FatalErrorIn
00107         (
00108             "fileFormats::OFSsurfaceFormat::read"
00109             "(Istream&, pointField&, List<Face>&, List<surfZone>&)"
00110         )
00111             << "read error "
00112             << exit(FatalError);
00113     }
00114 
00115     // read surfZones:
00116     is >> zoneLst;
00117 
00118     // read points:
00119     is >> pointLst;
00120 
00121     // must triangulate?
00122     if (MeshedSurface<Face>::isTri())
00123     {
00124         // read faces as 'face' and transcribe to 'triFace'
00125         List<face> origFaces(is);
00126 
00127         MeshedSurface<face> origSurf
00128         (
00129             xferMove(pointLst),
00130             xferMove(origFaces),
00131             xferMove(zoneLst)
00132         );
00133 
00134         MeshedSurface<Face> surf;
00135         surf.transcribe(origSurf);
00136     }
00137     else
00138     {
00139         // read faces directly
00140         is >> faceLst;
00141     }
00142 
00143     return true;
00144 }
00145 
00146 
00147 template<class Face>
00148 bool Foam::fileFormats::OFSsurfaceFormat<Face>::read
00149 (
00150     Istream& is,
00151     MeshedSurface<Face>& surf
00152 )
00153 {
00154     surf.clear();
00155 
00156     if (!is.good())
00157     {
00158         FatalErrorIn
00159         (
00160             "fileFormats::OFSsurfaceFormat::read"
00161             "(Istream&, MeshedSurface<Face>&)"
00162         )
00163             << "read error "
00164             << exit(FatalError);
00165     }
00166 
00167     pointField pointLst;
00168     List<Face> faceLst;
00169     List<surfZone> zoneLst;
00170 
00171     read(is, pointLst, faceLst, zoneLst);
00172 
00173     surf.reset
00174     (
00175         xferMove(pointLst),
00176         xferMove(faceLst),
00177         xferMove(zoneLst)
00178     );
00179 
00180     return true;
00181 }
00182 
00183 
00184 template<class Face>
00185 bool Foam::fileFormats::OFSsurfaceFormat<Face>::read
00186 (
00187     Istream& is,
00188     UnsortedMeshedSurface<Face>& surf
00189 )
00190 {
00191     surf.clear();
00192     MeshedSurface<Face> origSurf(is);
00193     surf.transfer(origSurf);
00194 
00195     return true;
00196 }
00197 
00198 
00199 
00200 template<class Face>
00201 void Foam::fileFormats::OFSsurfaceFormat<Face>::write
00202 (
00203     const fileName& filename,
00204     const MeshedSurfaceProxy<Face>& surf
00205 )
00206 {
00207     const List<Face>&  faceLst = surf.faces();
00208     const List<label>& faceMap = surf.faceMap();
00209 
00210     OFstream os(filename);
00211     if (!os.good())
00212     {
00213         FatalErrorIn
00214         (
00215             "fileFormats::OFSsurfaceFormat::write"
00216             "(const fileName&, const MeshedSurfaceProxy<Face>&)"
00217         )
00218             << "Cannot open file for writing " << filename
00219             << exit(FatalError);
00220     }
00221 
00222 
00223     OFSsurfaceFormatCore::writeHeader(os, surf.points(), surf.surfZones());
00224 
00225     const List<surfZone>& zones = surf.surfZones();
00226     const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
00227 
00228     if (useFaceMap)
00229     {
00230         os  << "\n// faces:"  << nl
00231             << faceLst.size() << token::BEGIN_LIST << nl;
00232 
00233         label faceI = 0;
00234         forAll(zones, zoneI)
00235         {
00236             // Print all faces belonging to this zone
00237             const surfZone& zone = zones[zoneI];
00238 
00239             forAll(zone, localFaceI)
00240             {
00241                 os << faceLst[faceMap[faceI++]] << nl;
00242             }
00243         }
00244         os << token::END_LIST << nl;
00245     }
00246     else
00247     {
00248         os  << "\n// faces:"  << nl << faceLst << nl;
00249     }
00250 
00251     IOobject::writeDivider(os);
00252 
00253     // Check state of Ostream
00254     os.check("OFSsurfaceFormat<Face>::write(Ostream&)");
00255 }
00256 
00257 
00258 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines