Actual source code: ex25.c
1: static char help[] = "Takes a patch of a large DMDA vector to one process.\n\n";
3: #include <petscdm.h>
4: #include <petscdmda.h>
5: #include <petscdmpatch.h>
6: #include <petscsf.h>
8: typedef struct {
9: PetscScalar x, y;
10: } Field;
12: int main(int argc, char **argv)
13: {
14: Vec xy, sxy;
15: DM da, sda = NULL;
16: PetscSF sf;
17: PetscInt m = 10, n = 10, dof = 2;
18: MatStencil lower = {0, 3, 2, 0}, upper = {0, 7, 8, 0}; /* These are in the order of the z, y, x, logical coordinates, the fourth entry is ignored */
19: MPI_Comm comm;
20: PetscMPIInt rank;
22: PetscFunctionBeginUser;
23: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
25: /* create the large DMDA and set coordinates (which we will copy down to the small DA). */
26: PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_BOX, m, n, PETSC_DECIDE, PETSC_DECIDE, dof, 1, 0, 0, &da));
27: PetscCall(DMSetFromOptions(da));
28: PetscCall(DMSetUp(da));
29: PetscCall(DMDASetUniformCoordinates(da, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0));
30: /* Just as a simple example we use the coordinates as the variables in the vectors we wish to examine. */
31: PetscCall(DMGetCoordinates(da, &xy));
32: /* The vector entries are displayed in the "natural" ordering on the two dimensional grid; interlaced x and y with the x variable increasing more rapidly than the y */
33: PetscCall(VecView(xy, 0));
35: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
36: if (rank == 0) comm = MPI_COMM_SELF;
37: else comm = MPI_COMM_NULL;
39: PetscCall(DMPatchZoom(da, lower, upper, comm, &sda, NULL, &sf));
40: if (rank == 0) {
41: PetscCall(DMCreateGlobalVector(sda, &sxy));
42: } else {
43: PetscCall(VecCreateSeq(PETSC_COMM_SELF, 0, &sxy));
44: }
45: /* A PetscSF can also be used as a VecScatter context */
46: PetscCall(VecScatterBegin(sf, xy, sxy, INSERT_VALUES, SCATTER_FORWARD));
47: PetscCall(VecScatterEnd(sf, xy, sxy, INSERT_VALUES, SCATTER_FORWARD));
48: /* Only rank == 0 has the entries of the patch, so run code only at that rank */
49: if (rank == 0) {
50: Field **vars;
51: DMDALocalInfo info;
52: PetscScalar sum = 0;
54: /* The vector entries of the patch are displayed in the "natural" ordering on the two grid; interlaced x and y with the x variable increasing more rapidly */
55: PetscCall(VecView(sxy, PETSC_VIEWER_STDOUT_SELF));
56: /* Compute some trivial statistic of the coordinates */
57: PetscCall(DMDAGetLocalInfo(sda, &info));
58: PetscCall(DMDAVecGetArray(sda, sxy, &vars));
59: /* Loop over the patch of the entire domain */
60: for (PetscInt j = info.ys; j < info.ys + info.ym; j++) {
61: for (PetscInt i = info.xs; i < info.xs + info.xm; i++) sum += vars[j][i].x;
62: }
63: PetscCall(PetscPrintf(PETSC_COMM_SELF, "The sum of the x coordinates is %g\n", (double)PetscRealPart(sum)));
64: PetscCall(DMDAVecRestoreArray(sda, sxy, &vars));
65: }
67: PetscCall(VecDestroy(&sxy));
68: PetscCall(PetscSFDestroy(&sf));
69: PetscCall(DMDestroy(&sda));
70: PetscCall(DMDestroy(&da));
71: PetscCall(PetscFinalize());
72: return 0;
73: }
75: /*TEST
77: test:
79: test:
80: nsize: 4
81: suffix: 2
83: TEST*/