# Updating a variable every time step using NeuralPDE.jl and Physics-Informed NN

**URL:** <https://discourse.julialang.org/t/updating-a-variable-every-time-step-using-neuralpde-jl-and-physics-informed-nn/49621>\
**Category:** General Usage\
**Tags:** diffeq\
**Created:** [November 5, 2020, 9:49am UTC](https://discourse.julialang.org/t/updating-a-variable-every-time-step-using-neuralpde-jl-and-physics-informed-nn/49621 "2020-11-05T09:49:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fathya.Salih](https://avatars.discourse-cdn.com/v4/letter/f/47e85d/32.png) [@Fathya.Salih](https://discourse.julialang.org/u/Fathya.Salih)\
**Post date:** [November 5, 2020, 9:49am UTC](https://discourse.julialang.org/t/updating-a-variable-every-time-step-using-neuralpde-jl-and-physics-informed-nn/49621/1 "2020-11-05T09:49:53Z")

</div>

Hey all,

I am trying to solve a finite volume problem with PINN and I am having trouble defining one of the boundary conditions. I have a variable that depends on the value of the function and I want to assign values to the variable based on previous solution steps. This is what I have in mind:

```julia
@parameters x, y, z, θ
@variables u(..)
@derivatives Dx'~x
@derivatives Dy'~y
@derivatives Dz'~z

function foo(u) #function takes value of the variable from a previous iteration
      P= c*u^4/n1
      return P
      # where n1 and c are constants and 'P' is the changing variable
end
for i in 1:i_max
       P = foo(u)
       bc[i] = P*n2 ~ Dx(u)+Dy(u)+Dz(u) #n2 is some other constant
end

```

I’m not sure how to implement this using PINN. I am following [these examples](https://github.com/SciML/NeuralPDE.jl/blob/master/docs/src/examples/pinns_example.md).

---

<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:** [November 5, 2020, 12:57pm UTC](https://discourse.julialang.org/t/updating-a-variable-every-time-step-using-neuralpde-jl-and-physics-informed-nn/49621/2 "2020-11-05T12:57:25Z")

</div>

Is there an issue with just writing that?

---

<div class="post-metadata">

**Author:** ![Fathya.Salih](https://avatars.discourse-cdn.com/v4/letter/f/47e85d/32.png) [@Fathya.Salih](https://discourse.julialang.org/u/Fathya.Salih)\
**Post date:** [November 5, 2020, 1:51pm UTC](https://discourse.julialang.org/t/updating-a-variable-every-time-step-using-neuralpde-jl-and-physics-informed-nn/49621/3 "2020-11-05T13:51:12Z")

</div>

I tried with one of the examples

```julia
#2D PDE
eq = Dt(u(t,x,θ)) + u(t,x,θ)*Dx(u(t,x,θ)) - (0.01/pi)*Dxx(u(t,x,θ)) ~ 0

function foo(u)
    a = u^2
    return a
end

# Initial and boundary conditions
X = collect(range(-1, 1, step=0.1))
U = -sin.(pi*X)
bcs = Vector{ModelingToolkit.Equation}(undef, length(U)+2)
for i in eachindex(U)
    a = foo(u)
    bcs[i] = u(0,X[i],θ) ~ -a*sin(X[i]*pi)
end
bcs[length(U)+1] = u(t,-1,θ) ~ 0.
bcs[length(U)+2] = u(t,1,θ) ~ 0.

```

and I get this error  
`ERROR: LoadError: MethodError: no method matching ^(::Variable{Number}, ::Int64)`

Also, I don’t know what value of the variable it is taking. I don’t want the definitions to be circular or over-constrained.

---

<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:** [November 5, 2020, 2:51pm UTC](https://discourse.julialang.org/t/updating-a-variable-every-time-step-using-neuralpde-jl-and-physics-informed-nn/49621/4 "2020-11-05T14:51:42Z")

</div>

@shashi is this fixed in v4?

---

<div class="post-metadata">

**Author:** ![shashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashi/32/1824_2.png) [@shashi](https://discourse.julialang.org/u/shashi)\
**Post date:** [November 5, 2020, 2:59pm UTC](https://discourse.julialang.org/t/updating-a-variable-every-time-step-using-neuralpde-jl-and-physics-informed-nn/49621/5 "2020-11-05T14:59:23Z")

</div>

> [@Fathya.Salih](#):
>
> ```julia
> P= c*u^4/n1
> 
> ```

I think this is wrong (doesn’t matter which version), it’s because `u` is a function-like object. Calling it will return a number like object.

So either use `u(something, something)` or define `@variables u` instead of `@variables u(..)`

---

<div class="post-metadata">

**Author:** ![Fathya.Salih](https://avatars.discourse-cdn.com/v4/letter/f/47e85d/32.png) [@Fathya.Salih](https://discourse.julialang.org/u/Fathya.Salih)\
**Post date:** [November 5, 2020, 3:26pm UTC](https://discourse.julialang.org/t/updating-a-variable-every-time-step-using-neuralpde-jl-and-physics-informed-nn/49621/6 "2020-11-05T15:26:39Z")

</div>

Thank you!  
Using `u(something)` worked.

```julia
function foo(u)
    a = u(t,x,θ)^2
    return a
end

# Initial and boundary conditions
X = collect(range(-1, 1, step=0.1))
bcs = Vector{ModelingToolkit.Equation}(undef, length(U)+2)
for i in eachindex(X)
    a = foo(u)
    bcs[i] = u(0,X[i],θ) ~ -a*sin(X[i]*pi)
end

```
