{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Starting your first 3Di simulation\n",
    "\n",
    "In this notebook we will go through the steps of connecting to the threedi-api and\n",
    "start our first simple 3Di simulation.\n",
    "\n",
    "Let's first import all required modules for the notebook:"
   ]
  },
  {
   "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 import ApiException\n",
    "from threedi_api_client.api import ThreediApi\n",
    "from threedi_api_client.openapi.api.v3_api import V3Api"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Then we'll provide our credentials to connect to the threedi-api"
   ]
  },
  {
   "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)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Check if we can connect to the threedi-api with the provided credentials:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    user = api_client.auth_profile_list()\n",
    "except ApiException as e:\n",
    "    print(\"Oops, something went wrong. Maybe you made a typo?\")\n",
    "else:\n",
    "    print(f\"Successfully logged in as {user.username}!\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "To run a simulation we'll need a threedi-model. Let's see which threedi-models are\n",
    "available:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "models = api_client.threedimodels_list(limit=5)  # limit to the first 5 results\n",
    "for model in models.results:\n",
    "    print(f\"{model.name}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "If we already know the name of a specific model, we can also look it up with the following api call:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "my_model = api_client.threedimodels_list(name__icontains='v2_bergermeer')\n",
    "print(my_model)\n",
    "\n",
    "my_model = my_model.results[0]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Now that we have a model we are almost ready to create the simulation. But first we'll need \n",
    "to get an organisation under which's name we will run the simulation.\n",
    "\n",
    "Let's see which organisations are available:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "organisations = api_client.organisations_list()\n",
    "for organisation in organisations.results:\n",
    "    print(f\"{organisation.name}: {organisation.unique_id}\")\n",
    "\n",
    "# Here I use the following organisation, you should select from the list below\n",
    "organisation_uuid = \"b08433fa47c1401eb9cbd4156034c679\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using a simulation template (with filled in settings and initials) makes it easier to start a new simulation. Let's find\n",
    "a simulation template for the 3Di model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simulation_templates = api_client.simulation_templates_list(simulation__threedimodel__id=my_model.id)\n",
    "\n",
    "for simulation_template in simulation_templates.results:\n",
    "    print(simulation_template.id, simulation_template.name)\n",
    "\n",
    "assert simulation_templates.count > 0, f\"No simulation templates found for threedimodel {my_model.name}\"\n",
    "\n",
    "simulation_template_id = simulation_templates.results[0].id"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Now we got all the pieces to create a simulation based on a simulation template"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "my_simulation = api_client.simulations_from_template(\n",
    "    data={\n",
    "        \"template\": simulation_template_id,\n",
    "        \"name\": \"my first simulation\",\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(f\"Created simulation with id: {my_simulation.id}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "The simulation has now been created but is not yet running. We can see the status of\n",
    "the simulation with the following call:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "status = api_client.simulations_status_list(my_simulation.id)\n",
    "print(status)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "To run this simulation we have to tell it to start:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "api_client.simulations_actions_create(my_simulation.id, data={\"name\": \"start\"})"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Eventually the simulation will finish. You can periodically check its progress by calling\n",
    "for the status again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "api_client.simulations_status_list(my_simulation.id)\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Eventually you should see something like this::\n",
    "\n",
    "    {\n",
    "        'created': datetime.datetime(2020, 7, 27, 14, 7, 6, 654905, tzinfo=tzutc()),\n",
    "        'id': 15866,\n",
    "        'name': 'finished',\n",
    "        'paused': False,\n",
    "        'time': 3600.0\n",
    "     }\n",
    "\n",
    "Congratulations, you just made your first simulation via the threedi-api-client!"
   ]
  }
 ],
 "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.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
