Actual source code: ex7.c

petsc-3.11.4 2019-09-28
Report Typos and Errors
  1: static char help[] = "Demonstrates using PetscViewerPushFormat(viewer,PETSC_VIEWER_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;

 20: int main(int argc,char **argv)
 21: {
 23:   PetscBag       bag;
 24:   Parameter      *params;
 25:   PetscViewer    viewer;
 26:   DM             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);if (ierr) return ierr;
 40:   /* Create a DMDA and an associated vector */
 41:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,10,10,PETSC_DECIDE,PETSC_DECIDE,2,1,NULL,NULL,&da);
 42:   DMSetFromOptions(da);
 43:   DMSetUp(da);
 44:   DMCreateGlobalVector(da,&global);
 45:   DMCreateLocalVector(da,&local);
 46:   VecSet(global,-1.0);
 47:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 48:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);
 49:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 50:   VecScale(local,rank+1);
 51:   DMLocalToGlobalBegin(da,local,ADD_VALUES,global);
 52:   DMLocalToGlobalEnd(da,local,ADD_VALUES,global);

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

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

 65:   /*
 66:      Write output file with PETSC_VIEWER_BINARY_MATLAB format
 67:      NOTE: the output generated with this viewer can be loaded into
 68:      MATLAB using $PETSC_DIR/share/petsc/matlab/PetscReadBinaryMatlab.m
 69:   */
 70:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,params->filename,FILE_MODE_WRITE,&viewer);
 71:   PetscViewerPushFormat(viewer,PETSC_VIEWER_BINARY_MATLAB);
 72:   PetscBagView(bag,viewer);
 73:   DMDASetFieldName(da,0,"field1");
 74:   DMDASetFieldName(da,1,"field2");
 75:   PetscObjectSetName((PetscObject)global,"da1");
 76:   VecView(global,viewer);
 77:   PetscViewerPopFormat(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 ierr;
 87: }


 90: /*TEST

 92:    test:

 94: TEST*/