Actual source code: ex42.c

petsc-dev 2014-02-02
Report Typos and Errors
  1: /* -*- Mode: C++; c-basic-offset:2 ; indent-tabs-mode:nil ; -*- */

  3: static char help[] = "Test VTK Rectilinear grid (.vtr) viewer support\n\n";

  5: #include <mpi.h>
  6: #include <petscdmda.h>

  8: /*
  9:   Write 3D DMDA vector with coordinates in VTK VTR format

 11: */
 12: PetscErrorCode test_3d(const char filename[])
 13: {
 14:   MPI_Comm          comm = MPI_COMM_WORLD;
 15:   const PetscInt    M=10, N=15, P=30, dof=1, sw=1;
 16:   const PetscScalar Lx=1.0, Ly=1.0, Lz=1.0;
 17:   DM                da;
 18:   Vec               v;
 19:   PetscViewer       view;
 20:   DMDALocalInfo     info;
 21:   PetscScalar       ***va;
 22:   PetscInt          i,j,k;
 23:   PetscErrorCode    ierr;

 25:   DMDACreate3d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,
 26:                       DMDA_STENCIL_STAR, M,N,P,
 27:                       PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,dof,sw,NULL,NULL,NULL,&da);

 29:   DMDASetUniformCoordinates(da,0.0,Lx,0.0,Ly,0.0,Lz);
 30:   DMDAGetLocalInfo(da,&info);
 31:   DMCreateGlobalVector(da,&v);
 32:   DMDAVecGetArray(da,v,&va);
 33:   for (k=info.zs; k<info.zs+info.zm; k++) {
 34:     for (j=info.ys; j<info.ys+info.ym; j++) {
 35:       for (i=info.xs; i<info.xs+info.xm; i++) {
 36:         PetscScalar x = (Lx*i)/M;
 37:         PetscScalar y = (Ly*j)/N;
 38:         PetscScalar z = (Lz*k)/P;
 39:         va[k][j][i] = PetscPowScalarInt(x-0.5*Lx,2)+PetscPowScalarInt(y-0.5*Ly,2)+PetscPowScalarInt(z-0.5*Lz,2);
 40:       }
 41:     }
 42:   }
 43:   DMDAVecRestoreArray(da,v,&va);
 44:   PetscViewerVTKOpen(comm,filename,FILE_MODE_WRITE,&view);
 45:   VecView(v,view);
 46:   PetscViewerDestroy(&view);
 47:   VecDestroy(&v);
 48:   DMDestroy(&da);
 49:   return 0;
 50: }


 53: /*
 54:   Write 2D DMDA vector with coordinates in VTK VTR format

 56: */
 57: PetscErrorCode test_2d(const char filename[])
 58: {
 59:   MPI_Comm          comm = MPI_COMM_WORLD;
 60:   const PetscInt    M=10, N=20, dof=1, sw=1;
 61:   const PetscScalar Lx=1.0, Ly=1.0, Lz=1.0;
 62:   DM                da;
 63:   Vec               v;
 64:   PetscViewer       view;
 65:   DMDALocalInfo     info;
 66:   PetscScalar       **va;
 67:   PetscInt          i,j;
 68:   PetscErrorCode    ierr;

 70:   DMDACreate2d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,
 71:                       DMDA_STENCIL_STAR, M,N,
 72:                       PETSC_DECIDE,PETSC_DECIDE,dof,sw,NULL,NULL,&da);
 73:   DMDASetUniformCoordinates(da,0.0,Lx,0.0,Ly,0.0,Lz);
 74:   DMDAGetLocalInfo(da,&info);
 75:   DMCreateGlobalVector(da,&v);
 76:   DMDAVecGetArray(da,v,&va);
 77:   for (j=info.ys; j<info.ys+info.ym; j++) {
 78:     for (i=info.xs; i<info.xs+info.xm; i++) {
 79:       PetscScalar x = (Lx*i)/M;
 80:       PetscScalar y = (Ly*j)/N;
 81:       va[j][i] = PetscPowScalarInt(x-0.5*Lx,2)+PetscPowScalarInt(y-0.5*Ly,2);
 82:     }
 83:   }
 84:   DMDAVecRestoreArray(da,v,&va);
 85:   PetscViewerVTKOpen(comm,filename,FILE_MODE_WRITE,&view);
 86:   VecView(v,view);
 87:   PetscViewerDestroy(&view);
 88:   VecDestroy(&v);
 89:   DMDestroy(&da);
 90:   return 0;
 91: }


 94: /*
 95:   Write 2D DMDA vector without coordinates in VTK VTR format

 97: */
 98: PetscErrorCode test_2d_nocoord(const char filename[])
 99: {
100:   MPI_Comm          comm = MPI_COMM_WORLD;
101:   const PetscInt    M=10, N=20, dof=1, sw=1;
102:   const PetscScalar Lx=1.0, Ly=1.0;
103:   DM                da;
104:   Vec               v;
105:   PetscViewer       view;
106:   DMDALocalInfo     info;
107:   PetscScalar       **va;
108:   PetscInt          i,j;
109:   PetscErrorCode    ierr;

111:   DMDACreate2d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,
112:                       DMDA_STENCIL_STAR, M,N,
113:                       PETSC_DECIDE,PETSC_DECIDE,dof,sw,NULL,NULL,&da);

