Actual source code: ex17.c

  1: /*$Id: ex17.c,v 1.21 2001/08/07 21:30:08 bsmith Exp $*/

  3: static char help[] = "Tests the use of MatSolveTranspose().nn";

 5:  #include petscmat.h

  7: int main(int argc,char **args)
  8: {
  9:   Mat          C,A;
 10:   int          i,j,m = 5,n = 5,I,J,ierr;
 11:   PetscScalar  v,five = 5.0,one = 1.0,mone = -1.0;
 12:   IS           isrow,row,col;
 13:   Vec          x,u,b;
 14:   PetscReal    norm;

 16:   PetscInitialize(&argc,&args,(char *)0,help);
 17:   PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
 18:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);

 20:   MatCreateSeqAIJ(PETSC_COMM_SELF,m*n,m*n,5,PETSC_NULL,&C);

 22:   /* create the matrix for the five point stencil, YET AGAIN*/
 23:   for (i=0; i<m; i++) {
 24:     for (j=0; j<n; j++) {
 25:       v = -1.0;  I = j + n*i;
 26:       if (i>0)   {J = I - n; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
 27:       if (i<m-1) {J = I + n; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
 28:       if (j>0)   {J = I - 1; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
 29:       if (j<n-1) {J = I + 1; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
 30:       v = 4.0; MatSetValues(C,1,&I,1,&I,&v,INSERT_VALUES);
 31:     }
 32:   }
 33:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 34:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

 36:   ISCreateStride(PETSC_COMM_SELF,(m*n)/2,0,2,&isrow);
 37:   MatZeroRows(C,isrow,&five);

 39:   VecCreateSeq(PETSC_COMM_SELF,m*n,&u);
 40:   VecDuplicate(u,&x);
 41:   VecDuplicate(u,&b);
 42:   VecSet(&one,u);

 44:   MatMultTranspose(C,u,b);

 46:   /* Set default ordering to be Quotient Minimum Degree; also read
 47:      orderings from the options database */
 48:   MatGetOrdering(C,MATORDERING_QMD,&row,&col);

 50:   MatLUFactorSymbolic(C,row,col,PETSC_NULL,&A);
 51:   MatLUFactorNumeric(C,&A);
 52:   MatSolveTranspose(A,b,x);

 54:   ISView(row,PETSC_VIEWER_STDOUT_SELF);
 55:   VecAXPY(&mone,u,x);
 56:   VecNorm(x,NORM_2,&norm);
 57:   PetscPrintf(PETSC_COMM_SELF,"Norm of error %gn",norm);

 59:   ISDestroy(row);
 60:   ISDestroy(col);
 61:   ISDestroy(isrow);
 62:   VecDestroy(u);
 63:   VecDestroy(x);
 64:   VecDestroy(b);
 65:   MatDestroy(C);
 66:   MatDestroy(A);
 67:   PetscFinalize();
 68:   return 0;
 69: }