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

foamFormatConvert.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 Application
00025     foamFormatConvert
00026 
00027 Description
00028     Converts all IOobjects associated with a case into the format specified
00029     in the controlDict.
00030 
00031     Mainly used to convert binary mesh/field files to ASCII.
00032 
00033 Bugs
00034     Any zero-size List written binary gets written as '0'.
00035     When reading the file as a dictionary this is interpreted
00036     as a label. This is (usually) not a problem when doing
00037     patch fields since these get the 'uniform', 'nonuniform'
00038     prefix. However zone contents are labelLists not
00039     labelFields and these go wrong. For now hacked a solution
00040     where we detect the keywords in zones and redo the
00041     dictionary entries to be labelLists.
00042     
00043 Usage
00044 
00045     - foamFormatConvert [OPTIONS]
00046 
00047     @param -noZero \n
00048     Ignore timestep 0.
00049 
00050     @param -constant \n
00051     Include the constant directory.
00052 
00053     @param -time <time>\n
00054     Apply only to specific time.
00055 
00056     @param -latestTime \n
00057     Only apply to latest time step.
00058 
00059     @param -case <dir>\n
00060     Case directory.
00061 
00062     @param -parallel \n
00063     Run in parallel.
00064 
00065     @param -help \n
00066     Display help message.
00067 
00068     @param -doc \n
00069     Display Doxygen API documentation page for this application.
00070 
00071     @param -srcDoc \n
00072     Display Doxygen source documentation page for this application.
00073 
00074 \*---------------------------------------------------------------------------*/
00075 
00076 #include <OpenFOAM/argList.H>
00077 #include <OpenFOAM/timeSelector.H>
00078 #include <OpenFOAM/Time.H>
00079 #include <finiteVolume/volFields.H>
00080 #include <finiteVolume/surfaceFields.H>
00081 #include <OpenFOAM/pointFields.H>
00082 #include <OpenFOAM/cellIOList.H>
00083 #include <OpenFOAM/IOobjectList.H>
00084 #include <OpenFOAM/IOPtrList.H>
00085 
00086 #include "writeMeshObject.H"
00087 #include "fieldDictionary.H"
00088 
00089 using namespace Foam;
00090 
00091 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00092 
00093 namespace Foam
00094 {
00095     defineTemplateTypeNameAndDebug(IOPtrList<entry>, 0);
00096 }
00097 
00098 
00099 // Hack to do zones which have Lists in them. See above.
00100 bool writeZones(const word& name, const fileName& meshDir, Time& runTime)
00101 {
00102     IOobject io
00103     (
00104         name,
00105         runTime.timeName(),
00106         meshDir,
00107         runTime,
00108         IOobject::MUST_READ,
00109         IOobject::NO_WRITE,
00110         false
00111     );
00112 
00113     bool writeOk = false;
00114 
00115     if (io.headerOk())
00116     {
00117         Info<< "        Reading " << io.headerClassName()
00118             << " : " << name << endl;
00119 
00120         // Switch off type checking (for reading e.g. faceZones as
00121         // generic list of dictionaries).
00122         const word oldTypeName = IOPtrList<entry>::typeName;
00123         const_cast<word&>(IOPtrList<entry>::typeName) = word::null;
00124 
00125         IOPtrList<entry> meshObject(io);
00126 
00127         forAll(meshObject, i)
00128         {
00129             if (meshObject[i].isDict())
00130             {
00131                 dictionary& d = meshObject[i].dict();
00132 
00133                 if (d.found("faceLabels"))
00134                 {
00135                     d.set("faceLabels", labelList(d.lookup("faceLabels")));
00136                 }
00137 
00138                 if (d.found("flipMap"))
00139                 {
00140                     d.set("flipMap", boolList(d.lookup("flipMap")));
00141                 }
00142 
00143                 if (d.found("cellLabels"))
00144                 {
00145                     d.set("cellLabels", labelList(d.lookup("cellLabels")));
00146                 }
00147 
00148                 if (d.found("pointLabels"))
00149                 {
00150                     d.set("pointLabels", labelList(d.lookup("pointLabels")));
00151                 }
00152             }
00153         }
00154 
00155         const_cast<word&>(IOPtrList<entry>::typeName) = oldTypeName;
00156         // Fake type back to what was in field
00157         const_cast<word&>(meshObject.type()) = io.headerClassName();
00158 
00159         Info<< "        Writing " << name << endl;
00160 
00161         // Force writing as ascii
00162         writeOk = meshObject.regIOobject::writeObject
00163         (
00164             IOstream::ASCII,
00165             IOstream::currentVersion,
00166             runTime.writeCompression()
00167         );
00168     }
00169 
00170     return writeOk;
00171 }
00172 
00173 
00174 
00175 // Main program:
00176 
00177 int main(int argc, char *argv[])
00178 {
00179     timeSelector::addOptions();
00180     #include <OpenFOAM/addRegionOption.H>
00181     #include <OpenFOAM/setRootCase.H>
00182     #include <OpenFOAM/createTime.H>
00183 
00184     fileName meshDir = polyMesh::meshSubDir;
00185     fileName regionPrefix = "";
00186     word regionName = polyMesh::defaultRegion;
00187     if (args.optionReadIfPresent("region", regionName))
00188     {
00189         Info<< "Using region " << regionName << nl << endl;
00190         regionPrefix = regionName;
00191         meshDir = regionName/polyMesh::meshSubDir;
00192     }
00193 
00194     Foam::instantList timeDirs = Foam::timeSelector::select0(runTime, args);
00195 
00196     forAll(timeDirs, timeI)
00197     {
00198         runTime.setTime(timeDirs[timeI], timeI);
00199         Info<< "Time = " << runTime.timeName() << endl;
00200 
00201         // Convert all the standard mesh files
00202         writeMeshObject<cellIOList>("cells", meshDir, runTime);
00203         writeMeshObject<labelIOList>("owner", meshDir, runTime);
00204         writeMeshObject<labelIOList>("neighbour", meshDir, runTime);
00205         writeMeshObject<faceIOList>("faces", meshDir, runTime);
00206         writeMeshObject<pointIOField>("points", meshDir, runTime);
00207         writeMeshObject<labelIOList>("pointProcAddressing", meshDir, runTime);
00208         writeMeshObject<labelIOList>("faceProcAddressing", meshDir, runTime);
00209         writeMeshObject<labelIOList>("cellProcAddressing", meshDir, runTime);
00210         writeMeshObject<labelIOList>
00211         (
00212             "boundaryProcAddressing",
00213             meshDir,
00214             runTime
00215         );
00216 
00217         if (runTime.writeFormat() == IOstream::ASCII)
00218         {
00219             // Only do zones when converting from binary to ascii
00220             // The other way gives problems since working on dictionary level.
00221             writeZones("cellZones", meshDir, runTime);
00222             writeZones("faceZones", meshDir, runTime);
00223             writeZones("pointZones", meshDir, runTime);
00224         }
00225 
00226         // Get list of objects from the database
00227         IOobjectList objects(runTime, runTime.timeName(), regionPrefix);
00228 
00229         forAllConstIter(IOobjectList, objects, iter)
00230         {
00231             const word& headerClassName = iter()->headerClassName();
00232 
00233             if
00234             (
00235                 headerClassName == volScalarField::typeName
00236              || headerClassName == volVectorField::typeName
00237              || headerClassName == volSphericalTensorField::typeName
00238              || headerClassName == volSymmTensorField::typeName
00239              || headerClassName == volTensorField::typeName
00240 
00241              || headerClassName == surfaceScalarField::typeName
00242              || headerClassName == surfaceVectorField::typeName
00243              || headerClassName == surfaceSphericalTensorField::typeName
00244              || headerClassName == surfaceSymmTensorField::typeName
00245              || headerClassName == surfaceTensorField::typeName
00246 
00247              || headerClassName == pointScalarField::typeName
00248              || headerClassName == pointVectorField::typeName
00249              || headerClassName == pointSphericalTensorField::typeName
00250              || headerClassName == pointSymmTensorField::typeName
00251              || headerClassName == pointTensorField::typeName
00252             )
00253             {
00254                 Info<< "        Reading " << headerClassName
00255                     << " : " << iter()->name() << endl;
00256 
00257                 fieldDictionary fDict
00258                 (
00259                     *iter(),
00260                     headerClassName
00261                 );
00262 
00263                 Info<< "        Writing " << iter()->name() << endl;
00264                 fDict.regIOobject::write();
00265             }
00266         }
00267 
00268         Info<< endl;
00269     }
00270 
00271     Info<< "End\n" << endl;
00272 
00273     return 0;
00274 }
00275 
00276 
00277 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines