36 #include "VertexMeshWriter.hpp"
37 #include "Version.hpp"
38 #include "Cylindrical2dVertexMesh.hpp"
39 #include "Toroidal2dVertexMesh.hpp"
44 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
57 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
59 const std::string& rBaseName,
60 const bool clearOutputDir)
65 mNodeMapCurrentIndex(0)
76 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
79 if (mpIters->pNodeIter)
81 delete mpIters->pNodeIter;
82 delete mpIters->pElemIter;
94 mpVtkUnstructedMesh->Delete();
98 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
104 assert(this->mNumNodes == mpMesh->GetNumNodes());
106 std::vector<double> coordinates(SPACE_DIM+1);
109 for (
unsigned j=0; j<SPACE_DIM; j++)
111 coordinates[j] = (*(mpIters->pNodeIter))->GetPoint()[j];
113 coordinates[SPACE_DIM] = (*(mpIters->pNodeIter))->IsBoundaryNode();
115 ++(*(mpIters->pNodeIter));
125 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
129 assert(SPACE_DIM == 3);
131 assert(this->mNumElements == mpMesh->GetNumElements());
137 elem_data.
NodeIndices.resize((*(mpIters->pElemIter))->GetNumNodes());
138 for (
unsigned j=0; j<elem_data.NodeIndices.size(); j++)
140 unsigned old_index = (*(mpIters->pElemIter))->GetNodeGlobalIndex(j);
141 elem_data.NodeIndices[j] = mpMesh->IsMeshChanging() ? mpNodeMap->GetNewIndex(old_index) : old_index;
145 elem_data.Faces.resize((*(mpIters->pElemIter))->GetNumFaces());
146 for (
unsigned i=0; i<elem_data.Faces.size(); i++)
158 face_data.
NodeIndices.resize(p_face->GetNumNodes());
159 for (
unsigned j=0; j<face_data.
NodeIndices.size(); j++)
161 unsigned old_index = p_face->GetNodeGlobalIndex(j);
162 face_data.
NodeIndices[j] = mpMesh->IsMeshChanging() ? mpNodeMap->GetNewIndex(old_index) : old_index;
166 elem_data.Faces[i] = face_data;
173 ++(*(mpIters->pElemIter));
178 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
185 assert(this->mNumElements == mpMesh->GetNumElements());
188 elem_data.
NodeIndices.resize((*(mpIters->pElemIter))->GetNumNodes());
189 for (
unsigned j=0; j<elem_data.NodeIndices.size(); j++)
191 unsigned old_index = (*(mpIters->pElemIter))->GetNodeGlobalIndex(j);
192 elem_data.NodeIndices[j] = mpMesh->IsMeshChanging() ? mpNodeMap->GetNewIndex(old_index) : old_index;
196 elem_data.AttributeValue = (*(mpIters->pElemIter))->GetAttribute();
197 ++(*(mpIters->pElemIter));
207 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
211 assert(SPACE_DIM==3 || SPACE_DIM == 2);
217 assert(mpVtkUnstructedMesh->CheckAttributes() == 0);
218 vtkXMLUnstructuredGridWriter* p_writer = vtkXMLUnstructuredGridWriter::New();
219 #if VTK_MAJOR_VERSION >= 6
220 p_writer->SetInputData(mpVtkUnstructedMesh);
222 p_writer->SetInput(mpVtkUnstructedMesh);
226 p_writer->SetCompressor(NULL);
229 std::string vtk_file_name = this->mpOutputFileHandler->GetOutputDirectoryFullPath() + this->mBaseName;
232 vtk_file_name +=
"_" + stamp;
234 vtk_file_name +=
".vtu";
236 p_writer->SetFileName(vtk_file_name.c_str());
254 if (dynamic_cast<Toroidal2dVertexMesh*>(&rMesh))
257 MakeVtkMesh(*p_mesh_for_vtk);
260 delete p_mesh_for_vtk;
262 else if (dynamic_cast<Cylindrical2dVertexMesh*>(&rMesh))
265 MakeVtkMesh(*p_mesh_for_vtk);
268 delete p_mesh_for_vtk;
276 assert(mpVtkUnstructedMesh->CheckAttributes() == 0);
277 vtkXMLUnstructuredGridWriter* p_writer = vtkXMLUnstructuredGridWriter::New();
278 #if VTK_MAJOR_VERSION >= 6
279 p_writer->SetInputData(mpVtkUnstructedMesh);
281 p_writer->SetInput(mpVtkUnstructedMesh);
285 p_writer->SetCompressor(NULL);
288 std::string vtk_file_name = this->mpOutputFileHandler->GetOutputDirectoryFullPath() + this->mBaseName;
291 vtk_file_name +=
"_" + stamp;
293 vtk_file_name +=
".vtu";
295 p_writer->SetFileName(vtk_file_name.c_str());
302 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
307 vtkPoints* p_pts = vtkPoints::New(VTK_DOUBLE);
308 p_pts->GetData()->SetName(
"Vertex positions");
309 for (
unsigned node_num=0; node_num<rMesh.
GetNumNodes(); node_num++)
311 c_vector<double, SPACE_DIM> position = rMesh.
GetNode(node_num)->rGetLocation();
314 p_pts->InsertPoint(node_num, position[0], position[1], 0.0);
318 p_pts->InsertPoint(node_num, position[0], position[1], position[2]);
322 mpVtkUnstructedMesh->SetPoints(p_pts);
331 p_cell = vtkPolygon::New();
335 p_cell = vtkConvexPointSet::New();
337 vtkIdList* p_cell_id_list = p_cell->GetPointIds();
338 p_cell_id_list->SetNumberOfIds(iter->GetNumNodes());
339 for (
unsigned j=0; j<iter->GetNumNodes(); ++j)
341 p_cell_id_list->SetId(j, iter->GetNodeGlobalIndex(j));
343 mpVtkUnstructedMesh->InsertNextCell(p_cell->GetCellType(), p_cell_id_list);
349 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
353 vtkDoubleArray* p_scalars = vtkDoubleArray::New();
354 p_scalars->SetName(dataName.c_str());
355 for (
unsigned i=0; i<dataPayload.size(); i++)
357 p_scalars->InsertNextValue(dataPayload[i]);
360 vtkCellData* p_cell_data = mpVtkUnstructedMesh->GetCellData();
361 p_cell_data->AddArray(p_scalars);
366 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
370 vtkDoubleArray* p_scalars = vtkDoubleArray::New();
371 p_scalars->SetName(dataName.c_str());
372 for (
unsigned i=0; i<dataPayload.size(); i++)
374 p_scalars->InsertNextValue(dataPayload[i]);
377 vtkPointData* p_point_data = mpVtkUnstructedMesh->GetPointData();
378 p_point_data->AddArray(p_scalars);
384 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
387 this->mpMeshReader = NULL;
391 this->mNumElements = mpMesh->GetNumElements();
394 mpIters->pNodeIter =
new NodeIterType(mpMesh->GetNodeIteratorBegin());
397 mpIters->pElemIter =
new ElemIterType(mpMesh->GetElementIteratorBegin());
400 mNodeMapCurrentIndex = 0;
401 if (mpMesh->IsMeshChanging())
403 mpNodeMap =
new NodeMap(mpMesh->GetNumAllNodes());
404 for (NodeIterType it = mpMesh->GetNodeIteratorBegin(); it != mpMesh->GetNodeIteratorEnd(); ++it)
406 mpNodeMap->
SetNewIndex(it->GetIndex(), mNodeMapCurrentIndex++);
412 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
418 std::string node_file_name = this->mBaseName +
".node";
419 out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name);
422 unsigned num_attr = 0;
423 unsigned max_bdy_marker = 1;
424 unsigned num_nodes = this->GetNumNodes();
426 *p_node_file << num_nodes <<
"\t";
427 *p_node_file << SPACE_DIM <<
"\t";
428 *p_node_file << num_attr <<
"\t";
429 *p_node_file << max_bdy_marker <<
"\n";
430 *p_node_file << std::setprecision(6);
433 for (
unsigned item_num=0; item_num<num_nodes; item_num++)
435 std::vector<double> current_item = this->GetNextNode();
436 *p_node_file << item_num;
437 for (
unsigned i=0; i<SPACE_DIM+1; i++)
439 *p_node_file <<
"\t" << current_item[i];
441 *p_node_file <<
"\n";
443 *p_node_file << comment <<
"\n";
444 p_node_file->close();
447 std::string element_file_name = this->mBaseName +
".cell";
448 out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name);
452 unsigned num_elements = this->GetNumElements();
453 *p_element_file << num_elements <<
"\t" << num_attr <<
"\n";
456 for (
unsigned item_num=0; item_num<num_elements; item_num++)
464 std::vector<unsigned> node_indices = elem_data.
NodeIndices;
467 *p_element_file << item_num <<
"\t" << node_indices.size();
470 for (
unsigned i=0; i<node_indices.size(); i++)
472 *p_element_file <<
"\t" << node_indices[i];
478 *p_element_file <<
"\n";
482 assert(SPACE_DIM == 3);
488 std::vector<unsigned> node_indices = elem_data_with_faces.
NodeIndices;
491 *p_element_file << item_num <<
"\t" << node_indices.size();
494 for (
unsigned i=0; i<node_indices.size(); i++)
496 *p_element_file <<
"\t" << node_indices[i];
500 std::vector<ElementData> faces = elem_data_with_faces.
Faces;
503 *p_element_file <<
"\t" << faces.size();
505 for (
unsigned j=0; j<faces.size(); j++)
508 std::vector<unsigned> face_node_indices = faces[j].NodeIndices;
511 *p_element_file <<
"\t" << faces[j].AttributeValue <<
"\t" << face_node_indices.size();
514 for (
unsigned i=0; i<face_node_indices.size(); i++)
516 *p_element_file <<
"\t" << face_node_indices[i];
525 *p_element_file <<
"\n";
529 *p_element_file << comment <<
"\n";
530 p_element_file->close();
void AddCellData(std::string dataName, std::vector< double > dataPayload)
MeshWriterIterators< ELEMENT_DIM, SPACE_DIM > * mpIters
void WriteFilesUsingMesh(VertexMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
Node< SPACE_DIM > * GetNode(unsigned index) const
virtual unsigned GetNumNodes() const
AbstractMesh< ELEMENT_DIM, SPACE_DIM >::NodeIterator * pNodeIter
virtual std::vector< double > GetNextNode()
vtkUnstructuredGrid * mpVtkUnstructedMesh
std::vector< ElementData > Faces
std::vector< unsigned > NodeIndices
VertexMeshWriter(const std::string &rDirectory, const std::string &rBaseName, const bool clearOutputDir=true)
void SetNewIndex(unsigned oldIndex, unsigned newIndex)
VertexElement< ELEMENT_DIM-1, SPACE_DIM > * GetFace(unsigned index) const
VertexElementData GetNextElementWithFaces()
VertexElementIterator GetElementIteratorBegin(bool skipDeletedElements=true)
void WriteVtkUsingMesh(VertexMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, std::string stamp="")
VertexMesh< ELEMENT_DIM, SPACE_DIM >::VertexElementIterator * pElemIter
void MakeVtkMesh(VertexMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
std::vector< double > GetNextNode()
ElementData GetNextElement()
static std::string GetProvenanceString()
virtual ElementData GetNextElement()
VertexElementIterator GetElementIteratorEnd()
std::vector< unsigned > NodeIndices
void AddPointData(std::string dataName, std::vector< double > dataPayload)