{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "31e5bf14-0da2-4664-b8b9-75d874309d5f",
   "metadata": {},
   "source": [
    "# Concatenated Simulations\n",
    "\n",
    "MobsPy allows for combining simulations to execute them sequentially. In combined simulations, the results from the precedent simulations are used for the following ones. It is done by passing the amount of the meta-species of the previous simulation into equally named meta-species in the next simulation.  \n",
    "\n",
    "From the user's perspective, to create a combined simulation, one must define several simulational objects, each with its model, and sum the simulational objects. \n",
    "\n",
    "Here, we supply an example where, in the first simulation, species A grows according to a rate. Afterward, A stops growing, and it must compete with species B."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0ce38184-bd62-4d7f-b618-43974c5bff06",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n",
      "Compiling model\n",
      "Starting Simulator\n",
      "Simulation is Over\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 13136 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from mobspy import *\n",
    "\n",
    "A = BaseSpecies()\n",
    "\n",
    "A >> 2*A [1]\n",
    "\n",
    "A(1)\n",
    "S1 = Simulation(A)\n",
    "S1.duration = 5\n",
    "S1.method = 'stochastic'\n",
    "\n",
    "A.reset_reactions()\n",
    "B = BaseSpecies()\n",
    "\n",
    "A + B >> Zero [0.01]\n",
    "\n",
    "B(50)\n",
    "S2 = Simulation (A | B)\n",
    "S2.duration = (A <= 0) | (B <= 0)\n",
    "S2.method = 'stochastic'\n",
    "\n",
    "S = S1 + S2\n",
    "S.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "054821a5-79a5-41c0-ab18-9b3caa5b4a18",
   "metadata": {},
   "source": [
    "For events in concatenated simulations, the event time is based on each simulation individually and not the global time. Each new simulation starts counting from zero again when considering the event trigger time. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "54df2d01-3489-4572-a2ff-90b5ea82afcc",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n",
      "Compiling model\n",
      "Starting Simulator\n",
      "Simulation is Over\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 11916 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from mobspy import *\n",
    "\n",
    "A, B = BaseSpecies()\n",
    "\n",
    "A >> 2*A [1]\n",
    "B >> 2*B [1]\n",
    "\n",
    "A(1), B(1)\n",
    "S1 = Simulation(A | B)\n",
    "S1.duration = 4\n",
    "S1.method = 'stochastic'\n",
    "\n",
    "# This overrides the value of B from S1\n",
    "B(0)\n",
    "S2 = Simulation (A | B)\n",
    "S2.duration = 2\n",
    "S2.method = 'stochastic'\n",
    "\n",
    "# The event will trigger at time 5 when S2 time is equal to 1\n",
    "with S2.event_time(1):\n",
    "    A(0)\n",
    "\n",
    "# The event will not trigger as the duration of S2 is equal to 2\n",
    "with S2.event_time(2):\n",
    "    A(100)\n",
    "\n",
    "S = S1 + S2\n",
    "S.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07fd6add-e3a3-4ea2-9f3d-9760b0d47e20",
   "metadata": {},
   "source": [
    "## Concatenated Simulation Configuration\n",
    "\n",
    "\n",
    "Setting up parameters is different when dealing with concatenated simulations. Some parameters can be set with the concatenated simulation objects, while others must be set individually or broadcast to the simulation objects.\n",
    "\n",
    "The 'duration' parameter must be set individually for each simulation through its simulation object or with a list of durations for each simulation in the concatenated object. \n",
    "Both 'volume' and 'method' can be set individually or with a list in the concatenated objects for each simulation to construct hybrid simulations. However,  when set with an individual value (int, str, unit) on the concatenated simulation, the concatenated simulation will broadcast this parameter through all simulations, using the same for all. \n",
    "Finally, 'level', 'rate_type', 'plot_type', and 'repetitions' are broadcasted through all simulation objects when set through the concatenated object. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7eb3b360-95e8-429d-bb2d-5bdf02acc1fc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