115:   DMDAGetLocalInfo(da,&info);
116:   DMCreateGlobalVector(da,&v);
117:   DMDAVecGetArray(da,v,&va);
118:   for (j=info.ys; j<info.ys+info.ym; j++) {
119:     for (i=info.xs; i<info.xs+info.xm; i++) {
120:       PetscScalar x = (Lx*i)/M;
121:       PetscScalar y = (Ly*j)/N;
122:       va[j][i] = PetscPowScalarInt(x-0.5*Lx,2)+PetscPowScalarInt(y-0.5*Ly,2);
123:     }
124:   }
125:   DMDAVecRestoreArray(da,v,&va);
126:   PetscViewerVTKOpen(comm,filename,FILE_MODE_WRITE,&view);
127:   VecView(v,view);
128:   PetscViewerDestroy(&view);
129:   VecDestroy(&v);
130:   DMDestroy(&da);
131:   return 0;
132: }


135: /*
136:   Write 3D DMDA vector without coordinates in VTK VTR format

138: */
139: PetscErrorCode test_3d_nocoord(const char filename[])
140: {
141:   MPI_Comm          comm = MPI_COMM_WORLD;
142:   const PetscInt    M=10, N=20, P=30, dof=1, sw=1;
143:   const PetscScalar Lx=1.0, Ly=1.0, Lz=1.0;
144:   DM                da;
145:   Vec               v;
146:   PetscViewer       view;
147:   DMDALocalInfo     info;
148:   PetscScalar       ***va;
149:   PetscInt          i,j,k;
150:   PetscErrorCode    ierr;

152:   DMDACreate3d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,
153:                       DMDA_STENCIL_STAR, M,N,P,
154:                       PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,dof,sw,NULL,NULL,NULL,&da);

156:   DMDAGetLocalInfo(da,&info);
157:   DMCreateGlobalVector(da,&v);
158:   DMDAVecGetArray(da,v,&va);
159:   for (k=info.zs; k<info.zs+info.zm; k++) {
160:     for (j=info.ys; j<info.ys+info.ym; j++) {
161:       for (i=info.xs; i<info.xs+info.xm; i++) {
162:         PetscScalar x = (Lx*i)/M;
163:         PetscScalar y = (Ly*j)/N;
164:         PetscScalar z = (Lz*k)/P;
165:         va[k][j][i] = PetscPowScalarInt(x-0.5*Lx,2)+PetscPowScalarInt(y-0.5*Ly,2)+PetscPowScalarInt(z-0.5*Lz,2);
166:       }
167:     }
168:   }
169:   DMDAVecRestoreArray(da,v,&va);
170:   PetscViewerVTKOpen(comm,filename,FILE_MODE_WRITE,&view);
171:   VecView(v,view);
172:   PetscViewerDestroy(&view);
173:   VecDestroy(&v);
174:   DMDestroy(&da);
175:   return 0;
176: }

178: int main(int argc, char *argv[])
179: {
181:   PetscInitialize(&argc,&argv,0,help);
182:   test_3d("3d.vtr");
183:   test_2d("2d.vtr");
184:   test_2d_nocoord("2d_nocoord.vtr");
185:   test_3d_nocoord("3d_nocoord.vtr");
186:   PetscFinalize();
187:   return 0;
188: }