code wiki / (root) / nx_vessel_thermal_test.nx

nx_vessel_thermal_test.nx source

↩ module page · 46 lines · 2124 B

1// nx_vessel_thermal_test.nx -- smoke for nx_vessel_thermal (H0). 2// 3// Proves a real-units physics twin that SIZES HARDWARE IN SOFTWARE: 4// - a 50 W heater on a well-insulated 4 L vessel heats up and reaches 5// the 43 C setpoint (1-2) 6// - equilibrium temperature is correct (3) 7// - DESIGN CHECK: an undersized 5 W heater can NEVER reach 43 C -- caught 8// before buying the part (4-5) 9// - hold duty is sane (< full) and BETTER INSULATION needs less duty (6-8) 10// Exit code = failed assertion number; 0 = all pass. 11 12import "nx_syscalls.nx" 13import "nx_vessel_thermal.nx" 14 15func main() -> i64 { 16 // 4 L milk: C = 4 kg * 4186 J/kg.K = 16744 J/K; 50 W heater; 1 W/K loss; 17 // ambient 20 C. 18 let v: *NxVesselThermal = nx_vessel_thermal_new(16744, 50000, 1000, 20000) 19 20 // --- heats up from ambient at full duty, crosses the 43 C setpoint --- 21 var temp: i64 = 20000 22 var step: i64 = 0 23 while step < 250 { 24 temp = nx_vessel_thermal_step(v, temp, 1000, 60) // full duty, 60 s steps 25 step = step + 1 26 } 27 if temp < 43000 { return 1 } // reached setpoint 28 if temp > nx_vessel_equilibrium_mc(v) { return 2 } // never exceeds physical max 29 30 // --- equilibrium = ambient + P/U = 20000 + 50000*1000/1000 = 70000 --- 31 if nx_vessel_equilibrium_mc(v) != 70000 { return 3 } 32 33 // --- DESIGN CHECK: a 5 W heater cannot reach 43 C (eq = 25 C) --- 34 let weak: *NxVesselThermal = nx_vessel_thermal_new(16744, 5000, 1000, 20000) 35 if nx_vessel_can_reach(weak, 43000) != 0 { return 4 } // correctly flags undersized 36 if nx_vessel_can_reach(v, 43000) != 1 { return 5 } // the 50 W design is fine 37 38 // --- hold duty is a sane fraction, and better insulation needs less --- 39 let d: i64 = nx_vessel_hold_duty_milli(v, 43000) 40 if d <= 0 { return 6 } 41 if d >= 1000 { return 7 } // heater holds with margin 42 let well: *NxVesselThermal = nx_vessel_thermal_new(16744, 50000, 500, 20000) 43 if nx_vessel_hold_duty_milli(well, 43000) >= d { return 8 } // half the loss, less duty 44 45 return 0 46}