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

smapToFoam.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 a STAR-CD SMAP data file into FOAM field format.
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "fvCFD.H"
00031 #include "IFstream.H"
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 // Main program:
00035 
00036 int main(int argc, char *argv[])
00037 {
00038     argList::noParallel();
00039     argList::validArgs.append("SMAP fileName");
00040 
00041     argList args(argc, argv);
00042 
00043     if (!args.check())
00044     {
00045         FatalError.exit();
00046     }
00047 
00048 #   include "createTime.H"
00049 
00050     fileNameList fieldNames = readDir(runTime.timePath(), fileName::FILE);
00051     dictionary fieldNameDict;
00052     forAll (fieldNames, i)
00053     {
00054         fieldNameDict.add(fieldNames[i], word(fieldNames[i]));
00055     }
00056 
00057     dictionary nameMap;
00058     if (fieldNameDict.found("U")) nameMap.add("SU", word("U"));
00059     if (fieldNameDict.found("p")) nameMap.add("P", word("p"));
00060     if (fieldNameDict.found("T")) nameMap.add("T", word("T"));
00061     if (fieldNameDict.found("rho")) nameMap.add("DENS", word("rho"));
00062     if (fieldNameDict.found("k")) nameMap.add("TE", word("k"));
00063     if (fieldNameDict.found("epsilon")) nameMap.add("ED", word("epsilon"));
00064     if (fieldNameDict.found("nuEff")) nameMap.add("VIS", word("nuEff"));
00065 
00066 #   include "createMesh.H"
00067 
00068     IFstream smapFile(args.additionalArgs()[0]);
00069 
00070     if (!smapFile.good())
00071     {
00072         FatalErrorIn(args.executable())
00073             << "Cannot open SMAP file " << smapFile.name()
00074             << exit(FatalError);
00075     }
00076 
00077     while (!smapFile.eof())
00078     {
00079         wordList starFieldNames(10);
00080 
00081         token fieldName(smapFile);
00082 
00083         if (!smapFile.good())
00084         {
00085             break;
00086         }
00087 
00088         if
00089         (
00090             fieldName.type() != token::WORD
00091          && fieldName.wordToken() != "CELL"
00092         )
00093         {
00094             FatalErrorIn(args.executable())
00095                 << "Expected first CELL, found "
00096                 << fieldName
00097                 << exit(FatalError);
00098         }
00099 
00100         label nCols = 0;
00101         smapFile >> fieldName;
00102         while (fieldName.type() == token::WORD)
00103         {
00104             starFieldNames[nCols++] = fieldName.wordToken();
00105             smapFile >> fieldName;
00106         }
00107 
00108         List<volScalarField*> sFields
00109         (
00110             nCols,
00111             reinterpret_cast<volScalarField*>(0)
00112         );
00113 
00114         List<volVectorField*> vFields
00115         (
00116             nCols,
00117             reinterpret_cast<volVectorField*>(0)
00118         );
00119 
00120         label i=0;
00121         while (i < nCols)
00122         {
00123             if (nameMap.found(starFieldNames[i]))
00124             {
00125                 if (starFieldNames[i] == "SU")
00126                 {
00127                     vFields[i] =
00128                     new volVectorField
00129                     (
00130                         IOobject
00131                         (
00132                             nameMap.lookup(starFieldNames[i]),
00133                             runTime.timeName(),
00134                             mesh,
00135                             IOobject::MUST_READ,
00136                             IOobject::AUTO_WRITE
00137                         ),
00138                         mesh
00139                     );
00140 
00141                     i += 3;
00142                 }
00143                 else
00144                 {
00145                     sFields[i] =
00146                     new volScalarField
00147                     (
00148                         IOobject
00149                         (
00150                             nameMap.lookup(starFieldNames[i]),
00151                             runTime.timeName(),
00152                             mesh,
00153                             IOobject::MUST_READ,
00154                             IOobject::AUTO_WRITE
00155                         ),
00156                         mesh
00157                     );
00158 
00159                     i++;
00160                 }
00161             }
00162             else
00163             {
00164                 i++;
00165             }
00166         }
00167 
00168 
00169         label cell;
00170         scalar value;
00171         forAll (mesh.cells(), celli)
00172         {
00173             if (celli > 0)
00174             {
00175                 smapFile >> cell;
00176             }
00177 
00178             label i=0;
00179             while (i < nCols)
00180             {
00181                 if (sFields[i])
00182                 {
00183                     smapFile >> (*sFields[i])[celli];
00184                     i++;
00185                 }
00186                 else if (vFields[i])
00187                 {
00188                     smapFile >> (*vFields[i])[celli].x();
00189                     smapFile >> (*vFields[i])[celli].y();
00190                     smapFile >> (*vFields[i])[celli].z();
00191                     i += 3;
00192                 }
00193                 else
00194                 {
00195                     smapFile >> value;
00196                     i++;
00197                 }
00198             }
00199         }
00200 
00201         for (label i=0; i<nCols; i++)
00202         {
00203             if (sFields[i])
00204             {
00205                 sFields[i]->correctBoundaryConditions();
00206                 sFields[i]->write();
00207                 delete sFields[i];
00208                 sFields[i] = NULL;
00209             }
00210             else if (vFields[i])
00211             {
00212                 vFields[i]->correctBoundaryConditions();
00213                 vFields[i]->write();
00214                 delete vFields[i];
00215                 vFields[i] = NULL;
00216             }
00217         }
00218 
00219         // Read dummy entry and check the cell index
00220         smapFile >> cell;
00221 
00222         if (cell != 0)
00223         {
00224             FatalErrorIn(args.executable())
00225                 << "Expected first SMAP dummy entry to be cell 0, found "
00226                 << cell
00227                 << exit(FatalError);
00228         }
00229 
00230         for (label i=0; i<nCols; i++)
00231         {
00232             smapFile >> value;
00233         }
00234     }
00235 
00236     Info << "End\n" << endl;
00237 
00238     return 0;
00239 }
00240 
00241 
00242 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd