Commits

Shao-Ching Huang  committed 919ae0d

test code for vtk rectilinear grid (.vtr) viewer

- test code: ex42.c
- follow PETSc style
- include ex42.c in src/dm/examples/tests/makefile

  • Participants
  • Parent commits a13bc4e

Comments (0)

Files changed (2)

File src/dm/examples/tests/ex42.c

+/* -*- Mode: C++; c-basic-offset:2 ; indent-tabs-mode:nil ; -*- */
+
+static char help[] = "Test VTK Rectilinear grid (.vtr) viewer support\n\n";
+
+#include <mpi.h>
+#include "petscdmda.h"
+
+/*
+  Write 3D DMDA vector with coordinates in VTK VTR format
+
+*/
+PetscErrorCode test_3d(const char filename[])
+{
+  MPI_Comm          comm = MPI_COMM_WORLD;
+  const PetscInt    M=10, N=15, P=30, dof=1, sw=1;
+  const PetscScalar Lx=1.0, Ly=1.0, Lz=1.0;
+  DM                da;
+  Vec               v;
+  PetscViewer       view;
+  DMDALocalInfo     info;
+  PetscScalar       ***va;
+  PetscInt          i,j,k;
+  PetscErrorCode    ierr;
+
+  ierr = DMDACreate3d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,
+                      DMDA_STENCIL_STAR, M,N,P,
+                      PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,dof,sw,NULL,NULL,NULL,&da);CHKERRQ(ierr);
+
+  ierr = DMDASetUniformCoordinates(da,0.0,Lx,0.0,Ly,0.0,Lz);CHKERRQ(ierr);
+  ierr = DMDAGetLocalInfo(da,&info);CHKERRQ(ierr);
+  ierr = DMCreateGlobalVector(da,&v);CHKERRQ(ierr);
+  ierr = DMDAVecGetArray(da,v,&va);CHKERRQ(ierr);
+  for (k=info.zs; k<info.zs+info.zm; k++) {
+    for (j=info.ys; j<info.ys+info.ym; j++) {
+      for (i=info.xs; i<info.xs+info.xm; i++) {
+        PetscScalar x = (Lx*i)/M;
+        PetscScalar y = (Ly*j)/N;
+        PetscScalar z = (Lz*k)/P;
+        va[k][j][i] = pow(x-0.5*Lx,2)+pow(y-0.5*Ly,2)+pow(z-0.5*Lz,2);
+      }
+    }
+  }
+  ierr = DMDAVecRestoreArray(da,v,&va);CHKERRQ(ierr);
+  ierr = PetscViewerVTKOpen(comm,filename,FILE_MODE_WRITE,&view);CHKERRQ(ierr);
+  ierr = VecView(v,view);CHKERRQ(ierr);
+  ierr = PetscViewerDestroy(&view);CHKERRQ(ierr);
+  ierr = VecDestroy(&v);CHKERRQ(ierr);
+  ierr = DMDestroy(&da);CHKERRQ(ierr);
+  return 0;
+}
+
+
+/*
+  Write 2D DMDA vector with coordinates in VTK VTR format
+
+*/
+PetscErrorCode test_2d(const char filename[])
+{
+  MPI_Comm          comm = MPI_COMM_WORLD;
+  const PetscInt    M=10, N=20, dof=1, sw=1;
+  const PetscScalar Lx=1.0, Ly=1.0, Lz=1.0;
+  DM                da;
+  Vec               v;
+  PetscViewer       view;
+  DMDALocalInfo     info;
+  PetscScalar       **va;
+  PetscInt          i,j;
+  PetscErrorCode    ierr;
+
+  ierr = DMDACreate2d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,
+                      DMDA_STENCIL_STAR, M,N,
+                      PETSC_DECIDE,PETSC_DECIDE,dof,sw,NULL,NULL,&da);CHKERRQ(ierr);
+  ierr = DMDASetUniformCoordinates(da,0.0,Lx,0.0,Ly,0.0,Lz);CHKERRQ(ierr);
+  ierr = DMDAGetLocalInfo(da,&info);CHKERRQ(ierr);
+  ierr = DMCreateGlobalVector(da,&v);CHKERRQ(ierr);
+  ierr = DMDAVecGetArray(da,v,&va);CHKERRQ(ierr);
+  for (j=info.ys; j<info.ys+info.ym; j++) {
+    for (i=info.xs; i<info.xs+info.xm; i++) {
+      PetscScalar x = (Lx*i)/M;
+      PetscScalar y = (Ly*j)/N;
+      va[j][i] = pow(x-0.5*Lx,2)+pow(y-0.5*Ly,2);
+    }
+  }
+  ierr = DMDAVecRestoreArray(da,v,&va);CHKERRQ(ierr);
+  ierr = PetscViewerVTKOpen(comm,filename,FILE_MODE_WRITE,&view);CHKERRQ(ierr);
+  ierr = VecView(v,view);CHKERRQ(ierr);
+  ierr = PetscViewerDestroy(&view);CHKERRQ(ierr);
+  ierr = VecDestroy(&v);CHKERRQ(ierr);
+  ierr = DMDestroy(&da);CHKERRQ(ierr);
+  return 0;
+}
+
+
+/*
+  Write 2D DMDA vector without coordinates in VTK VTR format
+
+*/
+PetscErrorCode test_2d_nocoord(const char filename[])
+{
+  MPI_Comm          comm = MPI_COMM_WORLD;
+  const PetscInt    M=10, N=20, dof=1, sw=1;
+  const PetscScalar Lx=1.0, Ly=1.0;
+  DM                da;
+  Vec               v;
+  PetscViewer       view;
+  DMDALocalInfo     info;
+  PetscScalar       **va;
+  PetscInt          i,j;
+  PetscErrorCode    ierr;
+
+  ierr = DMDACreate2d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,
+                      DMDA_STENCIL_STAR, M,N,
+                      PETSC_DECIDE,PETSC_DECIDE,dof,sw,NULL,NULL,&da);CHKERRQ(ierr);
+
+  ierr = DMDAGetLocalInfo(da,&info);CHKERRQ(ierr);
+  ierr = DMCreateGlobalVector(da,&v);CHKERRQ(ierr);
+  ierr = DMDAVecGetArray(da,v,&va);CHKERRQ(ierr);
+  for (j=info.ys; j<info.ys+info.ym; j++) {
+    for (i=info.xs; i<info.xs+info.xm; i++) {
+      PetscScalar x = (Lx*i)/M;
+      PetscScalar y = (Ly*j)/N;
+      va[j][i] = pow(x-0.5*Lx,2)+pow(y-0.5*Ly,2);
+    }
+  }
+  ierr = DMDAVecRestoreArray(da,v,&va);CHKERRQ(ierr);
+  ierr = PetscViewerVTKOpen(comm,filename,FILE_MODE_WRITE,&view);CHKERRQ(ierr);
+  ierr = VecView(v,view);CHKERRQ(ierr);
+  ierr = PetscViewerDestroy(&view);CHKERRQ(ierr);
+  ierr = VecDestroy(&v);CHKERRQ(ierr);
+  ierr = DMDestroy(&da);CHKERRQ(ierr);
+  return 0;
+}
+
+
+/*
+  Write 3D DMDA vector without coordinates in VTK VTR format
+
+*/
+PetscErrorCode test_3d_nocoord(const char filename[])
+{
+  MPI_Comm          comm = MPI_COMM_WORLD;
+  const PetscInt    M=10, N=20, P=30, dof=1, sw=1;
+  const PetscScalar Lx=1.0, Ly=1.0, Lz=1.0;
+  DM                da;
+  Vec               v;
+  PetscViewer       view;
+  DMDALocalInfo     info;
+  PetscScalar       ***va;
+  PetscInt          i,j,k;
+  PetscErrorCode    ierr;
+
+  ierr = DMDACreate3d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,
+                      DMDA_STENCIL_STAR, M,N,P,
+                      PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,dof,sw,NULL,NULL,NULL,&da);CHKERRQ(ierr);
+
+  ierr = DMDAGetLocalInfo(da,&info);CHKERRQ(ierr);
+  ierr = DMCreateGlobalVector(da,&v);CHKERRQ(ierr);
+  ierr = DMDAVecGetArray(da,v,&va);CHKERRQ(ierr);
+  for (k=info.zs; k<info.zs+info.zm; k++) {
+    for (j=info.ys; j<info.ys+info.ym; j++) {
+      for (i=info.xs; i<info.xs+info.xm; i++) {
+        PetscScalar x = (Lx*i)/M;
+        PetscScalar y = (Ly*j)/N;
+        PetscScalar z = (Lz*k)/P;
+        va[k][j][i] = pow(x-0.5*Lx,2)+pow(y-0.5*Ly,2)+pow(z-0.5*Lz,2);
+      }
+    }
+  }
+  ierr = DMDAVecRestoreArray(da,v,&va);CHKERRQ(ierr);
+  ierr = PetscViewerVTKOpen(comm,filename,FILE_MODE_WRITE,&view);CHKERRQ(ierr);
+  ierr = VecView(v,view);CHKERRQ(ierr);
+  ierr = PetscViewerDestroy(&view);CHKERRQ(ierr);
+  ierr = VecDestroy(&v);CHKERRQ(ierr);
+  ierr = DMDestroy(&da);CHKERRQ(ierr);
+  return 0;
+}
+
+int main(int argc, char *argv[])
+{
+  PetscErrorCode ierr;
+  ierr = PetscInitialize(&argc,&argv,0,help);CHKERRQ(ierr);
+  test_3d("3d.vtr");
+  test_2d("2d.vtr");
+  test_2d_nocoord("2d_nocoord.vtr");
+  test_3d_nocoord("3d_nocoord.vtr");
+  ierr = PetscFinalize();
+  return 0;
+}

File src/dm/examples/tests/makefile

 EXAMPLESC       = ex1.c ex2.c ex3.c ex4.c ex5.c ex6.c ex7.c ex8.c ex9.c ex10.c\
                   ex11.c ex12.c ex12.m ex13.c ex14.c ex15.c ex16.c ex17.c ex18.c ex19.c \
 	          ex21.c ex22.c ex23.c ex24.c ex25.c ex26.c ex27.c ex28.c ex30.c \
-	          ex31.c ex32.c ex34.c ex36.c ex37.c ex38.c ex39.c ex40.c ex41.c
+	          ex31.c ex32.c ex34.c ex36.c ex37.c ex38.c ex39.c ex40.c ex41.c \
+	          ex42.c
 EXAMPLESF       =
 MANSEC          = DM
 
 ex41:ex41.o   chkopts
 	-${CLINKER} -o ex41 ex41.o  ${PETSC_DM_LIB}
 	${RM} -f ex41.o
+ex42:ex42.o   chkopts
+	-${CLINKER} -o ex42 ex42.o  ${PETSC_DM_LIB}
+	${RM} -f ex42.o
 #-------------------------------------------------------------------------------
 runex1:
 	-@${MPIEXEC} -n 2 ./ex1 -nox | grep -v -i Object > ex1_1.tmp 2>&1;	  \
 	-@${MPIEXEC} -n 2 ./ex37 ;\
 	  ${RM} -f ex32_1.tmp
 
