fluent3DMeshToFoam.L 39.8 KB
Newer Older
1
/*--------------------------------*- C++ -*----------------------------------*\
2
3
4
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
6
7
8
9
10
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
15
16
17
18
19
20
21

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

Application
    fluent3DMeshToFoam

Description
    Converts a Fluent mesh to OpenFOAM format.

\*---------------------------------------------------------------------------*/

%{
#undef yyFlexLexer

 /* ------------------------------------------------------------------------ *\
   ------ local definitions
 \* ------------------------------------------------------------------------ */

39
#include "cyclicPolyPatch.H"
40
41
42
43
44
45
46
#include "argList.H"
#include "Time.H"
#include "polyMesh.H"
#include "polyTopoChange.H"
#include "polyMeshZipUpCells.H"
#include "wallPolyPatch.H"
#include "symmetryPolyPatch.H"
47
#include "oldCyclicPolyPatch.H"
48
49
#include "Swap.H"
#include "IFstream.H"
mattijs's avatar
mattijs committed
50
#include "readHexLabel.H"
51
52
53
54
55
56
57

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

using namespace Foam;

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

58
59
60
61
// Line number
label lineNo = 1;

// Scale factor used to scale points (optional command line argument)
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
scalar scaleFactor = 1.0;

label dimensionOfGrid = 0;
label nPoints = 0;
label nFaces = 0;
label nCells = 0;

bool hangingNodes = false;

pointField points(0);
faceList faces(0);
labelList owner(0);
labelList neighbour(0);

// Group type and name
Map<word> groupType(100);
Map<word> groupName(100);

// Point groups
DynamicList<label> pointGroupZoneID;
DynamicList<label> pointGroupStartIndex;
DynamicList<label> pointGroupEndIndex;

// Face groups
DynamicList<label> faceGroupZoneID;
DynamicList<label> faceGroupStartIndex;
DynamicList<label> faceGroupEndIndex;

// Cell groups
DynamicList<label> cellGroupZoneID;
DynamicList<label> cellGroupStartIndex;
DynamicList<label> cellGroupEndIndex;
DynamicList<label> cellGroupType;

mattijs's avatar
mattijs committed
96
97
98
// Special parsing of (incorrect) Cubit files
bool cubitFile = false;

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

void uniquify(word& name, HashSet<word>& patchNames)
{
    if (!patchNames.found(name))
    {
        patchNames.insert(name);
    }
    else
    {
        Info<< "    name " << name << " already used";

        label i = 1;
        word baseName = name;

        do
        {
            name = baseName + "_" + Foam::name(i++);
        } while (patchNames.found(name));

        Info<< ", changing to " << name << endl;
    }
}


// Dummy yywrap to keep yylex happy at compile time.
// It is called by yylex but is not used as the mechanism to change file.
// See <<EOF>>
126
#if YY_FLEX_SUBMINOR_VERSION < 34
127
extern "C" int yywrap()
128
129
130
#else
int yyFlexLexer::yywrap()
#endif
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
{
    return 1;
}

%}

one_space                  [ \t\f]
space                      {one_space}*
some_space                 {one_space}+
cspace                     ","{space}

alpha                      [_[:alpha:]]
digit                      [[:digit:]]
decDigit                   [[:digit:]]
octalDigit                 [0-7]
hexDigit                   [[:xdigit:]]

lbrac                      "("
rbrac                      ")"
quote                      \"
dash                       "-"
dotColonDash               [.:-]

154
schemeSpecialInitial       [!$%&*/\\:<=>?~_^#.@']
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
schemeSpecialSubsequent    [.+-]
schemeSymbol               (({some_space}|{alpha}|{quote}|{schemeSpecialInitial})({alpha}|{quote}|{digit}|{schemeSpecialInitial}|{schemeSpecialSubsequent})*)


identifier                 {alpha}({alpha}|{digit})*
integer                    {decDigit}+
label                      [1-9]{decDigit}*
hexLabel                   {hexDigit}+
zeroLabel                  {digit}*
signedInteger              [-+]?{integer}
word                       ({alpha}|{digit}|{dotColonDash})*

exponent_part              [eE][-+]?{digit}+
fractional_constant        [-+]?(({digit}*"."{digit}+)|({digit}+".")|({digit}))

double                     ((({fractional_constant}{exponent_part}?)|({digit}+{exponent_part}))|0)

x                          {double}
y                          {double}
z                          {double}
scalar                     {double}
labelListElement           {space}{zeroLabel}
hexLabelListElement        {space}{hexLabel}
scalarListElement          {space}{double}
schemeSymbolListElement    {space}{schemeSymbol}
labelList                  ({labelListElement}+{space})
hexLabelList               ({hexLabelListElement}+{space})
scalarList                 ({scalarListElement}+{space})
schemeSymbolList           ({schemeSymbolListElement}+{space})

starStar                   ("**")
text                       ({space}({word}*{space})*)
anythingInBlock            ([^)]*)

dateDDMMYYYY               ({digit}{digit}"/"{digit}{digit}"/"{digit}{digit}{digit}{digit})
dateDDMonYYYY              ((({digit}{digit}{space})|({digit}{space})){alpha}*{space}{digit}{digit}{digit}{digit})
time                       ({digit}{digit}":"{digit}{digit}":"{digit}{digit})

versionNumber              ({digit}|".")*

