Actual source code: ex7.c

petsc-3.3-p7 2013-05-11
  1: static char help[] = "Demonstrates using the PetscViewerBinaryMatlab viewer\n\n";

  3: /*T
  4:    Concepts: viewers
  5:    Concepts: bags
  6:    Processors: n
  7: T*/
  8: #include <petscsys.h>
  9: #include <petscdmda.h>
 10: #include <petscbag.h>

 12: typedef struct {
 13:   char          filename[PETSC_MAX_PATH_LEN];
 14:   PetscReal     ra;
 15:   PetscInt      ia;
 16:   PetscBool     ta;
 17: } Parameter;

 21: int main(int argc,char **argv)
 22: {
 24:   PetscBag       bag;
 25:   Parameter      *params;
 26:   PetscViewer    viewer;
 27:   DM             da;
 28:   Vec            global,local;
 29:   PetscMPIInt    rank;

 31:   /*
 32:     Every PETSc routine should begin with the PetscInitialize() routine.
 33:     argc, argv - These command line arguments are taken to extract the options
 34:                  supplied to PETSc and options supplied to MPI.
 35:     help       - When PETSc executable is invoked with the option -help, 
 36:                  it prints the various options that can be applied at 
 37:                  runtime.  The user can use the "help" variable place
 38:                  additional help messages in this printout.
 39:   */
 40:   PetscInitialize(&argc,&argv,(char *)0,help);

 42:   /* Create a DMDA and an associated vector */
 43:   DMDACreate2d(PETSC_COMM_WORLD, DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_NONE,DMDA_STENCIL_BOX,10,10,
 44:                     PETSC_DECIDE,PETSC_DECIDE,2,1,PETSC_NULL,PETSC_NULL,&da);
 45:   DMCreateGlobalVector(da,&global);
 46:   DMCreateLocalVector(da,&local);
 47:   VecSet(global,-1.0);
 48:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 49:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);
 50:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 51:   VecScale(local,rank+1);
 52:   DMLocalToGlobalBegin(da,local,ADD_VALUES,global);
 53:   DMLocalToGlobalEnd(da,local,ADD_VALUES,global);

 55:   /* Create an empty bag */
 56:   PetscBagCreate(PETSC_COMM_WORLD,sizeof(Parameter),&bag);
 57:   PetscBagGetData(bag,(void **)&params);

 59:   /* fill bag: register variables, defaults, names, help strings */
 60:   PetscBagSetName(bag,"ParameterBag","contains problem parameters");
 61:   PetscBagRegisterString(bag,&params->filename,PETSC_MAX_PATH_LEN,"output_file","filename","Name of secret file");
 62:   PetscBagRegisterReal  (bag,&params->ra,1.0,"param_1","The first parameter");
 63:   PetscBagRegisterInt   (bag,&params->ia,5  ,"param_2","The second parameter");
 64:   PetscBagRegisterBool (bag,&params->ta,PETSC_TRUE,"do_output","Write output file (true/false)");

 66:   /* 
 67:      Write output file with PetscViewerBinaryMatlab viewer. 
 68:      NOTE: the output generated with this viewer can be loaded into
 69:      matlab using bin/matlab/PetscBinaryReadMatlab.m 
 70:   */
 71:   PetscViewerBinaryMatlabOpen(PETSC_COMM_WORLD,params->filename,&viewer);
 72:   PetscViewerBinaryMatlabOutputBag(viewer,"params",bag);
 73:   DMDASetFieldName(da,0,"field1");
 74:   DMDASetFieldName(da,1,"field2");
 75:   PetscViewerBinaryMatlabOutputVecDA(viewer,"da1",global,da);
 76:   PetscViewerBinaryMatlabDestroy(&viewer);
 77: 
 78:   /* clean up and exit */
 79:   PetscBagDestroy(&bag);
 80:   DMDestroy(&da);
 81:   VecDestroy(&local);
 82:   VecDestroy(&global);
 83:   PetscFinalize();
 84:   return 0;
 85: }