{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "90d62ef7-a8f7-40a8-9006-9420802fabae",
   "metadata": {},
   "source": [
    "# Welcome\n",
    "\n",
    "Welcome to MobsPy tutorial notebooks. These notebooks will teach you the basics of using MobsPy, a language designed to simplifying the construction of chemical reaction networks. This tutorial will teach you how to define species, reactions, counts and rates. Afterwards, we will discuss the inner structure of MobsPy and how it simplifies modeling."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "77802f3c-7d80-4033-937e-3e42c0dbac46",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip install mobspy --upgrade\n",
    "#!pip install copasi-basico --upgrade\n",
    "\n",
    "\n",
    "from mobspy import *\n",
    "import matplotlib.pyplot as plt\n",
    "import PIL"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84e22aa9-c817-47b2-925d-d45dbcbe8f5d",
   "metadata": {},
   "source": [
    "# Meta-Reaction Syntax\n",
    "\n",
    "In MobsPy, meta-reactions are used to define reactions and are constructed using Python operators and Base Species objects. The + operator sums reactants and products, while the * operator defines stoichiometry. The >> operator indicates the transformation of reactants to products, and the [] operator assigns a rate to the reaction. Species are assigned counts or concentrations using the species variable and the call operator ():"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1d176d55-1a94-43e1-95cf-9d860470f474",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n",
      "Starting Simulator\n",
      "Simulation is Over\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 22952 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "A, B, C, D = BaseSpecies()\n",
    "\n",
    "# One molecule of A and one molecule of B becomes two molecules of C and one of D at a rate of 0.01\n",
    "A + B >> 2*C + D [0.01]\n",
    "\n",
    "# We start with 200 units of A and 100 units of B\n",
    "A(200), B(100)\n",
    "# Define the simulation using the given meta-species \n",
    "S = Simulation(A | B | C | D)\n",
    "S.duration = 5\n",
    "S.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5545d2e3-49eb-4379-8f20-8cce6d04479f",
   "metadata": {},
   "source": [
    "# How MobsPy works?\n",
    "\n",
    "MobsPy uses meta-species and meta-reactions to simplify models. Meta-species are simply set of species constructed with all possible states of a specie. Meta-reactions define multiple reactions by using sets or subsets of these meta-species. Moreover, MobsPy simplifies the definition of chemical reaction networks with a bottom-up approach. Base meta-species representing simple tasks are defined and then combined to form more complex meta-species. For example, to model the aging process, we can define a base meta-species, \"Ager\" and use the >> operator to represent the transformation from \"young\" to \"old\" with a given reaction constant rate.\n",
    "The variable \"u\" is used to assign units to the given reaction constant rate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4887dd2c-eeb7-4261-be2d-fc553d2a170b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mobspy.modules.meta_class.Reactions at 0x7fc51cba9280>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Ager = BaseSpecies()\n",
    "\n",
    "Ager.young >> Ager.old [1/10/u.year]\n",
    "#Note that the characteristics young and old are automatically added to Ager when used with the . operator"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9723fd9e-b974-4a8c-a728-756fb67fe1f2",
   "metadata": {},
   "source": [
    "To add characteristics to meta-species, we can use the dot operator.\n",
    "Characteristics can be added both inside and outside reactions. In the example above, the \"young\" and \"old\" characteristics are added to the \"Ager\" meta-species using the dot operator while defining the aging reaction.\n",
    "\n",
    "As an example of how to use the dot operator, we can create two new base species, \"Colored\" and \"Location,\" and assign characteristics to them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ebff93da-b7a8-4633-947a-f0bebfc4015d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<mobspy.modules.meta_class.Reacting_Species at 0x7fc51cba93d0>,\n",
       " <mobspy.modules.meta_class.Reacting_Species at 0x7fc51cba9370>)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Colored, Location = BaseSpecies(2)\n",
    "\n",
    "Colored.green, Colored.yellow, Colored.brown\n",
    "Location.dense, Location.sparse"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f17308f0-b1d6-4efb-bfd7-b648fe68bb62",
   "metadata": {},
   "source": [
    "With MobsPy, it's possible to create meta-species and assign reactions to them without assigning characteristics. \n",
    "In this example, we use the provided meta-species Zero to define the Mortal meta-species, which represents death. \n",
    "Zero represents the absence of reactants or products in a reaction. \n",
    "To model a death rate higher for beings in the old state, we pass a function as a rate that returns reaction constant rates based on the state of the meta-species."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9d7a0cab-1e8a-4599-9698-44298607668a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mobspy.modules.meta_class.Reactions at 0x7fc52347d550>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Mortal = BaseSpecies(1)\n",
    "\n",
    "# Functions can be rates. Reactants will be passed to the function which will return a higher rate if it contains the\n",
    "# charcteristic old\n",
    "Mortal >> Zero [lambda r1: 1/u.year if r1.old else 0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47f02dc0-e42c-4ec4-af21-5138aca0d7a2",
   "metadata": {},
   "source": [
    "We can combine the previously defined base meta-species to construct a more complex one with all desired characteristics and reactions by multiplying the previously defined meta-species. For example, we can create a \"Tree\" meta-species that has both the characteristics and reactions of Ager, Colored, and Location, and Mortal:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3edb192a-732a-44e4-b53a-e67fdc6447f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "Tree = Ager*Colored*Mortal*Location"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33a5ea7d-c2cd-46d2-b070-14dcdc4fd69c",
   "metadata": {},
   "source": [
    "The Tree meta-species is a model that incorporates the Mortal reaction for death and the Ager reaction for aging, as well as the Colored, Location and Ager characteristics. Therefore, the Tree meta-species can be in the following states:\n",
    "\n",
    "**Tree.young.brown.dense, Tree.young.brown.sparse, \\  \n",
    "Tree.young.green.dense, Tree.young.green.sparse, \\\n",
    "Tree.young.yellow.dense, Tree.young.yellow.sparse, \\\n",
    "Tree.old.brown.dense, Tree.old.brown.sparse, \\\n",
    "Tree.old.green.dense, Tree.old.green.sparse, \\\n",
    "Tree.old.yellow.dense, Tree.old.yellow.sparse.**\n",
    "\n",
    "MobsPy created these states through a vectorial representation. In this vector space, the species Ager, Location, and Colored represent the axes, and the characteristics define the coordinate markings on those axes. The characteristics are position according to the axes of the first meta-species they have been directly added in. \n",
    "So the young and old characteristic bellong to the Ager axis, the brown, green and yellow belong to the Colored axis, and finally the sparce and dense belong to the Location axis.\n",
    "The vector space of Tree can be seen below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "edab6319-29ea-4de2-861d-090c8a9b5439",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fc52352c070>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 34640 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(PIL.Image.open('images/vectorial_space.png'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dedaf18b-2854-46da-9ade-b5e9da4bef80",
   "metadata": {},
   "source": [
    "All states partake in the meta-reactions from the base meta-species used to construct Tree to define reactions. For instance, the Ager meta-reaction yields the following six reactions:\n",
    "\n",
    "**Tree.young.brown.dense -> Tree.old.brown.dense \\\n",
    "Tree.young.brown.sparse -> Tree.old.brown.sparse \\\n",
    "Tree.young.green.dense -> Tree.old.green.dense \\\n",
    "Tree.young.green.sparse -> Tree.old.green.sparse \\\n",
    "Tree.young.yellow.dense -> Tree.old.yellow.dense \\\n",
    "Tree.young.yellow.sparse -> Tree.old.yellow.sparse** \n",
    "\n",
    "When using the dot notation in the reactants, MobsPy performs a query and filters the states that do not contain the specified characteristics. When using the dot notation in the products, one specifies the coordinate on the vector space where the transformation should occur. Thus, the Ager reaction transforms young to old as those are the characteritics in the Ager vector axis."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bf67209-6b46-429b-90f1-6d00dc14a7b4",
   "metadata": {},
   "source": [
    "To continue building upon this model, we add two more reactions to the new Tree meta-species. A competition reaction between Trees in the dense location where the older Trees kill the young threes and a replication reaction where old Trees generate new Trees."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "81e1b6aa-2040-49da-af8e-1e3c8a34ac3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mobspy.modules.meta_class.Reactions at 0x7fc52353e460>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# competition\n",
    "Tree.dense.old + Tree.dense.young >> Tree.dense.old [1e-10*u.decimeter**2/u.year]\n",
    "# replication\n",
    "Tree.old >> Tree + Tree.young [0.1/u.year]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d8c1999-7893-4ffe-acdb-e9a44a54637b",
   "metadata": {},
   "source": [
    "In MobsPy, the order of reactants and products has meaning. The order of reactants and products is used to generate the reactions from meta-reactions. \n",
    "The state of the first meta-species in the reactants will be used to generate the first product of the same meta-species. \n",
    "Further, the second reactant becomes the second product unless there are more products than reactants. \n",
    "In that case, if the products run out of corresponding reactants, MobsPy will cycle through the reactants and pick the first one again. \n",
    "\n",
    "For exemple, we show a diagram bellow for the reaction 2 * Meta_Species_1 + Meta_Species_2 >> Meta_Species_2 + 3 * Meta_Species_1. Here the first and second Meta_Species_1 from the product are assigned the states of the first and second Meta_Species_1 from the reactants, then the third Meta_Species_1 from the product will be assigned the state of the first\n",
    "Meta_Species_1 from the reactant. Meta_Species_2 is not matched with Meta_Species one since they are different Meta_Species. The diagram follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d0670dac-bb3a-4676-98d3-63c9a2b619f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16388 characters]\n",
      "text/plain": [
       "<PIL.PngImagePlugin.PngImageFile image mode=P size=567x331>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PIL.Image.open('images/Matching_Meta.png')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afeeeab1-336f-4cfd-8244-3225f64ad22f",
   "metadata": {},
   "source": [
    "Consider the competition reaction Tree.dense.old + Tree.dense.young >> Tree.dense.old. Here, the order of the reactants indicates that the old Tree survives the competition and kills the young Tree, thus we preserve the state of the first Tree meta-species in the reaction. Thus, this meta-reaction yields the following reactions:\n",
    "\n",
    "**Tree.brown.dense.old + Tree.brown.dense.young -> Tree.brown.dense.old \\\n",
    "Tree.brown.dense.old + Tree.green.dense.young -> Tree.brown.dense.old \\\n",
    "Tree.brown.dense.old + Tree.yellow.dense.young -> Tree.brown.dense.old \\\n",
    "Tree.green.dense.old + Tree.brown.dense.young -> Tree.green.dense.old \\\n",
    "Tree.green.dense.old + Tree.green.dense.young -> Tree.green.dense.old \\\n",
    "Tree.green.dense.old + Tree.yellow.dense.young -> Tree.green.dense.old \\\n",
    "Tree.yellow.dense.old + Tree.brown.dense.young -> Tree.yellow.dense.old \\\n",
    "Tree.yellow.dense.old + Tree.green.dense.young -> Tree.yellow.dense.old \\\n",
    "Tree.yellow.dense.old + Tree.yellow.dense.young -> Tree.yellow.dense.old**\n",
    "\n",
    "On the other hand, in the replication reaction Tree.old >> Tree + Tree.young, the state of the first Tree is used to construct both products due to the round-robin cycle. As a result, the Trees of the products will retain the state of the first reactant. However, since a transformation is specified for the second Tree in the products, it will maintain its location and color, but its age will change to young, yielding the reactions:\n",
    "\n",
    "**Tree.brown.dense.old -> Tree.brown.dense.old + Tree.brown.dense.young \\\n",
    "Tree.brown.sparse.old -> Tree.brown.sparse.old + Tree.brown.sparse.young \\\n",
    "Tree.green.dense.old -> Tree.green.dense.old + Tree.green.dense.young \\\n",
    "Tree.green.sparse.old -> Tree.green.sparse.old + Tree.green.sparse.young \\\n",
    "Tree.yellow.sparse.old -> Tree.yellow.sparse.old + Tree.yellow.sparse.young \\\n",
    "Tree.yellow.dense.old -> Tree.yellow.dense.old + Tree.yellow.dense.young** \n",
    "\n",
    "Finally, although the order of reactants has meaning, the queries can be performed in any order."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63b56243-47a7-44d6-8ba9-ecafced995e2",
   "metadata": {},
   "source": [
    "It is necessary to specify the meta-species used in the simulation. \n",
    "As the Ager, Location, Mortal, and Color were defined only to construct the Tree meta-species, they must not be considered in the simulation. \n",
    "Furthermore, it is necessary to indicate to the simulator that this is a 2D-based model instead of the default 3D by using units of the area in the simulation volume.\n",
    "Once the simulation object has been defined, one can configure the simulation using the dot operator. Below, we define and simulate the model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "05e16ff0-cb05-470e-a53f-a9cf1e06900f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n",
      "Starting Simulator\n",
      "Simulation is Over\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 28124 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# initial conditions, in this model version we only have green trees\n",
    "Tree.dense(25), Tree.dense.old(25), Tree.sparse(25), Tree.sparse.old(25)\n",
    "S = Simulation(Tree)\n",
    "S.simulation_method = 'stochastic'\n",
    "S.repetitions = 3\n",
    "S.duration = 20*u.years\n",
    "S.unit_x = 'year'\n",
    "# Need\n",
    "S.volume = 1*u.m**2\n",
    "S.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "350e15ee-f0f5-47b1-8e21-1f1bc86c7018",
   "metadata": {},
   "source": [
    "The counts of resulting states are automatically mapped to the Three meta-species. MobsPy also provides in it's results the counts of the individual states. The results can be accessed in the following manner:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "efa57176-8d9f-4df9-87dd-a3f818fd0578",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.0000000000000002, 0.9800000000000001, 0.8900000000000001, 0.8200000000000001, 0.7800000000000001, 0.7300000000000001, 0.7100000000000001, 0.67, 0.6600000000000001, 0.6200000000000001, 0.6100000000000001, 0.5900000000000001, 0.5900000000000001, 0.5800000000000001, 0.5900000000000001, 0.54, 0.5000000000000001, 0.4700000000000001, 0.4600000000000001, 0.4600000000000001, 0.45000000000000007, 0.45000000000000007, 0.43000000000000005, 0.43000000000000005, 0.43000000000000005, 0.39000000000000007, 0.38000000000000006, 0.36000000000000004, 0.34, 0.34, 0.33000000000000007, 0.33000000000000007, 0.29000000000000004, 0.30000000000000004, 0.30000000000000004, 0.29000000000000004, 0.29000000000000004, 0.29000000000000004, 0.28, 0.29000000000000004, 0.26000000000000006, 0.26000000000000006, 0.26000000000000006, 0.26000000000000006, 0.25000000000000006, 0.25000000000000006, 0.24000000000000005, 0.24000000000000005, 0.24000000000000005, 0.24000000000000005, 0.24000000000000005, 0.24000000000000005, 0.23000000000000004, 0.22000000000000003, 0.21000000000000002, 0.20000000000000004, 0.20000000000000004, 0.20000000000000004, 0.20000000000000004, 0.20000000000000004, 0.20000000000000004, 0.20000000000000004, 0.20000000000000004, 0.20000000000000004, 0.20000000000000004, 0.20000000000000004, 0.20000000000000004, 0.20000000000000004, 0.20000000000000004, 0.19000000000000003, 0.18000000000000002, 0.18000000000000002, 0.18000000000000002, 0.17, 0.16000000000000003, 0.15000000000000002, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.13000000000000003, 0.13000000000000003, 0.13000000000000003, 0.12000000000000002, 0.12000000000000002, 0.12000000000000002, 0.12000000000000002, 0.12000000000000002, 0.12000000000000002, 0.12000000000000002, 0.12000000000000002, 0.12000000000000002], [1.0000000000000002, 0.9700000000000001, 0.8700000000000001, 0.8400000000000001, 0.7700000000000001, 0.7500000000000001, 0.7000000000000001, 0.6400000000000001, 0.5900000000000001, 0.55, 0.53, 0.5200000000000001, 0.5200000000000001, 0.5000000000000001, 0.5000000000000001, 0.49000000000000005, 0.4700000000000001, 0.4600000000000001, 0.44000000000000006, 0.44000000000000006, 0.43000000000000005, 0.42000000000000004, 0.42000000000000004, 0.41000000000000003, 0.39000000000000007, 0.39000000000000007, 0.39000000000000007, 0.38000000000000006, 0.37000000000000005, 0.37000000000000005, 0.37000000000000005, 0.37000000000000005, 0.37000000000000005, 0.37000000000000005, 0.36000000000000004, 0.36000000000000004, 0.36000000000000004, 0.34, 0.32000000000000006, 0.31000000000000005, 0.29000000000000004, 0.29000000000000004, 0.29000000000000004, 0.29000000000000004, 0.29000000000000004, 0.29000000000000004, 0.28, 0.28, 0.27, 0.26000000000000006, 0.25000000000000006, 0.24000000000000005, 0.24000000000000005, 0.24000000000000005, 0.24000000000000005, 0.24000000000000005, 0.24000000000000005, 0.23000000000000004, 0.23000000000000004, 0.23000000000000004, 0.23000000000000004, 0.22000000000000003, 0.22000000000000003, 0.22000000000000003, 0.21000000000000002, 0.21000000000000002, 0.21000000000000002, 0.21000000000000002, 0.21000000000000002, 0.21000000000000002, 0.21000000000000002, 0.20000000000000004, 0.19000000000000003, 0.19000000000000003, 0.19000000000000003, 0.18000000000000002, 0.18000000000000002, 0.18000000000000002, 0.18000000000000002, 0.17, 0.17, 0.15000000000000002, 0.15000000000000002, 0.14, 0.15000000000000002, 0.15000000000000002, 0.15000000000000002, 0.15000000000000002, 0.15000000000000002, 0.14, 0.14, 0.14, 0.13000000000000003, 0.12000000000000002, 0.12000000000000002, 0.12000000000000002, 0.11000000000000001, 0.10000000000000002, 0.10000000000000002, 0.09000000000000001, 0.09000000000000001], [1.0000000000000002, 0.9200000000000002, 0.8500000000000001, 0.8100000000000002, 0.7800000000000001, 0.7400000000000001, 0.6900000000000001, 0.6500000000000001, 0.5700000000000001, 0.56, 0.54, 0.5200000000000001, 0.5000000000000001, 0.4800000000000001, 0.4600000000000001, 0.4600000000000001, 0.44000000000000006, 0.43000000000000005, 0.41000000000000003, 0.41000000000000003, 0.4000000000000001, 0.39000000000000007, 0.38000000000000006, 0.37000000000000005, 0.37000000000000005, 0.37000000000000005, 0.35000000000000003, 0.35000000000000003, 0.35000000000000003, 0.34, 0.34, 0.32000000000000006, 0.31000000000000005, 0.31000000000000005, 0.31000000000000005, 0.29000000000000004, 0.29000000000000004, 0.29000000000000004, 0.27, 0.26000000000000006, 0.26000000000000006, 0.26000000000000006, 0.25000000000000006, 0.25000000000000006, 0.25000000000000006, 0.24000000000000005, 0.24000000000000005, 0.24000000000000005, 0.24000000000000005, 0.24000000000000005, 0.25000000000000006, 0.24000000000000005, 0.24000000000000005, 0.24000000000000005, 0.23000000000000004, 0.23000000000000004, 0.22000000000000003, 0.22000000000000003, 0.23000000000000004, 0.21000000000000002, 0.20000000000000004, 0.18000000000000002, 0.18000000000000002, 0.18000000000000002, 0.18000000000000002, 0.18000000000000002, 0.18000000000000002, 0.18000000000000002, 0.18000000000000002, 0.18000000000000002, 0.18000000000000002, 0.18000000000000002, 0.17, 0.17, 0.17, 0.18000000000000002, 0.18000000000000002, 0.17, 0.17, 0.17, 0.16000000000000003, 0.15000000000000002, 0.14, 0.14, 0.14, 0.12000000000000002, 0.12000000000000002, 0.12000000000000002, 0.12000000000000002, 0.11000000000000001, 0.11000000000000001, 0.10000000000000002, 0.08000000000000002, 0.08000000000000002, 0.08000000000000002, 0.08000000000000002, 0.07, 0.07, 0.06000000000000001, 0.06000000000000001, 0.06000000000000001]]\n"
     ]
    }
   ],
   "source": [
    "print(S.results[Tree])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2189332-b6bd-4b8b-b9c5-cbdb5cc5cb29",
   "metadata": {},
   "source": [
    "We also compile the model to show it in detail:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e1443de3-1436-490e-bcfe-5beba62e2778",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Species\n",
      "Tree.brown.dense.old,0\n",
      "Tree.brown.dense.young,0\n",
      "Tree.brown.sparse.old,0\n",
      "Tree.brown.sparse.young,0\n",
      "Tree.green.dense.old,25\n",
      "Tree.green.dense.young,25\n",
      "Tree.green.sparse.old,25\n",
      "Tree.green.sparse.young,25\n",
      "Tree.yellow.dense.old,0\n",
      "Tree.yellow.dense.young,0\n",
      "Tree.yellow.sparse.old,0\n",
      "Tree.yellow.sparse.young,0\n",
      "\n",
      "Mappings\n",
      "Tree :\n",
      "Tree.brown.dense.old\n",
      "Tree.brown.dense.young\n",
      "Tree.brown.sparse.old\n",
      "Tree.brown.sparse.young\n",
      "Tree.green.dense.old\n",
      "Tree.green.dense.young\n",
      "Tree.green.sparse.old\n",
      "Tree.green.sparse.young\n",
      "Tree.yellow.dense.old\n",
      "Tree.yellow.dense.young\n",
      "Tree.yellow.sparse.old\n",
      "Tree.yellow.sparse.young\n",
      "\n",
      "Parameters\n",
      "volume,99.99999999999999\n",
      "\n",
      "Reactions\n",
      "reaction_0,{'re': [(1, 'Tree.brown.dense.old'), (1, 'Tree.brown.dense.young')], 'pr': [(1, 'Tree.brown.dense.old')], 'kin': 'Tree.brown.dense.old * Tree.brown.dense.young * 3.1688087814028948e-18 * volume^-1'}\n",
      "reaction_1,{'re': [(1, 'Tree.brown.dense.old'), (1, 'Tree.green.dense.young')], 'pr': [(1, 'Tree.brown.dense.old')], 'kin': 'Tree.brown.dense.old * Tree.green.dense.young * 3.1688087814028948e-18 * volume^-1'}\n",
      "reaction_2,{'re': [(1, 'Tree.brown.dense.old'), (1, 'Tree.yellow.dense.young')], 'pr': [(1, 'Tree.brown.dense.old')], 'kin': 'Tree.brown.dense.old * Tree.yellow.dense.young * 3.1688087814028948e-18 * volume^-1'}\n",
      "reaction_3,{'re': [(1, 'Tree.brown.dense.old')], 'pr': [(1, 'Tree.brown.dense.old'), (1, 'Tree.brown.dense.young')], 'kin': 'Tree.brown.dense.old * 3.168808781402895e-09'}\n",
      "reaction_4,{'re': [(1, 'Tree.brown.dense.old')], 'pr': [], 'kin': 'Tree.brown.dense.old * 3.168808781402895e-08'}\n",
      "reaction_5,{'re': [(1, 'Tree.brown.dense.young')], 'pr': [(1, 'Tree.brown.dense.old')], 'kin': 'Tree.brown.dense.young * 3.168808781402895e-09'}\n",
      "reaction_6,{'re': [(1, 'Tree.brown.sparse.old')], 'pr': [(1, 'Tree.brown.sparse.old'), (1, 'Tree.brown.sparse.young')], 'kin': 'Tree.brown.sparse.old * 3.168808781402895e-09'}\n",
      "reaction_7,{'re': [(1, 'Tree.brown.sparse.old')], 'pr': [], 'kin': 'Tree.brown.sparse.old * 3.168808781402895e-08'}\n",
      "reaction_8,{'re': [(1, 'Tree.brown.sparse.young')], 'pr': [(1, 'Tree.brown.sparse.old')], 'kin': 'Tree.brown.sparse.young * 3.168808781402895e-09'}\n",
      "reaction_9,{'re': [(1, 'Tree.green.dense.old'), (1, 'Tree.brown.dense.young')], 'pr': [(1, 'Tree.green.dense.old')], 'kin': 'Tree.green.dense.old * Tree.brown.dense.young * 3.1688087814028948e-18 * volume^-1'}\n",
      "reaction_10,{'re': [(1, 'Tree.green.dense.old'), (1, 'Tree.green.dense.young')], 'pr': [(1, 'Tree.green.dense.old')], 'kin': 'Tree.green.dense.old * Tree.green.dense.young * 3.1688087814028948e-18 * volume^-1'}\n",
      "reaction_11,{'re': [(1, 'Tree.green.dense.old'), (1, 'Tree.yellow.dense.young')], 'pr': [(1, 'Tree.green.dense.old')], 'kin': 'Tree.green.dense.old * Tree.yellow.dense.young * 3.1688087814028948e-18 * volume^-1'}\n",
      "reaction_12,{'re': [(1, 'Tree.green.dense.old')], 'pr': [(1, 'Tree.green.dense.old'), (1, 'Tree.green.dense.young')], 'kin': 'Tree.green.dense.old * 3.168808781402895e-09'}\n",
      "reaction_13,{'re': [(1, 'Tree.green.dense.old')], 'pr': [], 'kin': 'Tree.green.dense.old * 3.168808781402895e-08'}\n",
      "reaction_14,{'re': [(1, 'Tree.green.dense.young')], 'pr': [(1, 'Tree.green.dense.old')], 'kin': 'Tree.green.dense.young * 3.168808781402895e-09'}\n",
      "reaction_15,{'re': [(1, 'Tree.green.sparse.old')], 'pr': [(1, 'Tree.green.sparse.old'), (1, 'Tree.green.sparse.young')], 'kin': 'Tree.green.sparse.old * 3.168808781402895e-09'}\n",
      "reaction_16,{'re': [(1, 'Tree.green.sparse.old')], 'pr': [], 'kin': 'Tree.green.sparse.old * 3.168808781402895e-08'}\n",
      "reaction_17,{'re': [(1, 'Tree.green.sparse.young')], 'pr': [(1, 'Tree.green.sparse.old')], 'kin': 'Tree.green.sparse.young * 3.168808781402895e-09'}\n",
      "reaction_18,{'re': [(1, 'Tree.yellow.dense.old'), (1, 'Tree.brown.dense.young')], 'pr': [(1, 'Tree.yellow.dense.old')], 'kin': 'Tree.yellow.dense.old * Tree.brown.dense.young * 3.1688087814028948e-18 * volume^-1'}\n",
      "reaction_19,{'re': [(1, 'Tree.yellow.dense.old'), (1, 'Tree.green.dense.young')], 'pr': [(1, 'Tree.yellow.dense.old')], 'kin': 'Tree.yellow.dense.old * Tree.green.dense.young * 3.1688087814028948e-18 * volume^-1'}\n",
      "reaction_20,{'re': [(1, 'Tree.yellow.dense.old'), (1, 'Tree.yellow.dense.young')], 'pr': [(1, 'Tree.yellow.dense.old')], 'kin': 'Tree.yellow.dense.old * Tree.yellow.dense.young * 3.1688087814028948e-18 * volume^-1'}\n",
      "reaction_21,{'re': [(1, 'Tree.yellow.dense.old')], 'pr': [(1, 'Tree.yellow.dense.old'), (1, 'Tree.yellow.dense.young')], 'kin': 'Tree.yellow.dense.old * 3.168808781402895e-09'}\n",
      "reaction_22,{'re': [(1, 'Tree.yellow.dense.old')], 'pr': [], 'kin': 'Tree.yellow.dense.old * 3.168808781402895e-08'}\n",
      "reaction_23,{'re': [(1, 'Tree.yellow.dense.young')], 'pr': [(1, 'Tree.yellow.dense.old')], 'kin': 'Tree.yellow.dense.young * 3.168808781402895e-09'}\n",
      "reaction_24,{'re': [(1, 'Tree.yellow.sparse.old')], 'pr': [(1, 'Tree.yellow.sparse.old'), (1, 'Tree.yellow.sparse.young')], 'kin': 'Tree.yellow.sparse.old * 3.168808781402895e-09'}\n",
      "reaction_25,{'re': [(1, 'Tree.yellow.sparse.old')], 'pr': [], 'kin': 'Tree.yellow.sparse.old * 3.168808781402895e-08'}\n",
      "reaction_26,{'re': [(1, 'Tree.yellow.sparse.young')], 'pr': [(1, 'Tree.yellow.sparse.old')], 'kin': 'Tree.yellow.sparse.young * 3.168808781402895e-09'}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(S.compile())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c309a72-a6d0-4eab-8d31-fc968ac592ac",
   "metadata": {},
   "source": [
    "One can see how MobsPy was able to reduce a 27 reaction model into three meta-reactions. \n",
    "Finally, one can acess the results inside the simulation object "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50709f10-ed45-470d-8053-568f2be27b38",
   "metadata": {},
   "source": [
    "# Following Notebooks\n",
    "\n",
    "In the following tutorials, we will revise in more detail some of the tools used here and explain other resources that can be further used to simplify models. "
   ]
  }
 ],
 "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
}
