Hi all, 

in my research I run numerical simulations (ODEs and SDEs) for circuit 
models that can be composed, i.e.,
each system has an ode that modifies in-place the elements of an output 
array based on the current state variable. 
Moreover, dimensionality of each system may vary.

function sys1_ode(t, x, xdot)
    xdot[1] = # some function of x, t
    xdot[2] = # some other function of x, t
end


function sys2_ode(t, x, xdot)
    xdot[1] = # some expression with x, t
    xdot[2] = # some other expression with x, t
    xdot[3] = # some other expression with x, t
end


What I would like to do is use metaprogramming to construct a combined ode 
for both systems where the state vectors are just stacked. For each system 
I compute the offset within the state vector (0 for sys1 and 2 for sys2) 
and then modify and recombine the function code as follows

function sys12_ode(t, x, xdot)
    xdot[1+0] = # some expression with x[1:2], t
    xdot[2+0] = # some other expression with x[1:2], t
    xdot[1+2] = # some expression with x[1+2:3+2], t

    xdot[2+2] = # some expression with x[1+2:3+2], t
    xdot[3+2] = # some expression with x[1+2:3+2], t

end


So far, that would seem to be quite straightforward and I think I could get 
this working by calling code_lowered(sys1_ode) and using the rewritten 
output to construct an AST for the combined function.

The difficulty now arises when my sys1 and sys2 odes are defined with some 
internal parameters that are not passed as an argument but rather through a 
closure from the surrounding scope, i.e. I have some ode factory:

funcion make_sys1_ode(p1, p2)
    function sys1_ode(t, x, xdot)
        xdot[1] = # some expression with x, t AND p1, p2
        xdot[2] = # some expression with of x, t AND p1, p2
    end
    sys1_ode
end

Given the constructed sys1_ode method is there someway to dynamically 
access the captured variables, i.e. those that come from its closure?
Otherwise, I suppose I could resort to passing parameters via an extra ODE 
argument, but it would be super nice if I could avoid this.
The reason why I am trying to implement things this way is to speed up ODE 
evaluation for very large complex (i.e. nested) circuits by expanding out 
all ODE bodies into a single function.

I hope this write-up makes sense.
Thanks,
Nik

Reply via email to