195
196
197
198
199
200
201
202
header                     {space}"(1"{space}
dimension                  {space}"(2"{space}
points                     {space}"(10"{space}
faces                      {space}"(13"{space}
cells                      {space}"(12"{space}
zoneVariant1               {space}"(39"{space}
zoneVariant2               {space}"(45"{space}
faceTree                   {space}"(59"{space}
mattijs's avatar
mattijs committed
203
204
205
206
207
208

comment                    "0"{space}
unknownPeriodicFace        "17"{space}
periodicFace               "18"{space}
cellTree                   "58"{space}
faceParents                "61"{space}
209
ignoreBlocks               ("4"|"37"|"38"|"40"|"41"|"60"|"64"){space}
210

211
redundantBlock             {space}({comment}|{unknownPeriodicFace}|{periodicFace}|{cellTree}|{faceParents}|{ignoreBlocks}){space}
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297

endOfSection               {space}")"{space}



 /* ------------------------------------------------------------------------ *\
                      -----  Exclusive start states -----
 \* ------------------------------------------------------------------------ */

%option stack

%x readHeader
%x readDimension
%x readPoint
%x readPointHeader
%x readNumberOfPoints
%x readPointGroupData
%x readPointData
%x readScalarList
%x fluentFace
%x readFaceHeader
%x readNumberOfFaces
%x readFaceGroupData
%x readFaceData
%x readFacesMixed
%x readFacesUniform
%x cell
%x readCellHeader
%x readNumberOfCells
%x readCellGroupData
%x readCellData
%x readCellsUniform
%x zone
%x readZoneHeader
%x readZoneGroupData
%x readZoneData
%x readZoneBlock

%x ignoreBlock
%x ignoreEmbeddedBlock
%%

%{
    // End of read character pointer returned by strtol and strtod
    char* endPtr;

    // Point data
    label pointGroupNumberOfComponents = 3;
    label pointi = 0; // index used for reading points
    label cmpt = 0;   // component index used for reading points

    // Face data
    label faceGroupElementType = -1;
    label facei = 0;  // index used for reading faces
%}


 /* ------------------------------------------------------------------------ *\
                            ------ Start Lexing ------
 \* ------------------------------------------------------------------------ */

 /*                      ------ Reading control header ------                */

{header} {
        BEGIN(readHeader);
    }

<readHeader>{anythingInBlock} {
        Info<< "Header: " << YYText() << endl;
    }


{dimension} {
        BEGIN(readDimension);
    }

<readDimension>{space}{label}{space} {
        dimensionOfGrid = atoi(YYText());
        Info<< "Dimension of grid: " << dimensionOfGrid << endl;
    }


{points} {
        BEGIN(readPointHeader);
    }

298
<readPointHeader>{space}{lbrac}{space}"0"{space}"1"{space} {
299
300
301
302
303
304
305
306
307
308
309
        BEGIN(readNumberOfPoints);
    }

<readNumberOfPoints>{hexLabel}{space}{labelList} {
        nPoints = strtol(YYText(), &endPtr, 16);
        Info<< "Number of points: " << nPoints << endl;
        points.setSize(nPoints);

        // Ignore rest of stream
    }

310
<readPointHeader>{space}{lbrac} {
311
312
313
314
315
316
317
318
319
320
321
322
323
324
        BEGIN(readPointGroupData);
    }

<readPointGroupData>{space}{hexLabel}{space}{hexLabel}{space}{hexLabel}{labelList} {
        // Read point zone-ID, start and end-label
        // the indices will be used for checking later.
        pointGroupZoneID.append(strtol(YYText(), &endPtr, 16));

        // In FOAM, indices start from zero - adjust
        pointGroupStartIndex.append(strtol(endPtr, &endPtr, 16) - 1);

        pointGroupEndIndex.append(strtol(endPtr, &endPtr, 16) - 1);

        // point group type skipped
mattijs's avatar
mattijs committed
325
        (void)strtol(endPtr, &endPtr, 16);
326

327
        pointi = pointGroupStartIndex.last();
328
329
330
331
332
333
334
335
336
337
338

        // reset number of components to default
        pointGroupNumberOfComponents = 3;

        // read number of components in the vector
        if (endPtr < &(YYText()[YYLeng()-1]))
        {
            pointGroupNumberOfComponents = strtol(endPtr, &endPtr, 16);
        }

        Info<< "PointGroup: "
339
            << pointGroupZoneID.last()
340
            << " start: "
341
            << pointGroupStartIndex.last()
342
            << " end: "
343
            << pointGroupEndIndex.last() << flush;
344
345
346
347
348
349
    }

<readNumberOfPoints,readPointGroupData>{endOfSection} {
        BEGIN(readPointData);
    }

350
<readPointData>{space}{lbrac}{space} {
351
352
353
354
355
356
357
        Info<< ".  Reading points..." << flush;
        cmpt = 0;
        yy_push_state(readScalarList);
    }

<readScalarList>{signedInteger}{space} {
        points[pointi][cmpt++] = scaleFactor*atol(YYText());
358
359
360
361
362
363
364
365
366
367
368

        if (cmpt == pointGroupNumberOfComponents)
        {
            if (pointGroupNumberOfComponents == 2)
            {
                points[pointi].z() = 0.0;
            }

            cmpt = 0;
            pointi++;
        }
369
370
371
372
373
    }

