# Symbolic Geometry definition for PINN

**URL:** <https://discourse.julialang.org/t/symbolic-geometry-definition-for-pinn/80058>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [April 26, 2022, 1:03pm UTC](https://discourse.julialang.org/t/symbolic-geometry-definition-for-pinn/80058 "2022-04-26T13:03:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeoCott](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@LeoCott](https://discourse.julialang.org/u/LeoCott)\
**Post date:** [April 26, 2022, 1:03pm UTC](https://discourse.julialang.org/t/symbolic-geometry-definition-for-pinn/80058/1 "2022-04-26T13:03:37Z")

</div>

Dear all,

I’m trying to solve the Reynolds-Averaged Navier Stokes (RANS) equations in a channel with periodic hills (see Figure below).

 ![Capture d’écran 2022-04-26 à 14.46.43](https://global.discourse-cdn.com/julialang/original/3X/4/f/4f84641793b6ddc0f70b95cbb7a91ab99aab6be5.png)

The hill geometry is defined by a third order polynomial, here assumed to be computed in the function _shape\_hill(x)_. Is it possible to use the present symbolic function _shape\_hill(x)_ to define the parameters domains ?

```julia
# Parameters
@parameters x, y
@variables u(..), v(..), uu(..), uv(..), vv(..), p(..)
rho = 1.2
nu = 0.000015

# 2D RANS Partial Differential Equations
Dx = Differential(x)
Dxx = Differential(x)^2
Dy = Differential(y)
Dyy = Differential(y)^2

eqs = [u(x,y)*Dx(u(x,y)) + v(x,y)*Dy(u(x,y)) + (1/rho)*Dx(p(x,y)) - nu*(Dxx(u(x,y)) + Dyy(u(x,y))) + Dx(uu(x,y)) + Dy(uv(x,y)) ~ 0. ,
        u(x,y)*Dx(v(x,y)) + v(x,y)*Dy(v(x,y)) + (1/rho)*Dy(p(x,y)) - nu*(Dxx(v(x,y)) + Dyy(v(x,y))) + Dx(uv(x,y)) + Dy(vv(x,y)) ~ 0. ,
        Dx(u(x,y)) + Dy(v(x,y)) ~ 0.0]

function shape_hill(x)
...
end

domains = [x ∈ [0.,9.], y ∈ [shape_hill(x),3.]]

```

If yes, could it also be used for the boundary conditions (e.g zero velocity on the ground) ?

Something like:

```julia
bcs = [u(x,shape_hill(x)) ~ 0., v(x,shape_hill(x)) ~ 0.]

```

When I tried to use _shape\_hill(x)_ for the domain definition I had the following error message _**TypeError: non-boolean (Num) used in boolean context**_.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [April 26, 2022, 2:57pm UTC](https://discourse.julialang.org/t/symbolic-geometry-definition-for-pinn/80058/2 "2022-04-26T14:57:58Z")

</div>

Hard to say for sure without seeing the code for shape\_hill, but This is most likely due to something like this: [Frequently Asked Questions · Symbolics.jl](https://symbolics.juliasymbolics.org/dev/manual/faq/#Transforming-my-function-to-a-symbolic-equation-has-failed.-What-do-I-do?-1)

It may also be a simple conditional, if you can replace if-else control flow with [IfElse.ifelse](https://github.com/SciML/IfElse.jl), you can sometimes fix this.

---

<div class="post-metadata">

**Author:** ![LeoCott](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@LeoCott](https://discourse.julialang.org/u/LeoCott)\
**Post date:** [April 27, 2022, 9:13am UTC](https://discourse.julialang.org/t/symbolic-geometry-definition-for-pinn/80058/3 "2022-04-27T09:13:34Z")

</div>

Thanks @contradict for your answer, I’ll have a look to the links!

Here is the _shape\_hill(x)_ function :

```julia
function shape_hill(x)

        if x > 126.0
                x = 252.0 - x
        end
        
        if x>=0 && x<9
                return min(28., 2.8e+01 + 0.0e+00*x + 6.775070969851e-03*x^2 - 2.124527775800e-03*x^3)/H
        elseif x>=9 && x<14
                return (2.507355893131e+01 + 9.754803562315e-01*x - 1.016116352781e-01*x^2 + 1.889794677828e-03*x^3)/H
        elseif x>=14 && x<20
                return (2.579601052357e+01 + 8.206693007457e-01*x - 9.055370274339e-02*x^2 + 1.626510569859e-03*x^3)/H
        elseif x>=20 && x<30
                return (4.046435022819e+01 - 1.379581654948e+00*x + 1.945884504128e-02*x^2 - 2.070318932190e-04*x^3)/H
        elseif x>=30 && x<40
                return (1.792461334664e+01 + 8.743920332081e-01*x - 5.567361123058e-02*x^2 + 6.277731764683e-04*x^3)/H
        elseif x>=40 && x<=54
                return max(0., 5.639011190988e+01 - 2.010520359035e+00*x + 1.644919857549e-02*x^2 + 2.674976141766e-05*x^3)/H
        elseif x>54 && x<=126
                return 0
        end

end

```

It is based on the DNS work from Breuer et al. (2009).

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [April 27, 2022, 3:12pm UTC](https://discourse.julialang.org/t/symbolic-geometry-definition-for-pinn/80058/4 "2022-04-27T15:12:36Z")

</div>

Yeah, either `@register_symbolic shape_hill(x)` to entirely prevent tracing that or re-write it to use `ifelse` should fix this problem.

---

<div class="post-metadata">

**Author:** ![LeoCott](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@LeoCott](https://discourse.julialang.org/u/LeoCott)\
**Post date:** [April 27, 2022, 7:20pm UTC](https://discourse.julialang.org/t/symbolic-geometry-definition-for-pinn/80058/5 "2022-04-27T19:20:09Z")

</div>

Indeed, thanks a lot @contradict, I tested both methods and that got rid of the error. However I performed some checks and I am not sure it gives what I want, e.g:

```julia
@register_symbolic shape_hill(x)
domains = [x ∈ Interval(0.,9.), y ∈ Interval(shape_hill(x),3.)]
[3. 2.] in domains **False**

```

But [3. 2.] should be in the domain. I also checked _shape\_hill(x)_ and it works properly.

Moreover, I tried to pursue the PINN implementation following the different tutorials

```julia
# Initial and boundary conditions
bcs = [u(x,wall_bottom(x)) ~ 0., v(x,wall_bottom(x)) ~ 0.,
       uu(x,wall_bottom(x)) ~ 0., uv(x,wall_bottom(x)) ~ 0.,
       vv(x,wall_bottom(x)) ~ 0.]

# Neural network
inner = 16
chain =[FastChain(FastDense(2,inner,Flux.σ),
                 FastDense(inner,inner,Flux.σ),
                 FastDense(inner,1))
                 for _ in 1:6]

strategy = GridTraining(0.1)
discretization = PhysicsInformedNN(chain,strategy)

@named pde_system = PDESystem(eqs,bcs,domains,[x,y],[u,v,uu,uv,vv, p])
prob = discretize(pde_system,discretization)

```

But I had an issue regarding the use of the _discretize_ function : _**TypeError: non-boolean (Num) used in boolean context**_.

To overcome it, I used _symbolic\_discretize_ but had an issue when solving the optimization problem : **MethodError: no method matching init(::Tuple{Vector{Expr}, Vector{Expr}}, ::ADAM; cb=var"#23#24" (), maxiters=500)**

```julia
res = GalacticOptim.solve(prob,ADAM(0.01);cb=cb,maxiters=500)

```

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [April 27, 2022, 11:37pm UTC](https://discourse.julialang.org/t/symbolic-geometry-definition-for-pinn/80058/6 "2022-04-27T23:37:44Z")

</div>

I don’t expect the symbolic interval to work as written, but you could probably write a function to test that with some [Expression Manipulation · Symbolics.jl](https://symbolics.juliasymbolics.org/dev/manual/expression_manipulation/)

The error from `symbolic_discretize` is not one I recognize, but just from the signature it is looking for, I suspect you forgot a step. It looks like prob is actually a tuple, no a problem instance. Perhaps you need to call some kind of problem constructor between discretize and solve.

---

<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:** [June 4, 2022, 12:01pm UTC](https://discourse.julialang.org/t/symbolic-geometry-definition-for-pinn/80058/7 "2022-06-04T12:01:30Z")

</div>

Can you turn this into an issue?
