Skip to content

Commit ffab1e8

Browse files
committed
Update test-multirate-output-export.jl
Add test with all defaults for models running at different rates (testing inference of multirate traits)
1 parent 9b75ab0 commit ffab1e8

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

test/test-multirate-output-export.jl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,35 @@ function PlantSimEngine.run!(m::MRExportSourceModel, models, status, meteo, cons
1515
status.X = float(m.n[])
1616
end
1717

18+
PlantSimEngine.@process "mrdefaultleafsource" verbose = false
19+
struct MRDefaultLeafSourceModel <: AbstractMrdefaultleafsourceModel
20+
n::Base.RefValue{Int}
21+
end
22+
PlantSimEngine.inputs_(::MRDefaultLeafSourceModel) = NamedTuple()
23+
PlantSimEngine.outputs_(::MRDefaultLeafSourceModel) = (X=-Inf,)
24+
function PlantSimEngine.run!(m::MRDefaultLeafSourceModel, models, status, meteo, constants=nothing, extra=nothing)
25+
m.n[] += 1
26+
status.X = float(m.n[])
27+
end
28+
29+
PlantSimEngine.@process "mrdefaultplantagg" verbose = false
30+
struct MRDefaultPlantAggModel <: AbstractMrdefaultplantaggModel end
31+
PlantSimEngine.inputs_(::MRDefaultPlantAggModel) = (X=-Inf,)
32+
PlantSimEngine.outputs_(::MRDefaultPlantAggModel) = (XP=-Inf,)
33+
function PlantSimEngine.run!(::MRDefaultPlantAggModel, models, status, meteo, constants=nothing, extra=nothing)
34+
status.XP = sum(status.X) + 100.0
35+
end
36+
PlantSimEngine.timespec(::Type{<:MRDefaultPlantAggModel}) = ClockSpec(2.0, 1.0)
37+
38+
PlantSimEngine.@process "mrdefaultsceneagg" verbose = false
39+
struct MRDefaultSceneAggModel <: AbstractMrdefaultsceneaggModel end
40+
PlantSimEngine.inputs_(::MRDefaultSceneAggModel) = (XP=-Inf,)
41+
PlantSimEngine.outputs_(::MRDefaultSceneAggModel) = (XS=-Inf,)
42+
function PlantSimEngine.run!(::MRDefaultSceneAggModel, models, status, meteo, constants=nothing, extra=nothing)
43+
status.XS = sum(status.XP) + 1000.0
44+
end
45+
PlantSimEngine.timespec(::Type{<:MRDefaultSceneAggModel}) = ClockSpec(4.0, 1.0)
46+
1847
@testset "Multi-rate output export API" begin
1948
mtg = Node(MultiScaleTreeGraph.NodeMTG("/", "Scene", 1, 0))
2049
plant = Node(mtg, MultiScaleTreeGraph.NodeMTG("+", "Plant", 1, 1))
@@ -120,3 +149,55 @@ end
120149
@test haskey(out_status_mtg, "Leaf")
121150
@test out_requested_mtg[:x_mtg][:, :value] == [1.0, 2.0, 3.0, 4.0]
122151
end
152+
153+
@testset "Multi-rate output export defaults on multi-scale mapping with timespec traits" begin
154+
mtg = Node(MultiScaleTreeGraph.NodeMTG("/", "Scene", 1, 0))
155+
plant = Node(mtg, MultiScaleTreeGraph.NodeMTG("+", "Plant", 1, 1))
156+
internode = Node(plant, MultiScaleTreeGraph.NodeMTG("/", "Internode", 1, 2))
157+
Node(internode, MultiScaleTreeGraph.NodeMTG("+", "Leaf", 1, 2))
158+
159+
meteo8 = Weather(repeat([Atmosphere(T=20.0, Wind=1.0, Rh=0.65)], 8))
160+
161+
mapping_defaults = Dict(
162+
"Leaf" => (
163+
MultiScaleModel(
164+
model=MRDefaultLeafSourceModel(Ref(0)),
165+
mapped_variables=[:X => "Plant"],
166+
),
167+
),
168+
"Plant" => (
169+
MultiScaleModel(
170+
model=MRDefaultPlantAggModel(),
171+
mapped_variables=[:XP => "Scene"],
172+
),
173+
),
174+
"Scene" => (
175+
MRDefaultSceneAggModel(),
176+
),
177+
)
178+
179+
sim_defaults = PlantSimEngine.GraphSimulation(
180+
mtg,
181+
mapping_defaults,
182+
nsteps=8,
183+
check=true,
184+
outputs=Dict("Leaf" => (:X,), "Plant" => (:XP,), "Scene" => (:XS,)),
185+
)
186+
187+
run!(
188+
sim_defaults,
189+
meteo8,
190+
multirate=true,
191+
executor=SequentialEx(),
192+
tracked_outputs=[
193+
OutputRequest("Plant", :XP),
194+
OutputRequest("Scene", :XS),
195+
],
196+
)
197+
198+
exported_defaults = collect_outputs(sim_defaults; sink=DataFrame)
199+
200+
@test sort(collect(keys(exported_defaults))) == [:XP, :XS]
201+
@test exported_defaults[:XP][:, :timestep] == collect(1:8)
202+
@test exported_defaults[:XS][:, :timestep] == collect(1:8)
203+
end

0 commit comments

Comments
 (0)