Date: 2025-03-28T21:11:14+00:00

Actual source code: ex9f.F90

      1: !
      2: !   Example of using PetscOptionsBegin in Fortran
      3: program ex9f
      4: #include "petsc/finclude/petsc.h"
      5:     use petsc
      6:     implicit none

      8:     PetscReal,Parameter                       :: PReal = 1.0
      9:     Integer,Parameter                         :: Pr = Selected_Real_Kind(Precision(PReal))
     10:     PetscInt,Parameter                        :: PInt = 1
     11:     Integer,Parameter                         :: Pi = kind(PInt)

     13:     PetscErrorCode                            :: ierr
     14:     PetscBool                                 :: flg
     15:     PetscInt                                  :: nopt = 3_Pi
     16:     PetscBool                                 :: bvalue, bdefault = PETSC_TRUE
     17:     PetscBool,dimension(:),pointer            :: barray
     18:     PetscEnum                                 :: evalue, edefault = 2
     19:     PetscInt                                  :: ivalue, idefault = 2_Pi
     20:     PetscInt,dimension(:),pointer             :: iarray
     21:     PetscReal                                 :: rvalue, rdefault = 1.23_Pr
     22:     PetscReal,dimension(:),pointer            :: rarray
     23:     PetscScalar                               :: svalue, sdefault = -4.56_Pr
     24:     PetscScalar,dimension(:),pointer          :: sarray
     25:     character(len=256)                        :: IOBuffer
     26:     character(len=256)                        :: stvalue,stdefault
     27:     character(len=256)                        :: list(6)

     29:     PetscCallA(PetscInitialize(ierr))
     30:     list(1)   = 'a123   '
     31:     list(2)   = 'b456   '
     32:     list(3)   = 'c789   '
     33:     list(4)   = 'list   '
     34:     list(5)   = 'prefix_'
     35:     list(6)   = ''
     36:     stdefault = 'oulala oulala'

     38:     Allocate(iarray(nopt),source=-1_Pi)
     39:     Allocate(rarray(nopt),source=-99.0_pr)
     40:     Allocate(barray(nopt),source=PETSC_FALSE)
     41:     Allocate(sarray(nopt))
     42:     sarray = 123.456_Pr

     44:     PetscCallA(PetscOptionsBegin(PETSC_COMM_WORLD,'prefix_','Setting options for my application','Section 1',ierr))
     45:         PetscCallA(PetscOptionsBool('-bool','Get an application bool','Man page',bdefault,bvalue,flg,ierr))
     46:         if (flg) then
     47:             write(IOBuffer,'("The bool value was set to ",L1,"\n")') bvalue
     48:             PetscCallA(PetscPrintf(PETSC_COMM_WORLD,IOBuffer,ierr))
     49:         endif
     50:         PetscCallA(PetscOptionsBoolArray('-boolarray','Get an application bool array','Man page',barray,nopt,flg,ierr))
     51:         if (flg) then
     52:             write(IOBuffer,'("The bool array was set to ",*(L1," "))') barray
     53:             PetscCallA(PetscPrintf(PETSC_COMM_WORLD,trim(IOBuffer)//"\n",ierr))
     54:         endif
     55:         PetscCallA(PetscOptionsEnum('-enum','Get an application enum','Man page',list,edefault,evalue,flg,ierr))
     56:         if (flg) then
     57:             write(IOBuffer,'("The bool value was set to ",A,"\n")') trim(list(evalue+1))
     58:             PetscCallA(PetscPrintf(PETSC_COMM_WORLD,IOBuffer,ierr))
     59:         endif
     60:         PetscCallA(PetscOptionsInt('-int','Get an application int','Man page',idefault,ivalue,flg,ierr))
     61:         if (flg) then
     62:             write(IOBuffer,'("The integer value was set to ",I8,"\n")') ivalue
     63:             PetscCallA(PetscPrintf(PETSC_COMM_WORLD,IOBuffer,ierr))
     64:         endif
     65:         PetscCallA(PetscOptionsIntArray('-intarray','Get an application int array','Man page',iarray,nopt,flg,ierr))
     66:         if (flg) then
     67:             write(IOBuffer, '("The integer array was set to ",*(I8," "))') iarray
     68:             PetscCallA(PetscPrintf(PETSC_COMM_WORLD,trim(IOBuffer)//"\n",ierr))
     69:         endif
     70:         PetscCallA(PetscOptionsReal('-real','Get an application real','Man page',rdefault,rvalue,flg,ierr))
     71:         if (flg) then
     72:             write(IOBuffer,'("The real value was set to ",ES12.5,"\n")') rvalue
     73:             PetscCallA(PetscPrintf(PETSC_COMM_WORLD,IOBuffer,ierr))
     74:         endif
     75:         PetscCallA(PetscOptionsRealArray('-realarray','Get an application real array','Man page',rarray,nopt,flg,ierr))
     76:         if (flg) then
     77:             write(IOBuffer,'("The real array was set to ",*(ES12.5," "))') rarray
     78:             PetscCallA(PetscPrintf(PETSC_COMM_WORLD,trim(IOBuffer)//"\n",ierr))
     79:         endif
     80:         PetscCallA(PetscOptionsScalar('-scalar','Get an application scalar','Man page',sdefault,svalue,flg,ierr))
     81:         if (flg) then
     82:             write(IOBuffer,'("The scalar value was set to ",ES12.5,"\n")') svalue
     83:             PetscCallA(PetscPrintf(PETSC_COMM_WORLD,IOBuffer,ierr))
     84:         endif
     85:         PetscCallA(PetscOptionsScalarArray('-scalararray','Get an application scalar array','Man page',sarray,nopt,flg,ierr))
     86:         if (flg) then
     87:             write(IOBuffer,'("The scalar array was set to ",*(ES12.5," "))') sarray
     88:             PetscCallA(PetscPrintf(PETSC_COMM_WORLD,trim(IOBuffer)//"\n",ierr))
     89:         endif
     90:         PetscCallA(PetscOptionsString('-string','Get an application string','Man page',stdefault,stvalue,flg,ierr))
     91:         if (flg) then
     92:             write(IOBuffer,'("The string value was set to ",A,"\n")') trim(stvalue)
     93:             PetscCallA(PetscPrintf(PETSC_COMM_WORLD,IOBuffer,ierr))
     94:         endif
     95:     PetscCallA(PetscOptionsEnd(ierr))

     97:     deallocate(iarray)
     98:     deallocate(rarray)
     99:     deallocate(barray)
    100:     deallocate(sarray)
    101:     PetscCallA(PetscFinalize(ierr))
    102: end program ex9f

    104: !
    105: !/*TEST
    106: !
    107: !   build:
    108: !      requires: defined(PETSC_USING_F2003) defined(PETSC_USING_F90FREEFORM) !complex
    109: !
    110: !   test:
    111: !
    112: !   test:
    113: !      suffix: 2
    114: !      args: -prefix_int 22 -prefix_intarray 2-5 -prefix_real 2.34 -prefix_realarray -3,-4,5.5 -prefix_scalar 7.89 -prefix_scalararray 1.,2.,3. -prefix_bool no -prefix_boolarray 1,no,true -prefix_string This_is_a_test_of_the_emergency_alert_system
    115: !
    116: !TEST*/