5. Editing and saving
After 2000 steps heat writes a restart file: the same case, with a smaller time step, more steps and a new section that says where to restart from.
program heat
!< Tutorial, chapter 5: editing and saving.
use finer
use penf, only : I4P, R8P
implicit none
type(file_ini) :: case_file, restart
integer(I4P) :: error, iostat
real(R8P) :: dt
call case_file%load(filename='heat.ini')
call case_file%add(section_name='time', option_name='dt', val=5.0e-5_R8P) ! an option that exists: updated
call case_file%add(section_name='time', option_name='steps', val=4000_I4P)
call case_file%add(section_name='time', option_name='start', val=0.2_R8P) ! a new option: appended
call case_file%add(section_name='restart', option_name='from', val='heat-0002000.vtk')
call case_file%add(section_name='restart', option_name='step', val=2000_I4P)
call case_file%del(section_name='time', option_name='adaptive')
call case_file%del(section_name='output')
call case_file%save(filename='restart.ini', retain_comments=.true., iostat=iostat)
if (iostat /= 0) stop 'cannot save restart.ini'
call restart%load(filename='restart.ini', error=error)
call restart%get(section_name='time', option_name='dt', val=dt)
print '(A,ES10.3,A,L1)', 'dt read back: ', dt, ', exact: ', dt == 5.0e-5_R8P
endprogram heatUpdating and appending
call case_file%add(section_name='time', option_name='dt', val=5.0e-5_R8P) ! an option that exists: updated
call case_file%add(section_name='time', option_name='steps', val=4000_I4P)
call case_file%add(section_name='time', option_name='start', val=0.2_R8P) ! a new option: appendedadd(section_name=, option_name=, val=) sets an option: an option that exists is updated in place, a new one is appended to its section, and a missing section is created first (at the end of the file). val is any supported type, scalar or array, and FiNeR writes it as text (see Writing values).
Adding and deleting sections
call case_file%add(section_name='restart', option_name='from', val='heat-0002000.vtk')
call case_file%add(section_name='restart', option_name='step', val=2000_I4P)
call case_file%del(section_name='time', option_name='adaptive')
call case_file%del(section_name='output')del(section_name=, option_name=) deletes one option, del(section_name=) a whole section with its options.
Saving
call case_file%save(filename='restart.ini', retain_comments=.true., iostat=iostat)
if (iostat /= 0) stop 'cannot save restart.ini'save(filename=) writes the file; without filename it uses filename, the public member set by load or by an earlier save. iostat is 0 when everything was written, otherwise the status of the first operation that failed. retain_comments=.true. writes the inline comments back.
$ cat restart.ini
title = cooling of a copper rod
[grid]
cells = 64
length = 0.5 ; metres
origin = 0.0 0.0 0.0
[time]
dt = 0.00005
steps = 4000
start = 0.2
[material]
name = copper
conductivity = 401.0 ; W/(m K)
density = 8960.0
[boundaries]
left = 350.0
right = 300.0
probes = 0.1 0.25 0.4
[restart]
from = heat-0002000.vtk
step = 2000What a save writes, as you can see above:
- options as
name = value, without indentation, whatever their layout in the source; - the inline comments only with
retain_comments=.true.; comment lines are not kept; - a value written on several lines as one line;
- reals as the shortest text that reads back to the same number:
5.0e-5_R8Pbecomes0.00005.
Reading it back
call restart%load(filename='restart.ini', error=error)
call restart%get(section_name='time', option_name='dt', val=dt)
print '(A,ES10.3,A,L1)', 'dt read back: ', dt, ', exact: ', dt == 5.0e-5_R8P$ heat
dt read back: 5.000E-05, exact: TWhat you learned
add updates or appends, del deletes, save(filename=, retain_comments=, iostat=) writes; reals round-trip exactly. Reference: Writing values, Loading, saving, printing.
Next: 6. Writing from scratch.