<readScalarList>{scalar}{space} {
        points[pointi][cmpt++] = scaleFactor*atof(YYText());

374
        if (cmpt == pointGroupNumberOfComponents)
375
        {
376
377
378
379
380
381
382
            if (pointGroupNumberOfComponents == 2)
            {
                points[pointi].z() = 0.0;
            }

            cmpt = 0;
            pointi++;
383
384
385
386
387
388
389
        }
    }

<readScalarList>{endOfSection} {
        Info<< "done." << endl;

        // check read of points
390
        if (pointi != pointGroupEndIndex.last()+1)
391
392
        {
            Warning
393
394
                << "Problem with reading points: " << nl
                << "    start index: "
395
                << pointGroupStartIndex.last()
396
                << " end index: "
397
                << pointGroupEndIndex.last()
398
399
                << " last points read: " << pointi << nl
                << "    on line " << lineNo << endl;
400
401
402
403
404
405
406
407
408
        }

        yy_pop_state();
    }

{faces} {
        BEGIN(readFaceHeader);
    }

409
<readFaceHeader>{space}{lbrac}{space}"0"{space}"1"{space} {
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
        BEGIN(readNumberOfFaces);
    }

<readNumberOfFaces>{space}{hexLabel}{space}{labelListElement}+ {
        nFaces = strtol(YYText(), &endPtr, 16);

        Info<< "Number of faces: " << nFaces << endl;

        faces.setSize(nFaces);
        owner.setSize(nFaces);
        neighbour.setSize(nFaces);

        // Type and element type not read
    }

425
<readFaceHeader>{space}{lbrac} {
426
427
428
429
430
431
432
433
434
435
436
437
438
        BEGIN(readFaceGroupData);
    }

<readFaceGroupData>{space}{hexLabel}{space}{hexLabel}{space}{hexLabel}{hexLabelListElement}+ {
        // read fluentFace zone-ID, start and end-label
        faceGroupZoneID.append(strtol(YYText(), &endPtr, 16));

        // In FOAM, indices start from zero - adjust
        faceGroupStartIndex.append(strtol(endPtr, &endPtr, 16) - 1);

        faceGroupEndIndex.append(strtol(endPtr, &endPtr, 16) - 1);

        // face group type
mattijs's avatar
mattijs committed
439
        (void)strtol(endPtr, &endPtr, 16);
440
441
442

        faceGroupElementType = strtol(endPtr, &endPtr, 16);

443
        facei = faceGroupStartIndex.last();
444
445

        Info<< "FaceGroup: "
446
            << faceGroupZoneID.last()
447
            << " start: "
448
            << faceGroupStartIndex.last()
449
            << " end: "
450
            << faceGroupEndIndex.last() << flush;
451
452
    }

453
<readNumberOfFaces,readFaceGroupData>{space}{endOfSection} {
454
455
456
        BEGIN(readFaceData);
    }

457
458
<readFaceData>{space}{lbrac} {
        if (faceGroupElementType == 0 || faceGroupElementType > 4)
459
460
461
462
463
464
465
466
467
468
469
        {
            Info<< ".  Reading mixed faces..." << flush;
            yy_push_state(readFacesMixed);
        }
        else
        {
            Info<< ".  Reading uniform faces..." << flush;
            yy_push_state(readFacesUniform);
        }
    }

470
<readFacesMixed>{space}{hexLabelList} {
471
472
473
474
475
        face& curFaceLabels = faces[facei];

        // set size of label list
        curFaceLabels.setSize(strtol(YYText(), &endPtr, 16));

476
        forAll(curFaceLabels, i)
477
478
479
480
481
482
483
484
485
486
        {
            curFaceLabels[i] = strtol(endPtr, &endPtr, 16) - 1;
        }

        // read neighbour and owner. Neighbour comes first
        neighbour[facei] = strtol(endPtr, &endPtr, 16) - 1;
        owner[facei] = strtol(endPtr, &endPtr, 16) - 1;
        facei++;
    }

487
<readFacesUniform>{space}{hexLabelList} {
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
        face& curFaceLabels = faces[facei];

        // Set size of label list.
        curFaceLabels.setSize(faceGroupElementType);

        curFaceLabels[0] = strtol(YYText(), &endPtr, 16) - 1;

        for (int i=1; i<faceGroupElementType; i++)
        {
            curFaceLabels[i] = strtol(endPtr, &endPtr, 16) - 1;
        }

        // read neighbour and owner. Neighbour comes first
        neighbour[facei] = strtol(endPtr, &endPtr, 16) - 1;
        owner[facei] = strtol(endPtr, &endPtr, 16) - 1;
        facei++;
    }

506
<readFacesMixed,readFacesUniform>{space}{endOfSection} {
507
508
509
        Info<< "done." << endl;

        // check read of fluentFaces
510
        if (facei != faceGroupEndIndex.last()+1)
511
512
        {
            Warning
513
514
                << "Problem with reading fluentFaces: " << nl
                << "    start index: "
515
                << faceGroupStartIndex.last()
516
                << " end index: "
517
                << faceGroupEndIndex.last()
518
519
                << " last fluentFaces read: " << facei << nl
                << "    on line " << lineNo << endl;
520
521
522
523
524
525
526
527
528
529
        }

        yy_pop_state();
    }


{cells} {
        BEGIN(readCellHeader);
    }

