# Simulations

MIKE+Py allows you to execute simulations defined within your MIKE+ model directly from Python. This is primarily done using the [run()](../reference/Database.md#mikeplus.Database.run) method of an opened [Database](../reference/Database.md#mikeplus.Database) object.


# <img src="data:image/svg+xml;base64,PHN2ZyBzdHlsZT0iaGVpZ2h0OjFlbTt3aWR0aDoxZW07dmVydGljYWwtYWxpZ246LTAuMTI1ZW07Zm9udC1zaXplOmluaGVyaXQ7b3ZlcmZsb3c6dmlzaWJsZTtwb3NpdGlvbjpyZWxhdGl2ZTsiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld2JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9ImN1cnJlbnRDb2xvciIgc3Ryb2tlLSBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGNsYXNzPSJnZC1pY29uIiBhcmlhLWhpZGRlbj0idHJ1ZSI+PHBhdGggZD0iTTUgNWEyIDIgMCAwIDEgMy4wMDgtMS43MjhsMTEuOTk3IDYuOTk4YTIgMiAwIDAgMSAuMDAzIDMuNDU4bC0xMiA3QTIgMiAwIDAgMSA1IDE5eiIgLz48L3N2Zz4=" class="gd-icon" /> 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.

``` python
# db is an opened mikeplus.Database object
result_files = db.run()
```

The [run()](../reference/Database.md#mikeplus.Database.run) method will pause your Python script (it's a blocking call) until the MIKE+ simulation engine has completed its execution.


# <img src="data:image/svg+xml;base64,PHN2ZyBzdHlsZT0iaGVpZ2h0OjFlbTt3aWR0aDoxZW07dmVydGljYWwtYWxpZ246LTAuMTI1ZW07Zm9udC1zaXplOmluaGVyaXQ7b3ZlcmZsb3c6dmlzaWJsZTtwb3NpdGlvbjpyZWxhdGl2ZTsiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld2JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9ImN1cnJlbnRDb2xvciIgc3Ryb2tlLSBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGNsYXNzPSJnZC1pY29uIiBhcmlhLWhpZGRlbj0idHJ1ZSI+PHBhdGggZD0iTTEwIDVIMyIgLz48cGF0aCBkPSJNMTIgMTlIMyIgLz48cGF0aCBkPSJNMTQgM3Y0IiAvPjxwYXRoIGQ9Ik0xNiAxN3Y0IiAvPjxwYXRoIGQ9Ik0yMSAxMmgtOSIgLz48cGF0aCBkPSJNMjEgMTloLTUiIC8+PHBhdGggZD0iTTIxIDVoLTciIC8+PHBhdGggZD0iTTggMTB2NCIgLz48cGF0aCBkPSJNOCAxMkgzIiAvPjwvc3ZnPg==" class="gd-icon" /> 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.

  ``` python
  # 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.

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

You can combine these parameters:

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


# <img src="data:image/svg+xml;base64,PHN2ZyBzdHlsZT0iaGVpZ2h0OjFlbTt3aWR0aDoxZW07dmVydGljYWwtYWxpZ246LTAuMTI1ZW07Zm9udC1zaXplOmluaGVyaXQ7b3ZlcmZsb3c6dmlzaWJsZTtwb3NpdGlvbjpyZWxhdGl2ZTsiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld2JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9ImN1cnJlbnRDb2xvciIgc3Ryb2tlLSBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGNsYXNzPSJnZC1pY29uIiBhcmlhLWhpZGRlbj0idHJ1ZSI+PHBhdGggZD0iTTYgMjJhMiAyIDAgMCAxLTItMlY0YTIgMiAwIDAgMSAyLTJoOGEyLjQgMi40IDAgMCAxIDEuNzA0LjcwNmwzLjU4OCAzLjU4OEEyLjQgMi40IDAgMCAxIDIwIDh2MTJhMiAyIDAgMCAxLTIgMnoiIC8+PHBhdGggZD0iTTE0IDJ2NWExIDEgMCAwIDAgMSAxaDUiIC8+PC9zdmc+" class="gd-icon" /> 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.

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


# <img src="data:image/svg+xml;base64,PHN2ZyBzdHlsZT0iaGVpZ2h0OjFlbTt3aWR0aDoxZW07dmVydGljYWwtYWxpZ246LTAuMTI1ZW07Zm9udC1zaXplOmluaGVyaXQ7b3ZlcmZsb3c6dmlzaWJsZTtwb3NpdGlvbjpyZWxhdGl2ZTsiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld2JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9ImN1cnJlbnRDb2xvciIgc3Ryb2tlLSBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGNsYXNzPSJnZC1pY29uIiBhcmlhLWhpZGRlbj0idHJ1ZSI+PHBhdGggZD0ibTIxLjczIDE4LTgtMTRhMiAyIDAgMCAwLTMuNDggMGwtOCAxNEEyIDIgMCAwIDAgNCAyMWgxNmEyIDIgMCAwIDAgMS43My0zIiAvPjxwYXRoIGQ9Ik0xMiA5djQiIC8+PHBhdGggZD0iTTEyIDE3aC4wMSIgLz48L3N2Zz4=" class="gd-icon" /> 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.

``` python
try:
    result_files = db.run()
except RuntimeError as e:
    print(f"Simulation failed: {e}")
```


# <img src="data:image/svg+xml;base64,PHN2ZyBzdHlsZT0iaGVpZ2h0OjFlbTt3aWR0aDoxZW07dmVydGljYWwtYWxpZ246LTAuMTI1ZW07Zm9udC1zaXplOmluaGVyaXQ7b3ZlcmZsb3c6dmlzaWJsZTtwb3NpdGlvbjpyZWxhdGl2ZTsiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld2JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9ImN1cnJlbnRDb2xvciIgc3Ryb2tlLSBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGNsYXNzPSJnZC1pY29uIiBhcmlhLWhpZGRlbj0idHJ1ZSI+PHBhdGggZD0iTTYgMjJhMiAyIDAgMCAxLTItMlY0YTIgMiAwIDAgMSAyLTJoOGEyLjQgMi40IDAgMCAxIDEuNzA0LjcwNmwzLjU4OCAzLjU4OEEyLjQgMi40IDAgMCAxIDIwIDh2MTJhMiAyIDAgMCAxLTIgMnoiIC8+PHBhdGggZD0iTTE0IDJ2NWExIDEgMCAwIDAgMSAxaDUiIC8+PHBhdGggZD0iTTEwIDEyLjUgOCAxNWwyIDIuNSIgLz48cGF0aCBkPSJtMTQgMTIuNSAyIDIuNS0yIDIuNSIgLz48L3N2Zz4=" class="gd-icon" /> Example

Here's a complete example of opening a database and running the active simulation:

``` python
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}")
```
