code wiki / _hdl_build / nx_research_chem_gate.nx

nx_research_chem_gate.nx source

↩ module page · 66 lines · 4575 B

1// nx_research_chem_gate.nx -- the research-engine reproducibility loop in a SECOND domain: CHEMISTRY (operator: 2// "discover new capabilities through different math/chemistry/physics via simulations... s class exceed on 3// research AND reproducibility"). The TESTING-TEAM computes molar masses of well-known molecules FROM the 4// foundationed atomic-weight data (Element.mass_q3 = IUPAC atomic weight x1000, validated against the 5// foundationed CIAAW seg_store by nx_ciaaw_gate) and REPRODUCES the textbook published molar masses to the 6// milli-AMU. The VERIFIER liar-kill: a WRONG formula (water as HO instead of H2O) is REJECTED. Pure integer 7// fixed-point (q3, no float), reproducible. Demonstrates the substrate for sovereign stoichiometry / 8// chemistry simulation. GREEN iff 6/6. license_tier: ORIGINAL 9import "nx_syscalls.nx" 10import "nx_chem_periodic.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_q3(v: i64) -> i64 { g_n(v / 1000); g_w("."); let f: i64 = v % 1000; if f < 100 { g_w("0") } if f < 10 { g_w("0") } g_n(f); 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 } 17 18// atomic weight x1000 (q3, milli-AMU) of element Z, from the foundationed periodic table. 19func el_q3(arr: *Element, z: i64) -> i64 { 20 let e: *Element = ((arr as i64) + (z-1) * NX_ELEM_BYTES) as *Element 21 return e.mass_q3 22} 23 24func main() -> i64 { 25 let pass: *i64 = sys_mmap(8) as *i64; pass[0] = 0 26 g_w("=== NX-RESEARCH-CHEM GATE (reproduce textbook molar masses from foundationed atomic weights) ===\n") 27 28 let arr: *Element = nx_chem_periodic_table_118() 29 let H: i64 = el_q3(arr,1); let C: i64 = el_q3(arr,6); let O: i64 = el_q3(arr,8) 30 let Na: i64 = el_q3(arr,11); let S: i64 = el_q3(arr,16); let Cl: i64 = el_q3(arr,17) 31 32 // molar masses computed from the foundationed atomic weights (q3 = milli-g/mol) 33 let h2o: i64 = 2*H + O 34 let co2: i64 = C + 2*O 35 let nacl: i64 = Na + Cl 36 let ch4: i64 = C + 4*H 37 let glucose: i64 = 6*C + 12*H + 6*O 38 let h2so4: i64 = 2*H + S + 4*O 39 let bad_h2o: i64 = H + O // WRONG: "HO" -- the liar-kill candidate 40 41 // textbook published molar masses (g/mol, x1000) -- independent references we must reproduce 42 let k_h2o: i64 = 18015; let k_co2: i64 = 44009; let k_nacl: i64 = 58443 43 let k_ch4: i64 = 16043; let k_glu: i64 = 180156; let k_h2so4: i64 = 98079 44 let tol: i64 = 2 // +/-2 milli-AMU rounding 45 46 g_w(" atomic weights (foundationed): H="); g_q3(H); g_w(" C="); g_q3(C); g_w(" O="); g_q3(O); g_w(" Na="); g_q3(Na); g_w(" S="); g_q3(S); g_w(" Cl="); g_q3(Cl); g_w("\n") 47 g_w(" H2O ours="); g_q3(h2o); g_w(" known="); g_q3(k_h2o); g_w("\n") 48 g_w(" CO2 ours="); g_q3(co2); g_w(" known="); g_q3(k_co2); g_w("\n") 49 g_w(" NaCl ours="); g_q3(nacl); g_w(" known="); g_q3(k_nacl); g_w("\n") 50 g_w(" CH4 ours="); g_q3(ch4); g_w(" known="); g_q3(k_ch4); g_w("\n") 51 g_w(" C6H12O6 ours="); g_q3(glucose); g_w(" known="); g_q3(k_glu); g_w("\n") 52 g_w(" H2SO4 ours="); g_q3(h2so4); g_w(" known="); g_q3(k_h2so4); g_w("\n") 53 54 g_row("foundationed atomic-weight table loaded (H=1.008, C=12.011, O=15.999)" as *u8, ((H==1008) & (C==12011) & (O==15999)) as i64, pass) 55 var m2: i64=0; if iabs(h2o-k_h2o)<=tol { if iabs(co2-k_co2)<=tol { m2=1 } } 56 g_row("reproduce H2O (18.015) + CO2 (44.009) to the milli-AMU" as *u8, m2, pass) 57 var m3: i64=0; if iabs(nacl-k_nacl)<=tol { if iabs(ch4-k_ch4)<=tol { m3=1 } } 58 g_row("reproduce NaCl (58.443, ionic) + CH4 (16.043, organic)" as *u8, m3, pass) 59 g_row("reproduce C6H12O6 glucose (180.156, 24 atoms)" as *u8, (iabs(glucose-k_glu)<=tol) as i64, pass) 60 g_row("reproduce H2SO4 (98.079, multi-element)" as *u8, (iabs(h2so4-k_h2so4)<=tol) as i64, pass) 61 g_row("LIAR-KILL: a WRONG formula (HO, not H2O) is REJECTED" as *u8, (iabs(bad_h2o-k_h2o) > tol) as i64, pass) 62 63 g_w("RESEARCH-CHEM-GATE rows=6 pass="); g_n(pass[0]) 64 if pass[0] == 6 { g_w(" verdict=GREEN\n"); sys_exit(0); return 0 } 65 g_w(" verdict=RED\n"); sys_exit(1); return 1 66}