xref: /petsc/src/dm/tests/ex23.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 
2 static char help[] = "Tests VecView()/VecLoad() for DMDA vectors (this tests DMDAGlobalToNatural()).\n\n";
3 
4 #include <petscdm.h>
5 #include <petscdmda.h>
6 
7 int main(int argc,char **argv)
8 {
9   PetscMPIInt      size;
10   PetscInt         N = 6,m=PETSC_DECIDE,n=PETSC_DECIDE,p=PETSC_DECIDE,M=8,dof=1,stencil_width=1,P=5,pt = 0,st = 0;
11   PetscBool        flg2,flg3,native = PETSC_FALSE;
12   DMBoundaryType   bx           = DM_BOUNDARY_NONE,by = DM_BOUNDARY_NONE,bz = DM_BOUNDARY_NONE;
13   DMDAStencilType  stencil_type = DMDA_STENCIL_STAR;
14   DM               da;
15   Vec              global1,global2,global3,global4;
16   PetscScalar      mone = -1.0;
17   PetscReal        norm;
18   PetscViewer      viewer;
19   PetscRandom      rdm;
20 
21   PetscFunctionBeginUser;
22   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
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,"-dof",&dof,NULL));
27   PetscCall(PetscOptionsGetInt(NULL,NULL,"-stencil_width",&stencil_width,NULL));
28   PetscCall(PetscOptionsGetInt(NULL,NULL,"-periodic",&pt,NULL));
29   PetscCall(PetscOptionsGetBool(NULL,NULL,"-native",&native,NULL));
30   if (pt == 1) bx = DM_BOUNDARY_PERIODIC;
31   if (pt == 2) by = DM_BOUNDARY_PERIODIC;
32   if (pt == 3) {bx = DM_BOUNDARY_PERIODIC; by = DM_BOUNDARY_PERIODIC;}
33   if (pt == 4) bz = DM_BOUNDARY_PERIODIC;
34 
35   PetscCall(PetscOptionsGetInt(NULL,NULL,"-stencil_type",&st,NULL));
36   stencil_type = (DMDAStencilType) st;
37 
38   PetscCall(PetscOptionsHasName(NULL,NULL,"-one",&flg2));
39   PetscCall(PetscOptionsHasName(NULL,NULL,"-two",&flg2));
40   PetscCall(PetscOptionsHasName(NULL,NULL,"-three",&flg3));
41   if (flg2) {
42     PetscCall(DMDACreate2d(PETSC_COMM_WORLD,bx,by,stencil_type,M,N,m,n,dof,stencil_width,0,0,&da));
43   } else if (flg3) {
44     PetscCall(DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,m,n,p,dof,stencil_width,0,0,0,&da));
45   } else {
46     PetscCall(DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,stencil_width,NULL,&da));
47   }
48   PetscCall(DMSetFromOptions(da));
49   PetscCall(DMSetUp(da));
50 
51   PetscCall(DMCreateGlobalVector(da,&global1));
52   PetscCall(PetscRandomCreate(PETSC_COMM_WORLD,&rdm));
53   PetscCall(PetscRandomSetFromOptions(rdm));
54   PetscCall(DMCreateGlobalVector(da,&global2));
55   PetscCall(DMCreateGlobalVector(da,&global3));
56   PetscCall(DMCreateGlobalVector(da,&global4));
57 
58   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_WRITE,&viewer));
59   if (native) PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE));
60   PetscCall(VecSetRandom(global1,rdm));
61   PetscCall(VecView(global1,viewer));
62   PetscCall(VecSetRandom(global3,rdm));
63   PetscCall(VecView(global3,viewer));
64   if (native) PetscCall(PetscViewerPopFormat(viewer));
65   PetscCall(PetscViewerDestroy(&viewer));
66 
67   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer));
68   if (native) PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE));
69   PetscCall(VecLoad(global2,viewer));
70   PetscCall(VecLoad(global4,viewer));
71   if (native) PetscCall(PetscViewerPopFormat(viewer));
72   PetscCall(PetscViewerDestroy(&viewer));
73 
74   if (native) {
75     Vec filenative;
76     PetscBool same;
77     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer));
78     PetscCall(DMDACreateNaturalVector(da,&filenative));
79     /* DMDA "natural" Vec does not commandeer VecLoad.  The following load will only work when run on the same process
80      * layout, where as the standard VecView/VecLoad (using DMDA and not PETSC_VIEWER_NATIVE) can be read on a different
81      * number of processors. */
82     PetscCall(VecLoad(filenative,viewer));
83     PetscCall(VecEqual(global2,filenative,&same));
84     if (!same) {
85       PetscCall(PetscPrintf(PETSC_COMM_WORLD,"ex23: global vector does not match contents of file\n"));
86       PetscCall(VecView(global2,0));
87       PetscCall(VecView(filenative,0));
88     }
89     PetscCall(PetscViewerDestroy(&viewer));
90     PetscCall(VecDestroy(&filenative));
91   }
92 
93   PetscCall(VecAXPY(global2,mone,global1));
94   PetscCall(VecNorm(global2,NORM_MAX,&norm));
95   if (norm != 0.0) {
96     PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
97     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"ex23: Norm of difference %g should be zero\n",(double)norm));
98     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  Number of processors %d\n",size));
99     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  M,N,P,dof %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n",M,N,P,dof));
100     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  stencil_width %" PetscInt_FMT " stencil_type %d periodic %d\n",stencil_width,(int)stencil_type,(int)bx));
101     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  dimension %d\n",1 + (int) flg2 + (int) flg3));
102   }
103   PetscCall(VecAXPY(global4,mone,global3));
104   PetscCall(VecNorm(global4,NORM_MAX,&norm));
105   if (norm != 0.0) {
106     PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
107     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"ex23: Norm of difference %g should be zero\n",(double)norm));
108     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  Number of processors %d\n",size));
109     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  M,N,P,dof %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n",M,N,P,dof));
110     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  stencil_width %" PetscInt_FMT " stencil_type %d periodic %d\n",stencil_width,(int)stencil_type,(int)bx));
111     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  dimension %d\n",1 + (int) flg2 + (int) flg3));
112   }
113 
114   PetscCall(PetscRandomDestroy(&rdm));
115   PetscCall(DMDestroy(&da));
116   PetscCall(VecDestroy(&global1));
117   PetscCall(VecDestroy(&global2));
118   PetscCall(VecDestroy(&global3));
119   PetscCall(VecDestroy(&global4));
120   PetscCall(PetscFinalize());
121   return 0;
122 }
123 
124 /*TEST
125 
126    test:
127       nsize: {{1  3}}
128       args: -one -dof {{1 2 3}} -stencil_type {{0 1}}
129 
130    test:
131       suffix: 3
132       nsize: {{2 4}}
133       args: -two -dof {{1 3}} -stencil_type {{0 1}}
134 
135    test:
136       suffix: 4
137       nsize: {{1 4}}
138       args: -three -dof {{2 3}} -stencil_type {{0 1}}
139 
140    test:
141       suffix: 2
142       nsize: 2
143       args: -two -native
144 
145 TEST*/
146