xref: /petsc/src/dm/tests/ex22.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 
2 static char help[] = "Tests MatSetValuesBlockedStencil() in 3d.\n\n";
3 
4 #include <petscmat.h>
5 #include <petscdm.h>
6 #include <petscdmda.h>
7 
8 int main(int argc,char **argv)
9 {
10   PetscInt        M = 3,N = 4,P = 2,s = 1,w = 2,i, m = PETSC_DECIDE,n = PETSC_DECIDE,p = PETSC_DECIDE;
11   DM              da;
12   Mat             mat;
13   DMDAStencilType stencil_type = DMDA_STENCIL_BOX;
14   PetscBool       flg          = PETSC_FALSE;
15   MatStencil      idx[2],idy[2];
16   PetscScalar     *values;
17 
18   PetscFunctionBeginUser;
19   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
20   PetscCall(PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL));
21   PetscCall(PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL));
22   PetscCall(PetscOptionsGetInt(NULL,NULL,"-P",&P,NULL));
23   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
24   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
25   PetscCall(PetscOptionsGetInt(NULL,NULL,"-p",&p,NULL));
26   PetscCall(PetscOptionsGetInt(NULL,NULL,"-s",&s,NULL));
27   PetscCall(PetscOptionsGetInt(NULL,NULL,"-w",&w,NULL));
28   PetscCall(PetscOptionsGetBool(NULL,NULL,"-star",&flg,NULL));
29   if (flg) stencil_type =  DMDA_STENCIL_STAR;
30 
31   /* Create distributed array and get vectors */
32   PetscCall(DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,stencil_type,M,N,P,m,n,p,w,s,0,0,0,&da));
33   PetscCall(DMSetFromOptions(da));
34   PetscCall(DMSetUp(da));
35   PetscCall(DMSetMatType(da,MATMPIBAIJ));
36   PetscCall(DMCreateMatrix(da,&mat));
37 
38   idx[0].i = 1;   idx[0].j = 1; idx[0].k = 0;
39   idx[1].i = 2;   idx[1].j = 1; idx[1].k = 0;
40   idy[0].i = 1;   idy[0].j = 2; idy[0].k = 0;
41   idy[1].i = 2;   idy[1].j = 2; idy[1].k = 0;
42   PetscCall(PetscMalloc1(2*2*w*w,&values));
43   for (i=0; i<2*2*w*w; i++) values[i] = i;
44   PetscCall(MatSetValuesBlockedStencil(mat,2,idx,2,idy,values,INSERT_VALUES));
45   PetscCall(MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY));
46   PetscCall(MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY));
47 
48   /* Free memory */
49   PetscCall(PetscFree(values));
50   PetscCall(MatDestroy(&mat));
51   PetscCall(DMDestroy(&da));
52   PetscCall(PetscFinalize());
53   return 0;
54 }
55