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
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #include "argList.H"
00064 #include "Time.H"
00065 #include "polyMesh.H"
00066 #include "IFstream.H"
00067 #include "cellModeller.H"
00068
00069 using namespace Foam;
00070
00071
00072
00073
00074 label findFace(const primitiveMesh& mesh, const face& f)
00075 {
00076 const labelList& pFaces = mesh.pointFaces()[f[0]];
00077
00078 forAll(pFaces, i)
00079 {
00080 label faceI = pFaces[i];
00081
00082 if (mesh.faces()[faceI] == f)
00083 {
00084 return faceI;
00085 }
00086 }
00087
00088 FatalErrorIn("findFace(const primitiveMesh&, const face&)")
00089 << "Cannot find face " << f << " in mesh." << abort(FatalError);
00090
00091 return -1;
00092 }
00093
00094
00095
00096
00097 int main(int argc, char *argv[])
00098 {
00099 argList::validArgs.append("file prefix");
00100 argList::validOptions.insert("noFaceFile", "");
00101
00102 # include "setRootCase.H"
00103 # include "createTime.H"
00104
00105
00106 bool readFaceFile = !args.optionFound("noFaceFile");
00107
00108 fileName prefix(args.additionalArgs()[0]);
00109
00110 fileName nodeFile(prefix + ".node");
00111 fileName eleFile(prefix + ".ele");
00112 fileName faceFile(prefix + ".face");
00113
00114 if (!readFaceFile)
00115 {
00116 Info<< "Files:" << endl
00117 << " nodes : " << nodeFile << endl
00118 << " elems : " << eleFile << endl
00119 << endl;
00120 }
00121 else
00122 {
00123 Info<< "Files:" << endl
00124 << " nodes : " << nodeFile << endl
00125 << " elems : " << eleFile << endl
00126 << " faces : " << faceFile << endl
00127 << endl;
00128
00129 Info<< "Reading .face file for boundary information" << nl << endl;
00130 }
00131
00132 if (!isFile(nodeFile) || !isFile(eleFile))
00133 {
00134 FatalErrorIn(args.executable())
00135 << "Cannot read " << nodeFile << " or " << eleFile
00136 << exit(FatalError);
00137 }
00138
00139 if (readFaceFile && !isFile(faceFile))
00140 {
00141 FatalErrorIn(args.executable())
00142 << "Cannot read " << faceFile << endl
00143 << "Did you run tetgen with -f option?" << endl
00144 << "If you don't want to read the .face file and thus not have"
00145 << " patches please\nrerun with the -noFaceFile option"
00146 << exit(FatalError);
00147 }
00148
00149
00150 IFstream nodeStream(nodeFile);
00151
00152
00153
00154
00155
00156
00157 string line;
00158
00159 do
00160 {
00161 nodeStream.getLine(line);
00162 }
00163 while (line.size() && line[0] == '#');
00164
00165 IStringStream nodeLine(line);
00166
00167 label nNodes, nDims, nNodeAttr;
00168 bool hasRegion;
00169
00170 nodeLine >> nNodes >> nDims >> nNodeAttr >> hasRegion;
00171
00172
00173 Info<< "Read .node header:" << endl
00174 << " nodes : " << nNodes << endl
00175 << " nDims : " << nDims << endl
00176 << " nAttr : " << nNodeAttr << endl
00177 << " hasRegion : " << hasRegion << endl
00178 << endl;
00179
00180
00181
00182
00183
00184 pointField points(nNodes);
00185 Map<label> nodeToPoint(nNodes);
00186
00187 {
00188 labelList pointIndex(nNodes);
00189
00190 label pointI = 0;
00191
00192 while (nodeStream.good())
00193 {
00194 nodeStream.getLine(line);
00195
00196 if (line.size() && line[0] != '#')
00197 {
00198 IStringStream nodeLine(line);
00199
00200 label nodeI;
00201 scalar x, y, z;
00202 label dummy;
00203
00204 nodeLine >> nodeI >> x >> y >> z;
00205
00206 for (label i = 0; i < nNodeAttr; i++)
00207 {
00208 nodeLine >> dummy;
00209 }
00210
00211 if (hasRegion)
00212 {
00213 nodeLine >> dummy;
00214 }
00215
00216
00217 points[pointI] = point(x, y, z);
00218 nodeToPoint.insert(nodeI, pointI);
00219 pointI++;
00220 }
00221 }
00222 if (pointI != nNodes)
00223 {
00224 FatalIOErrorIn(args.executable().c_str(), nodeStream)
00225 << "Only " << pointI << " nodes present instead of " << nNodes
00226 << " from header." << exit(FatalIOError);
00227 }
00228 }
00229
00230
00231
00232
00233
00234 IFstream eleStream(eleFile);
00235
00236 do
00237 {
00238 eleStream.getLine(line);
00239 }
00240 while (line.size() && line[0] == '#');
00241
00242 IStringStream eleLine(line);
00243
00244 label nTets, nPtsPerTet, nElemAttr;
00245
00246 eleLine >> nTets >> nPtsPerTet >> nElemAttr;
00247
00248
00249 Info<< "Read .ele header:" << endl
00250 << " tets : " << nTets << endl
00251 << " pointsPerTet : " << nPtsPerTet << endl
00252 << " nAttr : " << nElemAttr << endl
00253 << endl;
00254
00255 if (nPtsPerTet != 4)
00256 {
00257 FatalIOErrorIn(args.executable().c_str(), eleStream)
00258 << "Cannot handle tets with "
00259 << nPtsPerTet << " points per tetrahedron in .ele file" << endl
00260 << "Can only handle tetrahedra with four points"
00261 << exit(FatalIOError);
00262 }
00263
00264 if (nElemAttr != 0)
00265 {
00266 WarningIn(args.executable())
00267 << "Element attributes (third elemenent in .ele header)"
00268 << " not used" << endl;
00269 }
00270
00271
00272
00273 const cellModel& tet = *(cellModeller::lookup("tet"));
00274
00275 labelList tetPoints(4);
00276
00277 cellShapeList cells(nTets);
00278 label cellI = 0;
00279
00280 while (eleStream.good())
00281 {
00282 eleStream.getLine(line);
00283
00284 if (line.size() && line[0] != '#')
00285 {
00286 IStringStream eleLine(line);
00287
00288 label elemI;
00289 eleLine >> elemI;
00290
00291 for (label i = 0; i < 4; i++)
00292 {
00293 label nodeI;
00294 eleLine >> nodeI;
00295 tetPoints[i] = nodeToPoint[nodeI];
00296 }
00297
00298 cells[cellI++] = cellShape(tet, tetPoints);
00299
00300
00301 for (label i = 0; i < nElemAttr; i++)
00302 {
00303 label dummy;
00304
00305 eleLine >> dummy;
00306 }
00307 }
00308 }
00309
00310
00311
00312
00313
00314
00315 autoPtr<polyMesh> meshPtr
00316 (
00317 new polyMesh
00318 (
00319 IOobject
00320 (
00321 polyMesh::defaultRegion,
00322 runTime.constant(),
00323 runTime
00324 ),
00325 xferCopy(points),
00326 cells,
00327 faceListList(0),
00328 wordList(0),
00329 wordList(0),
00330 "defaultFaces",
00331 polyPatch::typeName,
00332 wordList(0)
00333 )
00334 );
00335 const polyMesh& mesh = meshPtr;
00336
00337
00338
00339 if (readFaceFile)
00340 {
00341 label nPatches = 0;
00342
00343
00344 faceList boundaryFaces;
00345
00346
00347 labelList boundaryPatch;
00348
00349
00350
00351
00352
00353 IFstream faceStream(faceFile);
00354
00355 do
00356 {
00357 faceStream.getLine(line);
00358 }
00359 while (line.size() && line[0] == '#');
00360
00361 IStringStream faceLine(line);
00362
00363 label nFaces, nFaceAttr;
00364
00365 faceLine >> nFaces >> nFaceAttr;
00366
00367
00368 Info<< "Read .face header:" << endl
00369 << " faces : " << nFaces << endl
00370 << " nAttr : " << nFaceAttr << endl
00371 << endl;
00372
00373
00374 if (nFaceAttr != 1)
00375 {
00376 FatalIOErrorIn(args.executable().c_str(), faceStream)
00377 << "Expect boundary markers to be"
00378 << " present in .face file." << endl
00379 << "This is the second number in the header which is now:"
00380 << nFaceAttr << exit(FatalIOError);
00381 }
00382
00383
00384 boundaryFaces.setSize(nFaces);
00385
00386
00387 boundaryPatch.setSize(nFaces);
00388 boundaryPatch = -1;
00389
00390 label faceI = 0;
00391
00392
00393 Map<label> regionToPatch;
00394
00395 face f(3);
00396
00397 while (faceStream.good())
00398 {
00399 faceStream.getLine(line);
00400
00401 if (line.size() && line[0] != '#')
00402 {
00403 IStringStream faceLine(line);
00404
00405 label tetGenFaceI, dummy, region;
00406
00407 faceLine >> tetGenFaceI;
00408
00409
00410
00411 for (label i = 0; i < 3; i++)
00412 {
00413 label nodeI;
00414 faceLine >> nodeI;
00415 f[2-i] = nodeToPoint[nodeI];
00416 }
00417
00418
00419 if (findFace(mesh, f) >= mesh.nInternalFaces())
00420 {
00421 boundaryFaces[faceI] = f;
00422
00423 if (nFaceAttr > 0)
00424 {
00425
00426 faceLine >> region;
00427
00428
00429
00430 label patchI = 0;
00431
00432 Map<label>::iterator patchFind =
00433 regionToPatch.find(region);
00434
00435 if (patchFind == regionToPatch.end())
00436 {
00437 patchI = nPatches;
00438
00439 Info<< "Mapping tetgen region " << region
00440 << " to Foam patch "
00441 << patchI << endl;
00442
00443 regionToPatch.insert(region, nPatches++);
00444 }
00445 else
00446 {
00447 patchI = patchFind();
00448 }
00449
00450 boundaryPatch[faceI] = patchI;
00451
00452
00453 for (label i = 1; i < nFaceAttr; i++)
00454 {
00455 faceLine >> dummy;
00456 }
00457 }
00458
00459 faceI++;
00460 }
00461 }
00462 }
00463
00464
00465
00466 boundaryFaces.setSize(faceI);
00467 boundaryPatch.setSize(faceI);
00468
00469
00470
00471 Info<< "Regions:" << endl;
00472
00473 for
00474 (
00475 Map<label>::const_iterator iter = regionToPatch.begin();
00476 iter != regionToPatch.end();
00477 ++iter
00478 )
00479 {
00480 Info<< " region:" << iter.key() << '\t' << "patch:"
00481 << iter() << endl;
00482 }
00483 Info<< endl;
00484
00485
00486
00487 faceListList patchFaces(nPatches);
00488 wordList patchNames(nPatches);
00489
00490 forAll(patchNames, patchI)
00491 {
00492 patchNames[patchI] = word("patch") + name(patchI);
00493 }
00494
00495 wordList patchTypes(nPatches, polyPatch::typeName);
00496 word defaultFacesName = "defaultFaces";
00497 word defaultFacesType = polyPatch::typeName;
00498 wordList patchPhysicalTypes(nPatches, polyPatch::typeName);
00499
00500
00501
00502 List<DynamicList<face> > allPatchFaces(nPatches);
00503
00504 forAll(boundaryPatch, faceI)
00505 {
00506 label patchI = boundaryPatch[faceI];
00507
00508 allPatchFaces[patchI].append(boundaryFaces[faceI]);
00509 }
00510
00511 Info<< "Patch sizes:" << endl;
00512
00513 forAll(allPatchFaces, patchI)
00514 {
00515 Info<< " " << patchNames[patchI] << " : "
00516 << allPatchFaces[patchI].size() << endl;
00517
00518 patchFaces[patchI].transfer(allPatchFaces[patchI]);
00519 }
00520
00521 Info<< endl;
00522
00523
00524 meshPtr.reset
00525 (
00526 new polyMesh
00527 (
00528 IOobject
00529 (
00530 polyMesh::defaultRegion,
00531 runTime.constant(),
00532 runTime
00533 ),
00534 xferMove(points),
00535 cells,
00536 patchFaces,
00537 patchNames,
00538 patchTypes,
00539 defaultFacesName,
00540 defaultFacesType,
00541 patchPhysicalTypes
00542 )
00543 );
00544 }
00545
00546
00547 Info<< "Writing mesh to " << runTime.constant() << endl << endl;
00548
00549 meshPtr().write();
00550
00551 Info<< "End\n" << endl;
00552
00553 return 0;
00554 }
00555
00556
00557