code wiki / _hdl_build / nx_solarsim_gate.nx
nx_solarsim_gate.nx source
↩ module page · 137 lines · 8729 B
1// nx_solarsim_gate.nx -- NATIVE verify of the browser solar-system sim (base = mmap; the SAME base-relative code
2// runs in the sovereign WASM). Proves it is REAL physics, not animation: each planet is leapfrog-integrated under
3// the Sun's gravity, and we (1) measure each EMERGENT orbital period (steps to close the orbit) and show the
4// ratios match the REAL measured planetary periods (Kepler T~a^1.5) within 3%, (2) confirm the inner-faster
5// ordering, (3) confirm orbits stay bounded (no spiral/escape) with energy conserved, (4) raster a frame to PNG.
6// This is the "video games are simulators / full real physics" deliverable, browser-ready. license_tier: ORIGINAL expect_exit: 0
7import "nx_syscalls.nx"
8import "nx_solarsim.nx"
9import "nx_png.nx"
10import "nx_gate_verdict.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 }
14// ★D001 MIGRATED BY HAND. nx_gate_dry_apply SKIPPED this gate -- "counter idiom not recognised" --
15// because it counted through a pass[0] pointer against a LITERAL denominator (rows=9). Rather than
16// take the allow_own_verdict escape, which ships a gate no judge can read, the rows are moved onto
17// the shared base class directly: gv_check both prints and counts, so declared == executed BY
18// CONSTRUCTION and the denominator can never drift out of step with the rows again. Adding a row now
19// moves the count by itself, where the hand-written literal would have printed 10/9.
20func g_row(id: *u8, ok: i64, ctr: *i64) -> i64 { return gv_check(id, ok, ctr) }
21func iabs(x: i64) -> i64 { if x<0 { return 0-x } return x }
22
23// measure planet p's emergent orbital period (steps to return to a y=0 up-crossing with x>0), others frozen
24func measure_period(base: i64, p: i64) -> i64 {
25 init_impl(base)
26 let X: *i64=(base+O_X) as *i64; let Y: *i64=(base+O_Y) as *i64
27 var prev: i64=Y[p]; var step: i64=0
28 while step<300000 {
29 step_one_impl(base, p)
30 let cy: i64=Y[p]
31 if prev<0 { if cy>=0 { if X[p]>0 { if step>10 { return step } } } }
32 prev=cy; step=step+1
33 }
34 return 0
35}
36
37func main() -> i64 {
38 let pass: *i64 = gv_ctr()
39 g_w("=== NX-SOLARSIM native verify (real N-body solar system; SAME code runs in sovereign wasm) ===\n")
40 let base: i64 = sys_mmap(2*1048576) as i64
41
42 // ---- emergent periods from the gravity integration ----
43 let T: *i64=sys_mmap(8*8) as *i64
44 var p: i64=0; while p<N { T[p]=measure_period(base, p); p=p+1 }
45 // real measured period ratios x1000 (T_planet / T_earth): from nx_sim_validation_real_gate's measured data
46 let rr: *i64=sys_mmap(8*8) as *i64
47 rr[0]=241; rr[1]=615; rr[2]=1000; rr[3]=1881; rr[4]=11860
48 let nmh: *i64=sys_mmap(8*8) as *i64
49 nmh[0]=("Mercury" as *u8) as i64; nmh[1]=("Venus " as *u8) as i64; nmh[2]=("Earth " as *u8) as i64; nmh[3]=("Mars " as *u8) as i64; nmh[4]=("Jupiter" as *u8) as i64
50
51 var kepler_ok: i64=1; var worst: i64=0
52 p=0
53 while p<N {
54 let mr: i64 = T[p]*1000/T[2] // measured ratio to Earth x1000
55 let err: i64 = iabs(mr - rr[p])
56 if err > rr[p]*3/100 { kepler_ok=0 } // 3% tolerance
57 if err*1000/rr[p] > worst { worst = err*1000/rr[p] }
58 g_w(" "); g_w((nmh[p] as *u8)); g_w(" emergent T(steps)="); g_n(T[p]); g_w(" ratio/Earth(x1000)="); g_n(mr); g_w(" real="); g_n(rr[p]); g_w("\n")
59 p=p+1
60 }
61 // ordering: strictly increasing periods (inner faster)
62 var ordered: i64=1; p=1; while p<N { if T[p] <= T[p-1] { ordered=0 } p=p+1 }
63
64 // ---- stability + energy over a full inner-system run ----
65 init_impl(base)
66 let e0: *i64=sys_mmap(8*8) as *i64; p=0; while p<N { e0[p]=energy_p(base,p); p=p+1 }
67 var t: i64=0; while t<300 { tick_impl(base, 0); t=t+1 }
68 let X: *i64=(base+O_X) as *i64; let Y: *i64=(base+O_Y) as *i64; let R0: *i64=(base+O_R0) as *i64
69 var bounded: i64=1; var econs: i64=1
70 p=0
71 while p<N {
72 let r: i64=isqrt(X[p]*X[p]+Y[p]*Y[p])
73 if r < R0[p]*85/100 { bounded=0 }
74 if r > R0[p]*115/100 { bounded=0 }
75 let ec: i64=energy_p(base,p); var ae0: i64=e0[p]; if ae0<0 { ae0=0-ae0 }
76 let drift: i64=iabs(ec-e0[p])*1000/ae0
77 if drift > 50 { econs=0 } // <5% energy drift
78 p=p+1
79 }
80
81 // ---- raster a frame ----
82 render_impl(base)
83 write_png((base + O_FB) as *i64, W, H, "knowledge/nx_solarsim.png" as *u8)
84 let fb: *i64=(base+O_FB) as *i64; let sun: i64=fb[CY*W+CX]
85 g_w(" worst Kepler ratio error="); g_n(worst); g_w("/1000 bounded="); g_n(bounded); g_w(" energy-conserved="); g_n(econs); g_w(" -> knowledge/nx_solarsim.png\n")
86
87 var ran: i64=0; if T[0]>0 { if T[4]>0 { ran=1 } }
88 g_row("SIM RUNS: all 5 planets leapfrog-integrated under the Sun's gravity and completed orbits" as *u8, ran, pass)
89 g_row("STABLE ORBITS: every planet's radius stays within 15% of its true semi-major axis (no spiral/escape) over a full inner-system run" as *u8, bounded, pass)
90 g_row("ENERGY CONSERVED: symplectic leapfrog keeps each planet's orbital energy bounded (drift < 5%)" as *u8, econs, pass)
91 g_row("EMERGENT KEPLER: the periods EMERGE from the gravity integration and match the REAL measured planetary periods within 3% (T~a^1.5)" as *u8, kepler_ok, pass)
92 g_row("INNER-FASTER ORDERING: T(Mercury)<Venus<Earth<Mars<Jupiter -- the real Kepler ordering, from physics not script" as *u8, ordered, pass)
93 g_row("RASTER: the SUN is drawn at frame centre (this row tests the Sun ONLY -- see the two below)" as *u8, ((sun & 255)>200) as i64, pass)
94
95 // ★THE ROW ABOVE USED TO CLAIM "Sun + planets" WHILE TESTING ONE PIXEL AT THE CENTRE -- the Sun.
96 // Every planet could have been missing from the frame and it passed, because the Sun is drawn
97 // before the planet loop and never depends on it. Found 2026-08-14 by nx_vacuity_census; the same
98 // label-promises-more-than-the-predicate-checks class as six sibling game gates fixed that day.
99 // The planet screen mapping is taken from render_impl itself rather than re-derived:
100 // bpx = CX + X[p]*RPX[p]/R0[p] bpy = CY - Y[p]*RPX[p]/R0[p]
101 // and the expected colour comes from the engine's own color_of(p), so this names no literal and
102 // cannot drift if the palette moves. EVERY planet is checked, never a sample, and the counts are
103 // printed against N so a tooth that silently examined nothing cannot read as a pass.
104 let RPXg: *i64=(base+O_RPX) as *i64
105 var drawn: i64=0; var checked: i64=0
106 var offdiff: i64=0; var offchecked: i64=0
107 var pq: i64=0
108 while pq<N {
109 let bpx: i64 = CX + X[pq]*RPXg[pq]/R0[pq]
110 let bpy: i64 = CY - Y[pq]*RPXg[pq]/R0[pq]
111 if bpx>=0 { if bpx<W { if bpy>=0 { if bpy<H {
112 checked = checked + 1
113 if fb[bpy*W + bpx] == color_of(pq) { drawn = drawn + 1 }
114 } } } }
115 // neg-control point: clear of this planet's own disc, so it must NOT be its colour
116 let opx: i64 = bpx + radius_of(pq)*4 + 6
117 if opx>=0 { if opx<W { if bpy>=0 { if bpy<H {
118 offchecked = offchecked + 1
119 if fb[bpy*W + opx] != color_of(pq) { offdiff = offdiff + 1 }
120 } } } }
121 pq = pq + 1
122 }
123 g_w(" planets drawn="); g_n(drawn); g_w("/"); g_n(checked); g_w(" (of N="); g_n(N)
124 g_w(") neg-control off-disc differs="); g_n(offdiff); g_w("/"); g_n(offchecked); g_w("\n")
125 var planets_ok: i64=0
126 if checked==N { if drawn==N { planets_ok=1 } }
127 g_row("PLANETS IN FRAME: every one of the N planets is drawn at the screen position render_impl computes, in its own colour -- checked against N so an empty scan cannot pass" as *u8, planets_ok, pass)
128 var offctl_ok: i64=0
129 if offchecked==N { if offdiff==N { offctl_ok=1 } }
130 g_row("neg-control-off-disc: a point clear of each planet's disc is NOT that planet's colour, so the row above measures POSITION and not merely the presence of a colour somewhere" as *u8, offctl_ok, pass)
131 var iface: i64=0; if ww()==W { if hh()==H { if fb_off()==O_FB { iface=1 } } }
132 g_row("WASM-READY: init/tick/render/ww/hh/fb_off interface intact -- the exact base-relative code wasm runs" as *u8, iface, pass)
133
134 let rc: i64 = gv_verdict("SOLARSIM-GATE" as *u8, pass, "real N-body solar system; emergent Kepler periods match real measured planetary data; every planet verified PRESENT in the rendered frame" as *u8)
135 sys_exit(rc)
136 return rc
137}