Actual source code: ex129.c

petsc-dev 2014-02-02
Report Typos and Errors
  2: /*
  3:   Laplacian in 3D. Use for testing MatSolve routines.
  4:   Modeled by the partial differential equation

  6:    - Laplacian u = 1,0 < x,y,z < 1,

  8:    with boundary conditions
  9:    u = 1 for x = 0, x = 1, y = 0, y = 1, z = 0, z = 1.
 10: */

 12: static char help[] = "This example is for testing different MatSolve routines :MatSolve(), MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), and MatMatSolve().\n\
 13: Example usage: ./ex129 -mat_type aij -dof 2\n\n";

 15: #include <petscdmda.h>

 17: extern PetscErrorCode ComputeMatrix(DM,Mat);
 18: extern PetscErrorCode ComputeRHS(DM,Vec);
 19: extern PetscErrorCode ComputeRHSMatrix(PetscInt,PetscInt,Mat*);

 23: int main(int argc,char **args)
 24: {
 26:   PetscMPIInt    size;
 27:   Vec            x,b,y,b1;
 28:   DM             da;
 29:   Mat            A,F,RHS,X,C1;
 30:   MatFactorInfo  info;
 31:   IS             perm,iperm;
 32:   PetscInt       dof =1,M=-8,m,n,nrhs;
 33:   PetscScalar    one = 1.0;
 34:   PetscReal      norm,tol=1.e-13;
 35:   PetscBool      InplaceLU=PETSC_FALSE;

 37:   PetscInitialize(&argc,&args,(char*)0,help);
 38:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 39:   if (size != 1) SETERRQ(PETSC_COMM_WORLD,1,"This is a uniprocessor example only\n");
 40:   PetscOptionsGetInt(NULL,"-dof",&dof,NULL);
 41:   PetscOptionsGetInt(NULL,"-M",&M,NULL);

 43:   DMDACreate(PETSC_COMM_WORLD,&da);
 44:   DMDASetDim(da,3);
 45:   DMDASetBoundaryType(da,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE);
 46:   DMDASetStencilType(da,DMDA_STENCIL_STAR);
 47:   DMDASetSizes(da,M,M,M);
 48:   DMDASetNumProcs(da,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE);
 49:   DMDASetDof(da,dof);
 50:   DMDASetStencilWidth(da,1);
 51:   DMDASetOwnershipRanges(da,NULL,NULL,NULL);
 52:   DMSetFromOptions(da);
 53:   DMSetUp(da);

 55:   DMCreateGlobalVector(da,&x);
 56:   DMCreateGlobalVector(da,&b);
 57:   VecDuplicate(b,&y);
 58:   ComputeRHS(da,b);
 59:   VecSet(y,one);
 60:   DMSetMatType(da,MATBAIJ);
 61:   DMCreateMatrix(da,&A);
 62:   ComputeMatrix(da,A);
 63:   MatGetSize(A,&m,&n);
 64:   nrhs = 2;
 65:   PetscOptionsGetInt(NULL,"-nrhs",&nrhs,NULL);
 66:   ComputeRHSMatrix(m,nrhs,&RHS);
 67:   MatDuplicate(RHS,MAT_DO_NOT_COPY_VALUES,&X);

 69:   MatGetOrdering(A,MATORDERINGND,&perm,&iperm);


 72:   PetscOptionsGetBool(NULL,"-inplacelu",&InplaceLU,NULL);
 73:   MatFactorInfoInitialize(&info);
 74:   if (!InplaceLU) {
 75:     MatGetFactor(A,MATSOLVERPETSC,MAT_FACTOR_LU,&F);
 76:     info.fill = 5.0;
 77:     MatLUFactorSymbolic(F,A,perm,iperm,&info);
 78:     MatLUFactorNumeric(F,A,&info);
 79:   } else { /* Test inplace factorization */
 80:     MatDuplicate(A,MAT_COPY_VALUES,&F);
 81:     /* or create F without DMDA
 82:     MatType     type;
 83:     PetscInt          i,ncols;
 84:     const PetscInt    *cols;
 85:     const PetscScalar *vals;
 86:     MatGetSize(A,&m,&n);
 87:     MatGetType(A,&type);
 88:     MatCreate(PetscObjectComm((PetscObject)A),&F);
 89:     MatSetSizes(F,PETSC_DECIDE,PETSC_DECIDE,m,n);
 90:     MatSetType(F,type);
 91:     MatSetFromOptions(F);
 92:     for (i=0; i<m; i++) {
 93:       MatGetRow(A,i,&ncols,&cols,&vals);
 94:       MatSetValues(F,1,&i,ncols,cols,vals,INSERT_VALUES);
 95:     }
 96:     MatAssemblyBegin(F,MAT_FINAL_ASSEMBLY);
 97:     MatAssemblyEnd(F,MAT_FINAL_ASSEMBLY);
 98:     */
 99:     MatLUFactor(F,perm,iperm,&info);
100:   }

102:   VecDuplicate(y,&b1);

104:   /* MatSolve */
105:   MatSolve(F,b,x);
106:   MatMult(A,x,b1);
107:   VecAXPY(b1,-1.0,b);
108:   VecNorm(b1,NORM_2,&norm);
109:   if (norm > tol) {
110:     PetscPrintf(PETSC_COMM_WORLD,"MatSolve              : Error of norm %g\n",(double)norm);
111:   }

113:   /* MatSolveTranspose */
114:   MatSolveTranspose(F,b,x);
115:   MatMultTranspose(A,x,b1);
116:   VecAXPY(b1,-1.0,b);
117:   VecNorm(b1,NORM_2,&norm);
118:   if (norm > tol) {
119:     PetscPrintf(PETSC_COMM_WORLD,"MatSolveTranspose     : Error of norm %g\n",(double)norm);
120:   }

122:   /* MatSolveAdd */
123:   MatSolveAdd(F,b,y,x);
124:   MatMult(A,y,b1);
125:   VecScale(b1,-1.0);
126:   MatMultAdd(A,x,b1,b1);
127:   VecAXPY(b1,-1.0,b);
128:   VecNorm(b1,NORM_2,&norm);
129:   if (norm > tol) {
130:     PetscPrintf(PETSC_COMM_WORLD,"MatSolveAdd           : Error of norm %g\n",(double)norm);
131:   }

133:   /* MatSolveTransposeAdd */
134:   MatSolveTransposeAdd(F,b,y,x);
135:   MatMultTranspose(A,y,b1);
136:   VecScale(b1,-1.0);
137:   MatMultTransposeAdd(A,x,b1,b1);
138:   VecAXPY(b1,-1.0,b);
139:   VecNorm(b1,NORM_2,&norm);
140:   if (norm > tol) {
141:     PetscPrintf(PETSC_COMM_WORLD,"MatSolveTransposeAdd  : Error of norm %g\n",(double)norm);
142:   }

144:   /* MatMatSolve */
145:   MatMatSolve(F,RHS,X);
146:   MatMatMult(A,X,MAT_INITIAL_MATRIX,2.0,&C1);
147:   MatAXPY(C1,-1.0,RHS,SAME_NONZERO_PATTERN);
148:   MatNorm(C1,NORM_FROBENIUS,&norm);
149:   if (norm > tol) {
150:     PetscPrintf(PETSC_COMM_WORLD,"MatMatSolve           : Error of norm %g\n",(double)norm);
151:   }

153:   VecDestroy(&x);
154:   VecDestroy(&b);
155:   VecDestroy(&b1);
156:   VecDestroy(&y);
157:   MatDestroy(&A);
158:   MatDestroy(&F);
159:   MatDestroy(&RHS);
160:   MatDestroy(&C1);
161:   MatDestroy(&X);
162:   ISDestroy(&perm);
163:   ISDestroy(&iperm);
164:   DMDestroy(&da);
165:   PetscFinalize();
166:   return 0;
167: }

