# Discontinuous boundary condition in NDSolve

Assume we have any PDE to be solved on the rectangular domain $0<X<4$ and $0<y<2$ How do we tell Mathematica to impose the following boundary conditions?

$\cases{ U[0,y,t]=0,& if$0<y<1$,\cr U[0,y,t]=1,& if$1<y<2$,\cr}$

$U[x,2,t]=1$

and zero for the rest of the boundary.

I have the following code which runs but does not return a result:

NDSolve[{D[u[x, y, t], t] == (1/500)*(D[u[x, y, t], x, x]+D[u[x, y, t], y, y]),
u[x, y, 0] == 0,
DirichletCondition[u[x, y, t] == 0,x==0 && 0 <= y <= 1],
DirichletCondition[u[x, y, t] == 1, x == 0 && 1 <= y <= 2],
DirichletCondition[u[x, y, t] == 0, x == 4],
DirichletCondition[u[x, y, t]==1, y == 2],
DirichletCondition[u[x, y, t] == 1, y == 0]},
u, {x, 0, 4}, {y,0, 2}, {t, 0, 2000}]


When you see the warning

NDSolve::femcscd: The PDE is convection dominated and the result may not be stable. Adding artificial diffusion may help.


This may indicate the NDSolve did try to solve this as a purely spatial problem. You can force it so use the MethodOfLines via the option Method -> "MethodOfLines" for time integration. This then solves in seconds.

s = NDSolve[{D[u[x, y, t],
t] == (1/500)*(D[u[x, y, t], x, x] + D[u[x, y, t], y, y]),
u[x, y, 0] == 0,
DirichletCondition[u[x, y, t] == 0, x == 0 && 0 <= y <= 1],
DirichletCondition[u[x, y, t] == 1, x == 0 && 1 <= y <= 2],
DirichletCondition[u[x, y, t] == 0, x == 4],
DirichletCondition[u[x, y, t] == 1, y == 2],
DirichletCondition[u[x, y, t] == 1, y == 0]}, u, {x, 0, 4}, {y, 0,
2}, {t, 0, 2000}, Method -> "MethodOfLines"]


Plotting the solution at t=2000:

Plot3D[u[x, y, 2000] /. s, {x, 0, 4}, {y, 0, 2}, PlotRange -> All,
AxesLabel -> {x, y, u}] The code in the question produces results, but running to t = 2000 takes a while. Here is the result for t = 100, which took about seven minutes on my computer. Running to t = 2000 could take a few hours, and at some point memory usage could become an issue.

Plot3D[u[x, y, 100] /. s, {x, 0, 4}, {y, 0, 2}, PlotRange -> All,
AxesLabel -> {x, y, u}] Note that NDSolve gives the warning,

NDSolve::femcscd: The PDE is convection dominated and the result may not be stable. Adding artificial diffusion may help. >>


Although no instability is obvious in the plot, one might develop at later time. Note, also, that discontinuities in the boundary conditions may not be well resolved on the grid. However, improving resolution will increase run time.