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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  smapToFoam
29 
30 Group
31  grpPostProcessingUtilities
32 
33 Description
34  Translate a STARCD SMAP data file into OpenFOAM field format.
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #include "fvCFD.H"
39 #include "IFstream.H"
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 int main(int argc, char *argv[])
44 {
45  argList::addNote
46  (
47  "Translate a STARCD SMAP data file into OpenFOAM field format"
48  );
49 
50  argList::noParallel();
51  argList::addArgument("SMAP fileName");
52 
53  argList args(argc, argv);
54 
55  if (!args.check())
56  {
57  FatalError.exit();
58  }
59 
60  #include "createTime.H"
61 
62  fileNameList fieldNames = readDir(runTime.timePath(), fileName::FILE);
63  dictionary fieldNameDict;
64  forAll(fieldNames, i)
65  {
66  fieldNameDict.add(fieldNames[i], word(fieldNames[i]));
67  }
68 
69  dictionary nameMap;
70  if (fieldNameDict.found("U")) nameMap.add("SU", word("U"));
71  if (fieldNameDict.found("p")) nameMap.add("P", word("p"));
72  if (fieldNameDict.found("T")) nameMap.add("T", word("T"));
73  if (fieldNameDict.found("rho")) nameMap.add("DENS", word("rho"));
74  if (fieldNameDict.found("k")) nameMap.add("TE", word("k"));
75  if (fieldNameDict.found("epsilon")) nameMap.add("ED", word("epsilon"));
76  if (fieldNameDict.found("nuEff")) nameMap.add("VIS", word("nuEff"));
77 
78  #include "createNamedMesh.H"
79 
80  IFstream smapFile(args.get<fileName>(1));
81 
82  if (!smapFile.good())
83  {
85  << "Cannot open SMAP file " << smapFile.name()
86  << exit(FatalError);
87  }
88 
89  while (!smapFile.eof())
90  {
91  wordList starFieldNames(10);
92 
93  token fieldName(smapFile);
94 
95  if (!smapFile.good())
96  {
97  break;
98  }
99 
100  if (!fieldName.isWord("CELL"))
101  {
103  << "Expected first CELL, found "
104  << fieldName
105  << exit(FatalError);
106  }
107 
108  label nCols = 0;
109  smapFile >> fieldName;
110  while (fieldName.isWord())
111  {
112  starFieldNames[nCols++] = fieldName.wordToken();
113  smapFile >> fieldName;
114  }
115 
116  List<volScalarField*> sFields
117  (
118  nCols,
119  reinterpret_cast<volScalarField*>(0)
120  );
121 
122  List<volVectorField*> vFields
123  (
124  nCols,
125  reinterpret_cast<volVectorField*>(0)
126  );
127 
128  label i=0;
129  while (i < nCols)
130  {
131  if (nameMap.found(starFieldNames[i]))
132  {
133  IOobject io
134  (
135  nameMap.get<word>(starFieldNames[i]),
136  runTime.timeName(),
137  mesh,
138  IOobject::MUST_READ,
139  IOobject::AUTO_WRITE
140  );
141 
142  if (starFieldNames[i] == "SU")
143  {
144  vFields[i] = new volVectorField(io, mesh);
145  i += 3;
146  }
147  else
148  {
149  sFields[i] = new volScalarField(io, mesh);
150  ++i;
151  }
152  }
153  else
154  {
155  ++i;
156  }
157  }
158 
159 
160  label cell;
161  scalar value;
162  forAll(mesh.cells(), celli)
163  {
164  if (celli > 0)
165  {
166  smapFile >> cell;
167  }
168 
169  label i=0;
170  while (i < nCols)
171  {
172  if (sFields[i])
173  {
174  smapFile >> (*sFields[i])[celli];
175  i++;
176  }
177  else if (vFields[i])
178  {
179  smapFile >> (*vFields[i])[celli].x();
180  smapFile >> (*vFields[i])[celli].y();
181  smapFile >> (*vFields[i])[celli].z();
182  i += 3;
183  }
184  else
185  {
186  smapFile >> value;
187  i++;
188  }
189  }
190  }
191 
192  for (label i=0; i<nCols; i++)
193  {
194  if (sFields[i])
195  {
196  sFields[i]->correctBoundaryConditions();
197  sFields[i]->write();
198  delete sFields[i];
199  sFields[i] = nullptr;
200  }
201  else if (vFields[i])
202  {
203  vFields[i]->correctBoundaryConditions();
204  vFields[i]->write();
205  delete vFields[i];
206  vFields[i] = nullptr;
207  }
208  }
209 
210  // Read dummy entry and check the cell index
211  smapFile >> cell;
212 
213  if (cell != 0)
214  {
216  << "Expected first SMAP dummy entry to be cell 0, found "
217  << cell
218  << exit(FatalError);
219  }
220 
221  for (label i=0; i<nCols; i++)
222  {
223  smapFile >> value;
224  }
225  }
226 
227  Info<< "End\n" << endl;
228 
229  return 0;
230 }
231 
232 
233 // ************************************************************************* //
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
Required Variables.
GeometricField< vector, fvPatchField, volMesh > volVectorField
Definition: volFieldsFwd.H:85
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:327
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:413
GeometricField< scalar, fvPatchField, volMesh > volScalarField
Definition: volFieldsFwd.H:84
scalar y
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
dynamicFvMesh & mesh
List< word > wordList
A List of words.
Definition: fileName.H:58
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:271
messageStream Info
Information stream (stdout output on master, null elsewhere)
fileNameList readDir(const fileName &directory, const fileName::Type type=fileName::Type::FILE, const bool filtergz=true, const bool followLink=true)
Read a directory and return the entries as a fileName List.
Definition: POSIX.C:916
Foam::argList args(argc, argv)
List< fileName > fileNameList
A List of fileNames.
Definition: fileNameList.H:54
bool check(bool checkArgs=argList::argsMandatory(), bool checkOpts=true) const
Check the parsed command-line for mandatory arguments and that all the options are correct...
Definition: argList.C:1996