Actual source code: ex22.c


  2: static char help[] = "Tests MatSetValuesBlockedStencil() in 3d.\n\n";

  4: #include <petscmat.h>
  5: #include <petscdm.h>
  6: #include <petscdmda.h>

  8: int main(int argc, char **argv)
  9: {
 10:   PetscInt        M = 3, N = 4, P = 2, s = 1, w = 2, i, m = PETSC_DECIDE, n = PETSC_DECIDE, p = PETSC_DECIDE;
 11:   DM              da;
 12:   Mat             mat;
 13:   DMDAStencilType stencil_type = DMDA_STENCIL_BOX;
 14:   PetscBool       flg          = PETSC_FALSE;
 15:   MatStencil      idx[2], idy[2];
 16:   PetscScalar    *values;

 19:   PetscInitialize(&argc, &argv, (char *)0, help);
 20:   PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL);
 21:   PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL);
 22:   PetscOptionsGetInt(NULL, NULL, "-P", &P, NULL);
 23:   PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL);
 24:   PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL);
 25:   PetscOptionsGetInt(NULL, NULL, "-p", &p, NULL);
 26:   PetscOptionsGetInt(NULL, NULL, "-s", &s, NULL);
 27:   PetscOptionsGetInt(NULL, NULL, "-w", &w, NULL);
 28:   PetscOptionsGetBool(NULL, NULL, "-star", &flg, NULL);
 29:   if (flg) stencil_type = DMDA_STENCIL_STAR;

 31:   /* Create distributed array and get vectors */
 32:   DMDACreate3d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, stencil_type, M, N, P, m, n, p, w, s, 0, 0, 0, &da);
 33:   DMSetFromOptions(da);
 34:   DMSetUp(da);
 35:   DMSetMatType(da, MATMPIBAIJ);
 36:   DMCreateMatrix(da, &mat);

 38:   idx[0].i = 1;
 39:   idx[0].j = 1;
 40:   idx[0].k = 0;
 41:   idx[1].i = 2;
 42:   idx[1].j = 1;
 43:   idx[1].k = 0;
 44:   idy[0].i = 1;
 45:   idy[0].j = 2;
 46:   idy[0].k = 0;
 47:   idy[1].i = 2;
 48:   idy[1].j = 2;
 49:   idy[1].k = 0;
 50:   PetscMalloc1(2 * 2 * w * w, &values);
 51:   for (i = 0; i < 2 * 2 * w * w; i++) values[i] = i;
 52:   MatSetValuesBlockedStencil(mat, 2, idx, 2, idy, values, INSERT_VALUES);
 53:   MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);
 54:   MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);

 56:   /* Free memory */
 57:   PetscFree(values);
 58:   MatDestroy(&mat);
 59:   DMDestroy(&da);
 60:   PetscFinalize();
 61:   return 0;
 62: }