Cumulative distribution for 2 A \mathrel {\mkern 4mu}\mathrel {\smash {\mathchar "392D}}\mathrel {\mkern -2.5mu}-> B at t=1 with n_0=500, \Omega =500. Discrete master equation (steps) versus omega expansion (smooth).
importnumpyasnpfromscipy.linalgimportexpm,solve_bandedfromscipy.specialimporterfcfromscipy.sparseimportdiagsimportmatplotlib.pyplotaspltfrommiscimportcollocfromscikits.odesimportdae# Parametersomega=500k=1c0=1n0=int(c0*omega)b=3ncol=25;x,A,B,q=colloc(ncol-1,left=True)x=b*x;A=A/b;B=B/(b*b);q=q*b;defcontinuous(t,F,Fdot,resid):"""Residual function for the DAE system."""c=1/(1/c0+k*t)v=-2*k*c*xD=k*c*cFx=A@FdFdt=-v*Fx+D*B@Fresid[0:]=Fdot-dFdt# resid = Fdot - dFdtresid[0]=F[0]-0.5resid[-1]=F[-1]-1# Time parameterststop=1nts=25tsteps=np.linspace(0,tstop,nts)var=1e-2# Initial conditionsy0=0.5*erfc(-x/(np.sqrt(2)*5*var))ydot0=np.zeros(ncol)resid=np.zeros(ncol)continuous(0,y0,ydot0,resid)ydot0=-residydot0[0]=0solver=dae('ida',continuous,compute_initcond='yp0',first_step_size=1e-6,atol=1e-6,rtol=1e-6,algebraic_vars_idx=[0,-1],old_api=False)solution=solver.solve(tsteps,y0,ydot0)y=solution.values.y# Final adjustmentscstop=1/(1/c0+k*tstop)xshift=np.concatenate([-np.flipud(x),x])/np.sqrt(omega)+cstopF=np.concatenate([1-np.flipud(y[-1,:]),y[-1,:]])# Stochastic simulationn=np.arange(n0+1)d1=-k*n*(n-1)/(2*omega)d2=k*(n+2)*(n+1)/(2*omega)A=diags([d1,d2[:-2]],[0,2]).toarray()P0=np.zeros_like(n)P0[-1]=1P=expm(A*tstop)@P0Fmas=np.cumsum(P)nscal=n/omega# Plottingplt.figure()plt.plot(nscal,Fmas,label='Master Equation')plt.plot(xshift,F,label='PDE')plt.axis([0.4,0.6,0,1])plt.legend()plt.figure()plt.plot(nscal,Fmas,label='Master Equation')plt.plot(xshift,F,label='PDE')plt.legend()plt.show(block=False)# Save resultswithopen("F2nd.dat","w")asf:np.savetxt(f,np.vstack((nscal,Fmas)).T,fmt='%f',header="Master Equation")f.write("\n\n")np.savetxt(f,np.vstack((xshift,F)).T,fmt='%f',header="PDE")