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

foamToEnsightParts.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 Application
00026     foamToEnsightParts
00027 
00028 Description
00029     Translates OpenFOAM data to Ensight format.
00030     An Ensight part is created for each cellZone and patch.
00031 
00032 Usage
00033     - foamToEnsightParts [OPTION] \n
00034     Translates OpenFOAM data to Ensight format
00035 
00036     @param -ascii \n
00037     Write Ensight data in ASCII format instead of "C Binary"
00038 
00039     @param -noZero \n
00040     Exclude the often incomplete initial conditions.
00041 
00042     @param -index <start>\n
00043     Ignore the time index contained in the time file and use a
00044     simple indexing when creating the @c Ensight/data/######## files.
00045 
00046     @param -noMesh \n
00047     Suppress writing the geometry. Can be useful for converting partial
00048     results for a static geometry.
00049 
00050 Note
00051     - no parallel data.
00052     - writes to @a Ensight directory to avoid collisions with foamToEnsight.
00053 
00054 \*---------------------------------------------------------------------------*/
00055 
00056 #include "argList.H"
00057 #include "timeSelector.H"
00058 
00059 #include "volFields.H"
00060 #include "OFstream.H"
00061 #include "IOmanip.H"
00062 #include "IOobjectList.H"
00063 #include "scalarIOField.H"
00064 #include "tensorIOField.H"
00065 
00066 #include "ensightParts.H"
00067 #include "ensightOutputFunctions.H"
00068 
00069 using namespace Foam;
00070 
00071 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00072 
00073 // Main program:
00074 
00075 int main(int argc, char *argv[])
00076 {
00077     // enable -constant
00078     // probably don't need -zeroTime though, since the fields are vetted
00079     // afterwards anyhow
00080     timeSelector::addOptions(true, false);
00081     argList::noParallel();
00082     argList::validOptions.insert("ascii", "");
00083     argList::validOptions.insert("index",  "start");
00084     argList::validOptions.insert("noMesh", "");
00085 
00086     // the volume field types that we handle
00087     wordHashSet volFieldTypes;
00088     volFieldTypes.insert(volScalarField::typeName);
00089     volFieldTypes.insert(volVectorField::typeName);
00090     volFieldTypes.insert(volSphericalTensorField::typeName);
00091     volFieldTypes.insert(volSymmTensorField::typeName);
00092     volFieldTypes.insert(volTensorField::typeName);
00093 
00094     // the lagrangian field types that we handle
00095     wordHashSet cloudFieldTypes;
00096     cloudFieldTypes.insert(scalarIOField::typeName);
00097     cloudFieldTypes.insert(vectorIOField::typeName);
00098     cloudFieldTypes.insert(tensorIOField::typeName);
00099 
00100     const char* geometryName = "geometry";
00101 
00102 #   include "setRootCase.H"
00103 #   include "createTime.H"
00104 
00105     // get times list
00106     instantList timeDirs = timeSelector::select0(runTime, args);
00107 
00108     // default to binary output, unless otherwise specified
00109     IOstream::streamFormat format = IOstream::BINARY;
00110     if (args.optionFound("ascii"))
00111     {
00112         format = IOstream::ASCII;
00113     }
00114 
00115     // control for renumbering iterations
00116     bool optIndex = false;
00117     label indexingNumber = 0;
00118     if (args.optionFound("index"))
00119     {
00120         optIndex = true;
00121         indexingNumber = args.optionRead<label>("index");
00122     }
00123 
00124     // always write the geometry, unless the -noMesh option is specified
00125     bool optNoMesh = args.optionFound("noMesh");
00126 
00127     fileName ensightDir = args.rootPath()/args.globalCaseName()/"Ensight";
00128     fileName dataDir = ensightDir/"data";
00129     fileName caseFileName = "Ensight.case";
00130     fileName dataMask = fileName("data")/ensightFile::mask();
00131 
00132     // Ensight and Ensight/data directories must exist
00133     // do not remove old data - we might wish to convert new results
00134     // or a particular time interval
00135     if (isDir(ensightDir))
00136     {
00137         Info<<"Warning: reusing existing directory" << nl
00138             << "    " << ensightDir << endl;
00139     }
00140     mkDir(ensightDir);
00141     mkDir(dataDir);
00142 
00143 #   include "createNamedMesh.H"
00144 
00145     // Mesh instance (region0 gets filtered out)
00146     fileName regionPrefix;
00147 
00148     if (regionName != polyMesh::defaultRegion)
00149     {
00150         regionPrefix = regionName;
00151     }
00152 
00153     // Construct the list of ensight parts for the entire mesh
00154     ensightParts partsList(mesh);
00155 
00156     // write summary information
00157     {
00158         OFstream partsInfoFile(ensightDir/"partsInfo");
00159 
00160         partsInfoFile
00161             << "// summary of ensight parts" << nl << nl;
00162         partsList.writeSummary(partsInfoFile);
00163     }
00164 
00165 #   include "checkHasMovingMesh.H"
00166 #   include "findFields.H"
00167 
00168     if (hasMovingMesh && optNoMesh)
00169     {
00170         Info<< "mesh is moving: ignoring '-noMesh' option" << endl;
00171         optNoMesh = false;
00172     }
00173 
00174 
00175     // map times used
00176     Map<scalar>  timeIndices;
00177 
00178     // Track the time indices used by the volume fields
00179     DynamicList<label> fieldTimesUsed;
00180 
00181     // Track the time indices used by each cloud
00182     HashTable<DynamicList<label> > cloudTimesUsed;
00183 
00184     // Create a new DynamicList for each cloud
00185     forAllConstIter(HashTable<HashTable<word> >, cloudFields, cloudIter)
00186     {
00187         cloudTimesUsed.insert(cloudIter.key(), DynamicList<label>());
00188     }
00189 
00190 
00191     forAll(timeDirs, timeI)
00192     {
00193         runTime.setTime(timeDirs[timeI], timeI);
00194 
00195 #       include "getTimeIndex.H"
00196 
00197         // remember the time index
00198         fieldTimesUsed.append(timeIndex);
00199 
00200         // the data/ITER subdirectory must exist
00201         fileName subDir = ensightFile::subDir(timeIndex);
00202         mkDir(dataDir/subDir);
00203 
00204         // place a timestamp in the directory for future reference
00205         {
00206             OFstream timeStamp(dataDir/subDir/"time");
00207             timeStamp
00208                 << "#   timestep time" << nl
00209                 << subDir.c_str() << " " << runTime.timeName() << nl;
00210         }
00211 
00212 #       include "moveMesh.H"
00213 
00214         if (timeI == 0 || mesh.moving())
00215         {
00216             if (mesh.moving())
00217             {
00218                 partsList.recalculate(mesh);
00219             }
00220 
00221             if (!optNoMesh)
00222             {
00223                 fileName geomDir;
00224                 if (hasMovingMesh)
00225                 {
00226                     geomDir = dataDir/subDir;
00227                 }
00228 
00229                 ensightGeoFile geoFile(ensightDir/geomDir/geometryName, format);
00230                 partsList.writeGeometry(geoFile);
00231                 Info<< nl;
00232             }
00233         }
00234 
00235         Info<< "write volume field (" << flush;
00236 
00237         forAllConstIter(HashTable<word>, volumeFields, fieldIter)
00238         {
00239             const word& fieldName = fieldIter.key();
00240             const word& fieldType = fieldIter();
00241 
00242             IOobject fieldObject
00243             (
00244                 fieldName,
00245                 mesh.time().timeName(),
00246                 mesh,
00247                 IOobject::MUST_READ,
00248                 IOobject::NO_WRITE
00249             );
00250 
00251             if (fieldType == volScalarField::typeName)
00252             {
00253                 ensightVolField<scalar>
00254                 (
00255                     partsList,
00256                     fieldObject,
00257                     mesh,
00258                     dataDir,
00259                     subDir,
00260                     format
00261                 );
00262 
00263             }
00264             else if (fieldType == volVectorField::typeName)
00265             {
00266                 ensightVolField<vector>
00267                 (
00268                     partsList,
00269                     fieldObject,
00270                     mesh,
00271                     dataDir,
00272                     subDir,
00273                     format
00274                 );
00275 
00276             }
00277             else if (fieldType == volSphericalTensorField::typeName)
00278             {
00279                 ensightVolField<sphericalTensor>
00280                 (
00281                     partsList,
00282                     fieldObject,
00283                     mesh,
00284                     dataDir,
00285                     subDir,
00286                     format
00287                 );
00288 
00289             }
00290             else if (fieldType == volSymmTensorField::typeName)
00291             {
00292                 ensightVolField<symmTensor>
00293                 (
00294                     partsList,
00295                     fieldObject,
00296                     mesh,
00297                     dataDir,
00298                     subDir,
00299                     format
00300                 );
00301             }
00302             else if (fieldType == volTensorField::typeName)
00303             {
00304                 ensightVolField<tensor>
00305                 (
00306                     partsList,
00307                     fieldObject,
00308                     mesh,
00309                     dataDir,
00310                     subDir,
00311                     format
00312                 );
00313             }
00314         }
00315         Info<< " )" << endl;
00316 
00317         // check for clouds
00318         forAllConstIter(HashTable<HashTable<word> >, cloudFields, cloudIter)
00319         {
00320             const word& cloudName = cloudIter.key();
00321 
00322             if
00323             (
00324                 !isDir
00325                 (
00326                     runTime.timePath()/regionPrefix/
00327                     cloud::prefix/cloudName
00328                 )
00329             )
00330             {
00331                 continue;
00332             }
00333 
00334             IOobjectList cloudObjs
00335             (
00336                 mesh,
00337                 runTime.timeName(),
00338                 cloud::prefix/cloudName
00339             );
00340 
00341             // check that the positions field is present for this time
00342             if (cloudObjs.lookup("positions"))
00343             {
00344                 ensightParticlePositions
00345                 (
00346                     mesh,
00347                     dataDir,
00348                     subDir,
00349                     cloudName,
00350                     format
00351                 );
00352             }
00353             else
00354             {
00355                 continue;
00356             }
00357 
00358             Info<< "write " << cloudName << " (" << flush;
00359 
00360             forAllConstIter(HashTable<word>, cloudIter(), fieldIter)
00361             {
00362                 const word& fieldName = fieldIter.key();
00363                 const word& fieldType = fieldIter();
00364 
00365                 IOobject *fieldObject = cloudObjs.lookup(fieldName);
00366 
00367                 if (!fieldObject)
00368                 {
00369                     Info<< "missing "
00370                         << runTime.timeName()/cloud::prefix/cloudName
00371                         / fieldName
00372                         << endl;
00373                     continue;
00374                 }
00375 
00376                 if (fieldType == scalarIOField::typeName)
00377                 {
00378                     ensightLagrangianField<scalar>
00379                     (
00380                         *fieldObject,
00381                         dataDir,
00382                         subDir,
00383                         cloudName,
00384                         format
00385                     );
00386 
00387                 }
00388                 else if (fieldType == vectorIOField::typeName)
00389                 {
00390                     ensightLagrangianField<vector>
00391                     (
00392                         *fieldObject,
00393                         dataDir,
00394                         subDir,
00395                         cloudName,
00396                         format
00397                     );
00398 
00399                 }
00400                 else if (fieldType == tensorIOField::typeName)
00401                 {
00402                     ensightLagrangianField<tensor>
00403                     (
00404                         *fieldObject,
00405                         dataDir,
00406                         subDir,
00407                         cloudName,
00408                         format
00409                     );
00410 
00411                 }
00412             }
00413 
00414             Info<< " )" << endl;
00415 
00416             // remember the time index
00417             cloudTimesUsed[cloudName].append(timeIndex);
00418         }
00419     }
00420 
00421 #   include "ensightOutputCase.H"
00422 
00423     Info<< "\nEnd\n"<< endl;
00424 
00425     return 0;
00426 }
00427 
00428 
00429 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd