Skip to content

Commit

Permalink
Merge pull request #2864 from hersle/error_missing_defaults
Browse files Browse the repository at this point in the history
Error missing defaults
  • Loading branch information
ChrisRackauckas authored Jul 16, 2024
2 parents 0cc516e + a9e0c6b commit 6d8e86e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ function iv_from_nested_derivative(x, op = Differential)
end

hasdefault(v) = hasmetadata(v, Symbolics.VariableDefaultValue)
getdefault(v) = value(getmetadata(v, Symbolics.VariableDefaultValue))
getdefault(v) = value(Symbolics.getdefaultval(v))
function getdefaulttype(v)
def = value(getmetadata(unwrap(v), Symbolics.VariableDefaultValue, nothing))
def === nothing ? Float64 : typeof(def)
Expand Down
2 changes: 1 addition & 1 deletion test/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ end
@test getdefault(model.cval) == 1
@test isequal(getdefault(model.c), model.cval + model.jval)
@test getdefault(model.d) == 2
@test_throws KeyError getdefault(model.e)
@test_throws ErrorException getdefault(model.e)
@test getdefault(model.f) == 3
@test getdefault(model.i) == 4
@test all(getdefault.(scalarize(model.b2)) .== [1, 3])
Expand Down
8 changes: 5 additions & 3 deletions test/variable_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,18 @@ end
y = 2, [connect = Flow]
end

@test_throws ErrorException ModelingToolkit.getdefault(x)
@test !hasmetadata(x, VariableDefaultValue)
@test getmetadata(x, VariableConnectType) == Flow
@test getmetadata(x, VariableUnit) == u
@test getmetadata(y, VariableDefaultValue) === 2
@test getmetadata(y, VariableConnectType) == Flow

a = rename(value(x), :a)
@test !hasmetadata(x, VariableDefaultValue)
@test getmetadata(x, VariableConnectType) == Flow
@test getmetadata(x, VariableUnit) == u
@test_throws ErrorException ModelingToolkit.getdefault(a)
@test !hasmetadata(a, VariableDefaultValue)
@test getmetadata(a, VariableConnectType) == Flow
@test getmetadata(a, VariableUnit) == u

@variables t x(t)=1 [connect = Flow, unit = u]

Expand Down

0 comments on commit 6d8e86e

Please sign in to comment.