{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "*This notebook contains course material from [CBE30338](https://jckantor.github.io/CBE30338)\n", "by Jeffrey Kantor (jeff at nd.edu); the content is available [on Github](https://github.com/jckantor/CBE30338.git).\n", "The text is released under the [CC-BY-NC-ND-4.0 license](https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode),\n", "and code is released under the [MIT license](https://opensource.org/licenses/MIT).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [Student Projects](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/B.00-Student-Projects.ipynb) | [Contents](toc.ipynb) | [Visual Tracking of an Object with a Drone](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/B.02-Visual-Tracking-of-an-Object-with-a-Drone.ipynb) >
"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "iCQqStv99W-Z"
},
"source": [
"# Diabetes: Controlling Blood Glucose Concentrations\n",
"\n",
"Kevin Hansen, James Bathon, Ramon Villafana"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "iCQqStv99W-Z"
},
"source": [
"## Problem Statement"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "iCQqStv99W-Z"
},
"source": [
"### Introduction and Problem\n",
"Diabetes is a condition that affects a person’s ability to regulate their blood glucose level. There are several forms of diabetes such as Type 1, Type 2 and Gestational diabetes. In a healthy person, the pancreas is the main organ that works to control blood glucose levels - glucose being the body’s main source of energy. When blood sugar decreases, the pancreas beta cells are signaled to release glucagon. Glucagon is a signaling hormone that stimulates the liver to release glucose stores, subsequently increasing blood glucose levels. When blood glucose is high or increasing, the pancreas alpha cells release insulin. Insulin is an endocrine signaling hormone that stimulates and allows somatic tissues and cells to take up blood glucose for energy usage or storage, which subsequently decreases blood glucose levels. \n",
"\n",
"Type 1 diabetes, or insulin-dependent diabetes, is a chronic condition in which the pancreas produces little to no insulin. As a result, blood sugar concentrations can rise to dangerous levels. This can be caused by various factors such as genetics, an autoimmune condition, or disease. Type 1 diabetes is treated through insulin injections and by managing blood glucose levels through diet and exercise to prevent complications. With insulin injections, there is the risk of overdosing, which could lead to over-uptake of blood glucose. This can cause low blood glucose levels and is referred to as hypoglycemia. Symptoms of hypoglycemia include chills, confusion, shakiness, unconsciousness and possibly death. If insulin is not administered, blood glucose levels could become dangerously high, a condition which is referred to as hyperglycemia. Recurrent hyperglycemia can cause neuropathy (leading to amputation), retinopathy (leading to blindness), blood vessel damage and kidney damage. With Type 1 diabetes, individuals check their blood glucose with a hand-held meter or a body attachment monitor. Based on the reading from the monitor, the individual would determine how much insulin is needed. Typically, insulin injections occur two to four times a day, but diet and lifestyle (such as exercise) can cause this to change, as both of them impact blood glucose levels. Such management can be a lot of work and a hassle for the individual. One method to improve the life of those with insulin dependence would be to use an automated insulin pump that could measure blood glucose levels and administer insulin automatically.\n",
"\n",
"Consequently, it would be advantageous to model blood glucose levels in these diabetic individuals and provide insulin administration methods that maintain blood glucose levels within healthy ranges. Doing so would help lead to improved quality of life and overall health for diabetic patients. "
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "iCQqStv99W-Z"
},
"source": [
"### Technology Challenge\n",
"\n",
"Conventionally, for Type 1 diabetes, blood glucose levels are monitored by personal devices that test the glucose levels from drops of the patient’s blood. More recent technologies have resulted in continuous glucose monitoring systems, which consist of sensors placed under the skin that monitor blood glucose levels once every 1-15 minutes. Based on the readings from either of these monitoring systems, insulin dosages would need to be administered in order to achieve acceptable blood glucose levels. \n",
"\n",
"The challenge associated with this is determining acceptable insulin dosage frequencies and amounts to ensure that blood glucose levels do not rise or drop into unhealthy ranges. This is especially difficult when considering the effects of meals and exercise, as these activities can cause significant changes in blood glucose concentrations when compared to normal behavior. In severe cases, diabetic patients have to actively keep track of exercise habits and glucose content of meals in order to ensure that their blood glucose levels are within the healthy range.\n",
"\n",
"To improve quality of life, it would be ideal to pair continuous glucose monitoring devices to an automated insulin pump, which would be attached to diabetic patients to recommend how much insulin to administer to the patient and when to administer the insulin. Currently, these insulin pumps are designed to administer insulin at a basal rate with increased dosages before or after meals, which are designated as bolus doses. The goal is to determine adequate basal rates and bolus dose systems for administering insulin while taking into account fluctuations in the patients’ glucose intake and physical activities."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "iCQqStv99W-Z"
},
"source": [
"### Goals and Objectives:\n",
"\n",
"1) To create a simulation for blood glucose and insulin concentrations for a patient with Type 1 diabetes while taking into account disturbances in glucose concentration due to meals and exercise.\n",
"\n",
"2) To model high concentration injections and basal dosage administration of insulin and observe their effects on blood glucose and insulin concentrations under normal conditions and in the presence of disturbances such as meals and exercise.\n",
"\n",
"3) Use PID-control to regulate insulin administration in response to blood glucose levels in order to maintain a setpoint blood glucose concentration.\n",
"\n",
"4) Determine individual treatments and combinations of treatments that provide acceptable blood glucose control in the presence of meal and exercise disturbances."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "pEo1J3rXY6uN"
},
"source": [
"## Theoretical Development"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "pEo1J3rXY6uN"
},
"source": [
"### The Bergman Model for Type I Diabetes\n",
"\n",
"A popular model for describing blood glucose control in diabetic patients is known as the Bergman Minimal Model, a system of three ordinary differential equations. The primary variables involved include $G$, the deviation of the blood glucose concentration from basal levels measured in $mg\\space dL^{-1}$, $I$, the deviation of the blood insulin concentration from basal levels measured in $mU\\space L^{-1}$, and $X$, a proportionality variable that describes the insulin concentration in a remote compartment measured in $mU\\space L^{-1}$. (The unit $U$ represents units of Insulin, which are roughly equivalent to $0.01 mL$) Written as a system of equations, the Bergman Model can be represented as:\n",
"\n",
"$$ \\frac{dG}{dt} = -P_1G - X(G+G_b) + D $$\n",
"$$ \\frac{dI}{dt} = -n(I+I_b) + \\frac{U}{V_1} $$\n",
"$$ \\frac{dX}{dt} = -P_2X + P_3I $$\n",
"\n",
"The parameters present in these equations represent rates and conversions for the different processes that this model simulates. $G_b$ represents the basal blood glucose level, $I_b$ represents the basal blood insulin level, $V_1$ represents the effective tissue and blood volume in the body, $P_1$ describes glucose removal from bloodstream independent of insulin, $P_2$ describes the insulin removal rate from the remote compartment, $P_3$ describes the insulin appearance rate in the remote compartment, and $n$ describes the insulin removal rate from the bloodstream. Some typical values and units for each of these parameters are included in the table below.\n",
"\n",
"|Parameter|Typical Magnitude |Units |\n",
"|---------|---------------|---------|\n",
"| $G_b$ | $81$ | $mg \\space dL^{-1}$ |\n",
"| $I_b$ | $15$ | $mU L^{-1}$ |\n",
"| $n$ | $0.0926$ | $min^{-1}$ | \n",
"| $V_1$ | $12$ | $L$ |\n",
"| $P_1$ | $\\leq 0.035$ | $min^{-1}$ |\n",
"| $P_2$ | $0.025$ | $min^{-1}$ |\n",
"| $P_3$ | $5.3*10^{-5}$ | $min^{-1}$ |\n",
"\n",
"\n",
"Two important variables present in the model are $D$ and $U$, which are time-dependent variables. $D$ is a disturbance variable representing the intake of glucose from external sources, typically food, and is measured in $mg\\space dL^{-1} min^{-1}$. For the purpose of the model, $D$ is represented by the equation:\n",
"\n",
"$$ D(t) = \\frac{F_g}{V_g}e^{-0.05t} $$\n",
"\n",
"where $V_g$ is the glucose distribution volume which is based on an individual's weight and size and $F_g$ is the rate of glucose infusion dependent on the glucose contained within a meal and an individual's weight and size.\n",
"\n",
"$U$ is the insulin input from an external source and into the bloodstream, measured in $mU\\space min^{-1}$. For the purposes of this study, a PID controller was utilized to provide a control mechanism to maintain $G$ at a given setpoint $G_{sp}$. The governing equation for a PID controller is:\n",
"\n",
"$$ U = \\bar{U}_{ff} + K_p(G-G_{sp}) + K_d\\frac{dG}{dt} + K_e\\int_0^\\infty(G-G_{sp})dt $$\n",
"\n",
"In this controller setup, $\\bar{U}_{ff}$ is a feedforward controller output determined by a steady-state behavior in which the glucose concentration is equal to the setpoint. This feedforward term would serve as the basal rate of insulin administration. $K_p$ is the proportional controller gain which influences how insulin output from the controller changes based on glucose deviation from the setpoint. For this system, the desired output when $G>G_{sp}$ is an increase in insulin from the controller. $K_d$ is the derivative controller gain which influences how insulin output from the controller changes based on the rate at which the glucose concentration changes with time. For this system, the desired output is to decrease insulin output when $\\frac{dG}{dt}$ is negative and large in magnitude and increase insulin output when $\\frac{dG}{dt}$ is positive and large in magnitude. $K_e$ is the integral controller gain which influences how insulin output from the controller changes based on the cumulative deviation of the glucose concentration from the setpoint. For this system, the desired output is to eliminate small deviations from the setpoint using this term.\n",
"\n",
"Another input mechanism analyzed was a high concentration insulin injection, which was modeled similarly to the glucose disturbance function. Current injections utilize high-concentration, fast-acting insulin and are administered a short period of time prior to meals. Our interpretation of the insulin absorbance rate is:\n",
"\n",
"$$ U = \\bar{U}_{inject}e^{-10t}$$\n",
"\n",
"In this equation, the insulin decay rate is $10$ instead of $0.05$ (the glucose decay rate after meals) because the insulin is directly injected into the bloodstream. Normally, injections would instead be modeled by a delta function, but limitations on the absorption of insulin into the blood and tissues throughout the body was predicted to cause injections to be better represented by a sharp exponential function. $\\bar{U}_{inject}$ is an estimated parameter that determines the sharpness of the injection peak, which was estimated to scale with the glucose intake from corresponding meals. Values for this parameter are displayed in the following table and were approximated based on typical dosages for high-concentration injections.\n",
"\n",
"| Meal|Typical Magnitude |Units |\n",
"|---------|---------------|---------|\n",
"| Breakfast | $5000$ | $mU min^{-1}$ |\n",
"| Lunch | $7500$ | $mU min^{-1}$ |\n",
"| Dinner | $10000$ | $mU min^{-1}$ |"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "gjuZob6n8Zeq"
},
"source": [
"### Exercise Model\n",
"A variation of the Bergman Minimal Model includes variables typically used to describe actions in the respiratory system and uses them to estimate blood glucose concentrations as a result of physical activity. The primary variables involved include all the same variables and parameters from the Bergman Minimal Model alongside $G_{prod}$, glucose release from the liver caused by exercise and measured in $mg\\space kg^{-1}\\space min^{-1}$, $G_{up}$, the glucose absorption rate caused by exercise in $mg\\space kg^{-1}\\space min^{-1}$, $G_{gly}$, the decrease in glycogen levels due to the liver converting glycogen reserves to glucose in $mg\\space kg^{-1}\\space min^{-1}$, $I_e$, the insulin removal rate due to exercise measured in in $mU\\space L^{-1}\\space min^{-1}$, $PVO_2^{max}$, the percentage of the maximum oxygen consumption rate in $mL\\space kg^{-1}\\space min^{-1}$, and $A$, the integral of exercise intensity. Expressed in full, the model is:\n",
"\n",
"$$ \\frac{dG}{dt} = -P_1G - X(G+G_b) + D + \\frac{W}{V_G}(G_{prod}-G_{gly}-G_{up})$$\n",
"$$ \\frac{dI}{dt} = -n(I+I_b) + \\frac{U}{V_1} - I_e$$\n",
"$$ \\frac{dX}{dt} = -P_2X + P_3I $$\n",
"$$ \\frac{dG_{prod}}{dt} = a_1PVO_2^{max}-a_2G_{prod} $$\n",
"$$ \\frac{dG_{up}}{dt} = a_3PVO_2^{max}-a_4G_{up} $$\n",
"$$ \\frac{dI_e}{dt} = a_5PVO_2^{max}-a_6I_e $$\n",
"$$ \\frac{dPVO_2^{max}}{dt} = 0.8(u_{ex}-PVO_2^{max}) $$\n",
"$$ \\frac{dG_{gly}}{dt} = \\left\\{\n",
" \\begin{array}{ll}\n",
" 0 & \\quad A < A_{TH} \\\\\n",
" k & \\quad A \\geq A_{TH} \\\\\n",
" -\\frac{G_{gly}}{T_1} & \\quad u_{ex} = 0\n",
" \\end{array}\n",
" \\right. $$\n",
"$$ \\frac{dA}{dt} = \\left\\{\n",
" \\begin{array}{ll}\n",
" u_{ex} & \\quad u_{ex} > 0 \\\\\n",
" -A/0.001 & \\quad u_{ex} = 0\n",
" \\end{array}\n",
" \\right. $$\n",
"\n",
"The parameters $P_1$, $P_2$, $P_3$, $G_b$, $I_b$, $n$, and $V_1$ are the same as the Bergman Minimal Model. New parameters include $W$, the patient's weight, $T_1$, the time constant describing the time it takes for glycogen levels to return to basal levels, $k$, the rate of glycogen depletion when glycogen stores become close to depleted, and $a_1$ through $a_6$, which relate the percentage of the maximum oxygen consumption rate to the glucose release rate from the liver, insulin removal rate due to exercise, and the glucose absorption rate caused by exercise. Their typical values are described below with units.\n",
"\n",
"|Parameter|Typical Magnitude|Units |\n",
"|---------|-----------------|---------|\n",
"| $W$ | $62-81$ | $kg$ |\n",
"| $V_G$ | $117-136$ | $dL$ |\n",
"| $T_1$ | $1.86-10.14$ | $min$ | \n",
"| $k$ | $0.0085-0.0131$ | $mg\\space kg^{-1}\\space min^{-2}$ | \n",
"| $a_1$ | $0.0013-0.0019$ | $mg\\space kg^{-1}\\space min^{-2}$ | \n",
"| $a_2$ | $0.0441-0.0679$ | $min^{-1}$ | \n",
"| $a_3$ | $0.0015-0.0024$ | $mg\\space kg^{-1}\\space min^{-2}$ | \n",
"| $a_4$ | $0.0355-0.0617$ | $min^{-1}$ | \n",
"| $a_5$ | $0.0010-0.0015$ | $mU\\space L^{-1}\\space min^{-2}$ | \n",
"| $a_6$ | $0.0588-0.0912$ | $min^{-1}$ | \n",
"\n",
"$u_{ex}$ is a variable which describes exercise magnitude, ranges from 0-92, has the same units as $PVO_2^{max}$, and varies with time. $A_{TH}$, the critical threshold value for energy expenditure, varies with $u_{ex}$, according to the equation:\n",
"$$ A_{TH} = −1.1521(u_{ex})^2 + 87.471(u_{ex})$$"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "gjuZob6n8Zeq"
},
"source": [
"### Core Model Assumptions\n",
"The core assumptions made when using the Bergman Minimal Model and Exercise Model to simulate the blood glucose and blood insulin concentrations in Type I diabetes patients included:\n",
"\n",
"1) The values for parameters $P_1$, $P_2$, $P_3$, $a_1$, $a_2$, $a_3$, $a_4$, $a_5$, $a_6$, $k$, and $T_1$ are valid for the different scenarios tested.\n",
"\n",
"2) The recommended ceiling for blood glucose levels following a meal is $180\\space mg\\space dL^{-1}$, which must be met after 60 minutes.\n",
"\n",
"3) The recommended bounds for blood glucose levels prior to a meal are $80-130\\space mg\\space dL^{-1}$.\n",
"\n",
"4) The volume of blood and tissue of patients who are not obese and are obese is approximately $12\\space L$, as specified in the first table.\n",
"\n",
"5) The ratio of glucose infusion rate to glucose distribution volumes for an average person was $6.0\\space mg\\space dL^{-1} \\space min^{-1}$ at breakfast, $9.0\\space mg\\space dL^{-1} \\space min^{-1}$ at lunch, and $12.0\\space mg\\space dL^{-1} \\space min^{-1}$ at dinner. For obese patients, these values were increased by a factor of $1.124$ due to a larger glucose distribution volume and considerably larger average weight.\n",
"\n",
"6) The decay rate of insulin from high-concentration injections follows a sharp exponential decay instead of a delta function.\n",
"\n",
"7) The decay constants in the exponentials of the glucose disturbance function and insulin injection exponential are $0.05\\space min^{-1}$ and $10\\space min^{-1}$ respectively."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "hvQ8KjTSmwm9"
},
"source": [
"## Results and Executable Elements"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "hvQ8KjTSmwm9"
},
"source": [
"### Import Libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "GQxWnmPe8ZdR"
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"from scipy.integrate import odeint\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "bxYITISV8Zdc"
},
"source": [
"### Define Model Function for Bergman Model\n",
"Function that expresses the set of differential equations, has booleans to activate/deactivate certain variables, and takes in parameter values in order to evaluate blood glucose and insulin levels in the body as it changes in time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "vQWXan1r8Zdd"
},
"outputs": [],
"source": [
"def bergman(y,t,obese_set,U_set,P1,Kp,Kd,Ke,Gsp):\n",
" \n",
" # E is the integral control term used in PID-Control\n",
" # obese_set and U_set are settings that determine whether the system is solved for an obese\n",
" # Type I Diabetes patient and whether the system is solved using controller input\n",
" \n",
" G,I,X,E = y # unpack y variable into its component parts\n",
" \n",
" # Define the Glucose Disturbance Function based on food intake (mg/dL/min)\n",
" D = disturb_meal(t,obese_set,t_b,t_l,t_d) \n",
" \n",
" # Create a differential equation for evaluating the integral control term\n",
" if G-Gsp < 15: # Prevent integral term from \"winding up\" by limiting its application\n",
" dEdt = G-Gsp # to scenarios close to steady-state\n",
" else:\n",
" dEdt = 0\n",
" \n",
" # Glucose Concentration Differential Equation\n",
" dGdt = -P1*G - X*(G+Gb) + D\n",
" \n",
" # Define the controller behavior with respect to controller gain\n",
" U_max = 50 # Set an upper limit on the controller input\n",
" U_basal = n*V1*(Ib-P2*P1*Gsp/P3/(Gsp+Gb)) # Base Controller Input derived from a steady-state G at Gsp\n",
" \n",
" if U_set == 0: # allow a setting where there is no controller input\n",
" U = 0\n",
" elif U_set == 1: # allow a setting where the controller input is just a basal dose\n",
" U = U_basal\n",
" elif U_set == 2: # allow a setting where the controller input is just a single, high concentration\n",
" U2 = U_meal(t,t_b,t_l,t_d,t_p) # injection of insulin prior to meal\n",
" U = U2\n",
" elif U_set == 3: # allow a setting where the controller input is a high concentration injection\n",
" U2 = U_meal(t,t_b,t_l,t_d,t_p) # of insulin prior to meal alongside the basal dose\n",
" U = U_basal + U2\n",
" elif U_set == 4: # allow a setting where the controller input is PID control without basal dose\n",
" U_comp = Kp*(G-Gsp)+Kd*(dGdt)+Ke*E \n",
" U = max(0,min(U_max,U_comp)) \n",
" elif U_set == 5: # allow a setting where the controller input is PID control with basal dose\n",
" U_comp = U_basal + Kp*(G-Gsp)+Kd*(dGdt)+Ke*E \n",
" U = max(0,min(U_max,U_comp))\n",
" elif U_set == 6: # allow a setting that combines PID control and high concentration injection with basal dose\n",
" U_comp = U_basal + Kp*(G-Gsp)+Kd*(dGdt)+Ke*E\n",
" U = max(0,min(U_max,U_comp))\n",
" U2 = U_meal(t,t_b,t_l,t_d,t_p)\n",
" U = U + U2 \n",
" \n",
" # Remaining Differential Equations\n",
" dIdt = -n*(I+Ib) + U/V1\n",
" dXdt = -P2*X + P3*I\n",
" \n",
" return [dGdt,dIdt,dXdt,dEdt]"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "kTFvG-hj8Zev"
},
"source": [
"### Define Model Function for Exercise Model\n",
"Function that expresses the set of differential equations, has booleans to activate/deactivate certain variables, and takes in parameter values in order to evaluate blood glucose and insulin levels in the body as it changes in time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "jAW185EZ8Zew"
},
"outputs": [],
"source": [
"def bergman_ex(y,t,obese_set,U_set,P1,Kp,Kd,Ke,Gsp):\n",
"\n",
" G,I,X,Gprod,Gup,Ie,PVO2,Ggly,A,E = y\n",
" \n",
" # Define the Glucose Disturbance Function based on food intake (mg/dL/min)\n",
" if t>=t_b_ex:\n",
" D = Fg_break*np.exp(-0.05*(t-t_b_ex)) # breakfast\n",
" else:\n",
" D = 0\n",
" \n",
" # Define Exercise Variable u_ex, which is a function that depends on start time and duration\n",
" if t < t_ex_start + duration and t >= t_ex_start:\n",
" u_ex = ex_set\n",
" else:\n",
" u_ex = 0\n",
" Ath = -1.1521*(u_ex)**2+87.471*(u_ex)\n",
" \n",
" # Set parameters for obese patients and non-obese patients\n",
" if obese_set == 1:\n",
" W = avg_w_obese\n",
" Vg = Vg_obese\n",
" D = D*glucose_inf_ratio\n",
" else:\n",
" W = avg_w_lean\n",
" Vg = Vg_lean\n",
" \n",
" # Define how the deviation in glucose concentration changes with time\n",
" dGdt = -P1*G - X*(G+Gb) + D + W/Vg*(Gprod-Gup-Ggly)\n",
" \n",
" # Create a differential equation for evaluating the integral control term\n",
" if G-Gsp < 15:\n",
" dEdt = G-Gsp\n",
" else:\n",
" dEdt = 0\n",
" \n",
" # Define the controller behavior with respect to controller gain\n",
" U_max = 50 # Set an upper limit on the controller input\n",
" U_basal = n*V1*(Ib-P2*P1*Gsp/P3/(Gsp+Gb)) # Base Controller Input derived from a steady-state G at Gsp\n",
" if U_set == 0: # allow a setting where there is no controller input\n",
" U = 0\n",
" elif U_set == 1: # allow a setting where the controller input is just a basal dose\n",
" U = U_basal\n",
" elif U_set == 2: # allow a setting where the controller input is PID control without basal dose\n",
" U_comp = Kp*(G-Gsp)+Kd*(dGdt)+Ke*E \n",
" U = max(0,min(U_max,U_comp)) \n",
" elif U_set == 3: # allow a setting where the controller input is PID control with basal dose\n",
" U_comp = U_basal + Kp*(G-Gsp)+Kd*(dGdt)+Ke*E \n",
" U = max(0,min(U_max,U_comp))\n",
"\n",
" # Define how the deviation in insulin concentration and the intermediate variable change with time\n",
" dIdt = -n*(I+Ib) + U/V1 - Ie\n",
" if I <= -15:\n",
" dIdt = max(0,dIdt)\n",
" dXdt = -P2*X + P3*I\n",
" \n",
" # Define the remaining differential equations\n",
" dGproddt = a1*PVO2-a2*Gprod\n",
" dGupdt = a3*PVO2-a4*Gup\n",
" dIedt = a5*PVO2-a6*Ie\n",
" dPVO2dt = 0.8*(u_ex-PVO2)\n",
" \n",
" if u_ex > 0:\n",
" dAdt = u_ex\n",
" else:\n",
" dAdt = -A/dt\n",
"\n",
" if A < Ath:\n",
" dGglydt = 0\n",
" elif A >= Ath and u_ex > 0:\n",
" dGglydt = k\n",
" elif u_ex == 0:\n",
" dGglydt = -Ggly/T1\n",
" \n",
" if A < 0:\n",
" A = 0\n",
" \n",
" return [dGdt,dIdt,dXdt,dGproddt,dGupdt,dIedt,dPVO2dt,dGglydt,dAdt,dEdt]"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "ErM0c_o68Zdg"
},
"source": [
"### Disturbance Function\n",
"Function that takes in parameters corresponding to glucose levels for different meals and outputs the blood glucose infusion rate for use in calculating the change in blood glucose concentration with time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "vft14RBW8Zdh"
},
"outputs": [],
"source": [
"def disturb_meal(t,obese_set,t_breakfast,t_lunch,t_dinner):\n",
" # Function which descibes glucose intake rate due to different meals\n",
" # Takes into account whether patient is obese and the different meal sizes and times at which they are eaten\n",
" if t