Date: 2026-07-30T21:14:27+00:00

Actual source code: ex9f.F90

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

      8:   integer, parameter :: Pr = PETSC_REAL_KIND
      9:   integer, parameter :: Pi = PETSC_INT_KIND

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

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

     36:   allocate (iarray(nopt), source=-1_Pi)
     37:   allocate (rarray(nopt), source=-99.0_pr)
     38:   allocate (barray(nopt), source=PETSC_FALSE)
     39:   allocate (sarray(nopt))
     40:   sarray = 123.456_Pr

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

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

    102: !
    103: !/*TEST
    104: !
    105: !   build:
    106: !      requires: !complex
    107: !
    108: !   test:
    109: !      output_file: output/empty.out
    110: !
    111: !   test:
    112: !      suffix: 2
    113: !      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
    114: !
    115: !TEST*/