fixed writing out entries in advective bc
[OpenFOAM-1.6-ext.git] / applications / utilities / preProcessing / fluentDataToFoam / fluentDataToFoam.L
blobfd112a601cb83fc396b1e42e5e334867082a6917
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
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     fluentDataToFoam
28 Description
29     Converts Fluent data to OpenFOAM format
31 \*---------------------------------------------------------------------------*/
35 #undef yyFlexLexer
37 /* ------------------------------------------------------------------------- *\
38    ------ local definitions
39 \* ------------------------------------------------------------------------- */
41 #include "fvCFD.H"
42 #include <fstream>
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 using namespace Foam;
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 label dimensionOfGrid = 0;
53 // Machine config
54 label mc1 = 0;
55 label mc2 = 0;
56 label mc3 = 0;
57 label mc4 = 0;
58 label mc5 = 0;
59 label mc6 = 0;
60 label mc7 = 0;
61 label mc8 = 0;
62 label mc9 = 0;
63 label mc10 = 0;
64 label mc11 = 0;
66 label nPoints = 0;
67 label nFaces = 0;
68 label nCells = 0;
70 SLList<label> fieldID;
71 SLList<label> zoneID;
72 SLList<label> nEntriesPerObject;
73 SLList<label> firstID;
74 SLList<label> lastID;
76 SLPtrList<FieldField<Field, scalar> > zoneData;
78 // Dummy yywrap to keep yylex happy at compile time.
79 // It is called by yylex but is not used as the mechanism to change file.
80 // See <<EOF>>
81 #if YY_FLEX_SUBMINOR_VERSION < 34
82 extern "C" int yywrap()
83 #else
84 int yyFlexLexer::yywrap()
85 #endif
87     return 1;
92 one_space                  [ \t\f]
93 space                      {one_space}*
94 some_space                 {one_space}+
95 cspace                     ","{space}
96 spaceNl                    ({space}|\n|\r)*
98 alpha                      [_[:alpha:]]
99 digit                      [[:digit:]]
100 decDigit                   [[:digit:]]
101 octalDigit                 [0-7]
102 hexDigit                   [[:xdigit:]]
104 lbrac                      "("
105 rbrac                      ")"
106 quote                      \"
107 dash                       "-"
108 comma                      ","
109 dotColonDash               [.:-]
111 schemeSpecialInitial       [!$%&*/:<=>?~_^#.]
112 schemeSpecialSubsequent    [.+-]
113 schemeSymbol               (({some_space}|{alpha}|{quote}|{schemeSpecialInitial})({alpha}|{quote}|{digit}|{schemeSpecialInitial}|{schemeSpecialSubsequent})*)
116 identifier                 {alpha}({alpha}|{digit})*
117 integer                    {decDigit}+
118 label                      [1-9]{decDigit}*
119 hexLabel                   {hexDigit}+
120 zeroLabel                  {digit}*
122 word                       ({alpha}|{digit}|{dotColonDash})*
124 exponent_part              [eE][-+]?{digit}+
125 fractional_constant        [-+]?(({digit}*"."{digit}+)|({digit}+".")|({digit}))
127 double                     ((({fractional_constant}{exponent_part}?)|({digit}+{exponent_part}))|0)
129 x                          {double}
130 y                          {double}
131 z                          {double}
132 scalar                     {double}
133 labelListElement           {space}{zeroLabel}
134 hexLabelListElement        {space}{hexLabel}
135 scalarListElement          {space}{double}
136 schemeSymbolListElement    {space}{schemeSymbol}
137 labelList                  ({labelListElement}+{space})
138 hexLabelList               ({hexLabelListElement}+{space})
139 scalarList                 ({scalarListElement}+{space})
140 schemeSymbolList           ({schemeSymbolListElement}+{space})
142 starStar                   ("**")
143 text                       ({space}({comma}*{word}*{space})*)
145 dateDDMMYYYY               ({digit}{digit}"/"{digit}{digit}"/"{digit}{digit}{digit}{digit})
146 dateDDMonYYYY              ((({digit}{digit}{space})|({digit}{space})){alpha}*{space}{digit}{digit}{digit}{digit})
147 time                       ({digit}{digit}":"{digit}{digit}":"{digit}{digit})
149 versionNumber              ({digit}|".")*
151 comment                    {spaceNl}"(0"{space}
152 header                     {spaceNl}"(1"{space}
153 dimension                  {spaceNl}"(2"{space}
154 machineConfig              {spaceNl}"(4"{space}
155 gridSize                   {spaceNl}"(33"{space}
156 variables                  {spaceNl}"(37"{space}
157 fieldData                  {spaceNl}"(300"{space}
159 endOfSection               {space}")"{space}
163  /* ------------------------------------------------------------------------- *\
164                       -----  Exclusive start states -----
165  \* ------------------------------------------------------------------------- */
167 %option stack
169 %x readComment
170 %x embeddedCommentState
171 %x readHeader
172 %x readDimension
173 %x readMachineConfigHeader
174 %x readMachineConfig
175 %x readGridSizeHeader
176 %x readGridSize
178 %x readFieldData
179 %x readFieldDataHeader
180 %x readFieldSize
181 %x readField
183 %x unknownBlock
184 %x embeddedUnknownBlock
188     // Data index
189     label curNEntriesPerObject = 0;
191     // Dummy, not used
192     label nTimeLevels;
193     label nPhases;
195     label curFirstID = 0;
196     label curLastID = 0;
197     label objI = 0;
199     FieldField<Field, scalar>* curZdPtr = NULL;
203  /* ------------------------------------------------------------------------- *\
204                             ------ Start Lexing ------
205  \* ------------------------------------------------------------------------- */
207  /*                      ------ Reading control header ------                 */
209 {comment} {
210         yy_push_state(readComment);
211     }
214 <readComment>{quote}{text}{quote} {
215     }
218 <readComment>{spaceNl}{endOfSection} {
219         yy_pop_state();
220     }
222 {header} {
223         BEGIN(readHeader);
224     }
226 <readHeader>{quote}{text}{quote} {
227         Info<< "Reading header: " << YYText() << endl;
228     }
231 {dimension} {
232         BEGIN(readDimension);
233     }
235 <readDimension>{space}{label}{space} {
236         IStringStream dimOfGridStream(YYText());
238         dimensionOfGrid = readLabel(dimOfGridStream);
240         Info<< "Dimension of grid: " << dimensionOfGrid << endl;
241     }
244 {machineConfig} {
245         yy_push_state(readMachineConfigHeader);
246     }
248 <readMachineConfigHeader>{spaceNl}{lbrac} {
249         yy_push_state(readMachineConfig);
250     }
252 <readMachineConfig>{space}{labelList} {
253         IStringStream machineConfigStream(YYText());
255         mc1 = readLabel(machineConfigStream);
256         mc2 = readLabel(machineConfigStream);
257         mc3 = readLabel(machineConfigStream);
258         mc4 = readLabel(machineConfigStream);
259         mc5 = readLabel(machineConfigStream);
260         mc6 = readLabel(machineConfigStream);
261         mc7 = readLabel(machineConfigStream);
262         mc8 = readLabel(machineConfigStream);
263         mc9 = readLabel(machineConfigStream);
264         mc10 = readLabel(machineConfigStream);
265         mc11 = readLabel(machineConfigStream);
267         Info<< "Machine config: "
268             << mc1 << mc2 << mc3 << mc4 << mc5 << mc6
269             << mc7 << mc8 << mc9 << mc10 << mc11
270             << endl;
271     }
274 {gridSize} {
275         yy_push_state(readGridSizeHeader);
276     }
279 <readGridSizeHeader>{spaceNl}{lbrac} {
280         yy_push_state(readGridSize);
281     }
284 <readGridSize>{space}{labelList} {
285         IStringStream gridSizeStream(YYText());
287         nCells = readLabel(gridSizeStream);
288         nFaces = readLabel(gridSizeStream);
289         nPoints = readLabel(gridSizeStream);
291         Info<< "Grid size: nCells = " << nCells << " nFaces = " << nFaces
292             << " nPoints = " << nPoints << endl;
293     }
296 {variables} {
297         yy_push_state(readComment);
298     }
301 {fieldData} {
302         yy_push_state(readFieldDataHeader);
303     }
306 <readFieldDataHeader>{spaceNl}{lbrac} {
307         BEGIN(readFieldSize);
308     }
311 <readFieldSize>{space}{labelList} {
312         IStringStream fieldDataStream(YYText());
314         // Field and zone ID
315         fieldID.append(readLabel(fieldDataStream));
316         zoneID.append(readLabel(fieldDataStream));
318         // Number of entries per object (cell/face)
319         curNEntriesPerObject = readLabel(fieldDataStream);
320         nEntriesPerObject.append(curNEntriesPerObject);
322         // Dummy, not used
323         nTimeLevels = readLabel(fieldDataStream);
324         nPhases = readLabel(fieldDataStream);
326         // Start and end of list
327         curFirstID = readLabel(fieldDataStream);
328         firstID.append(curFirstID);
329         curLastID = readLabel(fieldDataStream);
330         lastID.append(curLastID);
332         // Create field for data
333 //         Info << "Create : " << curNEntriesPerObject << endl;
334         curZdPtr = new FieldField<Field, scalar>(curNEntriesPerObject);
336 //         Info<< "Setting field with " << curNEntriesPerObject
337 //             << " components of length " << curLastID - curFirstID +1 << endl;
339         for (label cmptI = 0; cmptI < curNEntriesPerObject; cmptI++)
340         {
341             curZdPtr->set(cmptI, new scalarField(curLastID - curFirstID + 1));
342         }
344         zoneData.append(curZdPtr);
345     }
348 <readFieldSize>{endOfSection} {
349         BEGIN(readFieldData);
350     }
353 <readFieldData>{spaceNl}{lbrac} {
354 //         Info<< "Reading field. nEntries = " << curNEntriesPerObject
355 //             << " firstID = " << curFirstID << " lastID = " << curLastID << endl;
357         // Reset the counter
358         objI = 0;
360         yy_push_state(readField);
361     }
364 <readField>{spaceNl}{scalarList} {
366         IStringStream fieldStream(YYText());
368         FieldField<Field, scalar>& zd = *curZdPtr;
370         // Reading field
371         for (label entryI = 0; entryI < curNEntriesPerObject; entryI++)
372         {
373             zd[entryI][objI] = readScalar(fieldStream);
374         }
375         objI++;
376     }
379 <readMachineConfig,readMachineConfigHeader,readGridSize,readGridSizeHeader,readFieldData,readField>{spaceNl}{endOfSection} {
381         yy_pop_state();
382     }
384  /*             ------ Reading end of section and others ------               */
386 <readHeader,readDimension>{spaceNl}{endOfSection} {
387         BEGIN(INITIAL);
388     }
390  /*    ------ Reading unknown type or non-standard comment ------             */
393 {lbrac}{label} {
394 //         Info<< "Found unknown block:" << YYText() << endl;
395         yy_push_state(unknownBlock);
396     }
398 <readComment,unknownBlock,embeddedUnknownBlock>{spaceNl}{schemeSymbol} {
399     }
401 <readComment,unknownBlock,embeddedUnknownBlock>{spaceNl}{lbrac} {
402 //         Info<< "Embedded blocks in comment or unknown:" << YYText() << endl;
403         yy_push_state(embeddedUnknownBlock);
405     }
407 <readComment,unknownBlock,embeddedUnknownBlock>{spaceNl}{endOfSection} {
408 //         Info<< "Found end of section in unknown:" << YYText() << endl;
409         yy_pop_state();
410     }
412 <unknownBlock,embeddedUnknownBlock>{spaceNl}{labelList} {
413     }
415 <unknownBlock,embeddedUnknownBlock>{spaceNl}{hexLabelList} {
416     }
418 <unknownBlock,embeddedUnknownBlock>{spaceNl}{scalarList} {
419     }
421 <unknownBlock,embeddedUnknownBlock>{spaceNl}{schemeSymbolList} {
422     }
424 <unknownBlock,embeddedUnknownBlock>{spaceNl}{text} {
425     }
428  /* ------ Ignore remaining space and \n s.  Any other characters are errors. */
430  /*  ------ On EOF return to previous file, if none exists terminate. ------  */
432 <<EOF>> {
433             yyterminate();
434     }
438 #include "fileName.H"
439 #include "fluentDataConverter.H"
441 int main(int argc, char *argv[])
443     argList::noParallel();
444     argList::validArgs.append("Fluent data file");
446     argList args(argc, argv);
448     if (!args.check())
449     {
450         FatalError.exit();
451     }
453 #   include "createTime.H"
455     fileName fluentFile(args.additionalArgs()[0]);
456     std::ifstream fluentStream(fluentFile.c_str());
458     if (!fluentStream)
459     {
460         FatalErrorIn("fluentToFoam::main(int argc, char *argv[])")
461             << args.executable()
462             << ": file " << fluentFile << " not found"
463             << exit(FatalError);
464     }
466     yyFlexLexer lexer(&fluentStream);
467     while(lexer.yylex() != 0)
468     {}
470     Info<< "\n\nFINISHED LEXING\n\n\n";
472     // Re-package the fields into OpenFOAM data
474     // Warning: syncronous iterators, check size
475     // HJ, 25/May/2009
476     const label iterSize = fieldID.size();
477     Info << "Number of entries read: " << iterSize << nl << endl;
479     if
480     (
481         zoneID.size() != iterSize
482      || nEntriesPerObject.size() != iterSize
483      || firstID.size() != iterSize
484      || lastID.size() != iterSize
485      || zoneData.size() != iterSize
486     )
487     {
488         FatalErrorIn(args.executable())
489             << "Problem in reading: incorrect iterator size: " << iterSize
490             << abort(FatalError);
491     }
493     labelHashSet fields;
495     for
496     (
497         SLList<label>::const_iterator fieldIDIter = fieldID.begin();
498         fieldIDIter != fieldID.end();
499         ++fieldIDIter
500     )
501     {
502         fields.insert(fieldIDIter());
503     }
505     // Info: Available units
506     {
507         labelList u = fields.toc();
508         sort(u);
509         Info<< "Available units: " << u << endl;
510     }
512     // Create a mesh
513 #   include "createMesh.H"
515     // Create a converter
516     fluentDataConverter fdc
517     (
518         mesh,
519         fieldID,
520         zoneID,
521         firstID,
522         lastID,
523         zoneData
524     );
526     if (fields.found(1))
527     {
528         Info << "Converting pressure field" << endl;
529         fdc.convertField
530         (
531             "p",   // field name
532             1,     // fluent Unit number
533             dimensionedScalar("zero", dimPressure, 0)
534         )().write();
535     }
537     if (fields.found(2))
538     {
539         Info << "Converting momentum field" << endl;
540         fdc.convertField
541         (
542             "momentum",   // field name
543             2,            // fluent Unit number
544             dimensionedScalar("zero", dimDensity*dimVelocity, 0)
545         )().write();
546     }
548     if (fields.found(3))
549     {
550         Info << "Converting temperature field" << endl;
551         fdc.convertField
552         (
553             "T",   // field name
554             3,     // fluent Unit number
555             dimensionedScalar("zero", dimTemperature, 273.15)
556         )().write();
557     }
559     if (fields.found(4))
560     {
561         Info << "Converting enthalpy field" << endl;
562         fdc.convertField
563         (
564             "h",   // field name
565             4,     // fluent Unit number
566             dimensionedScalar("zero", dimensionSet(0, 2, -2, 0, 0, 0, 0), 0)
567         )().write();
568     }
570     if (fields.found(5))
571     {
572         Info << "Converting turbulence kinetic energy field" << endl;
573         fdc.convertField
574         (
575             "k",   // field name
576             5,     // fluent Unit number
577             dimensionedScalar("zero", sqr(dimVelocity), 0)
578         )().write();
579     }
581     // Species not done yet.  HJ, 29/May/2009
583     if (fields.found(8))
584     {
585         Info << "Converting G field" << endl;
586         fdc.convertField
587         (
588             "G",   // field name
589             8,           // fluent Unit number
590             dimensionedScalar("zero", dimless, 0)
591         )().write();
592     }
594     if (fields.found(15))
595     {
596         Info << "Converting body force field" << endl;
597         fdc.convertField
598         (
599             "bodyForce",   // field name
600             15,            // fluent Unit number
601             dimensionedScalar("zero", dimAcceleration, 0)
602         )().write();
603     }
605     if (fields.found(101))
606     {
607         Info << "Converting density field" << endl;
608         fdc.convertField
609         (
610             "rho",   // field name
611             101,            // fluent Unit number
612             dimensionedScalar("zero", dimDensity, 0)
613         )().write();
614     }
616     if (fields.found(102))
617     {
618         Info << "Converting laminar viscosity field" << endl;
619         fdc.convertField
620         (
621             "muLam",   // field name
622             102,       // fluent Unit number
623             dimensionedScalar("zero", dimPressure*dimTime, 0)
624         )().write();
625     }
627     if (fields.found(103))
628     {
629         Info << "Converting turbulent field" << endl;
630         fdc.convertField
631         (
632             "muT",   // field name
633             103,     // fluent Unit number
634             dimensionedScalar("zero", dimPressure*dimTime, 0)
635         )().write();
636     }
638     if (fields.found(104))
639     {
640         Info << "Converting  field" << endl;
641         fdc.convertField
642         (
643             "Cp",   // field name
644             104,    // fluent Unit number
645             dimensionedScalar("zero", dimSpecificHeatCapacity, 0)
646         )().write();
647     }
649     // Velocity field
650     if (fields.found(111) || fields.found(112) || fields.found(113))
651     {
652         Info << "Converting velocity field" << endl;
653         volVectorField U
654         (
655             IOobject
656             (
657                 "U",
658                 mesh.time().timeName(),
659                 mesh,
660                 IOobject::NO_READ,
661                 IOobject::NO_WRITE
662             ),
663             mesh,
664             dimensionedVector("zero", dimVelocity, vector::zero)
665         );
667         if (fields.found(111))
668         {
669             U.replace
670             (
671                 vector::X, 
672                 fdc.convertField
673                 (
674                     "Ux",   // field name
675                     111,           // fluent Unit number
676                     dimensionedScalar("zero", dimVelocity, 0)
677                 )
678             );
679         }
681         if (fields.found(112))
682         {
683             U.replace
684             (
685                 vector::Y, 
686                 fdc.convertField
687                 (
688                     "Uy",   // field name
689                     112,    // fluent Unit number
690                     dimensionedScalar("zero", dimVelocity, 0)
691                 )
692             );
693         }
695         if (fields.found(113))
696         {
697             U.replace
698             (
699                 vector::Z, 
700                 fdc.convertField
701                 (
702                     "Uz",   // field name
703                     113,    // fluent Unit number
704                     dimensionedScalar("zero", dimVelocity, 0)
705                 )
706             );
707         }
709         U.write();
710     }
713     Info<< nl << "End" << endl;
714     return 0;
718  /* ------------------------------------------------------------------------- *\
719     ------ End of fluentDataToFoam.L
720  \* ------------------------------------------------------------------------- */