Created
December 2, 2016 20:05
-
-
Save jwpeterson/d41f5f4ab43b997fdaa4b6464b79bb46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Originally got this code from http://www.vtk.org/Wiki/VTK_Autoconf | |
| // 5.x | |
| // #include "vtkConfigure.h" | |
| // 7.x | |
| // Suppress warnings about missing overrides in VTK headers | |
| #ifdef __clang__ | |
| #pragma clang diagnostic push | |
| #pragma clang diagnostic ignored "-Winconsistent-missing-override" | |
| #endif | |
| #include "vtkVersionMacros.h" | |
| #include "vtkSmartPointer.h" | |
| #include "vtkXMLPUnstructuredGridWriter.h" | |
| #include "vtkUnstructuredGrid.h" | |
| #include "vtkDoubleArray.h" | |
| #include "vtkIntArray.h" | |
| #include "vtkCellArray.h" | |
| #include "vtkCellData.h" | |
| #include "vtkPointData.h" | |
| int main() | |
| { | |
| std::cout << "VTK version is: " | |
| << VTK_MAJOR_VERSION << "." | |
| << VTK_MINOR_VERSION << "." | |
| << VTK_BUILD_VERSION << std::endl; | |
| vtkSmartPointer<vtkXMLPUnstructuredGridWriter> writer = | |
| vtkSmartPointer<vtkXMLPUnstructuredGridWriter>::New(); | |
| vtkSmartPointer<vtkUnstructuredGrid> grid = | |
| vtkSmartPointer<vtkUnstructuredGrid>::New(); | |
| // Something else we call in libmesh | |
| writer->SetGhostLevel(1); | |
| // Writer will not write without input | |
| writer->SetInputData(grid); | |
| // Actually set some nodal values the same way we do in libmesh | |
| { | |
| vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); | |
| vtkSmartPointer<vtkDoubleArray> pcoords = vtkSmartPointer<vtkDoubleArray>::New(); | |
| pcoords->SetNumberOfComponents(3); | |
| points->SetNumberOfPoints(4); | |
| { | |
| double pnt[3] = {0., 0., 0.}; | |
| pcoords->InsertNextTuple(pnt); | |
| } | |
| { | |
| double pnt[3] = {1., 0., 0.}; | |
| pcoords->InsertNextTuple(pnt); | |
| } | |
| { | |
| double pnt[3] = {1., 1., 0.}; | |
| pcoords->InsertNextTuple(pnt); | |
| } | |
| { | |
| double pnt[3] = {0., 1., 0.}; | |
| pcoords->InsertNextTuple(pnt); | |
| } | |
| // add coordinates to points | |
| points->SetData(pcoords); | |
| // add points to grid | |
| grid->SetPoints(points); | |
| } | |
| // Set some cells the same way we do in libmesh | |
| { | |
| vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New(); | |
| vtkSmartPointer<vtkIdList> connectivity = vtkSmartPointer<vtkIdList>::New(); | |
| // To be passed to the SetCells() routine. | |
| std::vector<int> elem_types(1); | |
| elem_types[0] = VTK_QUAD; | |
| vtkSmartPointer<vtkIntArray> elem_id = vtkSmartPointer<vtkIntArray>::New(); | |
| elem_id->SetName("elem_id"); | |
| elem_id->SetNumberOfComponents(1); | |
| vtkSmartPointer<vtkIntArray> subdomain_id = vtkSmartPointer<vtkIntArray>::New(); | |
| subdomain_id->SetName("subdomain_id"); | |
| subdomain_id->SetNumberOfComponents(1); | |
| vtkSmartPointer<vtkIntArray> elem_proc_id = vtkSmartPointer<vtkIntArray>::New(); | |
| elem_proc_id->SetName("processor_id"); | |
| elem_proc_id->SetNumberOfComponents(1); | |
| connectivity->SetNumberOfIds(4); | |
| // Set up element connectivity (just the identity map). | |
| for (unsigned int i=0; i<4; ++i) | |
| connectivity->InsertId(i, i); | |
| // Insert the cell into the grid | |
| vtkIdType vtkcellid = cells->InsertNextCell(connectivity); | |
| // Set up element, subdomain, and processor ids. | |
| elem_id->InsertTuple1(vtkcellid, 0); | |
| subdomain_id->InsertTuple1(vtkcellid, 0); | |
| elem_proc_id->InsertTuple1(vtkcellid, 0); | |
| // Tell the grid about the cells we've set up. | |
| grid->SetCells(&elem_types[0], cells); | |
| grid->GetCellData()->AddArray(elem_id); | |
| grid->GetCellData()->AddArray(subdomain_id); | |
| grid->GetCellData()->AddArray(elem_proc_id); | |
| } | |
| // Set some data values on the mesh | |
| { | |
| vtkSmartPointer<vtkDoubleArray> data = vtkSmartPointer<vtkDoubleArray>::New(); | |
| data->SetName("u"); | |
| data->SetNumberOfValues(4); | |
| // u = x | |
| data->SetValue(0, 0.); | |
| data->SetValue(1, 1.); | |
| data->SetValue(2, 1.); | |
| data->SetValue(3, 0.); | |
| // Set the data on the grid | |
| grid->GetPointData()->AddArray(data); | |
| } | |
| // Use writer in serial. | |
| writer->SetNumberOfPieces(1); | |
| writer->SetStartPiece(0); | |
| writer->SetEndPiece(0); | |
| const std::string fname = "test.pvtu"; | |
| writer->SetFileName(fname.c_str()); | |
| writer->SetDataModeToAscii(); | |
| writer->Write(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment