Actual source code: ex259.c

  1: static char help[] = "Test of setting values in a matrix without preallocation\n\n";

  3: #include <petscmat.h>

  5: PetscErrorCode ex1_nonsquare_bs1(void)
  6: {
  7:   Mat       A;
  8:   PetscInt  M, N, m, n, bs = 1;
  9:   char      type[16];
 10:   PetscBool flg;

 12:   /*
 13:      Create the matrix
 14:   */
 15:   PetscFunctionBegin;
 16:   M = 10;
 17:   N = 12;
 18:   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
 19:   PetscCall(PetscOptionsGetString(NULL, NULL, "-type", type, sizeof(type), &flg));
 20:   if (flg) PetscCall(MatSetType(A, type));
 21:   else PetscCall(MatSetType(A, MATAIJ));
 22:   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, M, N));
 23:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-bs", &bs, NULL));
 24:   PetscCall(MatSetBlockSize(A, bs));
 25:   PetscCall(MatSetFromOptions(A));

 27:   /*
 28:      Get the sizes of the matrix
 29:   */
 30:   PetscCall(MatGetLocalSize(A, &m, &n));

 32:   /*
 33:      Insert non-zero pattern (e.g. perform a sweep over the grid).
 34:      You can use MatSetValues(), MatSetValuesBlocked() or MatSetValue().
 35:   */
 36:   {
 37:     PetscInt    ii, jj;
 38:     PetscScalar vv = 22.0;

 40:     ii = 3;
 41:     jj = 3;
 42:     PetscCall(MatSetValue(A, ii, jj, vv, INSERT_VALUES));

 44:     ii = 7;
 45:     jj = 4;
 46:     PetscCall(MatSetValue(A, ii, jj, vv, INSERT_VALUES));
 47:     PetscCall(MatSetValue(A, jj, ii, vv, INSERT_VALUES));

 49:     ii = 9;
 50:     jj = 7;
 51:     PetscCall(MatSetValue(A, ii, jj, vv, INSERT_VALUES));
 52:     PetscCall(MatSetValue(A, jj, ii, vv, INSERT_VALUES));
 53:   }
 54:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 55:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

 57:   PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_COMMON));
 58:   PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));

 60:   /*
 61:      Insert same location non-zero values into A.
 62:   */
 63:   {
 64:     PetscInt    ii, jj;
 65:     PetscScalar vv;

 67:     ii = 3;
 68:     jj = 3;
 69:     vv = 0.3;
 70:     PetscCall(MatSetValue(A, ii, jj, vv, INSERT_VALUES));

 72:     ii = 7;
 73:     jj = 4;
 74:     vv = 3.3;
 75:     PetscCall(MatSetValue(A, ii, jj, vv, INSERT_VALUES));
 76:     PetscCall(MatSetValue(A, jj, ii, vv, INSERT_VALUES));

 78:     ii = 9;
 79:     jj = 7;
 80:     vv = 4.3;
 81:     PetscCall(MatSetValue(A, ii, jj, vv, INSERT_VALUES));
 82:     PetscCall(MatSetValue(A, jj, ii, vv, INSERT_VALUES));
 83:   }
 84:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 85:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

 87:   PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));

 89:   PetscCall(MatDestroy(&A));
 90:   PetscFunctionReturn(PETSC_SUCCESS);
 91: }

 93: int main(int argc, char **args)
 94: {
 95:   PetscFunctionBeginUser;
 96:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 97:   PetscCall(ex1_nonsquare_bs1());
 98:   PetscCall(PetscFinalize());
 99:   return 0;
100: }

102: /*TEST

104:    testset:
105:      args: -bs {{1 2}} -type {{aij baij sbaij}}
106:      filter: grep -v "type:"
107:      test:
108:      test:
109:        suffix: 2
110:        nsize: 2

112: TEST*/