# Julia PINN converged but bcs are not fulfilled

**URL:** <https://discourse.julialang.org/t/julia-pinn-converged-but-bcs-are-not-fulfilled/92351>\
**Category:** Machine Learning\
**Tags:** pde, neural-network\
**Created:** [December 31, 2022, 8:55am UTC](https://discourse.julialang.org/t/julia-pinn-converged-but-bcs-are-not-fulfilled/92351 "2022-12-31T08:55:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Post date:** [December 31, 2022, 8:55am UTC](https://discourse.julialang.org/t/julia-pinn-converged-but-bcs-are-not-fulfilled/92351/1 "2022-12-31T08:55:41Z")

</div>

Hi everyone,  
I am trying to solve a PDE with the julia package Neural.jl.  
I have 3 bcs. At two boundarys the pressure has to be zero. The last bsc ist a periodic boundary condition. The pressure at the one side has to be the same as the pressure at the other side.  
My final loss is (depending on the training strategie) around 1e-07 for grid-training and 1e-11 for quadraturTraining.  
But the periodic bcs is not fullfullid at all. Differences around (0.5 for quadraturTraining and 0.04 for gridtraining.

The code I am using:

```julia
using NeuralPDE, Lux, Optimization, OptimizationOptimJL, Plots
import ModelingToolkit: Interval

function main()
    @parameters x y
    @variables p(..)
    Dx = Differential(x)
    Dy = Differential(y)
    Dxx = Differential(x)^2

    # Bearing
    ω = 600.0 # [rad/s]
    D = 18e-03 # [m]
    B = 10e-03
    psi = 3.58e-03
    eta = 6.4e-03
    R = D/2
    
    

    C = psi * D/2
    u = sign(ω*D/2);

    p_fak = eta * abs(ω*R) * R/(C^2)
    # 2D PDE+
    eta_nd = 1
    x0 = -1e-05
    y0 = 1e-05
    phi = atan(x0,y0)
    e = sqrt(x0^2 + y0^2)
    H_spalt(x,y) = (C + e*cos(pi/2 + phi + x))/C

    eq = [Dx(H_spalt(x,y)^3/12 * Dx(p(x,y))) + Dy(H_spalt(x,y)^3/12 * Dy(p(x,y))) ~ Dx(H_spalt(x,y))]

    
    # Boundary conditions
    bcs = [p(x,0) ~ 0, # pressure at the left and rigth side
        p(x,B/R) ~ 0,
        p(0,y) ~ p(2*pi,y)] # periodic boundary condition
    
    # Space and time domains
    domains = [x ∈ Interval(0.0,2*pi),
            y ∈ Interval(0.0,B/R)]

    # Neural network
    dim = 2 # number of dimensions
    
    n_neurons = 16
    af = Lux.tanh
    chain = Lux.Chain(Dense(dim,n_neurons,af),
        Dense(n_neurons,n_neurons,af),
        Dense(n_neurons,1))

    # Discretization
    nx = 60
    ny = 40
    dx = pi*D/nx/R
    dy = B/(ny-1)/R

    strategy = GridTraining([dx,dy])

    strategy = QuadratureTraining(batch = 2000)
    discretization = PhysicsInformedNN(chain,strategy,
    adaptiv_loss = GradientScaleAdaptiveLoss(1))

    @named pde_system = PDESystem(eq,bcs,domains,[x,y],[p(x, y)])
    prob = discretize(pde_system,discretization)

    #Optimizer
    opt = OptimizationOptimJL.BFGS()

    #Callback function
    callback = function (p,l)
        println("Current loss is: $l")
        return false
    end

    @time res = 
    Optimization.solve(prob, opt, callback = callback, maxiters=1000)
    phi = discretization.phi

    return domains, res, phi, dx, dy, p_fak
end

function post_process(domains,res,phi,dx ,dy, p_fak)

    # Bearing
    D = 18e-03 # [m]
    B = 10e-03
    R = D/2

    xs = collect(0:dx:2*pi)
    ys = collect(0:dy:B/R)

    u_predict = zeros(length(xs),length(ys))
    for i = eachindex(xs)
        for j = eachindex(ys)
            u_predict[i,j] = first(phi([xs[i],ys[j]],res.u))*1
            if u_predict[i,j] < 0
                u_predict[i,j] = 0
            end
        end
    end

    plotly()
    p = plot(ys,xs,u_predict,seriestype=:surface, camera=(70, 30),size = (800, 600))
    display(p)

    return u_predict
end

domains, res, phi, dx, dy, p_fak = main()
u_predict = post_process(domains,res,phi, dx, dy, p_fak)

```

And the result:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/5/156f8caa5c1173f7c931915c049ec705ee2c8319.png)  
Thanks for your help and a happy new year:)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 31, 2022, 1:32pm UTC](https://discourse.julialang.org/t/julia-pinn-converged-but-bcs-are-not-fulfilled/92351/2 "2022-12-31T13:32:19Z")

</div>

What happens if you use the constrained mode?

---

<div class="post-metadata">

**Author:** ![MilkshakeForReal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milkshakeforreal/32/32861_2.png) [@MilkshakeForReal](https://discourse.julialang.org/u/MilkshakeForReal)\
**Post date:** [January 3, 2023, 4:26pm UTC](https://discourse.julialang.org/t/julia-pinn-converged-but-bcs-are-not-fulfilled/92351/3 "2023-01-03T16:26:06Z")

</div>

NeuralPDE.jl does not properly support periodic boundary conditions, see this [issue](https://github.com/SciML/NeuralPDE.jl/issues/469). Perhaps this should be made explicit in the documentation.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 3, 2023, 7:12pm UTC](https://discourse.julialang.org/t/julia-pinn-converged-but-bcs-are-not-fulfilled/92351/4 "2023-01-03T19:12:15Z")

</div>

> [@MilkshakeForReal](#):
>
> NeuralPDE.jl does not properly support periodic boundary conditions, see this [issue](https://github.com/SciML/NeuralPDE.jl/issues/469)

Ahh I forgot about that.

> [@MilkshakeForReal](#):
>
> Perhaps this should be made explicit in the documentation.

Rather than documentation, we should probably throw an error for now.

---

<div class="post-metadata">

**Author:** ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Post date:** [January 3, 2023, 7:12pm UTC](https://discourse.julialang.org/t/julia-pinn-converged-but-bcs-are-not-fulfilled/92351/5 "2023-01-03T19:12:37Z")

</div>

Thank you boath for your responce. I now did it with an extra loss term.
