Skip to content

4. Editing and saving ​

For a restart, solver changes its case: a longer run with a finer time step, no output frequency, no notes, and a new restart section. Then it saves the result.

f90
program solver
!< Tutorial, chapter 4: editing and saving.
use foxy
implicit none
type(xml_file) :: case_file
integer(I4P)   :: time, restart, iostat

call case_file%parse(filename='case.xml')
time = case_file%find(path='case/time')
call case_file%set_content(id=case_file%find(name='steps', parent=time), value=1000_I4P)   ! numbers as they read back
call case_file%set_attribute(id=case_file%find(name='dt', parent=time), name='unit', value='ms')
call case_file%set_content(id=case_file%find(name='dt', parent=time), value=0.5_R8P)
call case_file%delete_attribute(id=case_file%find(name='output'), name='every')
call case_file%delete_tag(name='notes')                      ! the tag and its subtree
call case_file%set_name(id=case_file%find(name='output'), name='results')     ! rename
call case_file%add_tag(xml_tag(name='restart'), after=time, id=restart)    ! right after <time>
call case_file%add_tag(xml_tag(name='from', content='results.xml'), parent=restart)
call case_file%add_tag(xml_tag(name='step', content='500'), parent=restart)
call case_file%save(filename='restart.xml', iostat=iostat)
print '(A,I0)', 'saved restart.xml, iostat = ', iostat
endprogram solver

Every edit takes a tag ID. Contents and attributes:

  • set_content(id, value=) replaces the whole content of a tag (its children included) with a number, a logical or a list (values=): integers with their digits, reals with the shortest text that reads back exactly (0.5, not 5.000000000000000E-001), or as fmt= formats them. set_content(id, content=) takes text, stored as it is: escape text that comes from users with xml_escape (see chapter 9).
  • set_attribute(id, name, value) adds or replaces an attribute: text (escaped for you), a number, a logical, or a list (values=); delete_attribute(id, name) removes one.

The structure:

  • xml_tag(name=, content=, attributes_stream=, is_self_closing=) makes a single tag, not yet in a document; add_tag puts a copy of it into the document and returns its ID, so the new tag can get children of its own.
  • add_tag(tag, parent=) appends it as the last child of parent (a root tag without parent); add_tag(tag, after=) (or before=) inserts it next to a sibling: here restart goes right after time.
  • delete_tag(name=) or delete_tag(id=) removes a tag with its subtree.
  • set_name(id, name) renames a tag (the new name must be an XML name).
  • IDs change with the structure: a deletion, or an insertion with before=/after=, renumbers the tags after it, so that IDs keep the document order; an append with parent= takes the next free ID (the new tag may then come before tags with smaller IDs). Find IDs again after a structural edit.

Then save writes the document: the prolog kept from the source (declaration and comments), the tags, a final new line.

Running it ​

$ solver
saved restart.xml, iostat = 0
$ cat restart.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- the case of a (pretend) flow solver: the input of the FoXy tutorial -->
<case name="cavity" version="2">
  <mesh>
    <grid ni="64" nj="32" nk="1"/>
    <domain>0.0 1.0 0.0 0.5 0.0 0.0</domain>
  </mesh>
  <time>
    <dt unit="ms">0.5</dt>
    <steps>1000</steps>
    <adaptive>true</adaptive>
  </time>
  <restart>
    <from>results.xml</from>
    <step>500</step>
  </restart>
  <fluid name="air">
    <density unit="kg/m3">1.225</density>
    <viscosity unit="Pa s">1.8e-5</viscosity>
  </fluid>
  <boundary side="west" type="inflow">
    <velocity>1.0 0.0 0.0</velocity>
  </boundary>
  <boundary side="east" type="outflow"/>
  <boundary side="north" type="wall"/>
  <boundary side="south" type="wall"/>
  <results>results.xml</results>
</case>

What you learned

Edit by ID: contents and attributes (set_content, set_attribute, delete_attribute), the structure (add_tag with parent=, before= or after=, delete_tag, set_name); find IDs again after a structural edit; then save. Reference: Editing.

Next: 5. Writing the results.