Run TUFLOW From PowerShell: Difference between revisions
Content deleted Content added
RobertWright (talk | contribs) |
Chris Huxley (talk | contribs) |
||
| (66 intermediate revisions by 3 users not shown) | |||
Line 6:
A PowerShell script can be created in a similar way to a batch file. For example, right click in File Explorer, then click New > Text Document, and then provide a file name and extension (.ps1 for PowerShell). Alternatively, when using the [[Create_TUFLOW_Project|Create TUFLOW Project Tool in QGIS]], a PowerShell script is automatically generated in addition to a batch file.
=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 (' '):▼
A simple PowerShell script is as follows:▼
<pre>
cd '<Path-to-Tuflow-Runs-Folder>'▼
C:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe model_001.tcf▼
</pre>
Assuming the PowerShell script filename is
<pre>
.\
</pre>
{{Video|name=TUFLOW Run_Edited.mp4|width=850}}<br>
▲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:
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
▲cd <Path-to-Tuflow-Runs-Folder>
</pre>
* Double left mouse click the batch file from Window Explorer to execute the batch file that calls the PowerShell script
=Simple Example=
=Multiple simulations=▼
Running multiple simulations in series can be handled by adding new lines to run.ps1:▼
▲A simple PowerShell script to run a single simulation is as follows:
<pre>
C:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe model_001.tcf
C:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe model_002.tcf▼
</pre>
If there are spaces in your executable path, use single quotations around the executable entry (' ') and prefix the reference with &. For example:
=Adding switches=▼
To alter the behaviour of TUFLOW, switches can be used in the same way as a batch file. For example, running with the test switch -t:▼
<pre>
& 'C:\bin\TUFLOW Example\2025.0.3\TUFLOW_iSP_w64.exe
</pre>
▲Running multiple simulations in series can be handled by adding new lines to run.ps1. Use a –b (batch) switch to suppress 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
▲C:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe -b model_002.tcf
</pre>
▲To alter the behaviour of TUFLOW, switches can be used in the same way as a batch file
<pre>
C:\bin\TUFLOW\2025.0.3\TUFLOW_iSP_w64.exe -b -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,
Comments can be added using #.
<pre>
#TUFLOW RUN VARIABLES
$scenarios = @("1m", "2m", "5m")
$events = @("1hr", "2hr", "5hr")
#SIMULATION EXECUTION LOOP
foreach ($scenario in $scenarios){
foreach ($event in $events){
C:
}
}
</pre>
=Email Notifications=
An email can be sent using PowerShell to inform the user that a TUFLOW run has completed (successfully or otherwise). The following approach requires that Outlook is already set up on the computer running TUFLOW:
<pre>
Line 54 ⟶ 75:
$Mail = $Outlook.CreateItem(0)
$Mail.Subject = "TUFLOW Simulation Finished"
$Mail.To = "
$Mail.To = "second_recipient_email_address_here"
$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
| |||