Hi
I am trying to do the time depend problem by explicit RK4 method and starting by 1D advection equation in periodic UnitInterval. but it is very unstable even if I try very tiny time step size to full fill CFL number limitation. actually I tried to do it manually in matlab, which is very stable while I choose the same mesh size and time step size.
here is code:
from dolfin import *
mesh = UnitInterval(128)
V = FunctionSpace(mesh,'CG',1)
c0=Expression('exp(-pow((x[0]-0.5),2)/0.025)')
# Sub domain for Periodic boundary condition
class PeriodicBoundary(SubDomain):
def inside(self, x, on_boundary):
return bool(x[0] < DOLFIN_EPS and x[0] > -DOLFIN_EPS and on_boundary)
def map(self, x, y):
y[0] = x[0] - 1.0
# Create periodic boundary condition
pbc = PeriodicBoundary()
bc = PeriodicBC(V, pbc)
c=TrialFunction(V)
v=TestFunction(V)
a_K=-nabla_grad(v)*c*dx
# a_K=-nabla_grad(c)*v*dx
a_M=c*v*dx
M=assemble(a_M)
bc.apply(M)
K=assemble(a_K)
c=Function(V)
c_1=interpolate(c0,V)
c.assign(c_1)
k=[]
for i in range(4):
k.append(Function(V))
T = 4
t = 0
dt =0.001
while t<=T:
b1=K*c.vector()
bc.apply(b1)
solve(M,k[0].vector(),b1)
b2=K*(c.vector()+0.5*dt*k[0].vector())
bc.apply(b2)
solve(M,k[1].vector(),b2)
b3=K*(c.vector()+0.5*dt*k[1].vector())
bc.apply(b2)
solve(M,k[2].vector(),b3)
b4=K*(c.vector()+dt*k[2].vector())
bc.apply(b2)
solve(M,k[3].vector(),b4)
c.vector()[:]= c.vector()+dt*(k[0].vector()+ 2 * k[1].vector() + 2 * k[2].vector() +k[3].vector())/6.0
t += dt
plot(c,rescale=False)