/* G10-118, 0-degree normal-operation experimental PWM (2026-10-29). * Physical neutral-point prediction replaces the 113--117 artificial * integrated charge-error proxy B and its preprogrammed carrier phase target. * For equal capacitors C: C*d(VC1-VC2)/dt = -sum_O currents. * The solver's generic bucket variable is reused to represent * B_next = -(VC1-VC2) + Ts/C * I_np = -(VC1-VC2)_predicted. * This is RESET FROM MEASURED C1/C2 EACH CARRIER, not accumulated. * A hard |B_next|<=6 V constrains predicted PHYSICAL midpoint mismatch, * alongside the existing 20 A instantaneous NP-current deviation bound. * All 117 K/Q switching path bank and gate budgets are retained. No PLECS * success or 16-kHz hard-real-time execution is asserted. */ #ifndef G10_0DEG_118_PHYSICALNP_CORE_H #define G10_0DEG_118_PHYSICALNP_CORE_H #include #include #include #include "g10_0deg_107fix_paths_PLECS.h" #include "g10_dwell_npband_c99.c" #include "g10_112_bank.h" #include "g10_116_kq_pool.h" #define G10_TS_US 62.5 #define G10_START_S 0.2 #define G10_CNP 0.0015 #define G10_KNP 40.0 #define G10_NP_BAND_A 20.0 #define G10_NP_CAP_V 12.0 #define G10_NP_COST_BUCKET 0.02 #define G10_NP_COST_LOCAL 0.0 /* Research-only adaptive deviation bound (not a device current rating). */ static double g10_np_band_runtime_A=G10_NP_BAND_A; static int g10_ref_fallback_count=0; /* Diagnostic: negative predicted PHYSICAL midpoint delta, not accumulated B. */ static double g10_np_bucket_V=0.; static double g10_last_np_deviation_A=0.; /* Diagnostics only: summary of current carrier's two-pass minimax solve. */ static double g10_diag_primary_errV=0.; static double g10_diag_secondary_errV=-1.; static double g10_diag_third_errV=-1.; static int g10_diag_twohop_used=0; static int g10_diag_twohop_lp_attempts=0; static int g10_diag_twohop_src=-1; static int g10_diag_twohop_pos=-1; static int g10_diag_threehop_used=0; static int g10_diag_threehop_lp_attempts=0; static int g10_diag_fallback_used=0; static int g10_diag_fallback_attempted=0; /* State sequence of current carrier. Copy is finalized only after LP selection. */ static uint16_t g10_live_codes[12]={0}; static int g10_selected_src_cycle=-1; static int g10_selected_candidate_idx=-1; static void g10_decode(uint16_t code,int8_t s[8]){ for(int k=0;k<8;k++){s[k]=(int8_t)((code%3)-1);code/=3;} } static int g10_fourleg_ref(const double m3[3],double vp,double vn,double pole[4]){ double e[5]={0,1,0,0,0};double m[3];int nedge=2; for(int j=0;j<3;j++){ double z=m3[j];if(z>1)z=1;if(z< -1)z=-1;m[j]=z; double q=fabs(z);int duplicate=(q<1e-12 || q>1-1e-12); for(int k=0;k0&&e[j]mid)?1:((m[j]<-mid)?-1:0); s[3]=-(s[0]+s[1]+s[2]); if(s[3]<-1 || s[3]>1){ /* A rigid zero-sequence schedule can be unavailable for a non-zero sum of three modulation references. Construct physically consistent four-leg average pole refs instead of rejecting the reference before LP. */ double vabc[3],vneutral=0.; for(int j=0;j<3;j++){vabc[j]=m[j]*.5*(vp+vn);vneutral-=.25*vabc[j];} pole[3]=vneutral; for(int j=0;j<3;j++)pole[j]=vabc[j]+vneutral; for(int j=0;j<4;j++)if(pole[j] < -vn-1.e-6 || pole[j]>vp+1.e-6)return 0; g10_ref_fallback_count++; return 1; } for(int j=0;j<4;j++)pole[j]+=du*(s[j]==1?vp:s[j]==-1?-vn:0.); } return 1; } static int g10_calc_period_trial(const double inputs[16],int cycle,double duration_us[12],double *errv,double costBucket){ if(cycle<0 || cycle>=G10_NCYCLE)cycle=(cycle%G10_NCYCLE+G10_NCYCLE)%G10_NCYCLE; const double vp=inputs[6],vn=inputs[7];if(vp<1||vn<1)return -1; double outL[4],outR[4],refs[6]; if(!g10_fourleg_ref(inputs,vp,vn,outL))return -2; if(!g10_fourleg_ref(inputs+3,vp,vn,outR))return -3; for(int j=0;j<3;j++){refs[j]=outL[j]-outL[3];refs[j+3]=outR[j]-outR[3];} const int n=G10_nseg[cycle];const double bucketTarget=0.; /* Physical midpoint model: C*d(VC1-VC2)/dt = -NPcurrent. * The reused solver variable is negative predicted delta: * start = -delta + Ts/C * NPtarget, * next = start + Ts/C*(NPactual-NPtarget) = -delta + Ts/C*NPactual. * Thus abs(next)<=6 V directly bounds the predicted physical capacitor mismatch. */ const double NPstart=-(vp-vn)+(1./16000.)*G10_KNP*(vp-vn); double b[8]={1.,0,0,0,0,0,0,0}; for(int j=0;j<6;j++)b[j+1]=refs[j]; b[7]=G10_CNP*G10_KNP*(vp-vn); double bestScore=1.e100,bestDuration[12]={0},bestBucket=0,bestNP=0,bestErr=1.e100; int bestSource=-1,bestPos=-1,bestQ=-1; uint16_t bestAlt=0; for(int q=0;q1.e-3)continue; const double excess=total-n*min_us; if(excess<=0)continue; const double fact=(G10_TS_US-n*min_us)/excess; for(int i=0;idur[imax])imax=i; } dur[imax]+=G10_TS_US-sum; double np=0,maxErr=0,checkSum=0; for(int i=0;i1e-9)continue; if(fabs(np-b[7])>g10_np_band_runtime_A+0.01)continue; const double correctedBucket=NPstart+(1./16000.)/G10_CNP*(np-b[7]); if(fabs(correctedBucket)>G10_NP_CAP_V+0.001||fabs(trial_bucket-correctedBucket)>0.001)continue; for(int row=1;row<=6;row++){ double u=0; for(int i=0;imaxErr)maxErr=er; } if(!isfinite(maxErr)||maxErr>trial_err+0.05)continue; /* Same scalar objective as inside banded solver: voltage minimax + NP drift costs. */ const double score=maxErr+costBucket*fabs(correctedBucket-bucketTarget) +G10_NP_COST_LOCAL*fabs(np-b[7]); if(score1||b-a>1)return 0;if(++ch>2)return 0;} } return 1; } static void g10_116_gates(uint16_t code,uint8_t g[32]){ for(int j=0;j<8;j++){ int x=code%3;code/=3; g[4*j+0]=(x==2);g[4*j+1]=(x==1); g[4*j+2]=(x==1);g[4*j+3]=(x==0); } } static int g10_116_budget(uint16_t prev,const uint16_t p[12],int n){ uint8_t last[32],now[32];int up[32]={0},down[32]={0}; g10_116_gates(prev,last); for(int k=0;klast[j]){if(++up[j]>2)return 0;} if(now[j]2)return 0;} last[j]=now[j]; } } return 1; } /* Emergency search helper: exact same voltage and NP physics as the 115 * core, including post-solver normalization, 1us minimum and hard limits. */ static int g10_117_eval_path(const double inputs[16],const uint16_t path[12],int n, const double b[8],double Bprev,double d_out[12],double *e_out,double *B_out,double *np_out){ double vp=inputs[6],vn=inputs[7],A[8*12]={0}; for(int i=0;i1.e-3)return 0; double factor=(G10_TS_US-n)/(total-n); int imax=0;double sum=0.; for(int i=0;idur[imax])imax=i; } dur[imax]+=G10_TS_US-sum; double actualnp=0.,maxErr=0.; for(int i=0;imaxErr)maxErr=x; } double actualB=Bprev+(1./16000.)/G10_CNP*(actualnp-b[7]); if(fabs(actualB)>G10_NP_CAP_V+.001 || fabs(actualnp-b[7])>g10_np_band_runtime_A+.01 || maxErr>err+.05)return 0; *e_out=maxErr;*B_out=actualB;*np_out=actualnp-b[7]; for(int i=0;i0 && e=1800)break; } if(attempts>=1800)break; } g10_diag_twohop_lp_attempts=attempts; /* no further state changes if already enough voltage margin */ if(bestE>12.){ for(int q=0;q=3000)break; } path[pos]=old; if(attempts3>=3000)break; } if(attempts3>=3000)break; } } g10_diag_threehop_lp_attempts=attempts3; if(bestSrc<0)return -4; g10_np_bucket_V=bestB;g10_last_np_deviation_A=bestNp; g10_selected_src_cycle=bestSrc;g10_selected_candidate_idx=bestQ; g10_diag_twohop_src=bestSrc;g10_diag_twohop_pos=bestPos; for(int i=0;i~12.865 V. */ const double required=(fabs(delta)-G10_NP_CAP_V)*G10_CNP/(G10_TS_US*1.e-6) -G10_CNP*G10_KNP*fabs(delta); g10_np_band_runtime_A=fmax(G10_NP_BAND_A,fmin(30.0,required+0.40)); const double Bbefore=-delta+(1./16000.)*G10_KNP*delta; int first=g10_calc_period_trial(inputs,cycle,duration_us,errv,G10_NP_COST_BUCKET); if(first>0)g10_diag_primary_errV=*errv; if(first>0 && *errv<=18.0)return first; /* snapshot first valid result */ double bestE=first>0?*errv:1.e100,bestB=g10_np_bucket_V; double bestNP=g10_last_np_deviation_A,bestD[12]={0}; uint16_t bestCodes[12]={0};int bestSrc=g10_selected_src_cycle,bestQ=g10_selected_candidate_idx; for(int i=0;i<12;i++){bestD[i]=duration_us[i];bestCodes[i]=g10_live_codes[i];} g10_np_bucket_V=Bbefore; g10_diag_fallback_attempted=1; double d2[12]={0},e2=0.; int second=g10_calc_period_trial(inputs,cycle,d2,&e2,0.005); if(second>0){ g10_diag_secondary_errV=e2; if(e2+0.218.0){ double d3[12]={0},e3=0.;g10_np_bucket_V=Bbefore; int third=g10_116_twohop_trial(inputs,cycle,Bbefore,d3,&e3); if(third>0){ g10_diag_third_errV=e3; if(e3+0.1=1.e90)return first>0?first:-4; *errv=bestE;g10_np_bucket_V=bestB;g10_last_np_deviation_A=bestNP; g10_selected_src_cycle=bestSrc;g10_selected_candidate_idx=bestQ; for(int i=0;i<12;i++){duration_us[i]=bestD[i];g10_live_codes[i]=bestCodes[i];} return G10_nseg[(cycle%G10_NCYCLE+G10_NCYCLE)%G10_NCYCLE]; } #endif