Simulating Disease in the Browser with Rust and WebAssembly
The Problem
Soil-transmitted helminths β Ascaris lumbricoides, Trichuris trichiura, and Hookworm β infect over 1.5 billion people worldwide. The burden falls hardest on school-aged children in tropical regions where sanitation infrastructure is inadequate. In Cebu City, Philippines, prevalence rates vary dramatically between urban and rural barangays, and the pattern is not what you would expect.
Nikka's master's thesis collected real prevalence data from field surveys across Cebu City. The numbers told a counterintuitive story: 20.3% prevalence in urban barangays versus 4.9% in rural ones. The conventional assumption β that rural areas with more soil exposure would have higher transmission β was wrong. The question became: can we model the actual transmission dynamics, understand why urban prevalence is higher, and test intervention strategies computationally before deploying them in the field?
Traditional compartmental models like SIR and SIS treat populations as homogeneous buckets. Susceptible flows to Infected flows to Susceptible again, governed by aggregate rate parameters. These models are mathematically elegant. They are also insufficient for this problem. They cannot capture individual behavioral differences. They cannot represent spatial heterogeneity β the fact that contamination concentrates near specific locations. They cannot model the interaction between knowledge, attitudes, infrastructure access, and actual infection risk. They tell you that prevalence will be X percent. They cannot tell you where to build the next toilet.
An agent-based model can do all of these things. Each simulated person is an autonomous entity with their own behaviors, knowledge levels, and environmental context. The emergent population-level dynamics arise from individual interactions with a contaminated environment. This is the only modeling approach that can answer operational questions about intervention targeting.
Constraints
The simulation had to run in a browser. No desktop application, no installation step, no Python environment to configure. The target users are field researchers and public health officials working on Chromebooks and tablets. If it requires downloading an executable, it will not get used.
Performance was non-negotiable: 500+ agents running at 60 frames per second with real-time parameter adjustment. A researcher needs to drag a slider, change MDA frequency from annual to biannual, and immediately see the impact on the prevalence curve. Anything less than interactive speed makes the tool a demo instead of an instrument.
The simulation had to be calibrated against published thesis data. This is not a toy model or a classroom exercise. The numbers have to match real observations from real field surveys. If the model cannot reproduce the 20.3% urban and 4.9% rural prevalence split, it is wrong and must be corrected until it can.
The compiled WASM binary had to stay under 300KB. Rural Philippine internet connections are slow and expensive. A multi-megabyte download is a hard barrier to adoption. Every kilobyte of the binary had to earn its place.
Finally, the KAP (Knowledge, Attitudes, Practices) behavioral model had to reflect real-world findings from the thesis data. The central finding: infrastructure access dominates behavioral interventions. This had to be encoded as a first-class mechanic in the simulation, not an afterthought.
System
Architecture
The core simulation engine is written in Rust, compiled to WebAssembly via wasm-pack. The frontend is Next.js. The critical architectural decision is Web Worker isolation: the entire simulation runs off the main thread, communicating with the UI via Comlink and zero-copy data transfer using transferable ArrayBuffers. The UI thread never touches simulation state directly. This means the browser never freezes regardless of agent count or simulation complexity. Drag a slider, click a button, resize the window β the simulation keeps running at full speed in its isolated thread.
The data flow is unidirectional. The UI sends parameter changes to the worker. The worker runs simulation ticks and produces a flat Float32Array of agent state. That buffer is transferred (not copied) back to the main thread where the WebGL2 renderer consumes it directly. No serialization, no JSON parsing, no garbage collection pressure from temporary objects.
The Agent-Based Model
Each of the 500+ agents is a discrete entity with individual state. Every agent has a daily schedule divided into hourly time steps. They move between locations β home, school, work, communal water sources, open defecation sites. Each agent carries a WHO EPG (eggs per gram) intensity classification for each of three STH species simultaneously: uninfected, light, moderate, or heavy. Each agent has individual KAP scores that influence their behavioral choices. Each agent has a spatial position on a contamination grid.
The infection dynamics follow a stochastic SIS model at the individual level. There is no recovered compartment because STH does not confer lasting immunity. You get infected, you get treated or your body partially clears the worms over years, and you get infected again. The SIS cycle is the fundamental challenge of STH control.
Infection acquisition is probabilistic. At each time step, a susceptible agent's infection probability depends on the contamination level of their current grid cell, their exposure behavior (barefoot walking, handwashing frequency), and a stochastic roll. Heavy infections produce more eggs, contributing more to environmental contamination, creating a positive feedback loop.
Environmental Contamination
The simulation world is a discrete grid. Infected agents shed helminth eggs into their current cell based on their infection intensity and species-specific egg output rates. Contamination in each cell decays over time β eggs have finite environmental survival β but the decay rate varies by species and environmental conditions. Rain events redistribute contamination, spreading eggs to adjacent cells and simulating the real-world mechanism of runoff spreading fecal contamination.
Other agents moving through contaminated cells accumulate exposure. The probability of infection is a function of cumulative exposure, not a single contact event. This matters because it means agents who spend more time in contaminated areas β because they lack access to sanitation facilities, because their daily route passes through contaminated zones β accumulate risk faster. The spatial dimension of the model is not decorative. It is mechanistically necessary.
The KAP Behavioral Model and the Structural Dominance Gate
This is the core intellectual contribution of the simulation, derived directly from the thesis data. The behavioral model encodes a hierarchy that contradicts the naive assumption that health education reduces infection.
Each agent has three KAP scores: Knowledge (understanding of transmission routes), Attitudes (belief that prevention matters), and Practices (actual behavioral habits). In a simple model, higher KAP scores would directly reduce infection probability. In this model, they do not β unless a structural precondition is met.
The structural dominance gate asks: does this agent have access to a toilet and clean water? If yes, KAP scores modulate behavior meaningfully. An agent who knows about handwashing and has access to a handwashing station will wash their hands. If no β if there is no toilet and no clean water β KAP scores are nearly irrelevant. An agent who knows everything about STH transmission but has nowhere to defecate except an open field will still contaminate the environment. An agent who believes in handwashing but has no water source will not wash their hands.
This gate explains the thesis data. Urban barangays have higher population density packed into areas with inadequate sanitation infrastructure. More people per toilet, more people per water point, more fecal contamination per square meter of shared space. Rural barangays have lower density and, counterintuitively, better per-capita infrastructure access. The contamination load per unit area is lower. Density multiplied by infrastructure deficit is the driver, not proximity to soil.
Execution
Three Species, Three Problems
Ascaris lumbricoides, Trichuris trichiura, and Hookworm are not interchangeable. Each species has distinct epidemiological parameters that matter for intervention planning.
Ascaris has the highest egg output β a single female worm produces up to 200,000 eggs per day. Eggs are extraordinarily hardy, surviving months in soil. But Ascaris responds well to treatment: albendazole achieves roughly 95% cure rates. MDA campaigns hit Ascaris hard.
Trichuris has lower egg output but is stubbornly resistant to treatment. Albendazole cure rates for Trichuris hover around 50%. This single number has enormous policy implications: mass drug administration alone cannot eliminate Trichuris. You need environmental intervention.
Hookworm has a unique transmission route β larvae penetrate skin on contact with contaminated soil, making barefoot walking the primary risk factor. Albendazole cure rates are approximately 75%, intermediate between Ascaris and Trichuris. Hookworm is the species most directly addressable by behavioral intervention (wearing shoes), but only where shoes are available and culturally accepted.
The simulation models all three species simultaneously in each agent. Co-infection is common and the treatment implications of co-infection β particularly the differential drug efficacy β make single-species models dangerously misleading for policy planning.
The Intervention System
Five intervention types are available, each with configurable parameters:
Mass Drug Administration (MDA): Deworming campaigns at configurable frequency (annual, biannual, quarterly), population coverage percentage, and drug type. Drug efficacy values are pulled from published meta-analyses, not invented. The simulation applies treatment probabilistically β 95% of treated Ascaris infections clear, 50% of Trichuris, 75% of Hookworm.
WASH Infrastructure: Toilet construction, water access points, handwashing stations. Each facility has a location on the grid and a capacity. Building a toilet near a cluster of agents without sanitation access immediately unlocks the KAP behavioral model for those agents. The spatial placement matters β a toilet on the wrong side of the barangay does nothing for agents who cannot reach it.
Health Education Programs: Increase KAP scores across the population over time. Effective only where infrastructure exists to act on the knowledge. The simulation makes this dependency brutally visible.
BHW (Barangay Health Worker) Visits: Targeted individual interventions β testing, treatment, and education delivered to specific agents. More expensive per capita than mass campaigns but more effective for persistent high-intensity infections.
Budget Constraints: You cannot deploy everything simultaneously. The simulation includes a budget allocation system that forces trade-offs. Spend the budget on MDA or on building toilets? The answer depends on the current prevalence, the species mix, and the existing infrastructure β exactly the kind of decision the simulation is built to inform.
The Reinfection Cycle
This is what makes STH control genuinely hard, and what the simulation demonstrates more viscerally than any paper can.
Run MDA every six months with 80% coverage. Watch prevalence drop sharply after each campaign. Watch it climb back to baseline within four to six months as agents are reinfected from the still-contaminated environment. The eggs are in the soil. The worms are gone from the treated individuals, but the eggs laid before treatment persist for months. New infections begin immediately.
Run the same scenario but add WASH infrastructure before the first MDA round. Prevalence drops after MDA and stays down. The floor drops permanently because the reinfection pathway is partially blocked. Fewer agents contaminating the environment means lower environmental egg load means lower reinfection probability. The positive feedback loop reverses direction.
The simulation validates what WHO has published for years: integrated strategies combining MDA with WASH produce sustained reductions. MDA alone is a treadmill.
WebGL2 Rendering
The visualization uses a WebGL2 instanced renderer. Each agent is a single draw call instance consuming 16 floats from a shared buffer: position (x, y), infection state per species, KAP scores, and rendering metadata. The entire agent population renders in a single draw call.
Agents appear as colored circles. Green for susceptible, yellow for light infection, orange for moderate, red for heavy. The contamination grid renders as a heatmap overlay showing environmental egg density β darker red means more contamination, and you can see it pool around areas without sanitation facilities.
Facility placement is interactive. Click on the grid to build a toilet. Watch the contamination heatmap lighten around it over the next few simulation months as agents in range shift their defecation behavior. Watch the agent colors around it shift from red and orange toward green.
The urban versus rural split-screen comparison mode runs two simulations side-by-side with identical disease parameters but different population densities and infrastructure layouts. Same pathogen, same drug efficacy, same human behavior model β different spatial configuration. The prevalence divergence emerges naturally from the simulation mechanics, matching the thesis data without any special-case code.
Outcome
The compiled WASM binary comes in under 300KB. The simulation sustains 500+ agents at 60fps in Chrome on mid-range hardware. The Web Worker architecture ensures zero UI jank regardless of simulation load.
Calibration against the thesis data succeeds: the model reproduces the 20.3% urban and 4.9% rural prevalence split using only mechanistic parameters β population density, infrastructure distribution, and species-specific epidemiological constants. No curve-fitting hacks. The prevalence emerges from the agent interactions.
The intervention experiments validate established public health findings. MDA alone produces temporary reductions that return to baseline within six to twelve months, consistent with WHO published data. WASH infrastructure alone produces smaller but permanent reductions. The combined MDA plus WASH strategy achieves the lowest sustained prevalence, validating the WHO integrated approach recommendation.
The simulation also produces actionable spatial insights that aggregate models cannot: given a fixed budget, where should the next sanitation facility be built to maximize prevalence reduction? The answer depends on current contamination patterns and population density β exactly the information an agent-based spatial model provides.
Lessons
Real data is humbling. The first version of the model predicted higher rural prevalence. The urban density interaction with infrastructure deficit was not obvious from first principles. Calibration against field data forced the correction. Models without calibration data are fiction. They tell you what your assumptions imply. They do not tell you what is true.
Agent-based beats compartmental for policy testing. A compartmental SIS model can match aggregate prevalence curves with the right parameters. It cannot tell you where to place a toilet, which subpopulation to target with BHW visits, or how spatial clustering of infrastructure affects reinfection dynamics. If the question is "what is the prevalence," use a compartmental model. If the question is "what should we do about it," use an agent-based model.
Infrastructure outweighs behavior change. The structural dominance gate in the KAP model is the single most important finding. Health education without sanitation infrastructure is noise. You can teach every child in a barangay about handwashing. If there is no water source, nothing changes. This is not a modeling insight. It is a public health insight that the model validates and makes impossible to ignore.
Rust and WebAssembly is the right tool for browser simulation. JavaScript cannot sustain 500+ agents with per-tick stochastic infection dynamics, environmental grid updates, and pathfinding at 60fps. Rust compiles to WASM that runs at near-native speed with predictable memory behavior and no garbage collection pauses. The 300KB binary loads faster than most JavaScript bundle splits. The Web Worker isolation means the simulation engine has zero coupling to the rendering framework β it could run headless for batch experiments with no code changes.
The reinfection cycle is the enemy. Any intervention strategy that does not address environmental contamination is buying time, not solving the problem. The simulation makes this viscerally obvious in a way that reading a paper does not. Watch five hundred green dots turn yellow and orange and red over eight months after a "successful" deworming campaign that achieved 95% treatment coverage. The worms are back because the eggs never left. The only permanent intervention is the one that breaks the environmental transmission pathway.