530
<readCellHeader>{space}{lbrac}{space}"0"{space}"1"{space} {
531
532
533
534
535
536
537
538
        BEGIN(readNumberOfCells);
    }

<readNumberOfCells>{space}{hexLabel}{space}{labelListElement}+ {
        nCells = strtol(YYText(), &endPtr, 16);
        Info<< "Number of cells: " << nCells << endl;
    }

539
<readCellHeader>{space}{lbrac} {
540
541
542
543
544
545
546
547
548
        BEGIN(readCellGroupData);
    }

<readCellGroupData>{space}{hexLabel}{space}{hexLabel}{space}{hexLabel}{space}{hexLabel} {
        // Warning. This entry must be above the next one because of the lexing
        // rules. It is introduced to deal with the problem of reading
        // non-standard cell definition from Tgrid, which misses the type label.

        Warning
549
550
            << "Tgrid syntax problem: " << YYText() << nl
            << "    on line " << lineNo << endl;
551
552
553
554
555
556
557
558
559
560
561
562

        // read cell zone-ID, start and end-label
        cellGroupZoneID.append(strtol(YYText(), &endPtr, 16));

        // the indices will be used for checking later.
        cellGroupStartIndex.append(strtol(endPtr, &endPtr, 16) - 1);

        cellGroupEndIndex.append(strtol(endPtr, &endPtr, 16) - 1);

        cellGroupType.append(strtol(endPtr, &endPtr, 16));

        Info<< "CellGroup: "
563
            << cellGroupZoneID.last()
564
            << " start: "
565
            << cellGroupStartIndex.last()
566
            << " end: "
567
            << cellGroupEndIndex.last()
568
            << " type: "
569
            << cellGroupType.last()
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
            << endl;
    }

<readCellGroupData>{space}{hexLabel}{space}{hexLabel}{space}{hexLabel}{space}{hexLabel}{space}{hexLabel} {
        // Warning. See above

        // read cell zone-ID, start and end-label
        cellGroupZoneID.append(strtol(YYText(), &endPtr, 16));

        // the indices will be used for checking later.
        cellGroupStartIndex.append(strtol(endPtr, &endPtr, 16) - 1);

        cellGroupEndIndex.append(strtol(endPtr, &endPtr, 16) - 1);

        cellGroupType.append(strtol(endPtr, &endPtr, 16));

        // Note. Potentially skip cell set if type is zero.
mattijs's avatar
mattijs committed
587
        (void)strtol(endPtr, &endPtr, 16);
588
589

        Info<< "CellGroup: "
590
            << cellGroupZoneID.last()
591
            << " start: "
592
            << cellGroupStartIndex.last()
593
            << " end: "
594
            << cellGroupEndIndex.last()
595
            << " type: "
596
            << cellGroupType.last()
597
598
599
600
601
602
603
            << endl;
    }

<readNumberOfCells,readCellGroupData>{endOfSection} {
        BEGIN(readCellData);
    }

604
<readCellData>{space}{lbrac} {
605
606
        // Quickly scan to the end of the cell data block and discard
        register int c;
607
608
        while ((c = yyinput()) != 0 && c != ')')
        {}
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
    }

{faceTree} {
        // There are hanging nodes in the mesh so make sure it gets zipped-up
        hangingNodes = true;
        yy_push_state(ignoreBlock);
    }

{zoneVariant1} {
        BEGIN(readZoneHeader);
    }

{zoneVariant2} {
        BEGIN(readZoneHeader);
    }

625
<readZoneHeader>{space}{lbrac} {
626
627
628
        BEGIN(readZoneGroupData);
    }

mattijs's avatar
mattijs committed
629
<readZoneGroupData>{space}{hexLabel}{space}{word}{space}{word}{space}{label}? {
630
631
632
        IStringStream zoneDataStream(YYText());

        // cell zone-ID not in hexadecimal!!! Inconsistency
mattijs's avatar
mattijs committed
633
634
635
636
637
638
639
640
641
642
        label zoneID = -1;

        if (cubitFile)
        {
            zoneID = readHexLabel(zoneDataStream);
        }
        else
        {
            zoneID = readLabel(zoneDataStream);
        }
643
644
645
646
647
648
649
650
651
652
653
654
655

        groupType.insert(zoneID, word(zoneDataStream));
        groupName.insert(zoneID, word(zoneDataStream));

        Info<< "Zone: " << zoneID
            << " name: " << groupName[zoneID]
            << " type: " << groupType[zoneID] << flush;
    }

<readZoneGroupData>{endOfSection} {
        BEGIN(readZoneData);
    }

656
<readZoneData>{space}{lbrac} {
657
658
659
660
        Info<< ".  Reading zone data..." << flush;
        yy_push_state(readZoneBlock);
    }

661
<readZoneBlock>{space}{schemeSymbolList} {
662
663
664
    }

<readZoneBlock>{lbrac} {
665
666
667
        //Warning
        //    << "Found unknown block in zone: " << YYText() << nl
        //    << "    on line " << lineNo << endl;
668
669
670
671
672
673
674
675
676
677
678
679
        yy_push_state(ignoreBlock);
    }

<readZoneBlock>{endOfSection} {
        Info<< "done." << endl;
        yy_pop_state();
    }



 /*             ------ Reading end of section and others ------              */

