Actual source code: ex5f90.F90

  2:       module MyModule
  3: #define PETSC_AVOID_DECLARATIONS
 4:  #include include/finclude/petsc.h
 5:  #include include/finclude/petscbag.h
 6:  #include include/finclude/petscviewer.h
  7: #undef PETSC_AVOID_DECLARATIONS
  8: !   Data structure used to contain information about the problem
  9: !   You can add physical values etc here

 11:       type appctx
 12:         PetscScalar :: x
 13:         PetscReal :: y
 14:         PetscInt :: nxc
 15:         PetscTruth :: t
 16:         character*(80) :: c

 18:       end type appctx
 19:       end module MyModule

 21:       module MyInterface
 22:       Interface PetscBagGetData
 23:         Subroutine PetscBagGetData(bag,ctx,ierr)
 24:           use MyModule
 25:           PetscBag bag
 26:           type(AppCtx), pointer :: ctx
 27:           PetscErrorCode ierr
 28:         End Subroutine
 29:       End Interface PetscBagGetData
 30:       End module MyInterface

 32:       program ex5f90
 33:       use MyModule
 34:       use MyInterface
 35:       implicit none
 36:  #include include/finclude/petsc.h
 37:  #include include/finclude/petscbag.h
 38:  #include include/finclude/petscviewer.h

 40:       PetscBag bag
 41:       PetscErrorCode ierr
 42:       type(AppCtx), pointer :: ctx
 43:       PetscViewer viewer
 44:       PetscInt sizeofctx,sizeofint
 45:       PetscInt sizeofscalar,sizeoftruth
 46:       PetscInt sizeofchar,sizeofreal
 47: 
 48:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)

 50: !      compute size of ctx
 51:       call PetscDataTypeGetSize(PETSC_INT,sizeofint,ierr)
 52:       call PetscDataTypeGetSize(PETSC_SCALAR,sizeofscalar,ierr)
 53:       call PetscDataTypeGetSize(PETSC_TRUTH,sizeoftruth,ierr)
 54:       call PetscDataTypeGetSize(PETSC_CHAR,sizeofchar,ierr)
 55:       call PetscDataTypeGetSize(PETSC_REAL,sizeofreal,ierr)

 57: !     really need a sizeof(ctx) operator here. There could be padding inside the
 58: !     structure due to alignment issues - so, this computed value cold be wrong.
 59:       sizeofctx = sizeofint + sizeofscalar+sizeoftruth+sizeofchar*80+sizeofreal

 61:       call PetscBagCreate(PETSC_COMM_WORLD,sizeofctx,bag,ierr)
 62:       call PetscBagGetData(bag,ctx,ierr)
 63:       call PetscBagRegisterInt(bag,ctx%nxc ,56,'nxc','nxc_variable help message',ierr)
 64:       call PetscBagRegisterScalar(bag,ctx%x ,103.2d0,'x','x variable help message',ierr)
 65:       call PetscBagRegisterTruth(bag,ctx%t ,PETSC_TRUE,'t','t boolean help message',ierr)
 66:       call PetscBagRegisterString(bag,ctx%c,'hello','c','string help message',ierr)
 67:       call PetscBagRegisterReal(bag,ctx%y ,-11.0d0,'y','y variable help message',ierr)
 68:       ctx%nxc = 23
 69:       ctx%x   = 155.4
 70:       ctx%c   = 'a whole new string'
 71:       call PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD,ierr)
 72:       call PetscBagView(bag,PETSC_VIEWER_BINARY_WORLD,ierr)
 73:       call PetscBagDestroy(bag,ierr)

 75:       call PetscViewerBinaryOpen(PETSC_COMM_WORLD,'binaryoutput',FILE_MODE_READ,viewer,ierr)
 76:       call PetscBagLoad(viewer,bag,ierr)
 77:       call PetscViewerDestroy(viewer,ierr)
 78:       call PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD,ierr)
 79:       call PetscBagDestroy(bag,ierr)

 81:       call PetscFinalize(ierr)
 82:       end