Actual source code: ex55.c

  1: static char help[] = "Tests that DMSetLocalSection() and DMSetGlobalSection() keep a local-to-global mapping built by the DM implementation.\n\n";

  3: #include <petscdmda.h>
  4: #include <petscsection.h>

  6: int main(int argc, char **argv)
  7: {
  8:   DM                     da;
  9:   PetscSection           section;
 10:   ISLocalToGlobalMapping ltog, ltognew;
 11:   PetscInt               p, xs, xm;

 13:   PetscFunctionBeginUser;
 14:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 15:   PetscCall(DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, 8, 1, 1, NULL, &da));
 16:   PetscCall(DMSetUp(da));
 17:   /* DMDA builds its mapping in DMSetUp() and cannot rebuild it from a section, so the section setters must keep it.
 18:      Hold a reference to the mapping so a wrongly rebuilt one cannot alias its address. */
 19:   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
 20:   PetscCall(PetscObjectReference((PetscObject)ltog));
 21:   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &xm, NULL, NULL));
 22:   PetscCall(PetscSectionCreate(PETSC_COMM_WORLD, &section));
 23:   PetscCall(PetscSectionSetChart(section, xs, xs + xm));
 24:   for (p = xs; p < xs + xm; ++p) PetscCall(PetscSectionSetDof(section, p, 1));
 25:   PetscCall(PetscSectionSetUp(section));
 26:   PetscCall(DMSetLocalSection(da, section));
 27:   PetscCall(PetscSectionDestroy(&section));
 28:   PetscCall(DMGetLocalToGlobalMapping(da, &ltognew));
 29:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "l2g map %s by DMSetLocalSection()\n", ltognew == ltog ? "kept" : "NOT kept"));
 30:   PetscCall(DMSetGlobalSection(da, NULL));
 31:   PetscCall(DMGetLocalToGlobalMapping(da, &ltognew));
 32:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "l2g map %s by DMSetGlobalSection()\n", ltognew == ltog ? "kept" : "NOT kept"));
 33:   PetscCall(ISLocalToGlobalMappingDestroy(&ltog));
 34:   PetscCall(DMDestroy(&da));
 35:   PetscCall(PetscFinalize());
 36:   return 0;
 37: }

 39: /*TEST

 41:   # A DMDA builds its local-to-global mapping in DMSetUp() and registers no way to rebuild it from a
 42:   # section, so the section setters must not destroy it (they invalidate only section-derived maps)
 43:   test:
 44:     suffix: keep_ltog

 46:   test:
 47:     suffix: keep_ltog_par
 48:     nsize: 2
 49:     output_file: output/ex55_keep_ltog.out

 51: TEST*/