Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

Code Block
languagepytext
titlespacedecl.f90
linenumberstrue
<%
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 

...

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. Each line uses the "fortrantype", "rank" and variables "name" from the "variables" dictionary  as is visible in line 13. The "rank" is used to generate a Fortran code snippet to define the variables dimensions. Apart from the "variables" dictionary also a "parameters" dictionary is available that holds all parameters from params.F90. Take a look at the other templates in src/xbeachlibrary/templates/ for more examples.

...

The spaceparams.tmpl file is replaced by the src/xbeachlibrary/variables.F90 file. The main reason is that the Python script that parses the variables.F90 and params.F90 file only needs to parse Fortran code and not also a custom template format as used in the original spaceparams.tmpl file. Each variable is defined on a separate line and all variables are defined as "target". A comment at the end of the line contains meta-data related to the variable and adheres to a specific format. An example variable definition is as follows:

Code Block
languagetext
titlevariables.F90py
 double precision, allocatable, target :: x(:,:)           !< [m] x-coord. original cmp. grid {"shape": ["s%nx+1", "s%ny+1"], "standard_name": "projection_x_coordinate", "broadcast": "d"}

...

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

Fortran variable type definitions

In the new templating system all input and output variables are defined in the params.F90 and variables.F90 files respectively. These Fortran files are parsed by the templating system and need to adhere to a certain syntax as explained in above. Also the variable types definitions are restricted to a certain notation since multiple notations exist in Fortran. The types that are supported are:

  • character
  • logical
  • double precision
  • integer

Static templates

If you use static templates that do not depend on the params or spaceparams structures, still put them in the src/xbeachlibrary/templates/ directory as F90 files. These files are converted to INC files automatically and without modification. If you would put these files as INC files directly in the src/xbeachlibrary/ directory they will be deleted upon project cleaning and not generated again.

Template for writelog functions

...

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 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", "mako" and "py2exe" packages installed. Compiling the executable is done from the command-line as follows:

Code Block
languagebashtext
>> 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 templates, nor after changing or adding variables to the params.F90 or variables.F90 files. The only reason to recompile the Python script is if the templating system in scripts/generate.py itself has changed (e.g. the Fortran parser).

C interface

The original C-interface has become superfluous with the new BMI interface and is removed. In the following table a mapping form the original C-interface to the new BMI interface can be found:

original C-interfaceBMI interface
init()initialize()
outputext() 
executestep()update(dt)
final()finalize()
 get_var(var)
 get_var_type(var)
 get_var_rank(var)
 get_var_shape(var)
 set_var(var, val)
 get_start_time()
 get_current_time()
 get_end_time()

The BMI interface does not support getting derived variables (min, max, avg, var) nor does it support setting of input parameters. It does support some externally controlled time stepping. Externally set time steps are maximized according to the CFL condition. Negative time steps make the model to skip the absolute value of the time step in time, without executing any model. The external program should provide the updated model state as well.