I posted before here regarding this when my project was less fleshed out. Now that Ive actually got things mostly working I’m still struggling with this last task.
Ive got some JSON that looks something like this:
[
"model1": {
"system": {
"parameters": [
{name: "a", value: "1"},
{name: "b", value: "2"},
{name: "c", value: "3"},
]
}
},
"model2": {
"system": {
"parameters": [
{name: "d", value: "1"},
{name: "e", value: "2"},
{name: "f", value: "3"},
]
}
}
]
I need to somehow get this into a parameter map programatically such that the parameter map is:
ps = [
model1.a => 1,
model1.b => 2,
model1.c => 3,
model2.d => 1,
model2.e => 2,
model2.f => 3,
]
the models (with model names matching the names in the JSON) have already been generated and composed using modeling toolkit, now its just a matter of making this map. But im kind of stumped on how to go about it. It seems like a good job for julias metaprogramming but I tried some simple stuff and im not sure how to generate the modelname.parameter name portion. Any ideas?
Edit:
Ah, Meta.parse is exactly what I wanted. I was trying to convert the string directly to a symbol and was running into problems there
Edit2: Hmm. Ive run into an issue. My models dont exist in the global scope (they are stored in a dictionary, and in my function that builds the parameter map I need to call eval on my expression. But eval only evaluates in the global scope:
function build_parameter_map(systems_map::Dict{String, ModelingToolkit.AbstractSystem}, model_nodes)
ps = []
for model_node in model_nodes
node_data = model_node.data
# this corresponds to one of the model objects in the JSON above
model = node_data.model
for parameter in model.system.parameters
p_name = parameter.name
p_value = Float64(parameter.value)
# this is the problematic part, model_node does not exist in the global scope
expression = Meta.parse("systems_map[model_node.id].$p_name => $p_value")
push!(ps, eval(expression))
end
end
return ps
end
@ChrisRackauckas any tips? I thought it would be easy to supply parameters programatically to a problem constructor but I cant figure it out.