You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

In XBeach repetitive code is stored in include files. These include files are generated by the "makeincludes" program that is compiled and run as a pre-compile action for the XBeach library (src/xbeachlibrary). The "makeincludes" program is, like XBeach, written in Fortran. Recently, a new development branch for XBeach is started that implements a Basic Model Interface (BMI). A BMI lets XBeach communicate with other models and is used to connect XBeach, for example, to aeolian sediment transport models. Implementation of a BMI is relatively simple, but involves a lot of repetitive code and therefore a many include files. Generating all these files using more Fortan code was considered cumbersome, time-consuming and not transparent. Therefore the "makeincludes" program is replaced by a simple Python script in this branch.

The BMI branch is soon to be merged with the trunk. Therefore it becomes relevant for all XBeach developers to understand the differences between the "makeincludes" program and the Python script. These differences are listed on this page.

Components

The "makeincludes" program is replaced by the following components:

  • Python script that parses Fortran code and generates include files based on templates (stored in scripts/generate.py)
  • Templates for include files (stored in src/xbeachlibrary/templates/)

Python script

The "makeincludes" program used to parse src/xbeachlibrary/params.F90 and src/xbeachlibrary/spaceparams.tmpl to find all input parameters and output variables respectively. The new system uses a Python script located in scripts/generate.py to parse the Fortran code. Also, the spaceparams.tmpl file is replaced by the src/xbeachlibrary/variables.F90 file. The main reason is that the Python script then only needs to parse Fortran code and not also a custom template format as used in the original spaceparams.tmpl file.

After parsing the relevant Fortran code the Python script defines two dictionaries: "parameters" and "variables" that hold all the input parameters and output variables respectively, including their properties like rank, size, type and description. These dictionaries are used to parse the templates stored in src/xbeachlibrary/templates/ and generate the include files necessary to compile XBeach.

Templates

Templates are written in Fortran and therefore have the F90 file extension. Each template file will result in a single include file that has the INC file extension and will be written in the src/xbeachlibrary/ directory. To add an include file simply add a template file to the src/xbeachlibrary/templates/ directory and it will be picked up by the Python script. Templates define the Fortran code that is repeated in include files. Additional template markers define how the code is repeated using snippets of Python code. The Mako template engine is used to do so (see http://www.makotemplates.org).

An example of a template that defines the "spaceparams" structure is:

spacedecl.f90
<%
def dimstr(variable):
    if not variable['rank']:
        txt = ''
    else:
        txt = 'dimension({colons}), '.format(
            colons=','.join(':'*variable['rank'])
        )
    return txt
%>

%for variable in variables:
    ${variable['fortrantype']}, ${dimstr(variable)} pointer :: ${variable['name']}
%endfor
!directions for vi vim: filetype=fortran : syntax=fortran 

Python code can be added to a template in between the "<%" and "%>" markers. Single line code can be preceded simply by a "%". In this example the first 10 lines define a Python function that generates a Fortran code snippet to define the dimensions of a variable. The function is used on line 13. Line 13 is in fact the only Fortran code line in this template. Line 12 and 14 form a for-loop that loops over all variables defined in the "variables" dictionary provided by the Python script and holds all variables defined in src/xbeachlibrary/variables.F90. This include file will therefore contain a single line of Fortran code for each variable defined in src/xbeachlibrary/variables.F90.

It is possible to use Python variables within the Fortran code by including the Python variable within "${" and "}" markers as done multiple times in line 13.

More changes

A few other things changed in the XBeach code structure along with the new templating system as explained in the following subsections.

Template for writelog functions

The writelog functions are also considered repetitive and therefore generated using a template as well. You can add writelog functions by adding a format code to the "formats" variable in the src/xbeachlibrary/templates/writelog.f90 and src/xbeachlibrary/templates/writeloginterface.f90 templates (e.g. "aiaiafai"). The necessary functions and interfaces are generated accordingly.

Python dependency

Compiling XBeach now depends on Python to generate the include files. Python is shipped with all popular Unix distributions, therefore this is no problem for Unix users. For Windows users there Python is not always available. Therefore the scripts/generate.py is compiled as Windows executable so Windows users do not need Python to compile XBeach. Only when the scripts/generate.py script is modified the executable needs to be recompiled on a Windows machine with Python and the "distutils" and "py2exe" packages installed. Compiling the executable is done from the command-line as follows:

>> cd scripts/
>> python setup.py py2exe

The Windows executable is stored in scripts/dist/. Be aware that there is no need to recompile the Python script after changing or adding on of the templates, not after changing or adding the params.F90 or variables.F90 files. The only reason to recompile the Python script is if the templating system itself has changed (e.g. the Fortran parser).


  • No labels