Published
Edited
Apr 5, 2020
20 stars
Insert cell
Insert cell
Insert cell
// Note: this parameter is different from "beta" in SIR differential equation
beta = 0.25
Insert cell
// Note: this parameter is different from "gamma" in SIR differential equation
gamma = 1 / 5
Insert cell
Insert cell
Insert cell
Insert cell
generateInitState = (nInfected, density) => {
const initialState = (Array(radius * radius * 4)).fill(0);

const nSusceptible = Math.floor(radius * radius * density - nInfected);
const shuffled = [...Array(radius * radius).keys()] // Shuffle twice
.sort(() => 0.5 - Math.random()).sort(() => 0.5 - Math.random());
const infectedIdx = shuffled.slice(0, nInfected);
const susceptibleIdx = shuffled.slice(nInfected, nInfected + nSusceptible);

infectedIdx.forEach(i => {
initialState[4 * i + 1] = 255;
});
susceptibleIdx.forEach(i => {
initialState[4 * i] = 255;
});

return initialState;
}
Insert cell
state = {
const initialState = generateInitState(nInfected, density);
return (Array(2)).fill().map(() =>
gl.regl.framebuffer({
color: gl.regl.texture({
radius: radius,
data: initialState
}),
depth: false,
depthStencil: false
})
);
}
Insert cell
drawQuad = gl.regl({
vert: quadVert,
frag: quadFrag,
attributes: {
position: [[0, 0], [1, 0], [0, 1], [0, 1], [1, 0], [1, 1]]
},
uniforms: {
prevState: ({ tick }) => state[tick % 2]
},
count: 6
})
Insert cell
update = gl.regl({
vert: quadVert,
frag: updateFrag,
attributes: {
position: [[0, 0], [1, 0], [0, 1], [0, 1], [1, 0], [1, 1]]
},
uniforms: {
radius: radius,
beta: beta,
gamma: gamma,
seed: gl.regl.prop("seed"),
prevState: ({ tick }) => state[tick % 2]
},
count: 6
})
Insert cell
{
return gl.regl.frame(({tick}) => {
drawQuad();
state[(tick + 1) % 2].use(() => {
update({seed: Math.random()});
})
})
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more