{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "31e5bf14-0da2-4664-b8b9-75d874309d5f",
   "metadata": {},
   "source": [
    "# Concatenated simulations\n",
    "\n",
    "MobsPy allows for combining simulations. In combined simulations, the results from the precedent simulations is fed to the following ones. Thus, allowing the user to make changes to the models for subsequent simulations. \n",
    "\n",
    "Here we suply an example where in the first simulation species A grows according to a rate. Afterwards, A stops growing and it must compete with a 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",
      "Running simulation in parallel\n",
      "Simulation is Over\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 15116 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",
    "\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",
    "\n",
    "S = S1 + S2\n",
    "S.method = 'stochastic'\n",
    "S.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c1c2da2-54c1-4933-8beb-b1973dc433ca",
   "metadata": {},
   "source": [
    "An important point to note is that species counts must always be defined before the creation of the simulation object. For the subsequent simulations, if a count was defined before the simulation object creation, it will override the resulting value from the previous sim. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ab943574-45f0-4d06-89a6-860fcf880c19",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n",
      "Compiling model\n",
      "Starting Simulator\n",
      "Running simulation in parallel\n",
      "Simulation is Over\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 14816 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "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",
    "\n",
    "# This overrides the value of B from S1\n",
    "B(0)\n",
    "S2 = Simulation (A | B)\n",
    "S2.duration = 2\n",
    "\n",
    "S = S1 + S2\n",
    "S.method = 'stochastic'\n",
    "S.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "054821a5-79a5-41c0-ab18-9b3caa5b4a18",
   "metadata": {},
   "source": [
    "For events in concatenated simulations, the event time is based-around 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": 3,
   "id": "54df2d01-3489-4572-a2ff-90b5ea82afcc",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n",
      "Compiling model\n",
      "Starting Simulator\n",
      "Running simulation in parallel\n",
      "Simulation is Over\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 13344 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "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",
    "\n",
    "# This overrides the value of B from S1\n",
    "B(0)\n",
    "S2 = Simulation (A | B)\n",
    "S2.duration = 2\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(1):\n",
    "    A(100)\n",
    "\n",
    "S = S1 + S2\n",
    "S.method = 'stochastic'\n",
    "S.run()"
   ]
  }
 ],
 "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
}
