Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions src/core/connections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@ function disconnect_param!(obj::AbstractCompositeComponentDef, comp_name::Symbol
disconnect_param!(obj, comp, param_name)
end

"""
disconnect_param!(obj::AbstractCompositeComponentDef, comp_path::ComponentPath, param_name::Symbol)

Remove any parameter connections for a given parameter `param_name` in the component identified by
`comp_path` which must be under the composite `obj`.
"""
function disconnect_param!(obj::AbstractCompositeComponentDef, comp_path::ComponentPath, param_name::Symbol)
if (comp_def = find_comp(obj, comp_path)) === nothing
return
end
disconnect_param!(obj, comp_def, param_name)
end

# Default string, string unit check function
verify_units(unit1::AbstractString, unit2::AbstractString) = (unit1 == unit2)

Expand Down Expand Up @@ -129,7 +116,7 @@ function connect_param!(obj::AbstractCompositeComponentDef, comp_def::AbstractCo
end

"""
connect_param!(obj::AbstractCompositeComponentDef,
_connect_param!(obj::AbstractCompositeComponentDef,
dst_comp_path::ComponentPath, dst_par_name::Symbol,
src_comp_path::ComponentPath, src_var_name::Symbol,
backup::Union{Nothing, Array}=nothing;
Expand All @@ -142,18 +129,18 @@ check match units between the two. The `offset` argument indicates the offset b
the destination and the source ie. the value would be `1` if the destination component
parameter should only be calculated for the second timestep and beyond.
"""
function connect_param!(obj::AbstractCompositeComponentDef,
function _connect_param!(obj::AbstractCompositeComponentDef,
dst_comp_path::ComponentPath, dst_par_name::Symbol,
src_comp_path::ComponentPath, src_var_name::Symbol,
backup::Union{Nothing, Array}=nothing;
ignoreunits::Bool=false, offset::Int=0)

# remove any existing connections for this dst parameter
disconnect_param!(obj, dst_comp_path, dst_par_name) # calls dirty!()

dst_comp_def = compdef(obj, dst_comp_path)
src_comp_def = compdef(obj, src_comp_path)

# remove any existing connections for this dst parameter
disconnect_param!(obj, dst_comp_def, dst_par_name) # calls dirty!()

# @info "dst_comp_def: $dst_comp_def"
# @info "src_comp_def: $src_comp_def"

Expand Down Expand Up @@ -242,7 +229,7 @@ function connect_param!(obj::AbstractCompositeComponentDef,
dst_comp_name::Symbol, dst_par_name::Symbol,
src_comp_name::Symbol, src_var_name::Symbol,
backup::Union{Nothing, Array}=nothing; ignoreunits::Bool=false, offset::Int=0)
connect_param!(obj, ComponentPath(obj, dst_comp_name), dst_par_name,
_connect_param!(obj, ComponentPath(obj, dst_comp_name), dst_par_name,
ComponentPath(obj, src_comp_name), src_var_name,
backup; ignoreunits=ignoreunits, offset=offset)
end
Expand Down
13 changes: 0 additions & 13 deletions src/core/defs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -456,19 +456,6 @@ function set_param!(md::ModelDef, comp_name::Symbol, value_dict::Dict{Symbol, An
end
end

"""
set_param!(md::ModelDef, comp_path::ComponentPath, param_name::Symbol,
value_dict::Dict{Symbol, Any}; dims=nothing)

Call `set_param!()` with `param_name` and a value dict in which `value_dict[param_name]` references
the value of parameter `param_name`.
"""
function set_param!(md::ModelDef, comp_name::Symbol, value_dict::Dict{Symbol, Any},
param_name::Symbol; dims=nothing)
value = value_dict[param_name]
set_param!(md, comp_name, param_name, value, dims=dims)
end

function set_param!(md::ModelDef, comp_name::Symbol, param_name::Symbol, value; dims=nothing)
set_param!(md, comp_name, param_name, param_name, value, dims=dims)
end
Expand Down
33 changes: 17 additions & 16 deletions src/core/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,34 @@ is_built(mm::MarginalModel) = (is_built(mm.base) && is_built(mm.modified))
@delegate add_connector_comps!(m::Model) => md

"""
connect_param!(m::Model, dst_comp_path::ComponentPath, dst_par_name::Symbol, src_comp_path::ComponentPath,
src_var_name::Symbol, backup::Union{Nothing, Array}=nothing; ignoreunits::Bool=false, offset::Int=0)
connect_param!(m::Model, dst_comp_name::Symbol, dst_par_name::Symbol, src_comp_name::Symbol, src_var_name::Symbol,
backup::Union{Nothing, Array}=nothing; ignoreunits::Bool=false, offset::Int=0)

Bind the parameter `dst_par_name` of one component `dst_comp_path` of model `md`
to a variable `src_var_name` in another component `src_comp_path` of the same model
Bind the parameter `dst_par_name` of one component `dst_comp_name` of model `m`
to a variable `src_var_name` in another component `src_comp_name` of the same model
using `backup` to provide default values and the `ignoreunits` flag to indicate the need
to check match units between the two. The `offset` argument indicates the offset
between the destination and the source ie. the value would be `1` if the destination
component parameter should only be calculated for the second timestep and beyond.
"""
@delegate connect_param!(m::Model,
dst_comp_path::ComponentPath, dst_par_name::Symbol,
src_comp_path::ComponentPath, src_var_name::Symbol,
backup::Union{Nothing, Array}=nothing;
ignoreunits::Bool=false, offset::Int=0) => md

@delegate connect_param!(m::Model,
dst_comp_name::Symbol, dst_par_name::Symbol,
src_comp_name::Symbol, src_var_name::Symbol,
backup::Union{Nothing, Array}=nothing;
ignoreunits::Bool=false, offset::Int=0) => md

@delegate connect_param!(m::Model, comp_name::Symbol, param_name::Symbol, ext_param_name::Symbol) => md

@delegate connect_param!(m::Model, dst::AbstractString, src::AbstractString, backup::Union{Nothing, Array}=nothing;
ignoreunits::Bool=false, offset::Int=0) => md
"""
connect_param!(m::Model, comp_name::Symbol, param_name::Symbol, ext_param_name::Symbol)

Bind the parameter `param_name` in the component `comp_name` of model `m` to the external parameter
`ext_param_name` already present in the model's list of external parameters.
"""
@delegate connect_param!(m::Model, comp_name::Symbol, param_name::Symbol, ext_param_name::Symbol) => md

"""
connect_param!(m::Model, dst::Pair{Symbol, Symbol}, src::Pair{Symbol, Symbol}, backup::Array; ignoreunits::Bool=false)

Bind the parameter `dst[2]` of one component `dst[1]` of model `md`
Bind the parameter `dst[2]` of one component `dst[1]` of model `m`
to a variable `src[2]` in another component `src[1]` of the same model
using `backup` to provide default values and the `ignoreunits` flag to indicate the need
to check match units between the two. The `offset` argument indicates the offset
Expand All @@ -80,7 +76,12 @@ function connect_param!(m::Model, dst::Pair{Symbol, Symbol}, src::Pair{Symbol, S
connect_param!(m.md, dst[1], dst[2], src[1], src[2], backup; ignoreunits=ignoreunits, offset=offset)
end

@delegate disconnect_param!(m::Model, comp_path::ComponentPath, param_name::Symbol) => md
"""
disconnect_param!(m::Model, comp_name::Symbol, param_name::Symbol)

Remove any parameter connections for a given parameter `param_name` in a given component
`comp_def` in model `m`.
"""
@delegate disconnect_param!(m::Model, comp_name::Symbol, param_name::Symbol) => md

# TBD: these may not be needed as delegators
Expand Down
6 changes: 3 additions & 3 deletions src/core/references.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end
Connect two components as `connect_param!(dst, dst_name, src, src_name)`.
"""
function connect_param!(dst::ComponentReference, dst_name::Symbol, src::ComponentReference, src_name::Symbol)
connect_param!(parent(dst), pathof(dst), dst_name, pathof(src), src_name)
_connect_param!(parent(dst), pathof(dst), dst_name, pathof(src), src_name)
end

"""
Expand All @@ -37,7 +37,7 @@ end
Connect two components with the same name as `connect_param!(dst, src, name)`.
"""
function connect_param!(dst::ComponentReference, src::ComponentReference, name::Symbol)
connect_param!(parent(dst), pathof(dst), name, pathof(src), name)
_connect_param!(parent(dst), pathof(dst), name, pathof(src), name)
end

"""
Expand Down Expand Up @@ -81,5 +81,5 @@ Connect two components as `comp_ref[var_name] = var_ref`.
function Base.setindex!(comp_ref::ComponentReference, var_ref::VariableReference, vname::Symbol)
_same_composite(comp_ref, var_ref) || error("Can't connect variables defined in different composite trees")

connect_param!(parent(comp_ref), pathof(comp_ref), vname, pathof(var_ref), var_name(var_ref))
_connect_param!(parent(comp_ref), pathof(comp_ref), vname, pathof(var_ref), var_name(var_ref))
end
14 changes: 14 additions & 0 deletions test/test_references.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,18 @@ run(m)
@test m[:B, :p1] == 5
@test m[:B, :p2] == collect(1:10)

# Test `connect_param!` methods for ComponentReferences

connect_param!(refB, :p2, refA, :v1)
run(m)
@test m[:B, :p2] == collect(1:10)

@defcomp C begin
v1 = Parameter(index = [time])
end
refC = add_comp!(m, C)
connect_param!(refC, refA, :v1)
run(m)
@test m[:C, :v1] == collect(1:10)

end