Run a 60-pig finishing barn: set the fans, inlets, sprinklers and feed while the weather changes. Pigs produce about 150–200 W each; the barn air, humidity, CO₂ and ammonia follow from the ventilation rate, and once the effective temperature passes the pigs’ comfort limit their feed intake and growth drop.
Tap a pig in the barn to inspect it.
Compares the code with closed-form results: steady-state barn temperature, CO₂ and humidity, the fan/inlet operating point, saturation vapour pressure, moisture mass closure and a literature plausibility band.
The science
Heat production. Metabolisable energy intake (MEI) is split into maintenance, retained gain and heat. Maintenance is 0.458·M0.75 MJ/day; surplus energy is retained with an efficiency k (0.62–0.74 depending on the diet) and every joule not retained leaves as heat:
THP = MEI − Egain, Egain = k·(MEI − MEm − MEthermoreg), ADG = Egain/16 MJ·kg⁻¹
A 60 kg pig eating to appetite makes about 180 W, ~9 W per kg0.75 (literature range roughly 7–10). Part is latent (evaporated water): the latent share rises with temperature, flat = 0.25 + 0.02·(T−20), clamped to 0.08–0.6.
Barn air balance (well-mixed volume V, outdoor air To, flow Q):
C·dT/dt = n·THP·(1−flat) − Qspray + UA·(Tsa−T) − ρcpQ·(T−To)
ρV·dw/dt = ṁlat + ṁevap − ρQ·(w−wo)
V·dc/dt = PCO2 − Q·(c−co), PCO2 = 0.185 m³/h per kW of heat
V·dN/dt = ENH3 − Q·N
Each equation is stepped with an implicit (unconditionally stable) scheme. At steady state T = To + Qs/(UA + ρcpQ), c = co + P/Q — so the ventilation needed to hold each limit (the table above) is simply the rearranged balance; whichever limit needs the most air is the controlling one. In winter it is usually CO₂, ammonia or humidity, in summer temperature.
Fans and inlets. The fans follow a linear curve Δp = Δp0(1 − Q/Q0) (Q0 = fans on × speed × 1.4 m³/s). The inlet slots are an orifice, Δp = ½ρ(Q/(CdA))² with Cd = 0.6. The operating point is where the two meet (solved in closed form and by bisection); closing the inlets chokes the airflow even with every fan running.
What the pig feels. Effective temperature = air temperature + a humidity term (− air speed, − evaporative cooling from wet skin). The temperature–humidity index THI = 0.8T + RH·(T − 14.4) + 46.4 (T in °C, RH as a fraction) is shown with the common 74 / 79 / 84 alert / danger / emergency thresholds. Above the upper critical temperature (UCT = 27 − 0.05·M) appetite falls ~3 % per °C, panting adds maintenance cost, so heat stress cuts both feed intake and growth. Below the lower critical temperature (LCT = 24 − 0.09·M) pigs eat more but waste energy keeping warm, and huddle.
Heater. The optional supplementary heater adds its power to the sensible balance whenever the barn is below the set point (its energy is counted in the kWh readout). Sprinklers convert sensible heat into latent heat: they cool pigs and air only while the air can still take up water (evaporated fraction falls with RH). In humid weather they just add humidity; wet floors also raise ammonia emission.
Honest simplifications. One average pig and one well-mixed air node; no stratification, wall condensation, manure-pit dynamics or individual variation. UA, thermal mass, fan sizes, emission factors, 3 %/°C appetite loss, the effective-temperature corrections and the THI thresholds (borrowed from cattle practice) are plausible round numbers, not measurements of a particular barn or breed. The pig animation (panting, lying, huddling) is illustrative. Educational model, not veterinary or engineering advice.
Baked 3D scene. Barn, equipment and pig models and their baked albedo, normal and ambient-occlusion textures come from Blender 5.2 (Cycles bake); Three.js r160 runs the live part.