xref: /petsc/src/dm/tutorials/ex7.c (revision a69119a591a03a9d906b29c0a4e9802e4d7c9795)
1 static char help[] = "Demonstrates using PetscViewerPushFormat(viewer,PETSC_VIEWER_BINARY_MATLAB)\n\n";
2 
3 #include <petscsys.h>
4 #include <petscdm.h>
5 #include <petscdmda.h>
6 #include <petscbag.h>
7 
8 typedef struct {
9   char      filename[PETSC_MAX_PATH_LEN];
10   PetscReal ra;
11   PetscInt  ia;
12   PetscBool ta;
13 } Parameter;
14 
15 int main(int argc, char **argv) {
16   PetscBag    bag;
17   Parameter  *params;
18   PetscViewer viewer;
19   DM          da;
20   Vec         global, local;
21   PetscMPIInt rank;
22 
23   /*
24     Every PETSc routine should begin with the PetscInitialize() routine.
25     argc, argv - These command line arguments are taken to extract the options
26                  supplied to PETSc and options supplied to MPI.
27     help       - When PETSc executable is invoked with the option -help,
28                  it prints the various options that can be applied at
29                  runtime.  The user can use the "help" variable place
30                  additional help messages in this printout.
31   */
32   PetscFunctionBeginUser;
33   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
34   /* Create a DMDA and an associated vector */
35   PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_BOX, 10, 10, PETSC_DECIDE, PETSC_DECIDE, 2, 1, NULL, NULL, &da));
36   PetscCall(DMSetFromOptions(da));
37   PetscCall(DMSetUp(da));
38   PetscCall(DMCreateGlobalVector(da, &global));
39   PetscCall(DMCreateLocalVector(da, &local));
40   PetscCall(VecSet(global, -1.0));
41   PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
42   PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));
43   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
44   PetscCall(VecScale(local, rank + 1));
45   PetscCall(DMLocalToGlobalBegin(da, local, ADD_VALUES, global));
46   PetscCall(DMLocalToGlobalEnd(da, local, ADD_VALUES, global));
47 
48   /* Create an empty bag */
49   PetscCall(PetscBagCreate(PETSC_COMM_WORLD, sizeof(Parameter), &bag));
50   PetscCall(PetscBagGetData(bag, (void **)&params));
51 
52   /* fill bag: register variables, defaults, names, help strings */
53   PetscCall(PetscBagSetName(bag, "ParameterBag", "contains problem parameters"));
54   PetscCall(PetscBagRegisterString(bag, &params->filename, PETSC_MAX_PATH_LEN, "output_file", "filename", "Name of secret file"));
55   PetscCall(PetscBagRegisterReal(bag, &params->ra, 1.0, "param_1", "The first parameter"));
56   PetscCall(PetscBagRegisterInt(bag, &params->ia, 5, "param_2", "The second parameter"));
57   PetscCall(PetscBagRegisterBool(bag, &params->ta, PETSC_TRUE, "do_output", "Write output file (true/false)"));
58 
59   /*
60      Write output file with PETSC_VIEWER_BINARY_MATLAB format
61      NOTE: the output generated with this viewer can be loaded into
62      MATLAB using $PETSC_DIR/share/petsc/matlab/PetscReadBinaryMatlab.m
63   */
64   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, params->filename, FILE_MODE_WRITE, &viewer));
65   PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_BINARY_MATLAB));
66   PetscCall(PetscBagView(bag, viewer));
67   PetscCall(DMDASetFieldName(da, 0, "field1"));
68   PetscCall(DMDASetFieldName(da, 1, "field2"));
69   PetscCall(PetscObjectSetName((PetscObject)global, "da1"));
70   PetscCall(VecView(global, viewer));
71   PetscCall(PetscViewerPopFormat(viewer));
72   PetscCall(PetscViewerDestroy(&viewer));
73 
74   /* clean up and exit */
75   PetscCall(PetscBagDestroy(&bag));
76   PetscCall(DMDestroy(&da));
77   PetscCall(VecDestroy(&local));
78   PetscCall(VecDestroy(&global));
79   PetscCall(PetscFinalize());
80   return 0;
81 }
82 
83 /*TEST
84 
85    test:
86 
87 TEST*/
88