Test code for 1D

  • Test code for 1D

    In order to test the WENO5 method and the 5 point center difference method, we test follow simple case
    \[\begin{align}u_t&=u_{xx}+\sin(x),x\in [0,2\pi]\\u_x|_{x=0} &=\sin(0)\\u_x|_{x=2\pi} &=\sin(2\pi)\\\end{align}\]
    we treat \(u_x\) with 5 points center difference , no boundary condition, the outside use Dirichlet boundary condition.
  • Step1 . we use implicit scheme, RungeKutta-WENO5 method.
    The optimal third order TVD Runge-Kutta method is given by follow:
    \[ \begin{align} u_t &=L(u)\\ u_1 &=u^n+dt L(u^n)\\ u_2 &=\frac{3}{4}u^n+\frac{1}{4}u_1+\frac{1}{4}dtL(u_1)\\ u^{n+1} &=\frac{1}{3}u^n+\frac{2}{3}u_2+\frac{2}{3}dtL(u_2) \end{align}\]
    发现了一些而问题:WENO5 的Dirichlet 边界条件施加的时候,不需要插值,直接放置左右各两点即可, 启用 ‘smooth’ 模式比较好。内部五点中心差分不需要边界条件,对ghost points 使用 Lagrange5 插值即可。
function T76
N=100;
x=linspace(0,2*pi,N)';
h=x(2)-x(1);
D_BC=[cos(x(1)-2*h),cos(x(1)-h),cos(x(end)+h),cos(x(end)+2*h)];
N_BC=[sin(x(1)),sin(x(end))];
U1=sin(x);
U2=x.^2;
%u=[U1(1:5);U2(6:end-11);U1(end-10:end)];
u=cos(x)*0.1;
t=0;
dt=h^2;
t_end=20;
%============= Runge-Kutta =================
while t<t_end
    u1=u+dt*L(u);
    u2=3/4*u+1/4*u1+1/4*dt*L(u1);
    u=1/3*u+2/3*u2+2/3*dt*L(u2);
    t=t+dt;
     plot(x,u,x,sin(x),'r')
    title(['t=',num2str(t)])
    drawnow
end

   function y=L(u)
        %-----1.内部使用中心差分-------
         du=D1_5points(x,u,N_BC,'no');
        %-----2. 外部使用WENO5---------
         d2u_p=WENO5_1D(x,du,D_BC, 1,'D','smooth');
         d2u_m=WENO5_1D(x,du,D_BC,-1,'D','smooth');
         d2u=(d2u_p+d2u_m)/2;
         y=d2u+sin(x);
    end
end
知识兔
计算机