# Periodic boundary conditions with MethodOfLines not working

**URL:** <https://discourse.julialang.org/t/periodic-boundary-conditions-with-methodoflines-not-working/115123>\
**Category:** Numerics\
**Tags:** question, differentialequation, methodoflines\
**Created:** [June 3, 2024, 2:50pm UTC](https://discourse.julialang.org/t/periodic-boundary-conditions-with-methodoflines-not-working/115123 "2024-06-03T14:50:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dbox/32/209217_2.png) [@dbox](https://discourse.julialang.org/u/dbox)\
**Post date:** [June 3, 2024, 2:50pm UTC](https://discourse.julialang.org/t/periodic-boundary-conditions-with-methodoflines-not-working/115123/1 "2024-06-03T14:50:54Z")

</div>

I’m trying to use a basic advection/diffusion equation with MethodOfLines.jl. I have an initial condition which is a Gaussian shaped density, and I’m struggling to get periodic boundary conditions to work.

![image](https://global.discourse-cdn.com/julialang/original/3X/b/a/ba7840944f880ca08c7e046851cf385f2c0691d5.png)  
My initial state looks like this, so perhaps the left boundary is trying to take the value of the right? I have tried swapping the order in `bcs` but no luck.

Any help appreciated!

I get

```julia
 Warning: The system contains interface boundaries, which are not compatible with system transformation. The system will not be transformed. Please post an issue if you need this feature.

```

```julia
using ModelingToolkit
using DifferentialEquations
using MethodOfLines
using Revise
using DomainSets

@variables x t
@variables c(..)

∂t = Differential(t)
∂x = Differential(x)

# advection-diffusion constants
R = 1.0
D = 0.1
v = 1.0
cₒ = 1.0
x_min = 0.0
x_max = 1.0
t_min = 0.0
t_max = 1.0

c_normal(x) = exp(-(10*x)^2)

# advection-diffusion equation
eqn = R * ∂t(c(x, t)) ~ D * ∂x(c(x, t))^2 - v * ∂x(c(x, t))

# initial and boundary conditions
bcs = [c(x,t_min) ~ c_normal(x),
       c(x_min,t) ~ c(x_max,t)]

# space and time domains
dom = [x ∈ Interval(x_min, x_max), t ∈ Interval(t_min, t_max)]

# define PDE system
@named sys = PDESystem(eqn, bcs, dom, [x, t], [c(x, t)])

# convert the PDE into an ODE problem
prob = discretize(sys, MOLFiniteDifference([x => 100], t))#, advection_scheme=WENOScheme()))

# solve the problem
sol = solve(prob, Rosenbrock23(), saveat=0.2)

c = sol[c(x,t)]

using Plots

plot(xlim=(0,100),ylim=(0,10))
n = size(c, 2)
anim = @animate for t ∈ 1:n
    plot!(c[:,t])
end
gif(anim, "diff.gif", fps=1)

```

(edit: I have also tried adding the BC on dc/dx (although this is not specified in the Periodic BCs section of MethodOfLines) and the result is not correct still)

---

<div class="post-metadata">

**Author:** ![karlroyen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karlroyen/32/9301_2.png) [@karlroyen](https://discourse.julialang.org/u/karlroyen)\
**Post date:** [June 4, 2024, 7:37am UTC](https://discourse.julialang.org/t/periodic-boundary-conditions-with-methodoflines-not-working/115123/2 "2024-06-04T07:37:05Z")

</div>

I think your initial condition `c(x,t_min) ~ c_normal(x)` has to fulfill your boundary condition `c(x_min,t) ~ c(x_max,t)`. Your initial Gaussian around zero is not periodic between your boundaries of `x_min = 0.0` and `x_max = 1.0`
