BUGFIX: Illegal use of uninitialised value (backport)
[foam-extend-3.2.git] / applications / utilities / postProcessing / dataConversion / foamToEnsight / foamToEnsight.C
blob97b9fc49e3407cd72eaf917bccaefe2b115e7a47
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     Translates FOAM data to EnSight format.
28     An Ensight part is created for the internalMesh and for each patch.
30 Usage
31     - foamToEnsight [OPTION] \n
32     Translates OpenFOAM data to Ensight format
34     @param -ascii \n
35     Write Ensight data in ASCII format instead of "C Binary"
37     @param -patches patchList \n
38     Specify particular patches to write.
39     Specifying an empty list suppresses writing the internalMesh.
41     @param -noPatches \n
42     Suppress writing any patches.
44 Note
45     Parallel support for cloud data is not supported
46     - writes to @a EnSight directory to avoid collisions with foamToEnsightParts
48 \*---------------------------------------------------------------------------*/
50 #include "argList.H"
51 #include "timeSelector.H"
52 #include "IOobjectList.H"
53 #include "IOmanip.H"
54 #include "OFstream.H"
56 #include "volFields.H"
58 #include "labelIOField.H"
59 #include "scalarIOField.H"
60 #include "tensorIOField.H"
62 #include "ensightMesh.H"
63 #include "ensightField.H"
65 #include "ensightParticlePositions.H"
66 #include "ensightCloudField.H"
68 #include "fvc.H"
70 using namespace Foam;
72 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
74 bool inFileNameList
76     const fileNameList& nameList,
77     const word& name
80     forAll(nameList, i)
81     {
82         if (nameList[i] == name)
83         {
84             return true;
85         }
86     }
88     return false;
92 // Main program:
94 int main(int argc, char *argv[])
96     argList::validOptions.insert("ascii", "" );
97     argList::validOptions.insert("patches", "patchList");
98     argList::validOptions.insert("noPatches", "");
100 #   include "addTimeOptions.H"
101 #   include "setRootCase.H"
103     // Check options
104     bool binary = !args.optionFound("ascii");
106 #   include "createTime.H"
108     // get the available time-steps
109     instantList Times = runTime.times();
111 #   include "checkTimeOptions.H"
113     runTime.setTime(Times[startTime], startTime);
115 #   include "createNamedMesh.H"
117     // Mesh instance (region0 gets filtered out)
118     fileName regionPrefix = "";
120     if (regionName != polyMesh::defaultRegion)
121     {
122         regionPrefix = regionName;
123     }
125     const label nVolFieldTypes = 5;
126     const word volFieldTypes[] =
127     {
128         volScalarField::typeName,
129         volVectorField::typeName,
130         volSphericalTensorField::typeName,
131         volSymmTensorField::typeName,
132         volTensorField::typeName
133     };
135     // Path to EnSight folder at case level only
136     // - For parallel cases, data only written from master
137     fileName ensightDir = args.rootPath()/args.globalCaseName()/"EnSight";
139     if (Pstream::master())
140     {
141         if (isDir(ensightDir))
142         {
143             rmDir(ensightDir);
144         }
146         mkDir(ensightDir);
147     }
149     // Start of case file header output
150     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
152     const word prepend = args.globalCaseName() + '.';
154     OFstream *ensightCaseFilePtr = NULL;
155     if (Pstream::master())
156     {
157         fileName caseFileName = prepend + "case";
158         Info<< nl << "write case: " << caseFileName.c_str() << endl;
160         // the case file is always ASCII
161         ensightCaseFilePtr = new OFstream
162         (
163             ensightDir/caseFileName,
164             ios_base::out|ios_base::trunc,
165             IOstream::ASCII
166         );
168         *ensightCaseFilePtr
169             << "FORMAT" << nl
170             << "type: ensight gold" << nl << nl;
171     }
173     OFstream& ensightCaseFile = *ensightCaseFilePtr;
175     // Construct the EnSight mesh
176     ensightMesh eMesh(mesh, args, binary);
178     // Set Time to the last time before looking for the lagrangian objects
179     runTime.setTime(Times[Times.size()-1], Times.size()-1);
181     IOobjectList objects(mesh, runTime.timeName());
183 #   include "checkMeshMoving.H"
185     wordHashSet allCloudNames;
186     if (Pstream::master())
187     {
188         word geomFileName = prepend + "000";
190         // test pre check variable if there is a moving mesh
191         if (meshMoving)
192         {
193             geomFileName = prepend + "***";
194         }
196         ensightCaseFile
197             << "GEOMETRY" << nl
198             << "model:        1     "
199             << (geomFileName + ".mesh").c_str() << nl;
200     }
202     // Identify if lagrangian data exists at each time, and add clouds
203     // to the 'allCloudNames' hash set
204     for (label n=startTime; n<endTime; n++)
205     {
206         runTime.setTime(Times[n], n);
208         fileNameList cloudDirs = readDir
209         (
210             runTime.timePath()/regionPrefix/cloud::prefix,
211             fileName::DIRECTORY
212         );
214         forAll(cloudDirs, cloudI)
215         {
216             IOobjectList cloudObjs
217             (
218                 mesh,
219                 runTime.timeName(),
220                 cloud::prefix/cloudDirs[cloudI]
221             );
223             IOobject* positionsPtr = cloudObjs.lookup("positions");
225             if (positionsPtr)
226             {
227                 allCloudNames.insert(cloudDirs[cloudI]);
228             }
229         }
230     }
232     HashTable<HashTable<word> > allCloudFields;
233     forAllConstIter(wordHashSet, allCloudNames, cloudIter)
234     {
235         // Add the name of the cloud(s) to the case file header
236         if (Pstream::master())
237         {
238             ensightCaseFile
239             <<  (
240                     "measured:     1     "
241                   + prepend
242                   + "***."
243                   + cloudIter.key()
244                 ).c_str()
245             << nl;
246         }
248         // Create a new hash table for each cloud
249         allCloudFields.insert(cloudIter.key(), HashTable<word>());
251         // Identify the new cloud in the hash table
252         HashTable<HashTable<word> >::iterator newCloudIter =
253             allCloudFields.find(cloudIter.key());
255         // Loop over all times to build list of fields and field types
256         // for each cloud
257         for (label n=startTime; n<endTime; n++)
258         {
259             runTime.setTime(Times[n], n);
261             IOobjectList cloudObjs
262             (
263                 mesh,
264                 runTime.timeName(),
265                 cloud::prefix/cloudIter.key()
266             );
268             forAllConstIter(IOobjectList, cloudObjs, fieldIter)
269             {
270                 const IOobject obj = *fieldIter();
272                 if (obj.name() != "positions")
273                 {
274                     // Add field and field type
275                     newCloudIter().insert
276                     (
277                         obj.name(),
278                         obj.headerClassName()
279                     );
280                 }
281             }
282         }
283     }
285     label nTimeSteps = 0;
286     for (label n=startTime; n<endTime; n++)
287     {
288         nTimeSteps++;
289         runTime.setTime(Times[n], n);
290         label timeIndex = n - startTime;
292         word timeName = itoa(timeIndex);
293         word timeFile = prepend + timeName;
295         Info<< "Translating time = " << runTime.timeName() << nl;
297 #       include "moveMesh.H"
299         if (timeIndex == 0 || mesh.moving())
300         {
301             eMesh.write
302             (
303                 ensightDir,
304                 prepend,
305                 timeIndex,
306                 ensightCaseFile
307             );
308         }
311         // Start of field data output
312         // ~~~~~~~~~~~~~~~~~~~~~~~~~~
314         if (timeIndex == 0 && Pstream::master())
315         {
316             ensightCaseFile<< nl << "VARIABLE" << nl;
317         }
320         // Cell field data output
321         // ~~~~~~~~~~~~~~~~~~~~~~
323         for (label i=0; i<nVolFieldTypes; i++)
324         {
325             wordList fieldNames = objects.names(volFieldTypes[i]);
327             for (label j=0; j<fieldNames.size(); j++)
328             {
329                 word fieldName = fieldNames[j];
331 #               include "checkData.H"
333                 if (!variableGood)
334                 {
335                     continue;
336                 }
338                 IOobject fieldObject
339                 (
340                     fieldName,
341                     mesh.time().timeName(),
342                     mesh,
343                     IOobject::MUST_READ,
344                     IOobject::NO_WRITE
345                 );
347                 if (volFieldTypes[i] == volScalarField::typeName)
348                 {
349                     ensightField<scalar>
350                     (
351                         fieldObject,
352                         eMesh,
353                         ensightDir,
354                         prepend,
355                         timeIndex,
356                         binary,
357                         ensightCaseFile
358                     );
359                 }
360                 else if (volFieldTypes[i] == volVectorField::typeName)
361                 {
362                     ensightField<vector>
363                     (
364                         fieldObject,
365                         eMesh,
366                         ensightDir,
367                         prepend,
368                         timeIndex,
369                         binary,
370                         ensightCaseFile
371                     );
372                 }
373                 else if (volFieldTypes[i] == volSphericalTensorField::typeName)
374                 {
375                     ensightField<sphericalTensor>
376                     (
377                         fieldObject,
378                         eMesh,
379                         ensightDir,
380                         prepend,
381                         timeIndex,
382                         binary,
383                         ensightCaseFile
384                     );
385                 }
386                 else if (volFieldTypes[i] == volSymmTensorField::typeName)
387                 {
388                     ensightField<symmTensor>
389                     (
390                         fieldObject,
391                         eMesh,
392                         ensightDir,
393                         prepend,
394                         timeIndex,
395                         binary,
396                         ensightCaseFile
397                     );
398                 }
399                 else if (volFieldTypes[i] == volTensorField::typeName)
400                 {
401                     ensightField<tensor>
402                     (
403                         fieldObject,
404                         eMesh,
405                         ensightDir,
406                         prepend,
407                         timeIndex,
408                         binary,
409                         ensightCaseFile
410                     );
411                 }
412             }
413         }
416         // Cloud field data output
417         // ~~~~~~~~~~~~~~~~~~~~~~~
419         forAllConstIter(HashTable<HashTable<word> >, allCloudFields, cloudIter)
420         {
421             const word& cloudName = cloudIter.key();
423             fileNameList currentCloudDirs = readDir
424             (
425                 runTime.timePath()/regionPrefix/cloud::prefix,
426                 fileName::DIRECTORY
427             );
429             bool cloudExists = inFileNameList(currentCloudDirs, cloudName);
430             ensightParticlePositions
431             (
432                 mesh,
433                 ensightDir,
434                 timeFile,
435                 cloudName,
436                 cloudExists
437             );
439             forAllConstIter(HashTable<word>, cloudIter(), fieldIter)
440             {
441                 const word& fieldName = fieldIter.key();
442                 const word& fieldType = fieldIter();
444                 IOobject fieldObject
445                 (
446                     fieldName,
447                     mesh.time().timeName(),
448                     cloud::prefix/cloudName,
449                     mesh,
450                     IOobject::MUST_READ
451                 );
453                 bool fieldExists = fieldObject.headerOk();
454                 if (fieldType == scalarIOField::typeName)
455                 {
456                     ensightCloudField<scalar>
457                     (
458                         fieldObject,
459                         ensightDir,
460                         prepend,
461                         timeIndex,
462                         cloudName,
463                         ensightCaseFile,
464                         fieldExists
465                     );
466                 }
467                 else if (fieldType == vectorIOField::typeName)
468                 {
469                     ensightCloudField<vector>
470                     (
471                         fieldObject,
472                         ensightDir,
473                         prepend,
474                         timeIndex,
475                         cloudName,
476                         ensightCaseFile,
477                         fieldExists
478                     );
479                 }
480                 else
481                 {
482                     Info<< "Unable to convert field type " << fieldType
483                         << " for field " << fieldName << endl;
484                 }
485             }
486         }
487     }
489 #   include "ensightCaseTail.H"
491     if (Pstream::master())
492     {
493         delete ensightCaseFilePtr;
494     }
496     Info<< "End\n" << endl;
498     return 0;
502 // ************************************************************************* //