1 2 static char help[] = "Tests DMDA with variable multiple degrees of freedom per node.\n\n"; 3 4 /* 5 This code only compiles with gcc, since it is not ANSI C 6 */ 7 8 #include <petscdm.h> 9 #include <petscdmda.h> 10 11 PetscErrorCode doit(DM da,Vec global) 12 { 13 PetscErrorCode ierr; 14 PetscInt i,j,k,M,N,dof; 15 16 PetscCall(DMDAGetInfo(da,0,&M,&N,0,0,0,0,&dof,0,0,0,0,0)); 17 { 18 struct {PetscScalar inside[dof];} **mystruct; 19 PetscCall(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 PetscCall(PetscPrintf(PETSC_COMM_WORLD,"%D %D %g\n",i,j,(double)mystruct[i][j].inside[0])); 24 25 mystruct[i][j].inside[1] = 2.1; 26 } 27 } 28 } 29 PetscCall(DMDAVecRestoreArrayRead(da,global,(void*) &mystruct)); 30 } 31 PetscFunctionReturn(0); 32 } 33 34 int main(int argc,char **argv) 35 { 36 PetscInt dof = 2,M = 3,N = 3,m = PETSC_DECIDE,n = PETSC_DECIDE; 37 PetscErrorCode ierr; 38 DM da; 39 Vec global,local; 40 41 PetscCall(PetscInitialize(&argc,&argv,(char*)0,help)); 42 PetscCall(PetscOptionsGetInt(NULL,0,"-dof",&dof,0)); 43 /* Create distributed array and get vectors */ 44 PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,m,n,dof,1,NULL,NULL,&da)); 45 PetscCall(DMSetFromOptions(da)); 46 PetscCall(DMSetUp(da)); 47 PetscCall(DMCreateGlobalVector(da,&global)); 48 PetscCall(DMCreateLocalVector(da,&local)); 49 50 PetscCall(doit(da,global)); 51 52 PetscCall(VecView(global,0)); 53 54 /* Free memory */ 55 PetscCall(VecDestroy(&local)); 56 PetscCall(VecDestroy(&global)); 57 PetscCall(DMDestroy(&da)); 58 PetscCall(PetscFinalize()); 59 return 0; 60 } 61