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 Bag_data_module
  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 Bag_data_module

 27:       module Bag_interface_module
 28:       use Bag_data_module

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

 40:       program ex5f90
 41:       use Bag_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:       Call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 58:       if (ierr .ne. 0) then
 59:          print*,'Unable to initialize PETSc'
 60:          stop
 61:       endif
 62:       list(1) = 'a123'
 63:       list(2) = 'b456'
 64:       list(3) = 'c789'
 65:       list(4) = 'list'
 66:       list(5) = 'prefix_'
 67:       list(6) = ''
 68: !     cannot just pass a 3 to PetscBagRegisterXXXArray() because it is expecting a PetscInt
 69:       three   = 3

 71: !   compute size of the data
 72: !
 73:       sizeofbag = size(transfer(dummydata,dummychar))


 76: ! create the bag
 77:       call PetscBagCreate(PETSC_COMM_WORLD,sizeofbag,bag,ierr);CHKERRA(ierr)
 78:       call PetscBagGetData(bag,data,ierr);CHKERRA(ierr)
 79:       call PetscBagSetName(bag,'demo parameters','super secret demo parameters in a bag',ierr);CHKERRA(ierr)
 80:       call PetscBagSetOptionsPrefix(bag, 'pbag_', ierr);CHKERRA(ierr)

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

102:       data%nxc = 23
103:       data%rarray(1) = -1.0
104:       data%rarray(2) = -2.0
105:       data%rarray(3) = -3.0
106:       data%x   = 155.4
107:       data%c   = 'a whole new string'
108:       data%t   = PETSC_TRUE
109:       data%tarray   = (/PETSC_TRUE,PETSC_FALSE,PETSC_TRUE/)
110:       call PetscBagView(bag,PETSC_VIEWER_BINARY_WORLD,ierr);CHKERRA(ierr)

112:       call PetscViewerBinaryOpen(PETSC_COMM_WORLD,'binaryoutput',FILE_MODE_READ,viewer,ierr);CHKERRA(ierr)
113:       call PetscBagLoad(viewer,bag,ierr);CHKERRA(ierr)
114:       call PetscViewerDestroy(viewer,ierr);CHKERRA(ierr)
115:       call PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)

117:       call PetscBagSetFromOptions(bag,ierr);CHKERRA(ierr)
118:       call PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)
119:       call PetscBagDestroy(bag,ierr);CHKERRA(ierr)

121:       call PetscFinalize(ierr)
122:       end program ex5f90

124: !
125: !/*TEST
126: !
127: !   build:
128: !      requires: define(PETSC_USING_F2003) define(PETSC_USING_F90FREEFORM)
129: !
130: !   test:
131: !      args: -pbag_rarray 4,5,88
132: !
133: !TEST*/