68 #include <libccmio/ccmio.h>
75 static char const kDefaultState[] =
"default";
76 static int const kVertOffset = 2;
100 label faceI = cFaces[i];
102 label nbrCellI = neighbour[faceI];
107 if (nbrCellI == cellI)
109 nbrCellI = owner[faceI];
112 if (cellI < nbrCellI)
136 oldToNew[cFaces[nbr.indices()[i]]] = newFaceI++;
142 for (label faceI = newFaceI; faceI < owner.
size(); faceI++)
144 oldToNew[faceI] = faceI;
154 const label cellType,
163 if (zoneFnd == typeToZone.
end())
166 zoneCells.setSize(zoneCells.size() + 1);
168 label zoneI = zoneCells.size()-1;
170 Info<<
"Mapping type " << cellType <<
" to Foam cellZone "
172 typeToZone.
insert(cellType, zoneI);
174 zoneCells[zoneI].append(cellI);
179 zoneCells[zoneFnd()].append(cellI);
185 void CheckError(CCMIOError
const &err,
const Foam::string& str)
187 if (err != kCCMIONoErr)
211 CCMIOEntitySize(&err, vertices, &nVertices, NULL);
217 int offsetPlusSize = offset+nVertices;
222 CCMIOReadVerticesf(&err, vertices, &dims, &scale, &mapID, verts.begin(),
223 offset, offsetPlusSize);
224 CCMIOReadMap(&err, mapID, mapData.begin(), offset, offsetPlusSize);
236 foamPointMap.
setSize(nVertices);
240 foamPointMap[i] = mapData[i];
241 for (
direction cmpt = 0; cmpt < dims; cmpt++)
243 foamPoints[i][cmpt] = verts[dims*i + cmpt]*scale;
265 CCMIONextEntity(NULL, problem, kCCMIOCellType, &i, &next)
280 CCMIOReadOptstr(NULL, next,
"MaterialType", &size, NULL)
284 name =
new char[size + 1];
285 CCMIOReadOptstr(&err, next,
"MaterialType", &size, name);
286 CCMIOGetEntityIndex(&err, next, &cellType);
288 foamCellTypeNames.
insert(cellType, name);
289 Pout<<
"Celltype:" << cellType <<
" name:" << name <<
endl;
303 CCMIONextEntity(NULL, problem, kCCMIOBoundaryRegion, &k, &boundary)
308 label foamPatchI = -1;
315 CCMIOReadOpti(NULL, boundary,
"ProstarRegionNumber", &prostarI)
319 Pout<<
"For region:" << regionI
320 <<
" found ProstarRegionNumber:" << prostarI <<
endl;
326 Pout<<
"For region:" << regionI
327 <<
"did not find ProstarRegionNumber entry. Assuming "
332 if (prostarToFoamPatch.
found(prostarI))
334 foamPatchI = prostarToFoamPatch[prostarI];
341 CCMIOReadOptstr(NULL, boundary,
"BoundaryType", &size, NULL)
345 char* s =
new char[size + 1];
346 CCMIOReadOptstr(NULL, boundary,
"BoundaryType", &size, s);
348 foamPatchTypes[foamPatchI] = string::validate<word>(
string(s));
363 CCMIOReadOptstr(NULL, boundary,
"BoundaryName", &size, NULL)
367 char* name =
new char[size + 1];
368 CCMIOReadOptstr(NULL, boundary,
"BoundaryName", &size, name);
370 foamPatchNames[foamPatchI] = string::validate<word>(
string(name));
375 CCMIOReadOptstr(NULL, boundary,
"Label", &size, NULL)
379 char* name =
new char[size + 1];
380 CCMIOReadOptstr(NULL, boundary,
"Label", &size, name);
382 foamPatchNames[foamPatchI] = string::validate<word>(
string(name));
387 foamPatchNames[foamPatchI] =
388 foamPatchTypes[foamPatchI]
390 Pout<<
"Made up name:" << foamPatchNames[foamPatchI]
394 Pout<<
"Read patch:" << foamPatchI
395 <<
" name:" << foamPatchNames[foamPatchI]
396 <<
" foamPatchTypes:" << foamPatchTypes[foamPatchI]
426 CCMIOGetEntity(&err, topology, kCCMIOCells, 0, &
id);
428 CCMIOEntitySize(&err,
id, &nCells, NULL);
430 std::vector<int> mapData(nCells);
431 std::vector<int> cellType(nCells);
434 CCMIOReadCells(&err,
id, &mapID, &cellType[0], 0, nCells);
435 CCMIOReadMap(&err, mapID, &mapData[0], 0, nCells);
436 CheckError(err,
"Error reading cells");
442 foamCellMap[i] = mapData[i];
443 foamCellType[i] = cellType[i];
450 CCMIOGetEntity(&err, topology, kCCMIOInternalFaces, 0, &
id);
451 CCMIOSize nInternalFaces;
452 CCMIOEntitySize(&err,
id, &nInternalFaces, NULL);
453 Pout<<
"nInternalFaces:" << nInternalFaces <<
endl;
456 label foamNFaces = nInternalFaces;
460 CCMIONextEntity(NULL, topology, kCCMIOBoundaryFaces, &index, &
id)
465 CCMIOEntitySize(&err,
id, &size, NULL);
467 Pout<<
"Read kCCMIOBoundaryFaces entry with " << size
468 <<
" faces." <<
endl;
470 foamPatchStarts.
append(foamNFaces);
471 foamPatchSizes.
append(size);
477 Pout<<
"patchSizes:" << foamPatchSizes <<
endl;
478 Pout<<
"patchStarts:" << foamPatchStarts <<
endl;
479 Pout<<
"nFaces:" << foamNFaces <<
endl;
481 mapData.resize(nInternalFaces);
482 CCMIOGetEntity(&err, topology, kCCMIOInternalFaces, 0, &
id);
484 CCMIOReadFaces(&err,
id, kCCMIOInternalFaces, NULL, &size, NULL,
485 kCCMIOStart, kCCMIOEnd);
486 std::vector<int> faces(size);
487 CCMIOReadFaces(&err,
id, kCCMIOInternalFaces, &mapID, NULL, &faces[0],
488 kCCMIOStart, kCCMIOEnd);
489 std::vector<int> faceCells(2*nInternalFaces);
490 CCMIOReadFaceCells(&err,
id, kCCMIOInternalFaces, &faceCells[0],
491 kCCMIOStart, kCCMIOEnd);
492 CCMIOReadMap(&err, mapID, &mapData[0], kCCMIOStart, kCCMIOEnd);
493 CheckError(err,
"Error reading internal faces");
496 foamFaceMap.
setSize(foamNFaces);
499 foamNeighbour.
setSize(foamNFaces);
501 unsigned int pos = 0;
503 for (
unsigned int faceI = 0; faceI < nInternalFaces; faceI++)
505 foamFaceMap[faceI] = mapData[faceI];
506 foamOwner[faceI] = faceCells[2*faceI];
507 foamNeighbour[faceI] = faceCells[2*faceI+1];
508 face&
f = foamFaces[faceI];
513 f[fp] = faces[pos++];
524 CCMIONextEntity(NULL, topology, kCCMIOBoundaryFaces, &index, &
id)
529 CCMIOEntitySize(&err,
id, &nFaces, NULL);
531 mapData.resize(nFaces);
532 faceCells.resize(nFaces);
533 CCMIOReadFaces(&err,
id, kCCMIOBoundaryFaces, NULL, &size, NULL,
534 kCCMIOStart, kCCMIOEnd);
536 CCMIOReadFaces(&err,
id, kCCMIOBoundaryFaces, &mapID, NULL, &faces[0],
537 kCCMIOStart, kCCMIOEnd);
538 CCMIOReadFaceCells(&err,
id, kCCMIOBoundaryFaces, &faceCells[0],
539 kCCMIOStart, kCCMIOEnd);
540 CCMIOReadMap(&err, mapID, &mapData[0], kCCMIOStart, kCCMIOEnd);
541 CheckError(err,
"Error reading boundary faces");
547 CCMIOReadOpti(NULL,
id,
"ProstarRegionNumber", &prostarI)
551 Pout<<
"For region:" << regionI
552 <<
" found ProstarRegionNumber:" << prostarI <<
endl;
558 Pout<<
"For region:" << regionI
559 <<
" did not find ProstarRegionNumber entry. Assuming "
562 prostarToFoamPatch.
insert(prostarI, regionI);
565 Pout<<
"region:" << regionI
566 <<
" ProstarRegionNumber:" << prostarI
567 <<
" foamPatchStart:"
568 << foamPatchStarts[regionI]
570 << foamPatchSizes[regionI]
574 unsigned int pos = 0;
576 for (
unsigned int i = 0; i < nFaces; i++)
578 label foamFaceI = foamPatchStarts[regionI] + i;
580 foamFaceMap[foamFaceI] = mapData[i];
581 foamOwner[foamFaceI] = faceCells[i];
582 foamNeighbour[foamFaceI] = -1;
583 face& f = foamFaces[foamFaceI];
588 f[fp] = faces[pos++];
600 int main(
int argc,
char *argv[])
642 <<
"Cannot read file " << ccmFile
646 word ccmExt = ccmFile.ext();
648 if (ccmExt !=
"ccm" && ccmExt !=
"ccmg")
651 <<
"Illegal extension " << ccmExt <<
" for file " << ccmFile
652 <<
nl <<
"Allowed extensions are '.ccm', '.ccmg'"
660 CCMIOError err = CCMIOOpenFile(NULL, ccmFile.c_str(), kCCMIORead, &root);
667 CCMIONextEntity(&err, root, kCCMIOState, &stateI, &state);
668 CheckError(err,
"Error opening state");
671 CCMIOEntityDescription(&err, state, &size, NULL);
672 char *desc =
new char[size + 1];
673 CCMIOEntityDescription(&err, state, NULL, desc);
674 Pout<<
"Reading state '" << kDefaultState <<
"' (" << desc <<
")"
682 CCMIONextEntity(&err, state, kCCMIOProcessor, &i, &processor);
683 CCMIOID
solution, vertices, topology;
694 if (err != kCCMIONoErr)
707 if (err != kCCMIONoErr)
710 <<
"Could not read the file."
715 ReadVertices(err, vertices, foamPointMap, foamPoints);
717 Pout<<
"nPoints:" << foamPoints.
size() << endl
718 <<
"bounding box:" <<
boundBox(foamPoints) << endl
736 Pout<<
"nCells:" << foamCellMap.
size() << endl
737 <<
"nFaces:" << foamOwner.
size() << endl
738 <<
"nPatches:" << foamPatchStarts.
size() << endl
739 <<
"nInternalFaces:" << foamPatchStarts[0] << endl
750 foamPatchTypes[i] =
"patch";
761 kCCMIOProblemDescription,
765 CheckError(err,
"Error stepping to first problem description");
767 if (CCMIOIsValidEntity(problem))
782 CCMIOCloseFile(&err, vertices);
783 CCMIOCloseFile(&err, topology);
784 CCMIOCloseFile(&err, solution);
785 CCMIOCloseFile(&err, root);
789 Pout<<
"foamPatchNames:" << foamPatchNames <<
endl;
792 Pout<<
"foamOwner : min:" <<
min(foamOwner)
793 <<
" max:" <<
max(foamOwner)
795 <<
"foamNeighbour : min:" <<
min(foamNeighbour)
796 <<
" max:" <<
max(foamNeighbour)
798 <<
"foamCellType : min:" <<
min(foamCellType)
799 <<
" max:" <<
max(foamCellType)
813 label maxCCMPointI =
max(foamPointMap);
824 label maxCCMCellI =
max(foamCellMap);
843 forAll(foamNeighbour, faceI)
845 label nbr = foamNeighbour[faceI];
846 label own = foamOwner[faceI];
848 if (nbr >= foamCellType.
size() || own >= foamCellType.
size())
854 <<
" nCells:" << foamCellType.
size()
862 foamOwner[faceI] = foamNeighbour[faceI];
863 foamNeighbour[faceI] = own;
864 foamFaces[faceI] = foamFaces[faceI].reverseFace();
870 const face& f = foamFaces[faceI];
874 if (f[fp] < 0 || f[fp] >= foamPoints.
size())
889 primitiveMesh::calcCells
932 xferMove<pointField>(foamPoints),
933 xferMove<faceList>(foamFaces),
934 xferCopy<labelList>(foamOwner),
935 xferMove<labelList>(foamNeighbour)
941 label meshFaceI = foamPatchStarts[0];
943 forAll(newPatches, patchI)
945 const word& patchName = foamPatchNames[patchI];
946 const word& patchType = foamPatchTypes[patchI];
948 Pout<<
"Patch:" << patchName <<
" start at:" << meshFaceI
949 <<
" size:" << foamPatchSizes[patchI]
950 <<
" end at:" << meshFaceI+foamPatchSizes[patchI]
953 if (patchType ==
"wall")
959 foamPatchSizes[patchI],
965 else if (patchType ==
"symmetryplane")
971 foamPatchSizes[patchI],
977 else if (patchType ==
"empty")
984 foamPatchSizes[patchI],
998 foamPatchSizes[patchI],
1005 meshFaceI += foamPatchSizes[patchI];
1008 if (meshFaceI != foamOwner.
size())
1011 <<
"meshFaceI:" << meshFaceI
1012 <<
" nFaces:" << foamOwner.
size()
1022 label maxType =
max(foamCellType);
1023 label minType =
min(foamCellType);
1025 if (maxType > minType)
1032 forAll(foamCellType, cellI)
1037 foamCellType[cellI],
1046 label nValidCellZones = 0;
1050 label
type = iter.key();
1051 label zoneI = iter();
1053 zoneCells[zoneI].shrink();
1055 word zoneName =
"cellZone_" +
name(type);
1057 Info<<
"Writing zone " << type
1058 <<
" size " << zoneCells[zoneI].size()
1060 << zoneName <<
" and cellSet " << zoneName
1083 Info<<
"Writing mesh to " <<
mesh.objectRegistry::objectPath()
1102 forAll(foamCellMap, cellI)
1104 cellIdField[cellI] = foamCellMap[cellI];
1122 forAll(foamCellType, cellI)
1124 cellTypeField[cellI] = foamCellType[cellI];
1127 Info<<
"Writing cellIds as volScalarField to " << cellIdField.objectPath()