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