{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8ba03726-6b87-48e7-9d5d-d0c6ca77a896",
   "metadata": {},
   "source": [
    "# \"Communication-free and Parallel Simulation of Neutral Biodiversity Models\": Interactive Demonstration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8c3f401d-7d54-46dc-906a-e712a4c9ddb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import demo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e25347fe-a014-4dc5-93fa-a706ada101e7",
   "metadata": {},
   "source": [
    "<h2><img src=\"https://mybinder.org/static/logo.svg\" style=\"display:inline;margin-bottom:3px;vertical-align:middle;height:30px;\">Cell #1: Independent Reverse-Time Simulation</h2>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "367bd13d-40c7-4c1e-8b72-24512400ac71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[36mINFO \u001b[0m Starting to load the dispersal map \"maps/demo/dispersal.tif\" ...\n",
      "\u001b[33mWARN \u001b[0m No GDAL no data value was found ...\n",
      "\u001b[36mINFO \u001b[0m Successfully loaded the dispersal map \"maps/demo/dispersal.tif\" with dimensions 6x6 [cols x rows].\n",
      "\u001b[36mINFO \u001b[0m Starting to load the habitat map \"maps/demo/habitat.tif\" ...\n",
      "\u001b[33mWARN \u001b[0m No GDAL no data value was found ...\n",
      "\u001b[36mINFO \u001b[0m Successfully loaded the habitat map \"maps/demo/habitat.tif\" with dimensions 6x1 [cols x rows].\n",
      "\n",
      "=========================== Simulation Configuration ===========================\n",
      "\n",
      "(\n",
      "    speciation: 0.75,\n",
      "    sample: Sample(\n",
      "        percentage: 1.0,\n",
      "        origin: Habitat,\n",
      "        mode: Genesis,\n",
      "    ),\n",
      "    pause: None,\n",
      "    rng: Seed(7206077791098137251),\n",
      "    scenario: SpatiallyExplicitUniformTurnover(\n",
      "        habitat: \"maps/demo/habitat.tif\",\n",
      "        dispersal: \"maps/demo/dispersal.tif\",\n",
      "        turnover: 0.5,\n",
      "        mode: OffByOne,\n",
      "    ),\n",
      "    algorithm: Independent(\n",
      "        delta_t: 1.0,\n",
      "        step_slice: 10,\n",
      "        dedup_cache: None,\n",
      "        parallelism_mode: Monolithic(\n",
      "            event_slice: Relative(\n",
      "                factor: 2.0,\n",
      "            ),\n",
      "        ),\n",
      "    ),\n",
      "    partitioning: Monolithic(),\n",
      "    log: None,\n",
      "    reporters: [\n",
      "        Plugin(\n",
      "            library: \"plugins/demo.so\",\n",
      "            reporters: [\n",
      "                Demo(\n",
      "                    ignore: [\n",
      "                        Location(\n",
      "                            x: 5,\n",
      "                            y: 0,\n",
      "                        ),\n",
      "                        Location(\n",
      "                            x: 1,\n",
      "                            y: 0,\n",
      "                        ),\n",
      "                        Location(\n",
      "                            x: 0,\n",
      "                            y: 0,\n",
      "                        ),\n",
      "                    ],\n",
      "                ),\n",
      "            ],\n",
      "        ),\n",
      "    ],\n",
      ")\n",
      "\n",
      "=========================== Simulation Configuration ===========================\n",
      "\n",
      "\u001b[36mINFO \u001b[0m The simulation will start fresh.\n",
      "\u001b[36mINFO \u001b[0m The scenario will be simulated as one monolithic partition.\n",
      "\u001b[36mINFO \u001b[0m The simulation will be run on one processing unit.\n",
      "\u001b[36mINFO \u001b[0m The simulation will report speciation events live.\n",
      "\u001b[36mINFO \u001b[0m The simulation will report dispersal events live.\n",
      "\u001b[36mINFO \u001b[0m Events will be reported using the live water-level algorithm ...\n",
      "================================================================================\n",
      "=                          Starting Event Report ...                           =\n",
      "================================================================================\n",
      " 0.35: <3> disperses from (3,0):0 to (2,0):0 ...\n",
      " 0.55: <4> disperses from (4,0):0 to (3,0):0 ...\n",
      " 1.39: <2> disperses from (2,0):0 to (3,0):0 ...\n",
      " 1.39: <3> disperses from (2,0):0 to (3,0):0 ...\n",
      " 2.08: <4> speciates              at (3,0):0 ...\n",
      " 2.08: <2> speciates              at (3,0):0 ...\n",
      " 2.08: <3> speciates              at (3,0):0 ...\n",
      "\n",
      "\n",
      "=============================== Reporter Summary ===============================\n",
      "\n",
      "\n",
      "=============================== Reporter Summary ===============================\n",
      "\n",
      "\u001b[36mINFO \u001b[0m The simulation finished at time 4.853330974676176 after 12 steps.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!rustcoalescence simulate '(\n",
    "    speciation: 0.75,\n",
    "    sample: Sample(percentage: 1.0),\n",
    "    rng: Seed(7206077791098137251),\n",
    "\n",
    "    scenario: SpatiallyExplicitUniformTurnover(\n",
    "        habitat: \"maps/demo/habitat.tif\",\n",
    "        dispersal: \"maps/demo/dispersal.tif\",\n",
    "    ),\n",
    "    \n",
    "    algorithm: Independent(\n",
    "        delta_t: 1.0,\n",
    "        dedup_cache: None,\n",
    "    ),\n",
    "    \n",
    "    reporters: [ Plugin(\n",
    "        library: \"plugins/demo.so\",\n",
    "        reporters: [ Demo(\n",
    "            ignore: [\n",
    "                Location(x: 0, y: 0),\n",
    "                Location(x: 1, y: 0),\n",
    "                Location(x: 5, y: 0),\n",
    "            ],\n",
    "        ) ],\n",
    "    ) ],\n",
    ")'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23d44b21-c652-4099-bd87-b9dcc31cd335",
   "metadata": {},
   "source": [
    "<h2><img src=\"https://mybinder.org/static/logo.svg\" style=\"display:inline;margin-bottom:3px;vertical-align:middle;height:30px;\">Cell #2: Only simulate individual &lt;2&gt;</h2>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5989a9e8-9a6d-48bc-8920-0c5044b9a5dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 1.39: <2> disperses from (2,0):0 to (3,0):0 ...\n",
      " 2.08: <2> speciates              at (3,0):0 ...\n"
     ]
    }
   ],
   "source": [
    "!rustcoalescence simulate '(\n",
    "    speciation: 0.75,\n",
    "    sample: Sample(percentage: 1.0),\n",
    "    rng: Seed(7206077791098137251),\n",
    "\n",
    "    scenario: SpatiallyExplicitUniformTurnover(\n",
    "        habitat: \"maps/demo/habitat.tif\",\n",
    "        dispersal: \"maps/demo/dispersal.tif\",\n",
    "    ),\n",
    "    \n",
    "    algorithm: Independent(\n",
    "        delta_t: 1.0,\n",
    "        dedup_cache: None,\n",
    "        \n",
    "        parallelism_mode: IsolatedLandscape(\n",
    "            partition: Partition(\n",
    "                rank: 2,\n",
    "                size: 6,\n",
    "            ),\n",
    "            event_slice: Absolute(capacity: 10),\n",
    "        )\n",
    "    ),\n",
    "    \n",
    "    reporters: [ Plugin(\n",
    "        library: \"plugins/demo.so\",\n",
    "        reporters: [ Demo() ],\n",
    "    ) ],\n",
    ")' | grep \"<[0-9]*>\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "378f80e2-791f-48a4-abb0-017136ad6f36",
   "metadata": {},
   "source": [
    "<h2><img src=\"https://mybinder.org/static/logo.svg\" style=\"display:inline;margin-bottom:3px;vertical-align:middle;height:30px;\">Cell #3: Only simulate individual &lt;3&gt;</h2>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "358e8b31-3f43-4244-a44a-abe3b7c806ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 0.35: <3> disperses from (3,0):0 to (2,0):0 ...\n",
      " 1.39: <3> disperses from (2,0):0 to (3,0):0 ...\n",
      " 2.08: <3> speciates              at (3,0):0 ...\n"
     ]
    }
   ],
   "source": [
    "!rustcoalescence simulate '(\n",
    "    speciation: 0.75,\n",
    "    sample: Sample(percentage: 1.0),\n",
    "    rng: Seed(7206077791098137251),\n",
    "\n",
    "    scenario: SpatiallyExplicitUniformTurnover(\n",
    "        habitat: \"maps/demo/habitat.tif\",\n",
    "        dispersal: \"maps/demo/dispersal.tif\",\n",
    "    ),\n",
    "    \n",
    "    algorithm: Independent(\n",
    "        delta_t: 1.0,\n",
    "        dedup_cache: None,\n",
    "        \n",
    "        parallelism_mode: IsolatedLandscape(\n",
    "            partition: Partition(\n",
    "                rank: 3,\n",
    "                size: 6,\n",
    "            ),\n",
    "            event_slice: Absolute(capacity: 10),\n",
    "        )\n",
    "    ),\n",
    "    \n",
    "    reporters: [ Plugin(\n",
    "        library: \"plugins/demo.so\",\n",
    "        reporters: [ Demo() ],\n",
    "    ) ],\n",
    ")' | grep \"<[0-9]*>\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d38d6ce8-65c1-4328-9810-a85416101e77",
   "metadata": {},
   "source": [
    "<h2><img src=\"https://mybinder.org/static/logo.svg\" style=\"display:inline;margin-bottom:3px;vertical-align:middle;height:30px;\">Cell #4: Only simulate individual &lt;4&gt;</h2>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7d81f402-be33-48d6-8be4-bec13bff7574",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 0.55: <4> disperses from (4,0):0 to (3,0):0 ...\n",
      " 2.08: <4> speciates              at (3,0):0 ...\n"
     ]
    }
   ],
   "source": [
    "!rustcoalescence simulate '(\n",
    "    speciation: 0.75,\n",
    "    sample: Sample(percentage: 1.0),\n",
    "    rng: Seed(7206077791098137251),\n",
    "\n",
    "    scenario: SpatiallyExplicitUniformTurnover(\n",
    "        habitat: \"maps/demo/habitat.tif\",\n",
    "        dispersal: \"maps/demo/dispersal.tif\",\n",
    "    ),\n",
    "    \n",
    "    algorithm: Independent(\n",
    "        delta_t: 1.0,\n",
    "        dedup_cache: None,\n",
    "        \n",
    "        parallelism_mode: IsolatedLandscape(\n",
    "            partition: Partition(\n",
    "                rank: 4,\n",
    "                size: 6,\n",
    "            ),\n",
    "            event_slice: Absolute(capacity: 10),\n",
    "        )\n",
    "    ),\n",
    "    \n",
    "    reporters: [ Plugin(\n",
    "        library: \"plugins/demo.so\",\n",
    "        reporters: [ Demo() ],\n",
    "    ) ],\n",
    ")' | grep \"<[0-9]*>\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "finished-shark",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}