Skip to content

Commit d770909

Browse files
authored
Merge pull request #104 from VirtualPlantLab/codepaths_merging_modellist_multiscale
Fix issue of overspecialisation of outputs Dict of variables requested by the user
2 parents ca7a5ec + 734a139 commit d770909

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

src/mtg/save_results.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ julia> collect(keys(preallocated_vars["Leaf"]))
110110
```
111111
"""
112112
function pre_allocate_outputs(statuses, statuses_template, reverse_multiscale_mapping, vars_need_init, outs, nsteps; type_promotion=nothing, check=true)
113-
outs_ = copy(outs)
113+
outs_ = Dict{String,Vector{Symbol}}()
114+
for i in keys(outs) # i = "Plant"
115+
@assert isa(outs[i], Tuple{Vararg{Symbol}}) """Outputs for scale $i should be a tuple of symbols, *e.g.* `"$i" => (:a, :b)`, found `"$i" => $(outs[i])` instead."""
116+
outs_[i] = [outs[i]...]
117+
end
118+
114119
statuses_ = copy(statuses)
115120
# Checking that organs in outputs exist in the mtg (in the statuses):
116121
if !all(i in keys(statuses) for i in keys(outs_))
@@ -168,18 +173,20 @@ function pre_allocate_outputs(statuses, statuses_template, reverse_multiscale_ma
168173
delete!(outs_, organ)
169174
else
170175
# Some still exist, we only use the ones that do:
171-
outs_[organ] = (existing_vars_requested...,)
176+
outs_[organ] = [existing_vars_requested...]
172177
end
173178
end
174179
end
175180

176181
if :node outs_[organ]
177-
outs_[organ] = (outs_[organ]..., :node)
182+
push!(outs_[organ], :node)
178183
end
179184
end
180185

186+
outs_tuple = Dict(i => Tuple(x for x in outs_[i]) for i in keys(outs_))
187+
181188
# Making the pre-allocated outputs:
182-
Dict(organ => Dict(var => [typeof(statuses_[organ][1][var])[] for n in 1:nsteps] for var in vars) for (organ, vars) in outs_)
189+
Dict(organ => Dict(var => [typeof(statuses_[organ][1][var])[] for n in 1:nsteps] for var in vars) for (organ, vars) in outs_tuple)
183190
# Note: we use the type of the variable from the first status for each organ to pre-allocate the outputs, because they are
184191
# all the same type for others.
185192
end

0 commit comments

Comments
 (0)