Actual source code: ex5f90.F90

  2:       module MyModule
 3:  #include finclude/petscsysdef.h
 4:  #include finclude/petscbagdef.h
 5:  #include finclude/petscviewerdef.h
  6: !   Data structure used to contain information about the problem
  7: !   You can add physical values etc here

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

 16:       end type appctx
 17:       end module MyModule

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

 30:       program ex5f90
 31:       use MyModule
 32:       use MyInterface
 33:       implicit none
 34:  #include finclude/petscsys.h
 35:  #include finclude/petscbag.h
 36:  #include finclude/petscviewer.h

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

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

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

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

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

 81:       call PetscFinalize(ierr)
 82:       end