Actual source code: ex68.c
petsc-3.4.2 2013-07-02

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

      4: #include <petscmat.h>

      8: int main(int argc,char **argv)
      9: {
     10:   Mat            mat,B;
     12:   PetscInt       i,j;
     13:   PetscScalar    v;
     14:   IS             isrow,iscol;
     15:   PetscViewer    viewer;

     17:   PetscInitialize(&argc,&argv,(char*)0,help);

     20:   /* ------- Assemble matrix, --------- */

     22:   MatCreate(PETSC_COMM_WORLD,&mat);
     23:   MatSetSizes(mat,PETSC_DECIDE,PETSC_DECIDE,4,4);
     24:   MatSetFromOptions(mat);
     25:   MatSetUp(mat);

     27:   /* set anti-diagonal of matrix */
     28:   v    = 1.0;
     29:   i    = 0; j = 3;
     30:   MatSetValues(mat,1,&i,1,&j,&v,INSERT_VALUES);
     31:   v    = 2.0;
     32:   i    = 1; j = 2;
     33:   MatSetValues(mat,1,&i,1,&j,&v,INSERT_VALUES);
     34:   v    = 3.0;
     35:   i    = 2; j = 1;
     36:   MatSetValues(mat,1,&i,1,&j,&v,INSERT_VALUES);
     37:   v    = 4.0;
     38:   i    = 3; j = 0;
     39:   MatSetValues(mat,1,&i,1,&j,&v,INSERT_VALUES);

     41:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
     42:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);

     44:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
     45:   PetscViewerSetFormat(viewer,PETSC_VIEWER_ASCII_DENSE);
     46:   PetscViewerASCIIPrintf(viewer,"Original matrix\n");
     47:   MatView(mat,viewer);

     49:   MatGetOrdering(mat,MATORDERINGNATURAL,&isrow,&iscol);

     51:   MatPermute(mat,isrow,iscol,&B);
     52:   PetscViewerASCIIPrintf(viewer,"Original matrix permuted by identity\n");
     53:   MatView(B,viewer);
     54:   MatDestroy(&B);

     56:   MatReorderForNonzeroDiagonal(mat,1.e-8,isrow,iscol);
     57:   MatPermute(mat,isrow,iscol,&B);
     58:   PetscViewerASCIIPrintf(viewer,"Original matrix permuted by identity + NonzeroDiagonal()\n");
     59:   MatView(B,viewer);
     60:   PetscViewerASCIIPrintf(viewer,"Row permutation\n");
     61:   ISView(isrow,viewer);
     62:   PetscViewerASCIIPrintf(viewer,"Column permutation\n");
     63:   ISView(iscol,viewer);
     64:   MatDestroy(&B);

     66:   ISDestroy(&isrow);
     67:   ISDestroy(&iscol);

     69:   MatGetOrdering(mat,MATORDERINGND,&isrow,&iscol);
     70:   MatPermute(mat,isrow,iscol,&B);
     71:   PetscViewerASCIIPrintf(viewer,"Original matrix permuted by ND\n");
     72:   MatView(B,viewer);
     73:   MatDestroy(&B);
     74:   PetscViewerASCIIPrintf(viewer,"ND row permutation\n");
     75:   ISView(isrow,viewer);
     76:   PetscViewerASCIIPrintf(viewer,"ND column permutation\n");
     77:   ISView(iscol,viewer);

     79:   MatReorderForNonzeroDiagonal(mat,1.e-8,isrow,iscol);
     80:   MatPermute(mat,isrow,iscol,&B);
     81:   PetscViewerASCIIPrintf(viewer,"Original matrix permuted by ND + NonzeroDiagonal()\n");
     82:   MatView(B,viewer);
     83:   MatDestroy(&B);
     84:   PetscViewerASCIIPrintf(viewer,"ND + NonzeroDiagonal() row permutation\n");
     85:   ISView(isrow,viewer);
     86:   PetscViewerASCIIPrintf(viewer,"ND + NonzeroDiagonal() column permutation\n");
     87:   ISView(iscol,viewer);

     89:   ISDestroy(&isrow);
     90:   ISDestroy(&iscol);

     92:   MatGetOrdering(mat,MATORDERINGRCM,&isrow,&iscol);
     93:   MatPermute(mat,isrow,iscol,&B);
     94:   PetscViewerASCIIPrintf(viewer,"Original matrix permuted by RCM\n");
     95:   MatView(B,viewer);
     96:   MatDestroy(&B);
     97:   PetscViewerASCIIPrintf(viewer,"RCM row permutation\n");
     98:   ISView(isrow,viewer);
     99:   PetscViewerASCIIPrintf(viewer,"RCM column permutation\n");
    100:   ISView(iscol,viewer);

    102:   MatReorderForNonzeroDiagonal(mat,1.e-8,isrow,iscol);
    103:   MatPermute(mat,isrow,iscol,&B);
    104:   PetscViewerASCIIPrintf(viewer,"Original matrix permuted by RCM + NonzeroDiagonal()\n");
    105:   MatView(B,viewer);
    106:   MatDestroy(&B);
    107:   PetscViewerASCIIPrintf(viewer,"RCM + NonzeroDiagonal() row permutation\n");
    108:   ISView(isrow,viewer);
    109:   PetscViewerASCIIPrintf(viewer,"RCM + NonzeroDiagonal() column permutation\n");
    110:   ISView(iscol,viewer);

    112:   MatLUFactor(mat,isrow,iscol,NULL);
    113:   PetscViewerASCIIPrintf(viewer,"Factored matrix permuted by RCM + NonzeroDiagonal()\n");
    114:   MatView(mat,viewer);

    116:   /* Free data structures */
    117:   ISDestroy(&isrow);
    118:   ISDestroy(&iscol);
    119:   MatDestroy(&mat);

    121:   PetscFinalize();
    122:   return 0;
    123: }