34 #include <boost/filesystem.hpp> 37 #include <cellModeller.H> 39 #include <foamVtkVtuAdaptor.H> 43 #include <vtkCellData.h> 44 #include <vtkCellIterator.h> 45 #include <vtkCellTypes.h> 46 #include <vtkDataSetSurfaceFilter.h> 47 #include <vtkExtractEdges.h> 48 #include <vtkGenericCell.h> 49 #include <vtkIdList.h> 50 #include <vtkPointData.h> 51 #include <vtkPoints.h> 52 #include <vtkTriangleFilter.h> 53 #include <vtkUnstructuredGrid.h> 66 char **argv =
new char *[2];
67 argv[0] =
new char[100];
68 strcpy(argv[0],
"NONE");
69 _args =
new Foam::argList(argc, argv);
70 Foam::Info <<
"Create time\n" << Foam::endl;
71 _runTime =
new Foam::Time(Foam::Time::controlDictName, *
_args);
72 Foam::argList::noParallel();
96 std::cout <<
"foamMesh destroyed" << std::endl;
109 Foam::word regionName;
110 if (
_args->readIfPresent(
"region", regionName)) {
111 Foam::Info <<
"Create mesh " << regionName
112 <<
" for time = " <<
_runTime->timeName() << Foam::nl
115 if (fname ==
"domain1")
116 regionName =
"domain1";
117 else if (fname ==
"domain0")
118 regionName =
"domain0";
119 else if (fname ==
"domain2")
120 regionName =
"domain2";
121 else if (fname ==
"domain100")
122 regionName =
"domain100";
124 regionName = Foam::fvMesh::defaultRegion;
125 Foam::Info <<
"Create mesh for time = " <<
_runTime->timeName()
126 << Foam::nl << Foam::endl;
130 _fmesh =
new Foam::fvMesh(Foam::IOobject(
141 std::cout <<
"Performing topological decomposition.\n";
142 auto objVfoam = Foam::vtk::vtuAdaptor();
148 std::cout <<
"Number of points " <<
numPoints << std::endl;
149 std::cout <<
"Number of cells " <<
numCells << std::endl;
156 std::vector<double> result(coords, coords + 3);
162 std::vector<std::vector<double>> comp_crds(3);
163 for (
int i = 0; i < 3; ++i) { comp_crds[i].resize(
numPoints); }
167 comp_crds[0][i] = coords[0];
168 comp_crds[1][i] = coords[1];
169 comp_crds[2][i] = coords[2];
177 std::map<nemId_t, std::vector<double>> cell;
179 point_ids =
dataSet->GetCell(
id)->GetPointIds();
180 vtkIdType num_ids = point_ids->GetNumberOfIds();
181 for (vtkIdType i = 0; i < num_ids; ++i) {
182 nemId_t pntId = point_ids->GetId(i);
183 std::vector<double> coord =
getPoint(pntId);
184 cell.insert(std::pair<
nemId_t, std::vector<double>>(pntId, coord));
189 std::cerr <<
"Cell ID is out of range!" << std::endl;
196 std::vector<std::vector<double>> cell;
198 point_ids =
dataSet->GetCell(
id)->GetPointIds();
199 vtkIdType num_ids = point_ids->GetNumberOfIds();
200 cell.resize(num_ids);
201 for (vtkIdType i = 0; i < num_ids; ++i) {
202 nemId_t pntId = point_ids->GetId(i);
207 std::cerr <<
"Cell ID is out of range!" << std::endl;
213 std::ofstream outputStream(ofname);
214 if (!outputStream.good()) {
215 std::cerr <<
"error opening " << ofname << std::endl;
219 vtkSmartPointer<vtkExtractEdges> extractEdges =
221 extractEdges->SetInputData(
dataSet);
222 extractEdges->Update();
224 vtkSmartPointer<vtkGenericCell> genCell =
226 for (
int i = 0; i < extractEdges->GetOutput()->GetNumberOfCells(); ++i) {
227 extractEdges->GetOutput()->GetCell(i, genCell);
228 vtkPoints *
points = genCell->GetPoints();
230 points->GetPoint(0, p1);
231 points->GetPoint(1, p2);
232 double len = std::sqrt(pow(p1[0] - p2[0], 2) + pow(p1[1] - p2[1], 2) +
233 pow(p1[2] - p2[2], 2));
234 outputStream << len << std::endl;
239 std::vector<nemId_t> connectivities;
240 vtkSmartPointer<vtkCellIterator> it =
241 vtkSmartPointer<vtkCellIterator>::Take(
dataSet->NewCellIterator());
242 for (it->InitTraversal(); !it->IsDoneWithTraversal(); it->GoToNextCell()) {
243 vtkSmartPointer<vtkIdList> pointIds = it->GetPointIds();
244 for (vtkIdType i = 0; i < pointIds->GetNumberOfIds(); ++i) {
245 connectivities.push_back(pointIds->GetId(i));
248 return connectivities;
253 std::cout <<
"dataSet has not been populated!" << std::endl;
257 using CellContainer = std::map<int, int>;
259 std::cout <<
"Processing the dataset generated from " <<
filename << std::endl
260 <<
" dataSet contains a " <<
dataSet->GetClassName() <<
" that has " 262 <<
" and " <<
numPoints <<
" points." << std::endl;
264 CellContainer cellMap;
265 for (
int i = 0; i <
numCells; i++) { cellMap[
dataSet->GetCellType(i)]++; }
267 CellContainer::const_iterator it = cellMap.begin();
268 while (it != cellMap.end()) {
269 std::cout <<
"\tCell type " 270 << vtkCellTypes::GetClassNameFromTypeId(it->first) <<
" occurs " 271 << it->second <<
" times." << std::endl;
276 vtkPointData *pd =
dataSet->GetPointData();
278 std::cout <<
" contains point data with " << pd->GetNumberOfArrays()
279 <<
" arrays." << std::endl;
280 for (
int i = 0; i < pd->GetNumberOfArrays(); i++) {
281 std::cout <<
"\tArray " << i <<
" is named " 282 << (pd->GetArrayName(i) ? pd->GetArrayName(i) :
"NULL");
283 vtkDataArray *da = pd->GetArray(i);
284 std::cout <<
" with " << da->GetNumberOfTuples() <<
" values. " 289 vtkCellData *cd =
dataSet->GetCellData();
291 std::cout <<
" contains cell data with " << cd->GetNumberOfArrays()
292 <<
" arrays." << std::endl;
293 for (
int i = 0; i < cd->GetNumberOfArrays(); i++) {
294 std::cout <<
"\tArray " << i <<
" is named " 295 << (cd->GetArrayName(i) ? cd->GetArrayName(i) :
"NULL");
296 vtkDataArray *da = cd->GetArray(i);
297 std::cout <<
" with " << da->GetNumberOfTuples() <<
" values. " 303 std::cout <<
" contains field data with " 304 <<
dataSet->GetFieldData()->GetNumberOfArrays() <<
" arrays." 306 for (
int i = 0; i <
dataSet->GetFieldData()->GetNumberOfArrays(); i++) {
307 std::cout <<
"\tArray " << i <<
" is named " 308 <<
dataSet->GetFieldData()->GetArray(i)->GetName();
309 vtkDataArray *da =
dataSet->GetFieldData()->GetArray(i);
310 std::cout <<
" with " << da->GetNumberOfTuples() <<
" values. " 318 std::vector<double> result;
321 result[i] = std::sqrt(
dataSet->GetCell(i)->GetLength2());
328 std::vector<double> center(3, 0.0);
329 std::vector<std::vector<double>> cell =
getCellVec(cellID);
331 using nemAux::operator*;
332 using nemAux::operator+;
333 for (
int i = 0; i < cell.size(); ++i) { center = center + cell[i]; }
334 return (1. / cell.size()) * center;
342 vtkSmartPointer<vtkDataSetSurfaceFilter> surfFilt =
344 surfFilt->SetInputData(
dataSet);
348 vtkSmartPointer<vtkTriangleFilter> triFilt =
350 triFilt->SetInputData(surfFilt->GetOutput());
353 return triFilt->GetOutput();
361 const char dir_path[] =
"./system";
362 boost::filesystem::path dir(dir_path);
364 boost::filesystem::create_directory(dir);
365 }
catch (boost::filesystem::filesystem_error &e) {
366 std::cerr <<
"Problem in creating system directory for the cfMesh" 368 std::cerr << e.what() << std::endl;
372 std::ofstream contDict;
373 contDict.open(std::string(dir_path) +
"/fvSchemes");
374 std::string contText =
376 /*--------------------------------*- C++ -*----------------------------------*\n\ 378 | \\\\ / F ield | NEMoSys: Mesh Conversion interface |\n\ 379 | \\\\ / O peration | |\n\ 381 | \\\\/ M anipulation | |\n\ 382 \\*---------------------------------------------------------------------------*/\n\ 391 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n\n\ 394 default Gauss linear;\n\ 395 grad(p) Gauss linear;\n\ 401 div(phi,U) Gauss linear;\n\ 407 laplacian(nu,U) Gauss linear corrected;\n\ 408 laplacian((1|A(U)),p) Gauss linear corrected;\n\ 410 // ************************************************************************* //";
411 contDict << contText;
416 std::ofstream contDict2;
417 contDict2.open(std::string(dir_path) +
"/fvSolution");
418 std::string contText2 =
420 /*--------------------------------*- C++ -*----------------------------------*\n\ 422 | \\\\ / F ield | NEMoSys: Mesh Conversion interface |\n\ 423 | \\\\ / O peration | |\n\ 425 | \\\\/ M anipulation | |\n\ 426 \\*---------------------------------------------------------------------------*/\n\ 433 object fvSolution;\n\ 435 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n\n\ 437 // ************************************************************************* //";
438 contDict2 << contText2;
443 std::ofstream contDict3;
444 contDict3.open(std::string(dir_path) +
"/controlDict");
445 std::string contText3 =
447 /*--------------------------------*- C++ -*----------------------------------*\n\ 449 | \\\\ / F ield | NEMoSys: Mesh Conversion interface |\n\ 450 | \\\\ / O peration | |\n\ 452 | \\\\/ M anipulation | |\n\ 453 \\*---------------------------------------------------------------------------*/\n\ 460 location \"system\";\n\ 461 object controlDict;\n\ 463 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n\n\ 466 writeInterval 1;\n\n\ 467 // ************************************************************************* //";
468 contDict3 << contText3;
475 char **argv =
new char *[2];
476 argv[0] =
new char[100];
477 strcpy(argv[0],
"NONE");
478 Foam::argList args(argc, argv);
479 Foam::Info <<
"Create time\n" << Foam::endl;
480 Foam::argList::noParallel();
482 Foam::Time runTime(Foam::Time::controlDictName, Foam::fileName(
"."),
483 Foam::fileName(
"."));
486 vtkSmartPointer<vtkDataSet> vtkDS =
_volMB->getDataSet();
488 int numPoints = vtkDS->GetNumberOfPoints();
489 int numCells = vtkDS->GetNumberOfCells();
492 std::vector<std::vector<double>> verticeZZ;
493 verticeZZ.resize(numPoints);
494 for (
int ipt = 0; ipt <
numPoints; ipt++) {
495 std::vector<double> getPt = std::vector<double>(3);
496 vtkDS->GetPoint(ipt, &getPt[0]);
497 verticeZZ[ipt].resize(3);
498 verticeZZ[ipt][0] = getPt[0];
499 verticeZZ[ipt][1] = getPt[1];
500 verticeZZ[ipt][2] = getPt[2];
504 std::vector<std::vector<int>> cellIdZZ;
505 cellIdZZ.resize(numCells);
508 Foam::pointField pointData(numPoints);
511 std::vector<int> typeCell;
512 typeCell.resize(numCells);
515 Foam::cellShapeList cellShapeData(numCells);
519 Foam::vector(verticeZZ[i][0], verticeZZ[i][1], verticeZZ[i][2]);
522 for (
int i = 0; i <
numCells; i++) {
524 vtkDS->GetCellPoints(i, ptIds);
525 int numIds = ptIds->GetNumberOfIds();
526 cellIdZZ[i].resize(numIds);
527 for (
int j = 0; j < numIds; j++) { cellIdZZ[i][j] = ptIds->GetId(j); }
529 Foam::labelList meshPoints(numIds);
531 for (
int k = 0; k < numIds; k++) { meshPoints[k] = cellIdZZ[i][k]; }
533 typeCell[i] = vtkDS->GetCellType(i);
535 if (typeCell[i] == 12) {
536 cellShapeData[i] = Foam::cellShape(
"hex", meshPoints,
true);
537 }
else if (typeCell[i] == 14) {
538 cellShapeData[i] = Foam::cellShape(
"pyr", meshPoints,
true);
539 }
else if (typeCell[i] == 10) {
540 cellShapeData[i] = Foam::cellShape(
"tet", meshPoints,
true);
542 std::cerr <<
"Only Hexahedral, Tetrahedral," 543 <<
" and Pyramid cells are supported for VTK->FOAM!" 549 Foam::polyMesh
mesh(Foam::IOobject(Foam::polyMesh::defaultRegion,
550 runTime.constant(), runTime),
551 std::move(pointData),
553 Foam::faceListList(),
557 Foam::polyPatch::typeName,
562 std::cout <<
"Writing mesh for time 0" << std::endl;
568 _fmesh =
new Foam::fvMesh(Foam::IOobject(Foam::fvMesh::defaultRegion,
569 runTime.timeName(), runTime,
570 Foam::IOobject::MUST_READ));
nemId_t numPoints
number of points in mesh
void inspectEdges(const std::string &ofname) const override
get edge lengths of dataSet
vtkSmartPointer< vtkDataSet > dataSet
mesh points, topology and data
std::map< nemId_t, std::vector< double > > getCell(nemId_t id) const override
get cell with id : returns point indices and respective coordinates
std::shared_ptr< meshBase > _volMB
Shared meshBase pointer for foamMesh constructor and some methods.
~foamMesh() override
foamMesh standard destructor
void read(const std::string &fname) override
general purpose mesh information read method
geoMeshBase * New(MeshType meshType)
Create a new mesh object.
void readAMR(const Foam::Time &runTime)
Reads fvMesh for adaptive mesh refinement process.
std::vector< double > getPoint(nemId_t id) const override
get point with id
nemId_t numCells
number of cells in mesh
void genMshDB()
Return a polyMesh Object.
std::vector< std::vector< double > > getVertCrds() const override
get 3 vecs with x,y and z coords
std::vector< std::vector< double > > getCellVec(nemId_t id) const override
get cell points ids and their coordinates
virtual void write() const
write the mesh to file named after the private var 'filename'.
std::shared_ptr< meshBase > mesh
int getCellType() const override
get cell type as an integer.
std::vector< vtkIdType > points
points given by id in .inp file
foamMesh(bool readDB=false)
foamMesh default constructor.
std::vector< double > getCellLengths() const override
get diameter of circumsphere of each cell
void createFoamDicts()
Created necessary dictionaries for OpenFOAM runtime environment.
std::string filename
name of mesh file
std::vector< double > getCellCenter(nemId_t cellID) const override
get center of a cell
vtkSmartPointer< vtkDataSet > extractSurface() override
Extracts foam mesh surface using VTK data set.
nemId_t getNumberOfCells() const
return the number of cells
std::vector< nemId_t > getConnectivities() const override
get vtk cell connectivities in terms of point ids
void report() const override
generate a report of the mesh