Actual source code: ex7.c
1: static char help[] = "Demonstrates using the PetscViewerBinaryMatlab viewer\n\n";
3: /*T
4: Concepts: viewers; bags;
5: Processors: n
6: T*/
7: #include petscsys.h
8: #include petscda.h
9: #include petscbag.h
11: typedef struct {
12: char filename[PETSC_MAX_PATH_LEN];
13: PetscReal ra;
14: PetscInt ia;
15: PetscTruth ta;
16: } Parameter;
20: int main(int argc,char **argv)
21: {
23: PetscBag bag;
24: Parameter *params;
25: PetscViewer viewer;
26: DA da;
27: Vec global,local;
28: PetscMPIInt rank;
30: /*
31: Every PETSc routine should begin with the PetscInitialize() routine.
32: argc, argv - These command line arguments are taken to extract the options
33: supplied to PETSc and options supplied to MPI.
34: help - When PETSc executable is invoked with the option -help,
35: it prints the various options that can be applied at
36: runtime. The user can use the "help" variable place
37: additional help messages in this printout.
38: */
39: PetscInitialize(&argc,&argv,(char *)0,help);
41: /* Create a DA and an associated vector */
42: DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_BOX,10,10,
43: PETSC_DECIDE,PETSC_DECIDE,2,1,PETSC_NULL,PETSC_NULL,&da);
44: DACreateGlobalVector(da,&global);
45: DACreateLocalVector(da,&local);
46: VecSet(global,-1.0);
47: DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);
48: DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);
49: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
50: VecScale(local,rank+1);
51: DALocalToGlobal(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: PetscBagRegisterTruth (bag,¶ms->ta,PETSC_TRUE,"do_output","Write output file (true/false)");
64: /*
65: Write output file with PetscViewerBinaryMatlab viewer.
66: NOTE: the output generated with this viewer can be loaded into
67: matlab using bin/matlab/PetscBinaryReadMatlab.m
68: */
69: PetscViewerBinaryMatlabOpen(PETSC_COMM_WORLD,params->filename,&viewer);
70: PetscViewerBinaryMatlabOutputBag(viewer,"params",bag);
71: DASetFieldName(da,0,"field1");
72: DASetFieldName(da,1,"field2");
73: PetscViewerBinaryMatlabOutputVecDA(viewer,"da1",global,da);
74: PetscViewerBinaryMatlabDestroy(viewer);
75:
76: /* clean up and exit */
77: PetscBagDestroy(bag);
78: DADestroy(da);
79: VecDestroy(local);
80: VecDestroy(global);
81: PetscFinalize();
82: return 0;
83: }