Actual source code: ex5f90.F90


  2: #include <petsc/finclude/petscsys.h>
  3: #include <petsc/finclude/petscbag.h>
  4: #include <petsc/finclude/petscviewer.h>

  6:       module ex5f90module
  7: !     Data structure used to contain information about the problem
  8: !     You can add physical values etc here

 10:       type tuple
 11:          PetscReal:: x1,x2
 12:       end type tuple

 14:       type bag_data_type
 15:          PetscScalar :: x
 16:          PetscReal :: y
 17:          PetscInt  :: nxc
 18:          PetscReal :: rarray(3)
 19:          PetscBool  :: t
 20:          PetscBool  :: tarray(3)
 21:          PetscEnum :: enum
 22:          character*(80) :: c
 23:          type(tuple) :: pos
 24:       end type bag_data_type
 25:       end module ex5f90module

 27:       module ex5f90Bag_interface_module
 28:       use ex5f90module

 30:       interface PetscBagGetData
 31:          subroutine PetscBagGetData(bag,data,ierr)
 32:            use ex5f90module
 33:            PetscBag bag
 34:            type(bag_data_type),pointer :: data
 35:            PetscErrorCode ierr
 36:          end subroutine PetscBagGetData
 37:       end interface
 38:       end module ex5f90Bag_interface_module

 40:       program ex5f90
 41:       use ex5f90Bag_interface_module
 42:       use petsc
 43:       implicit none

 45:       PetscBag bag
 46:       PetscErrorCode ierr
 47:       type(bag_data_type), pointer :: data
 48:       type(bag_data_type)          :: dummydata
 49:       character(len=1),pointer     :: dummychar(:)
 50:       PetscViewer viewer
 51:       PetscSizeT sizeofbag
 52:       Character(len=99) list(6)
 53:       PetscInt three,int56
 54:       PetscReal value
 55:       PetscScalar svalue

 57:       PetscCallA(PetscInitialize(ierr))
 58:       list(1) = 'a123'
 59:       list(2) = 'b456'
 60:       list(3) = 'c789'
 61:       list(4) = 'list'
 62:       list(5) = 'prefix_'
 63:       list(6) = ''
 64: !     cannot just pass a 3 to PetscBagRegisterXXXArray() because it is expecting a PetscInt
 65:       three   = 3

 67: !   compute size of the data
 68: !
 69:       sizeofbag = size(transfer(dummydata,dummychar))

 71: ! create the bag
 72:       PetscCallA(PetscBagCreate(PETSC_COMM_WORLD,sizeofbag,bag,ierr))
 73:       PetscCallA(PetscBagGetData(bag,data,ierr))
 74:       PetscCallA(PetscBagSetName(bag,'demo parameters','super secret demo parameters in a bag',ierr))
 75:       PetscCallA(PetscBagSetOptionsPrefix(bag, 'pbag_', ierr))

 77: ! register the data within the bag, grabbing values from the options database
 78: !     Need to put the value into a variable for 64 bit indices
 79:       int56 = 56
 80:       PetscCallA(PetscBagRegisterInt(bag,data%nxc ,int56,'nxc','nxc_variable help message',ierr))
 81:       PetscCallA(PetscBagRegisterRealArray(bag,data%rarray,three,'rarray','rarray help message',ierr))
 82: !     Need to put the value into a variable to pass correctly for 128 bit quad precision numbers
 83:       svalue = 103.20
 84:       PetscCallA(PetscBagRegisterScalar(bag,data%x ,svalue,'x','x variable help message',ierr))
 85:       PetscCallA(PetscBagRegisterBool(bag,data%t ,PETSC_TRUE,'t','t boolean help message',ierr))
 86:       PetscCallA(PetscBagRegisterBoolArray(bag,data%tarray,three,'tarray','tarray help message',ierr))
 87:       PetscCallA(PetscBagRegisterString(bag,data%c,'hello','c','string help message',ierr))
 88:       value = -11.00
 89:       PetscCallA(PetscBagRegisterReal(bag,data%y ,value,'y','y variable help message',ierr))
 90:       value = 1.00
 91:       PetscCallA(PetscBagRegisterReal(bag,data%pos%x1 ,value,'pos_x1','tuple value 1 help message',ierr))
 92:       value = 2.00
 93:       PetscCallA(PetscBagRegisterReal(bag,data%pos%x2 ,value,'pos_x2','tuple value 2 help message',ierr))
 94:       PetscCallA(PetscBagRegisterEnum(bag,data%enum ,list,1,'enum','tuple value 2 help message',ierr))
 95:       PetscCallA(PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD,ierr))

 97:       data%nxc = 23
 98:       data%rarray(1) = -1.0
 99:       data%rarray(2) = -2.0
100:       data%rarray(3) = -3.0
101:       data%x   = 155.4
102:       data%c   = 'a whole new string'
103:       data%t   = PETSC_TRUE
104:       data%tarray   = (/PETSC_TRUE,PETSC_FALSE,PETSC_TRUE/)
105:       PetscCallA(PetscBagView(bag,PETSC_VIEWER_BINARY_WORLD,ierr))

107:       PetscCallA(PetscViewerBinaryOpen(PETSC_COMM_WORLD,'binaryoutput',FILE_MODE_READ,viewer,ierr))
108:       PetscCallA(PetscBagLoad(viewer,bag,ierr))
109:       PetscCallA(PetscViewerDestroy(viewer,ierr))
110:       PetscCallA(PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD,ierr))

112:       PetscCallA(PetscBagSetFromOptions(bag,ierr))
113:       PetscCallA(PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD,ierr))
114:       PetscCallA(PetscBagDestroy(bag,ierr))

116:       PetscCallA(PetscFinalize(ierr))
117:       end program ex5f90

119: !
120: !/*TEST
121: !
122: !   build:
123: !      requires: defined(PETSC_USING_F2003) defined(PETSC_USING_F90FREEFORM)
124: !
125: !   test:
126: !      args: -pbag_rarray 4,5,88
127: !
128: !TEST*/