# Converted from etahougwat.m - eta for Hougen-Watson kinetics (Langmuir-Hinshelwood)importnumpyasnpfromscipy.integrateimportsolve_ivpfrommiscimportsave_asciip1=1.p2=1.npts=200phivec=[0.1,10.,100.,1000.]nphi=len(phivec)results=Noneeps_sq=np.sqrt(np.finfo(float).eps)defhougensrt(t,x,p1,p2):ca=x[0];dcadr=x[1];s1=x[2];s2=x[3]return[dcadr,p1*p2*ca/(1+p2*ca),s2,p1*ca/(1+p2*ca)**2+p1*p2*s1/(1+p2*ca)**2]defmake_g(phi_target,p1,p2):defg(t,x):returnp2*x[0]-phi_targetg.terminal=Trueg.direction=0returngforjinrange(nphi):phi=phivec[j]p2vec=[1e-40,1e-10,.1,1.,10.,20.,30.,40.,50.,55.,60.,65.,70.,75.,80.,90.,95.,99.,99.5]ifphi>=100.:p2vec[0]=1e-20np2=len(p2vec)c0=1e-2*phiPhivec=np.zeros(np2)etavec=np.zeros(np2)foriinrange(np2):p2=p2vec[i]rstop=max(100.,100.*np.sqrt(1./p2))rsteps=np.concatenate([[0.],np.linspace(0.001,rstop,npts)])x0=np.array([c0,0.,0.,0.])ev=make_g(phi,p1,p2)sol=solve_ivp(lambdat,x:hougensrt(t,x,p1,p2),[rsteps[0],rsteps[-1]],x0,method='Radau',t_eval=rsteps,events=ev,rtol=eps_sq,atol=eps_sq)# Use event time/state if event fired; Octave's ode15s appends event# point to output, but scipy solve_ivp with t_eval does not.iflen(sol.t_events[0])>0:r1=sol.t_events[0][0]xf=sol.y_events[0][0]else:r1=sol.t[-1]xf=sol.y[:,-1]Phivec[i]=np.sqrt(p1*p2)*r1etavec[i]=((1+phi)*xf[1]*r1/xf[0]/Phivec[i]**2)Phiprimevec=phi/(1+phi)/np.sqrt(2*(phi-np.log(1+phi)))*Phivecblock=np.column_stack([Phivec,Phiprimevec,etavec])results=blockifresultsisNoneelsenp.column_stack([results,block])save_ascii('etahougwat.dat',results)