用 JavaScript 計算 HVAC 風管尺寸:不靠圖表求解 Colebrook–White 方程
Sizing HVAC ducts in JavaScript: Colebrook–White without the chart
Ductwise 的 HVAC 風管尺寸計算器以約 80 行原生 JavaScript,根據氣流量和設計摩擦率求出圓形風管直徑及下一個可購買的標準尺寸。程式以 Swamee–Jain 近似值起步,再用 Newton 法迭代求解 Colebrook–White 摩擦因子,並透過二分搜尋確定直徑。相同計算引擎用於瀏覽器版及 Ductwise iPhone 和 Android 應用程式。
HVAC installers have sized ducts with a cardboard slide rule (a "ductulator") or a printed friction chart for decades. Both are drawings of the same two equations. We needed those equations in code for a duct sizing app and its free web version, and the result is about 80 lines of plain JavaScript. This post walks through them.
The problem
Input: an airflow Q (m³/s) and a design friction rate (pressure lost per metre of straight duct, Pa/m).
Output: the round duct diameter D that carries Q at exactly that friction rate, then the next diameter you can actually buy.
There is no closed form for D, because the friction factor itself depends on D. So it is two nested numerical problems: solve for the friction factor at a given diameter, and search for the diameter that gives the target pressure drop.
Step 1: air properties
Standard air at 20 °C and sea level. Density from the ideal gas law, viscosity from Sutherland's law:
const R = 287.052874; // J/(kg·K), dry air
function air(T, p) {
const density = p / (R * T);
const viscosity = 1.716e-5 * ((273.15 + 110.4) / (T + 110.4)) * Math.pow(T / 273.15, 1.5);
return { density, viscosity };
}
const standardAir = air(293.15, 101325); // ≈ 1.204 kg/m³
Keeping T and p as parameters matters: a mile above sea level the air is about 17 % lighter, and every rule-of-thumb constant drifts with it.
Step 2: the friction factor (Colebrook–White)
Darcy–Weisbach gives the pressure loss per metre:
Δp/L = f / D · ½ ρ v²
The Darcy friction factor f comes from the implicit Colebrook–White equation, in terms of the Reynolds number Re and the relative roughness ε/D:
1/√f = −2 log10( ε/(3.7 D) + 2.51 / (Re √f) )
We start from the explicit Swamee–Jain approximation and polish it with Newton's method on x = 1/√f. It converges in a handful of iterations:
function swameeJain(re, rr) {
if (re < 2300) return 64 / re; // laminar
const d = Math.log10(rr / 3.7 + 5.74 / Math.pow(re, 0.9));
return 0.25 / (d * d);
}
function colebrook(re, rr) {
if (re < 2300) return 64 / re;
let x = 1 / Math.sqrt(swameeJain(re, rr));
const a = 2.51, b = rr / 3.7;
for (let i = 0; i < 60; i++) {
const arg = b + a * x / re;
const g = x + 2 * Math.log10(arg); // we want g(x) = 0
const dg = 1 + (2 / Math.LN10) * (a / re) / arg;
const step = g / dg;
x -= step;
if (Math.abs(step) < 1e-12) break;
}
return 1 / (x * x);
}
Swamee–Jain alone is within about 1 % of Colebrook, which is fine for a chart. We iterate anyway, so the numbers match the app's own test vectors exactly.
Step 3: analyse one diameter
function analyseRound(Q, D, eps, air) {
const area = Math.PI * D * D / 4;
const v = Q / area;
const re = air.density * v * D / air.viscosity;
const f = colebrook(re, eps / D);
const pv = 0.5 * air.density * v * v; // velocity pressure
return { velocity: v, reynolds: re, frictionRate: f / D * pv };
}
For a rectangular duct you would use the hydraulic diameter 4A/P here instead of D.
Step 4: search for the diameter
The friction rate falls steadily as the diameter grows, so a bisection works. We bisect in log space (geometric mean), because diameters range from 10 mm to 5 m:
function sizeByFriction(Q, target, eps, air) {
let lo = 0.01, hi = 5.0; // metres
for (let i = 0; i < 300; i++) {
const mid = Math.sqrt(lo * hi);
if (analyseRound(Q, mid, eps, air).frictionRate > target) lo = mid; else hi = mid;
if (hi / lo - 1 < 1e-10) break;
}
return Math.sqrt(lo * hi);
}
Then round up to a catalogue size (EN 1506 in metric; 4–10 in by 1 in and 12–48 in by 2 in in imperial), and analyse that duct again to report its real velocity and friction.
A worked example
400 CFM (0.189 m³/s) at 0.1 inches of water per 100 ft (0.817 Pa/m) in galvanised steel (ε = 0.09 mm):
| Value | |
|---|---|
| Exact diameter | 9.68 in |
| Next standard size | 10 in |
| Velocity in the 10 in duct | 733 fpm (3.73 m/s) |
| Its friction rate | 0.085 in.wg/100 ft |
Rounding up always lowers both the velocity and the friction, which is why the "actual" numbers are a little under the target.
Units: keep SI inside
All the maths runs in SI. The UI converts at the edges only (CFM, inches, fpm, in.wg/100 ft), with the conversion factors in one table. Mixing units inside the solver is where duct calculators usually go wrong.
Testing against the app
The web version is a port of the app's C# core. We hold it to the app's own outputs with golden vectors: a list of inputs and the exact numbers the app produces, checked on every change. If the two ever disagree, the web page is wrong by definition.
Try it
The calculator runs in the browser at bekservice.uk/ductwise/duct-size-calculator, and there is a plain-language duct sizing guide for the HVAC side. The same engine is in the Ductwise app for iPhone and Android, made by BEK Service Ltd.
This post was written with AI assistance from our own code and documentation.
來源:DEV Community · dev.to