Actual source code: ex19.c

petsc-3.11.4 2019-09-28
Report Typos and Errors

  2: static char help[] = "Tests DMDA with variable multiple degrees of freedom per node.\n\n";

  4: /*
  5:    This code only compiles with gcc, since it is not ANSI C
  6: */

  8:  #include <petscdm.h>
  9:  #include <petscdmda.h>

 11: PetscErrorCode doit(DM da,Vec global)
 12: {
 14:   PetscInt       i,j,k,M,N,dof;

 16:   DMDAGetInfo(da,0,&M,&N,0,0,0,0,&dof,0,0,0,0,0);
 17:   {
 18:     struct {PetscScalar inside[dof];} **mystruct;
 19:     DMDAVecGetArrayRead(da,global,(void*) &mystruct);
 20:     for (i=0; i<N; i++) {
 21:       for (j=0; j<M; j++) {
 22:         for (k=0; k<dof; k++) {
 23:           PetscPrintf(PETSC_COMM_WORLD,"%D %D %g\n",i,j,(double)mystruct[i][j].inside[0]);

 25:           mystruct[i][j].inside[1] = 2.1;
 26:         }
 27:       }
 28:     }
 29:     DMDAVecRestoreArrayRead(da,global,(void*) &mystruct);
 30:   }
 31:   return(0);
 32: }

 34: int main(int argc,char **argv)
 35: {
 36:   PetscInt       dof = 2,M = 3,N = 3,m = PETSC_DECIDE,n = PETSC_DECIDE;
 38:   DM             da;
 39:   Vec            global,local;

 41:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 42:   PetscOptionsGetInt(NULL,0,"-dof",&dof,0);
 43:   /* Create distributed array and get vectors */
 44:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,m,n,dof,1,NULL,NULL,&da);
 45:   DMSetFromOptions(da);
 46:   DMSetUp(da);
 47:   DMCreateGlobalVector(da,&global);
 48:   DMCreateLocalVector(da,&local);

 50:   doit(da,global);


 53:   VecView(global,0);

 55:   /* Free memory */
 56:   VecDestroy(&local);
 57:   VecDestroy(&global);
 58:   DMDestroy(&da);
 59:   PetscFinalize();
 60:   return ierr;
 61: }