Skip to content

Commit

Permalink
Output variables for unexisting node types now OK (can appear after i…
Browse files Browse the repository at this point in the history
…n the simulation)
  • Loading branch information
VEZY committed Jul 22, 2024
1 parent afeeba0 commit 33cc0af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/mtg/initialisation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ function init_simulation(mtg, mapping; nsteps=1, outputs=nothing, type_promotion

models = Dict(first(m) => parse_models(get_models(last(m))) for m in mapping)

outputs = pre_allocate_outputs(statuses, outputs, nsteps, check=check)
outputs = pre_allocate_outputs(statuses, status_templates, reverse_multiscale_mapping, vars_need_init, outputs, nsteps, type_promotion=type_promotion, check=check)

return (; mtg, statuses, status_templates, reverse_multiscale_mapping, vars_need_init, dependency_graph=dep(mapping, verbose=verbose), models, outputs)
end
25 changes: 18 additions & 7 deletions src/mtg/save_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ julia> collect(keys(preallocated_vars["Leaf"]))
:carbon_demand
```
"""
function pre_allocate_outputs(statuses, outs, nsteps; check=true)
function pre_allocate_outputs(statuses, statuses_template, reverse_multiscale_mapping, vars_need_init, outs, nsteps; type_promotion=nothing, check=true)
outs_ = copy(outs)

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_))
not_in_statuses = setdiff(keys(outs_), keys(statuses))
Expand All @@ -135,11 +135,22 @@ function pre_allocate_outputs(statuses, outs, nsteps; check=true)
for (organ, vars) in outs_ # organ = "Leaf"; vars = outs_[organ]
if length(statuses[organ]) == 0
# The organ is not found in the mtg, we return an info and get along (it might be created during the simulation):
check && @info "You required outputs for organ $organ, but this organ is not found in the provided MTG."
continue
check && @info "You required outputs for organ $organ, but this organ is not found in the provided MTG at this point."

status_from_template = PlantSimEngine.init_node_status!(
MultiScaleTreeGraph.Node(MultiScaleTreeGraph.NodeMTG("/", organ, 0, 0)),
statuses,
statuses_template,
reverse_multiscale_mapping,
vars_need_init,
type_promotion;
check=check
)

statuses_[organ] = [status_from_template]
end
if !all(i in collect(keys(statuses[organ][1])) for i in vars)
not_in_statuses = (setdiff(vars, keys(statuses[organ][1]))...,)
if !all(i in collect(keys(statuses_[organ][1])) for i in vars)
not_in_statuses = (setdiff(vars, keys(statuses_[organ][1]))...,)
plural = length(not_in_statuses) == 1 ? "" : "s"
e = string(
"You requested outputs for variable", plural, " ",
Expand Down Expand Up @@ -168,7 +179,7 @@ function pre_allocate_outputs(statuses, outs, nsteps; check=true)
end

# 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_)
# 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 33cc0af

Please sign in to comment.