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