Skip to content

Commit

Permalink
Merge pull request #104 from VirtualPlantLab/codepaths_merging_modell…
Browse files Browse the repository at this point in the history
…ist_multiscale

Fix issue of overspecialisation of outputs Dict of variables requested by the user
  • Loading branch information
Samuel-amap authored Oct 25, 2024
2 parents ca7a5ec + 734a139 commit d770909
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/mtg/save_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ julia> collect(keys(preallocated_vars["Leaf"]))
```
"""
function pre_allocate_outputs(statuses, statuses_template, reverse_multiscale_mapping, vars_need_init, outs, nsteps; type_promotion=nothing, check=true)
outs_ = copy(outs)
outs_ = Dict{String,Vector{Symbol}}()
for i in keys(outs) # i = "Plant"
@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."""
outs_[i] = [outs[i]...]
end

statuses_ = copy(statuses)
# Checking that organs in outputs exist in the mtg (in the statuses):
if !all(i in keys(statuses) for i in keys(outs_))
Expand Down Expand Up @@ -168,18 +173,20 @@ function pre_allocate_outputs(statuses, statuses_template, reverse_multiscale_ma
delete!(outs_, organ)
else
# Some still exist, we only use the ones that do:
outs_[organ] = (existing_vars_requested...,)
outs_[organ] = [existing_vars_requested...]
end
end
end

if :node outs_[organ]
outs_[organ] = (outs_[organ]..., :node)
push!(outs_[organ], :node)
end
end

outs_tuple = Dict(i => Tuple(x for x in outs_[i]) for i in keys(outs_))

# Making the pre-allocated outputs:
Dict(organ => Dict(var => [typeof(statuses_[organ][1][var])[] for n in 1:nsteps] for var in vars) for (organ, vars) in outs_)
Dict(organ => Dict(var => [typeof(statuses_[organ][1][var])[] for n in 1:nsteps] for var in vars) for (organ, vars) in outs_tuple)
# Note: we use the type of the variable from the first status for each organ to pre-allocate the outputs, because they are
# all the same type for others.
end
Expand Down

0 comments on commit d770909

Please sign in to comment.