ENH: autoLayerDriver: better layering information message
[OpenFOAM-2.0.x.git] / applications / utilities / miscellaneous / foamFormatConvert / foamFormatConvert.C
blob6f3f040a0e7fac0f6c9bee04cfa3141c940ec4ce
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by
13     the Free Software Foundation, either version 3 of the License, or
14     (at your option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
24 Application
25     foamFormatConvert
27 Description
28     Converts all IOobjects associated with a case into the format specified
29     in the controlDict.
31     Mainly used to convert binary mesh/field files to ASCII.
33     Problem: any zero-size List written binary gets written as '0'. When
34     reading the file as a dictionary this is interpreted as a label. This
35     is (usually) not a problem when doing patch fields since these get the
36     'uniform', 'nonuniform' prefix. However zone contents are labelLists
37     not labelFields and these go wrong. For now hacked a solution where
38     we detect the keywords in zones and redo the dictionary entries
39     to be labelLists.
41 \*---------------------------------------------------------------------------*/
43 #include "argList.H"
44 #include "timeSelector.H"
45 #include "Time.H"
46 #include "volFields.H"
47 #include "surfaceFields.H"
48 #include "pointFields.H"
49 #include "cellIOList.H"
50 #include "IOobjectList.H"
51 #include "IOPtrList.H"
53 #include "writeMeshObject.H"
54 #include "fieldDictionary.H"
56 using namespace Foam;
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60 namespace Foam
62     defineTemplateTypeNameAndDebug(IOPtrList<entry>, 0);
66 // Hack to do zones which have Lists in them. See above.
67 bool writeZones(const word& name, const fileName& meshDir, Time& runTime)
69     IOobject io
70     (
71         name,
72         runTime.timeName(),
73         meshDir,
74         runTime,
75         IOobject::MUST_READ,
76         IOobject::NO_WRITE,
77         false
78     );
80     bool writeOk = false;
82     if (io.headerOk())
83     {
84         Info<< "        Reading " << io.headerClassName()
85             << " : " << name << endl;
87         // Switch off type checking (for reading e.g. faceZones as
88         // generic list of dictionaries).
89         const word oldTypeName = IOPtrList<entry>::typeName;
90         const_cast<word&>(IOPtrList<entry>::typeName) = word::null;
92         IOPtrList<entry> meshObject(io);
94         forAll(meshObject, i)
95         {
96             if (meshObject[i].isDict())
97             {
98                 dictionary& d = meshObject[i].dict();
100                 if (d.found("faceLabels"))
101                 {
102                     d.set("faceLabels", labelList(d.lookup("faceLabels")));
103                 }
105                 if (d.found("flipMap"))
106                 {
107                     d.set("flipMap", boolList(d.lookup("flipMap")));
108                 }
110                 if (d.found("cellLabels"))
111                 {
112                     d.set("cellLabels", labelList(d.lookup("cellLabels")));
113                 }
115                 if (d.found("pointLabels"))
116                 {
117                     d.set("pointLabels", labelList(d.lookup("pointLabels")));
118                 }
119             }
120         }
122         const_cast<word&>(IOPtrList<entry>::typeName) = oldTypeName;
123         // Fake type back to what was in field
124         const_cast<word&>(meshObject.type()) = io.headerClassName();
126         Info<< "        Writing " << name << endl;
128         // Force writing as ascii
129         writeOk = meshObject.regIOobject::writeObject
130         (
131             IOstream::ASCII,
132             IOstream::currentVersion,
133             runTime.writeCompression()
134         );
135     }
137     return writeOk;
142 // Main program:
144 int main(int argc, char *argv[])
146     timeSelector::addOptions();
147     argList::addBoolOption
148     (
149         "noConstant",
150         "exclude the 'constant/' dir in the times list"
151     );
153 #   include "addRegionOption.H"
154 #   include "setRootCase.H"
156     // enable noConstant by switching
157     if (!args.optionFound("noConstant"))
158     {
159         args.setOption("constant", "");
160     }
161     else
162     {
163         args.unsetOption("constant");
164         Info<< "Excluding the constant directory." << nl << endl;
165     }
168 #   include "createTime.H"
171     // Make sure we do not use the master-only reading since we read
172     // fields (different per processor) as dictionaries.
173     regIOobject::fileModificationChecking = regIOobject::timeStamp;
176     fileName meshDir = polyMesh::meshSubDir;
177     fileName regionPrefix = "";
178     word regionName = polyMesh::defaultRegion;
179     if (args.optionReadIfPresent("region", regionName))
180     {
181         Info<< "Using region " << regionName << nl << endl;
182         regionPrefix = regionName;
183         meshDir = regionName/polyMesh::meshSubDir;
184     }
186     Foam::instantList timeDirs = Foam::timeSelector::select0(runTime, args);
188     forAll(timeDirs, timeI)
189     {
190         runTime.setTime(timeDirs[timeI], timeI);
191         Info<< "Time = " << runTime.timeName() << endl;
193         // Convert all the standard mesh files
194         writeMeshObject<cellCompactIOList>("cells", meshDir, runTime);
195         writeMeshObject<labelIOList>("owner", meshDir, runTime);
196         writeMeshObject<labelIOList>("neighbour", meshDir, runTime);
197         writeMeshObject<faceCompactIOList>("faces", meshDir, runTime);
198         writeMeshObject<pointIOField>("points", meshDir, runTime);
199         writeMeshObject<labelIOList>("pointProcAddressing", meshDir, runTime);
200         writeMeshObject<labelIOList>("faceProcAddressing", meshDir, runTime);
201         writeMeshObject<labelIOList>("cellProcAddressing", meshDir, runTime);
202         writeMeshObject<labelIOList>
203         (
204             "boundaryProcAddressing",
205             meshDir,
206             runTime
207         );
209         if (runTime.writeFormat() == IOstream::ASCII)
210         {
211             // Only do zones when converting from binary to ascii
212             // The other way gives problems since working on dictionary level.
213             writeZones("cellZones", meshDir, runTime);
214             writeZones("faceZones", meshDir, runTime);
215             writeZones("pointZones", meshDir, runTime);
216         }
218         // Get list of objects from the database
219         IOobjectList objects(runTime, runTime.timeName(), regionPrefix);
221         forAllConstIter(IOobjectList, objects, iter)
222         {
223             const word& headerClassName = iter()->headerClassName();
225             if
226             (
227                 headerClassName == volScalarField::typeName
228              || headerClassName == volVectorField::typeName
229              || headerClassName == volSphericalTensorField::typeName
230              || headerClassName == volSymmTensorField::typeName
231              || headerClassName == volTensorField::typeName
233              || headerClassName == surfaceScalarField::typeName
234              || headerClassName == surfaceVectorField::typeName
235              || headerClassName == surfaceSphericalTensorField::typeName
236              || headerClassName == surfaceSymmTensorField::typeName
237              || headerClassName == surfaceTensorField::typeName
239              || headerClassName == pointScalarField::typeName
240              || headerClassName == pointVectorField::typeName
241              || headerClassName == pointSphericalTensorField::typeName
242              || headerClassName == pointSymmTensorField::typeName
243              || headerClassName == pointTensorField::typeName
244             )
245             {
246                 Info<< "        Reading " << headerClassName
247                     << " : " << iter()->name() << endl;
249                 fieldDictionary fDict
250                 (
251                     *iter(),
252                     headerClassName
253                 );
255                 Info<< "        Writing " << iter()->name() << endl;
256                 fDict.regIOobject::write();
257             }
258         }
260         Info<< endl;
261     }
263     Info<< "End\n" << endl;
265     return 0;
269 // ************************************************************************* //