GRVY-0.38.0
Input Parsing Example using Fortran API

An illustrative code example in F90 using the input parsing API is shown below (and is also available in the examples directory from a top-level install). This C example is configured to parse the example pecos-input.txt file shown here and uses many of the available parsing routines including a demonstration of default value registration.

!!-----------------------------------------------------------------------bl-
!!--------------------------------------------------------------------------
!!
!! libGRVY - a utility library for scientific computing.
!!
!! Copyright (C) 2008-2013,2018-2022 The PECOS Development Team
!! Additional Copyright (C) 2018 individual authors
!!
!! This library is free software; you can redistribute it and/or
!! modify it under the terms of the Version 2.1 GNU Lesser General
!! Public License as published by the Free Software Foundation.
!!
!! This library is distributed in the hope that it will be useful,
!! but WITHOUT ANY WARRANTY; without even the implied warranty of
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
!! Lesser General Public License for more details.
!!
!! You should have received a copy of the GNU Lesser General Public
!! License along with this library; if not, write to the Free Software
!! Foundation, Inc. 51 Franklin Street, Fifth Floor,
!! Boston, MA 02110-1301 USA
!!
!!-----------------------------------------------------------------------el-
!!
!! input.c: Fortran example of basic input file parsing via libGRVY.
!!
!!--------------------------------------------------------------------------
!!--------------------------------------------------------------------------
program main
use grvy
implicit none
integer :: flag
integer :: error
real :: reyn
real*8 :: magicval
integer :: iter_max
integer :: switches(4)
integer, allocatable :: switches2(:)
character :: filename*100
character :: key1*100
character :: key2*100
logical :: restart
! Initialize/read the file
call grvy_input_fopen("./pecos-input.txt ",flag);
! Dump the whole file to stdout
print*,' ------ Full Dump ------'
call grvy_input_fdump(flag);
print*,' ---- End Full Dump ----'
print*,' ------ Full Dump (delimited) ------'
call grvy_input_fdump_delim('# ',flag);
! Read specific variables
call grvy_input_fread_real ("reyn",reyn,flag)
call grvy_input_fread_int ("iter_max",iter_max,flag)
call grvy_input_fread_char ("gridfile",filename,flag)
print*,'reyn = ',reyn
print*,'iter_max = ',iter_max
print*,'gridfile = ',trim(filename)
call grvy_input_fread_int_vec ("solver/switches",switches,4,flag)
print*,'aa:switches = ',switches(1),switches(2),switches(3),switches(4)
allocate(switches2(4))
call grvy_input_fread_int_vec ("solver/switches",switches2,4,flag)
print*,'bb:switches2 = ',switches2(1),switches2(2),switches2(3),switches2(4)
call grvy_input_fread_char_ivec("solver/keywords",key1,1,flag)
call grvy_input_fread_char_ivec("solver/keywords",key2,2,flag)
print*,'solver/keyword 1 = ',trim(key1)
print*,'solver/keyword 2 = ',trim(key2)
! Read a logical (you need to provide a default value which is used if the
! desired keyword is not available in the input file)
call grvy_input_fread_logical ("restart",restart,.false.,flag)
print*,'restart = ',restart
! Attempt to read undefined variable (should get error message)
call grvy_input_fread_int("koomie",iter_max,flag)
! Register a default value and read variable which is not in input
! file (allows for backwards compatability of input files)
call grvy_input_register_double ("solver/magicval",0.03d0,flag)
error = error*flag
call grvy_input_fread_double ("solver/magicval",magicval,flag)
print*,'solver/magicval = ',magicval
! Disable error messages if you want to control them yourself
! If you query the unknown variable again it should be quiet
call grvy_log_setlevel(grvy_nolog)
call grvy_input_fread_int("koomie",iter_max,flag)
stop
end program main
grvy_input_fread_int
int grvy_input_fread_int(const char *keyword, int *value)
Read a single input integer value corresponding to the keyword path.
grvy_input_fdump_delim
int grvy_input_fdump_delim(const char *prefix)
Dump input file settings to stdout prefixed by a delimiter.
grvy_input_fread_char_ivec
int grvy_input_fread_char_ivec(const char *keyword, char **values, int elem)
Read a character array of size nelems corresponding to the keyword path.
grvy_input_fread_char
int grvy_input_fread_char(const char *keyword, char **value)
Read a single input character string value corresponding to the keyword path. For convenience,...
grvy_input_register_double
int grvy_input_register_double(const char *keyword, double value)
Register default double value corresponding to the keyword path.
grvy_input_fread_int_vec
int grvy_input_fread_int_vec(const char *keyword, int *values, int nelems)
Read an integer array of size nelems corresponding to the keyword path.
grvy_input_fopen
int grvy_input_fopen(const char *filename)
Open a libGRVY style input file.
grvy_log_setlevel
void grvy_log_setlevel(int priority)
Set current logging priority level.
grvy_input_fread_double
int grvy_input_fread_double(const char *keyword, double *value)
Read a single input double value corresponding to the keyword path.
grvy_input_fdump
int grvy_input_fdump()
Dump input file settings to stdout.

Generated on Wed Jul 12 2023 16:53:23 for GRVY-0.38.0 by  doxygen 1.8.17