initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / utilities / postProcessing / dataConversion / smapToFoam / smapToFoam.C
blob748ce3f5991764aaea464b2dcf72d074efd6b810
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 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 a STAR-CD SMAP data file into FOAM field format.
28 \*---------------------------------------------------------------------------*/
30 #include "fvCFD.H"
31 #include "IFstream.H"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 // Main program:
36 int main(int argc, char *argv[])
38     argList::noParallel();
39     argList::validArgs.append("SMAP fileName");
41     argList args(argc, argv);
43     if (!args.check())
44     {
45         FatalError.exit();
46     }
48 #   include "createTime.H"
50     fileNameList fieldNames = readDir(runTime.timePath(), fileName::FILE);
51     dictionary fieldNameDict;
52     forAll (fieldNames, i)
53     {
54         fieldNameDict.add(fieldNames[i], word(fieldNames[i]));
55     }
57     dictionary nameMap;
58     if (fieldNameDict.found("U")) nameMap.add("SU", word("U"));
59     if (fieldNameDict.found("p")) nameMap.add("P", word("p"));
60     if (fieldNameDict.found("T")) nameMap.add("T", word("T"));
61     if (fieldNameDict.found("rho")) nameMap.add("DENS", word("rho"));
62     if (fieldNameDict.found("k")) nameMap.add("TE", word("k"));
63     if (fieldNameDict.found("epsilon")) nameMap.add("ED", word("epsilon"));
64     if (fieldNameDict.found("nuEff")) nameMap.add("VIS", word("nuEff"));
66 #   include "createMesh.H"
68     IFstream smapFile(args.additionalArgs()[0]);
70     if (!smapFile.good())
71     {
72         FatalErrorIn(args.executable())
73             << "Cannot open SMAP file " << smapFile.name()
74             << exit(FatalError);
75     }
77     while (!smapFile.eof())
78     {
79         wordList starFieldNames(10);
81         token fieldName(smapFile);
83         if (!smapFile.good())
84         {
85             break;
86         }
88         if
89         (
90             fieldName.type() != token::WORD
91          && fieldName.wordToken() != "CELL"
92         )
93         {
94             FatalErrorIn(args.executable())
95                 << "Expected first CELL, found "
96                 << fieldName
97                 << exit(FatalError);
98         }
100         label nCols = 0;
101         smapFile >> fieldName;
102         while (fieldName.type() == token::WORD)
103         {
104             starFieldNames[nCols++] = fieldName.wordToken();
105             smapFile >> fieldName;
106         }
108         List<volScalarField*> sFields
109         (
110             nCols,
111             reinterpret_cast<volScalarField*>(0)
112         );
114         List<volVectorField*> vFields
115         (
116             nCols,
117             reinterpret_cast<volVectorField*>(0)
118         );
120         label i=0;
121         while (i < nCols)
122         {
123             if (nameMap.found(starFieldNames[i]))
124             {
125                 if (starFieldNames[i] == "SU")
126                 {
127                     vFields[i] =
128                     new volVectorField
129                     (
130                         IOobject
131                         (
132                             nameMap.lookup(starFieldNames[i]),
133                             runTime.timeName(),
134                             mesh,
135                             IOobject::MUST_READ,
136                             IOobject::AUTO_WRITE
137                         ),
138                         mesh
139                     );
141                     i += 3;
142                 }
143                 else
144                 {
145                     sFields[i] =
146                     new volScalarField
147                     (
148                         IOobject
149                         (
150                             nameMap.lookup(starFieldNames[i]),
151                             runTime.timeName(),
152                             mesh,
153                             IOobject::MUST_READ,
154                             IOobject::AUTO_WRITE
155                         ),
156                         mesh
157                     );
159                     i++;
160                 }
161             }
162             else
163             {
164                 i++;
165             }
166         }
169         label cell;
170         scalar value;
171         forAll (mesh.cells(), celli)
172         {
173             if (celli > 0)
174             {
175                 smapFile >> cell;
176             }
178             label i=0;
179             while (i < nCols)
180             {
181                 if (sFields[i])
182                 {
183                     smapFile >> (*sFields[i])[celli];
184                     i++;
185                 }
186                 else if (vFields[i])
187                 {
188                     smapFile >> (*vFields[i])[celli].x();
189                     smapFile >> (*vFields[i])[celli].y();
190                     smapFile >> (*vFields[i])[celli].z();
191                     i += 3;
192                 }
193                 else
194                 {
195                     smapFile >> value;
196                     i++;
197                 }
198             }
199         }
201         for (label i=0; i<nCols; i++)
202         {
203             if (sFields[i])
204             {
205                 sFields[i]->correctBoundaryConditions();
206                 sFields[i]->write();
207                 delete sFields[i];
208                 sFields[i] = NULL;
209             }
210             else if (vFields[i])
211             {
212                 vFields[i]->correctBoundaryConditions();
213                 vFields[i]->write();
214                 delete vFields[i];
215                 vFields[i] = NULL;
216             }
217         }
219         // Read dummy entry and check the cell index
220         smapFile >> cell;
222         if (cell != 0)
223         {
224             FatalErrorIn(args.executable())
225                 << "Expected first SMAP dummy entry to be cell 0, found "
226                 << cell
227                 << exit(FatalError);
228         }
230         for (label i=0; i<nCols; i++)
231         {
232             smapFile >> value;
233         }
234     }
236     Info << "End\n" << endl;
238     return 0;
242 // ************************************************************************* //