171: PetscErrorCode ComputeRHS(DM da,Vec b)
172: {
174:   PetscInt       mx,my,mz;
175:   PetscScalar    h;

178:   DMDAGetInfo(da,0,&mx,&my,&mz,0,0,0,0,0,0,0,0,0);
179:   h    = 1.0/((mx-1)*(my-1)*(mz-1));
180:   VecSet(b,h);
181:   return(0);
182: }

186: PetscErrorCode ComputeRHSMatrix(PetscInt m,PetscInt nrhs,Mat *C)
187: {
189:   PetscRandom    rand;
190:   Mat            RHS;
191:   PetscScalar    *array,rval;
192:   PetscInt       i,k;

195:   MatCreate(PETSC_COMM_WORLD,&RHS);
196:   MatSetSizes(RHS,m,PETSC_DECIDE,PETSC_DECIDE,nrhs);
197:   MatSetType(RHS,MATSEQDENSE);
198:   MatSetUp(RHS);

200:   PetscRandomCreate(PETSC_COMM_WORLD,&rand);
201:   PetscRandomSetFromOptions(rand);
202:   MatDenseGetArray(RHS,&array);
203:   for (i=0; i<m; i++) {
204:     PetscRandomGetValue(rand,&rval);
205:     array[i] = rval;
206:   }
207:   if (nrhs > 1) {
208:     for (k=1; k<nrhs; k++) {
209:       for (i=0; i<m; i++) {
210:         array[m*k+i] = array[i];
211:       }
212:     }
213:   }
214:   MatDenseRestoreArray(RHS,&array);
215:   MatAssemblyBegin(RHS,MAT_FINAL_ASSEMBLY);
216:   MatAssemblyEnd(RHS,MAT_FINAL_ASSEMBLY);
217:   *C   = RHS;
218:   PetscRandomDestroy(&rand);
219:   return(0);
220: }


