code wiki / _hdl_build / nx_pendulum_gate.nx

nx_pendulum_gate.nx source

↩ module page · 72 lines · 5366 B

1// nx_pendulum_gate.nx -- NATIVE verify of the browser pendulum (base = mmap; SAME code runs in sovereign WASM). 2// Measures the EMERGENT period from integrating the true nonlinear pendulum and validates the real, experimentally 3// established laws: (a) Galileo's T proportional to sqrt(L) [T(4L)/T(L)=2], (b) small-amplitude ISOCHRONISM 4// [period ~ independent of amplitude], (c) large-amplitude period GROWTH [+~18% at 90 deg, the exact-pendulum 5// value (2/pi)K(sin45)=1.18]. Liar-kill: the LINEAR approximation (-k theta) stays isochronous at 90 deg (no 6// growth) -- so the growth test distinguishes real nonlinear pendulum physics from the textbook linearization. 7// license_tier: ORIGINAL expect_exit: 0 8import "nx_syscalls.nx" 9import "nx_pendulum.nx" 10import "nx_png.nx" 11 12func 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 } 13func 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 } 14func 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 } 15func iabs(x: i64) -> i64 { if x<0 { return 0-x } return x } 16 17// integrate from (theta=amp, omega=0) and return the full period in steps (first omega neg->pos crossing = half) 18func measure_period(base: i64, gln: i64, amp: i64, lin: i64) -> i64 { 19 let TH: *i64=(base+O_TH) as *i64; let OM: *i64=(base+O_OM) as *i64; let GLN: *i64=(base+O_GLN) as *i64; let LIN: *i64=(base+O_LIN) as *i64 20 GLN[0]=gln; LIN[0]=lin; TH[0]=amp; OM[0]=0 21 step_one_impl(base) 22 var prev: i64=OM[0]; var step: i64=1 23 while step<2000000 { 24 step_one_impl(base) 25 let om: i64=OM[0] 26 if prev<0 { if om>=0 { return step*2 } } 27 prev=om; step=step+1 28 } 29 return 0 30} 31 32func main() -> i64 { 33 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0 34 g_w("=== NX-PENDULUM native verify (real nonlinear pendulum; SAME code runs in sovereign wasm) ===\n") 35 let base: i64 = sys_mmap(2*1048576) as i64 36 37 let T1: i64 = measure_period(base, 980, 87266, 0) // L=1m, 5deg, true 38 let T4: i64 = measure_period(base, 245, 87266, 0) // L=4m (g/L=2.45 -> GLN=245), 5deg, true 39 let T10: i64 = measure_period(base, 980, 174533, 0) // L=1m, 10deg, true 40 let T90: i64 = measure_period(base, 980, 1570796, 0) // L=1m, 90deg, true 41 let T5L: i64 = measure_period(base, 980, 87266, 1) // L=1m, 5deg, LINEAR 42 let T90L: i64 = measure_period(base, 980, 1570796, 1) // L=1m, 90deg, LINEAR 43 44 let sqrtL: i64 = T4*1000/T1 // expect 2000 (sqrt(4)=2) 45 let iso: i64 = iabs(T10-T1)*1000/T1 // expect ~0 46 let growth: i64 = T90*1000/T1 // expect ~1180 (exact pendulum +18%) 47 let lingrowth: i64= T90L*1000/T5L // expect ~1000 (linear is isochronous) 48 49 init_impl(base) 50 var t: i64=0; while t<60 { tick_impl(base, 0); t=t+1 } 51 render_impl(base) 52 write_png((base + O_FB) as *i64, W, H, "knowledge/nx_pendulum.png" as *u8) 53 let fb: *i64=(base+O_FB) as *i64; var nb: i64=0; var i: i64=0 54 while i<W*H { let b: i64=i*8; let m: *u8=(base+O_FB) as *u8; if (m[b] as i64)!=10 { 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 } 55 56 g_w(" periods(steps): T(L1,5d)="); g_n(T1); g_w(" T(L4,5d)="); g_n(T4); g_w(" T(L1,10d)="); g_n(T10); g_w(" T(L1,90d)="); g_n(T90); g_w("\n") 57 g_w(" sqrtL_ratio(x1000)="); g_n(sqrtL); g_w(" (2000) isochronism_diff(x1000)="); g_n(iso); g_w(" growth90(x1000)="); g_n(growth); g_w(" (~1180) linear_growth(x1000)="); g_n(lingrowth); g_w(" (~1000)\n") 58 59 var ran: i64=0; if T1>0 { if T90>0 { ran=1 } } 60 g_row("RUNS: the nonlinear pendulum integrated and an emergent period was measured at every config" as *u8, ran, pass) 61 g_row("GALILEO T~sqrt(L): T(4L)/T(L) = 2.0 within 3% (period scales as the square root of length)" as *u8, (iabs(sqrtL-2000) < 60) as i64, pass) 62 g_row("ISOCHRONISM: at small amplitude the period is ~independent of amplitude (5deg vs 10deg < 2%)" as *u8, (iso < 20) as i64, pass) 63 g_row("LARGE-ANGLE GROWTH: the period at 90deg exceeds the small-angle period by >10% (real nonlinear effect, ~18%)" as *u8, ((growth > 1100) as i64)*((growth < 1260) as i64), pass) 64 g_row("LIAR-KILL: the LINEAR approximation stays isochronous at 90deg (<5% growth) -> the growth test detects true nonlinear physics" as *u8, ((lingrowth < 1050) as i64)*((growth > 1100) as i64), pass) 65 g_row("RASTER: framebuffer drawn (pivot + rod + bob + trail) -> knowledge/nx_pendulum.png" as *u8, (nb>100) as i64, pass) 66 var iface: i64=0; if ww()==W { if hh()==H { if fb_off()==O_FB { iface=1 } } } 67 g_row("WASM-READY: init/tick/render/ww/hh/fb_off interface intact -- the exact base-relative code wasm runs" as *u8, iface, pass) 68 69 g_w("NX-PENDULUM-GATE rows=7 pass="); g_n(pass[0]) 70 if pass[0]==7 { g_w(" verdict=GREEN (real nonlinear pendulum; Galileo sqrt(L) + isochronism + large-angle growth, browser-ready)\n"); sys_exit(0); return 0 } 71 g_w(" verdict=RED\n"); sys_exit(1); return 1 72}