Simulating the Spread of Infectious Disease in 200 lines of C
Published 18 March 2020
Tags: #covid, #infection, #c, #simulation
Greetings from Italy. I am currently quarantined in my apartment. For me and many other people this lockdown can often get motivationally debilitating when it comes to working to personal, academic or work projects. The lockdown measures taken by governments to fight off the pandemic are necessary to reduce the damage that the disease caused by the SARS-COV-2 "coronavirus" is doing to the global society. Inspired by a recent popular news article about the effectivity of containment measures during outbreaks, I wrote this small animation/simulation in a single C file (using raylib, you need it to build this project) to show what happens if you avoid moving and social contact during an epidemic. You can find the code in this GitHub repository. All the parameters are easily adjustable and hardcoded. Yes, you need to recompile if you change them.
Keep in mind that this is not a precise simulation and it is quite buggy. For the sake of simplicity, the bouncing balls do not even remotely resemble a realistic physical simulation of colliding particles in a box, but instead have randomized drifting of their trajectory. This is a toy animation made to show that distancing/quarantine measures during outbreaks are good countermeasures and delay the rapid spread of infectious disease. Spheres in a box that are moving (or not moving) around are simple enough to show the difference between situations where a percentage of the population is not moving from their houses during an epidemic. In reality, a precise mathematical model of a pandemic response has probably dozens more parameters. For example, in this unprecise simulation, there is a global variable representing the chance - between 0 and 1
contact. In real mathematical modeling of disease there's a number called Basic reproduction number or R0, which represents "the expected number of cases directly generated by one case in a population where all individuals are susceptible to infection". You can read more about the SARS-COV-2 R0 in this paper: Estimating the effective reproduction number of the 2019-nCoV in China. In a precise simulation one should also keep track of the average geographical location and movements, age, social context and health condition. This requires a lot of additional data that does not fit in 200 lines of C code.
The graph shows a sample (taken every 5 frames by default) of the size of these partitions of the total population over time. Default simulation time is 40 seconds and is easily tweakable. Other easily adjustable values are window width and height, probability of dying if infected, probability of infecting another individual upon contact, the time needed to heal or die, the population size, the fraction of people that are moving and the graph sampling rate.
You can easily see that the less people are moving, the slower it takes to the disease to reach the whole population. In contrast, if everybody ignores the quarantining measures, such as in the fourth screenshot of the simulation, you can see that the disease spreads extremely fast.