Actual source code: ex5f90.F90
petsc-3.6.1 2015-08-06
1: #define PETSC_USE_FORTRAN_MODULES 1
2: #include <petsc/finclude/petscsysdef.h>
3: #include <petsc/finclude/petscbagdef.h>
4: #include <petsc/finclude/petscviewerdef.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: 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))
72: ! create the bag
73: call PetscBagCreate(PETSC_COMM_WORLD,sizeofbag,bag,ierr)
74: call PetscBagGetData(bag,data,ierr)
75: call PetscBagSetName(bag,'demo parameters', &
76: & 'super secret demo parameters in a bag',ierr)
77: call PetscBagSetOptionsPrefix(bag, 'pbag_', ierr)
79: ! register the data within the bag, grabbing values from the options database
80: ! Need to put the value into a variable for 64 bit indices
81: int56 = 56
82: call PetscBagRegisterInt(bag,data%nxc ,int56,'nxc', &
83: & 'nxc_variable help message',ierr)
84: call PetscBagRegisterRealArray(bag,data%rarray,three,'rarray', &
85: & 'rarray help message',ierr)
86: ! Need to put the value into a variable to pass correctly for 128 bit quad precision numbers
87: svalue = 103.2d0
88: call PetscBagRegisterScalar(bag,data%x ,svalue,'x', &
89: & 'x variable help message',ierr)
90: call PetscBagRegisterBool(bag,data%t ,PETSC_TRUE,'t', &
91: & 't boolean help message',ierr)
92: call PetscBagRegisterBoolArray(bag,data%tarray,three,'tarray', &
93: & 'tarray help message',ierr)
94: call PetscBagRegisterString(bag,data%c,'hello','c', &
95: & 'string help message',ierr)
96: value = -11.0d0
97: call PetscBagRegisterReal(bag,data%y ,value,'y', &
98: & 'y variable help message',ierr)
99: value = 1.0d0
100: call PetscBagRegisterReal(bag,data%pos%x1 ,value,'pos_x1', &
101: & 'tuple value 1 help message',ierr)
102: value = 2.0d0
103: call PetscBagRegisterReal(bag,data%pos%x2 ,value,'pos_x2', &
104: & 'tuple value 2 help message',ierr)
105: call PetscBagRegisterEnum(bag,data%enum ,list,1,'enum', &
106: & 'tuple value 2 help message',ierr)
107: call PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD,ierr)
109: data%nxc = 23
110: data%rarray(1) = -1.0
111: data%rarray(2) = -2.0
112: data%rarray(3) = -3.0
113: data%x = 155.4
114: data%c = 'a whole new string'
115: data%t = PETSC_TRUE
116: data%tarray = (/PETSC_TRUE,PETSC_FALSE,PETSC_TRUE/)
117: call PetscBagView(bag,PETSC_VIEWER_BINARY_WORLD,ierr)
119: call PetscViewerBinaryOpen(PETSC_COMM_WORLD,'binaryoutput', &
120: & FILE_MODE_READ,viewer,ierr)
121: call PetscBagLoad(viewer,bag,ierr)
122: call PetscViewerDestroy(viewer,ierr)
123: call PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD,ierr)
125: call PetscBagSetFromOptions(bag,ierr)
126: call PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD,ierr)
127: call PetscBagDestroy(bag,ierr)
129: call PetscFinalize(ierr)
130: end program ex5f90