225: PetscErrorCode ComputeMatrix(DM da,Mat B)
226: {
228:   PetscInt       i,j,k,mx,my,mz,xm,ym,zm,xs,ys,zs,dof,k1,k2,k3;
229:   PetscScalar    *v,*v_neighbor,Hx,Hy,Hz,HxHydHz,HyHzdHx,HxHzdHy,r1,r2;
230:   MatStencil     row,col;
231:   PetscRandom    rand;

234:   PetscRandomCreate(PETSC_COMM_WORLD,&rand);
235:   PetscRandomSetType(rand,PETSCRAND);
236:   PetscRandomSetSeed(rand,1);
237:   PetscRandomSetInterval(rand,-.001,.001);
238:   PetscRandomSetFromOptions(rand);

240:   DMDAGetInfo(da,0,&mx,&my,&mz,0,0,0,&dof,0,0,0,0,0);
241:   /* For simplicity, this example only works on mx=my=mz */
242:   if (mx != my || mx != mz) SETERRQ3(PETSC_COMM_SELF,1,"This example only works with mx %d = my %d = mz %d\n",mx,my,mz);

244:   Hx      = 1.0 / (PetscReal)(mx-1); Hy = 1.0 / (PetscReal)(my-1); Hz = 1.0 / (PetscReal)(mz-1);
245:   HxHydHz = Hx*Hy/Hz; HxHzdHy = Hx*Hz/Hy; HyHzdHx = Hy*Hz/Hx;

247:   PetscMalloc1((2*dof*dof+1),&v);
248:   v_neighbor = v + dof*dof;
249:   PetscMemzero(v,(2*dof*dof+1)*sizeof(PetscScalar));
250:   k3         = 0;
251:   for (k1=0; k1<dof; k1++) {
252:     for (k2=0; k2<dof; k2++) {
253:       if (k1 == k2) {
254:         v[k3]          = 2.0*(HxHydHz + HxHzdHy + HyHzdHx);
255:         v_neighbor[k3] = -HxHydHz;
256:       } else {
257:         PetscRandomGetValue(rand,&r1);
258:         PetscRandomGetValue(rand,&r2);

260:         v[k3]          = r1;
261:         v_neighbor[k3] = r2;
262:       }
263:       k3++;
264:     }
265:   }
266:   DMDAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);

268:   for (k=zs; k<zs+zm; k++) {
269:     for (j=ys; j<ys+ym; j++) {
270:       for (i=xs; i<xs+xm; i++) {
271:         row.i = i; row.j = j; row.k = k;
272:         if (i==0 || j==0 || k==0 || i==mx-1 || j==my-1 || k==mz-1) { /* boudary points */
273:           MatSetValuesBlockedStencil(B,1,&row,1,&row,v,INSERT_VALUES);
274:         } else { /* interior points */
275:           /* center */
276:           col.i = i; col.j = j; col.k = k;
277:           MatSetValuesBlockedStencil(B,1,&row,1,&col,v,INSERT_VALUES);

279:           /* x neighbors */
280:           col.i = i-1; col.j = j; col.k = k;
281:           MatSetValuesBlockedStencil(B,1,&row,1,&col,v_neighbor,INSERT_VALUES);
282:           col.i = i+1; col.j = j; col.k = k;
283:           MatSetValuesBlockedStencil(B,1,&row,1,&col,v_neighbor,INSERT_VALUES);

285:           /* y neighbors */
286:           col.i = i; col.j = j-1; col.k = k;
287:           MatSetValuesBlockedStencil(B,1,&row,1,&col,v_neighbor,INSERT_VALUES);
288:           col.i = i; col.j = j+1; col.k = k;
289:           MatSetValuesBlockedStencil(B,1,&row,1,&col,v_neighbor,INSERT_VALUES);

291:           /* z neighbors */
292:           col.i = i; col.j = j; col.k = k-1;
293:           MatSetValuesBlockedStencil(B,1,&row,1,&col,v_neighbor,INSERT_VALUES);
294:           col.i = i; col.j = j; col.k = k+1;
295:           MatSetValuesBlockedStencil(B,1,&row,1,&col,v_neighbor,INSERT_VALUES);
296:         }
297:       }
298:     }
299:   }
300:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
301:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
302:   PetscFree(v);
303:   PetscRandomDestroy(&rand);
304:   return(0);
305: }