Skip to content

Commit 2e56472

Browse files
committed
Fix tests and doc
1 parent 3518cf5 commit 2e56472

6 files changed

Lines changed: 52 additions & 11 deletions

File tree

docs/src/multiscale/multiscale.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ This graph has a root node that defines a scene, then a soil, and a plant with t
210210
For long simulations on plants with many organs, the output data can be very significant. It's possible to restrict the output variables that are tracked for the whole simulation to a subset of all the variables:
211211

212212
```@example usepkg
213-
outs = ModelMapping(
213+
outs = Dict(
214214
"Scene" => (:TT, :TT_cu,),
215215
"Plant" => (:aPPFD, :LAI),
216216
"Leaf" => (:carbon_assimilation, :carbon_demand, :carbon_allocation, :TT),

src/dependencies/dependencies.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ dep(::T, nsteps=1) where {T<:AbstractModel} = NamedTuple()
33
"""
44
dep(mapping::ModelMapping; verbose=true)
55
dep(mapping::AbstractDict{String,T}; verbose=true)
6-
dep!(m::check_multiscale_simulation_is_equivalent_begin, nsteps=1)
6+
dep!(m::ModelMapping, nsteps=1)
77
8-
Get the model dependency graph given a ModelList or a multiscale model mapping. If one graph is returned,
8+
Get the model dependency graph given a ModelMapping or a multiscale model mapping. If one graph is returned,
99
then all models are coupled. If several graphs are returned, then only the models inside each graph are coupled, and
1010
the models in different graphs are not coupled.
1111
`nsteps` is the number of steps the dependency graph will be used over. It is used to determine
@@ -39,7 +39,7 @@ these graphs independently to r
3939
4040
# Notes
4141
42-
The difference between `dep(m::ModelList)` and `dep!(m::ModelList, nsteps)` is that the first one returns the dependency graph found in the model list, while the
42+
The difference between `dep(m::ModelMapping)` and `dep!(m::ModelMapping, nsteps)` is that the first one returns the dependency graph found in the model list, while the
4343
second one returns the dependency graph with the specified number of steps, modifying the simulation IDs of each node in the graph (`simulation_id=fill(0, nsteps)`).
4444
4545
# Examples
@@ -50,7 +50,7 @@ using PlantSimEngine
5050
# Including example processes and models:
5151
using PlantSimEngine.Examples;
5252
53-
models = ModelList(
53+
models = ModelMapping(
5454
process1=Process1Model(1.0),
5555
process2=Process2Model(),
5656
process3=Process3Model(),

src/mtg/mapping/mapping.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,20 @@ function ModelMapping(
234234
"Invalid argument mix: scale-level pairs must not be mixed with model arguments."
235235
)
236236

237-
return ModelMapping{SingleScale,ModelList}(ModelList(args...; status=status, type_promotion=nothing, variables_check=check, processes...))
237+
flat_args = Any[]
238+
for arg in args
239+
if arg isa Pair && first(arg) isa Symbol
240+
push!(flat_args, last(arg))
241+
elseif arg isa NamedTuple
242+
append!(flat_args, values(arg))
243+
elseif arg isa Tuple
244+
append!(flat_args, arg)
245+
else
246+
push!(flat_args, arg)
247+
end
248+
end
249+
250+
return ModelMapping{SingleScale,ModelList}(ModelList(flat_args...; status=status, type_promotion=nothing, variables_check=check, processes...))
238251

239252
#TODO: Use the following when we merge the ModelList and ModelMapping paths (create a fake scale):
240253
single_scale_models = _single_scale_mapping_entries(args, processes, status)
@@ -304,6 +317,10 @@ function _normalize_scale_mapping(scale::String, scale_mapping::ModelList)
304317
return _normalize_scale_mapping(scale, (values(scale_mapping.models)..., status(scale_mapping)))
305318
end
306319

320+
function _normalize_scale_mapping(scale::String, scale_mapping::ModelMapping{SingleScale})
321+
return _normalize_scale_mapping(scale, scale_mapping.data)
322+
end
323+
307324
function _normalize_scale_mapping(scale::String, scale_mapping::Union{AbstractModel,MultiScaleModel,ModelSpec})
308325
return (scale_mapping,)
309326
end

src/run.jl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ function run!(
165165
)
166166
end
167167

168+
function run!(
169+
::ModelMapping{MultiScale},
170+
meteo=nothing,
171+
constants=PlantMeteo.Constants(),
172+
extra=nothing;
173+
tracked_outputs=nothing,
174+
check=true,
175+
executor=ThreadedEx(),
176+
multirate=false,
177+
return_requested_outputs=false,
178+
requested_outputs_sink=DataFrames.DataFrame
179+
)
180+
error("This `ModelMapping` is a multiscale mapping. ", "Use `run!(mtg, mapping, ...)` for multiscale mappings.")
181+
end
168182

169183
# User entry point, which uses traits to dispatch to the correct method.
170184
# The traits are defined in table_traits.jl
@@ -575,7 +589,10 @@ function run!(
575589
nsteps=nothing,
576590
tracked_outputs=nothing,
577591
check=true,
578-
executor=ThreadedEx()
592+
executor=ThreadedEx(),
593+
multirate=false,
594+
return_requested_outputs=false,
595+
requested_outputs_sink=DataFrames.DataFrame
579596
)
580597
Base.depwarn(
581598
"`run!(mtg, mapping::AbstractDict, ...)` is deprecated. Use `run!(mtg, ModelMapping(mapping), ...)` or construct `ModelMapping(...)` directly.",
@@ -590,7 +607,10 @@ function run!(
590607
nsteps=nsteps,
591608
tracked_outputs=tracked_outputs,
592609
check=check,
593-
executor=executor
610+
executor=executor,
611+
multirate=multirate,
612+
return_requested_outputs=return_requested_outputs,
613+
requested_outputs_sink=requested_outputs_sink
594614
)
595615
end
596616

test/helper-functions.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ function compare_outputs_modellist_mapping(filtered_outputs_modellist, graphsim)
3434
outputs_df = convert_outputs(graphsim.outputs, DataFrame)
3535
@assert haskey(outputs_df, "Default")
3636
common_cols = filter(c -> c in names(outputs_df["Default"]), names(modellist_sorted))
37-
mapping_sorted = outputs_df["Default"][:, sortperm(common_cols)]
38-
modellist_sorted = modellist_sorted[:, sortperm(common_cols)]
37+
mapping_sorted = outputs_df["Default"][:, common_cols]
38+
modellist_sorted = modellist_sorted[:, common_cols]
39+
40+
# Keep deterministic order in case columns are provided in different orders.
41+
mapping_sorted = mapping_sorted[:, sortperm(names(mapping_sorted))]
42+
modellist_sorted = modellist_sorted[:, sortperm(names(modellist_sorted))]
3943

4044
return modellist_sorted == mapping_sorted
4145
end

test/test-simulation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ end;
3434

3535
@test_deprecated run!(models, meteo)
3636
@test_deprecated run!([models], meteo)
37-
@test_deprecated run!(ModelMapping("mod1" => models), meteo)
37+
@test_throws ErrorException run!(ModelMapping("mod1" => models), meteo)
3838

3939
mtg = Node(MultiScaleTreeGraph.NodeMTG("/", "Leaf", 1, 1))
4040
mtg[:var1] = 15.0

0 commit comments

Comments
 (0)