Actual source code: ex7.c
petsc-3.4.2 2013-07-02

      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 <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,NULL,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 PETSC_VIEWER_BINARY_MATLAB format
     68:      NOTE: the output generated with this viewer can be loaded into
     69:      MATLAB using $PETSC_DIR/bin/matlab/PetscReadBinaryMatlab.m
     70:   */
     71:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,params->filename,FILE_MODE_WRITE,&viewer);
     72:   PetscViewerSetFormat(viewer,PETSC_VIEWER_BINARY_MATLAB);
     73:   PetscBagView(bag,viewer);
     74:   DMDASetFieldName(da,0,"field1");
     75:   DMDASetFieldName(da,1,"field2");
     76:   PetscObjectSetName((PetscObject)global,"da1");
     77:   VecView(global,viewer);
     78:   PetscViewerDestroy(&viewer);

     80:   /* clean up and exit */
     81:   PetscBagDestroy(&bag);
     82:   DMDestroy(&da);
     83:   VecDestroy(&local);
     84:   VecDestroy(&global);
     85:   PetscFinalize();
     86:   return 0;
     87: }