incorrect comment
[OpenFOAM-1.5.x.git] / applications / utilities / postProcessing / dataConversion / foamToEnsightParts / foamToEnsightParts.C
blob61308a34b50c0c2bf59ea359b752bfeeec24de85
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Application
26     foamToEnsightParts
28 Description
29     Translates OpenFOAM data to Ensight format.
30     An Ensight part is created for each cellZone and patch.
32 Usage
33     - foamToEnsightParts [OPTION] \n
34     Translates OpenFOAM data to Ensight format
36     @param -ascii \n
37     Write Ensight data in ASCII format instead of "C Binary"
39     @param -noZero \n
40     Exclude the often incomplete initial conditions.
42 Note
43     - no parallel data.
44     - writes to @a Ensight directory to avoid collisions with foamToEnsight.
46 \*---------------------------------------------------------------------------*/
48 #include "argList.H"
49 #include "timeSelector.H"
51 #include "volFields.H"
52 #include "OFstream.H"
53 #include "IOmanip.H"
54 #include "IOobjectList.H"
55 #include "scalarIOField.H"
56 #include "tensorIOField.H"
58 #include "ensightParts.H"
59 #include "ensightOutputFunctions.H"
61 using namespace Foam;
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 // Main program:
67 int main(int argc, char *argv[])
69     // with -constant and -zeroTime
70     timeSelector::addOptions(true, false);
71     argList::noParallel();
72     argList::validOptions.insert("ascii", "");
74     const label nTypes = 2;
75     const word fieldTypes[] =
76     {
77         volScalarField::typeName,
78         volVectorField::typeName
79     };
81     const label nSprayFieldTypes = 2;
82     const word sprayFieldTypes[] =
83     {
84         scalarIOField::typeName,
85         vectorIOField::typeName
86     };
88 #   include "setRootCase.H"
89 #   include "createTime.H"
91     // get times list
92     instantList timeDirs = timeSelector::select0(runTime, args);
94     // default to binary output, unless otherwise specified
95     IOstream::streamFormat format = IOstream::BINARY;
96     if (args.options().found("ascii"))
97     {
98         format = IOstream::ASCII;
99     }
101     fileName ensightDir = args.rootPath()/args.globalCaseName()/"Ensight";
102     fileName dataDir = ensightDir/"data";
103     fileName caseFileName = "Ensight.case";
104     fileName dataMask = fileName("data")/ensightFile::mask();
106     // Ensight and Ensight/data directories must exist
107     if (dir(ensightDir))
108     {
109         rmDir(ensightDir);
110     }
111     mkDir(ensightDir);
112     mkDir(dataDir);
114 #   include "createMesh.H"
115     // Construct the list of ensight parts for the entire mesh
116     ensightParts partsList(mesh);
118     // write summary information
119     {
120         OFstream partsInfoFile(ensightDir/"partsInfo");
122         partsInfoFile
123             << "// summary of ensight parts" << nl << nl;
124         partsList.writeSummary(partsInfoFile);
125     }
127 #   include "checkHasMovingMesh.H"
128 #   include "checkHasLagrangian.H"
130     // only take the objects that exists at the end of the calculation
131     IOobjectList objects(mesh, timeDirs[timeDirs.size()-1].name());
132     IOobjectList sprayObjects(mesh, timeDirs[timeDirs.size()-1].name(), "lagrangian");
134     // write single geometry or one per time step
135     fileName geometryFileName("geometry");
136     if (hasMovingMesh)
137     {
138         geometryFileName = dataMask/geometryFileName;
139     }
141     // the case file is always ASCII
142     Info << "write case: " << caseFileName.c_str() << endl;
144     OFstream caseFile(ensightDir/caseFileName, IOstream::ASCII);
145     caseFile.setf(ios_base::left);
146     caseFile
147         << "FORMAT" << nl
148         << setw(16) << "type:" << "ensight gold" << nl << nl
149         << "GEOMETRY" << nl
150         << setw(16) << "model: 1" << geometryFileName.c_str() << nl;
152     if (hasLagrangian)
153     {
154         caseFile
155             << setw(16) << "measured: 2"
156             << fileName(dataMask/"lagrangian"/"positions").c_str() << nl;
157     }
158     caseFile
159         << nl << "VARIABLE" << nl;
161     label nFieldTime = timeDirs.size();
162     if (nFieldTime < 0)
163     {
164         nFieldTime = 0;
165     }
167     List<label> fieldFileNumbers(nFieldTime);
168     List<label> sprayFileNumbers(nFieldTime);
170     // map used times used
171     Map<scalar>  timeIndices;
173     nFieldTime = 0;
174     label nSprayTime = 0;
176     forAll(timeDirs, timeI)
177     {
178         runTime.setTime(timeDirs[timeI], timeI);
180 #       include "getTimeIndex.H"
182         fieldFileNumbers[nFieldTime++] = timeIndex;
184         // the data/ITER subdirectory must exist
185         fileName subDir = ensightFile::subDir(timeIndex);
186         mkDir(dataDir/subDir);
188         // place a timestamp in the directory for future reference
189         {
190             OFstream timeStamp(dataDir/subDir/"time");
191             timeStamp
192                 << "#   timestep time" << nl
193                 << subDir.c_str() << " " << runTime.timeName() << nl;
194         }
196 #       include "moveMesh.H"
198         if (nFieldTime == 1 || mesh.moving())
199         {
200             if (hasMovingMesh)
201             {
202                 geometryFileName = dataDir/subDir/"geometry";
203             }
204             if (mesh.moving())
205             {
206                 partsList.recalculate(mesh);
207             }
209             ensightGeoFile geoFile(ensightDir/geometryFileName, format);
210             partsList.writeGeometry(geoFile);
211             Info << nl;
212         }
214         Info<< "write volume field: " << flush;
216         for (label i=0; i < nTypes; i++)
217         {
218             wordList fieldNames = objects.names(fieldTypes[i]);
220             forAll (fieldNames, fieldI)
221             {
222                 word fieldName = fieldNames[fieldI];
224 #               include "checkHasValidField.H"
226                 if (!hasValidField)
227                 {
228                     continue;
229                 }
231                 IOobject fieldObject
232                 (
233                     fieldName,
234                     mesh.time().timeName(),
235                     mesh,
236                     IOobject::MUST_READ,
237                     IOobject::NO_WRITE
238                 );
240                 if (fieldTypes[i] == volScalarField::typeName)
241                 {
242                     if (nFieldTime == 1)
243                     {
244                         ensightCaseEntry<scalar>
245                         (
246                             caseFile,
247                             fieldObject,
248                             dataMask
249                         );
250                     }
252                     ensightVolField<scalar>
253                     (
254                         partsList,
255                         fieldObject,
256                         mesh,
257                         dataDir,
258                         subDir,
259                         format
260                     );
262                 }
263                 else if (fieldTypes[i] == volVectorField::typeName)
264                 {
265                     if (nFieldTime == 1)
266                     {
267                         ensightCaseEntry<vector>
268                         (
269                             caseFile,
270                             fieldObject,
271                             dataMask
272                         );
273                     }
275                     ensightVolField<vector>
276                     (
277                         partsList,
278                         fieldObject,
279                         mesh,
280                         dataDir,
281                         subDir,
282                         format
283                     );
285                 }
286                 else if (fieldTypes[i] == volSphericalTensorField::typeName)
287                 {
288                     if (nFieldTime == 1)
289                     {
290                         ensightCaseEntry<sphericalTensor>
291                         (
292                             caseFile,
293                             fieldObject,
294                             dataMask
295                         );
296                     }
298                     ensightVolField<sphericalTensor>
299                     (
300                         partsList,
301                         fieldObject,
302                         mesh,
303                         dataDir,
304                         subDir,
305                         format
306                     );
308                 }
309                 else if (fieldTypes[i] == volSymmTensorField::typeName)
310                 {
311                     if (nFieldTime == 1)
312                     {
313                         ensightCaseEntry<symmTensor>
314                         (
315                             caseFile,
316                             fieldObject,
317                             dataMask
318                         );
319                     }
321                     ensightVolField<symmTensor>
322                     (
323                         partsList,
324                         fieldObject,
325                         mesh,
326                         dataDir,
327                         subDir,
328                         format
329                     );
331                 }
332                 else if (fieldTypes[i] == volTensorField::typeName)
333                 {
334                     if (nFieldTime == 1)
335                     {
336                         ensightCaseEntry<tensor>
337                         (
338                             caseFile,
339                             fieldObject,
340                             dataMask
341                         );
342                     }
344                     ensightVolField<tensor>
345                     (
346                         partsList,
347                         fieldObject,
348                         mesh,
349                         dataDir,
350                         subDir,
351                         format
352                     );
354                 }
355             }
356         }
357         Info<< endl;
360         if (hasLagrangian)
361         {
362             // check that the positions field is present for this time
363             {
364                 IOobject ioHeader
365                 (
366                     "positions",
367                     mesh.time().timeName(),
368                     "lagrangian",
369                     mesh,
370                     IOobject::NO_READ
371                 );
373                 if (ioHeader.headerOk())
374                 {
375                     sprayFileNumbers[nSprayTime++] = timeIndex;
376                 }
377             }
379             Info<< "write  spray field: " << flush;
381             ensightParticlePositions
382             (
383                 mesh,
384                 dataDir,
385                 subDir,
386                 format
387             );
389             for (label i=0; i < nSprayFieldTypes; i++)
390             {
391                 wordList fieldNames = sprayObjects.names(sprayFieldTypes[i]);
393                 forAll (fieldNames, fieldI)
394                 {
395                     word fieldName = fieldNames[fieldI];
397 #                   include "checkHasSprayField.H"
399                     if (!hasSprayField)
400                     {
401                         continue;
402                     }
404                     IOobject fieldObject
405                     (
406                         fieldName,
407                         mesh.time().timeName(),
408                         "lagrangian",
409                         mesh,
410                         IOobject::MUST_READ,
411                         IOobject::NO_WRITE
412                     );
414                     if (sprayFieldTypes[i] == scalarIOField::typeName)
415                     {
416                         if (nSprayTime == 1)
417                         {
418                             ensightCaseEntry<scalar>
419                             (
420                                 caseFile,
421                                 fieldObject,
422                                 dataMask,
423                                 true
424                             );
425                         }
427                         ensightSprayField<scalar>
428                         (
429                             fieldObject,
430                             dataDir,
431                             subDir,
432                             format
433                         );
435                     }
436                     else if (sprayFieldTypes[i] == vectorIOField::typeName)
437                     {
438                         if (nSprayTime == 1)
439                         {
440                             ensightCaseEntry<vector>
441                             (
442                                 caseFile,
443                                 fieldObject,
444                                 dataMask,
445                                 true
446                             );
447                         }
449                         ensightSprayField<vector>
450                         (
451                             fieldObject,
452                             dataDir,
453                             subDir,
454                             format
455                         );
457                     }
458                     else if (sprayFieldTypes[i] == tensorIOField::typeName)
459                     {
460                         if (nSprayTime == 1)
461                         {
462                             ensightCaseEntry<tensor>
463                             (
464                                 caseFile,
465                                 fieldObject,
466                                 dataMask,
467                                 true
468                             );
469                         }
471                         ensightSprayField<tensor>
472                         (
473                             fieldObject,
474                             dataDir,
475                             subDir,
476                             format
477                         );
479                     }
480                 }
481             }
482             Info<< endl;
483         }
484     }
486     fieldFileNumbers.setSize(nFieldTime);
487     sprayFileNumbers.setSize(nSprayTime);
489     // add time values
490     caseFile << nl << "TIME" << nl;
491 #   include "ensightCaseTimes.H"
493     Info<< "\nEnd\n"<< endl;
495     return 0;
499 // ************************************************************************* //