{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Simulation with events\n",
    "\n",
    "In the [previous](./starting_your_first_simulation.ipynb) notebook we showed you how to start a 3Di simulation. In this notebook\n",
    "we'll continue with the simulation by adding several events:\n",
    "- inital waterlevels\n",
    "- rain\n",
    "- breaches\n",
    "- laterals\n",
    "\n",
    "Let's quickly build a simulation again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "from getpass import getpass\n",
    "from threedi_api_client.openapi.api.v3_api import V3Api\n",
    "from threedi_api_client.api import ThreediApi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Provide authentication details\n",
    "API_HOST = \"https://api.3di.live\"\n",
    "PERSONAL_API_KEY = getpass(\"Personal API token\")  # https://management.3di.live/personal_api_keys\n",
    "\n",
    "config = {\n",
    "    \"THREEDI_API_HOST\": API_HOST,\n",
    "    \"THREEDI_API_PERSONAL_API_TOKEN\": PERSONAL_API_KEY\n",
    "}\n",
    "\n",
    "api_client: V3Api = ThreediApi(config=config)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Specify the threedi-model and organisation\n",
    "organisation_uuid = \"b08433fa47c1401eb9cbd4156034c679\"\n",
    "threedimodel = api_client.threedimodels_list(name__icontains='v2_bergermeer').results[0]\n",
    "\n",
    "# Retrieve first simulation template\n",
    "simulation_templates = api_client.simulation_templates_list(simulation__threedimodel__id=threedimodel.id)\n",
    "assert simulation_templates.count > 0, f\"No simulation templates found for threedimodel {threedimodel.name}\"\n",
    "simulation_template_id = simulation_templates.results[0].id\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "simulation = api_client.simulations_from_template(\n",
    "    data={\n",
    "        \"template\": simulation_template_id,\n",
    "        \"name\": \"my simulation with events\",\n",
    "        \"organisation\": organisation_uuid,\n",
    "        \"start_datetime\": datetime.now(),\n",
    "        \"duration\": 3600  # in seconds, so we simulate for 1 hour\n",
    "    }\n",
    ")\n",
    "\n",
    "print(simulation)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Now that we have created a simulation again, we can start adding some interesting events to\n",
    "the simulation before starting it."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Rain\n",
    "\n",
    "Let's make a time varying rain: every 15 minutes it will decrease in intensity."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "rain = api_client.simulations_events_rain_timeseries_create(\n",
    "    simulation_pk=simulation.id, data={\n",
    "        'offset':0,\n",
    "        'values': [[0, 0.005], [900, 0.002], [1800, 0.001], [2700, 0.0005], [3600, 0]],\n",
    "        'units': 'm/s'}\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Breaches\n",
    "\n",
    "Breaches are dependent on the selected model. So let's first see if the model has\n",
    "any breaches configured and if so use the first one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "potential_breaches = api_client.threedimodels_potentialbreaches_list(threedimodel.id)\n",
    "print(potential_breaches)\n",
    "\n",
    "# Let's pick the first potential breach\n",
    "breach = potential_breaches.results[0]\n",
    "\n",
    "breach_event = api_client.simulations_events_breaches_create(\n",
    "    simulation.id, data={\n",
    "        \"potential_breach\": breach.id,\n",
    "        \"duration_till_max_depth\": 1800,\n",
    "        \"initial_width\": 0.5,\n",
    "        \"offset\": 1800\n",
    "    }\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Laterals\n",
    "\n",
    "We'll add a lateral by specifying a connection_node on which we want the lateral to apply."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "lateral = api_client.simulations_events_lateral_constant_create(\n",
    "    simulation.id, data={\n",
    "        \"offset\": 900,\n",
    "        \"connection_node\": 4,\n",
    "        \"value\": 0.001,\n",
    "        \"duration\": 300,\n",
    "        \"units\": \"m3/s\"\n",
    "    }\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Events overview\n",
    "\n",
    "To get an overview of all our created events on this simulation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "events = api_client.simulations_events(simulation.id)\n",
    "print(events)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Now let's start the simulation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "api_client.simulations_actions_create(simulation.id, data={\"name\": \"start\"})"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "And eventually the simulation will finish!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "api_client.simulations_status_list(simulation.id)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
