Created
December 5, 2016 23:00
-
-
Save jwpeterson/92f4b8422d6fbb1056e848c9b14ee1d7 to your computer and use it in GitHub Desktop.
This file contains 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
// MPI headers so we can make a parallel example | |
#include <mpi.h> | |
// 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" | |
// Run this test code on two processors with e.g.: | |
// mpiexec -np 2 ./vtk_test | |
int main(int argc, char ** argv) | |
{ | |
MPI_Init(&argc, &argv); | |
int n_procs = 0, rank = 0; | |
MPI_Comm_size(MPI_COMM_WORLD, &n_procs); | |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
vtkSmartPointer<vtkXMLPUnstructuredGridWriter> writer = | |
vtkSmartPointer<vtkXMLPUnstructuredGridWriter>::New(); | |
vtkSmartPointer<vtkUnstructuredGrid> grid = | |
vtkSmartPointer<vtkUnstructuredGrid>::New(); | |
writer->SetGhostLevel(1); | |
writer->SetInputData(grid); | |
// Add nodes | |
{ | |
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); | |
vtkSmartPointer<vtkDoubleArray> pcoords = vtkSmartPointer<vtkDoubleArray>::New(); | |
pcoords->SetNumberOfComponents(3); | |
points->SetNumberOfPoints(4); | |
if (rank == 0) | |
{ | |
{ | |
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); | |
} | |
} | |
if (rank == 1) | |
{ | |
{ | |
double pnt[3] = {1., 0., 0.}; | |
pcoords->InsertNextTuple(pnt); | |
} | |
{ | |
double pnt[3] = {2., 0., 0.}; | |
pcoords->InsertNextTuple(pnt); | |
} | |
{ | |
double pnt[3] = {2., 1., 0.}; | |
pcoords->InsertNextTuple(pnt); | |
} | |
{ | |
double pnt[3] = {1., 1., 0.}; | |
pcoords->InsertNextTuple(pnt); | |
} | |
} | |
// add coordinates to points | |
points->SetData(pcoords); | |
// add points to grid | |
grid->SetPoints(points); | |
} | |
// Add elements | |
{ | |
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, rank); | |
subdomain_id->InsertTuple1(vtkcellid, rank); | |
elem_proc_id->InsertTuple1(vtkcellid, rank); | |
// 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 | |
if (rank == 0) | |
{ | |
data->SetValue(0, 0.); | |
data->SetValue(1, 1.); | |
data->SetValue(2, 1.); | |
data->SetValue(3, 0.); | |
} | |
if (rank == 1) | |
{ | |
data->SetValue(0, 1.); | |
data->SetValue(1, 2.); | |
data->SetValue(2, 2.); | |
data->SetValue(3, 1.); | |
} | |
// Set the data on the grid | |
grid->GetPointData()->AddArray(data); | |
} | |
// Set number of pieces and start/end piece. | |
writer->SetNumberOfPieces(n_procs); | |
writer->SetStartPiece(rank); | |
writer->SetEndPiece(rank); | |
// VTK figures out how to build the filenames on the different processors. | |
writer->SetFileName("test.pvtu"); | |
writer->SetDataModeToAscii(); | |
writer->Write(); | |
// Wait for everyone to be done. | |
MPI_Finalize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice example but when I try it, it generates the vtu files but pvtu only take into account only one of the domains.
So I have to modify it manually in order to see all my domains