Actual source code: ex15.c
1: /*$Id: ex15.c,v 1.22 2001/08/07 21:30:08 bsmith Exp $*/
3: static char help[] = "Tests MatNorm(), MatLUFactor(), MatSolve() and MatSolveAdd().\n\n";
5: #include petscmat.h
9: int main(int argc,char **args)
10: {
11: Mat C;
12: int i,j,m = 3,n = 3,I,J,ierr;
13: PetscTruth flg;
14: PetscScalar v,mone = -1.0,one = 1.0,alpha = 2.0;
15: IS perm,iperm;
16: Vec x,u,b,y;
17: PetscReal norm;
18: MatFactorInfo info;
20: PetscInitialize(&argc,&args,(char *)0,help);
22: MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n,&C);
23: MatSetFromOptions(C);
24: PetscOptionsHasName(PETSC_NULL,"-symmetric",&flg);
25: if (flg) { /* Treat matrix as symmetric only if we set this flag */
26: MatSetOption(C,MAT_SYMMETRIC);
27: MatSetOption(C,MAT_SYMMETRY_ETERNAL);
28: }
30: /* Create the matrix for the five point stencil, YET AGAIN */
31: for (i=0; i<m; i++) {
32: for (j=0; j<n; j++) {
33: v = -1.0; I = j + n*i;
34: if (i>0) {J = I - n; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
35: if (i<m-1) {J = I + n; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
36: if (j>0) {J = I - 1; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
37: if (j<n-1) {J = I + 1; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
38: v = 4.0; MatSetValues(C,1,&I,1,&I,&v,INSERT_VALUES);
39: }
40: }
41: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
42: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
43: MatGetOrdering(C,MATORDERING_RCM,&perm,&iperm);
44: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
45: ISView(perm,PETSC_VIEWER_STDOUT_SELF);
46: VecCreateSeq(PETSC_COMM_SELF,m*n,&u);
47: VecSet(&one,u);
48: VecDuplicate(u,&x);
49: VecDuplicate(u,&b);
50: VecDuplicate(u,&y);
51: MatMult(C,u,b);
52: VecCopy(b,y);
53: VecScale(&alpha,y);
55: MatNorm(C,NORM_FROBENIUS,&norm);
56: PetscPrintf(PETSC_COMM_SELF,"Frobenius norm of matrix %g\n",norm);
57: MatNorm(C,NORM_1,&norm);
58: PetscPrintf(PETSC_COMM_SELF,"One norm of matrix %g\n",norm);
59: MatNorm(C,NORM_INFINITY,&norm);
60: PetscPrintf(PETSC_COMM_SELF,"Infinity norm of matrix %g\n",norm);
62: info.fill = 2.0;
63: info.dtcol = 0.0;
64: info.damping = 0.0;
65: info.zeropivot = 1.e-14;
66: info.pivotinblocks = 1.0;
67: MatLUFactor(C,perm,iperm,&info);
68: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
70: /* Test MatSolve */
71: MatSolve(C,b,x);
72: VecView(b,PETSC_VIEWER_STDOUT_SELF);
73: VecView(x,PETSC_VIEWER_STDOUT_SELF);
74: VecAXPY(&mone,u,x);
75: VecNorm(x,NORM_2,&norm);
76: PetscPrintf(PETSC_COMM_SELF,"Norm of error %A\n",norm);
78: /* Test MatSolveAdd */
79: MatSolveAdd(C,b,y,x);
80: VecAXPY(&mone,y,x);
81: VecAXPY(&mone,u,x);
82: VecNorm(x,NORM_2,&norm);
84: PetscPrintf(PETSC_COMM_SELF,"Norm of error %A\n",norm);
86: ISDestroy(perm);
87: ISDestroy(iperm);
88: VecDestroy(u);
89: VecDestroy(y);
90: VecDestroy(b);
91: VecDestroy(x);
92: MatDestroy(C);
93: PetscFinalize();
94: return 0;
95: }