680
<readHeader,readDimension,readPointData,readFaceData,readCellData,readZoneData>{space}{endOfSection} {
681
682
683
684
685
686
687
688
        BEGIN(INITIAL);
    }

 /*    ------ Reading unknown type or non-standard comment ------            */

{lbrac}{label} {
        Warning
            << "Found unknown block of type: "
689
690
            << Foam::string(YYText())(1, YYLeng()-1) << nl
            << "    on line " << lineNo << endl;
691
692
693
694
695
696
697
698

        yy_push_state(ignoreBlock);
    }

{lbrac}{redundantBlock} {
        yy_push_state(ignoreBlock);
    }

699
<ignoreBlock,ignoreEmbeddedBlock>{space}{quote}{text}{quote} {
700
701
    }

702
<ignoreBlock,ignoreEmbeddedBlock>{space}{schemeSymbol} {
703
704
    }

705
<ignoreBlock,ignoreEmbeddedBlock>{space}{lbrac} {
706
707
708
709
        yy_push_state(ignoreEmbeddedBlock);

    }

710
<ignoreBlock,ignoreEmbeddedBlock>{space}{endOfSection} {
711
712
713
        yy_pop_state();
    }

714
<ignoreBlock,ignoreEmbeddedBlock>{space}{labelList} {
715
716
    }

717
<ignoreBlock,ignoreEmbeddedBlock>{space}{hexLabelList} {
718
719
    }

720
<ignoreBlock,ignoreEmbeddedBlock>{space}{scalarList} {
721
722
    }

723
<ignoreBlock,ignoreEmbeddedBlock>{space}{schemeSymbolList} {
724
725
    }

726
727
728
729
730
731
732
733
<ignoreBlock,ignoreEmbeddedBlock>{space}{text} {
    }


 /* ------              Count newlines.                              ------  */

<*>\n {
        lineNo++;
734
735
736
    }


737
 /* ------              Ignore remaining space.                      ------  */
738

739
<*>{some_space}|\r {
740
741
742
743
744
745
746
747
    }


 /* ------              Any other characters are errors.              ------ */

<*>. {
        // This is a catch all.
        FatalErrorIn("fluentMeshToFoam::lexer")
748
749
            << "Do not understand characters: " << YYText() << nl
            << "    on line " << lineNo
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
            << exit(FatalError);
    }


 /*  ------ On EOF return to previous file, if none exists terminate. ------ */

<<EOF>> {
            yyterminate();
    }
%%

