Cgmsim
Realtime T1D simulation of multiple daily injections or insulin pump therapy. Coupled to Nightscout for visualisation and interaction. Can be coupled to iOS Loop or openAPS/AndoidAPS for testing the algorithms in a simulated patient.
Install / Use
/learn @lsandini/CgmsimREADME
Table of Contents
- Table of Contents
- CGM trace generator, MDI only or CSII (version 2.0)
- Modeling the activity of mealtime insulins
- Modeling the activities of the long-acting agonists
- Modeling Levemir (Levemir®)
- Modeling Glargine (Lantus®, Abasaglar®, Toujeo®)
- Switching to biexponential models
- Modeling the absorption of Carbs from the gut
- Modeling the Endogenous Glucose Production (EGP) by the liver
- Modeling exercise
- Random effects
- Mechanics of the simulator
- Adding an insulin pump +/- a DIY artificial pancreas
- NEW feature (random meals + autobolusing - TEST PHASE)
- Installation
- TO DO list
CGM trace generator, MDI only or CSII (version 2.0)
(accelerated view !!!)
This application is simulating in real time and in an extremely basic way the effects food and various insulin analogs on continuous glucose monitor (CGM) curve in type 1 diabetes (T1D). It uses Nightscout (NS) as an input and visualization method.
Since even accurate physiological simulators can at best provide only an approximation of what happens in a biological organism, the goal is not even to try to match reality, but to provide a tool helping to visualize the fluctuations of glucose sensor values in response to various insulin analogs and food.
The simulation can be used as a learning and practicing tool, with a goal of keeping the CGM curve values as much of possible in certain range. The target range is 3.9 – 10 mmol/l or 70-180 mg/dl, and the target Time In Range (TIR) is 70%.
To understand the mechanics of such a simulation, just a few things must be considered.
a) The liver releases glucose into the bloodstream by either releasing its stores (glycogenolysis) or synthesizing new glucose from amino acids (gluconeogenesis). This is called the endogenous glucose production (EGP), and various biological states can affect it (not only alcohol!).
b) The ingestion of food and the digestion and absorption of carbohydrates will also increase the blood glucose. After a short delay, the CGM will reflect the increase of blood glucose.
c) This tool does not (yet) model the effect of exercise.
d) Mealtime insulins have a short duration of activity (DIA -around 3 hours), while basal insulins are meant to be injected once or twice a day, and hence have a much longer duration of activity. Both will lower blood glucose and thus sensor glucose values.
Modeling the activity of mealtime insulins
This has been done has been done extensively for aspart, lispro (and glulisine), and the model provided here: (https://github.com/LoopKit/Loop/issues/388#issuecomment-317938473 ). This is the model I use to compute the activity of each mealtime insulin dose (called "bolus" from now on). I selected a peak time of 55 minutes and a DIA of 300 min. Please notice that these settings may vary, but they are used by the mathematical model and do not always reflect the perception of the duration of activity of a single dose. They can be easily modified in the code, should the user prefer a shorter time to peak, e.g. for faster aspart insulin Fiasp®.
Modeling the activities of the long-acting agonists
For detemir and glargine instead, models are still lacking. Based on clamp studies in T1D, the intra-individual, day-to-day variation is an important factor affecting the predictability of a single repeated dose. Moreover, the inter-individual variability makes modeling a challenge. Even with the best curve-fitting tools, no “global model” was achieved.
Since the goal is not to make a perfect model, I decided to use a sinusoidal curve to model detemir, and a half-ellipse for glargine. The obvious reasons are that the mathematics are simple.
Modeling Levemir (Levemir®)
The clamp studies show that the dose-response curve is linear. With increasing doses from 0.1U/kg to 1.6U/kg, the total activity, or area under the curve (AUC) of the glucose infusion rate (GIR) needed to maintain normal blood glucose is a straight line.

However, the DIA seems related to the dose/kg, so that had to be considered in the model. The AUC precisely reflects the injected number of units and is identical for 12U @0.1U/kg and 12U @0.4U/kg. However, when increasing the do dose from 12U @0.4U/kg to 24U @0.4U/kg, you can see that the DIA increases:

Duration of Levemir action = 16 + (20 * U/weight) So for 0.1 U/kg, the duration of action is 16+(20 * 0.1) = 18 hours, and for 0.4U/kg, the DIA is 16+(20 * 0.4) = 24 hours.
And the model itself is: y= units * (Math.PI/(duration * 2)) * (Math.sin(time * Math.PI/duration));
(for the biexponential model in detemir2.js, the duration of activity = (14 + (24 * dose/weight)), and the peak is duration/3.
Modeling Glargine (Lantus®, Abasaglar®, Toujeo®)
For the time being, only glargine U100 is basically modeled here, with a DIA of 27 hours. Again, the AUC reflects the injected dose.

I “chopped” the equation in little bits fo clarity :
b = (2 * basalDose)/(Math.PI * duration); // duration is 27 hours
x = (Date.now() - time0)/(60 * 60 * 1000); //time0 is the time of injection
g = x-(duration / 2);
gg = Math.pow(g,2);
h = duration / 2;
hh= Math.pow(h,2);
z = (x-gg) / hh;
bb = Math.pow(b,2);
y = 2 * Math.sqrt(bb * (1+z)); // where y is the activity of glargine over time
(for the biexponential model in glargine2.js, the duration of activity = (22 + (12 * dose/weight)), and the peak is duration/2.5 .
Here is a visual aid illustrating the differences between the activity curves of detemir and glargine at different doses:

Switching to biexponential models
30.09.2021 While the very basic mathematical models of long acting insulin analogs are good enough for training, getting closer to published "curves" and physiological activity is necessary. I decided to adapt the biexponential model used for mealtimes insulins.
The activity of detemir and glargine-U100 depend on the absolute dose, but also on the dose in relation to the patient's weight. Also, the peak of action is achieved at different timepoints. The DIA of detemir is more variable and dose dependent than that of glargine:
For detemir, the duration of action is 14h + (24* dose/weight), and the peak is at duration/3.
For glargine U100, the duration of action is 22h + (12* dose/weight), and the peak is at duration/2.5.


Notice that the curve colours are different, and some of the activity curves seem to derived from steady-state euglycemic clamps.
Modeling the absorption of Carbs from the gut
There are many complicated and more or less precise published models of carb absorption, but for the purpose of this simulation, a simple bilinear model like the one found in the book "Think Like a Pancreas" by Gary Scheiner, and used in Perceptus' Glucodyn simulator will do for now. https://github.com/Perceptus/GlucoDyn/blob/master/basic_math.pdf .
To make the absorption of carbs from meals more realistic, I divided each meal in fast and slow absorbing parts. The ratio of fast absorbing carbs is randomly computed to be 10-40 % of the whole meal. However, since todays June 10th 2021, the first 40 g of each meal will always be fast absorbing, so all small snacks will be fast carbs !!!. In the current version of the algorithm, the fast carbs will be abosrbed according to the bilinear model mentioned above, in a period of time of one hour. The remaining (slow) carbs are absorbed in a similar fashion, but over a period of 4 hours.
Modeling the Endogenous Glucose Production (EGP) by the liver
While this is absolutely neither true nor realistic, for the time being the EGP is modeled as a linear function of time. It equivalents to 10g of absorbed carbs/hour, so depending on the user's insulin sensitivity factor (ISF, mmol/l/U) and carb ratio (CR, g/U), an the EGP effect is EGP * ISF * CR expressed in mmol/l/g.
The EGP is significantly affected but the insulin ac
