00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "argList.H"
00037 #include "polyMesh.H"
00038 #include "Time.H"
00039 #include "IFstream.H"
00040 #include "cellModeller.H"
00041 #include "cellSet.H"
00042 #include "faceSet.H"
00043 #include "DynamicList.H"
00044 #include "triSurface.H"
00045
00046 using namespace Foam;
00047
00048
00049
00050 const string SEPARATOR(" -1");
00051
00052 bool isSeparator(const string& line)
00053 {
00054 return line.substr(0, 6) == SEPARATOR;
00055 }
00056
00057
00058
00059 label readTag(IFstream& is)
00060 {
00061 string tag;
00062 do
00063 {
00064 if (!is.good())
00065 {
00066 return -1;
00067 }
00068
00069 string line;
00070
00071 is.getLine(line);
00072
00073 if (line.size() < 6)
00074 {
00075 return -1;
00076 }
00077
00078 tag = line.substr(0, 6);
00079
00080 } while (tag == SEPARATOR);
00081
00082 return readLabel(IStringStream(tag)());
00083 }
00084
00085
00086
00087 void readHeader(IFstream& is)
00088 {
00089 string line;
00090
00091 while (is.good())
00092 {
00093 is.getLine(line);
00094
00095 if (isSeparator(line))
00096 {
00097 break;
00098 }
00099 else
00100 {
00101 Sout<< line << endl;
00102 }
00103 }
00104 }
00105
00106
00107
00108 void skipSection(IFstream& is)
00109 {
00110 Sout<< "Skipping section at line " << is.lineNumber() << '.' << endl;
00111
00112 string line;
00113
00114 while (is.good())
00115 {
00116 is.getLine(line);
00117
00118 if (isSeparator(line))
00119 {
00120 break;
00121 }
00122 else
00123 {
00124
00125 }
00126 }
00127 }
00128
00129
00130 scalar readUnvScalar(const string& unvString)
00131 {
00132 string s(unvString);
00133
00134 s.replaceAll("d", "E");
00135 s.replaceAll("D", "E");
00136
00137 return readScalar(IStringStream(s)());
00138 }
00139
00140
00141
00142 void readUnits
00143 (
00144 IFstream& is,
00145 scalar& lengthScale,
00146 scalar& forceScale,
00147 scalar& tempScale,
00148 scalar& tempOffset
00149 )
00150 {
00151 Sout<< "Starting reading units at line " << is.lineNumber() << '.' << endl;
00152
00153 string line;
00154 is.getLine(line);
00155
00156 label l = readLabel(IStringStream(line.substr(0, 10))());
00157 Sout<< "l:" << l << endl;
00158
00159 string units(line.substr(10, 20));
00160 Sout<< "units:" << units << endl;
00161
00162 label unitType = readLabel(IStringStream(line.substr(30, 10))());
00163 Sout<< "unitType:" << unitType << endl;
00164
00165
00166 is.getLine(line);
00167
00168 lengthScale = readUnvScalar(line.substr(0, 25));
00169 forceScale = readUnvScalar(line.substr(25, 25));
00170 tempScale = readUnvScalar(line.substr(50, 25));
00171
00172 is.getLine(line);
00173 tempOffset = readUnvScalar(line.substr(0, 25));
00174
00175 Sout<< "Unit factors:" << nl
00176 << " Length scale : " << lengthScale << nl
00177 << " Force scale : " << forceScale << nl
00178 << " Temperature scale : " << tempScale << nl
00179 << " Temperature offset : " << tempOffset << nl
00180 << endl;
00181 }
00182
00183
00184
00185 void readPoints
00186 (
00187 IFstream& is,
00188 DynamicList<point>& points,
00189 DynamicList<label>& unvPointID
00190 )
00191 {
00192 Sout<< "Starting reading points at line " << is.lineNumber() << '.' << endl;
00193
00194 static bool hasWarned = false;
00195
00196 while (true)
00197 {
00198 string line;
00199 is.getLine(line);
00200
00201 label pointI = readLabel(IStringStream(line.substr(0, 10))());
00202
00203 if (pointI == -1)
00204 {
00205 break;
00206 }
00207 else if (pointI != points.size()+1 && !hasWarned)
00208 {
00209 hasWarned = true;
00210
00211 WarningIn
00212 (
00213 "readPoints(IFstream&, label&, DynamicList<point>"
00214 ", DynamicList<label>&)"
00215 ) << "Points not in order starting at point " << pointI
00216 << " at line " << is.lineNumber()
00217
00218 << endl;
00219 }
00220
00221 point pt;
00222 is.getLine(line);
00223 pt[0] = readUnvScalar(line.substr(0, 25));
00224 pt[1] = readUnvScalar(line.substr(25, 25));
00225 pt[2] = readUnvScalar(line.substr(50, 25));
00226
00227 unvPointID.append(pointI);
00228 points.append(pt);
00229 }
00230
00231 points.shrink();
00232 unvPointID.shrink();
00233
00234 Sout<< "Read " << points.size() << " points." << endl;
00235 }
00236
00237
00238
00239
00240
00241 void readCells
00242 (
00243 IFstream& is,
00244 DynamicList<cellShape>& cellVerts,
00245 DynamicList<label>& cellMaterial,
00246 DynamicList<label>& boundaryFaceIndices,
00247 DynamicList<face>& boundaryFaces
00248 )
00249 {
00250 Sout<< "Starting reading cells at line " << is.lineNumber() << '.' << endl;
00251
00252 const cellModel& hex = *(cellModeller::lookup("hex"));
00253 const cellModel& prism = *(cellModeller::lookup("prism"));
00254 const cellModel& tet = *(cellModeller::lookup("tet"));
00255
00256 labelHashSet skippedElements;
00257
00258 labelHashSet foundFeType;
00259
00260 while (true)
00261 {
00262 string line;
00263 is.getLine(line);
00264
00265 if (isSeparator(line))
00266 {
00267 break;
00268 }
00269
00270 IStringStream lineStr(line);
00271 label cellI, feID, physProp, matProp, colour, nNodes;
00272 lineStr >> cellI >> feID >> physProp >> matProp >> colour >> nNodes;
00273
00274 if (foundFeType.insert(feID))
00275 {
00276 Info<< "First occurrence of element type " << feID
00277 << " for cell " << cellI << " at line "
00278 << is.lineNumber() << endl;
00279 }
00280
00281 if (feID == 11)
00282 {
00283
00284 is.getLine(line);
00285 is.getLine(line);
00286 }
00287 else if (feID == 171)
00288 {
00289
00290 is.getLine(line);
00291 }
00292 else if (feID == 41 || feID == 91)
00293 {
00294
00295 is.getLine(line);
00296
00297 face cVerts(3);
00298 IStringStream lineStr(line);
00299 lineStr >> cVerts[0] >> cVerts[1] >> cVerts[2];
00300 boundaryFaces.append(cVerts);
00301 boundaryFaceIndices.append(cellI);
00302 }
00303 else if (feID == 44 || feID == 94)
00304 {
00305
00306 is.getLine(line);
00307
00308 face cVerts(4);
00309 IStringStream lineStr(line);
00310 lineStr >> cVerts[0] >> cVerts[1] >> cVerts[2] >> cVerts[3];
00311 boundaryFaces.append(cVerts);
00312 boundaryFaceIndices.append(cellI);
00313 }
00314 else if (feID == 111)
00315 {
00316
00317 is.getLine(line);
00318
00319 labelList cVerts(4);
00320 IStringStream lineStr(line);
00321 lineStr >> cVerts[0] >> cVerts[1] >> cVerts[2] >> cVerts[3];
00322
00323 cellVerts.append(cellShape(tet, cVerts, true));
00324 cellMaterial.append(physProp);
00325
00326 if (cellVerts[cellVerts.size()-1].size() != cVerts.size())
00327 {
00328 Pout<< "Line:" << is.lineNumber()
00329 << " element:" << cellI
00330 << " type:" << feID
00331 << " collapsed from " << cVerts << nl
00332 << " to:" << cellVerts[cellVerts.size()-1]
00333 << endl;
00334 }
00335 }
00336 else if (feID == 112)
00337 {
00338
00339 is.getLine(line);
00340
00341 labelList cVerts(6);
00342 IStringStream lineStr(line);
00343 lineStr >> cVerts[0] >> cVerts[1] >> cVerts[2] >> cVerts[3]
00344 >> cVerts[4] >> cVerts[5];
00345
00346 cellVerts.append(cellShape(prism, cVerts, true));
00347 cellMaterial.append(physProp);
00348
00349 if (cellVerts[cellVerts.size()-1].size() != cVerts.size())
00350 {
00351 Pout<< "Line:" << is.lineNumber()
00352 << " element:" << cellI
00353 << " type:" << feID
00354 << " collapsed from " << cVerts << nl
00355 << " to:" << cellVerts[cellVerts.size()-1]
00356 << endl;
00357 }
00358 }
00359 else if (feID == 115)
00360 {
00361
00362 is.getLine(line);
00363
00364 labelList cVerts(8);
00365 IStringStream lineStr(line);
00366 lineStr
00367 >> cVerts[0] >> cVerts[1] >> cVerts[2] >> cVerts[3]
00368 >> cVerts[4] >> cVerts[5] >> cVerts[6] >> cVerts[7];
00369
00370 cellVerts.append(cellShape(hex, cVerts, true));
00371 cellMaterial.append(physProp);
00372
00373 if (cellVerts[cellVerts.size()-1].size() != cVerts.size())
00374 {
00375 Pout<< "Line:" << is.lineNumber()
00376 << " element:" << cellI
00377 << " type:" << feID
00378 << " collapsed from " << cVerts << nl
00379 << " to:" << cellVerts[cellVerts.size()-1]
00380 << endl;
00381 }
00382 }
00383 else
00384 {
00385 if (skippedElements.insert(feID))
00386 {
00387 IOWarningIn("readCells(IFstream&, label&)", is)
00388 << "Cell type " << feID << " not supported" << endl;
00389 }
00390 is.getLine(line);
00391 }
00392 }
00393
00394 cellVerts.shrink();
00395 cellMaterial.shrink();
00396 boundaryFaces.shrink();
00397 boundaryFaceIndices.shrink();
00398
00399 Sout<< "Read " << cellVerts.size() << " cells"
00400 << " and " << boundaryFaces.size() << " boundary faces." << endl;
00401 }
00402
00403
00404 void readPatches
00405 (
00406 IFstream& is,
00407 DynamicList<word>& patchNames,
00408 DynamicList<labelList>& patchFaceIndices
00409 )
00410 {
00411 Sout<< "Starting reading patches at line " << is.lineNumber() << '.'
00412 << endl;
00413
00414 while(true)
00415 {
00416 string line;
00417 is.getLine(line);
00418
00419 if (isSeparator(line))
00420 {
00421 break;
00422 }
00423
00424 IStringStream lineStr(line);
00425 label group, constraintSet, restraintSet, loadSet, dofSet,
00426 tempSet, contactSet, nFaces;
00427 lineStr
00428 >> group >> constraintSet >> restraintSet >> loadSet
00429 >> dofSet >> tempSet >> contactSet >> nFaces;
00430
00431 is.getLine(line);
00432 patchNames.append(string::validate<word>(line));
00433
00434 Info<< "For facegroup " << group
00435 << " named " << patchNames[patchNames.size()-1]
00436 << " trying to read " << nFaces << " patch face indices."
00437 << endl;
00438
00439 patchFaceIndices.append(labelList(0));
00440 labelList& faceIndices = patchFaceIndices[patchFaceIndices.size()-1];
00441 faceIndices.setSize(nFaces);
00442 label faceI = 0;
00443
00444 while (faceI < faceIndices.size())
00445 {
00446 is.getLine(line);
00447 IStringStream lineStr(line);
00448
00449
00450 label nRead = 2;
00451 if (faceI == faceIndices.size()-1)
00452 {
00453 nRead = 1;
00454 }
00455
00456 for (label i = 0; i < nRead; i++)
00457 {
00458 label typeCode, tag, nodeLeaf, component;
00459
00460 lineStr >> typeCode >> tag >> nodeLeaf >> component;
00461
00462 if (typeCode != 8)
00463 {
00464 FatalErrorIn("readPatches")
00465 << "When reading patches expect Entity Type Code 8"
00466 << nl << "At line " << is.lineNumber()
00467 << exit(FatalError);
00468 }
00469
00470 faceIndices[faceI++] = tag;
00471 }
00472 }
00473 }
00474
00475 patchNames.shrink();
00476 patchFaceIndices.shrink();
00477 }
00478
00479
00480
00481
00482 void readDOFS
00483 (
00484 IFstream& is,
00485 DynamicList<word>& patchNames,
00486 DynamicList<labelList>& dofVertices
00487 )
00488 {
00489 Sout<< "Starting reading contraints at line " << is.lineNumber() << '.'
00490 << endl;
00491
00492 string line;
00493 is.getLine(line);
00494 label group;
00495 {
00496 IStringStream lineStr(line);
00497 lineStr >> group;
00498 }
00499
00500 is.getLine(line);
00501 {
00502 IStringStream lineStr(line);
00503 patchNames.append(lineStr);
00504 }
00505
00506 Info<< "For DOF set " << group
00507 << " named " << patchNames[patchNames.size()-1]
00508 << " trying to read vertex indices."
00509 << endl;
00510
00511 DynamicList<label> vertices;
00512 while (true)
00513 {
00514 string line;
00515 is.getLine(line);
00516
00517 if (isSeparator(line))
00518 {
00519 break;
00520 }
00521
00522 IStringStream lineStr(line);
00523 label pointI;
00524 lineStr >> pointI;
00525
00526 vertices.append(pointI);
00527 }
00528
00529 Info<< "For DOF set " << group
00530 << " named " << patchNames[patchNames.size()-1]
00531 << " read " << vertices.size() << " vertex indices." << endl;
00532
00533 dofVertices.append(vertices.shrink());
00534
00535 patchNames.shrink();
00536 dofVertices.shrink();
00537 }
00538
00539
00540
00541 label findPatch(const List<labelHashSet>& dofGroups, const face& f)
00542 {
00543 forAll(dofGroups, patchI)
00544 {
00545 if (dofGroups[patchI].found(f[0]))
00546 {
00547 bool allInGroup = true;
00548
00549
00550 for (label fp = 1; fp < f.size(); fp++)
00551 {
00552 if (!dofGroups[patchI].found(f[fp]))
00553 {
00554 allInGroup = false;
00555 break;
00556 }
00557 }
00558
00559 if (allInGroup)
00560 {
00561 return patchI;
00562 }
00563 }
00564 }
00565 return -1;
00566 }
00567
00568
00569
00570
00571 int main(int argc, char *argv[])
00572 {
00573 argList::noParallel();
00574 argList::validArgs.append(".unv file");
00575 argList::validOptions.insert("dump", "");
00576
00577 # include "setRootCase.H"
00578 # include "createTime.H"
00579
00580 fileName ideasName(args.additionalArgs()[0]);
00581
00582 IFstream inFile(ideasName.c_str());
00583
00584 if (!inFile.good())
00585 {
00586 FatalErrorIn(args.executable())
00587 << "Cannot open file " << ideasName
00588 << exit(FatalError);
00589 }
00590
00591
00592
00593 scalar lengthScale = 1;
00594 scalar forceScale = 1;
00595 scalar tempScale = 1;
00596 scalar tempOffset = 0;
00597
00598
00599 DynamicList<point> points;
00600
00601 DynamicList<label> unvPointID;
00602
00603
00604 DynamicList<cellShape> cellVerts;
00605 DynamicList<label> cellMat;
00606
00607
00608 DynamicList<label> boundaryFaceIndices;
00609 DynamicList<face> boundaryFaces;
00610
00611
00612 DynamicList<word> patchNames;
00613 DynamicList<labelList> patchFaceIndices;
00614 DynamicList<labelList> dofVertIndices;
00615
00616
00617 while (inFile.good())
00618 {
00619 label tag = readTag(inFile);
00620
00621 if (tag == -1)
00622 {
00623 break;
00624 }
00625
00626 Sout<< "Processing tag:" << tag << endl;
00627
00628 switch (tag)
00629 {
00630 case 151:
00631 readHeader(inFile);
00632 break;
00633
00634 case 164:
00635 readUnits
00636 (
00637 inFile,
00638 lengthScale,
00639 forceScale,
00640 tempScale,
00641 tempOffset
00642 );
00643 break;
00644
00645 case 2411:
00646 readPoints(inFile, points, unvPointID);
00647 break;
00648
00649 case 2412:
00650 readCells
00651 (
00652 inFile,
00653 cellVerts,
00654 cellMat,
00655 boundaryFaceIndices,
00656 boundaryFaces
00657 );
00658 break;
00659
00660 case 2452:
00661 case 2467:
00662 readPatches
00663 (
00664 inFile,
00665 patchNames,
00666 patchFaceIndices
00667 );
00668 break;
00669
00670 case 757:
00671 readDOFS
00672 (
00673 inFile,
00674 patchNames,
00675 dofVertIndices
00676 );
00677 break;
00678
00679 default:
00680 Sout<< "Skipping tag " << tag << " on line "
00681 << inFile.lineNumber() << endl;
00682 skipSection(inFile);
00683 break;
00684 }
00685 Sout<< endl;
00686 }
00687
00688
00689
00690 label maxUnvPoint = 0;
00691 forAll(unvPointID, pointI)
00692 {
00693 maxUnvPoint = max(maxUnvPoint, unvPointID[pointI]);
00694 }
00695 labelList unvToFoam(invert(maxUnvPoint+1, unvPointID));
00696
00697
00698
00699
00700 forAll(cellVerts, cellI)
00701 {
00702 labelList foamVerts
00703 (
00704 renumber
00705 (
00706 unvToFoam,
00707 static_cast<labelList&>(cellVerts[cellI])
00708 )
00709 );
00710
00711 if (findIndex(foamVerts, -1) != -1)
00712 {
00713 FatalErrorIn(args.executable())
00714 << "Cell " << cellI
00715 << " unv vertices " << cellVerts[cellI]
00716 << " has some undefined vertices " << foamVerts
00717 << abort(FatalError);
00718 }
00719
00720
00721 cellVerts[cellI].transfer(foamVerts);
00722 }
00723
00724
00725
00726 forAll(boundaryFaces, bFaceI)
00727 {
00728 labelList foamVerts(renumber(unvToFoam, boundaryFaces[bFaceI]));
00729
00730 if (findIndex(foamVerts, -1) != -1)
00731 {
00732 FatalErrorIn(args.executable())
00733 << "Boundary face " << bFaceI
00734 << " unv vertices " << boundaryFaces[bFaceI]
00735 << " has some undefined vertices " << foamVerts
00736 << abort(FatalError);
00737 }
00738
00739
00740 boundaryFaces[bFaceI].transfer(foamVerts);
00741 }
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752 List<faceList> patchFaceVerts;
00753
00754
00755 if (dofVertIndices.size())
00756 {
00757
00758
00759
00760
00761
00762
00763 Info<< "Using " << dofVertIndices.size()
00764 << " DOF sets to detect boundary faces."<< endl;
00765
00766
00767 forAll(dofVertIndices, patchI)
00768 {
00769 inplaceRenumber(unvToFoam, dofVertIndices[patchI]);
00770 }
00771
00772
00773
00774 List<labelHashSet> dofGroups(dofVertIndices.size());
00775
00776 forAll(dofVertIndices, patchI)
00777 {
00778 const labelList& foamVerts = dofVertIndices[patchI];
00779
00780 forAll(foamVerts, i)
00781 {
00782 dofGroups[patchI].insert(foamVerts[i]);
00783 }
00784 }
00785
00786 List<DynamicList<face> > dynPatchFaces(dofVertIndices.size());
00787
00788 forAll(cellVerts, cellI)
00789 {
00790 const cellShape& shape = cellVerts[cellI];
00791
00792 const faceList shapeFaces(shape.faces());
00793
00794 forAll(shapeFaces, i)
00795 {
00796 label patchI = findPatch(dofGroups, shapeFaces[i]);
00797
00798 if (patchI != -1)
00799 {
00800 dynPatchFaces[patchI].append(shapeFaces[i]);
00801 }
00802 }
00803 }
00804
00805
00806 patchFaceVerts.setSize(dynPatchFaces.size());
00807
00808 forAll(dynPatchFaces, patchI)
00809 {
00810 patchFaceVerts[patchI].transfer(dynPatchFaces[patchI]);
00811 }
00812 }
00813 else
00814 {
00815
00816
00817
00818
00819 patchFaceVerts.setSize(patchFaceIndices.size());
00820
00821 Info<< "Sorting boundary faces according to group (patch)" << endl;
00822
00823
00824 Map<label> boundaryFaceToIndex(boundaryFaceIndices.size());
00825
00826 forAll(boundaryFaceIndices, i)
00827 {
00828 boundaryFaceToIndex.insert(boundaryFaceIndices[i], i);
00829 }
00830
00831 forAll(patchFaceVerts, patchI)
00832 {
00833 faceList& patchFaces = patchFaceVerts[patchI];
00834 const labelList& faceIndices = patchFaceIndices[patchI];
00835
00836 patchFaces.setSize(faceIndices.size());
00837
00838 forAll(patchFaces, i)
00839 {
00840 label bFaceI = boundaryFaceToIndex[faceIndices[i]];
00841
00842 patchFaces[i] = boundaryFaces[bFaceI];
00843 }
00844 }
00845 }
00846
00847 pointField polyPoints;
00848 polyPoints.transfer(points);
00849
00850
00851 polyPoints /= lengthScale;
00852
00853
00854
00855 if (args.optionFound("dump"))
00856 {
00857 DynamicList<labelledTri> triangles(boundaryFaces.size());
00858
00859 forAll(boundaryFaces, i)
00860 {
00861 const face& f = boundaryFaces[i];
00862
00863 faceList triFaces(f.nTriangles(polyPoints));
00864 label nTri = 0;
00865 f.triangles(polyPoints, nTri, triFaces);
00866
00867 forAll(triFaces, triFaceI)
00868 {
00869 const face& f = triFaces[triFaceI];
00870 triangles.append(labelledTri(f[0], f[1], f[2], 0));
00871 }
00872 }
00873
00874
00875 triSurface rawSurface(triangles.shrink(), polyPoints);
00876
00877
00878 triSurface surface
00879 (
00880 rawSurface.localFaces(),
00881 rawSurface.localPoints()
00882 );
00883
00884 Info<< "Writing boundary faces to STL file boundaryFaces.stl"
00885 << nl << endl;
00886
00887 surface.write(runTime.path()/"boundaryFaces.stl");
00888 }
00889
00890
00891 Info<< "Constructing mesh with non-default patches of size:" << nl;
00892 forAll(patchNames, patchI)
00893 {
00894 Info<< " " << patchNames[patchI] << '\t'
00895 << patchFaceVerts[patchI].size() << nl;
00896 }
00897 Info<< endl;
00898
00899
00900
00901
00902 polyMesh mesh
00903 (
00904 IOobject
00905 (
00906 polyMesh::defaultRegion,
00907 runTime.constant(),
00908 runTime
00909 ),
00910 xferMove(polyPoints),
00911 cellVerts,
00912 patchFaceVerts,
00913 patchNames,
00914 wordList(patchNames.size(), polyPatch::typeName),
00915 "defaultFaces",
00916 polyPatch::typeName,
00917 wordList(0)
00918 );
00919
00920 mesh.write();
00921
00922 Info<< "End\n" << endl;
00923
00924 return 0;
00925 }
00926
00927
00928