Actual source code: ex41.c

  1: static char help[] = "Test -dm_preallocate_only with DMStag\n\n";

  3: #include <petscdm.h>
  4: #include <petscdmstag.h>

  6: int main(int argc, char **argv)
  7: {
  8:   DM            dm;
  9:   PetscInt      dim;
 10:   Mat           A;
 11:   DMStagStencil row, col;
 12:   PetscScalar   value;

 14:   PetscFunctionBeginUser;
 15:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
 16:   dim = 1;
 17:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-dim", &dim, NULL));

 19:   switch (dim) {
 20:   case 1:
 21:     PetscCall(DMStagCreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, 4, 1, 1, DMSTAG_STENCIL_BOX, 1, NULL, &dm));
 22:     break;
 23:   default:
 24:     SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Unsupported dimension %" PetscInt_FMT, dim);
 25:   }
 26:   PetscCall(DMSetFromOptions(dm));
 27:   PetscCall(DMSetUp(dm));

 29:   PetscCall(DMCreateMatrix(dm, &A));

 31:   row.c   = 0;
 32:   row.i   = 0;
 33:   row.loc = DMSTAG_ELEMENT;

 35:   col.c   = 0;
 36:   col.i   = 1;
 37:   col.loc = DMSTAG_ELEMENT;

 39:   value = 1.234;

 41:   PetscCall(DMStagMatSetValuesStencil(dm, A, 1, &row, 1, &col, &value, INSERT_VALUES));
 42:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 43:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

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

 47:   PetscCall(MatDestroy(&A));
 48:   PetscCall(DMDestroy(&dm));
 49:   PetscCall(PetscFinalize());
 50:   return 0;
 51: }

 53: /*TEST

 55:    test:
 56:      args: -dm_preallocate_only

 58: TEST*/