Date: 2022-08-02T03:13:29+00:00

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

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

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

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

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

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

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

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