2. Reading the case file
The case lives in a file, heat.ini:
; heat.ini: the case of a heat conduction run
title = cooling of a copper rod
[grid]
cells = 64
length = 0.5 ; metres
origin = 0.0 0.0 0.0
[time]
dt = 1.0e-4
steps = 2000
adaptive = .true.
[material]
name = copper
conductivity = 401.0 ; W/(m K)
density = 8960.0
[boundaries]
left = 350.0
right = 300.0
; the probes, one value per line
probes = 0.1
0.25
0.4
[output]
format = vtk
fields = temperature flux
every = 500heat loads it and reads its settings as numbers, logicals and strings.
program heat
!< Tutorial, chapter 2: reading the case file.
use finer
use penf, only : I4P, R8P
implicit none
type(file_ini) :: case_file
integer(I4P) :: error
integer(I4P) :: cells, steps
real(R8P) :: length, dt, conductivity
logical :: adaptive
character(len=64) :: title
real(R8P) :: origin(3)
real(R8P), allocatable :: probes(:)
call case_file%load(filename='heat.ini', error=error)
if (error /= 0) then
print '(A,I0)', 'cannot read heat.ini, error ', error
stop 1
endif
call case_file%get(section_name='grid', option_name='cells', val=cells)
call case_file%get(section_name='grid', option_name='length', val=length)
call case_file%get(section_name='time', option_name='dt', val=dt)
call case_file%get(section_name='time', option_name='steps', val=steps)
call case_file%get(section_name='time', option_name='adaptive', val=adaptive)
call case_file%get(section_name='material', option_name='conductivity', val=conductivity)
call case_file%get(section_name='', option_name='title', val=title)
call case_file%get(section_name='grid', option_name='origin', val=origin)
allocate(probes(case_file%count_values(section_name='boundaries', option_name='probes')))
call case_file%get(section_name='boundaries', option_name='probes', val=probes)
print '(A)', trim(title)
print '(A,I0,A,F5.2,A)', 'grid: ', cells, ' cells over ', length, ' m'
print '(A,ES10.3,A,I0,A,L1)', 'time: dt = ', dt, ', ', steps, ' steps, adaptive = ', adaptive
print '(A,F6.1)', 'conductivity: ', conductivity
print '(A,3F5.1)', 'origin:', origin
print '(A,I0,A,3F6.2)', 'probes (', size(probes), '):', probes
endprogram heatLoading
call case_file%load(filename='heat.ini', error=error)
if (error /= 0) then
print '(A,I0)', 'cannot read heat.ini, error ', error
stop 1
endifload(filename=) reads the whole file. error is 0 on success, otherwise the I/O status of the read: a missing file gives a non-zero error, and the file_ini keeps the sections it held before (its filename becomes the new one).
Typed values
call case_file%get(section_name='grid', option_name='cells', val=cells)
call case_file%get(section_name='grid', option_name='length', val=length)
call case_file%get(section_name='time', option_name='dt', val=dt)
call case_file%get(section_name='time', option_name='steps', val=steps)
call case_file%get(section_name='time', option_name='adaptive', val=adaptive)
call case_file%get(section_name='material', option_name='conductivity', val=conductivity)
call case_file%get(section_name='', option_name='title', val=title)- Integers of any kind (
I1PtoI8P), reals (R4P,R8P, andR16Pwhere quadruple precision is enabled), complex numbers, logicals and strings: the type ofvalpicks the conversion. - A logical is written as Fortran reads it:
.true.,T,true,.F.,false(see the cookbook). titlecomes before the first header, so it belongs to the global section, whose name is the empty string:section_name=''.- Inline comments (
length = 0.5 ; metres) are not part of the value.
Lists
call case_file%get(section_name='grid', option_name='origin', val=origin)
allocate(probes(case_file%count_values(section_name='boundaries', option_name='probes')))
call case_file%get(section_name='boundaries', option_name='probes', val=probes)An array val reads a list of values separated by blanks. count_values gives the number of values first, so the array can be allocated to fit; probes is written on three lines, and continuation lines (lines without =) are joined to the option above them.
Running it
$ heat
cooling of a copper rod
grid: 64 cells over 0.50 m
time: dt = 1.000E-04, 2000 steps, adaptive = T
conductivity: 401.0
origin: 0.0 0.0 0.0
probes (3): 0.10 0.25 0.40What you learned
load(filename=, error=), typed get, the global section '', lists with count_values. Reference: Reading values, The INI syntax.
Next: 3. Optional settings.