FreeFOAM The Cross-Platform CFD Toolkit
smapToFoam.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  smapToFoam
26 
27 Description
28  Translates a STAR-CD SMAP data file into FOAM field format.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include <finiteVolume/fvCFD.H>
33 #include <OpenFOAM/IFstream.H>
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 // Main program:
37 
38 int main(int argc, char *argv[])
39 {
40  argList::noParallel();
41  argList::validArgs.append("SMAP fileName");
42 
43  argList args(argc, argv);
44 
45  if (!args.check())
46  {
47  FatalError.exit();
48  }
49 
50 # include <OpenFOAM/createTime.H>
51 
52  fileNameList fieldNames = readDir(runTime.timePath(), fileName::FILE);
53  dictionary fieldNameDict;
54  forAll (fieldNames, i)
55  {
56  fieldNameDict.add(fieldNames[i], word(fieldNames[i]));
57  }
58 
59  dictionary nameMap;
60  if (fieldNameDict.found("U")) nameMap.add("SU", word("U"));
61  if (fieldNameDict.found("p")) nameMap.add("P", word("p"));
62  if (fieldNameDict.found("T")) nameMap.add("T", word("T"));
63  if (fieldNameDict.found("rho")) nameMap.add("DENS", word("rho"));
64  if (fieldNameDict.found("k")) nameMap.add("TE", word("k"));
65  if (fieldNameDict.found("epsilon")) nameMap.add("ED", word("epsilon"));
66  if (fieldNameDict.found("nuEff")) nameMap.add("VIS", word("nuEff"));
67 
68 # include <OpenFOAM/createMesh.H>
69 
70  IFstream smapFile(args.additionalArgs()[0]);
71 
72  if (!smapFile.good())
73  {
75  << "Cannot open SMAP file " << smapFile.name()
76  << exit(FatalError);
77  }
78 
79  while (!smapFile.eof())
80  {
81  wordList starFieldNames(10);
82 
83  token fieldName(smapFile);
84 
85  if (!smapFile.good())
86  {
87  break;
88  }
89 
90  if
91  (
92  fieldName.type() != token::WORD
93  && fieldName.wordToken() != "CELL"
94  )
95  {
97  << "Expected first CELL, found "
98  << fieldName
99  << exit(FatalError);
100  }
101 
102  label nCols = 0;
103  smapFile >> fieldName;
104  while (fieldName.type() == token::WORD)
105  {
106  starFieldNames[nCols++] = fieldName.wordToken();
107  smapFile >> fieldName;
108  }
109 
110  List<volScalarField*> sFields
111  (
112  nCols,
113  reinterpret_cast<volScalarField*>(0)
114  );
115 
116  List<volVectorField*> vFields
117  (
118  nCols,
119  reinterpret_cast<volVectorField*>(0)
120  );
121 
122  label i=0;
123  while (i < nCols)
124  {
125  if (nameMap.found(starFieldNames[i]))
126  {
127  if (starFieldNames[i] == "SU")
128  {
129  vFields[i] =
130  new volVectorField
131  (
132  IOobject
133  (
134  nameMap.lookup(starFieldNames[i]),
135  runTime.timeName(),
136  mesh,
137  IOobject::MUST_READ,
138  IOobject::AUTO_WRITE
139  ),
140  mesh
141  );
142 
143  i += 3;
144  }
145  else
146  {
147  sFields[i] =
148  new volScalarField
149  (
150  IOobject
151  (
152  nameMap.lookup(starFieldNames[i]),
153  runTime.timeName(),
154  mesh,
155  IOobject::MUST_READ,
156  IOobject::AUTO_WRITE
157  ),
158  mesh
159  );
160 
161  i++;
162  }
163  }
164  else
165  {
166  i++;
167  }
168  }
169 
170 
171  label cell;
172  scalar value;
173  forAll (mesh.cells(), celli)
174  {
175  if (celli > 0)
176  {
177  smapFile >> cell;
178  }
179 
180  label i=0;
181  while (i < nCols)
182  {
183  if (sFields[i])
184  {
185  smapFile >> (*sFields[i])[celli];
186  i++;
187  }
188  else if (vFields[i])
189  {
190  smapFile >> (*vFields[i])[celli].x();
191  smapFile >> (*vFields[i])[celli].y();
192  smapFile >> (*vFields[i])[celli].z();
193  i += 3;
194  }
195  else
196  {
197  smapFile >> value;
198  i++;
199  }
200  }
201  }
202 
203  for (label i=0; i<nCols; i++)
204  {
205  if (sFields[i])
206  {
207  sFields[i]->correctBoundaryConditions();
208  sFields[i]->write();
209  delete sFields[i];
210  sFields[i] = NULL;
211  }
212  else if (vFields[i])
213  {
214  vFields[i]->correctBoundaryConditions();
215  vFields[i]->write();
216  delete vFields[i];
217  vFields[i] = NULL;
218  }
219  }
220 
221  // Read dummy entry and check the cell index
222  smapFile >> cell;
223 
224  if (cell != 0)
225  {
227  << "Expected first SMAP dummy entry to be cell 0, found "
228  << cell
229  << exit(FatalError);
230  }
231 
232  for (label i=0; i<nCols; i++)
233  {
234  smapFile >> value;
235  }
236  }
237 
238  Info << "End\n" << endl;
239 
240  return 0;
241 }
242 
243 
244 // ************************ vim: set sw=4 sts=4 et: ************************ //