Using ModelingToolkit Variables

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, recently.
The example I used there (reproduced here) worked fine because the variable ‘a’ was only defined and used in the same for-loop.

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:

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 for variables doesn’t have much on how to manipulate them.

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?

‘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)

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

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

Makes sense. Thank you!