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-2023 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 "argList.H"
33 #include "volFields.H"
34 #include "IFstream.H"
35 
36 using namespace Foam;
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 
40 int main(int argc, char *argv[])
41 {
43  argList::validArgs.append("SMAP file");
44 
45  argList args(argc, argv);
46 
47  if (!args.check())
48  {
49  FatalError.exit();
50  }
51 
52  #include "createTime.H"
53 
54  fileNameList fieldNames = readDir(runTime.timePath(), fileType::file);
55  dictionary fieldNameDict;
56  forAll(fieldNames, i)
57  {
58  fieldNameDict.add(word(fieldNames[i]), word(fieldNames[i]));
59  }
60 
61  dictionary nameMap;
62  if (fieldNameDict.found("U")) nameMap.add("SU", word("U"));
63  if (fieldNameDict.found("p")) nameMap.add("P", word("p"));
64  if (fieldNameDict.found("T")) nameMap.add("T", word("T"));
65  if (fieldNameDict.found("rho")) nameMap.add("DENS", word("rho"));
66  if (fieldNameDict.found("k")) nameMap.add("TE", word("k"));
67  if (fieldNameDict.found("epsilon")) nameMap.add("ED", word("epsilon"));
68  if (fieldNameDict.found("nuEff")) nameMap.add("VIS", word("nuEff"));
69 
70  #include "createMeshNoChangers.H"
71 
72  IFstream smapFile(args[1]);
73 
74  if (!smapFile.good())
75  {
77  << "Cannot open SMAP file " << smapFile.name()
78  << exit(FatalError);
79  }
80 
81  while (!smapFile.eof())
82  {
83  wordList starFieldNames(10);
84 
85  token fieldName(smapFile);
86 
87  if (!smapFile.good())
88  {
89  break;
90  }
91 
92  if
93  (
94  fieldName.type() != token::WORD
95  && fieldName.wordToken() != "CELL"
96  )
97  {
99  << "Expected first CELL, found "
100  << fieldName
101  << exit(FatalError);
102  }
103 
104  label nCols = 0;
105  smapFile >> fieldName;
106  while (fieldName.type() == token::WORD)
107  {
108  starFieldNames[nCols++] = fieldName.wordToken();
109  smapFile >> fieldName;
110  }
111 
112  List<volScalarField*> sFields
113  (
114  nCols,
115  reinterpret_cast<volScalarField*>(0)
116  );
117 
118  List<volVectorField*> vFields
119  (
120  nCols,
121  reinterpret_cast<volVectorField*>(0)
122  );
123 
124  label i=0;
125  while (i < nCols)
126  {
127  if (nameMap.found(starFieldNames[i]))
128  {
129  if (starFieldNames[i] == "SU")
130  {
131  vFields[i] =
132  new volVectorField
133  (
134  IOobject
135  (
136  nameMap.lookup(starFieldNames[i]),
137  runTime.name(),
138  mesh,
141  false
142  ),
143  mesh
144  );
145 
146  i += 3;
147  }
148  else
149  {
150  sFields[i] =
151  new volScalarField
152  (
153  IOobject
154  (
155  nameMap.lookup(starFieldNames[i]),
156  runTime.name(),
157  mesh,
160  false
161  ),
162  mesh
163  );
164 
165  i++;
166  }
167  }
168  else
169  {
170  i++;
171  }
172  }
173 
174 
175  label cell;
176  scalar value;
177  forAll(mesh.cells(), celli)
178  {
179  if (celli > 0)
180  {
181  smapFile >> cell;
182  }
183 
184  label i=0;
185  while (i < nCols)
186  {
187  if (sFields[i])
188  {
189  smapFile >> (*sFields[i])[celli];
190  i++;
191  }
192  else if (vFields[i])
193  {
194  smapFile >> (*vFields[i])[celli].x();
195  smapFile >> (*vFields[i])[celli].y();
196  smapFile >> (*vFields[i])[celli].z();
197  i += 3;
198  }
199  else
200  {
201  smapFile >> value;
202  i++;
203  }
204  }
205  }
206 
207  for (label i=0; i<nCols; i++)
208  {
209  if (sFields[i])
210  {
211  sFields[i]->correctBoundaryConditions();
212  sFields[i]->write();
213  delete sFields[i];
214  sFields[i] = nullptr;
215  }
216  else if (vFields[i])
217  {
218  vFields[i]->correctBoundaryConditions();
219  vFields[i]->write();
220  delete vFields[i];
221  vFields[i] = nullptr;
222  }
223  }
224 
225  // Read dummy entry and check the cell index
226  smapFile >> cell;
227 
228  if (cell != 0)
229  {
231  << "Expected first SMAP dummy entry to be cell 0, found "
232  << cell
233  << exit(FatalError);
234  }
235 
236  for (label i=0; i<nCols; i++)
237  {
238  smapFile >> value;
239  }
240  }
241 
242  Info<< "End\n" << endl;
243 
244  return 0;
245 }
246 
247 
248 // ************************************************************************* //
scalar y
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Generic GeometricField class.
Input from file stream.
Definition: IFstream.H:85
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
const fileName & name() const
Return the name of the stream.
Definition: ITstream.H:128
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:103
static void noParallel()
Remove the parallel options.
Definition: argList.C:175
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
bool check(bool checkArgs=true, bool checkOpts=true) const
Check argument list.
Definition: argList.C:1400
A cell is defined as a list of faces with extra functionality.
Definition: cell.H:60
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:860
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1169
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:659
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:125
A token holds items read from Istream.
Definition: token.H:73
@ WORD
Definition: token.H:83
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
int main(int argc, char *argv[])
Definition: financialFoam.C:44
static List< word > fieldNames
Definition: globalFoam.H:46
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
VolField< vector > volVectorField
Definition: volFieldsFwd.H:62
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
messageStream Info
VolField< scalar > volScalarField
Definition: volFieldsFwd.H:61
error FatalError
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
Foam::argList args(argc, argv)