code wiki / _hdl_build / nx_sim_validation_gate.nx

nx_sim_validation_gate.nx source

↩ module page · 78 lines · 5448 B

1// nx_sim_validation_gate.nx -- climb the VALIDATION axis with the ASME V&V 20 validation-metric METHODOLOGY: 2// comparison error E = |S - D|, validation uncertainty u_val = RSS(u_num, u_D), and the validated-within- 3// uncertainty test |E| <= k*u_val. Here S = our fixed-point LEAPFROG oscillator at time T; D = an INDEPENDENT 4// referent (the exact analytic x(t)=A cos t via our sovereign cos -- a different method than the integrator); 5// u_num = Richardson discretization-error estimate |x(dt) - x(dt/2)|; u_D = the referent's own error bound. 6// HONEST SCOPE (a gate row): the referent is an exact ANALYTIC solution, NOT a physical experiment, so this 7// demonstrates the V&V 20 validation METHODOLOGY against an independent referent -- it advances Validation to 8// BASIC, but full NASA-L4 / regulatory credibility still requires REAL-SYSTEM experimental data (the L4 ceiling). 9// LIAR-KILL: a wrong-physics model (wrong stiffness k=1.2) gives |E| >> u_val and is REJECTED. GREEN iff 6/6. 10// license_tier: ORIGINAL 11import "nx_syscalls.nx" 12 13func 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 } 14func 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 } 15func 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 } 16func iabs(x: i64) -> i64 { if x<0 { return 0-x } return x } 17func isqrt(N: i64) -> i64 { if N<2 { return N } var x: i64=N; var y: i64=(x+1)/2; while y<x { x=y; y=(x + N/x)/2 } return x } 18// sovereign cos referent (Taylor + range reduction, SC=1e6) 19func sin_fixed(Xin: i64, SC: i64) -> i64 { 20 let PI: i64=3141593; let TWO_PI: i64=6283185; let HALF: i64=1570796 21 var sign: i64=1; var X: i64=Xin 22 if X<0 { X=0-X; sign=0-1 } 23 X = X % TWO_PI 24 if X > PI { X = TWO_PI - X; sign = 0-sign } 25 if X > HALF { X = PI - X } 26 let x2: i64 = X*X/SC 27 var term: i64=X; var sum: i64=X; var k: i64=1 28 while k<=8 { term = (0-term)*x2/SC/((2*k)*(2*k+1)); sum=sum+term; k=k+1 } 29 return sum*sign 30} 31func cos_fixed(X: i64, SC: i64) -> i64 { return sin_fixed(X+1570796, SC) } 32// leapfrog oscillator with stiffness k = KK/KD (a = -(KK*x)/KD). x after N steps, dt=1/Dg. 33func lf_xT_k(A: i64, Dg: i64, N: i64, KK: i64, KD: i64) -> i64 { 34 var x: i64=A; var v: i64=0; var i: i64=0 35 while i<N { 36 let a: i64=(0 - KK*x)/KD 37 let vh: i64=v + a/(2*Dg) 38 x = x + vh/Dg 39 let a2: i64=(0 - KK*x)/KD 40 v = vh + a2/(2*Dg) 41 i=i+1 42 } 43 return x 44} 45 46func main() -> i64 { 47 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0 48 g_w("=== NX-SIM-VALIDATION GATE (ASME V&V 20 validation metric: E, u_val, |E|<=k*u_val) ===\n") 49 let SC: i64=1000000; let A: i64=1000000000; let Dg: i64=16 // T=1 -> N=Dg steps; positions scaled 1e9 50 // D = independent analytic referent x(1) = A cos(1) (A = 1000*SC -> 1000*cos_fixed) 51 let Dref: i64 = 1000*cos_fixed(1000000, SC) 52 // S = the model under test (correct physics, k=1) at two grids for Richardson u_num 53 let S1: i64 = lf_xT_k(A, Dg, Dg, 10, 10) 54 let S2: i64 = lf_xT_k(A, 2*Dg, 2*Dg, 10, 10) 55 let Ecorr: i64 = iabs(S1 - Dref) 56 let u_num: i64 = iabs(S1 - S2) // Richardson discretization-error estimate 57 let u_D: i64 = 4000 // referent error bound (cos ~2ppm of A=1e9 -> ~2000, x2 margin) 58 let u_val: i64 = isqrt(u_num*u_num + u_D*u_D) 59 // wrong-physics model: stiffness k=1.2 (KK=12,KD=10) -> wrong frequency -> large comparison error 60 let Sw: i64 = lf_xT_k(A, Dg, Dg, 12, 10) 61 let Ewrong: i64 = iabs(Sw - Dref) 62 63 let cos0: i64 = 1000*cos_fixed(0, SC) 64 g_w(" referent: A*cos(0)="); g_n(cos0); g_w(" (expect ~1e9) D=A*cos(1)="); g_n(Dref); g_w("\n") 65 g_w(" S(k=1)="); g_n(S1); g_w(" E=|S-D|="); g_n(Ecorr); g_w(" u_num="); g_n(u_num); g_w(" u_D="); g_n(u_D); g_w(" u_val="); g_n(u_val); g_w("\n") 66 g_w(" S(k=1.2 WRONG)="); g_n(Sw); g_w(" E_wrong="); g_n(Ewrong); g_w(" (ratio E_wrong/u_val="); g_n(Ewrong/u_val); g_w(")\n") 67 68 g_row("REFERENT (independent): analytic x(t)=A cos t via sovereign cos (a DIFFERENT method than leapfrog); cos(0)~A" as *u8, (iabs(cos0-A) < A/100) as i64, pass) 69 g_row("COMPARISON ERROR: E=|S-D| computed for the model vs the independent referent" as *u8, (Ecorr>0) as i64, pass) 70 g_row("VALIDATION UNCERTAINTY: u_val = RSS(u_num[Richardson], u_D[referent]) computed (V&V 20)" as *u8, (u_val>0) as i64, pass) 71 g_row("VALIDATED-WITHIN-UNCERTAINTY: correct-physics model agrees with referent, |E| <= 2*u_val" as *u8, (Ecorr <= 2*u_val) as i64, pass) 72 g_row("LIAR-KILL: a wrong-physics model (stiffness k=1.2) gives |E| >> u_val (>10x) -> validation REJECTS it" as *u8, (Ewrong > 10*u_val) as i64, pass) 73 g_row("HONEST SCOPE: referent is ANALYTIC not experimental -> V&V20 METHODOLOGY (Validation->BASIC); L4 still needs real-system data" as *u8, ((Ecorr<=2*u_val) as i64)*((Ewrong>10*u_val) as i64), pass) 74 75 g_w("NX-SIM-VALIDATION-GATE rows=6 pass="); g_n(pass[0]) 76 if pass[0]==6 { g_w(" verdict=GREEN (validation methodology vs independent referent; honest L4-needs-real-data)\n"); sys_exit(0); return 0 } 77 g_w(" verdict=RED\n"); sys_exit(1); return 1 78}