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