33 #ifdef LIBMESH_HAVE_VTK 39 #include "vtkXMLUnstructuredGridReader.h" 40 #include "vtkXMLUnstructuredGridWriter.h" 41 #include "vtkXMLPUnstructuredGridWriter.h" 42 #include "vtkUnstructuredGrid.h" 43 #include "vtkIntArray.h" 44 #include "vtkCellArray.h" 45 #include "vtkCellData.h" 46 #include "vtkConfigure.h" 47 #include "vtkDoubleArray.h" 48 #include "vtkGenericCell.h" 49 #include "vtkPointData.h" 50 #include "vtkPoints.h" 51 #include "vtkSmartPointer.h" 61 #define VTK_VERSION_LESS_THAN(major,minor,subminor) \ 62 ((LIBMESH_DETECTED_VTK_VERSION_MAJOR < (major) || \ 63 (LIBMESH_DETECTED_VTK_VERSION_MAJOR == (major) && (LIBMESH_DETECTED_VTK_VERSION_MINOR < (minor) || \ 64 (LIBMESH_DETECTED_VTK_VERSION_MINOR == (minor) && \ 65 LIBMESH_DETECTED_VTK_VERSION_SUBMINOR < (subminor))))) ? 1 : 0) 67 #endif // LIBMESH_HAVE_VTK 78 #ifdef LIBMESH_HAVE_VTK
89 #ifdef LIBMESH_HAVE_VTK
100 std::vector<Number> soln;
101 std::vector<std::string> names;
109 #ifdef LIBMESH_HAVE_VTK 137 #if VTK_MAJOR_VERSION > 5 || (VTK_MAJOR_VERSION == 5 && VTK_MINOR_VERSION > 0) 161 typedef vtkSmartPointer<vtkXMLUnstructuredGridReader> MyReader;
162 MyReader reader = MyReader::New();
165 reader->SetFileName(
name.c_str());
180 const unsigned int vtk_num_points =
static_cast<unsigned int>(
_vtk_grid->GetNumberOfPoints());
183 for (
unsigned int i=0; i<vtk_num_points; ++i)
188 _vtk_grid->GetPoint(static_cast<vtkIdType>(i), pnt);
189 Point xyz(pnt[0], pnt[1], pnt[2]);
194 const unsigned int vtk_num_cells =
static_cast<unsigned int>(
_vtk_grid->GetNumberOfCells());
196 vtkSmartPointer<vtkGenericCell> cell = vtkSmartPointer<vtkGenericCell>::New();
197 for (
unsigned int i=0; i<vtk_num_cells; ++i)
206 for (
unsigned int j=0; j<elem->
n_nodes(); ++j)
208 mesh.
node_ptr(cast_int<dof_id_type>(cell->GetPointId(j)));
211 std::vector<dof_id_type> conn;
216 for (
unsigned int j=0,
217 n_conn = cast_int<unsigned int>(conn.size());
229 for (
unsigned char i=0; i!=4; ++i)
235 libmesh_error_msg(
"Cannot open dimension " \
237 <<
" mesh file when configured without " \
240 #endif // LIBMESH_DIM < 3 246 const std::vector<Number> & soln,
247 const std::vector<std::string> & names)
254 if (fname.substr(fname.rfind(
"."), fname.size()) !=
".pvtu")
255 libmesh_do_once(
libMesh::err <<
"The .pvtu extension should be used when writing VTK files in libMesh.");
261 if (!names.empty() && soln.empty())
262 libmesh_error_msg(
"Empty soln vector in VTKIO::write_nodal_data().");
265 _vtk_grid = vtkSmartPointer<vtkUnstructuredGrid>::New();
266 vtkSmartPointer<vtkXMLPUnstructuredGridWriter> writer = vtkSmartPointer<vtkXMLPUnstructuredGridWriter>::New();
276 if (names.size() > 0)
278 std::size_t num_vars = names.size();
281 for (std::size_t variable=0; variable<num_vars; ++variable)
283 vtkSmartPointer<vtkDoubleArray>
data = vtkSmartPointer<vtkDoubleArray>::New();
284 data->SetName(names[variable].c_str());
293 std::map<dof_id_type, dof_id_type>::iterator local_node_it =
_local_node_map.find(k);
297 #ifdef LIBMESH_USE_COMPLEX_NUMBERS 298 libmesh_do_once (
libMesh::err <<
"Only writing the real part for complex numbers!\n" 299 <<
"if you need this support contact " << LIBMESH_PACKAGE_BUGREPORT
301 data->SetValue(local_node_it->second, soln[k*num_vars + variable].real());
303 data->SetValue(local_node_it->second, soln[k*num_vars + variable]);
320 writer->SetGhostLevel(1);
325 #if VTK_VERSION_LESS_THAN(6,0,0) 331 writer->SetFileName(fname.c_str());
332 writer->SetDataModeToAscii();
337 #if !VTK_VERSION_LESS_THAN(5,6,0) 338 writer->SetCompressorTypeToZLib();
340 libmesh_do_once(
libMesh::err <<
"Compression not implemented with old VTK libs!" << std::endl;);
369 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
370 vtkSmartPointer<vtkDoubleArray> pcoords = vtkSmartPointer<vtkDoubleArray>::New();
372 pcoords->SetNumberOfComponents(LIBMESH_DIM);
376 unsigned int local_node_counter = 0;
380 const Node & node = *node_ptr;
382 double pnt[3] = {0, 0, 0};
383 for (
unsigned int i=0; i<LIBMESH_DIM; ++i)
390 #if VTK_VERSION_LESS_THAN(7,1,0) 391 pcoords->InsertNextTupleValue(pnt);
393 pcoords->InsertNextTuple(pnt);
395 ++local_node_counter;
399 points->SetData(pcoords);
411 vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
412 vtkSmartPointer<vtkIdList> pts = vtkSmartPointer<vtkIdList>::New();
416 vtkSmartPointer<vtkIntArray> elem_id = vtkSmartPointer<vtkIntArray>::New();
417 elem_id->SetName(
"libmesh_elem_id");
418 elem_id->SetNumberOfComponents(1);
420 vtkSmartPointer<vtkIntArray> subdomain_id = vtkSmartPointer<vtkIntArray>::New();
421 subdomain_id->SetName(
"subdomain_id");
422 subdomain_id->SetNumberOfComponents(1);
424 vtkSmartPointer<vtkIntArray> elem_proc_id = vtkSmartPointer<vtkIntArray>::New();
425 elem_proc_id->SetName(
"processor_id");
426 elem_proc_id->SetNumberOfComponents(1);
428 unsigned active_element_counter = 0;
431 pts->SetNumberOfIds(elem->n_nodes());
434 std::vector<dof_id_type> conn;
435 elem->connectivity(0,
VTK, conn);
437 for (
unsigned int i=0,
438 n_conn = cast_int<unsigned int>(conn.size());
451 double pt[3] = {0., 0., 0.};
452 for (
unsigned int d=0; d<LIBMESH_DIM; ++d)
456 vtkIdType local =
_vtk_grid->GetPoints()->InsertNextPoint(pt);
460 cast_int<dof_id_type>(local);
468 vtkIdType vtkcellid = cells->InsertNextCell(pts);
471 elem_id->InsertTuple1(vtkcellid, elem->id());
472 subdomain_id->InsertTuple1(vtkcellid, elem->subdomain_id());
473 elem_proc_id->InsertTuple1(vtkcellid, elem->processor_id());
474 ++active_element_counter;
477 _vtk_grid->SetCells(types.data(), cells);
478 _vtk_grid->GetCellData()->AddArray(elem_id);
479 _vtk_grid->GetCellData()->AddArray(subdomain_id);
480 _vtk_grid->GetCellData()->AddArray(elem_proc_id);
543 #else // !LIBMESH_HAVE_VTK 547 libmesh_error_msg(
"Cannot read VTK file: " <<
name \
548 <<
"\nYou must have VTK installed and correctly configured to read VTK meshes.");
554 const std::vector<Number> &,
555 const std::vector<std::string> &)
557 libmesh_error_msg(
"Cannot write VTK file: " << fname \
558 <<
"\nYou must have VTK installed and correctly configured to read VTK meshes.");
562 #endif // LIBMESH_HAVE_VTK std::string name(const ElemQuality q)
void set_compression(bool b)
std::map< dof_id_type, dof_id_type > _local_node_map
virtual Node *& set_node(const unsigned int i)
A geometric point in (x,y,z) space associated with a DOF.
virtual SimpleRange< node_iterator > local_node_ptr_range()=0
vtkIdType find(ElemType libmesh_type)
vtkSmartPointer< vtkUnstructuredGrid > _vtk_grid
The base class for all geometric element types.
virtual void write(const std::string &) override
dof_id_type n_local_nodes() const
static ElementMaps _element_maps
virtual Node * add_point(const Point &p, const dof_id_type id=DofObject::invalid_id, const processor_id_type proc_id=DofObject::invalid_processor_id)=0
virtual void read(const std::string &) override
virtual SimpleRange< element_iterator > active_local_element_ptr_range()=0
dof_id_type n_active_local_elem() const
virtual unsigned int n_nodes() const =0
virtual Elem * add_elem(Elem *e)=0
static std::unique_ptr< Elem > build(const ElemType type, Elem *p=nullptr)
virtual void write_nodal_data(const std::string &, const std::vector< Number > &, const std::vector< std::string > &) override
static ElementMaps build_element_maps()
vtkUnstructuredGrid * get_vtk_grid()
OStreamProxy err(std::cerr)
void set_mesh_dimension(unsigned char d)
std::map< ElemType, vtkIdType > writing_map
void associate(ElemType libmesh_type, vtkIdType vtk_type)
virtual unsigned short dim() const =0
unsigned int mesh_dimension() const
virtual void connectivity(const unsigned int sc, const IOPackage iop, std::vector< dof_id_type > &conn) const =0
virtual const Point & point(const dof_id_type i) const =0
virtual const Node * node_ptr(const dof_id_type i) const =0
A geometric point in (x,y,z) space.
virtual dof_id_type n_nodes() const =0