Actual source code: ex28.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  2: static char help[] = "Tests MatReorderForNonzeroDiagonal()\n\n";

  4:  #include <petscmat.h>

  6: int main(int argc,char **args)
  7: {
  8:   Mat            A,LU;
  9:   Vec            x,y;
 10:   PetscInt       nnz[4]={2,1,1,1},col[4],i;
 12:   PetscScalar    values[4];
 13:   IS             rowperm,colperm;

 15:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 16:   MatCreateSeqAIJ(PETSC_COMM_WORLD,4,4,2,nnz,&A);

 18:   /* build test matrix */
 19:   values[0]=1.0;values[1]=-1.0;
 20:   col[0]   =0;col[1]=2; i=0;
 21:   MatSetValues(A,1,&i,2,col,values,INSERT_VALUES);
 22:   values[0]=1.0;
 23:   col[0]   =1;i=1;
 24:   MatSetValues(A,1,&i,1,col,values,INSERT_VALUES);
 25:   values[0]=-1.0;
 26:   col[0]   =3;i=2;
 27:   MatSetValues(A,1,&i,1,col,values,INSERT_VALUES);
 28:   values[0]=1.0;
 29:   col[0]   =2;i=3;

 31:   MatSetValues(A,1,&i,1,col,values,INSERT_VALUES);
 32:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 33:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 34:   MatView(A,PETSC_VIEWER_STDOUT_SELF);

 36:   MatGetOrdering(A,MATORDERINGNATURAL,&rowperm,&colperm);
 37:   MatReorderForNonzeroDiagonal(A,1.e-12,rowperm,colperm);
 38:   PetscPrintf(PETSC_COMM_SELF,"column and row perms\n");
 39:   ISView(rowperm,0);
 40:   ISView(colperm,0);
 41:   MatGetFactor(A,MATSOLVERPETSC,MAT_FACTOR_LU,&LU);
 42:   MatLUFactorSymbolic(LU,A,rowperm,colperm,NULL);
 43:   MatLUFactorNumeric(LU,A,NULL);
 44:   MatView(LU,PETSC_VIEWER_STDOUT_SELF);
 45:   VecCreate(PETSC_COMM_WORLD,&x);
 46:   VecSetSizes(x,PETSC_DECIDE,4);
 47:   VecSetFromOptions(x);
 48:   VecDuplicate(x,&y);

 50:   values[0]=0;values[1]=1.0;values[2]=-1.0;values[3]=1.0;
 51:   for (i=0; i<4; i++) col[i]=i;
 52:   VecSetValues(x,4,col,values,INSERT_VALUES);
 53:   VecAssemblyBegin(x);
 54:   VecAssemblyEnd(x);
 55:   VecView(x,PETSC_VIEWER_STDOUT_SELF);

 57:   MatSolve(LU,x,y);
 58:   VecView(y,PETSC_VIEWER_STDOUT_SELF);

 60:   ISDestroy(&rowperm);
 61:   ISDestroy(&colperm);
 62:   MatDestroy(&LU);
 63:   MatDestroy(&A);
 64:   VecDestroy(&x);
 65:   VecDestroy(&y);
 66:   PetscFinalize();
 67:   return ierr;
 68: }