From 59c7dd3a9c7d44f7692e570035c88a28cc68e952 Mon Sep 17 00:00:00 2001 From: lisarennels Date: Wed, 22 Jun 2022 14:37:27 -0700 Subject: [PATCH 1/2] Use unique name for backup parameter --- src/core/connections.jl | 14 ++++++++++++-- test/test_connectorcomp.jl | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/core/connections.jl b/src/core/connections.jl index 6e970de25..857216a96 100644 --- a/src/core/connections.jl +++ b/src/core/connections.jl @@ -324,10 +324,20 @@ function _connect_param!(obj::AbstractCompositeComponentDef, end + # create a backup parameter name with the destination leaf component and + # parameter names, as well as a trailing integer to ensure uniqueness in + # edge cases. Check if the name is already used, and if so increment the + # trailing integer until it is a unique model parameter name to the model + i = 1 + backup_param_name = Symbol("backup_", dst_comp_path.names[end], "_", dst_par_name, "_", i) + while haskey(obj.model_params, backup_param_name) + i += 1 + backup_param_name = Symbol("backup_", dst_comp_path.names[end], "_", dst_par_name, "_", i) + end + # NB: potentially unsafe way to add parameter/might be duplicating work so # advise shifting to create_model_param ... but leaving it as is for now - add_model_array_param!(obj, dst_par_name, values, dst_dims) - backup_param_name = dst_par_name + add_model_array_param!(obj, backup_param_name, values, dst_dims) else # cannot use backup_offset keyword argument if there is no backup diff --git a/test/test_connectorcomp.jl b/test/test_connectorcomp.jl index db5798097..733b8afca 100644 --- a/test/test_connectorcomp.jl +++ b/test/test_connectorcomp.jl @@ -2,6 +2,8 @@ module TestConnectorComp using Mimi using Test +using Query +using DataFrames import Mimi: compdef, compdefs, components, getspan @@ -261,5 +263,31 @@ short_var = model6[:Short, :var] @test all(ismissing, short_var[1:year_dim[late_start]-1]) @test short_var[year_dim[late_start]:end] == years[year_dim[late_start]:end] +#------------------------------------------------------------------------------ +# 6. Test multiple, identical components with backup +#------------------------------------------------------------------------------ + +model1 = Model() +set_dimension!(model1, :time, years) + +add_comp!(model1, Short, :Short_A, first=late_start) +add_comp!(model1, Long, :Long_A) +update_param!(model1, :Short_A, :a, 2.) +connect_param!(model1, :Long_A, :x, :Short_A, :b, zeros(length(years))); + +add_comp!(model1, Short, :Short_B, first=late_start) +add_comp!(model1, Long, :Long_B) +update_param!(model1, :Short_B, :a, 2.) +connect_param!(model1, :Long_B, :x, :Short_B, :b, ones(length(years))) + +run(model1) + +# the backup data should differ +@test all(iszero, (getdataframe(model1, :Long_A, :x) |> @filter(_.time < late_start) |> DataFrame).x) +@test all(i -> i == 1., (getdataframe(model1, :Long_B, :x) |> @filter(_.time < late_start) |> DataFrame).x) + +# should see two model parameters added to the model instance +@test haskey(model1.mi.md.model_params, :backup_Long_A_x_1) # created to connect to :Long_A +@test haskey(model1.mi.md.model_params, :backup_Long_B_x_1) #created to connect to :Long B end #module From 2114bd72e0fbbc62da3238c2d72dd9fac50b2216 Mon Sep 17 00:00:00 2001 From: lisarennels Date: Wed, 22 Jun 2022 14:53:49 -0700 Subject: [PATCH 2/2] Add warnings but leave commented out for now --- src/core/connections.jl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/core/connections.jl b/src/core/connections.jl index 857216a96..e492d638a 100644 --- a/src/core/connections.jl +++ b/src/core/connections.jl @@ -724,8 +724,8 @@ end Add an model parameter with name `name` and Model Parameter `value` to ModelDef `md`. """ function add_model_param!(md::ModelDef, name::Symbol, value::ModelParameter) - # if haskey(obj.model_params, name) - # @warn "Redefining model param :$name in $(obj.comp_path) from $(obj.model_params[name]) to $value" + # if haskey(md.model_params, name) + # @warn "Redefining model param :$name in $(md.comp_path) from $(md.model_params[name]) to $value" # end md.model_params[name] = value dirty!(md) @@ -750,6 +750,9 @@ using it and only do so under the hood. function add_model_param!(md::ModelDef, name::Symbol, value::Number; param_dims::Union{Nothing,Array{Symbol}} = nothing, is_shared::Bool = false) + # if haskey(md.model_params, name) + # @warn "Redefining model param :$name in $(md.comp_path) from $(md.model_params[name]) to $value" + # end add_model_scalar_param!(md, name, value, is_shared = is_shared) end @@ -767,6 +770,9 @@ function add_model_param!(md::ModelDef, name::Symbol, value::Union{AbstractArray, AbstractRange, Tuple}; param_dims::Union{Nothing,Array{Symbol}} = nothing, is_shared::Bool = false) + # if haskey(md.model_params, name) + # @warn "Redefining model param :$name in $(md.comp_path) from $(md.model_params[name]) to $value" + # end ti = get_time_index_position(param_dims) if !isnothing(ti)