Hi. This (I think) is probably a question about meta-programming. I’m writing a System Dynamics (Stock and Flow) wrapper around the DynamicalSystems package. In a method named build!()
, I pull together the various bits of stock-and-flow information that the user wants to build into a DynamicalSystems model, in order to put those bits together into a dynamical_rule
function that the constructor CoupledODEs()
requires as its first argument. The end of build!()
looks like this:
dynamical_rule! = (du,u,p,n) -> (
p[:] = eval.(p_vals);
du[:] = sum.(map(x->eval.(x),du_vals));
nothing
)
# sdm.ode_model = CoupledODEs(dynamical_rule!, u_vals, p_vals) # sdm is ready to run!
(dynamical_rule!,p_vals,du_vals)
end # of build!()
I have currently commented out the call to CoupledODEs() and instead am returning the three items of interest like this:
julia> dynamical_rule!,p_vals,du_vals = SystemDynamics.build!(niall)
(Main.SystemDynamics.var"#3#7"{Vector{Vector{Union{Float64, Expr}}}, Vector{Union{Float64, Expr}}}(Vector{Union{Float64, Expr}}[[:(p[1] * u[1])]], Union{Float64, Expr}[0.3]), Union{Float64, Expr}[0.3], Vector{Union{Float64, Expr}}[[:(p[1] * u[1])]])
julia> p_vals
1-element Vector{Union{Float64, Expr}}:
0.3
julia> du_vals
1-element Vector{Vector{Union{Float64, Expr}}}:
[:(p[1] * u[1])]
julia> dynamical_rule!([1.0],[2.0],[3.0],4.0)
ERROR: UndefVarError: `p` not defined
Stacktrace:
[1] top-level scope
@ none:1
So I have two questions really…
- First, I have clearly misunderstood how to link the argument
p
ofdynamical_rule!()
to the code that I have constructed in the statement
du[:] = sum.(map(x->eval.(x),du_vals));
Whendynamical_rule!()
is called, this should (in my understanding) evaluate to
du[:] = p[1] * u[1]
,
however julia says thatp
is not defined. So clearly I’ve got things wrong somewhere. - Second, I’m currently getting tangled up with lots of
Union{Float64,Expr}
objects that I don’t really want. What I really want areExpr
s that evaluate toFloat64
values, and which may in fact on occasion simply be numeric literals. Unfortunately, however, expressions such as:(0.3)
are immediately translated into aFloat64
and so are never recognised asExpr
s.
If anyone happens to be into this sort of stuff, I’d very much appreciate a helping hand.
Thanks!