Actual source code: ex19.c
petsc-3.14.6 2021-03-30
2: static char help[] = "Tests reusing MPI parallel matrices and MatGetValues().\n\
3: To test the parallel matrix assembly, this example intentionally lays out\n\
4: the matrix across processors differently from the way it is assembled.\n\
5: This example uses bilinear elements on the unit square. Input arguments are:\n\
6: -m <size> : problem size\n\n";
8: #include <petscmat.h>
10: int FormElementStiffness(PetscReal H,PetscScalar *Ke)
11: {
13: Ke[0] = H/6.0; Ke[1] = -.125*H; Ke[2] = H/12.0; Ke[3] = -.125*H;
14: Ke[4] = -.125*H; Ke[5] = H/6.0; Ke[6] = -.125*H; Ke[7] = H/12.0;
15: Ke[8] = H/12.0; Ke[9] = -.125*H; Ke[10] = H/6.0; Ke[11] = -.125*H;
16: Ke[12] = -.125*H; Ke[13] = H/12.0; Ke[14] = -.125*H; Ke[15] = H/6.0;
17: return(0);
18: }
20: int main(int argc,char **args)
21: {
22: Mat C;
23: Vec u,b;
25: PetscMPIInt size,rank;
26: PetscInt i,m = 5,N,start,end,M,idx[4];
27: PetscInt j,nrsub,ncsub,*rsub,*csub,mystart,myend;
28: PetscBool flg;
29: PetscScalar one = 1.0,Ke[16],*vals;
30: PetscReal h,norm;
32: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
33: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
35: N = (m+1)*(m+1); /* dimension of matrix */
36: M = m*m; /* number of elements */
37: h = 1.0/m; /* mesh width */
38: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
39: MPI_Comm_size(PETSC_COMM_WORLD,&size);
41: /* Create stiffness matrix */
42: MatCreate(PETSC_COMM_WORLD,&C);
43: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N);
44: MatSetFromOptions(C);
45: MatSetUp(C);
47: start = rank*(M/size) + ((M%size) < rank ? (M%size) : rank);
48: end = start + M/size + ((M%size) > rank);
50: /* Form the element stiffness for the Laplacian */
51: FormElementStiffness(h*h,Ke);
52: for (i=start; i<end; i++) {
53: /* location of lower left corner of element */
54: /* node numbers for the four corners of element */
55: idx[0] = (m+1)*(i/m) + (i % m);
56: idx[1] = idx[0]+1; idx[2] = idx[1] + m + 1; idx[3] = idx[2] - 1;
57: MatSetValues(C,4,idx,4,idx,Ke,ADD_VALUES);
58: }
59: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
60: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
62: /* Assemble the matrix again */
63: MatZeroEntries(C);
65: for (i=start; i<end; i++) {
66: /* location of lower left corner of element */
67: /* node numbers for the four corners of element */
68: idx[0] = (m+1)*(i/m) + (i % m);
69: idx[1] = idx[0]+1; idx[2] = idx[1] + m + 1; idx[3] = idx[2] - 1;
70: MatSetValues(C,4,idx,4,idx,Ke,ADD_VALUES);
71: }
72: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
73: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
75: /* Create test vectors */
76: VecCreate(PETSC_COMM_WORLD,&u);
77: VecSetSizes(u,PETSC_DECIDE,N);
78: VecSetFromOptions(u);
79: VecDuplicate(u,&b);
80: VecSet(u,one);
82: /* Check error */
83: MatMult(C,u,b);
84: VecNorm(b,NORM_2,&norm);
85: if (norm > PETSC_SQRT_MACHINE_EPSILON) {
86: PetscPrintf(PETSC_COMM_WORLD,"Norm of error b %g should be near 0\n",(double)norm);
87: }
89: /* Now test MatGetValues() */
90: PetscOptionsHasName(NULL,NULL,"-get_values",&flg);
91: if (flg) {
92: MatGetOwnershipRange(C,&mystart,&myend);
93: nrsub = myend - mystart; ncsub = 4;
94: PetscMalloc1(nrsub*ncsub,&vals);
95: PetscMalloc1(nrsub,&rsub);
96: PetscMalloc1(ncsub,&csub);
97: for (i=myend-1; i>=mystart; i--) rsub[myend-i-1] = i;
98: for (i=0; i<ncsub; i++) csub[i] = 2*(ncsub-i) + mystart;
99: MatGetValues(C,nrsub,rsub,ncsub,csub,vals);
100: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
101: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"processor number %d: start=%D, end=%D, mystart=%D, myend=%D\n",rank,start,end,mystart,myend);
102: for (i=0; i<nrsub; i++) {
103: for (j=0; j<ncsub; j++) {
104: if (PetscImaginaryPart(vals[i*ncsub+j]) != 0.0) {
105: PetscSynchronizedPrintf(PETSC_COMM_WORLD," C[%D, %D] = %g + %g i\n",rsub[i],csub[j],(double)PetscRealPart(vals[i*ncsub+j]),(double)PetscImaginaryPart(vals[i*ncsub+j]));
106: } else {
107: PetscSynchronizedPrintf(PETSC_COMM_WORLD," C[%D, %D] = %g\n",rsub[i],csub[j],(double)PetscRealPart(vals[i*ncsub+j]));
108: }
109: }
110: }
111: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
112: PetscFree(rsub);
113: PetscFree(csub);
114: PetscFree(vals);
115: }
117: /* Free data structures */
118: VecDestroy(&u);
119: VecDestroy(&b);
120: MatDestroy(&C);
121: PetscFinalize();
122: return ierr;
123: }
128: /*TEST
130: test:
131: nsize: 4
133: TEST*/