Actual source code: ex20.c


  2: static char help[] = "Tests converting a matrix to another format with MatConvert().\n\n";

  4: #include <petscmat.h>

  6: int main(int argc, char **args)
  7: {
  8:   Mat         C, A;
  9:   PetscInt    i, j, m = 5, n = 4, Ii, J;
 10:   PetscMPIInt rank, size;
 11:   PetscScalar v;
 12:   char        mtype[256];

 15:   PetscInitialize(&argc, &args, (char *)0, help);
 16:   MPI_Comm_rank(PETSC_COMM_WORLD, &rank);

 18:   /* This example does not work correctly for np > 2 */
 19:   MPI_Comm_size(PETSC_COMM_WORLD, &size);

 22:   /* Create the matrix for the five point stencil, YET AGAIN */
 23:   MatCreate(PETSC_COMM_WORLD, &C);
 24:   MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, m * n, m * n);
 25:   MatSetFromOptions(C);
 26:   MatSetUp(C);
 27:   for (i = 0; i < m; i++) {
 28:     for (j = 2 * rank; j < 2 * rank + 2; j++) {
 29:       v  = -1.0;
 30:       Ii = j + n * i;
 31:       if (i > 0) {
 32:         J = Ii - n;
 33:         MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES);
 34:       }
 35:       if (i < m - 1) {
 36:         J = Ii + n;
 37:         MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES);
 38:       }
 39:       if (j > 0) {
 40:         J = Ii - 1;
 41:         MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES);
 42:       }
 43:       if (j < n - 1) {
 44:         J = Ii + 1;
 45:         MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES);
 46:       }
 47:       v = 4.0;
 48:       MatSetValues(C, 1, &Ii, 1, &Ii, &v, INSERT_VALUES);
 49:     }
 50:   }

 52:   MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY);
 53:   MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY);
 54:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_INFO);
 55:   MatView(C, PETSC_VIEWER_STDOUT_WORLD);
 56:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 57:   MatView(C, PETSC_VIEWER_STDOUT_WORLD);

 59:   PetscStrcpy(mtype, MATSAME);
 60:   PetscOptionsGetString(NULL, NULL, "-conv_mat_type", mtype, sizeof(mtype), NULL);
 61:   MatConvert(C, mtype, MAT_INITIAL_MATRIX, &A);
 62:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_INFO);
 63:   MatView(A, PETSC_VIEWER_STDOUT_WORLD);
 64:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 65:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_IMPL);
 66:   MatView(A, PETSC_VIEWER_STDOUT_WORLD);
 67:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);

 69:   /* Free data structures */
 70:   MatDestroy(&A);
 71:   MatDestroy(&C);

 73:   PetscFinalize();
 74:   return 0;
 75: }

 77: /*TEST

 79:    test:
 80:       args: -conv_mat_type seqaij

 82: TEST*/