1 2 static char help[] = "Tests saving DMDA vectors to files.\n\n"; 3 4 /* 5 ex13.c reads in the DMDA and vector written by this program. 6 */ 7 8 #include <petscdm.h> 9 #include <petscdmda.h> 10 11 int main(int argc, char **argv) 12 { 13 PetscMPIInt rank; 14 PetscInt M = 10, N = 8, m = PETSC_DECIDE, n = PETSC_DECIDE, dof = 1; 15 DM da; 16 Vec local, global, natural; 17 PetscScalar value; 18 PetscViewer bviewer; 19 20 PetscFunctionBeginUser; 21 PetscCall(PetscInitialize(&argc, &argv, (char *)0, help)); 22 PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL)); 23 PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL)); 24 PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL)); 25 PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL)); 26 PetscCall(PetscOptionsGetInt(NULL, NULL, "-dof", &dof, NULL)); 27 28 /* Create distributed array and get vectors */ 29 PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_BOX, M, N, m, n, dof, 1, NULL, NULL, &da)); 30 PetscCall(DMSetFromOptions(da)); 31 PetscCall(DMSetUp(da)); 32 PetscCall(DMCreateGlobalVector(da, &global)); 33 PetscCall(DMCreateLocalVector(da, &local)); 34 35 value = -3.0; 36 PetscCall(VecSet(global, value)); 37 PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local)); 38 PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local)); 39 40 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 41 value = rank + 1; 42 PetscCall(VecScale(local, value)); 43 PetscCall(DMLocalToGlobalBegin(da, local, ADD_VALUES, global)); 44 PetscCall(DMLocalToGlobalEnd(da, local, ADD_VALUES, global)); 45 46 PetscCall(DMDACreateNaturalVector(da, &natural)); 47 PetscCall(DMDAGlobalToNaturalBegin(da, global, INSERT_VALUES, natural)); 48 PetscCall(DMDAGlobalToNaturalEnd(da, global, INSERT_VALUES, natural)); 49 50 PetscCall(DMDASetFieldName(da, 0, "First field")); 51 /* PetscCall(VecView(global,PETSC_VIEWER_DRAW_WORLD)); */ 52 53 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "daoutput", FILE_MODE_WRITE, &bviewer)); 54 PetscCall(DMView(da, bviewer)); 55 PetscCall(VecView(global, bviewer)); 56 PetscCall(PetscViewerDestroy(&bviewer)); 57 58 /* Free memory */ 59 PetscCall(VecDestroy(&local)); 60 PetscCall(VecDestroy(&global)); 61 PetscCall(VecDestroy(&natural)); 62 PetscCall(DMDestroy(&da)); 63 PetscCall(PetscFinalize()); 64 return 0; 65 } 66