int main(int argc, char *argv[])
{
    argList::noParallel();
    argList::validArgs.append("Fluent mesh file");
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
    argList::addOption
    (
        "scale",
        "factor",
        "geometry scaling factor - default is 1"
    );
    argList::addOption
    (
        "ignoreCellGroups",
        "names",
        "specify cell groups to ignore"
    );
    argList::addOption
    (
        "ignoreFaceGroups",
        "names",
        "specify face groups to ignore"
    );

    argList::addBoolOption
    (
        "cubit",
        "special parsing of (incorrect) cubit files"
    );
789
790
791
792
793
794
795
796

    argList args(argc, argv);

    if (!args.check())
    {
        FatalError.exit();
    }

797
    args.optionReadIfPresent("scale", scaleFactor);
798

799
800
    wordHashSet ignoreCellGroups;
    wordHashSet ignoreFaceGroups;
801

802
803
    args.optionReadIfPresent("ignoreCellGroups", ignoreCellGroups);
    args.optionReadIfPresent("ignoreFaceGroups", ignoreFaceGroups);
804

mattijs's avatar
mattijs committed
805
806
807
808
809
810
811
812
813
814
815
    cubitFile = args.options().found("cubit");

    if (cubitFile)
    {
        Info<< nl
            << "Assuming Cubit generated file"
            << " (incorrect face orientation; hexadecimal zoneIDs)."
            << nl << endl;
    }


816
817
#   include "createTime.H"

818
    const fileName fluentFile = args[1];
819
820
821
822
823
824
825
826
827
828
829
    IFstream fluentStream(fluentFile);

    if (!fluentStream)
    {
        FatalErrorIn(args.executable())
            << ": file " << fluentFile << " not found"
            << exit(FatalError);
    }

    yyFlexLexer lexer(&fluentStream.stdStream());

830
    while (lexer.yylex() != 0)
831
    {}
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857

    Info<< "\nFINISHED LEXING\n\n";

    if (dimensionOfGrid != 3)
    {
        FatalErrorIn(args.executable())
            << "Mesh is not 3D, dimension of grid: " << dimensionOfGrid
            << exit(FatalError);
    }

    pointGroupZoneID.shrink();
    pointGroupStartIndex.shrink();
    pointGroupEndIndex.shrink();

    faceGroupZoneID.shrink();
    faceGroupStartIndex.shrink();
    faceGroupEndIndex.shrink();

    cellGroupZoneID.shrink();
    cellGroupStartIndex.shrink();
    cellGroupEndIndex.shrink();
    cellGroupType.shrink();


    // Pre-filtering: flip "owner" boundary or wrong oriented internal
    // faces and move to neighbour
mattijs's avatar
mattijs committed
858
859

    boolList fm(faces.size(), false);
860
    forAll(faces, facei)
861
862
863
864
865
866
867
    {
        if
        (
            owner[facei] == -1
         || (neighbour[facei] != -1 && owner[facei] > neighbour[facei])
        )
        {
mattijs's avatar
mattijs committed
868
            fm[facei] = true;
mattijs's avatar
mattijs committed
869
870
871
872
            if (!cubitFile)
            {
                faces[facei] = faces[facei].reverseFace();
            }
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
            Swap(owner[facei], neighbour[facei]);
        }
    }


    // Foam type for Fluent type
    // ~~~~~~~~~~~~~~~~~~~~~~~~~

    HashTable<word> fluentToFoamType;

    fluentToFoamType.insert("pressure", polyPatch::typeName);
    fluentToFoamType.insert("pressure-inlet", polyPatch::typeName);
    fluentToFoamType.insert("inlet-vent", polyPatch::typeName);
    fluentToFoamType.insert("intake-fan", polyPatch::typeName);
    fluentToFoamType.insert("pressure-outlet", polyPatch::typeName);
    fluentToFoamType.insert("exhaust-fan", polyPatch::typeName);
    fluentToFoamType.insert("outlet-vent", polyPatch::typeName);
    fluentToFoamType.insert("pressure-far-field", polyPatch::typeName);
    fluentToFoamType.insert("velocity-inlet", polyPatch::typeName);
    fluentToFoamType.insert("mass-flow-inlet", polyPatch::typeName);
    fluentToFoamType.insert("outflow", polyPatch::typeName);

    fluentToFoamType.insert("wall" , wallPolyPatch::typeName);

    fluentToFoamType.insert("symmetry",  symmetryPolyPatch::typeName);
    fluentToFoamType.insert("axis",  symmetryPolyPatch::typeName);

    fluentToFoamType.insert("interior", polyPatch::typeName);
    fluentToFoamType.insert("interface", polyPatch::typeName);
    fluentToFoamType.insert("internal", polyPatch::typeName);
    fluentToFoamType.insert("solid", polyPatch::typeName);
904
    fluentToFoamType.insert("fan", oldCyclicPolyPatch::typeName);
905
906
907
    fluentToFoamType.insert("radiator", polyPatch::typeName);
    fluentToFoamType.insert("porous-jump", polyPatch::typeName);

908
909
910
911
912
913
914
    //- Periodic halves map directly into split cyclics. The problem is the
    //  initial matching since we require knowledge of the transformation.
    //  It is ok if the periodics are already ordered. We should read the
    //  periodic shadow faces section (section 18) to give use the ordering
    //  For now just disable.
    //fluentToFoamType.insert("periodic", cyclicPolyPatch::typeName);

915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934

    // Foam patch type for Fluent zone type
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    HashSet<word> fluentGroupToFoamPatch;
    fluentGroupToFoamPatch.insert("wall");
    fluentGroupToFoamPatch.insert("fan");


    // Create intial empty polyMesh
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    polyMesh mesh
    (
        IOobject
        (
            polyMesh::defaultRegion,
            runTime.constant(),
            runTime
        ),
935
936
937
938
        xferCopy(pointField()),
        xferCopy(faceList()),
        xferCopy(labelList()),
        xferCopy(labelList())
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
    );


    // Check the cell groups for zones ignoring those in ignoreCellGroups
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    label nCellZones = 0;
    labelList cellZoneIDs(cellGroupZoneID.size());

    forAll(cellGroupZoneID, cgi)
    {
        if (!ignoreCellGroups.found(groupName[cellGroupZoneID[cgi] ]))
        {
            cellZoneIDs[nCellZones++] = cgi;
        }
    }

    cellZoneIDs.setSize(nCellZones);


    // Check the face groups for boundary patches, baffles and faceZones
    // ignoring the interior zones in ignoreCellGroups
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
961
962
    DynamicList<label> patchIDs(faceGroupZoneID.size());
    DynamicList<label> faceZoneIDs(faceGroupZoneID.size());
963
964
965
966
967
968
969
970
971
972
973
974
975

    forAll(faceGroupZoneID, fgi)
    {
        label zoneID = faceGroupZoneID[fgi];
        label start = faceGroupStartIndex[fgi];

        if (groupType.found(zoneID))
        {
            const word& type = groupType[zoneID];

            // Check the first element of neighbour for boundary group
            if (neighbour[start] == -1 || fluentGroupToFoamPatch.found(type))
            {
976
                patchIDs.append(fgi);
977
978
979
            }
            else
            {
980
                if (!ignoreFaceGroups.found(groupName[faceGroupZoneID[fgi] ]))
981
                {
982
                    faceZoneIDs.append(fgi);
983
984
985
986
987
988
989
990
991
992
993
994
995
996
                }
            }
        }
        else if (hangingNodes)
        {
            label end = faceGroupEndIndex[fgi];

            Info<< "Unknown FaceGroup " << zoneID
                << " assumed to be parent faces of refinement "
                   "patterns and ignored."
                << endl;

            // Set the owner of these faces to -1 so that they do not get
            // added to the mesh
997
            for (label facei = start; facei <= end; facei++)
998
999
1000
1001
1002
1003
            {
                owner[facei] = -1;
            }
        }
        else
        {
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
            if (neighbour[start] == -1)
            {
                // Boundary face in unknown group. Create a patch for it.
                groupType.insert(zoneID, "unknown");
                groupName.insert(zoneID, "FaceGroup" + Foam::name(zoneID));
                patchIDs.append(fgi);
                Info<< "Created patch " << fgi << " for unknown FaceGroup "
                    << zoneID << '.' << endl;
            }
            else
            {
                WarningIn(args.executable())
                    << "Unknown FaceGroup " << zoneID << " not in a zone"
                    << endl;
            }
1019
1020
1021
        }
    }

1022
1023
    patchIDs.shrink();
    faceZoneIDs.shrink();
1024
1025
1026
1027
1028


    // Add empty patches
    // ~~~~~~~~~~~~~~~~~

1029
    List<polyPatch*> newPatches(patchIDs.size());
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
    HashSet<word> patchNames;

    forAll(patchIDs, patchi)
    {
        label zoneID = faceGroupZoneID[patchIDs[patchi] ];
        word name = groupName[zoneID];
        const word& type = groupType[zoneID];

        Info<< "Creating patch " << patchi
            << " for zone: " << zoneID
            << " name: " << name
            << " type: " << type
            << endl;

        uniquify(name, patchNames);

        HashTable<word>::const_iterator iter = fluentToFoamType.find(type);

        if (iter != fluentToFoamType.end())
        {
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
            // See if we have a periodic and can derive the other side.
            word neighbPatchName;
            if (iter() == cyclicPolyPatch::typeName)
            {
                // Periodic
                size_t n = name.rfind("-SIDE-1");

                if (n != string::npos)
                {
                    neighbPatchName = name.substr(0, n) + "-SIDE-2";
                }
                else
                {
                    n = name.rfind("-SIDE-2");
                    if (n != string::npos)
                    {
                        neighbPatchName = name.substr(0, n) + "-SIDE-1";
                    }
                }
            }

            if (neighbPatchName.size())
            {
                Info<< "Adding cyclicPolyPatch for Fluent zone " << name
                    << " with neighbour patch " << neighbPatchName
                    << endl;

                newPatches[patchi] = new cyclicPolyPatch
                (
                    name,
                    0,
                    0,
                    patchi,
                    mesh.boundaryMesh(),
                    neighbPatchName,
                    cyclicPolyPatch::NOORDERING,
                    vector::zero,
                    vector::zero,
                    vector::zero
                );
            }
            else
            {
                newPatches[patchi] = polyPatch::New
                (
                    iter(),
                    name,
                    0,
                    0,
                    patchi,
                    mesh.boundaryMesh()
                ).ptr();
            }
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
        }
        else
        {
            Info<< "Adding polyPatch for unknown Fluent type " << type
                << endl;

            newPatches[patchi] = new polyPatch
            (
                name,
                0,
                0,
                patchi,
                mesh.boundaryMesh()
            );
        }
    }
    mesh.addPatches(newPatches);


    // Add empty zones
    // ~~~~~~~~~~~~~~~

    // Cell zones
    mesh.cellZones().setSize(cellZoneIDs.size());
    HashSet<word> cellZoneNames;

    forAll(cellZoneIDs, cellZonei)
    {
        label zoneID = cellGroupZoneID[cellZoneIDs[cellZonei] ];
        word name = groupName[zoneID];
        const word& type = groupType[zoneID];

        Info<< "Creating cellZone " << cellZonei
            << " name: " << name
            << " type: " << type
            << endl;

        uniquify(name, cellZoneNames);

        mesh.cellZones().set
        (
            cellZonei,
            new cellZone
            (
                name,
                labelList(0),
                cellZonei,
                mesh.cellZones()
            )
        );
    }

    // Face zones
1156
    mesh.faceZones().setSize(faceZoneIDs.size());
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
    HashSet<word> faceZoneNames;

    forAll(faceZoneIDs, faceZonei)
    {
        label zoneID = faceGroupZoneID[faceZoneIDs[faceZonei] ];
        word name = groupName[zoneID];
        const word& type = groupType[zoneID];

        Info<< "Creating faceZone " << faceZonei
            << " name: " << name
            << " type: " << type
            << endl;

        uniquify(name, faceZoneNames);

        mesh.faceZones().set
        (
            faceZonei,
            new faceZone
            (
                name,
                labelList(0),
                boolList(0),
                faceZonei,
                mesh.faceZones()
            )
        );
    }


    // Modify mesh for points/cells/faces
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    // Mesh-change container
    polyTopoChange meshMod(mesh, false);

    // Add all points
    forAll(points, pointi)
    {
        meshMod.addPoint(points[pointi], pointi, -1, true);
    }
    points.setSize(0);

    // Add all cells
    for (label celli = 0; celli < nCells; celli++)
    {
        meshMod.addCell
        (
            -1,         // masterPointID
            -1,         // masterEdgeID
            -1,         // masterFaceID
            celli,      // masterCellID
            -1          // zoneID
        );
    }

    // Modify cells to be in zones as required
    forAll(cellZoneIDs, cellZonei)
    {
        label cgi = cellZoneIDs[cellZonei];

        for
        (
            label celli = cellGroupStartIndex[cgi];
            celli <= cellGroupEndIndex[cgi];
            celli++
        )
        {
            meshMod.modifyCell(celli, cellZonei);
        }
    }


1230
1231
    bool doneWarning = false;

1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
    // Add faceZone faces
    forAll(faceZoneIDs, faceZonei)
    {
        label fgi = faceZoneIDs[faceZonei];
        label start = faceGroupStartIndex[fgi];
        label end = faceGroupEndIndex[fgi];
        label zoneID = faceGroupZoneID[fgi];

        Info<< "faceZone from Fluent indices: " << start
            << " to: " << end
            << " type: " << groupType[zoneID]
            << endl;

        for (label facei = start; facei <= end; facei++)
        {
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
            if (owner[facei] >= nCells || neighbour[facei] >= nCells)
            {
                if (!doneWarning)
                {
                    WarningIn(args.executable())
                        << "Ignoring internal face " << facei
                        << " on FaceZone " << zoneID
                        << " since owner " << owner[facei] << " or neighbour "
                        << neighbour[facei] << " outside range of cells 0.."
                        << nCells-1 << endl
                        << "    Suppressing future warnings." << endl;
                    doneWarning = true;
                }
            }
            else
            {
                meshMod.addFace
                (
                    faces[facei],
                    owner[facei],
                    neighbour[facei],
                    -1,                 // masterPointID
                    -1,                 // masterEdgeID
                    facei,              // masterFace
                    false,              // flipFaceFlux
                    -1,                 // patchID
                    faceZonei,          // zoneID
mattijs's avatar
mattijs committed
1274
                    fm[facei]           // zoneFlip
1275
1276
                );
            }
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297

            // Mark face as being done
            owner[facei] = -1;
        }
    }

    // Add patch faces
    forAll(patchIDs, patchi)
    {
        label fgi = patchIDs[patchi];
        label start = faceGroupStartIndex[fgi];
        label end = faceGroupEndIndex[fgi];
        label zoneID = faceGroupZoneID[fgi];

        Info<< "patch " << patchi << " from Fluent indices: " << start
            << " to: " << end
            << " type: " << groupType[zoneID]
            << endl;

        for (label facei = start; facei <= end; facei++)
        {
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
            if (owner[facei] >= nCells || neighbour[facei] >= nCells)
            {
                if (!doneWarning)
                {
                    WarningIn(args.executable())
                        << "Ignoring patch face " << facei
                        << " on FaceZone " << zoneID
                        << " since owner " << owner[facei] << " or neighbour "
                        << neighbour[facei] << " outside range of cells 0.."
                        << nCells-1 << endl
                        << "    Suppressing future warnings." << endl;
                    doneWarning = true;
                }
            }
            else
1313
1314
1315
            {
                meshMod.addFace
                (
1316
1317
                    faces[facei],
                    owner[facei],
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
                    -1,
                    -1,                 // masterPointID
                    -1,                 // masterEdgeID
                    facei,              // masterFace
                    false,              // flipFaceFlux
                    patchi,             // patchID
                    -1,                 // zoneID
                    false               // zoneFlip
                );

1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
                // For baffles create the opposite face
                if (neighbour[start] != -1)
                {
                    meshMod.addFace
                    (
                        faces[facei].reverseFace(),
                        neighbour[facei],
                        -1,
                        -1,                 // masterPointID
                        -1,                 // masterEdgeID
                        facei,              // masterFace
                        false,              // flipFaceFlux
                        patchi,             // patchID
                        -1,                 // zoneID
                        false               // zoneFlip
                    );
                }
            }
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
            // Mark face as being done
            owner[facei] = -1;
        }
    }

    // Add remaining internal faces
    forAll(owner, facei)
    {
        if (owner[facei] != -1)
        {
            // Check the face being added as an internal face actually is one
            if (neighbour[facei] == -1)
            {
                FatalErrorIn(args.executable())
                    << "Attempt of add internal face " << facei
                    << " which is a boundary face"
                    << exit(FatalError);
            }

1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
            if (owner[facei] >= nCells || neighbour[facei] >= nCells)
            {
                if (!doneWarning)
                {
                    WarningIn(args.executable())
                        << "Ignoring internal face " << facei
                        << " since owner " << owner[facei] << " or neighbour "
                        << neighbour[facei] << " outside range of cells 0.."
                        << nCells-1 << endl
                        << "    Suppressing future warnings." << endl;
                    doneWarning = true;
                }
            }
            else
            {
                meshMod.addFace
                (
                    faces[facei],
                    owner[facei],
                    neighbour[facei],
                    -1,                 //masterPointID
                    -1,                 //masterEdgeID
                    facei,              //masterFace
                    false,              //flipFaceFlux
                    -1,                 //patchID
                    -1,                 //zoneID
                    false               //zoneFlip
                );
            }
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
        }
    }

    // Reclaim storage
    faces.setSize(0);
    owner.setSize(0);
    neighbour.setSize(0);


    // Modify mesh
    // ~~~~~~~~~~~

    autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false);

    // Zip-up the mesh if it contained hanging nodes
    if (hangingNodes)
    {
        Info<< "Zipping mesh to remove hanging nodes" << endl;
        polyMeshZipUpCells(mesh);
    }

    mesh.setInstance(runTime.constant());

    // Set the precision of the points data to 10
    IOstream::defaultPrecision(10);

    Info<< nl << "Writing mesh to " << mesh.objectPath() << endl;
    mesh.write();


Mark Olesen's avatar
Mark Olesen committed
1424
    Info<< "\nEnd\n" << endl;
1425
1426
1427
1428
1429
1430
1431
    return 0;
}


 /* ------------------------------------------------------------------------ *\
    ------ End of fluentMeshToFoam.L
 \* ------------------------------------------------------------------------ */