FreeFOAM The Cross-Platform CFD Toolkit
foamFormatConvert.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 Application
25  foamFormatConvert
26 
27 Description
28  Converts all IOobjects associated with a case into the format specified
29  in the controlDict.
30 
31  Mainly used to convert binary mesh/field files to ASCII.
32 
33 Bugs
34  Any zero-size List written binary gets written as '0'.
35  When reading the file as a dictionary this is interpreted
36  as a label. This is (usually) not a problem when doing
37  patch fields since these get the 'uniform', 'nonuniform'
38  prefix. However zone contents are labelLists not
39  labelFields and these go wrong. For now hacked a solution
40  where we detect the keywords in zones and redo the
41  dictionary entries to be labelLists.
42 
43 Usage
44 
45  - foamFormatConvert [OPTIONS]
46 
47  @param -noZero \n
48  Ignore timestep 0.
49 
50  @param -constant \n
51  Include the constant directory.
52 
53  @param -time <time>\n
54  Apply only to specific time.
55 
56  @param -latestTime \n
57  Only apply to latest time step.
58 
59  @param -case <dir>\n
60  Case directory.
61 
62  @param -parallel \n
63  Run in parallel.
64 
65  @param -help \n
66  Display help message.
67 
68  @param -doc \n
69  Display Doxygen API documentation page for this application.
70 
71  @param -srcDoc \n
72  Display Doxygen source documentation page for this application.
73 
74 \*---------------------------------------------------------------------------*/
75 
76 #include <OpenFOAM/argList.H>
77 #include <OpenFOAM/timeSelector.H>
78 #include <OpenFOAM/Time.H>
79 #include <finiteVolume/volFields.H>
81 #include <OpenFOAM/pointFields.H>
82 #include <OpenFOAM/cellIOList.H>
83 #include <OpenFOAM/IOobjectList.H>
84 #include <OpenFOAM/IOPtrList.H>
85 
86 #include "writeMeshObject.H"
87 #include "fieldDictionary.H"
88 
89 using namespace Foam;
90 
91 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
92 
93 namespace Foam
94 {
96 }
97 
98 
99 // Hack to do zones which have Lists in them. See above.
100 bool writeZones(const word& name, const fileName& meshDir, Time& runTime)
101 {
102  IOobject io
103  (
104  name,
105  runTime.timeName(),
106  meshDir,
107  runTime,
110  false
111  );
112 
113  bool writeOk = false;
114 
115  if (io.headerOk())
116  {
117  Info<< " Reading " << io.headerClassName()
118  << " : " << name << endl;
119 
120  // Switch off type checking (for reading e.g. faceZones as
121  // generic list of dictionaries).
122  const word oldTypeName = IOPtrList<entry>::typeName;
124 
125  IOPtrList<entry> meshObject(io);
126 
127  forAll(meshObject, i)
128  {
129  if (meshObject[i].isDict())
130  {
131  dictionary& d = meshObject[i].dict();
132 
133  if (d.found("faceLabels"))
134  {
135  d.set("faceLabels", labelList(d.lookup("faceLabels")));
136  }
137 
138  if (d.found("flipMap"))
139  {
140  d.set("flipMap", boolList(d.lookup("flipMap")));
141  }
142 
143  if (d.found("cellLabels"))
144  {
145  d.set("cellLabels", labelList(d.lookup("cellLabels")));
146  }
147 
148  if (d.found("pointLabels"))
149  {
150  d.set("pointLabels", labelList(d.lookup("pointLabels")));
151  }
152  }
153  }
154 
155  const_cast<word&>(IOPtrList<entry>::typeName) = oldTypeName;
156  // Fake type back to what was in field
157  const_cast<word&>(meshObject.type()) = io.headerClassName();
158 
159  Info<< " Writing " << name << endl;
160 
161  // Force writing as ascii
162  writeOk = meshObject.regIOobject::writeObject
163  (
166  runTime.writeCompression()
167  );
168  }
169 
170  return writeOk;
171 }
172 
173 
174 
175 // Main program:
176 
177 int main(int argc, char *argv[])
178 {
180  #include <OpenFOAM/addRegionOption.H>
181  #include <OpenFOAM/setRootCase.H>
182  #include <OpenFOAM/createTime.H>
183 
184  fileName meshDir = polyMesh::meshSubDir;
185  fileName regionPrefix = "";
187  if (args.optionReadIfPresent("region", regionName))
188  {
189  Info<< "Using region " << regionName << nl << endl;
190  regionPrefix = regionName;
191  meshDir = regionName/polyMesh::meshSubDir;
192  }
193 
195 
196  forAll(timeDirs, timeI)
197  {
198  runTime.setTime(timeDirs[timeI], timeI);
199  Info<< "Time = " << runTime.timeName() << endl;
200 
201  // Convert all the standard mesh files
202  writeMeshObject<cellIOList>("cells", meshDir, runTime);
203  writeMeshObject<labelIOList>("owner", meshDir, runTime);
204  writeMeshObject<labelIOList>("neighbour", meshDir, runTime);
205  writeMeshObject<faceIOList>("faces", meshDir, runTime);
206  writeMeshObject<pointIOField>("points", meshDir, runTime);
207  writeMeshObject<labelIOList>("pointProcAddressing", meshDir, runTime);
208  writeMeshObject<labelIOList>("faceProcAddressing", meshDir, runTime);
209  writeMeshObject<labelIOList>("cellProcAddressing", meshDir, runTime);
210  writeMeshObject<labelIOList>
211  (
212  "boundaryProcAddressing",
213  meshDir,
214  runTime
215  );
216 
217  if (runTime.writeFormat() == IOstream::ASCII)
218  {
219  // Only do zones when converting from binary to ascii
220  // The other way gives problems since working on dictionary level.
221  writeZones("cellZones", meshDir, runTime);
222  writeZones("faceZones", meshDir, runTime);
223  writeZones("pointZones", meshDir, runTime);
224  }
225 
226  // Get list of objects from the database
227  IOobjectList objects(runTime, runTime.timeName(), regionPrefix);
228 
229  forAllConstIter(IOobjectList, objects, iter)
230  {
231  const word& headerClassName = iter()->headerClassName();
232 
233  if
234  (
235  headerClassName == volScalarField::typeName
236  || headerClassName == volVectorField::typeName
237  || headerClassName == volSphericalTensorField::typeName
238  || headerClassName == volSymmTensorField::typeName
239  || headerClassName == volTensorField::typeName
240 
241  || headerClassName == surfaceScalarField::typeName
242  || headerClassName == surfaceVectorField::typeName
243  || headerClassName == surfaceSphericalTensorField::typeName
244  || headerClassName == surfaceSymmTensorField::typeName
245  || headerClassName == surfaceTensorField::typeName
246 
247  || headerClassName == pointScalarField::typeName
248  || headerClassName == pointVectorField::typeName
249  || headerClassName == pointSphericalTensorField::typeName
250  || headerClassName == pointSymmTensorField::typeName
251  || headerClassName == pointTensorField::typeName
252  )
253  {
254  Info<< " Reading " << headerClassName
255  << " : " << iter()->name() << endl;
256 
257  fieldDictionary fDict
258  (
259  *iter(),
260  headerClassName
261  );
262 
263  Info<< " Writing " << iter()->name() << endl;
264  fDict.regIOobject::write();
265  }
266  }
267 
268  Info<< endl;
269  }
270 
271  Info<< "End\n" << endl;
272 
273  return 0;
274 }
275 
276 
277 // ************************ vim: set sw=4 sts=4 et: ************************ //