An exciting update to the PyTUFLOW Library

Exciting Potential for Workflow Efficiencies and Automated Workflows

8 October 2025

Introducing the Updated PyTUFLOW Library

We're excited to announce the release of a significant update to the PyTUFLOW library which brings a seamless interface between Python and TUFLOW, offering the potential for automating workflows on both the input and output side of TUFLOW, offering time-saving efficiencies to your TUFLOW modelling projects.

The PyTUFLOW library has been around since July 2019, providing a method for extracting TUFLOW time series results for the purposes of checking model health and automating plotting of results including during the simulation.  The PyTUFLOW library underpins the plotting of 1D results within the TUFLOW Viewer which is part of the QGIS TUFLOW plugin

Since its release in 2019, the library has been extended to provide a framework for future development of the TUFLOW Viewer with support for additional results formats further to time-series results, such as the XMDF, GPKG and NetCDF result formats.  These updates are now available to TUFLOW users as well as the extension into the TUFLOW input side providing a programmatic approach for users to interact with TUFLOW control files and databases.  This updated library is now available to all and documented here with examples.

As well as providing users the opportunity to build their own tools and scripts, the update to PyTUFLOW also allows it to be used as the engine for the TUFLOW Plugin.  This allows the plugin to be dedicated to GUI functionality as well as the potential to increase the offering for model build and checking tools, within the TUFLOW plugin.  With PyTUFLOW being the engine for interacting with TUFLOW control and database files, this also allows its use in other software outside of the QGIS environment. 

The following provides some simple examples of the use of PyTUFLOW in a standalone workflow.

Input Example

The following script opens an existing TUFLOW model TCF, identifies the input command Read Materials File and then updates the right hand side of the command to include a multiplier to apply to the roughness specification to increase any Manning's n values by 10% for a sensitivity analysis.  Changes to the TCF are made temporarily and therefore need writing to disk, in this case to the same TCF using the inc='inplace' option.  It would be possible to increment the TCF if required.

from pytuflow import TCF

# Define Roughness Multiplier

mannings_n_multiplier = 1.1

# Define TUFLOW Control File
tuflow_tcf = r"path_to_tuflow_tcf"
tcf = TCF(tuflow_tcf)
roughness = tcf.find_input('Read Materials File')[0] # Find Read Materials File Command
roughness.rhs = fr" ..\model\materials.csv | {mannings_n_multiplier}" # Update Right Hand Side of TUFLOW Command
tcf.write(inc='inplace') # Write the TCF

Running TUFLOW Simulations Example

It's also possible to run TUFLOW using the PyTUFLOW library.  This could be used in conjunction with a scripted model build/input workflow process and/or a QA/Results analysis for the simulation outputs.

The key to running a TUFLOW model from PyTUFLOW is to provide a location where all the TUFLOW executables are located. This only needs to be done once and can be done by registering a TUFLOW binary folder using the following code.

from pytuflow import register_tuflow_binary_folder

register_tuflow_binary_folder(r'path_to_tuflow_executables')

The folder structure should be similar to the below structure, where the folder name is the TUFLOW version number and the TUFLOW executables are located within that folder:

/path_to_tuflow_executables
  ├── 2025.0.0
  │   ├── TUFLOW_iSP_w64.exe
  │   ├── TUFLOW_iDP_w64.exe
  ├── 2025.1.0
  │   ├── TUFLOW_iSP_w64.exe
  │   ├── TUFLOW_iDP_w64.exe
  ├── 2025.1.2
  │   ├── TUFLOW_iSP_w64.exe
  │   ├── TUFLOW_iDP_w64.exe
  ├── 2025.2.0 
  │   ├── TUFLOW_iSP_w64.exe 
  │   ├── TUFLOW_iDP_w64.exe

Once the TUFLOW executables location has been registered, TUFLOW can then be run by specifying the TCF and the version of TUFLOW to be used.

from pytuflow import TCF

# Define TUFLOW Control File
tcf = TCF(r'path_to_tuflow_tcf')
tcf_run = tcf.context()
proc = tcf_run.run('2025.2.0') # Define which TUFLOW Version to use
proc.wait() # Wait for the model to finish running

Accessing TUFLOW 2D Results Example

It is also possible to interrogate the resulting TUFLOW outputs.  This has always been possible with 1D results, including 2D Plot Outputs (2D_PO), within PyTUFLOW (see the example here) but is now extended to interrogating 2D results directly.  For example, the following script will read the water level time series from a NetCDF grid output for a specified TUFLOW TCF for the location 293275.172,6177973.546.

from pytuflow import TCF
tcf = TCF(r'path_to_tuflow_tcf')
nc_path = tcf.context().output_folder_2d() / f'{tcf.context().output_name()}.nc' # Define NetCDF output path
ncgrid = NCGrid(nc_path) # Read the NetCDF output
df = ncgrid.time_series((293241.680, 6178015.150), 'water level') # Read Water Level Time Series

This outputs a dataframe which can be further analysed or plotted.  If GDAL Python bindings are installed, a Shapefile or GPKG (or any other GDAL supported vector format) can be used as the location input rather than an X,Y co-ordinate.

