Skip to content

Commit a3c8973

Browse files
committed
Start to implement an orchestrator struct that centralizes timestep issues and connector data
1 parent c5c68ca commit a3c8973

4 files changed

Lines changed: 53 additions & 7 deletions

File tree

src/PlantSimEngine.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import Statistics
2626
import SHA: sha1
2727

2828
using PlantMeteo
29+
using PlantMeteo.Dates
2930

3031
# UninitializedVar + PreviousTimeStep:
3132
include("variables_wrappers.jl")
@@ -65,6 +66,9 @@ include("dependencies/printing.jl")
6566
include("dependencies/dependencies.jl")
6667
include("dependencies/get_model_in_dependency_graph.jl")
6768

69+
# Timesteps. :
70+
include("timestep/timestep_mapping.jl")
71+
6872
# MTG compatibility:
6973
include("mtg/GraphSimulation.jl")
7074
include("mtg/mapping/getters.jl")
@@ -103,6 +107,7 @@ include("examples_import.jl")
103107
export PreviousTimeStep
104108
export AbstractModel
105109
export ModelList, MultiScaleModel
110+
export Orchestrator
106111
export RMSE, NRMSE, EF, dr
107112
export Status, TimeStepTable, status
108113
export init_status!

src/mtg/GraphSimulation.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,23 @@ A type that holds all information for a simulation over a graph.
1414
- `var_need_init`: a dictionary indicating if a variable needs to be initialized
1515
- `dependency_graph`: the dependency graph of the models applied to the graph
1616
- `models`: a dictionary of models
17+
- `Orchestrator : the structure that handles timestep peculiarities
1718
- `outputs`: a dictionary of outputs
1819
"""
19-
struct GraphSimulation{T,S,U,O,V,W}
20+
struct GraphSimulation{T,S,U,O,V}
2021
graph::T
2122
statuses::S
2223
status_templates::Dict{String,Dict{Symbol,Any}}
2324
reverse_multiscale_mapping::Dict{String,Dict{String,Dict{Symbol,Any}}}
2425
var_need_init::Dict{String,V}
2526
dependency_graph::DependencyGraph
2627
models::Dict{String,U}
28+
orchestrator::Orchestrator
2729
outputs::Dict{String,O}
28-
outputs_index::Dict{String, Int}
29-
default_timestep::Int # TODO make it a period ?
30-
model_timesteps::Dict{W, Int} #where {W <: AbstractModel}
3130
end
3231

33-
function GraphSimulation(graph, mapping; nsteps=1, outputs=nothing, type_promotion=nothing, check=true, verbose=false, default_timestep=1, model_timesteps=Dict{String, Int}())
34-
GraphSimulation(init_simulation(graph, mapping; nsteps=nsteps, outputs=outputs, type_promotion=type_promotion, check=check, verbose=verbose)..., default_timestep, model_timesteps)
32+
function GraphSimulation(graph, mapping; nsteps=1, outputs=nothing, type_promotion=nothing, check=true, verbose=false, orchestrator=Orchestrator())
33+
GraphSimulation(init_simulation(graph, mapping; nsteps=nsteps, outputs=outputs, type_promotion=type_promotion, check=check, verbose=verbose)..., orchestrator)
3534
end
3635

3736
dep(g::GraphSimulation) = g.dependency_graph

src/mtg/initialisation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ The value is not a reference to the one in the attribute of the MTG, but a copy
305305
a value in a Dict. If you need a reference, you can use a `Ref` for your variable in the MTG directly, and it will be
306306
automatically passed as is.
307307
"""
308-
function init_simulation(mtg, mapping; nsteps=1, outputs=nothing, type_promotion=nothing, check=true, verbose=false)
308+
function init_simulation(mtg, mapping; nsteps=1, outputs=nothing, type_promotion=nothing, check=true, verbose=false, orchestrator=Orchestrator())
309309

310310
# Ensure the user called the model generation function to handle vectors passed into a status
311311
# before we keep going

src/timestep/timestep_mapping.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
# Those names all suck, need to change them
3+
# Some of them are probably not ideal for new users, too
4+
5+
# Some types can also be constrained a lot more, probably
6+
7+
struct TimestepMapper#{V}
8+
variable_from#::V
9+
timestep_from::Int
10+
mapping_function
11+
end
12+
13+
struct SimulationTimestepHandler#{W,V}
14+
model_timesteps::Dict{Any, Int} # where {W <: AbstractModel} # if a model isn't in there, then it follows the default, todo check if the given timestep respects the model's range
15+
timestep_variable_mapping::Dict{Any, TimestepMapper} #where {V}
16+
end
17+
18+
SimulationTimestepHandler() = SimulationTimestepHandler(Dict(), Dict()) #Dict{W, Int}(), Dict{V, TimestepMapper}()) where {W, V}
19+
20+
mutable struct Orchestrator
21+
# This is actually a general simulation parameter, not-scale specific
22+
# todo change to Period
23+
default_timestep::Int64
24+
25+
# This needs to be per-scale : if a model is used at two different scales,
26+
# and the same variable of that model maps to some other timestep to two *different* variables
27+
# then I believe we can only rely on the different scale to disambiguate
28+
non_default_timestep_data_per_scale::Dict{String, SimulationTimestepHandler}
29+
30+
function Orchestrator(default::Int64, per_scale::Dict{String, SimulationTimestepHandler})
31+
@assert default >= 0 "The default_timestep should be greater than or equal to 0."
32+
return new(default, per_scale)
33+
end
34+
end
35+
36+
# TODO have a default constructor take in a meteo or something, and set up the default timestep automagically to be the finest weather timestep
37+
# Other options are possible
38+
Orchestrator() = Orchestrator(1, Dict{String, SimulationTimestepHandler}())
39+
40+
41+
#o = Orchestrator()
42+
#oo = Orchestrator(1, Dict{String, SimulationTimestepHandler}())

0 commit comments

Comments
 (0)