"""Reproduce the manuscript audit with Python 3 and its standard library.
Run: python3 reproduce_checks.py
Inputs are the printed values in the two supplied Word documents.
No raw experimental data are fitted. Uncertainty formulas assume independence
unless stated otherwise. All energies used in calculations are in eV.
"""
import math,json
hbar=6.582119569e-16;h=2*math.pi*hbar;kb=8.617333262145e-5;e=1.602176634e-19;eps0=8.8541878128e-12
w=1.13e-9;d=300e-9;l=58e-9;er=3.9;Ea=.152
B=e*math.log(4*d/w)/(2*math.pi*eps0*er)
result={'constants':dict(hbar_eVs=hbar,kB_eVK=kb,e_C=e,eps0_Fm=eps0,epsilon_r=er),'geometry':{'EC_length_eVnm':B*1e9}}
for v in [1e6,1.6e5]:
 A=math.pi*hbar*v
 result[str(v)]={'gap_eV':A/w,'delta_meV_at_58nm':A/l*1e3,'EC_meV_at_58nm':B/l*1e3,'Ea_meV_at_58nm':(A+B)/l*1e3,'length_nm_for_152meV':(A+B)/Ea*1e9,'g_paper_formula':(1+2*B/A)**(-.5),'LT_hbar_nm_180K':hbar*v/(kb*180)*1e9,'LT_h_nm_180K':h*v/(kb*180)*1e9,'T_cross_hbar_K':hbar*v/(kb*l),'T_cross_h_K':h*v/(kb*l)}
alpha=.63;s=.08
result['summary']={'kBT_295_meV':kb*295*1e3,'kBT_300_meV':kb*300*1e3,'Ea_over_kBT295':Ea/(kb*295),'temperature_ratio':295/.025,'Gamma_over_kB':.00018/kb,'bias1mV_over_kB_K':.001/kb,'v_inferred_gap':1.8*w/(math.pi*hbar),'v_needed_length':(Ea*l-B)/(math.pi*hbar),'eps_needed_length_v1e6': e*math.log(4*d/w)/(2*math.pi*eps0*(Ea*l-math.pi*hbar*1e6)),'C_geom_aF':e/(B/l)*1e18,'Cg_metallic_aF':e/4*1e18,'gate_lever_arm':Ea/4,'FWHM_thermal_slope_VK':4*math.acosh(math.sqrt(2))*kb/(Ea/4)}
result['g_alternatives']={}
for mode,b,ds in [('direct_end',alpha,s),('sequential',alpha+1,s),('CST',(alpha+1)/2,s/2)]:
 g=1/(1+4*b);result['g_alternatives'][mode]={'TDOS_exponent':b,'g_M4':g,'sigma_g':4*ds/(1+4*b)**2,'g_M2':1/(1+2*b)}
x=[[.31,.54,.32,.60,.57,.57,.36],[.51,.65,.54,.62,.65,.83,.45],[1.56,1.76,1.65,1.43,1.35,1.61,1.36]]
errs=[[.11,.09,.12,.11,.08,.13,.2],[.06,.08,.03,.07,.12,.06,.09],[.2,.2,.1,.3,.2,.2,.3]]
result['tableS1']=[]
for a,sig in zip(x,errs):
 n=len(a);mu=sum(a)/n;sd=math.sqrt(sum((t-mu)**2 for t in a)/(n-1));wt=[1/t**2 for t in sig];wm=sum(t*u for t,u in zip(a,wt))/sum(wt)
 result['tableS1'].append({'mean':mu,'sample_sd':sd,'sem_scatter':sd/math.sqrt(n),'error_mean_independent':math.sqrt(sum(t*t for t in sig))/n,'weighted_mean':wm,'error_weighted':1/math.sqrt(sum(wt))})
result['area_minus_height']=[{'difference':c-b-1,'sigma_independent':math.hypot(sc,sb)} for b,c,sb,sc in zip(x[1],x[2],errs[1],errs[2])]
result['summary']['four_mode_g_spacing_convention']=(1+4*B/(math.pi*hbar*1e6))**(-.5)
result['summary']['mean_first_six_short_range']=sum(x[1][:6])/6
result['summary']['ratio_to_Postma_120meV']=152/120
result['summary']['length_sigma_energy_only_nm']=result[str(1e6)]['length_nm_for_152meV']*13/152
result['summary']['gate_width_Gamma_V']=.00018/.038
result['summary']['gate_C_from_total_geometry_aF']=result['summary']['C_geom_aF']*.038

def simpson(f,lo,hi,n=20000):
    step=(hi-lo)/n
    return step/3*(f(lo)+f(hi)+sum((4 if i%2 else 2)*f(lo+i*step) for i in range(1,n)))
checks={}
# Physical normalization and the free-particle limit.
checks['thermal_kernel_normalized']=abs(simpson(lambda z: 1/(4*math.cosh(z/2)**2),-30,30)-1)<1e-10
checks['thermal_FWHM_half_height']=abs(1/math.cosh(math.acosh(math.sqrt(2)))**2-.5)<1e-14
checks['lorentzian_normalized_infinite_limit']=abs(2/math.pi*math.atan(2e8)-1)<1e-8
checks['free_flavor_exponents_zero']=all(abs((1/1-1)/m)<1e-14 and abs((1+1/1-2)/(2*m))<1e-14 for m in [1,2,4])
# Verify the corrected SI field transform and inverse for several K.
for K in [.2,.4,1,2]:
    phiL,phiR=.73,-.21
    plus=(phiL-phiR)/math.sqrt(2*K)
    minus=math.sqrt(K/2)*(phiL+phiR)
    checks['field_inverse_K_'+str(K)]=abs(minus/math.sqrt(2*K)+math.sqrt(K/2)*plus-phiL)<1e-14 and abs(minus/math.sqrt(2*K)-math.sqrt(K/2)*plus-phiR)<1e-14
# Directly integrate a family of broadened peaks; height exponent .63 and
# thermal width imply area exponent 1.63 regardless of microscopic origin.
def peak_area(T):
    return simpson(lambda z: T**.63/math.cosh(z/(2*T))**2,-30*T,30*T)
checks['area_scaling']=abs(math.log(peak_area(4)/peak_area(2))/math.log(2)-1.63)<1e-12
checks['h_vs_hbar_thermal_length']=abs(result[str(1.6e5)]['LT_h_nm_180K']/result[str(1.6e5)]['LT_hbar_nm_180K']-2*math.pi)<1e-12
result['verification']=checks
assert all(checks.values()), checks
print(json.dumps(result,indent=2))