Combining the Above PyTUFLOW Examples

The above 3 examples, set up within their own individual functions, can be combined to create a process for running a Monte Carlo analysis similar to the following.

# Simple Example of a Monte Carlo Analysis for Manning's n sensitivity
# Monte Carlo analysis done on the Roughness Multiplier
# TUFLOW is then run. Results obtained for a single point and recorded to a csv file

from pytuflow import TCF, TPC, NCGrid
import os
import random
import numpy as np
import pandas as pd import plotly.graph_objs as go # Define TUFLOW Control File tuflow_tcf = r"path_to_TUFLOW_TCF" tcf = TCF(tuflow_tcf) # Number of Monte Carlo simulations num_simulations = 100 n_multiplier_min = 0.25 n_multiplier_max = 2.5 # Generate random samples of n n_values = np.random.uniform(n_multiplier_min, n_multiplier_max, num_simulations) # Check for existing csv file containing outputs and delete if it exists file_path = "cumulative_dataframe.csv" if os.path.exists(file_path): os.remove(file_path) # Function to update Manning's n multiplier def manning_update(mannings_n_multiplier): roughness = tcf.find_input('Read Materials File')[0] roughness.rhs = rf" ..\model\materials.csv | {mannings_n_multiplier}" tcf.write(inc='inplace') def run_tuflow_simulation(tcf, version): tcf_run = tcf.context() proc = tcf_run.run(version) proc.wait() def twod_result_analysis(x_point, y_point, result_parameter): nc_path = tcf.context().output_folder_2d() / f'{tcf.context().output_name()}.nc' ncgrid = NCGrid(nc_path) df = ncgrid.time_series((x_point, y_point), result_parameter) return df # Run simulations for n in n_values: manning_update(n) run_tuflow_simulation(tcf, '2025.2.0') df = twod_result_analysis((293241.680, 6178015.150), 'water level') if os.path.exists(file_path): cumulative_df = pd.read_csv(file_path) new_column = df.iloc[:, 0].reset_index(drop=True) cumulative_df[f"Run_{len(cumulative_df.columns)}"] = new_column cumulative_df.to_csv(file_path, index=False) else: cumulative_df = pd.DataFrame({"Run_1": df.iloc[:, 0]}) cumulative_df.to_csv(file_path, index=True) # Reset TCF to original setting final_roughness = tcf.find_input('Read Materials File')[0] final_roughness.rhs = rf" ..\model\materials.csv " tcf.write(inc='inplace') print(f"Monte Carlo analysis completed. Results saved to {file_path}.")

In this example, we generate a simple Monte Carlo analysis of Manning's n using the roughness multiplier, update the roughness multiplier in an existing TUFLOW TCF, run the simulation, interrogate the outputs and repeating this process for a user-defined number of ensemble simulations.  The ensemble simulation outputs are then saved to a csv file which could be plotted up similar to the below, showing the ensemble outputs. 

The full script, including an additional section plotting the Monte Carlo ensemble outputs, average output and 95% confidence interval, using the Plotly library can be found here.

Example Scripts

To support the update of PyTUFLOW, there are example scripts available on the TUFLOW Gitlab User Group including the above scripts.  There are other example scripts, which we will be adding to, providing potential applications as described below.

  • Update a TUFLOW Classic Model to TUFLOW HPC

This script converts TUFLOW Classic control files to TUFLOW HPC control files which utilise GPU hardware.  This simple script creates a copy of the existing TCF and appends the following commands to set the model up as a TUFLOW HPC model.

    Solution Scheme == HPC

    Hardware == GPU 

Prior to doing this, the script first checks that the model is not already set up as a TUFLOW HPC model and also checks and reports any TUFLOW Classic functionality that is not supported by TUFLOW HPC as documented here.  The final TUFLOW HPC control file has '_HPC' appended to the TCF name.

  • Sensitivity Scenarios

This script, available here, programmatically generates a scenario block which allows the simulation of Manning's n sensitivity tests.  The user specifies a % sensitivity they would like to test and the script identifies the existing Read Materials File command, and generates a scenario block around this with the existing Manning's n specifications adjusted by +/- the user defined percentage.

    If Scenario == -20pct

        Read Materials File == ..\model\materials.csv | 0.8

    Else If Scenario == +20pct

        Read Materials File == ..\model\materials.csv | 1.2

    Else

        Read Materials File == ..\model\materials.csv

    End If

The updated control file is then written to a new TCF file with the scenario wildcard '_~S1~_' appended to the name.

  • Addition of Metadata

This script allows the addition of a metadata block to the top of TUFLOW control files within a Runs directory.  The script using a simple GUI which allows the user-defined input of various metadata fields which is then appended to the TUFLOW control files.

  • Missing Files Check

This script inspects TCF files within a TUFLOW Runs folder and reports any GIS layers which are missing.  This is useful should you be sending the model to a colleague or external client and would like to check that any copy tool has selected the relevent TUFLOW Files.

The potential applications of the PyTUFLOW library are endless. Keep an eye on the TUFLOW Gitlab User Group for further examples and why not add your own.

Share this

Related content