Skip to content

Periodic boundary setting #469

@vavrines

Description

@vavrines

Say solving the advection equation with periodic boundary condition

u_t+u_x=0, u(0,x)=sin(2πx), u(t,0)=u(t,1)

The following code works well

using NeuralPDE, ModelingToolkit, Plots, Flux, DiffEqFlux, GalacticOptim, Optim
using ModelingToolkit: Interval

@parameters t, x
@variables u(..)
∂t, ∂x = Differential(t), Differential(x)
eq = ∂t(u(t, x)) + ∂x(u(t, x)) ~ 0
domains = [t  Interval(0.0, 1.0), x  Interval(0.0, 1.0)]
bcs = [u(t, 0) ~ sin(-2π * t), u(t, 1) ~ u(t, 0), u(0, x) ~ sin(2π * x)]
dx = 0.05
chain = FastChain(FastDense(2, 16, Flux.σ), FastDense(16, 16, Flux.σ), FastDense(16, 1))
initθ = Float64.(DiffEqFlux.initial_params(chain))
discretization = PhysicsInformedNN(chain, GridTraining(dx); init_params = initθ)
@named pde_system = PDESystem(eq, bcs, domains, [t, x], [u(t, x)])
prob = discretize(pde_system, discretization)
cb = function (p, l)
    println("loss: $l")
    return false
end
opt = Optim.BFGS()
res = GalacticOptim.solve(prob, opt; cb = cb, maxiters = 1200)
phi = discretization.phi
sol0 = [phi([0, x], res.u)[1] for x in xs]
sol1 = [phi([0.5, x], res.u)[1] for x in xs]
plot(xs, sol0)
plot!(xs, sol1)

right

If I change the sentence bcs = [u(t, 0) ~ sin(-2π * t), u(t, 1) ~ u(t, 0), u(0, x) ~ sin(2π * x)] to a real periodic setting, e.g., bcs = [u(t, 0) ~ u(t, 1), u(t, 1) ~ u(t, 0), u(0, x) ~ sin(2π * x)], the result becomes
wrong

I'm using NeuralPDE v4.2.0.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions