Forward compatibility: flex
[foam-extend-3.2.git] / applications / utilities / preProcessing / fluentDataToFoam / fluentDataToFoam.L
blob0ae99ef0d6fe271e146e662aeb0b0f34855a9068
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Application
25     fluentDataToFoam
27 Description
28     Converts Fluent data to FOAM format
30 \*---------------------------------------------------------------------------*/
34 #undef yyFlexLexer
36 /* ------------------------------------------------------------------------- *\
37    ------ local definitions
38 \* ------------------------------------------------------------------------- */
40 #include "fvCFD.H"
41 #include <fstream>
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 using namespace Foam;
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 label dimensionOfGrid = 0;
52 // Machine config
53 label mc1 = 0;
54 label mc2 = 0;
55 label mc3 = 0;
56 label mc4 = 0;
57 label mc5 = 0;
58 label mc6 = 0;
59 label mc7 = 0;
60 label mc8 = 0;
61 label mc9 = 0;
62 label mc10 = 0;
63 label mc11 = 0;
65 label nPoints = 0;
66 label nFaces = 0;
67 label nCells = 0;
69 SLList<label> fieldID;
70 SLList<label> zoneID;
71 SLList<label> nEntriesPerObject;
72 SLList<label> firstID;
73 SLList<label> lastID;
75 SLPtrList<FieldField<Field, scalar> > zoneData;
77 // Dummy yywrap to keep yylex happy at compile time.
78 // It is called by yylex but is not used as the mechanism to change file.
79 // See <<EOF>>
80 #if YY_FLEX_MINOR_VERSION < 6 && YY_FLEX_SUBMINOR_VERSION < 34
81 extern "C" int yywrap()
82 #else
83 int yyFlexLexer::yywrap()
84 #endif
86     return 1;
91 one_space                  [ \t\f]
92 space                      {one_space}*
93 some_space                 {one_space}+
94 cspace                     ","{space}
95 spaceNl                    ({space}|\n|\r)*
97 alpha                      [_[:alpha:]]
98 digit                      [[:digit:]]
99 decDigit                   [[:digit:]]
100 octalDigit                 [0-7]
101 hexDigit                   [[:xdigit:]]
103 lbrac                      "("
104 rbrac                      ")"
105 quote                      \"
106 dash                       "-"
107 comma                      ","
108 dotColonDash               [.:-]
110 schemeSpecialInitial       [!$%&*/:<=>?~_^#.]
111 schemeSpecialSubsequent    [.+-]
112 schemeSymbol               (({some_space}|{alpha}|{quote}|{schemeSpecialInitial})({alpha}|{quote}|{digit}|{schemeSpecialInitial}|{schemeSpecialSubsequent})*)
115 identifier                 {alpha}({alpha}|{digit})*
116 integer                    {decDigit}+
117 label                      [1-9]{decDigit}*
118 hexLabel                   {hexDigit}+
119 zeroLabel                  {digit}*
121 word                       ({alpha}|{digit}|{dotColonDash})*
123 exponent_part              [eE][-+]?{digit}+
124 fractional_constant        [-+]?(({digit}*"."{digit}+)|({digit}+".")|({digit}))
126 double                     ((({fractional_constant}{exponent_part}?)|({digit}+{exponent_part}))|0)
128 x                          {double}
129 y                          {double}
130 z                          {double}
131 scalar                     {double}
132 labelListElement           {space}{zeroLabel}
133 hexLabelListElement        {space}{hexLabel}
134 scalarListElement          {space}{double}
135 schemeSymbolListElement    {space}{schemeSymbol}
136 labelList                  ({labelListElement}+{space})
137 hexLabelList               ({hexLabelListElement}+{space})
138 scalarList                 ({scalarListElement}+{space})
139 schemeSymbolList           ({schemeSymbolListElement}+{space})
141 starStar                   ("**")
142 text                       ({space}({comma}*{word}*{space})*)
144 dateDDMMYYYY               ({digit}{digit}"/"{digit}{digit}"/"{digit}{digit}{digit}{digit})
145 dateDDMonYYYY              ((({digit}{digit}{space})|({digit}{space})){alpha}*{space}{digit}{digit}{digit}{digit})
146 time                       ({digit}{digit}":"{digit}{digit}":"{digit}{digit})
148 versionNumber              ({digit}|".")*
150 comment                    {spaceNl}"(0"{space}
151 header                     {spaceNl}"(1"{space}
152 dimension                  {spaceNl}"(2"{space}
153 machineConfig              {spaceNl}"(4"{space}
154 gridSize                   {spaceNl}"(33"{space}
155 variables                  {spaceNl}"(37"{space}
156 fieldData                  {spaceNl}"(300"{space}
158 endOfSection               {space}")"{space}
162  /* ------------------------------------------------------------------------- *\
163                       -----  Exclusive start states -----
164  \* ------------------------------------------------------------------------- */
166 %option stack
168 %x readComment
169 %x embeddedCommentState
170 %x readHeader
171 %x readDimension
172 %x readMachineConfigHeader
173 %x readMachineConfig
174 %x readGridSizeHeader
175 %x readGridSize
177 %x readFieldData
178 %x readFieldDataHeader
179 %x readFieldSize
180 %x readField
182 %x unknownBlock
183 %x embeddedUnknownBlock
187     // Data index
188     label curNEntriesPerObject = 0;
190     label curFirstID = 0;
191     label curLastID = 0;
192     label objI = 0;
194     FieldField<Field, scalar>* curZdPtr = NULL;
198  /* ------------------------------------------------------------------------ *\
199                             ------ Start Lexing ------
200  \* ------------------------------------------------------------------------ */
202  /*                     ------ Reading control header ------                 */
204 {comment} {
205         yy_push_state(readComment);
206     }
209 <readComment>{quote}{text}{quote} {
210     }
213 <readComment>{spaceNl}{endOfSection} {
214         yy_pop_state();
215     }
217 {header} {
218         BEGIN(readHeader);
219     }
221 <readHeader>{quote}{text}{quote} {
222         Info<< "Reading header: " << YYText() << endl;
223     }
226 {dimension} {
227         BEGIN(readDimension);
228     }
230 <readDimension>{space}{label}{space} {
231         IStringStream dimOfGridStream(YYText());
233         dimensionOfGrid = readLabel(dimOfGridStream);
235         Info<< "Dimension of grid: " << dimensionOfGrid << endl;
236     }
239 {machineConfig} {
240         yy_push_state(readMachineConfigHeader);
241     }
243 <readMachineConfigHeader>{spaceNl}{lbrac} {
244         yy_push_state(readMachineConfig);
245     }
247 <readMachineConfig>{space}{labelList} {
248         IStringStream machineConfigStream(YYText());
250         mc1 = readLabel(machineConfigStream);
251         mc2 = readLabel(machineConfigStream);
252         mc3 = readLabel(machineConfigStream);
253         mc4 = readLabel(machineConfigStream);
254         mc5 = readLabel(machineConfigStream);
255         mc6 = readLabel(machineConfigStream);
256         mc7 = readLabel(machineConfigStream);
257         mc8 = readLabel(machineConfigStream);
258         mc9 = readLabel(machineConfigStream);
259         mc10 = readLabel(machineConfigStream);
260         mc11 = readLabel(machineConfigStream);
262         Info<< "Machine config: "
263             << mc1 << mc2 << mc3 << mc4 << mc5 << mc6
264             << mc7 << mc8 << mc9 << mc10 << mc11
265             << endl;
266     }
269 {gridSize} {
270         yy_push_state(readGridSizeHeader);
271     }
274 <readGridSizeHeader>{spaceNl}{lbrac} {
275         yy_push_state(readGridSize);
276     }
279 <readGridSize>{space}{labelList} {
280         IStringStream gridSizeStream(YYText());
282         nCells = readLabel(gridSizeStream);
283         nFaces = readLabel(gridSizeStream);
284         nPoints = readLabel(gridSizeStream);
286         Info<< "Grid size: nCells = " << nCells << " nFaces = " << nFaces
287             << " nPoints = " << nPoints << endl;
288     }
291 {variables} {
292         yy_push_state(readComment);
293     }
296 {fieldData} {
297         yy_push_state(readFieldDataHeader);
298     }
301 <readFieldDataHeader>{spaceNl}{lbrac} {
302         BEGIN(readFieldSize);
303     }
306 <readFieldSize>{space}{labelList} {
307         IStringStream fieldDataStream(YYText());
309         // Field and zone ID
310         fieldID.append(readLabel(fieldDataStream));
311         zoneID.append(readLabel(fieldDataStream));
313         // Number of entries per object (cell/face)
314         curNEntriesPerObject = readLabel(fieldDataStream);
315         nEntriesPerObject.append(curNEntriesPerObject);
317         // Dummy, not used
318         //nTimeLevels =
319         readLabel(fieldDataStream);
320         //nPhases =
321         readLabel(fieldDataStream);
323         // Start and end of list
324         curFirstID = readLabel(fieldDataStream);
325         firstID.append(curFirstID);
326         curLastID = readLabel(fieldDataStream);
327         lastID.append(curLastID);
329         // Create field for data
330 //         Info << "Create : " << curNEntriesPerObject << endl;
331         curZdPtr = new FieldField<Field, scalar>(curNEntriesPerObject);
333 //         Info<< "Setting field with " << curNEntriesPerObject
334 //             << " components of length " << curLastID - curFirstID +1 << endl;
336         for (label cmptI = 0; cmptI < curNEntriesPerObject; cmptI++)
337         {
338             curZdPtr->set(cmptI, new scalarField(curLastID - curFirstID + 1));
339         }
341         zoneData.append(curZdPtr);
342     }
345 <readFieldSize>{endOfSection} {
346         BEGIN(readFieldData);
347     }
350 <readFieldData>{spaceNl}{lbrac} {
351 //         Info<< "Reading field. nEntries = " << curNEntriesPerObject
352 //             << " firstID = " << curFirstID << " lastID = " << curLastID << endl;
354         // Reset the counter
355         objI = 0;
357         yy_push_state(readField);
358     }
361 <readField>{spaceNl}{scalarList} {
363         IStringStream fieldStream(YYText());
365         FieldField<Field, scalar>& zd = *curZdPtr;
367         // Reading field
368         for (label entryI = 0; entryI < curNEntriesPerObject; entryI++)
369         {
370             zd[entryI][objI] = readScalar(fieldStream);
371         }
372         objI++;
373     }
376 <readMachineConfig,readMachineConfigHeader,readGridSize,readGridSizeHeader,readFieldData,readField>{spaceNl}{endOfSection} {
378         yy_pop_state();
379     }
381  /*             ------ Reading end of section and others ------               */
383 <readHeader,readDimension>{spaceNl}{endOfSection} {
384         BEGIN(INITIAL);
385     }
387  /*    ------ Reading unknown type or non-standard comment ------             */
390 {lbrac}{label} {
391 //         Info<< "Found unknown block:" << YYText() << endl;
392         yy_push_state(unknownBlock);
393     }
395 <readComment,unknownBlock,embeddedUnknownBlock>{spaceNl}{schemeSymbol} {
396     }
398 <readComment,unknownBlock,embeddedUnknownBlock>{spaceNl}{lbrac} {
399 //         Info<< "Embedded blocks in comment or unknown:" << YYText() << endl;
400         yy_push_state(embeddedUnknownBlock);
402     }
404 <readComment,unknownBlock,embeddedUnknownBlock>{spaceNl}{endOfSection} {
405 //         Info<< "Found end of section in unknown:" << YYText() << endl;
406         yy_pop_state();
407     }
409 <unknownBlock,embeddedUnknownBlock>{spaceNl}{labelList} {
410     }
412 <unknownBlock,embeddedUnknownBlock>{spaceNl}{hexLabelList} {
413     }
415 <unknownBlock,embeddedUnknownBlock>{spaceNl}{scalarList} {
416     }
418 <unknownBlock,embeddedUnknownBlock>{spaceNl}{schemeSymbolList} {
419     }
421 <unknownBlock,embeddedUnknownBlock>{spaceNl}{text} {
422     }
425  /* ------ Ignore remaining space and \n s.  Any other characters are errors. */
427  /*  ------ On EOF return to previous file, if none exists terminate. ------  */
429 <<EOF>> {
430             yyterminate();
431     }
435 #include "fileName.H"
436 #include "fluentDataConverter.H"
438 int main(int argc, char *argv[])
440     argList::noParallel();
441     argList::validArgs.append("Fluent data file");
443     argList args(argc, argv);
445     if (!args.check())
446     {
447         FatalError.exit();
448     }
450 #   include "createTime.H"
452     fileName fluentFile(args.additionalArgs()[0]);
453     std::ifstream fluentStream(fluentFile.c_str());
455     if (!fluentStream)
456     {
457         FatalErrorIn("fluentToFoam::main(int argc, char *argv[])")
458             << args.executable()
459             << ": file " << fluentFile << " not found"
460             << exit(FatalError);
461     }
463     yyFlexLexer lexer(&fluentStream);
464     while(lexer.yylex() != 0)
465     {}
467     Info<< "\n\nFINISHED LEXING\n\n\n";
469     // Re-package the fields into foam data
471     // Warning: syncronous iterators, check size
472     // HJ, 25/May/2009
473     const label iterSize = fieldID.size();
474     Info << "Number of entries read: " << iterSize << nl << endl;
476     if
477     (
478         zoneID.size() != iterSize
479      || nEntriesPerObject.size() != iterSize
480      || firstID.size() != iterSize
481      || lastID.size() != iterSize
482      || zoneData.size() != iterSize
483     )
484     {
485         FatalErrorIn(args.executable())
486             << "Problem in reading: incorrect iterator size: " << iterSize
487             << abort(FatalError);
488     }
490     labelHashSet fields;
492     for
493     (
494         SLList<label>::const_iterator fieldIDIter = fieldID.begin();
495         fieldIDIter != fieldID.end();
496         ++fieldIDIter
497     )
498     {
499         fields.insert(fieldIDIter());
500     }
502     // Info: Available units
503     {
504         labelList u = fields.toc();
505         sort(u);
506         Info<< "Available units: " << u << endl;
507     }
509     // Create a mesh
510 #   include "createMesh.H"
512     // Create a converter
513     fluentDataConverter fdc
514     (
515         mesh,
516         fieldID,
517         zoneID,
518         firstID,
519         lastID,
520         zoneData
521     );
523     if (fields.found(1))
524     {
525         Info << "Converting pressure field" << endl;
526         fdc.convertField
527         (
528             "p",   // field name
529             1,     // fluent Unit number
530             dimensionedScalar("zero", dimPressure, 0)
531         )().write();
532     }
534     if (fields.found(2))
535     {
536         Info << "Converting momentum field" << endl;
537         fdc.convertField
538         (
539             "momentum",   // field name
540             2,            // fluent Unit number
541             dimensionedScalar("zero", dimDensity*dimVelocity, 0)
542         )().write();
543     }
545     if (fields.found(3))
546     {
547         Info << "Converting temperature field" << endl;
548         fdc.convertField
549         (
550             "T",   // field name
551             3,     // fluent Unit number
552             dimensionedScalar("zero", dimTemperature, 273.15)
553         )().write();
554     }
556     if (fields.found(4))
557     {
558         Info << "Converting enthalpy field" << endl;
559         fdc.convertField
560         (
561             "h",   // field name
562             4,     // fluent Unit number
563             dimensionedScalar("zero", dimensionSet(0, 2, -2, 0, 0, 0, 0), 0)
564         )().write();
565     }
567     if (fields.found(5))
568     {
569         Info << "Converting turbulence kinetic energy field" << endl;
570         fdc.convertField
571         (
572             "k",   // field name
573             5,     // fluent Unit number
574             dimensionedScalar("zero", sqr(dimVelocity), 0)
575         )().write();
576     }
578     // Species not done yet.  HJ, 29/May/2009
580     if (fields.found(8))
581     {
582         Info << "Converting G field" << endl;
583         fdc.convertField
584         (
585             "G",   // field name
586             8,           // fluent Unit number
587             dimensionedScalar("zero", dimless, 0)
588         )().write();
589     }
591     if (fields.found(15))
592     {
593         Info << "Converting body force field" << endl;
594         fdc.convertField
595         (
596             "bodyForce",   // field name
597             15,            // fluent Unit number
598             dimensionedScalar("zero", dimAcceleration, 0)
599         )().write();
600     }
602     if (fields.found(101))
603     {
604         Info << "Converting density field" << endl;
605         fdc.convertField
606         (
607             "rho",   // field name
608             101,            // fluent Unit number
609             dimensionedScalar("zero", dimDensity, 0)
610         )().write();
611     }
613     if (fields.found(102))
614     {
615         Info << "Converting laminar viscosity field" << endl;
616         fdc.convertField
617         (
618             "muLam",   // field name
619             102,       // fluent Unit number
620             dimensionedScalar("zero", dimPressure*dimTime, 0)
621         )().write();
622     }
624     if (fields.found(103))
625     {
626         Info << "Converting turbulent field" << endl;
627         fdc.convertField
628         (
629             "muT",   // field name
630             103,     // fluent Unit number
631             dimensionedScalar("zero", dimPressure*dimTime, 0)
632         )().write();
633     }
635     if (fields.found(104))
636     {
637         Info << "Converting  field" << endl;
638         fdc.convertField
639         (
640             "Cp",   // field name
641             104,    // fluent Unit number
642             dimensionedScalar("zero", dimSpecificHeatCapacity, 0)
643         )().write();
644     }
646     // Velocity field
647     if (fields.found(111) || fields.found(112) || fields.found(113))
648     {
649         Info << "Converting velocity field" << endl;
650         volVectorField U
651         (
652             IOobject
653             (
654                 "U",
655                 mesh.time().timeName(),
656                 mesh,
657                 IOobject::NO_READ,
658                 IOobject::NO_WRITE
659             ),
660             mesh,
661             dimensionedVector("zero", dimVelocity, vector::zero)
662         );
664         if (fields.found(111))
665         {
666             U.replace
667             (
668                 vector::X,
669                 fdc.convertField
670                 (
671                     "Ux",   // field name
672                     111,           // fluent Unit number
673                     dimensionedScalar("zero", dimVelocity, 0)
674                 )
675             );
676         }
678         if (fields.found(112))
679         {
680             U.replace
681             (
682                 vector::Y,
683                 fdc.convertField
684                 (
685                     "Uy",   // field name
686                     112,    // fluent Unit number
687                     dimensionedScalar("zero", dimVelocity, 0)
688                 )
689             );
690         }
692         if (fields.found(113))
693         {
694             U.replace
695             (
696                 vector::Z,
697                 fdc.convertField
698                 (
699                     "Uz",   // field name
700                     113,    // fluent Unit number
701                     dimensionedScalar("zero", dimVelocity, 0)
702                 )
703             );
704         }
706         U.write();
707     }
710     Info<< nl << "End" << endl;
711     return 0;
715  /* ------------------------------------------------------------------------- *\
716     ------ End of fluentDataToFoam.L
717  \* ------------------------------------------------------------------------- */