Simulations
MIKE+Py allows you to execute simulations defined within your MIKE+ model directly from Python. This is primarily done using the run()
method of an opened Database
object.
Running Simulations
The most straightforward way to run a simulation is by calling db.run()
on your database object. By default, this executes the currently active simulation setup defined in the MIKE+ project.
# db is an opened mikepluspy.Database object
= db.run() result_files
The run()
method will pause your Python script (it’s a blocking call) until the MIKE+ simulation engine has completed its execution.
Specifying Simulation and Model
You can control which simulation to execute and which model engine to use:
Simulation Setup: The
simulation_muid
parameter allows you to specify the ID or name of the simulation setup to run (e.g., “BaseRun”, “Scenario_A_Sim”). If omitted, the active simulation in the model is used.# Runs a specific simulation setup = db.run(simulation_muid="FloodEvent_2023_Sim") results
Model Engine: The
model_option
parameter specifies the engine (e.g., “CS_MIKE1D” for Collection Systems, “WD_EPANET” for Water Distribution). If omitted, the database’s active model type is used.# Runs using the EPANET engine for water distribution = db.run(model_option="WD_EPANET") results
You can combine these parameters:
= db.run(
results ="PeakDemand_Scenario",
simulation_muid="WD_EPANET"
model_option )
Simulation Output
Upon successful completion, the db.run()
method returns a list of pathlib.Path
objects. Each Path
object points to a result file generated by the simulation.
= db.run()
result_paths for path in result_paths:
print(f"Generated result file: {path}")
Error Handling
If the simulation fails to start or encounters an error during execution, MIKE+Py will typically raise a RuntimeError
. It’s good practice to wrap simulation calls in a try-except block.
try:
= db.run()
result_files except RuntimeError as e:
print(f"Simulation failed: {e}")
Example
Here’s a complete example of opening a database and running the active simulation:
import mikepluspy as mp
with mp.open("path/to/your_model.sqlite") as db:
print(f"Active simulation: {db.active_simulation}")
print(f"Active model: {db.active_model}")
try:
= db.run()
results print(f"Simulation successful. Result files: {results}")
except RuntimeError as e:
print(f"An error occurred: {e}")