Skip to content

Running MIKE SHE simulations from Python scripts

This section describes the steps to execute a MIKE SHE model using a Python script. Functions are included in the MShePy module to initiate and run a MIKE SHE simulation from a Python script.

The MIKE SHE model has to be preprocessed before it can run using the MShePy module. The MIKE SHE model can contain Python plugins.

1. Initializing a MIKE SHE model

The MShePy module resides in the MIKE installation´s bin/x64 folder. This needs to be in your search path for python modules, e.g. by calling sys.path.append([MIKE installation]/bin/x64). Import the MShePy module by using the command import MShePy.

In order to initialize the MIKE SHE model, use the MShePy.wm.initialize(r”*.she”) function. The function argument is the path to the MIKE SHE model file. While a MIKE SHE model is initialized from a Python script, the model can be opened, but not run in MIKE ZERO.

2. Running MIKE SHE water movement

Three functions are available in the MShePy module to run a timestep in the MIKE SHE model:

  • MShePy.wm.runAll() which performs the entire simulation. When using this method you must not initialize or terminate the simulation, differently from all other functions;
  • MShePy.wm.performTimeStep() which performs one timestep
  • MShePy.wm.runToTime(time) which performs timesteps until reaching the simulation time specified as an argument (a datetime.datetime object). Be aware that source and sinks assigned from the Python script (including parameters such as a fixed hydraulic head which are converted to source and sinks in the MIKE SHE engine) will remain active for all time steps taken during the execution of this method and be reset only right before the method returns.

All MShePy functions can be called to retrieve and assign variable values in MIKE SHE during the simulation.

3. Termination and postprocessing

The function MShePy.wm.terminate() can be used to terminate the simulation. The user should provide as function input False if an error occurred during the simulation or True if no error occurred.

Postprocessing and the visualization of the results can be done in MIKE ZERO and MIKE VIEW. Results can also be exported using Python and handled in an external software.

4. Python script example

A Python script describing the steps of initializing, running and terminate a MIKE SHE model is shown below.

import datetime #Import daytime module

import MShePy #Import MShePy module MShePy.wm.initialize(r"C:\\Users\\PETR.she")  \#Initialize the model currentTime=MShePy.wm.currentTime()  \#Find the initial timestep

EndTime=currentTime+datetime.timedelta(days=5) #Calculate the end timestep after 5 days

MShePy.wm.runToTime(EndTime) #Run the model until the end timestep

MShePy.wm.terminate(True) #Terminate the simulation