Run TUFLOW From a Batch-file: Difference between revisions

Content deleted Content added
 
(53 intermediate revisions by 8 users not shown)
Line 11:
Batch files are executed by double clicking the created .bat file from Windows Explorer.
<ol>
==Simple batch file==
==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.
 
<pre>"C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" "M01_5m_001.tcf"</pre>
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.
 
==Batch File Switches==
'''TIP:'''<br>
These switches are available in all batch files, they are not specific to TUFLOW.
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.
 
<pre>"C:\TUFLOW\Releases\2016-03-AE\w64\TUFLOW_iSP_w64.exe" "M01_5m_001.tcf"
===Pause===
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 console window disappears from the screen, before you can read the output. The pause command will keep the window open.
<pre>"C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" "M01_5m_001.tcf"
pause</pre>
 
Click here to return to [[Tutorial_Module01#Run_TUFLOW_to_Create_Empty_.28Template.29_GIS_Files| Tutorial Model Module 1]].
 
==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.
 
<pre>Start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" "M01_5m_003M01_5m_001.tcf"</pre>
===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.<br>
An example of this is:<br>
<pre>Start "TUFLOW" /wait "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" M01_5m_003M01_5m_001.tcf
Start "TUFLOW" /wait "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" M01_2p5m_005M01_2.5m_001.tcf</pre>
Without the wait command, both simulations would start at the same time.<br>
'''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.
Line 54 ⟶ 53:
 
An example of a low priority simulation is:<br>
<pre>start "TUFLOW" /low "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" "M01_5m_003M01_5m_001.tcf"</pre>
'''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.
 
===Minimising the console window===
Using the /min switch will keep the console window for TUFLOW models minimised, so it doesn't pop up to the front every time when the next model starts.
<pre>Start "TUFLOW" /wait /min "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" M01_5m_001.tcf
Start "TUFLOW" /wait /min "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" M01_2.5m_001.tcf</pre>
 
==No Console Option==
Using the -nc switch will run a simulation without opening a console window. This option is desirable for people running simulation on the cloud. Note, if an invalid .tcf file is specified, the simulation stops and returns an error level of 1 to the operating system.
 
<pre>Start "TUFLOW" /wait /min "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -nc M01_5m_001.tcf
Start "TUFLOW" /wait /min "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -nc M01_2.5m_001.tcf</pre>
 
It is possible to get a text output of the console using:
<pre>Start "TUFLOW" /wait /min "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -nc M01_5m_001.tcf > M01_5m_001.txt
Start "TUFLOW" /wait /min "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -nc M01_2.5m_001.tcf > M01_2.5m_001.txt</pre>
 
== TUFLOW switches in a batch file==
Line 62 ⟶ 76:
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:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -t "M01_5m_003M01_5m_001.tcf"</pre>
 
===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:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -b "M01_5m_003M01_5m_001.tcf"</pre>
 
===Automatically Create Folders in a TUFLOW model===
The use of the -acf (automatically create folders) switch prevents the dialog prompt from appearing when encountering non-existent folders (ie. results folders), and creates these folders automatically. If for any reason the folder can't be created, a dialog will appear. This feature was first introduced for Build 2012-05-ACAE and is not available for TUFLOW builds prior to this. For models simulated with Build 2013-12-AA or later, non-existent folders are now automatically created. If you would prefer to have the create folder query dialog to appear, you can specify the –qcf run time option (stands for query create folders).<br>
An example is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -acf "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_003M01_5m_001.tcf"</pre>
 
===Suppress Queries in a TUFLOW model===
The use of the -nq (no queries) switch prevents the termination query dialog from displaying when Ctrl+C is pressed to terminate a simulation cleanly. If –nq is specified and Ctrl-C is pressed, the simulation terminates cleanly without a query dialog to check you are certain, so be careful! This feature was first introduced for Build 2012-05-ACAE and is not available for TUFLOW builds prior to this.<br>
An example is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -nq "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_003M01_5m_001.tcf"</pre>
 
===Copy a TUFLOW model===
Line 85 ⟶ 101:
;* “a” (all);<br>
: The addition of the “a” flag (e.g. -ca) copies all files of the same name for all input files (ie. same name, but different extensions). This option is particularly useful if the .tab and other associated files of a GIS layer need to be archived or delivered.<br>
;*“L” (list);<br>
: The addition of the “L” flag will output the files used by TUFLOW into a .tcl (TUFLOW Copy List) file but not copy the files to a destination folder. This can be useful if scripting the copying of models. To run the copy list the character “L” needs to be specified after the -c input argument. This works for all copy options, for example, the following are all valid; -cL, -caL, -capL. The .tcl file produced is output in the same directory as the .tcf and takes the simulation name.
;* “p” (path); and<br>
: The addition of the “p” flag (e.g. -cp) allows the user to specify an alternate path in which to copy the model. Without this flag, the location defaults to the .tcf’s location. For example, specifying the following, will place a copy of the model into a folder C:\put_model_here:<br>
:: <ttpre> start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -cp "C:\put_model_here" "C:\TUFLOW\runs\M01_5m_003M01_5m_001.tcf" </ttpre><br>
;* “ncf” (no check files)<br>
: The addition of the “ncf” flag (e.g. -cncf) copies the essential input files and excludes all check files.<br>
Line 95 ⟶ 113:
 
Specifying '''-c''' on the TUFLOW command line creates a folder “<.tcf filename>_copy” (or “<.tcf filename>_copy_all” if the “a” flag is added) in the same location as the .tcf file. Under the folder, input files are copied (including the full folder structure), and any check files and output folders created. For example, specifying:<br>
: <tt>start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -c “C:\tuflow_models\my model.tcf”</tt> will make a copy of the TUFLOW model based on the file <tt>“my model.tcf”</tt> in a folder <tt>"my model.tcf_copy"</tt>, or <tt>"my model.tcf_copy_all"</tt> if using the “a” flag.<br>
 
'''Note:'''
Line 112 ⟶ 130:
 
An example that copies all files (-ca switch) is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -ca "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_003M01_5m_001.tcf"</pre>
An example that copies all files to an alternate path (-cap) is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -cap "C:\Copy_of_model" "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_003M01_5m_001.tcf"</pre>
An example that copies all files to an alternate path, excluding any checkfilescheck files, (-capncf) is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -capncf "C:\Copy_of_model" "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_003M01_5m_001.tcf"</pre>
 
===Package a TUFLOW model===
Package model function attempts to copy all input files for all events and scenarios defined in the model, if only single set of events and scenarios is required, -c is preferable. Unlike the -c option, -pm does not read and process the data during the file copy, as such it is substantially faster than -c. The package model function does not require access to a TUFLOW licence.
 
A tool in QGIS (as part of the TUFLOW Plugin) exists which provides a dialog to the package model options, which allows users to bypass the creation of a batch file:<br>
<b><u>[[Package_Model_in_QGIS | Package Model in QGIS]]</b></u>
 
Additional optional flags can be added to the base '''-pm''' switch, in any combination, including:<br>
Line 127 ⟶ 148:
: The addition of the “L” flag (e.g. -pmL) list the files to be copied into an output file, but don’t copy.<br>
;* “ini” (file.ini)<br>
: The addition of the “ini” flag (e.g. -pmini) provides a .ini file with user defined options. A .ini file can be used to overwrite the default base and destination folders, and specify events/scenarios for models with events/scenarios in filenames. ValidNote, optionsscenarios shouldand beevents separatedare byspecified ato verticalhelp barthe package model utility find files that use scenario or event variable names in file names and does not limit the package to specific event and scenarios. To limit the scenarios and events, please use the [[#Copy_a_TUFLOW_model | copy model functionality]]. See the example below for more information.<br>
 
Combinations of the above are also valid, with the order of the optional switches not being important (-pmAllL would be treated the same as -pmLAll).<br>
Line 141 ⟶ 162:
<br>
An example of package model that copies all files is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\2020-0110-AAAE\TUFLOW_iSP_w64.exe" -pmAll "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_003M01_5m_001.tcf"</pre>
An example of package model that copies all files with specified .ini file (-pmAllini) is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\2020-01-AA\TUFLOW_iSP_w64.exe" -pmAllini package.ini "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_003.tcf"</pre>
An example of package model that copies all files except .xf files, (-pmAll -xf0) is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\2020-0110-AAAE\TUFLOW_iSP_w64.exe" -pmAll -xf0 "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_003M01_5m_001.tcf"</pre>
An example of package model that copies all files with specified .ini file (-pmAllini) is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -pmAllini package.ini "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_001.tcf"</pre>
 
====INI files====
An example of .ini file to overwrite the default base and destination folders, and specify events/scenarios for models with events/scenarios in filenames.<br>
The model scenario and event options are required only if there are files that use the event or scenario variables in file names. This option does not limit the files that TUFLOW will package by scenario or event.
E.g. you may have the following command to read in either a base or development TIF grid:
<pre>
Read GRID Zpts == ..\model\grid\DEM_<<~s1~>>.tif
</pre>
In this example, we can assume that the <tt><<~s1~>></tt> variable could be replaced with either <tt>"Base"</tt> or <tt>"Dev"</tt>. The example ini file below shows how to ensure that TUFLOW will package both the "Base" and "Dev" files correctly. The same concept applies for event variables, noting that events defined in the event file (TEF) and their associated boundary files are not required to use this. It is only required if the event variable is used i.e. <tt><<~e1~>></tt>.
<pre>
Base Folder == C:\TUFLOW\Model_to_Package
Copy Destination == C:\TUFLOW\Packaged_Model
Model Scenario ~s1~ == Base | Dev
Model Event ~e1~ == 60m | 90m
</pre>
 
===GPU SolverModule Control===
The -pu switch can be used to select which processingGPU unitscard or cards to direct the simulation towards. At present this only applies to the GPU solver where a simulation is to be directed to a particular GPU card or cards. –pu must be specified once for each device. For example, to direct the simulation to GPU devices 0 and 2, specify -pu0 -pu2. (Note: the GPU device numbering starts a 0, not 1). For example:
<pre>start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -pu0 -pu2 "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_003M01_5m_001.tcf"</pre>
 
===Event and Scenario Management===
The -e and -s switches can be used to manage multiple events and scenarios from a single tuflow control file. The -e and -s wildcard is replaced by the following entry specified in the batch file. For example, the following batch file commands will run two simulations in series:
* M01_Q050_24hr_2m_Dev_003M01_Q050_24hr_2m_Dev_001.tcf; and
* M01_Q100_24hr_2m_Dev_003M01_Q100_24hr_2m_Dev_001.tcf;
 
<pre>Start "TUFLOW" /wait "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -b -e1 Q050 -e2 24hr -s1 2m -s2 Dev "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_~e1~_~e2~_~s1~_s2~_003_001.tcf"
Start "TUFLOW" /wait "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -b -e1 Q100 -e2 24hr -s1 2m -s2 Dev "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_~e1~_~e2~_~s1~_s2~_003_001.tcf"</pre>
 
Please be aware that the % symbol is a special character in a batch file. If the % symbol is used within a wildcard, it will not be read when searching for that wildcard in TUFLOW.<br>
Line 169 ⟶ 205:
Alternatively, using the % symbol twice in a row as shown below should result in a desired wildcard value.<br>
*-e 10%% (TUFLOW will search for 10%)
 
===Set Hardware===
The -hwgpu or -hwcpu switches can be used overwrite any "Hardware ==' command specified in the .tcf to GPU or CPU, respectively. This feature was first introduced for Build 2023-03-AB and is not available for TUFLOW builds prior to this.
 
These switches can be used in conjunction with the -nt (number of threads) or -pu (processing unit id) to provide full control over the hardware usage via the command line.
 
The following command could be used to run a model with CPU hardware on 8 threads:
<pre>"C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -b -hwcpu -nt8 FS_Hardware_01.tcf </pre>
 
The following command could be used to run a model on GPU hardware using 2 devices (GPU 0 and GPU1):
<pre>"C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -b -hwgpu -pu0 -pu1 FS_Hardware_01.tcf </pre>
 
===Set Start Time for a Simulation===
The use of the -st<time_in_hours> (start time) switch allows the user to specify the start time for a simulation. Any start time specified via this run option argument is given the highest priority and overrides the "Start Time ==" settings in the .tcf, event files (.tef) and override files. <br>
An example is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -st0 "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_001.tcf"</pre>
 
===Set End Time for a Simulation===
The use of the -et<time_in_hours> (end time) switch allows the user to specify the end time for a simulation. Any end time specified via this run option argument is given the highest priority and overrides the "End Time ==" settings in the .tcf, event files (.tef) and override files. <br>
An example is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -et3 "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_001.tcf"</pre>
 
===Suppress Message Box Prompts===
The use of the -nmb (no message boxes) switch suppresses the use of Windows message boxes to prompt the user. All prompts will be via the console window. <br>
An example is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -nmb "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_001.tcf"</pre>
 
===Search for a Network License===
The use of the -nwk (network) switch forces TUFLOW to search for a network licence. I.e. Skip the search for a local license. <br>
An example is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -nwk "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_001.tcf"</pre>
 
===Search for a WIBU License===
The use of the -wibu (WIBU license) switch forces TUFLOW to search only for a WIBU Codemeter license.<br>
An example is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -wibu "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_001.tcf"</pre>
 
===Specify the Output Drive===
The -od<drive> (output drive) switch allows the user to specify the Output Drive for a simulation. <br>
For example, the following will redirect all outputs to the C:\ drive:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -odC "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_001.tcf"</pre>
 
===Specify Map Output===
The -oz<name> (output zone) switch specifies that the map output includes the input zone. <br>
For example, the following would include output for 'Zone A':<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -ozZoneA "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_001.tcf"</pre>
 
===Query the Creation of a Folder===
The -qcf (query the creation of a folder) switch is useful if you would prefer to have the create folder query dialog appear (rather than TUFLOW automatically creating folders - see <u>[[Run_TUFLOW_From_a_Batch-file#Automatically_Create_Folders_in_a_TUFLOW_model | -acf]]</u>). <br>
An example is:<br>
<pre>start "TUFLOW" "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -qcf "C:\TUFLOW\Tutorial_Wiki\TUFLOW\runs\M01_5m_001.tcf"</pre>
 
===Execute a Simulation (the Default)===
The -x (execute) switch executes the simulation. This switch is used by default and does not need to be included on the TUFLOW command line.
 
===Copy or Test Model License Free===
For Build 2018-03-AA or later, it is possible to use the <u>[[Run_TUFLOW_From_a_Batch-file#Copy_a_TUFLOW_model | copy model]]</u> (-c option) or <u>[[Run_TUFLOW_From_a_Batch-file#Testing_a_simulation | test model]]</u> (-t option) without using a license. To utilise this licence free copy/test, the -nlc (no license check) input argument must be specified. If running without a TUFLOW licence, no diagnostic output is generated (e.g. messages layer). If these are required, the -nlc option must be removed.
 
===Simulation Log Path (Legacy)===
The -slp (simulation log path) switch is a legacy option for Solftlock (blue) dongles to set the path to a folder on the intranet to log all simulation initiated from the lock. Refer to the <u>[https://www.tuflow.com/downloads/ TUFLOW Manual]</u> (2018 or earlier) for details.<br>
 
==Advanced Batch Files==
The below section shows example batch files for maximizing run efficiency,
===Creating a delay===
====Windows 7 and newer====
For a Windows 7 and newer operating system a delay of a set duration can be created with the Timeout <seconds> command. For example:<br>
<pre>Timeout 10</pre>
Will create a 10 second delay, a countdown timer is given. Press any key to continue.
Line 185 ⟶ 281:
<pre>call wait <time in seconds.</pre>
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.
<pre>Start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" M01_5m_003M01_5m_001.tcf
call wait 60
Start "TUFLOW" /wait "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" M01_2p5m_005M01_2.5m_001.tcf</pre>
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 <time in seconds> can be added to any batch file.<br>
To set the environment variable, you will need to have administrator access to the machine, and add a colon (;) and then the batchfile location (e.g. ";C:\batch_files\" , without the quotes) in the '''path''' system variables. The separator character is a colon (;) this is added to specify a new path. For more details on modifying the environment please see here: [httphttps://support.microsoft.com/kb/310519 httphttps://support.microsoft.com/kb/310519].
 
===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).
 
<pre>set TUFLOWEXE="C:\TUFLOW\Releases\20162020-0310-AE\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</pre>
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 theTUFLOW RUNexecutable variablepath – quotes are needed around file pathnames whenever they contain a space.
 
===Looping in a batch file===
If you have a large number of events to run using the event files a for loop can be used in a batch file. In this example we have 5 event magnitudes (-e1 input) and 5 event durations (-e2 input). The following batch file can be used to run all 25 simulations one after the other:
Line 205 ⟶ 302:
@echo off
 
::REM This sets the variables as local, so you can use another batch file with A and B variables
SetLocal
 
::REM set up variables
set A=Q010 Q020 Q050 Q100 Q200
set B=10min 30min 60min 120min 270min
 
::REM Loop Through
FOR %%a in (%A%) do (
FOR %%b in (%B%) DO (
start "TUFLOW" /wait "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -b -e1 %%a -e2 %%b filename.tcf
)
)
Line 221 ⟶ 318:
</pre>
In order to test the syntax, you can test the batch file by replacing the following line:<br>
<tt>start "TUFLOW" /wait "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -b -e1 %%a -e2 %%b filename.tcf</tt><br>
With the following line:<br>
<tt><u>echo</u> start "TUFLOW" /wait "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -b -e1 %%a -e2 %%b filename.tcf</tt><br>
By using the echo in the command line, rather than starting the simulations the command line will be displayed in the DOSconsole window output. In the example above the DOSconsole window output is shown in the image below.
[[File:DOS nested batch output.png|frame|none]]
 
Line 234 ⟶ 331:
set B=10min 30min 60min 120min 270min
 
FOR %%a in (%A%) do FOR %%b in (%B%) do start "TUFLOW" "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -b -e1 %%a -e2 %%b filename_~e1~_~e2~.tcf
pause
</pre>
Line 248 ⟶ 345:
FOR %%b in (%B%) do (
FOR %%c in (%C%) do (
start "TUFLOW" /wait "C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe" -b -e1 %%a -e2 %%b -s1 %%c filename_~e1~_~e2~_~s1~.tcf
)
)
Line 256 ⟶ 353:
 
===Looping in a batch file up to a certain number of runs===
The below example shows a method for controlling the number of concurrent simulations.<br>
See also this post by user Ben G T on the
[http://www.tuflow.com/forum//index.php?showtopic=1005&view=findpost&p=2997 TUFLOW forums].<br>
 
As a brief description of the below, the "do_while_loop_start" counts the number of processes in the task manager which have "TUFLOW" in the name. If this is greater than or equal to the variable "CPU_Cores" then the batch file waits for 60 seconds.
Line 273 ⟶ 368:
FOR %%b in (%B%) DO (
call :do_while_loop_start
Start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -b -e1 %%a -s1 %%b M11_5m_M01_5m_~e1~_~s1~_001.tcf
)
)
Line 304 ⟶ 399:
SET /A Counter+=1
IF /i !Counter!==!P! (
Start "TUFLOW" /wait "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -b -e1 %%a -s1 %%b M11_5m_M01_5m_~e1~_~s1~_001.tcf
SET /A Counter=0
) ELSE (
Start "TUFLOW" "C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe" -b -e1 %%a -s1 %%b M11_5m_M01_5m_~e1~_~s1~_001.tcf
)
)
Line 323 ⟶ 418:
setlocal enabledelayedexpansion
 
remREM ______________SET RUN VARIABLES_____________
set TUFLOWEXE="C:\TUFLOW\Releases\20162020-0310-AE\w64\TUFLOW_iSP_w64.exe"
set RUN=start "TUFLOW" /low "%TUFLOWEXE%" -b
set /a CPU_Cores=5
 
set tcf=M05_5m_M01_5m_~s1~_~e1~_~e2~_001.tcf
 
Set A=UDEF DEF
remREM set B in loop beow
Set C==01hr 03hr
 
 
remREM ______________SET LOOPS____________________
FOR %%a in (%A%) do (
 
remREM _____DEFINE RUN LOGIC______
IF "%%a"=="UDEF" (
set B=100yr
Line 346 ⟶ 441:
)
remREM ______________RUN LOOPS____________________
FOR %%b in (!B!) do (
FOR %%c in (%C%) do (
Line 358 ⟶ 453:
 
 
remREM ___________COUNT RUN INSTANCES______________
:do_while_loop_start
set /a count=0
Line 368 ⟶ 463:
 
endlocal
</pre>
 
===Copy model, checks and results in a looped batch file===
This batch file uses the copy model function to copy the model (inputs and check files), of both scenarios 5m and 2.5m, to a specified folder location. After copying, <u>[https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy robocopy]</u> is used to copy the results, based off the tcf filename and specified scenarios, to the destination result folder.
<pre>
@echo off
 
set TUFLOWEXE_iSP="C:\Releases\2023-03-AF\TUFLOW_iSP_w64.exe"
set RUN_iSP=start "TUFLOW" /wait %TUFLOWEXE_iSP% -cp "D:\tuflow_models\copy"
 
set A=5m 2.5m
set source_results=..\results
set destination_results=C:\tuflow_models\copy\results
 
 
FOR %%a in (%A%) do (
:: Copy model
%RUN_iSP% -s1 %%a M01_~s1~_001.tcf
:: Copy results folder to copy model location
robocopy "%source_results%" "%destination_results%" "M01_%%a_001*" /S
)
pause
</pre>
 
===Coordinating Asynchronous Simulations===
This batch file will set off asynchronous runs on separate threads, and then wait for all runs to finish before moving to the next process. This example kicks off 8 tuflow.exe processes and then waits before collating (in fortran) and processing the results (in python). Both “combine_therad_outputs.exe” and “Post_process.py” are dummy programs only (with dummy arguments), and are included just to demonstrate how any sort of post processing might be called after the synchronisation of all TUFLOW simulations.
 
<pre>
@echo off
 
:: Set local
setlocal
:: User defined variables – will need to be changed
set "for_infile=inputs_fortran.inp"
set "run_exe="C:\TUFLOW\Releases\2020-10-AE\TUFLOW_iSP_w64.exe""
set "file_prefix=C:\temp\test"
set "sum_exe=combine_thread_outputs.exe"
set "n_threads=8"
set "pyt_code=Post_process.py"
set "pyt_infile=inputs_python.inp"
 
:: Random lock file
set "lock=%temp%\wait%random%.lock"
 
:: Launch processes asynchronously, with stream 9 redirected to a lock file.
:: The lock file will remain locked until the individual exe instance ends.
:: %%a is sent to %run_exe% as an argument to set the thread to use
for /L %%a in (0,1,7) do (
start "" 9>"%lock%%%a" %run_exe% %for_infile% %%a
)
 
:: Wait for the processes to finish
:Waitrun
1>nul 2>nul ping /n %n_threads% ::1
for /L %%N in (0,1,7) do (
(call ) 9>"%lock%%%N" || goto :Waitrun
) 2>nul
 
:: Delete the lock files
del "%lock%*"
 
:: Collate thread outputs
%sum_exe% %file_prefix% %n_threads%
 
:: Make image
python %pyt_code% %pyt_infile%
 
:: Finish up
echo Done.
</pre>