Skip to content

Commit 620164a

Browse files
authored
Merge pull request #112 from VirtualPlantLab/issue-on-organ-statuses-for-non-existing-nodes-in-mtg
Update save_results.jl
2 parents 6dee96e + 57575ad commit 620164a

3 files changed

Lines changed: 19 additions & 19 deletions

File tree

docs/src/model_coupling/multiscale.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ Models can access only one time step at a time, so the output at the end of a si
191191

192192
```@example usepkg
193193
outs = Dict(
194-
"Scene" => (:TT, :TT_cu, :node),
194+
"Scene" => (:TT, :TT_cu,),
195195
"Plant" => (:aPPFD, :LAI),
196196
"Leaf" => (:carbon_assimilation, :carbon_demand, :carbon_allocation, :TT),
197197
"Internode" => (:carbon_allocation,),

src/mtg/initialisation.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ ref_var(v::T) where {T<:AbstractString} = Base.Ref(v) # No copy method for strin
262262
ref_var(v::T) where {T<:Base.RefValue} = v
263263
ref_var(v::T) where {T<:RefVector} = Base.Ref(v)
264264
ref_var(v::T) where {T<:RefVariable} = v
265+
ref_var(v::UninitializedVar) = Base.Ref(copy(v.value))
265266

266267
"""
267268
init_simulation(mtg, mapping; nsteps=1, outputs=nothing, type_promotion=nothing, check=true, verbose=true)

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)