+runex42:
+	-@${MPIEXEC} -n 2 ./ex42 ;\
+	  ${RM} -f 2d_nocoord.vtr 2d.vtr 3d_nocoord.vtr 3d.vtr
+
 
 TESTEXAMPLES_C		  = ex1.PETSc runex1 ex1.rm ex4.PETSc runex4 ex4.rm ex15.PETSc ex15.rm ex16.PETSc ex16.rm \
                             ex21.PETSc runex21 ex21.rm ex24.PETSc runex24 ex24.rm ex25.PETSc \
 TESTEXAMPLES_C_X_MPIUNI = ex1.PETSc ex1.rm ex2.PETSc ex2.rm ex3.PETSc ex3.rm ex4.PETSc ex4.rm ex5.PETSc ex5.rm\
                             ex6.PETSc ex6.rm ex12.PETSc ex12.rm
 TESTEXAMPLES_C_COMPLEX	  =
-TESTEXAMPLES_C_NOCOMPLEX  = ex16.PETSc runex16 runex16_2 ex16.rm
+TESTEXAMPLES_C_NOCOMPLEX  = ex16.PETSc runex16 runex16_2 ex16.rm ex42.PETSc runex42 ex42.rm
 TESTEXAMPLES_13		  = ex8.PETSc ex8.rm ex9.PETSc ex9.rm ex10.PETSc ex10.rm ex11.PETSc ex11.rm
 TESTEXAMPLES_MATLAB	  = ex12.PETSc runex12 ex12.rm