Run TUFLOW From a Batch-file

From Tuflow
Jump to navigation Jump to search

TUFLOW simulations can also be started from a batch file. Batch files are text files which contain a series of commands. Batch files allow a large degree of flexibility in starting TUFLOW simulations, advanced batch files can be used to:

  • Run multiple simulations
  • Set CPU priority for TUFLOW simulations
  • Create a wait (e.g. start a simulation in 1 hour)
  • Test simulation input
  • Copy a TUFLOW model
  • Post process results after simulation has finished.

Please add any suggestions (I know there are some batch file power users out there!) to the discussion page.

Batch file for Tutorial Model Module 1

To use a batch file, create a new text file in the TUFLOW\runs directory. The name is arbitrary but the file must have the extension ".bat". For example the file name could be _run_TUFLOW.bat.

Open this file in a text editor and add the following line.

"C:\TUFLOW\Releases\2011-09\w32\TUFLOW_iSP_w32.exe"  "M01_5m_001.tcf"

This is the simplest command line for starting TUFLOW, the line above contains only two arguments, the first is the path to the TUFLOW executable and the second argument "M01_5m_001.tcf" is the simulation file name. Depending on the location of the executable the above command line will need to be modified. The quotations around the TUFLOW executable and the .tcf file name are only required if there are spaces in the file names. However, it is recommended to include these to avoid problems.

TIP:
A second line with the text "pause" can be added, this will prompt the user to press a key at the end of the batch file. This is useful if the DOS window disappears from the screen, before you can read the output, the pause command will keep the window open.

"C:\TUFLOW\Releases\2011-09\w32\TUFLOW_iSP_w32.exe"  "M01_5m_001.tcf"
pause

Other Batch File Switches

These switches are available in all batch files, they are not specific to TUFLOW.

Starting process in new window

In order to run multiple simulations at the same time, each simulation needs be started in a new console window. To do this begin the batch file line with the "Start" command, a for the window is then added. For example, to start the simulation above in a new window titled "TUFLOW" the following would be used.

Start "TUFLOW" "C:\TUFLOW\Releases\2011-09\w32\TUFLOW_iSP_w32.exe"  "M01_5m_003.tcf"

Wait

If multiple simulations are to be run, it is often desirable to run these in series, i.e. the second simulation starts after the first has finished. a /wait switch can be added which makes the batch file wait until the process is finished before moving onto the next command.
An example of this is:

Start "TUFLOW" /wait "C:\TUFLOW\Releases\2011-09\w32\TUFLOW_iSP_w32.exe" M01_5m_003.tcf
Start "TUFLOW" /wait "C:\TUFLOW\Releases\2011-09\w32\TUFLOW_iSP_w32.exe" M01_2p5m_005.tcf

Without the wait command, both simulations would start at the same time.
Note: Unless there is a third simulation in the batch file the /wait command in the second line is not required, this prevents the batchfile from moving onto the third line.

Setting a priority

The priority of a process (such a TUFLOW simulation) can be specified within a batch file. This can be useful for a number of reasons:

  • Lowering the priority of a simulation to allow you to continue working;
  • Setting a high priority for critical runs; or
  • Making sure you runs are high priority on a shared computing resource!

The following are valid priority switches (in increasing order of priority):

  • /LOW
  • /BELOWNORMAL
  • /NORMAL
  • /ABOVENORMAL
  • /HIGH
  • /REALTIME

An example of a low priority simulation is:

start "TUFLOW" /low C:\TUFLOW\Releases\2011-09\w32\TUFLOW_iSP_w32.exe"  "M01_5m_003.tcf"

TIP: The priority of a TUFLOW simulation can be changed at a later stage by using the Windows Task Manager, navigate to the Processes tab and right click on the TUFLOW process (e.g. TUFLOW_iSP_w64.exe) and select the set priority option.

TUFLOW switches in a batch file

These switches are specific to TUFLOW simulations.

Testing a simulation

The -t (test) switch is very useful for testing the data input without running the simulation. It is good practice to use this switch before carrying out the simulations, as this will tell you whether there are any data input problems. The -t switch runs TUFLOW to just before it starts the hydrodynamic computations. An example is:

start "TUFLOW" C:\TUFLOW\Releases\2011-09\w32\TUFLOW_iSP_w32.exe" -t "M01_5m_003.tcf"

Batch mode in TUFLOW

The use of the –b (batch) switch which suppresses the need to press the return key at the end of a simulation. This ensures that one simulation proceeds on to the next without any need for user input. This is required for running multiple simulations in series (one after the other). An example is:

start "TUFLOW" C:\TUFLOW\Releases\2011-09\w32\TUFLOW_iSP_w32.exe" -b "M01_5m_003.tcf"

Copy a TUFLOW model

TUFLOW can be run in copy mode. This copies all TUFLOW inputs for the simulation into a subdirectory where the .tcf is located (generally TUFLOW\runs\). There are two copy options: -c and -ca.
-c copies only the files read by TUFLOW, for example the .mif and . mid files are read by TUFLOW, but the MapInfo format files (.tab, .id, .dat and .map) are not read by TUFLOW and these files will not be copied.
-ca copies all files with the same name (regardless of extension), if the 2d_code_M01_003.MIF and 2d_code_M01_003.MID files are copied all of the MapInfo files (2d_code_M01_003.DAT, 2d_code_M01_003.ID, 2d_code_M01_003.MAP and 2d_code_M01_003.TAB), will all be copied.
An example that copies all files is:

start "TUFLOW" C:\TUFLOW\Releases\2011-09\w32\TUFLOW_iSP_w32.exe" -ca "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_003.tcf"

Advanced Batch Files

This section is yet to be finalised.

Creating a delay

It is possible to create a delay of specified length in a batch file. The best way do this is actually to create a second batch file! To do this create a text file and call it wait.bat. Within that file enter the following text:

@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul

To create the pause we ping an address for a set time and disregard the output. Once the wait.bat has been created, it can be called from another batchfile using the following syntax:

call wait <time in seconds.

In the example below, the first simulation is started and then 60 seconds later (using the wait.bat file) the second simulation is started. Note there is no /wait flag specified, if there was the batchfile would wait for 60 seconds after the first simulation had finished before starting the second simulation.

Start "TUFLOW" "C:\TUFLOW\Releases\2011-09\w32\TUFLOW_iSP_w32.exe" M01_5m_003.tcf
call wait 60
Start "TUFLOW" /wait "C:\TUFLOW\Releases\2011-09\w32\TUFLOW_iSP_w32.exe" M01_2p5m_005.tcf

For this to work the wait.bat file needs to be in the same folder as the batchfile calling it. You can put the file in a specific location and add this path as an environment variable. If this is done "call wait

Variables

Batch files can be easily setup so that they are more generic and easily customised when moving from one project to another. For example, in the below a variable, TUFLOWEXE, is used to define the path to the TUFLOW exe to be used, and a variable RUN is used to incorporate options such as the /wait so that the simulations run in series (one after the other).

set TUFLOWEXE=C:\TUFLOW\Releases\2011-09\w64\TUFLOW_iSP_w64.exe
set RUN=start "TUFLOW" /wait "%TUFLOWEXE%" -b
%RUN% MR_H99_C25_Q100.tcf
%RUN% MR_H99_C25_Q050.tcf
%RUN% MR_H99_C25_Q020.tcf

The advantage of using variables is if the path to the TUFLOW exe changes, or to run a different version of TUFLOW, it is just a simple change in the .bat file. In the above, note the use of quotes around %TUFLOWEXE% in the definition for the RUN variable – quotes are needed around file pathnames whenever they contain a space.

Looping in a batch file

Coming soon.