OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

foamToEnsight.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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Translates FOAM data to EnSight format.
00027 
00028     An Ensight part is created for the internalMesh and for each patch.
00029 
00030 Usage
00031     - foamToEnsight [OPTION] \n
00032     Translates OpenFOAM data to Ensight format
00033 
00034     @param -ascii \n
00035     Write Ensight data in ASCII format instead of "C Binary"
00036 
00037     @param -patches patchList \n
00038     Specify particular patches to write.
00039     Specifying an empty list suppresses writing the internalMesh.
00040 
00041     @param -noPatches \n
00042     Suppress writing any patches.
00043 
00044 Note
00045     Parallel support for cloud data is not supported
00046     - writes to @a EnSight directory to avoid collisions with foamToEnsightParts
00047 
00048 \*---------------------------------------------------------------------------*/
00049 
00050 #include "argList.H"
00051 #include "timeSelector.H"
00052 #include "IOobjectList.H"
00053 #include "IOmanip.H"
00054 #include "OFstream.H"
00055 
00056 #include "volFields.H"
00057 
00058 #include "labelIOField.H"
00059 #include "scalarIOField.H"
00060 #include "tensorIOField.H"
00061 
00062 #include "ensightMesh.H"
00063 #include "ensightField.H"
00064 
00065 #include "ensightParticlePositions.H"
00066 #include "ensightCloudField.H"
00067 
00068 #include "fvc.H"
00069 
00070 using namespace Foam;
00071 
00072 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00073 
00074 bool inFileNameList
00075 (
00076     const fileNameList& nameList,
00077     const word& name
00078 )
00079 {
00080     forAll(nameList, i)
00081     {
00082         if (nameList[i] == name)
00083         {
00084             return true;
00085         }
00086     }
00087 
00088     return false;
00089 }
00090 
00091 
00092 // Main program:
00093 
00094 int main(int argc, char *argv[])
00095 {
00096     argList::validOptions.insert("ascii", "" );
00097     argList::validOptions.insert("patches", "patchList");
00098     argList::validOptions.insert("noPatches", "");
00099 
00100 #   include "addTimeOptions.H"
00101 #   include "setRootCase.H"
00102 
00103     // Check options
00104     bool binary = !args.optionFound("ascii");
00105 
00106 #   include "createTime.H"
00107 
00108     // get the available time-steps
00109     instantList Times = runTime.times();
00110 
00111 #   include "checkTimeOptions.H"
00112 
00113     runTime.setTime(Times[startTime], startTime);
00114 
00115 #   include "createNamedMesh.H"
00116 
00117     // Mesh instance (region0 gets filtered out)
00118     fileName regionPrefix = "";
00119 
00120     if (regionName != polyMesh::defaultRegion)
00121     {
00122         regionPrefix = regionName;
00123     }
00124 
00125     const label nVolFieldTypes = 5;
00126     const word volFieldTypes[] =
00127     {
00128         volScalarField::typeName,
00129         volVectorField::typeName,
00130         volSphericalTensorField::typeName,
00131         volSymmTensorField::typeName,
00132         volTensorField::typeName
00133     };
00134 
00135     // Path to EnSight folder at case level only
00136     // - For parallel cases, data only written from master
00137     fileName ensightDir = args.rootPath()/args.globalCaseName()/"EnSight";
00138 
00139     if (Pstream::master())
00140     {
00141         if (isDir(ensightDir))
00142         {
00143             rmDir(ensightDir);
00144         }
00145 
00146         mkDir(ensightDir);
00147     }
00148 
00149     // Start of case file header output
00150     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00151 
00152     const word prepend = args.globalCaseName() + '.';
00153 
00154     OFstream *ensightCaseFilePtr = NULL;
00155     if (Pstream::master())
00156     {
00157         fileName caseFileName = prepend + "case";
00158         Info<< nl << "write case: " << caseFileName.c_str() << endl;
00159 
00160         // the case file is always ASCII
00161         ensightCaseFilePtr = new OFstream
00162         (
00163             ensightDir/caseFileName,
00164             IOstream::ASCII
00165         );
00166 
00167         *ensightCaseFilePtr
00168             << "FORMAT" << nl
00169             << "type: ensight gold" << nl << nl;
00170     }
00171 
00172     OFstream& ensightCaseFile = *ensightCaseFilePtr;
00173 
00174     // Construct the EnSight mesh
00175     ensightMesh eMesh(mesh, args, binary);
00176 
00177     // Set Time to the last time before looking for the lagrangian objects
00178     runTime.setTime(Times[Times.size()-1], Times.size()-1);
00179 
00180     IOobjectList objects(mesh, runTime.timeName());
00181 
00182 #   include "checkMeshMoving.H"
00183 
00184     wordHashSet allCloudNames;
00185     if (Pstream::master())
00186     {
00187         word geomFileName = prepend + "000";
00188 
00189         // test pre check variable if there is a moving mesh
00190         if (meshMoving)
00191         {
00192             geomFileName = prepend + "***";
00193         }
00194 
00195         ensightCaseFile
00196             << "GEOMETRY" << nl
00197             << "model:        1     "
00198             << (geomFileName + ".mesh").c_str() << nl;
00199     }
00200 
00201     // Identify if lagrangian data exists at each time, and add clouds
00202     // to the 'allCloudNames' hash set
00203     for (label n=startTime; n<endTime; n++)
00204     {
00205         runTime.setTime(Times[n], n);
00206 
00207         fileNameList cloudDirs = readDir
00208         (
00209             runTime.timePath()/regionPrefix/cloud::prefix,
00210             fileName::DIRECTORY
00211         );
00212 
00213         forAll(cloudDirs, cloudI)
00214         {
00215             IOobjectList cloudObjs
00216             (
00217                 mesh,
00218                 runTime.timeName(),
00219                 cloud::prefix/cloudDirs[cloudI]
00220             );
00221 
00222             IOobject* positionsPtr = cloudObjs.lookup("positions");
00223 
00224             if (positionsPtr)
00225             {
00226                 allCloudNames.insert(cloudDirs[cloudI]);
00227             }
00228         }
00229     }
00230 
00231     HashTable<HashTable<word> > allCloudFields;
00232     forAllConstIter(wordHashSet, allCloudNames, cloudIter)
00233     {
00234         // Add the name of the cloud(s) to the case file header
00235         if (Pstream::master())
00236         {
00237             ensightCaseFile
00238             <<  (
00239                     "measured:     1     "
00240                   + prepend
00241                   + "***."
00242                   + cloudIter.key()
00243                 ).c_str()
00244             << nl;
00245         }
00246 
00247         // Create a new hash table for each cloud
00248         allCloudFields.insert(cloudIter.key(), HashTable<word>());
00249 
00250         // Identify the new cloud in the hash table
00251         HashTable<HashTable<word> >::iterator newCloudIter =
00252             allCloudFields.find(cloudIter.key());
00253 
00254         // Loop over all times to build list of fields and field types
00255         // for each cloud
00256         for (label n=startTime; n<endTime; n++)
00257         {
00258             runTime.setTime(Times[n], n);
00259 
00260             IOobjectList cloudObjs
00261             (
00262                 mesh,
00263                 runTime.timeName(),
00264                 cloud::prefix/cloudIter.key()
00265             );
00266 
00267             forAllConstIter(IOobjectList, cloudObjs, fieldIter)
00268             {
00269                 const IOobject obj = *fieldIter();
00270 
00271                 if (obj.name() != "positions")
00272                 {
00273                     // Add field and field type
00274                     newCloudIter().insert
00275                     (
00276                         obj.name(),
00277                         obj.headerClassName()
00278                     );
00279                 }
00280             }
00281         }
00282     }
00283 
00284     label nTimeSteps = 0;
00285     for (label n=startTime; n<endTime; n++)
00286     {
00287         nTimeSteps++;
00288         runTime.setTime(Times[n], n);
00289         label timeIndex = n - startTime;
00290 
00291         word timeName = itoa(timeIndex);
00292         word timeFile = prepend + timeName;
00293 
00294         Info<< "Translating time = " << runTime.timeName() << nl;
00295 
00296 #       include "moveMesh.H"
00297 
00298         if (timeIndex == 0 || mesh.moving())
00299         {
00300             eMesh.write
00301             (
00302                 ensightDir,
00303                 prepend,
00304                 timeIndex,
00305                 ensightCaseFile
00306             );
00307         }
00308 
00309 
00310         // Start of field data output
00311         // ~~~~~~~~~~~~~~~~~~~~~~~~~~
00312 
00313         if (timeIndex == 0 && Pstream::master())
00314         {
00315             ensightCaseFile<< nl << "VARIABLE" << nl;
00316         }
00317 
00318 
00319         // Cell field data output
00320         // ~~~~~~~~~~~~~~~~~~~~~~
00321 
00322         for (label i=0; i<nVolFieldTypes; i++)
00323         {
00324             wordList fieldNames = objects.names(volFieldTypes[i]);
00325 
00326             for (label j=0; j<fieldNames.size(); j++)
00327             {
00328                 word fieldName = fieldNames[j];
00329 
00330 #               include "checkData.H"
00331 
00332                 if (!variableGood)
00333                 {
00334                     continue;
00335                 }
00336 
00337                 IOobject fieldObject
00338                 (
00339                     fieldName,
00340                     mesh.time().timeName(),
00341                     mesh,
00342                     IOobject::MUST_READ,
00343                     IOobject::NO_WRITE
00344                 );
00345 
00346                 if (volFieldTypes[i] == volScalarField::typeName)
00347                 {
00348                     ensightField<scalar>
00349                     (
00350                         fieldObject,
00351                         eMesh,
00352                         ensightDir,
00353                         prepend,
00354                         timeIndex,
00355                         binary,
00356                         ensightCaseFile
00357                     );
00358                 }
00359                 else if (volFieldTypes[i] == volVectorField::typeName)
00360                 {
00361                     ensightField<vector>
00362                     (
00363                         fieldObject,
00364                         eMesh,
00365                         ensightDir,
00366                         prepend,
00367                         timeIndex,
00368                         binary,
00369                         ensightCaseFile
00370                     );
00371                 }
00372                 else if (volFieldTypes[i] == volSphericalTensorField::typeName)
00373                 {
00374                     ensightField<sphericalTensor>
00375                     (
00376                         fieldObject,
00377                         eMesh,
00378                         ensightDir,
00379                         prepend,
00380                         timeIndex,
00381                         binary,
00382                         ensightCaseFile
00383                     );
00384                 }
00385                 else if (volFieldTypes[i] == volSymmTensorField::typeName)
00386                 {
00387                     ensightField<symmTensor>
00388                     (
00389                         fieldObject,
00390                         eMesh,
00391                         ensightDir,
00392                         prepend,
00393                         timeIndex,
00394                         binary,
00395                         ensightCaseFile
00396                     );
00397                 }
00398                 else if (volFieldTypes[i] == volTensorField::typeName)
00399                 {
00400                     ensightField<tensor>
00401                     (
00402                         fieldObject,
00403                         eMesh,
00404                         ensightDir,
00405                         prepend,
00406                         timeIndex,
00407                         binary,
00408                         ensightCaseFile
00409                     );
00410                 }
00411             }
00412         }
00413 
00414 
00415         // Cloud field data output
00416         // ~~~~~~~~~~~~~~~~~~~~~~~
00417 
00418         forAllConstIter(HashTable<HashTable<word> >, allCloudFields, cloudIter)
00419         {
00420             const word& cloudName = cloudIter.key();
00421 
00422             fileNameList currentCloudDirs = readDir
00423             (
00424                 runTime.timePath()/regionPrefix/cloud::prefix,
00425                 fileName::DIRECTORY
00426             );
00427 
00428             bool cloudExists = inFileNameList(currentCloudDirs, cloudName);
00429             ensightParticlePositions
00430             (
00431                 mesh,
00432                 ensightDir,
00433                 timeFile,
00434                 cloudName,
00435                 cloudExists
00436             );
00437 
00438             forAllConstIter(HashTable<word>, cloudIter(), fieldIter)
00439             {
00440                 const word& fieldName = fieldIter.key();
00441                 const word& fieldType = fieldIter();
00442 
00443                 IOobject fieldObject
00444                 (
00445                     fieldName,
00446                     mesh.time().timeName(),
00447                     cloud::prefix/cloudName,
00448                     mesh,
00449                     IOobject::MUST_READ
00450                 );
00451 
00452                 bool fieldExists = fieldObject.headerOk();
00453                 if (fieldType == scalarIOField::typeName)
00454                 {
00455                     ensightCloudField<scalar>
00456                     (
00457                         fieldObject,
00458                         ensightDir,
00459                         prepend,
00460                         timeIndex,
00461                         cloudName,
00462                         ensightCaseFile,
00463                         fieldExists
00464                     );
00465                 }
00466                 else if (fieldType == vectorIOField::typeName)
00467                 {
00468                     ensightCloudField<vector>
00469                     (
00470                         fieldObject,
00471                         ensightDir,
00472                         prepend,
00473                         timeIndex,
00474                         cloudName,
00475                         ensightCaseFile,
00476                         fieldExists
00477                     );
00478                 }
00479                 else
00480                 {
00481                     Info<< "Unable to convert field type " << fieldType
00482                         << " for field " << fieldName << endl;
00483                 }
00484             }
00485         }
00486     }
00487 
00488 #   include "ensightCaseTail.H"
00489 
00490     if (Pstream::master())
00491     {
00492         delete ensightCaseFilePtr;
00493     }
00494 
00495     Info<< "End\n" << endl;
00496 
00497     return 0;
00498 }
00499 
00500 
00501 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd