Skip to content

Commit

Permalink
Check the dimensions when making internal parameter connections
Browse files Browse the repository at this point in the history
  • Loading branch information
lrennels committed Mar 31, 2022
1 parent 130926c commit f267b50
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/core/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ function _collect_params(md::ModelDef, var_dict::Dict{ComponentPath, Any})
ipcs = _get_leaf_level_ipcs(md, conn)
src_vars = var_dict[ipcs[1].src_comp_path]
var_value_obj = get_property_obj(src_vars, ipcs[1].src_var_name)
for ipc in ipcs
for ipc in ipcs
_check_attributes(md, ipc, var_dict)
pdict[(ipc.dst_comp_path, ipc.dst_par_name)] = var_value_obj
end
end
Expand Down
45 changes: 44 additions & 1 deletion src/core/connections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function _check_attributes(obj::AbstractCompositeComponentDef,
if ! isempty(param_dims) && size(param_dims) != size(model_dims)
d1 = size(model_dims)
d2 = size(param_dims)
error("Mismatched dimensions of parameter connection: Component: $(nameof(comp_def)) Parameter: $param_name ($d2) to Model Parameter ($d1)")
error("Mismatched dimensions of parameter connection: Component: $(nameof(comp_def)) Parameter: $param_name (size $d2) to Model Parameter (size $d1)")
end

# Don't check sizes for ConnectorComps since they won't match.
Expand All @@ -108,6 +108,49 @@ function _check_attributes(obj::AbstractCompositeComponentDef,
end
end

"""
_check_attributes(obj::AbstractCompositeComponentDef, ipc::InternalParameterConnection,
var_dict::Dict{ComponentPath, Any})
Check that the attributes of the source variable match the attributes of the
destination Parameter in InternalParameterConnection `ipc` and an object `obj
with variables defined by var_dict. Check dimensions and (TODO) datatypes.
"""
function _check_attributes(obj::AbstractCompositeComponentDef, ipc::InternalParameterConnection,
var_dict::Dict{ComponentPath, Any})

var_def = Mimi.variable(Mimi.find_comp(obj, ipc.src_comp_path), ipc.src_var_name)
param_def = Mimi.parameter(Mimi.find_comp(obj, ipc.dst_comp_path), ipc.dst_par_name)

param_dims = Mimi.dim_names(param_def)
var_dims = Mimi.dim_names(var_def)

if size(param_dims) != size(var_dims)
d1 = size(var_dims)
d2 = size(param_dims)
error("Mismatched dimensions of internal parameter connection: ",
"Component: $(nameof(Mimi.find_comp(obj, ipc.dst_comp_path))) Parameter: $(ipc.dst_par_name) (size $d2) ",
"Component: $(nameof(Mimi.find_comp(obj, ipc.src_comp_path))) Variable: $(ipc.src_var_name) (size $d1).")
end

for (i, dim) in enumerate(param_dims)
if isa(dim, Symbol)
param_dim_size = dim_count(obj,dim)

src_vars = var_dict[ipc.src_comp_path]
var_value_obj = Mimi.get_property_obj(src_vars, ipc.src_var_name)
var_data_size = size(var_value_obj.data,i)

if param_dim_size != var_data_size
error("Mismatched data size for internal parameter connection: ",
"dimension :$dim in $(nameof(Mimi.find_comp(obj, ipc.dst_comp_path)))'s Parameter $(ipc.dst_par_name) has $param_dim_size elements; ",
"while the same positioned (#$i) dimension for $(nameof(Mimi.find_comp(obj, ipc.src_comp_path)))'s Variable $(ipc.src_var_name) has $var_data_size elements.")
end
end
end

# TODO datatype
end
"""
_check_attributes(obj::AbstractCompositeComponentDef,
comp_def::AbstractComponentDef, param_name::Symbol,
Expand Down
36 changes: 36 additions & 0 deletions wip/dims_testing.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@defcomp comp1 begin
par1 = Parameter(index=[time])
var1 = Variable(index=[time])

function run_timestep(p,v,d,t)
v.var1[t] = p.par1[t]
end
end

@defcomp comp2 begin
par2 = Parameter(index=[time, country])
var2 = Variable(index=[time,country])

function run_timestep(p,v,d,t)
v.var2[t] = p.par2[t]
end
end

@defcomp comp3 begin
par3 = Parameter(index=[time, town])
var3 = Variable(index=[time,town])

function run_timestep(p,v,d,t)
v.var3[t,:] = p.par3[t,:]
end
end

m = Model()
set_dimension!(m, :time, 2000:2010)
set_dimension!(m, :country, [:A, :B, :C])
set_dimension!(m, :town, ["a", "b", "c", "d"])
add_comp!(m, comp2)
add_comp!(m, comp3)
set_param!(m, :comp2, :par2, zeros(11,3))
connect_param!(m, :comp3 => :par3, :comp2 => :var2)

0 comments on commit f267b50

Please sign in to comment.