# Converted from find_time_index.m - helper function for lc_reducedimportnumpyasnpdeffind_time_index(t,now):"""Return (index, is_switching_time) for the last t[index] <= now. t must be sorted with no duplicates; now must be >= t[0]. """t=np.asarray(t)ifnow<t[0]:raiseValueError(f'find_time_index: now={now} out of range [{t[0]}, {t[-1]}]')ifnow>t[-1]:index=len(t)-1else:index=int(np.max(np.where(t<=now)[0]))is_switching_time=bool(t[index]==now)returnindex,is_switching_time
# Converted from lc_reduced.m - reactor LC prediction with reduced model# [depends] flow.dat lc.dat find_time_index.py# This py-file loads data file 'flow.dat'.# This py-file loads data file 'lc.dat'.importnumpyasnpfromscipy.integrateimportsolve_ivpfrommiscimportgnuplotsavefromfind_time_indeximportfind_time_indexvrdiclo=2.3497k1k2ratio=2.0vro=2370.0teaf=0.00721teaden=0.728flow=np.loadtxt('flow.dat')lc=np.loadtxt('lc.dat')tflow=np.concatenate([[0.0],flow[:,0]])flowrate=np.concatenate([[0.0],flow[:,1]/teaden])tlc=lc[:,0]lcmeas=lc[:,1]ntimes=80tlin=np.linspace(0,tflow[-1],ntimes)timeout=np.unique(np.concatenate([tlin,tflow,tlc]))timeout=np.sort(timeout)idx=np.array([int(np.where(timeout==t)[0][0])fortintlc])assertnp.allclose(timeout[idx],tlc),'index extraction failed'defres_reduced(time,x,vrdiclo_,k1k2ratio_):vr,eps2=xifvr<=0.0:raiseValueError(f'vr is zero at time={time}')badded=(vr-vro)*teafeps1=badded-eps2index,_=find_time_index(tflow,time)fteaf=flowrate[index]*teafdvr_dt=flowrate[index]ifbadded==0.0:deps2_dt=0.0else:deps2_dt=fteaf/(1.0+k1k2ratio_*(vrdiclo_-(badded-eps2))/(badded-2.0*eps2))return[dvr_dt,deps2_dt]x0=[vro,0.0]ks=[0.5,1.0,2.0,8.0,32.0]nks=len(ks)lcpred=np.zeros((len(timeout),nks))fori,k12inenumerate(ks):sol=solve_ivp(res_reduced,[timeout[0],timeout[-1]],x0,args=(vrdiclo,k12),t_eval=timeout,method='Radau',rtol=1.5e-8,atol=1.5e-8,dense_output=False)vr=sol.y[0]eps2=sol.y[1]badded=(vr-vro)*teafeps1=badded-eps2cc=(eps1-eps2)/vrccd=eps2/vrwithnp.errstate(invalid='ignore',divide='ignore'):lcp=np.where((cc+2*ccd)>0,cc/(cc+2*ccd),1.0)# First index where flowrate is positive (0-indexed); before this,# no TEA has been added so cc=ccd=0 and the lc ratio is 0/0.firstposflow=int(np.min(np.where(flowrate>0)[0]))lcp[:firstposflow+1]=1.0lcpred[:,i]=lcptable=np.column_stack([timeout,lcpred])data={'lc':lc,'table':table}gnuplotsave('lc_reduced.dat',data)