initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / applications / utilities / postProcessing / dataConversion / foamToEnsight / foamToEnsight.C
blob7dac6294e6d6614337583d1f24021738424f151f
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
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 Usage
29     - foamToEnsight [OPTION] \n
30     Translates OpenFOAM data to Ensight format
32     @param -ascii \n
33     Write Ensight data in ASCII format instead of "C Binary"
35 Note
36     Parallel support for cloud data is not supported
38 \*---------------------------------------------------------------------------*/
40 #include "argList.H"
41 #include "IOobjectList.H"
42 #include "IOmanip.H"
43 #include "OFstream.H"
45 #include "volFields.H"
47 #include "labelIOField.H"
48 #include "scalarIOField.H"
49 #include "tensorIOField.H"
51 #include "ensightMesh.H"
52 #include "ensightField.H"
54 #include "ensightParticlePositions.H"
55 #include "ensightCloudField.H"
57 #include "fvc.H"
59 using namespace Foam;
61 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63 bool inFileNameList
65     const fileNameList& nameList,
66     const word& name
69     forAll(nameList, i)
70     {
71         if (nameList[i] == name)
72         {
73             return true;
74         }
75     }
77     return false;
81 // Main program:
83 int main(int argc, char *argv[])
85     argList::validOptions.insert("patches", "patch list");
86     argList::validOptions.insert("ascii", "" );
87 #   include "addTimeOptions.H"
89 #   include "setRootCase.H"
91     // Check options
92     bool binary = true;
93     if (args.options().found("ascii"))
94     {
95         binary = false;
96     }
98 #   include "createTime.H"
100     // get the available time-steps
101     instantList Times = runTime.times();
103 #   include "checkTimeOptions.H"
105     runTime.setTime(Times[startTime], startTime);
107 #   include "createNamedMesh.H"
109     // Mesh instance (region0 gets filtered out)
110     fileName regionPrefix = "";
112     if (regionName != polyMesh::defaultRegion)
113     {
114         regionPrefix = regionName;
115     }
117     const label nTypes = 2;
118     const word fieldTypes[] =
119     {
120         volScalarField::typeName,
121         volVectorField::typeName
122     };
124     // Create the output folder
125     const word postProcDir = "EnSight";
127     // Path to EnSight folder at case level only
128     // - For parallel cases, data only written from master
129 //    fileName postProcPath = runTime.path()/postProcDir;
130     fileName postProcPath = args.rootPath()/args.globalCaseName()/postProcDir;
132     if (Pstream::master())
133     {
134         if (dir(postProcPath))
135         {
136             rmDir(postProcPath);
137         }
139         mkDir(postProcPath);
140     }
142     // Start of case file header output
143     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145     const word prepend = args.globalCaseName() + '.';
147     OFstream *ensightCaseFilePtr = NULL;
148     if (Pstream::master())
149     {
150         fileName ensightCaseFileName = prepend + "case";
152         if (!binary)
153         {
154             ensightCaseFilePtr = new OFstream
155             (
156                 postProcPath/ensightCaseFileName,
157                 runTime.writeFormat(),
158                 runTime.writeVersion(),
159                 runTime.writeCompression()
160             );
161         }
162         else
163         {
164             ensightCaseFilePtr = new OFstream
165             (
166                 postProcPath/ensightCaseFileName,
167                 runTime.writeFormat(),
168                 runTime.writeVersion(),
169                 IOstream::UNCOMPRESSED
170             );
171         }
173         Info<< nl << "Case file is " << ensightCaseFileName << endl;
174     }
176     OFstream& ensightCaseFile = *ensightCaseFilePtr;
178 #   include "ensightCaseHeader.H"
180     // Construct the EnSight mesh
181     ensightMesh eMesh(mesh, args, binary);
183     // Set Time to the last time before looking for the lagrangian objects
184     runTime.setTime(Times[Times.size()-1], Times.size()-1);
186     IOobjectList objects(mesh, runTime.timeName());
188 #   include "checkMeshMoving.H"
190     wordHashSet allCloudNames;
191     word geomCaseFileName = prepend + "000";
192     if (Pstream::master())
193     {
194         // test pre check variable if there is a moving mesh
195         if (meshMoving == true)
196         {
197             geomCaseFileName = prepend + "***";
198         }
200         ensightCaseFile
201             << "GEOMETRY" << nl
202             << "model:        1     "
203             << (geomCaseFileName + ".mesh").c_str() << nl;
204     }
206     // Identify if lagrangian data exists at each time, and add clouds
207     // to the 'allCloudNames' hash set
208     for (label n=startTime; n<endTime; n++)
209     {
210         runTime.setTime(Times[n], n);
212         fileNameList cloudDirs = readDir
213         (
214             runTime.timePath()/regionPrefix/"lagrangian",
215             fileName::DIRECTORY
216         );
218         forAll(cloudDirs, cloudI)
219         {
220             IOobjectList cloudObjs
221             (
222                 mesh,
223                 runTime.timeName(),
224                 "lagrangian"/cloudDirs[cloudI]
225             );
227             IOobject* positionsPtr = cloudObjs.lookup("positions");
229             if (positionsPtr)
230             {
231                 allCloudNames.insert(cloudDirs[cloudI]);
232             }
233         }
234     }
236     HashTable<HashTable<word> > allCloudFields;
237     forAllConstIter(wordHashSet, allCloudNames, cloudIter)
238     {
239         // Add the name of the cloud(s) to the case file header
240         if (Pstream::master())
241         {
242             ensightCaseFile
243             <<  (
244                     "measured:     1     "
245                   + prepend
246                   + "***."
247                   + cloudIter.key()
248                 ).c_str()
249             << nl;
250         }
252         // Create a new hash table for each cloud
253         allCloudFields.insert(cloudIter.key(), HashTable<word>());
255         // Identify the new cloud in the hash table
256         HashTable<HashTable<word> >::iterator newCloudIter =
257             allCloudFields.find(cloudIter.key());
259         // Loop over all times to build list of fields and field types
260         // for each cloud
261         for (label n=startTime; n<endTime; n++)
262         {
263             runTime.setTime(Times[n], n);
265             IOobjectList cloudObjs
266             (
267                 mesh,
268                 runTime.timeName(),
269                 "lagrangian"/cloudIter.key()
270             );
272             forAllConstIter(IOobjectList, cloudObjs, fieldIter)
273             {
274                 const IOobject obj = *fieldIter();
276                 if (obj.name() != "positions")
277                 {
278                     // Add field and field type
279                     newCloudIter().insert
280                     (
281                         obj.name(),
282                         obj.headerClassName()
283                     );
284                 }
285             }
286         }
287     }
289     label nTimeSteps = 0;
290     for (label n=startTime; n<endTime; n++)
291     {
292         nTimeSteps++;
293         runTime.setTime(Times[n], n);
294         label timeIndex = n - startTime;
296         word timeName = itoa(timeIndex);
297         word timeFile = prepend + timeName;
299         Info<< "Translating time = " << runTime.timeName() << nl;
301 #       include "moveMesh.H"
303         if (timeIndex == 0 || mesh.moving())
304         {
305             eMesh.write
306             (
307                 postProcPath,
308                 prepend,
309                 timeIndex,
310                 ensightCaseFile
311             );
312         }
315         // Start of field data output
316         // ~~~~~~~~~~~~~~~~~~~~~~~~~~
318         if (timeIndex == 0 && Pstream::master())
319         {
320             ensightCaseFile<< nl << "VARIABLE" << nl;
321         }
324         // Cell field data output
325         // ~~~~~~~~~~~~~~~~~~~~~~
327         for (label i=0; i<nTypes; i++)
328         {
329             wordList fieldNames = objects.names(fieldTypes[i]);
331             for (label j=0; j<fieldNames.size(); j++)
332             {
333                 word fieldName = fieldNames[j];
335                 bool variableGood = true;
337 #               include "checkData.H"
339                 if (variableGood)
340                 {
341                     IOobject fieldObject
342                     (
343                         fieldName,
344                         mesh.time().timeName(),
345                         mesh,
346                         IOobject::MUST_READ,
347                         IOobject::NO_WRITE
348                     );
350                     if (fieldTypes[i] == volScalarField::typeName)
351                     {
352                         ensightField<scalar>
353                         (
354                             fieldObject,
355                             eMesh,
356                             postProcPath,
357                             prepend,
358                             timeIndex,
359                             binary,
360                             ensightCaseFile
361                         );
362                     }
363                     else if (fieldTypes[i] == volVectorField::typeName)
364                     {
365                         ensightField<vector>
366                         (
367                             fieldObject,
368                             eMesh,
369                             postProcPath,
370                             prepend,
371                             timeIndex,
372                             binary,
373                             ensightCaseFile
374                         );
375                     }
376                     else if (fieldTypes[i] == volSphericalTensorField::typeName)
377                     {
378                         ensightField<sphericalTensor>
379                         (
380                             fieldObject,
381                             eMesh,
382                             postProcPath,
383                             prepend,
384                             timeIndex,
385                             binary,
386                             ensightCaseFile
387                         );
388                     }
389                     else if (fieldTypes[i] == volSymmTensorField::typeName)
390                     {
391                         ensightField<symmTensor>
392                         (
393                             fieldObject,
394                             eMesh,
395                             postProcPath,
396                             prepend,
397                             timeIndex,
398                             binary,
399                             ensightCaseFile
400                         );
401                     }
402                     else if (fieldTypes[i] == volTensorField::typeName)
403                     {
404                         ensightField<tensor>
405                         (
406                             fieldObject,
407                             eMesh,
408                             postProcPath,
409                             prepend,
410                             timeIndex,
411                             binary,
412                             ensightCaseFile
413                         );
414                     }
415                 }
416             }
417         }
420         // Cloud field data output
421         // ~~~~~~~~~~~~~~~~~~~~~~~
423         forAllConstIter(HashTable<HashTable<word> >, allCloudFields, cloudIter)
424         {
425             const word& cloudName = cloudIter.key();
427             fileNameList currentCloudDirs = readDir
428             (
429                 runTime.timePath()/regionPrefix/"lagrangian",
430                 fileName::DIRECTORY
431             );
433             bool cloudExists = inFileNameList(currentCloudDirs, cloudName);
434             ensightParticlePositions
435             (
436                 mesh,
437                 postProcPath,
438                 timeFile,
439                 cloudName,
440                 cloudExists
441             );
443             forAllConstIter(HashTable<word>, cloudIter(), fieldIter)
444             {
445                 const word& fieldName = fieldIter.key();
446                 const word& fieldType = fieldIter();
448                 IOobject fieldObject
449                 (
450                     fieldName,
451                     mesh.time().timeName(),
452                     "lagrangian"/cloudName,
453                     mesh,
454                     IOobject::MUST_READ
455                 );
457                 bool fieldExists = fieldObject.headerOk();
458                 if (fieldType == scalarIOField::typeName)
459                 {
460                     ensightCloudField<scalar>
461                     (
462                         fieldObject,
463                         postProcPath,
464                         prepend,
465                         timeIndex,
466                         cloudName,
467                         ensightCaseFile,
468                         fieldExists
469                     );
470                 }
471                 else if (fieldType == vectorIOField::typeName)
472                 {
473                     ensightCloudField<vector>
474                     (
475                         fieldObject,
476                         postProcPath,
477                         prepend,
478                         timeIndex,
479                         cloudName,
480                         ensightCaseFile,
481                         fieldExists
482                     );
483                 }
484                 else
485                 {
486                     Info<< "Unable to convert field type " << fieldType
487                         << " for field " << fieldName << endl;
488                 }
489             }
490         }
491     }
493 #   include "ensightCaseTail.H"
495     if (Pstream::master())
496     {
497         delete ensightCaseFilePtr;
498     }
500     return 0;
504 // ************************************************************************* //