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 mikeplus.Database object
result_files = db.run()

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 is the MUID 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
    results = db.run(simulation_muid="FloodEvent_2023_Sim")
  • Simulation Option: The keyword-only sim_option parameter selects the engine: "CS_MIKE_1D" (rivers, collection systems and overland flow), "CS_SWMM", "WD_EPANET" (water distribution), or "CS_MIKE_1D_JobList" (generates an LTS job list). If omitted, it is derived from db.active_model; that only works for the CS_MIKE1D, CS_SWMM and WD_EPANET models, and raises ValueError otherwise.

    # Runs using the EPANET engine for water distribution
    results = db.run(sim_option="WD_EPANET")

You can combine these parameters:

results = db.run(
    simulation_muid="PeakDemand_Scenario",
    sim_option="WD_EPANET"
)

Simulation Output

When the engine finishes, db.run() returns a list of pathlib.Path objects: the result files the simulation setup is configured to write. They are not checked for existence, so a path may be missing if the simulation failed.

result_paths = db.run()
for path in result_paths:
    print(f"Generated result file: {path}")

Error Handling

db.run() raises RuntimeError if the engine fails to start, exits with an error, or doesn’t write its result files; the message names the engine’s logs. It raises ValueError if sim_option is invalid or cannot be derived from the active model, or if the model has no simulation with the given MUID.

try:
    result_files = db.run()
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 mikeplus 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:
        results = db.run()
    except RuntimeError as e:
        print(f"Simulation failed: {e}")
    else:
        missing = [path for path in results if not path.exists()]
        if missing:
            print(f"Simulation did not produce: {missing}")
        else:
            print(f"Result files: {results}")