# Using ModelingToolkit Variables

**URL:** https://discourse.julialang.org/t/using-modelingtoolkit-variables/50484
**Category:** New to Julia
**Created:** [November 20, 2020, 10:34am UTC](https://discourse.julialang.org/t/using-modelingtoolkit-variables/50484 "2020-11-20T10:34:55Z")
**Posts on this page:** 5
**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 20, 2020, 10:34am UTC](https://discourse.julialang.org/t/using-modelingtoolkit-variables/50484/1 "2020-11-20T10:34:55Z")

</div>

I am trying to simulate a heat transfer model with location-specific boundary conditions (each discrete volume element has its own Robin BC). I am using a Physics-Informed NN and have made a similar post regarding [the same issue](https://discourse.julialang.org/t/updating-a-variable-every-time-step-using-neuralpde-jl-and-physics-informed-nn/49621/6), recently.  
The example I used there (reproduced here) worked fine because the variable ‘a’ was only defined and used in the same for-loop.

```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

```

The problem is that my actual code would need many of these ‘a’ variables defined over several for-loops, but when I try this:

```julia
a = Variable(:a)(x) # unknown, depends on `x`
#-----------------------------------------------------------------------
for i in eachindex(X)
    ind = X[i]
     global a(ind) = foo(u, X[i], 0)
end
for i in eachindex(X)
    ind = X[i]
    bcs[i] = u(0,X[i],θ) ~ -a(ind)*sin(X[i]*pi)
end

```

I get this error in the second for-loop: `cannot define function a; it already has a value`

When I try without the `global`, I get this error: `MethodError: objects of type Operation are not callable`

I don’t really know what’s going on. The [documentation](https://juliahub.com/docs/ModelingToolkit/Qmdqu/3.0.0/IR/) for variables doesn’t have much on how to manipulate them.

---

<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 20, 2020, 1:28pm UTC](https://discourse.julialang.org/t/using-modelingtoolkit-variables/50484/2 "2020-11-20T13:28:21Z")

</div>

> [@Fathya.Salih](#):
>
> ```julia
> for i in eachindex(X)
> ind = X[i]
> global a(ind) = foo(u, X[i], 0)
> end
> 
> ```

I really don’t get what you’re trying to do here. You’re redefining `a` as a function? Julia is properly throwing you an error then. I guess my question is, what did you try to do?

---

<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 20, 2020, 2:13pm UTC](https://discourse.julialang.org/t/using-modelingtoolkit-variables/50484/3 "2020-11-20T14:13:54Z")

</div>

‘a’ is a parameter that depends on location. I am trying to define its value at each location (loop). I tried to do this with regular arrays, but I get an error `MethodError: no method matching Float64(::Operation)`

```julia
a = zeros(length(X))
for i in eachindex(X)
    global a[i] = u(0,X[i],θ)^2
end

```

I thought letting the parameter be a “variable” type instead of a float would avoid this error

---

<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 20, 2020, 2:18pm UTC](https://discourse.julialang.org/t/using-modelingtoolkit-variables/50484/4 "2020-11-20T14:18:06Z")

</div>

It looks like you want an array of `a`’s instead? `@variables a[1:n]`

---

<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 20, 2020, 2:28pm UTC](https://discourse.julialang.org/t/using-modelingtoolkit-variables/50484/5 "2020-11-20T14:28:27Z")

</div>

Makes sense. Thank you!
