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