Skip to content

Commit efdace7

Browse files
committed
Avoid always recomputing the reverse multiscale mapping when calling soft_dependencies_multiscale
1 parent 5e9886d commit efdace7

4 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/dependencies/dependencies.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,15 @@ function dep(mapping::Dict{String,T}; verbose::Bool=true) where {T}
105105
# only the nodes that are not hard-dependency of other nodes. These nodes are taken as roots for the soft-dependency graph because they
106106
# are independant.
107107
soft_dep_graphs_roots, hard_dep_dict = hard_dependencies(mapping; verbose=verbose)
108+
109+
mapped_vars = mapped_variables(mapping, soft_dep_graphs_roots, verbose=false)
110+
reverse_multiscale_mapping = reverse_mapping(mapped_vars, all=false)
111+
108112
# Second step, compute the soft-dependency graph between SoftDependencyNodes computed in the first step. To do so, we search the
109113
# inputs of each process into the outputs of the other processes, at the same scale, but also between scales. Then we keep only the
110114
# nodes that have no soft-dependencies, and we set them as root nodes of the soft-dependency graph. The other nodes are set as children
111115
# of the nodes that they depend on.
112-
dep_graph = soft_dependencies_multiscale(soft_dep_graphs_roots, mapping, hard_dep_dict)
116+
dep_graph = soft_dependencies_multiscale(soft_dep_graphs_roots, reverse_multiscale_mapping, hard_dep_dict)
113117
# During the building of the soft-dependency graph, we identified the inputs and outputs of each dependency node,
114118
# and also defined **inputs** as MappedVar if they are multiscale, i.e. if they take their values from another scale.
115119
# What we are missing is that we need to also define **outputs** as multiscale if they are needed by another scale.

src/dependencies/soft_dependencies.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,8 @@ function soft_dependencies(d::DependencyGraph{Dict{Symbol,HardDependencyNode}},
139139
end
140140

141141
# For multiscale mapping:
142-
function soft_dependencies_multiscale(soft_dep_graphs_roots::DependencyGraph{Dict{String,Any}}, mapping::Dict{String,A}, hard_dep_dict::Dict{Pair{Symbol,String},HardDependencyNode}) where {A<:Any}
143-
mapped_vars = mapped_variables(mapping, soft_dep_graphs_roots, verbose=false)
144-
rev_mapping = reverse_mapping(mapped_vars, all=false)
145-
142+
function soft_dependencies_multiscale(soft_dep_graphs_roots::DependencyGraph{Dict{String,Any}}, reverse_multiscale_mapping, hard_dep_dict::Dict{Pair{Symbol,String},HardDependencyNode})
143+
146144
independant_process_root = Dict{Pair{String,Symbol},SoftDependencyNode}()
147145
for (organ, (soft_dep_graph, ins, outs)) in soft_dep_graphs_roots.roots # e.g. organ = "Plant"; soft_dep_graph, ins, outs = soft_dep_graphs_roots.roots[organ]
148146
for (proc, i) in soft_dep_graph
@@ -158,7 +156,7 @@ function soft_dependencies_multiscale(soft_dep_graphs_roots::DependencyGraph{Dic
158156
# NB: if a node is already a hard dependency of the node, it cannot be a soft dependency
159157

160158
# Check if the process has soft dependencies at other scales:
161-
soft_deps_multiscale = search_inputs_in_multiscale_output(proc, organ, ins, soft_dep_graphs_roots.roots, rev_mapping, hard_dependencies_from_other_scale)
159+
soft_deps_multiscale = search_inputs_in_multiscale_output(proc, organ, ins, soft_dep_graphs_roots.roots, reverse_multiscale_mapping, hard_dependencies_from_other_scale)
162160
# Example output: "Soil" => Dict(:soil_water=>[:soil_water_content]), which means that the variable :soil_water_content
163161
# is computed by the process :soil_water at the scale "Soil".
164162

src/mtg/initialisation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ function init_simulation(mtg, mapping; nsteps=1, outputs=nothing, type_promotion
327327
# inputs of each process into the outputs of the other processes, at the same scale, but also between scales. Then we keep only the
328328
# nodes that have no soft-dependencies, and we set them as root nodes of the soft-dependency graph. The other nodes are set as children
329329
# of the nodes that they depend on.
330-
dep_graph = soft_dependencies_multiscale(soft_dep_graphs_roots, mapping, hard_dep_dict)
330+
dep_graph = soft_dependencies_multiscale(soft_dep_graphs_roots, reverse_multiscale_mapping, hard_dep_dict)
331331
# During the building of the soft-dependency graph, we identified the inputs and outputs of each dependency node,
332332
# and also defined **inputs** as MappedVar if they are multiscale, i.e. if they take their values from another scale.
333333
# What we are missing is that we need to also define **outputs** as multiscale if they are needed by another scale.

test/test-mtg-multiscale-cyclic-dep.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ out_vars = Dict(
4848
@test_throws "Cyclic dependency detected in the graph. Cycle:" dep(mapping_cyclic)
4949

5050
soft_dep_graphs_roots, hard_dep_dict = PlantSimEngine.hard_dependencies(mapping_cyclic)
51-
dep_graph = PlantSimEngine.soft_dependencies_multiscale(soft_dep_graphs_roots, mapping_cyclic, hard_dep_dict)
51+
52+
mapped_vars = PlantSimEngine.mapped_variables(mapping_cyclic, soft_dep_graphs_roots, verbose=false)
53+
reverse_mapping_cyclic = PlantSimEngine.reverse_mapping(mapped_vars, all=false)
54+
55+
dep_graph = PlantSimEngine.soft_dependencies_multiscale(soft_dep_graphs_roots, reverse_mapping_cyclic, hard_dep_dict)
5256
iscyclic, cycle_vec = PlantSimEngine.is_graph_cyclic(dep_graph; warn=false)
5357

5458
@test iscyclic
@@ -97,7 +101,10 @@ end
97101

98102
soft_dep_graphs_roots, hard_dep_dict = PlantSimEngine.hard_dependencies(mapping_nocyclic)
99103
# soft_dep_graphs_roots.roots["Leaf"].inputs
100-
dep_graph = PlantSimEngine.soft_dependencies_multiscale(soft_dep_graphs_roots, mapping_nocyclic, hard_dep_dict)
104+
mapped_vars = PlantSimEngine.mapped_variables(mapping_nocyclic, soft_dep_graphs_roots, verbose=false)
105+
reverse_mapping_nocyclic = PlantSimEngine.reverse_mapping(mapped_vars, all=false)
106+
107+
dep_graph = PlantSimEngine.soft_dependencies_multiscale(soft_dep_graphs_roots, reverse_mapping_nocyclic, hard_dep_dict)
101108
iscyclic, cycle_vec = PlantSimEngine.is_graph_cyclic(dep_graph; warn=false)
102109

103110
@test !iscyclic

0 commit comments

Comments
 (0)