Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "fix: fix namespacing of defaults and equations" #2595

Merged
merged 1 commit into from
Apr 1, 2024
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
22 changes: 8 additions & 14 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -819,24 +819,22 @@ namespace_controls(sys::AbstractSystem) = controls(sys, controls(sys))

function namespace_defaults(sys)
defs = defaults(sys)
Dict((isparameter(k) ? parameters(sys, k) : unknowns(sys, k)) => namespace_expr(
v, sys; check = true)
Dict((isparameter(k) ? parameters(sys, k) : unknowns(sys, k)) => namespace_expr(v, sys)
for (k, v) in pairs(defs))
end

function namespace_equations(sys::AbstractSystem, ivs = independent_variables(sys))
eqs = equations(sys)
isempty(eqs) && return Equation[]
map(eq -> namespace_equation(eq, sys; ivs, check = true), eqs)
map(eq -> namespace_equation(eq, sys; ivs), eqs)
end

function namespace_equation(eq::Equation,
sys,
n = nameof(sys);
ivs = independent_variables(sys),
check = false)
_lhs = namespace_expr(eq.lhs, sys, n; ivs, check)
_rhs = namespace_expr(eq.rhs, sys, n; ivs, check)
ivs = independent_variables(sys))
_lhs = namespace_expr(eq.lhs, sys, n; ivs)
_rhs = namespace_expr(eq.rhs, sys, n; ivs)
_lhs ~ _rhs
end

Expand All @@ -846,36 +844,32 @@ function namespace_assignment(eq::Assignment, sys)
Assignment(_lhs, _rhs)
end

function namespace_expr(
O, sys, n = nameof(sys); ivs = independent_variables(sys), check = false)
function namespace_expr(O, sys, n = nameof(sys); ivs = independent_variables(sys))
O = unwrap(O)
if any(isequal(O), ivs)
return O
elseif istree(O)
T = typeof(O)
renamed = let sys = sys, n = n, T = T
map(a -> namespace_expr(a, sys, n; ivs, check)::Any, arguments(O))
map(a -> namespace_expr(a, sys, n; ivs)::Any, arguments(O))
end
if isvariable(O)
check && !is_variable(sys, O) && !is_parameter(sys, O) && return O
# Use renamespace so the scope is correct, and make sure to use the
# metadata from the rescoped variable
rescoped = renamespace(n, O)
similarterm(O, operation(rescoped), renamed,
metadata = metadata(rescoped))
elseif Symbolics.isarraysymbolic(O)
check && !is_variable(sys, O) && !is_parameter(sys, O) && return O
# promote_symtype doesn't work for array symbolics
similarterm(O, operation(O), renamed, symtype(O), metadata = metadata(O))
else
similarterm(O, operation(O), renamed, metadata = metadata(O))
end
elseif isvariable(O)
check && !is_variable(sys, O) && !is_parameter(sys, O) && return O
renamespace(n, O)
elseif O isa Array
let sys = sys, n = n
map(o -> namespace_expr(o, sys, n; ivs, check), O)
map(o -> namespace_expr(o, sys, n; ivs), O)
end
else
O
Expand Down
58 changes: 0 additions & 58 deletions test/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -995,61 +995,3 @@ let # Issue https://github.com/SciML/ModelingToolkit.jl/issues/2322
sol = solve(prob, Rodas4())
@test sol(1)[]≈0.6065307685451087 rtol=1e-4
end

# Issue#2344
function FML2(; name)
@parameters begin
k2[1:1] = [1.0]
end
systems = @named begin
constant = Constant(k = k2[1])
end
@variables begin
x(t) = 0
end
eqs = [
D(x) ~ constant.output.u + k2[1]
]
ODESystem(eqs, t; systems, name)
end

@mtkbuild model = FML2()

@test isequal(ModelingToolkit.defaults(model)[model.constant.k], model.k2[1])
@test_nowarn ODEProblem(model, [], (0.0, 10.0))

# Issue#2477
function RealExpression(; name, y)
vars = @variables begin
u(t)
end
eqns = [
u ~ y
]
sys = ODESystem(eqns, t, vars, []; name)
end

function sys1(; name)
vars = @variables begin
x(t)
z(t)[1:1]
end # doing a collect on z doesn't work either.
@named e1 = RealExpression(y = x) # This works perfectly.
@named e2 = RealExpression(y = z[1]) # This bugs. However, `full_equations(e2)` works as expected.
systems = [e1, e2]
ODESystem(Equation[], t, Iterators.flatten(vars), []; systems, name)
end

@named sys = sys1()
sys = complete(sys)
@test Set(equations(sys)) == Set([sys.e1.u ~ sys.x, sys.e2.u ~ sys.z[1]])

# Issue#2522
@parameters a[1:2]=[1, 2] b=4 c=1
@variables x(t)=ParentScope(a[1]) y(t)=ParentScope(b)
@named level0 = ODESystem([D(x) ~ ParentScope(a[2]),
D(y) ~ ParentScope(c)], t, [x, y], [])
level1 = ODESystem(Equation[], t, [], [a..., b, c]; name = :level1) ∘ level0
level1 = structural_simplify(level1)
@test isequal(ModelingToolkit.defaults(level1)[level1.level0.x], level1.a[1])
@test_nowarn ODEProblem(level1, [], (0, 1))
Loading