Run TUFLOW From PowerShell: Difference between revisions

Content deleted Content added
 
(24 intermediate revisions by 3 users not shown)
Line 7:
 
=Using PowerShell=
Open Windows PowerShell from the Windows start menu. When running a script via the PowerShell command line, the current working directory should typically be set to the same directory as the location of the script and TCF file. To change the current working directory, use the “cd” command. If there are spaces in your folder path, use single quotations around the entry (' '):
<pre>
cd '<Path-to-Tuflow-Runs-Folder>'
</pre>
 
Assuming the PowerShell script filename is runrun_TUFLOW_simulations.ps1, this can be run from the PowerShell command line with this command:
<pre>
.\runrun_TUFLOW_simulations.ps1
</pre>
 
{{Video|name=TUFLOW Run_Edited.mp4|width=850}}<br>
 
A Windows batch file can also be used to execute a PowerShell script, avoiding the need to work in the PowerShell command line.
* Create a text file with a *.bat file extension, saved in your TUFLOW\runs folder (alongside your TCF)
* Add the following syntax to the batch file (if your ps1 file uses a different name, specify it instead of run_TUFLOW_simulations.ps1):
<pre>
@powershell -File .\run_TUFLOW_simulations.ps1
</pre>
* Double left mouse click the batch file from Window Explorer to execute the batch file that calls the PowerShell script
 
=Simple Example=
Line 30 ⟶ 40:
 
=Multiple Simulations=
Running multiple simulations in series can be handled by adding new lines to run.ps1. Use a –b (batch) switch to suppressessuppress the need to press the return key at the end of each simulation:
<pre>
C:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe -b model_001.tcf
Line 37 ⟶ 47:
 
=Adding Switches=
To alter the behaviour of TUFLOW, switches can be used in the same way as a batch file (see <u>[[Run_TUFLOW_From_a_Batch-file#TUFLOW_switches_in_a_batch_file | Batch File Switches]]</u>). It is recommended that switches be contained with single quotations (' '). For example, running with the test switch -t:
<pre>
C:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe '-b' '-t's1 model_0015m -s2 D01 model_~s1~_~s2~_001.tcf
</pre>
 
When specifying a tcf file that includes scenarios and/or events within the filename use single quotations around the tcf file reference (' '). The specific scenarios and event references do not require quotations.
<pre>
C:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe '-b' '-x' '-s1' 5m '-s2' D01 'model_~s1~_~s2~_001.tcf'
</pre>
 
This example demonstrates how to package a copy of a project milestone model (including all possible scenario and event inputs) for archiving:
<pre>
C:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe '-b' '-pmAll' '-xf0' '-s1' 5m '-s2' D01 '-e1' Q100 '-e2' 2hr 'model_~s1~_~s2~_~e1~_~e2~_001.tcf'
</pre>
 
=Defining Variables=
In PowerShell, variables are used to store values and are identified by a dollar sign ($) followed by a name. For example:
<pre>
$EXE = 'C:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe'
$EXE '-b' '-x' '-s1' 5m '-s2' D01 model_~s1~_~s2~_001.tcf
</pre>
 
=Looping=
Looping is undertaken in a similar way to batch files. The following PowerShell script contains a nested loop, so thatwhere TUFLOW runs a simulation for all combinations of scenarioscenarios and eventevents.
Comments can be added using #.
 
<pre>
#TUFLOW RUN VARIABLES
$EXE = C:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe
$scenarios = @("1m", "2m", "5m")
$events = @("1hr", "2hr", "5hr")
Line 72 ⟶ 64:
foreach ($scenario in $scenarios){
foreach ($event in $events){
$EXEC:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe '-b' '-s1' $scenario '-e1' $event 'model_~s1~_~e1~.tcf'
}
}
Line 87 ⟶ 79:
$Mail.Send()
</pre>
It is also possible to send the contents of a log file (for example, the simulation TLF or _ TUFLOW Simulations.log) as the body of the email. To do this, you must pre-empt the name and path of the TLF file that will be produced:
<pre>
$Mail.Body = Get-Content -Raw .\log\model_001.tlf
$Mail.Send()
</pre>
 
= Advanced Example=
<pre>
#TUFLOW RUN VARIABLES
$EXE = C:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe
$scenarios = @("1m", "2m", "5m")
$events = @("1hr", "2hr", "5hr")
 
#FINAL PROJECT SIMULATION EXECUTION LOOP
foreach ($scenario in $scenarios){
foreach ($event in $events){
$EXE '-b' '-s1' $scenario '-e1' $event 'model_~s1~_~e1~_001.tcf'
}
}
 
#CREATE COPY OF PROJECT MILESTONE MODEL INPUTS - ARCHIVE
$EXE '-b' '-pmAll' '-xf0' '-s1' $scenario '-e1' $event 'model_~s1~_~e1~_001.tcf'
 
#EMAIL NOTIFICATION
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.Subject = "Project 11053 - TUFLOW Simulations Finished"
$Mail.To = "john.smith@gmail.com"
$Mail.To = "amanda.miller@gmail.com"
$Mail.Send()
$Mail.Body = Get-Content -Raw '.\_ TUFLOW Simulations.log'
$Mail.Send()
exit
</pre>
 
</pre>