code wiki / _hdl_build / nx_gassim_gate.nx
nx_gassim_gate.nx source
↩ module page · 74 lines · 5424 B
1// nx_gassim_gate.nx -- NATIVE verify of the browser kinetic-theory gas sim (base = mmap; SAME code runs in the
2// sovereign WASM). Proves real statistical-mechanics physics: (1) the gas runs and collides, (2) every particle
3// stays contained (no tunnel/escape), (3) total kinetic energy is conserved over a long run (elastic dynamics),
4// (4) an isolated head-on elastic collision conserves total momentum AND kinetic energy EXACTLY, (5) THERMALIZATION
5// -- starting from a single speed the velocity distribution spreads (onset of Maxwell-Boltzmann), (6) raster -> PNG.
6// license_tier: ORIGINAL expect_exit: 0
7import "nx_syscalls.nx"
8import "nx_gassim.nx"
9import "nx_png.nx"
10
11func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
12func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-");m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1}; sys_write(1,o,k); return 0 }
13func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" "); g_w(id); g_w(": "); if ok==1 { g_w("OK\n"); pass[0]=pass[0]+1 } else { g_w("FAIL\n") } return 0 }
14func iabs(x: i64) -> i64 { if x<0 { return 0-x } return x }
15func isq(N0: i64) -> i64 { if N0<2 { return N0 } var x: i64=N0; var y: i64=(x+1)/2; while y<x { x=y; y=(x + N0/x)/2 } return x }
16
17func main() -> i64 {
18 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0
19 g_w("=== NX-GASSIM native verify (kinetic-theory gas; SAME code runs in sovereign wasm) ===\n")
20 let base: i64 = sys_mmap(2*1048576) as i64
21 let PX: *i64=(base+O_PX) as *i64; let PY: *i64=(base+O_PY) as *i64; let VX: *i64=(base+O_VX) as *i64; let VY: *i64=(base+O_VY) as *i64
22 let COL: *i64=(base+O_COL) as *i64
23
24 // ---- main run: thermalization + energy + containment ----
25 init_impl(base)
26 let e0: i64 = energy(base)
27 var t: i64=0; while t<1000 { step_one_impl(base); t=t+1 }
28 let e1: i64 = energy(base)
29 let drift: i64 = iabs(e1-e0)*1000/e0 // permille
30 let col: i64 = COL[0]
31 // speed spread (start: all 200 -> spread 0)
32 var smin: i64=999999; var smax: i64=0; var i: i64=0
33 while i<N { let sp: i64=isq(VX[i]*VX[i]+VY[i]*VY[i]); if sp<smin { smin=sp } if sp>smax { smax=sp } i=i+1 }
34 let spread: i64 = smax-smin
35 // containment
36 var allin: i64=1; i=0
37 while i<N { if PX[i]<RAD { allin=0 } if PX[i]>BW-RAD { allin=0 } if PY[i]<RAD { allin=0 } if PY[i]>BW-RAD { allin=0 } i=i+1 }
38
39 render_impl(base)
40 write_png((base + O_FB) as *i64, W, H, "knowledge/nx_gassim.png" as *u8)
41 var nb: i64=0; i=0
42 while i<W*H { let b: i64=i*8; let m: *u8=(base+O_FB) as *u8; if (m[b] as i64)!=12 { nb=nb+1 } else { if (m[b+1] as i64)!=12 { nb=nb+1 } else { if (m[b+2] as i64)!=20 { nb=nb+1 } } } i=i+1 }
43
44 // ---- isolated head-on collision: exact momentum + energy ----
45 init_impl(base)
46 i=0; while i<N { VX[i]=0; VY[i]=0; i=i+1 } // freeze the gas
47 PX[0]=7800; PY[0]=8000; VX[0]=120; VY[0]=0
48 PX[1]=8200; PY[1]=8000; VX[1]=0-120; VY[1]=0 // d=400 < 2R=480, head-on approaching
49 var pbx: i64=0; var pby: i64=0; i=0; while i<N { pbx=pbx+VX[i]; pby=pby+VY[i]; i=i+1 }
50 let eb: i64 = energy(base)
51 step_one_impl(base)
52 var pax: i64=0; var pay: i64=0; i=0; while i<N { pax=pax+VX[i]; pay=pay+VY[i]; i=i+1 }
53 let ea: i64 = energy(base)
54 var mom_ok: i64=0; if pax==pbx { if pay==pby { mom_ok=1 } }
55 let ecol_ok: i64 = (ea==eb) as i64 // head-on equal mass -> exact
56
57 g_w(" run: steps=1000 collisions="); g_n(col); g_w(" energy E0="); g_n(e0); g_w(" E1="); g_n(e1); g_w(" drift(permille)="); g_n(drift); g_w("\n")
58 g_w(" thermalization: speed min="); g_n(smin); g_w(" max="); g_n(smax); g_w(" spread="); g_n(spread); g_w(" (all started at 200)\n")
59 g_w(" head-on: momentum before=("); g_n(pbx); g_w(","); g_n(pby); g_w(") after=("); g_n(pax); g_w(","); g_n(pay); g_w(") E "); g_n(eb); g_w("->"); g_n(ea); g_w(" non-bg-px="); g_n(nb); g_w("\n")
60
61 var ran: i64=0; if col>50 { ran=1 }
62 g_row("RUNS + COLLISIONS: 1000 steps integrated and the gas underwent many elastic collisions (>50)" as *u8, ran, pass)
63 g_row("CONTAINED: every particle stayed inside the box (no escape/tunnelling) -- exact elastic walls" as *u8, allin, pass)
64 g_row("ENERGY CONSERVED: total kinetic energy drift < 3% over 1000 steps (elastic dynamics, integer collisions)" as *u8, (drift < 30) as i64, pass)
65 g_row("COLLISION EXACTNESS: an isolated head-on elastic collision conserves total momentum AND kinetic energy exactly" as *u8, mom_ok*ecol_ok, pass)
66 g_row("THERMALIZATION: from a single speed (200) a velocity DISTRIBUTION emerges (spread > 100) -- onset of Maxwell-Boltzmann" as *u8, (spread > 100) as i64, pass)
67 g_row("RASTER: framebuffer drawn (20 particles, speed-coloured) -> knowledge/nx_gassim.png" as *u8, (nb>100) as i64, pass)
68 var iface: i64=0; if ww()==W { if hh()==H { if fb_off()==O_FB { iface=1 } } }
69 g_row("WASM-READY: init/tick/render/ww/hh/fb_off interface intact -- the exact base-relative code wasm runs" as *u8, iface, pass)
70
71 g_w("NX-GASSIM-GATE rows=7 pass="); g_n(pass[0])
72 if pass[0]==7 { g_w(" verdict=GREEN (real kinetic-theory gas; energy-conserving, thermalizing, browser-ready)\n"); sys_exit(0); return 0 }
73 g_w(" verdict=RED\n"); sys_exit(1); return 1
74}