smapToFoam.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
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 OpenFOAM field format.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "fvCFD.H"
33 #include "IFstream.H"
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
37 int main(int argc, char *argv[])
38 {
39  argList::noParallel();
40  argList::validArgs.append("SMAP file");
41 
42  argList args(argc, argv);
43 
44  if (!args.check())
45  {
46  FatalError.exit();
47  }
48 
49  #include "createTime.H"
50 
51  fileNameList fieldNames = readDir(runTime.timePath(), fileType::file);
52  dictionary fieldNameDict;
53  forAll(fieldNames, i)
54  {
55  fieldNameDict.add(fieldNames[i], word(fieldNames[i]));
56  }
57 
58  dictionary nameMap;
59  if (fieldNameDict.found("U")) nameMap.add("SU", word("U"));
60  if (fieldNameDict.found("p")) nameMap.add("P", word("p"));
61  if (fieldNameDict.found("T")) nameMap.add("T", word("T"));
62  if (fieldNameDict.found("rho")) nameMap.add("DENS", word("rho"));
63  if (fieldNameDict.found("k")) nameMap.add("TE", word("k"));
64  if (fieldNameDict.found("epsilon")) nameMap.add("ED", word("epsilon"));
65  if (fieldNameDict.found("nuEff")) nameMap.add("VIS", word("nuEff"));
66 
67  #include "createMesh.H"
68 
69  IFstream smapFile(args[1]);
70 
71  if (!smapFile.good())
72  {
74  << "Cannot open SMAP file " << smapFile.name()
75  << exit(FatalError);
76  }
77 
78  while (!smapFile.eof())
79  {
80  wordList starFieldNames(10);
81 
82  token fieldName(smapFile);
83 
84  if (!smapFile.good())
85  {
86  break;
87  }
88 
89  if
90  (
91  fieldName.type() != token::WORD
92  && fieldName.wordToken() != "CELL"
93  )
94  {
96  << "Expected first CELL, found "
97  << fieldName
98  << exit(FatalError);
99  }
100 
101  label nCols = 0;
102  smapFile >> fieldName;
103  while (fieldName.type() == token::WORD)
104  {
105  starFieldNames[nCols++] = fieldName.wordToken();
106  smapFile >> fieldName;
107  }
108 
109  List<volScalarField*> sFields
110  (
111  nCols,
112  reinterpret_cast<volScalarField*>(0)
113  );
114 
115  List<volVectorField*> vFields
116  (
117  nCols,
118  reinterpret_cast<volVectorField*>(0)
119  );
120 
121  label i=0;
122  while (i < nCols)
123  {
124  if (nameMap.found(starFieldNames[i]))
125  {
126  if (starFieldNames[i] == "SU")
127  {
128  vFields[i] =
129  new volVectorField
130  (
131  IOobject
132  (
133  nameMap.lookup(starFieldNames[i]),
134  runTime.timeName(),
135  mesh,
136  IOobject::MUST_READ,
137  IOobject::AUTO_WRITE,
138  false
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  false
158  ),
159  mesh
160  );
161 
162  i++;
163  }
164  }
165  else
166  {
167  i++;
168  }
169  }
170 
171 
172  label cell;
173  scalar value;
174  forAll(mesh.cells(), celli)
175  {
176  if (celli > 0)
177  {
178  smapFile >> cell;
179  }
180 
181  label i=0;
182  while (i < nCols)
183  {
184  if (sFields[i])
185  {
186  smapFile >> (*sFields[i])[celli];
187  i++;
188  }
189  else if (vFields[i])
190  {
191  smapFile >> (*vFields[i])[celli].x();
192  smapFile >> (*vFields[i])[celli].y();
193  smapFile >> (*vFields[i])[celli].z();
194  i += 3;
195  }
196  else
197  {
198  smapFile >> value;
199  i++;
200  }
201  }
202  }
203 
204  for (label i=0; i<nCols; i++)
205  {
206  if (sFields[i])
207  {
208  sFields[i]->correctBoundaryConditions();
209  sFields[i]->write();
210  delete sFields[i];
211  sFields[i] = nullptr;
212  }
213  else if (vFields[i])
214  {
215  vFields[i]->correctBoundaryConditions();
216  vFields[i]->write();
217  delete vFields[i];
218  vFields[i] = nullptr;
219  }
220  }
221 
222  // Read dummy entry and check the cell index
223  smapFile >> cell;
224 
225  if (cell != 0)
226  {
228  << "Expected first SMAP dummy entry to be cell 0, found "
229  << cell
230  << exit(FatalError);
231  }
232 
233  for (label i=0; i<nCols; i++)
234  {
235  smapFile >> value;
236  }
237  }
238 
239  Info<< "End\n" << endl;
240 
241  return 0;
242 }
243 
244 
245 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
GeometricField< vector, fvPatchField, volMesh > volVectorField
Definition: volFieldsFwd.H:55
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:168
GeometricField< scalar, fvPatchField, volMesh > volScalarField
Definition: volFieldsFwd.H:52
scalar y
dynamicFvMesh & mesh
fileNameList readDir(const fileName &, const fileType=fileType::file, const bool filterVariants=true, const bool followLink=true)
Read a directory and return the entries as a string list.
Definition: POSIX.C:662
List< word > wordList
A List of words.
Definition: fileName.H:54
messageStream Info
bool check(bool checkArgs=true, bool checkOpts=true) const
Check argument list.
Definition: argList.C:1385
Foam::argList args(argc, argv)
List< fileName > fileNameList
A List of fileNames.
Definition: fileNameList.H:50