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(
nullptr);
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());
255 MakeVtkMesh(*p_mesh_for_vtk);
258 assert(mpVtkUnstructedMesh->CheckAttributes() == 0);
259 vtkXMLUnstructuredGridWriter* p_writer = vtkXMLUnstructuredGridWriter::New();
260 #if VTK_MAJOR_VERSION >= 6
261 p_writer->SetInputData(mpVtkUnstructedMesh);
263 p_writer->SetInput(mpVtkUnstructedMesh);
267 p_writer->SetCompressor(
nullptr);
270 std::string vtk_file_name = this->mpOutputFileHandler->GetOutputDirectoryFullPath() + this->mBaseName;
273 vtk_file_name +=
"_" + stamp;
275 vtk_file_name +=
".vtu";
277 p_writer->SetFileName(vtk_file_name.c_str());
284 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
289 vtkPoints* p_pts = vtkPoints::New(VTK_DOUBLE);
290 p_pts->GetData()->SetName(
"Vertex positions");
291 for (
unsigned node_num=0; node_num<rMesh.
GetNumNodes(); node_num++)
293 c_vector<double, SPACE_DIM> position = rMesh.
GetNode(node_num)->rGetLocation();
296 p_pts->InsertPoint(node_num, position[0], position[1], 0.0);
300 p_pts->InsertPoint(node_num, position[0], position[1], position[2]);
304 mpVtkUnstructedMesh->SetPoints(p_pts);
313 p_cell = vtkPolygon::New();
317 p_cell = vtkConvexPointSet::New();
319 vtkIdList* p_cell_id_list = p_cell->GetPointIds();
320 p_cell_id_list->SetNumberOfIds(iter->GetNumNodes());
321 for (
unsigned j=0; j<iter->GetNumNodes(); ++j)
323 p_cell_id_list->SetId(j, iter->GetNodeGlobalIndex(j));
325 mpVtkUnstructedMesh->InsertNextCell(p_cell->GetCellType(), p_cell_id_list);
331 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
335 vtkDoubleArray* p_scalars = vtkDoubleArray::New();
336 p_scalars->SetName(dataName.c_str());
337 for (
unsigned i=0; i<dataPayload.size(); i++)
339 p_scalars->InsertNextValue(dataPayload[i]);
342 vtkCellData* p_cell_data = mpVtkUnstructedMesh->GetCellData();
343 p_cell_data->AddArray(p_scalars);
348 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
352 vtkDoubleArray* p_scalars = vtkDoubleArray::New();
353 p_scalars->SetName(dataName.c_str());
354 for (
unsigned i=0; i<dataPayload.size(); i++)
356 p_scalars->InsertNextValue(dataPayload[i]);
359 vtkPointData* p_point_data = mpVtkUnstructedMesh->GetPointData();
360 p_point_data->AddArray(p_scalars);
366 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
369 this->mpMeshReader =
nullptr;
373 this->mNumElements = mpMesh->GetNumElements();
376 mpIters->pNodeIter =
new NodeIterType(mpMesh->GetNodeIteratorBegin());
379 mpIters->pElemIter =
new ElemIterType(mpMesh->GetElementIteratorBegin());
382 mNodeMapCurrentIndex = 0;
383 if (mpMesh->IsMeshChanging())
385 mpNodeMap =
new NodeMap(mpMesh->GetNumAllNodes());
386 for (NodeIterType it = mpMesh->GetNodeIteratorBegin(); it != mpMesh->GetNodeIteratorEnd(); ++it)
388 mpNodeMap->
SetNewIndex(it->GetIndex(), mNodeMapCurrentIndex++);
394 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
400 std::string node_file_name = this->mBaseName +
".node";
401 out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name);
404 unsigned num_attr = 0;
405 unsigned max_bdy_marker = 1;
406 unsigned num_nodes = this->GetNumNodes();
408 *p_node_file << num_nodes <<
"\t";
409 *p_node_file << SPACE_DIM <<
"\t";
410 *p_node_file << num_attr <<
"\t";
411 *p_node_file << max_bdy_marker <<
"\n";
412 *p_node_file << std::setprecision(6);
415 for (
unsigned item_num=0; item_num<num_nodes; item_num++)
417 std::vector<double> current_item = this->GetNextNode();
418 *p_node_file << item_num;
419 for (
unsigned i=0; i<SPACE_DIM+1; i++)
421 *p_node_file <<
"\t" << current_item[i];
423 *p_node_file <<
"\n";
425 *p_node_file << comment <<
"\n";
426 p_node_file->close();
429 std::string element_file_name = this->mBaseName +
".cell";
430 out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name);
434 unsigned num_elements = this->GetNumElements();
435 *p_element_file << num_elements <<
"\t" << num_attr <<
"\n";
438 for (
unsigned item_num=0; item_num<num_elements; item_num++)
446 std::vector<unsigned> node_indices = elem_data.
NodeIndices;
449 *p_element_file << item_num <<
"\t" << node_indices.size();
452 for (
unsigned i=0; i<node_indices.size(); i++)
454 *p_element_file <<
"\t" << node_indices[i];
460 *p_element_file <<
"\n";
464 assert(SPACE_DIM == 3);
470 std::vector<unsigned> node_indices = elem_data_with_faces.
NodeIndices;
473 *p_element_file << item_num <<
"\t" << node_indices.size();
476 for (
unsigned i=0; i<node_indices.size(); i++)
478 *p_element_file <<
"\t" << node_indices[i];
482 std::vector<ElementData> faces = elem_data_with_faces.
Faces;
485 *p_element_file <<
"\t" << faces.size();
487 for (
unsigned j=0; j<faces.size(); j++)
490 std::vector<unsigned> face_node_indices = faces[j].NodeIndices;
493 *p_element_file <<
"\t" << faces[j].AttributeValue <<
"\t" << face_node_indices.size();
496 for (
unsigned i=0; i<face_node_indices.size(); i++)
498 *p_element_file <<
"\t" << face_node_indices[i];
507 *p_element_file <<
"\n";
511 *p_element_file << comment <<
"\n";
512 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 VertexMesh< ELEMENT_DIM, SPACE_DIM > * GetMeshForVtk()
virtual ElementData GetNextElement()
VertexElementIterator GetElementIteratorEnd()
std::vector< unsigned > NodeIndices
void AddPointData(std::string dataName, std::vector< double > dataPayload)