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: */
33: PetscInitialize(&argc,&argv,(char*)0,help);
34: /* Create a DMDA and an associated vector */
35: 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: DMSetFromOptions(da);
37: DMSetUp(da);
38: DMCreateGlobalVector(da,&global);
39: DMCreateLocalVector(da,&local);
40: VecSet(global,-1.0);
41: DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
42: DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);
43: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
44: VecScale(local,rank+1);
45: DMLocalToGlobalBegin(da,local,ADD_VALUES,global);
46: DMLocalToGlobalEnd(da,local,ADD_VALUES,global);
48: /* Create an empty bag */
49: PetscBagCreate(PETSC_COMM_WORLD,sizeof(Parameter),&bag);
50: PetscBagGetData(bag,(void**)¶ms);
52: /* fill bag: register variables, defaults, names, help strings */
53: PetscBagSetName(bag,"ParameterBag","contains problem parameters");
54: PetscBagRegisterString(bag,¶ms->filename,PETSC_MAX_PATH_LEN,"output_file","filename","Name of secret file");
55: PetscBagRegisterReal (bag,¶ms->ra,1.0,"param_1","The first parameter");
56: PetscBagRegisterInt (bag,¶ms->ia,5,"param_2","The second parameter");
57: PetscBagRegisterBool (bag,¶ms->ta,PETSC_TRUE,"do_output","Write output file (true/false)");
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: PetscViewerBinaryOpen(PETSC_COMM_WORLD,params->filename,FILE_MODE_WRITE,&viewer);
65: PetscViewerPushFormat(viewer,PETSC_VIEWER_BINARY_MATLAB);
66: PetscBagView(bag,viewer);
67: DMDASetFieldName(da,0,"field1");
68: DMDASetFieldName(da,1,"field2");
69: PetscObjectSetName((PetscObject)global,"da1");
70: VecView(global,viewer);
71: PetscViewerPopFormat(viewer);
72: PetscViewerDestroy(&viewer);
74: /* clean up and exit */
75: PetscBagDestroy(&bag);
76: DMDestroy(&da);
77: VecDestroy(&local);
78: VecDestroy(&global);
79: PetscFinalize();
80: return 0;
81: }
83: /*TEST
85: test:
87: TEST*/