Skip to content

MIKE 1D Scripts

MIKE 1D supports scripting. A MIKE 1D script is a C# file with the extension .cs that is compiled and executed as part of a MIKE 1D simulation.

This contains documentation on how to enable a script, how to use script parameters and how to debug a script.

For example scripts, visit MIKE 1D Script Examples on GitHub

For more information on what a script can do, visit the MIKE 1D API

Enable a script

To enable a script there are different options:

MIKE+ GUI

From within the MIKE+ GUI go to the "Simulation" tab, select "MIKE 1D engine configuration", select the "Custom options" tab, and insert the following option:

Option name Value type Value
m1dscript Text [path-of]myM1DScript.cs

If the Value only contains the name of the script, the script must reside in the same folder as the MIKE+ setup (.mupp) file. It may also contain a relative or absolute path, where the relative path is relative to the MIKE+ setup (.mupp) file.

It is possible to provide parameters to the script through these "Custom options". If the script reads a CSV file and a weir coefficient, you can define the following custom options:

Option name Value type Value
csvFile Text .csv
weirCoeff Double 3.2

Then these options can be read from the script with the following code:

[Script]
public void Initialize(Mike1DData mike1DData, IDiagnostics diagnostics)
{
  // The csvFile custom option - having value of null if not specified for this simulation
  string csvFileName = mike1DData.AdditionalData.TryGetValue<string>("csvFile", null);
  // The weir coefficient, using 2.0 as "default" value (if not specified for this simulation)
  double weirCoeff = mike1DData.AdditionalData.TryGetValue<double>("weirCoeff", 2.0);
}

If the script or parameter is only relevant for some simulations, use the "Apply only to following Simulation IDs" for that custom option.

Automatic

Put the script file beside the setup file and give it the same name, keeping the .cs extension, i.e. if you have a mySetup.mupp or mySetup.mhydro, name the script file mySetup.cs. Similar for setups from other editors, e.g. MIKE URBAN classic mySetup.mdb, MIKE 11 mySetup.sim11, and also exported MIKE 1D data mySetup.m1dx.

For a MIKE+ / MIKE URBAN setup, if the script has the same name as the MIKE+ project .mupp file or MIKE URBAN database .mdb file, it will be executed as part of any export step, i.e for all simulations. To execute for a single simulation only, and to enable it in the run step only, export the simulation to .m1dx and give the script the same name as the exported .m1dx file.

Command line

Enable through command line using the -script parameter:

"c:\Program Files (x86)\DHI\2020\bin\x64\DHI.Mike1D.Application.exe" MySetup.m1dx -script=AdditionalOutput.cs -gui -close

If the .cs file is not in the same folder as the setup file (.m1dx) file, a relative or full path must be provided

"c:\Program Files (x86)\DHI\2020\bin\x64\DHI.Mike1D.Application.exe" MySetup.m1dx -script=..\myscripts\AdditionalOutput.cs -gui -close

Checking

It is possible to check in the simulation log file whether a script has been loaded. When a script is being loaded, the logfile will contain a line on the form:

2019-05-20 15:00:00: Loading script file mySetupScript.cs ...

The line is also written to the log file when exporting a MIKE URBAN setup to MIKE 1D (.m1dx file).

Define a script

A script method needs the [Script] attribute. The name of the method is not important, however the method must have either a a Mike1DData argument or a a IMike1DController argument as the first argument.

Modify a setup

To modify a setup after it has been loaded, add a Mike1DData parameter

[Script]
public void SetupLoaded(Mike1DData mike1DData)

Interact with engine during runtime

To interact with the engine during runtime, add a IMike1DController parameter

[Script]
public void ControllerCreated(IMike1DController controller)

Multiple scripts in same file

There can be multiple script methods in the same file. All methods with the [Script] attribute will be executed. If the order of the script methods is important, it is possible to order them like [Script(Order = 1)].

User defined script parameters

It is possible to define additional parameters to a script, on the form:

[Script]
public void ModifySetup(Mike1DData mike1DData, IDiagnostics diagnostics, bool addWaterDepth = false, double alpha = 1.0)
{
   ...
}

A diagnostics parameter can be added, and used for adding lines to the setup log file. All other parameters are considered user defined. The values of the user defined parameters can be modified from the command line by use of the -scriptpars: argument:

set m1d="c:\Program Files (x86)\DHI\2019\bin\x64\DHI.Mike1D.Application.exe"
%m1d% MySetup.m1dx -script=ScriptParameters.cs -scriptpars:addWaterDepth=true;alpha=1.5; -gui -close

The script method ModifySetup defines all the user defined parameters with a default value. In case the -scriptpars: is not used, or one of the parameters is not added to the -scriptpars:, the default value is used.

An example script with user defined script parameters can be found in ScriptParameters.cs

Debug a script

It is possible to debug scripts. The -scriptdebug argument to the MIKE 1D application will build the script with debug information.

Start Visual Studio or a similar IDE. Open the script file in Visual Studio. Add the -scriptdebug argument and also the -wait to the command line, which will pause the simulation at the start and pop op a messagebox. That makes it possible in Visual Studio to attach the debugger to the DHI.Mike1D.Application.exe process. When the debugger is attached, add some breakpoints in the script, and press "OK" in the messagebox to continue, and your breakpoints should be hit. Example of command line for attaching the Visual Studio debugger:

set m1d="c:\Program Files (x86)\DHI\2019\bin\x64\DHI.Mike1D.Application.exe"
%m1d% MySetup.m1dx -script=ScriptParameters.cs -scriptdebug -wait -gui -close