1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 96 97 98 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
|
/*--------------------------------*- C++ -*----------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
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.
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
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
ansysToFoam
Description
Converts an ANSYS input mesh file, exported from I-DEAS,
to OpenFOAM format.
\*---------------------------------------------------------------------------*/
%{
#undef yyFlexLexer
/* ------------------------------------------------------------------------- *\
------ local definitions
\* ------------------------------------------------------------------------- */
#include <sstream>
// For EOF only
#include <cstdio>
#include "scalar.H"
#include "IStringStream.H"
using namespace Foam;
#include "argList.H"
#include "Time.H"
#include "polyMesh.H"
#include "emptyPolyPatch.H"
#include "preservePatchTypes.H"
#include "cellShape.H"
#include "cellModeller.H"
#include "SLList.H"
#include "SLPtrList.H"
SLList<point> slPoints;
SLList<label> slPointMap;
label maxNodei = 0;
SLPtrList<labelList> slCellLabels;
SLList<label> slCellMap;
SLList<label> slCellType;
label maxCelli = 0;
PtrList<SLList<label>> slPatchCells;
PtrList<SLList<label>> slPatchCellFaces;
// Cell types
Map<word> cellTypes;
label currentTypei = -1;
// 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>>
int yyFlexLexer::yywrap()
{
return 1;
}
%}
one_space [ \t\f\r]
space {one_space}*
some_space {one_space}+
cspace ","{space}
alpha [_A-Za-z]
digit [0-9]
identifier {alpha}({alpha}|{digit})*
integer {digit}+
label [1-9]{digit}*
exponent_part [eE][-+]?{digit}+
fractional_constant [-+]?(({digit}*"."{digit}+)|({digit}+"."?))
floatNum (({fractional_constant}{exponent_part}?)|({digit}+{exponent_part}))
x {floatNum}
y {floatNum}
z {floatNum}
value {floatNum}
node ^{space}"N"{cspace}
element ^{space}"EN"{cspace}
bface ^{space}"SFE"{cspace}
elementTypeName ^{space}"ET"{cspace}
elementType ^{space}"TYPE"{cspace}
%%
%{
labelList labels(8);
%}
/* ------------------------------------------------------------------------- *\
------ Start Lexing ------
\* ------------------------------------------------------------------------- */
{node}{label}{cspace}{x}{cspace}{y}{cspace}{z}{space}\n {
IStringStream nodeStream(YYText());
char tag, c;
label nodei;
point node;
nodeStream
>> tag
>> c >> nodei
>> c >> node.x()
>> c >> node.y()
>> c >> node.z();
if (nodei > maxNodei) maxNodei = nodei;
slPointMap.append(nodei);
slPoints.append(node);
}
{element}{label}{cspace}{label}{cspace}{label}{cspace}{label}{cspace}{label}{cspace}{label}{cspace}{label}{cspace}{label}{cspace}{label}{space}\n {
IStringStream elementStream(YYText());
char tag, c;
label celli;
elementStream
>> tag >> tag
>> c >> celli
>> c >> labels[0]
>> c >> labels[1]
>> c >> labels[2]
>> c >> labels[3]
>> c >> labels[4]
>> c >> labels[5]
>> c >> labels[6]
>> c >> labels[7];
if (celli > maxCelli) maxCelli = celli;
slCellMap.append(celli);
slCellLabels.append(new labelList(labels));
slCellType.append(currentTypei);
}
{bface}{label}{cspace}{label}{cspace}{identifier}{cspace}{integer}{cspace}{value}{space}\n {
IStringStream bfaceStream(YYText());
char tag, c;
label elementi;
label facei;
scalar indexValue, unknown;
bfaceStream
>> tag >> tag >> tag
>> c >> elementi
>> c >> facei
>> c >> tag >> tag >> tag >> tag
>> c >> unknown
>> c >> indexValue;
label patchi = label(indexValue);
if (patchi > slPatchCells.size())
{
slPatchCells.setSize(patchi);
forAll(slPatchCells, i)
{
if (!slPatchCells(i))
{
slPatchCells.set(i, new SLList<label>);
}
}
}
if (patchi > slPatchCellFaces.size())
{
slPatchCellFaces.setSize(patchi);
forAll(slPatchCells, i)
{
if (!slPatchCellFaces(i))
{
slPatchCellFaces.set(i, new SLList<label>);
}
}
}
slPatchCells[patchi-1].append(elementi);
slPatchCellFaces[patchi-1].append(facei);
}
{elementTypeName}{label}{cspace}{identifier}{space}\n {
IStringStream elementStream(YYText());
char tag,c;
label cellTypei;
word cellTypeName;
elementStream
>> tag >> tag // skip 'ET'
>> c >> cellTypei
>> c >> cellTypeName;
Info<< "Read typeName " << cellTypeName
<< " for type " << cellTypei << endl;
cellTypes.insert(cellTypei, cellTypeName);
}
{elementType}{label}{space}\n {
IStringStream elementStream(YYText());
char tag,c;
label cellTypei;
elementStream
>> tag >> tag >> tag >> tag // skip 'TYPE'
>> c >> cellTypei;
currentTypei = cellTypei;
}
/* ------------------------------------------------------------------------- *\
------ Ignore remaining space and \n s. Any other characters are errors.
\* ------------------------------------------------------------------------- */
.|\n {}
/* ------------------------------------------------------------------------- *\
------ On EOF return to previous file, if none exists terminate.
\* ------------------------------------------------------------------------- */
<<EOF>> {
yyterminate();
}
%%
#include "fileName.H"
#include <fstream>
using std::ifstream;
label findFace(const polyMesh& mesh, const face& f)
{
const labelList& pFaces = mesh.pointFaces()[f[0]];
forAll(pFaces, i)
{
label facei = pFaces[i];
if (mesh.faces()[facei] == f)
{
return facei;
}
}
FatalErrorInFunction
<< "Cannot find a face matching " << f
<< exit(FatalError);
return -1;
}
int main(int argc, char *argv[])
{
argList::noParallel();
argList::validArgs.append("ANSYS input file");
argList::addOption
(
"scale",
"factor",
"geometry scaling factor - default is 1"
);
argList args(argc, argv);
if (!args.check())
{
FatalError.exit();
}
const scalar scaleFactor = args.optionLookupOrDefault("scale", 1.0);
#include "createTime.H"
fileName ansysFile(args[1]);
ifstream ansysStream(ansysFile.c_str());
if (!ansysStream)
{
FatalErrorInFunction
<< args.executable()
<< ": file " << ansysFile << " not found"
<< exit(FatalError);
}
yyFlexLexer lexer(&ansysStream);
while (lexer.yylex() != 0)
{}
Info<< "Creating points" << endl;
pointField points(slPoints.size());
label i = 0;
forAllConstIter(SLList<point>, slPoints, pointIter)
{
// Scale points for the given scale factor
points[i++] = scaleFactor * pointIter();
}
labelList pointMap(maxNodei+1);
i = 0;
forAllConstIter(SLList<label>, slPointMap, pointMapIter)
{
pointMap[pointMapIter()] = i++;
}
Info<< "Creating cells" << endl;
labelList cellMap(maxCelli+1);
i = 0;
forAllConstIter(SLList<label>, slCellMap, cellMapIter)
{
cellMap[cellMapIter()] = i++;
}
const cellModel& hex = *(cellModeller::lookup("hex"));
const cellModel& prism = *(cellModeller::lookup("prism"));
const cellModel& pyr = *(cellModeller::lookup("pyr"));
const cellModel& tet = *(cellModeller::lookup("tet"));
labelList labelsHex(8);
labelList labelsPrism(6);
labelList labelsPyramid(5);
labelList labelsTet(4);
cellShapeList cellShapes(slCellLabels.size());
label nCells = 0;
forAllConstIter(SLPtrList<labelList>, slCellLabels, cellIter)
{
if // Tetrahedron
(
cellIter()[2] == cellIter()[3]
&& cellIter()[4] == cellIter()[5]
&& cellIter()[5] == cellIter()[6]
&& cellIter()[6] == cellIter()[7]
)
{
labelsTet[0] = pointMap[cellIter()[0] ];
labelsTet[1] = pointMap[cellIter()[1] ];
labelsTet[2] = pointMap[cellIter()[2] ];
labelsTet[3] = pointMap[cellIter()[4] ];
cellShapes[nCells++] = cellShape(tet, labelsTet);
}
else if // Square-based pyramid
(
cellIter()[4] == cellIter()[5]
&& cellIter()[5] == cellIter()[6]
&& cellIter()[6] == cellIter()[7]
)
{
labelsPyramid[0] = pointMap[cellIter()[0] ];
labelsPyramid[1] = pointMap[cellIter()[1] ];
labelsPyramid[2] = pointMap[cellIter()[2] ];
labelsPyramid[3] = pointMap[cellIter()[3] ];
labelsPyramid[4] = pointMap[cellIter()[4] ];
cellShapes[nCells++] = cellShape(pyr, labelsPyramid);
}
else if // Triangular prism
(
cellIter()[2] == cellIter()[3]
&& cellIter()[6] == cellIter()[7]
)
{
labelsPrism[0] = pointMap[cellIter()[0] ];
labelsPrism[1] = pointMap[cellIter()[1] ];
labelsPrism[2] = pointMap[cellIter()[2] ];
labelsPrism[3] = pointMap[cellIter()[4] ];
labelsPrism[4] = pointMap[cellIter()[5] ];
labelsPrism[5] = pointMap[cellIter()[6] ];
cellShapes[nCells++] = cellShape(prism, labelsPrism);
}
else // Hex
{
labelsHex[0] = pointMap[cellIter()[0] ];
labelsHex[1] = pointMap[cellIter()[1] ];
labelsHex[2] = pointMap[cellIter()[2] ];
labelsHex[3] = pointMap[cellIter()[3] ];
labelsHex[4] = pointMap[cellIter()[4] ];
labelsHex[5] = pointMap[cellIter()[5] ];
labelsHex[6] = pointMap[cellIter()[6] ];
labelsHex[7] = pointMap[cellIter()[7] ];
cellShapes[nCells++] = cellShape(hex, labelsHex);
}
}
const word defaultFacesName = "defaultFaces";
word defaultFacesType = emptyPolyPatch::typeName;
// Create dummy mesh just to find out what are internal/external
// faces
autoPtr<polyMesh> dummyMesh
(
new polyMesh
(
IOobject
(
"dummyMesh",
runTime.constant(),
runTime
),
xferCopy(points),
cellShapes,
faceListList(0),
wordList(0),
wordList(0),
defaultFacesName,
defaultFacesType,
wordList(0)
)
);
// Warning: tet face order has changed between version 1.9.6 and 2.0
//
label faceIndex[7][6] =
{
{-1, -1, -1, -1, -1, -1}, // 0
{-1, -1, -1, -1, -1, -1}, // 1
{-1, -1, -1, -1, -1, -1}, // 2
{-1, -1, -1, -1, -1, -1}, // 3
{ 3, 2, 0, -1, 1, -1}, // tet (version 2.0)
{ 0, 4, 3, -1, 2, 1}, // prism
{ 4, 2, 1, 3, 0, 5}, // hex
};
Info<< "Creating boundary patches" << endl;
faceListList boundary(slPatchCells.size());
wordList patchNames(slPatchCells.size());
forAll(slPatchCells, patchi)
{
SLList<face> patchFaces;
SLList<label>::iterator cellIter(slPatchCells[patchi].begin());
SLList<label>::iterator faceIter(slPatchCellFaces[patchi].begin());
for
(
;
cellIter != slPatchCells[patchi].end()
&& faceIter != slPatchCellFaces[patchi].end();
++cellIter, ++faceIter
)
{
const cellShape& shape = cellShapes[cellMap[cellIter()]];
patchFaces.append
(
shape.faces()
[
faceIndex
[shape.nFaces()]
[faceIter()-1]
]
);
}
boundary[patchi] = patchFaces;
patchNames[patchi] = word("patch") + name(patchi + 1);
}
//
// Lookup the face labels for all the boundary faces
//
labelListList boundaryFaceLabels(boundary.size());
forAll(boundary, patchi)
{
const faceList& bFaces = boundary[patchi];
labelList& bFaceLabels = boundaryFaceLabels[patchi];
bFaceLabels.setSize(bFaces.size());
forAll(bFaces, i)
{
bFaceLabels[i] = findFace(dummyMesh(), bFaces[i]);
}
}
// Now split the boundary faces into external and internal faces. All
// faces go into faceZones and external faces go into patches.
List<faceList> patchFaces(slPatchCells.size());
labelList patchNFaces(slPatchCells.size(), 0);
forAll(boundary, patchi)
{
const faceList& bFaces = boundary[patchi];
const labelList& bFaceLabels = boundaryFaceLabels[patchi];
patchFaces[patchi].setSize(bFaces.size());
forAll(bFaces, i)
{
if (!dummyMesh().isInternalFace(bFaceLabels[i]))
{
patchFaces[patchi][patchNFaces[patchi]++] = bFaces[i];
}
}
patchFaces[patchi].setSize(patchNFaces[patchi]);
Info<< "Patch " << patchi << " named " << patchNames[patchi]
<< ": " << boundary[patchi].size() << " faces" << endl;
}
// We no longer need the dummyMesh
dummyMesh.clear();
Info<< "ansysToFoam: " << endl
<< "Ansys file format does not provide information about the type of "
<< "the patch (eg. wall, symmetry plane, cyclic etc)." << endl
<< "All the patches have been created "
<< "as type patch. Please reset after mesh conversion as necessary."
<< endl;
PtrList<dictionary> patchDicts;
preservePatchTypes
(
runTime,
runTime.constant(),
polyMesh::meshSubDir,
patchNames,
patchDicts,
defaultFacesName,
defaultFacesType
);
// Add information to dictionary
forAll(patchNames, patchi)
{
if (!patchDicts.set(patchi))
{
patchDicts.set(patchi, new dictionary());
}
// Add but not overwrite
patchDicts[patchi].add("type", polyPatch::typeName, false);
}
polyMesh pShapeMesh
(
IOobject
(
polyMesh::defaultRegion,
runTime.constant(),
runTime
),
xferMove(points),
cellShapes,
patchFaces,
patchNames,
patchDicts,
defaultFacesName,
defaultFacesType
);
if (cellTypes.size() > 0 || patchNames.size() > 0)
{
DynamicList<pointZone*> pz;
DynamicList<faceZone*> fz;
DynamicList<cellZone*> cz;
// FaceZones
forAll(boundaryFaceLabels, patchi)
{
if (boundaryFaceLabels[patchi].size())
{
// Re-do the boundaryFaceLabels since the boundary face
// labels will be different on the pShapeMesh.
const faceList& bFaces = boundary[patchi];
labelList& bFaceLabels = boundaryFaceLabels[patchi];
forAll(bFaceLabels, i)
{
bFaceLabels[i] = findFace(pShapeMesh, bFaces[i]);
}
Info<< "Creating faceZone " << patchNames[patchi]
<< " with " << bFaceLabels.size() << " faces" << endl;
fz.append
(
new faceZone
(
patchNames[patchi],
bFaceLabels,
boolList(bFaceLabels.size(), false),
fz.size(),
pShapeMesh.faceZones()
)
);
}
}
// CellZones
labelList types = cellTypes.sortedToc();
forAll(types, j)
{
label cellType = types[j];
// Pick up cells in zone
DynamicList<label> addr;
SLList<label>::iterator cellMapIter = slCellMap.begin();
SLList<label>::iterator typeIter = slCellType.begin();
for
(
;
typeIter != slCellType.end();
++typeIter, ++cellMapIter
)
{
if (typeIter() == cellType)
{
addr.append(cellMap[cellMapIter()]);
}
}
Info<< "Creating cellZone " << cellTypes[cellType]
<< " with " << addr.size() << " cells" << endl;
cz.append
(
new cellZone
(
cellTypes[cellType],
addr,
j,
pShapeMesh.cellZones()
)
);
}
pShapeMesh.addZones(pz, fz, cz);
}
// Set the precision of the points data to 10
IOstream::defaultPrecision(max(10u, IOstream::defaultPrecision()));
Info<< "Writing polyMesh" << endl;
pShapeMesh.write();
Info<< nl << "end" << endl;
return 0;
}
/* ------------------------------------------------------------------------- *\
------ End of ansysToFoam.L
\* ------------------------------------------------------------------------- */
|