xref: /petsc/src/dm/impls/stag/tests/ex42.c (revision 732aec7a18f2199fb53bb9a2f3aef439a834ce31)
1 static char help[] = "Test DMCreateFieldDecomposition_Stag()\n\n";
2 
3 #include <petscdm.h>
4 #include <petscdmstag.h>
5 
main(int argc,char ** argv)6 int main(int argc, char **argv)
7 {
8   DM       dm;
9   DM      *sub_dms;
10   PetscInt dim, n_fields;
11   IS      *fields;
12   char   **field_names;
13 
14   PetscFunctionBeginUser;
15   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
16   dim = 2;
17   PetscCall(PetscOptionsGetInt(NULL, NULL, "-dim", &dim, NULL));
18 
19   switch (dim) {
20   case 1:
21     PetscCall(DMStagCreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, 3, 1, 1, DMSTAG_STENCIL_BOX, 1, NULL, &dm));
22     break;
23   case 2:
24     PetscCall(DMStagCreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, 3, 2, PETSC_DECIDE, PETSC_DECIDE, 1, 1, 1, DMSTAG_STENCIL_BOX, 1, NULL, NULL, &dm));
25     break;
26   case 3:
27     PetscCall(DMStagCreate3d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, 3, 2, 4, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, 1, 1, 1, 1, DMSTAG_STENCIL_BOX, 1, NULL, NULL, NULL, &dm));
28     break;
29   default:
30     SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "No support for dimension %" PetscInt_FMT, dim);
31   }
32 
33   PetscCall(DMSetFromOptions(dm));
34   PetscCall(DMSetUp(dm));
35   PetscCall(DMView(dm, PETSC_VIEWER_STDOUT_WORLD));
36 
37   PetscCall(DMCreateFieldDecomposition(dm, &n_fields, &field_names, &fields, &sub_dms));
38   for (PetscInt i = 0; i < n_fields; ++i) {
39     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%" PetscInt_FMT " %s\n", i, field_names[i]));
40     PetscCall(ISView(fields[i], PETSC_VIEWER_STDOUT_WORLD));
41     PetscCall(DMView(sub_dms[i], PETSC_VIEWER_STDOUT_WORLD));
42   }
43 
44   for (PetscInt i = 0; i < n_fields; ++i) {
45     PetscCall(PetscFree(field_names[i]));
46     PetscCall(ISDestroy(&fields[i]));
47     PetscCall(DMDestroy(&sub_dms[i]));
48   }
49   PetscCall(PetscFree(fields));
50   PetscCall(PetscFree(field_names));
51   PetscCall(PetscFree(sub_dms));
52   PetscCall(DMDestroy(&dm));
53   PetscCall(PetscFinalize());
54   return 0;
55 }
56 
57 /*TEST
58 
59    test:
60       nsize: 2
61       args: -dim {{1 2 3}separate output}
62 
63 TEST*/
64