Skip to content

Commit a883289

Browse files
committed
Update save_results.jl
1 parent 6dee96e commit a883289

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

src/mtg/save_results.jl

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function pre_allocate_outputs(statuses, statuses_template, reverse_multiscale_ma
116116
outs_[i] = [outs[i]...]
117117
end
118118

119-
statuses_ = copy(statuses)
119+
statuses_ = copy(statuses_template)
120120
# Checking that organs in outputs exist in the mtg (in the statuses):
121121
if !all(i in keys(statuses) for i in keys(outs_))
122122
not_in_statuses = setdiff(keys(outs_), keys(statuses))
@@ -141,21 +141,9 @@ function pre_allocate_outputs(statuses, statuses_template, reverse_multiscale_ma
141141
if length(statuses[organ]) == 0
142142
# The organ is not found in the mtg, we return an info and get along (it might be created during the simulation):
143143
check && @info "You required outputs for organ $organ, but this organ is not found in the provided MTG at this point."
144-
145-
status_from_template = PlantSimEngine.init_node_status!(
146-
MultiScaleTreeGraph.Node(MultiScaleTreeGraph.NodeMTG("/", organ, 0, 0)),
147-
statuses,
148-
statuses_template,
149-
reverse_multiscale_mapping,
150-
vars_need_init,
151-
type_promotion;
152-
check=check
153-
)
154-
155-
statuses_[organ] = [status_from_template]
156144
end
157-
if !all(i in collect(keys(statuses_[organ][1])) for i in vars)
158-
not_in_statuses = (setdiff(vars, keys(statuses_[organ][1]))...,)
145+
if !all(i in collect(keys(statuses_[organ])) for i in vars)
146+
not_in_statuses = (setdiff(vars, keys(statuses_[organ]))...,)
159147
plural = length(not_in_statuses) == 1 ? "" : "s"
160148
e = string(
161149
"You requested outputs for variable", plural, " ",
@@ -185,10 +173,21 @@ function pre_allocate_outputs(statuses, statuses_template, reverse_multiscale_ma
185173

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

176+
node_types = []
177+
for o in keys(statuses)
178+
if length(statuses[o]) > 0
179+
push!(node_types, typeof(statuses[o][1].node))
180+
end
181+
end
182+
183+
node_type = unique(node_types)
184+
@assert length(node_type) == 1 "All plant graph nodes should have the same type, found $(unique(node_type))."
185+
node_type = only(node_type)
186+
188187
# Making the pre-allocated outputs:
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)
190-
# Note: we use the type of the variable from the first status for each organ to pre-allocate the outputs, because they are
191-
# all the same type for others.
188+
return Dict(organ => Dict(var => [var == :node ? node_type[] : typeof(status_from_template(statuses_template[organ])[var])[] for n in 1:nsteps] for var in vars) for (organ, vars) in outs_tuple)
189+
# Note: we use the type of the variable from the status template for each organ to pre-allocate the outputs. We transform the status template into a status to get the types of the variables
190+
# without the reference types, e.g. RefVector{Float64} becomes Vector{Float64}.
192191
end
193192

194193
pre_allocate_outputs(statuses, status_templates, reverse_multiscale_mapping, vars_need_init, ::Nothing, nsteps; type_promotion=nothing, check=true) = Dict{String,Tuple{Symbol,Vararg{Symbol}}}()

0 commit comments

Comments
 (0)