From 3bcc420a598613b5a60f3f62b7d555c41ca96e94 Mon Sep 17 00:00:00 2001 From: Christopher Tessum Date: Fri, 13 Jun 2025 11:25:16 +0800 Subject: [PATCH 1/3] switch parse_costs! and parse_constraints It seems like they are currently backward --- src/systems/model_parsing.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 9d293fd40d..67df29fae3 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -654,9 +654,9 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts, elseif mname == Symbol("@defaults") parse_system_defaults!(exprs, arg, dict) elseif mname == Symbol("@constraints") - parse_costs!(cons, dict, body) + parse_constraints!(cons, dict, body) elseif mname == Symbol("@costs") - parse_constraints!(costs, dict, body) + parse_costs!(costs, dict, body) elseif mname == Symbol("@consolidate") parse_consolidate!(body, dict) else From f1fe0caa176dabed63d58d0ba3263abdd2ba12ae Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 15:27:09 +0530 Subject: [PATCH 2/3] fix: fix parsing of costs and constraints for `@mtkmodel` metadata --- src/systems/model_parsing.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 67df29fae3..18c1b15922 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -1160,7 +1160,7 @@ function parse_constraints!(cons, dict, body) Base.remove_linenums!(body) for arg in body.args push!(cons, arg) - push!(dict[:constraints], readable_code.(cons)...) + push!(dict[:constraints], readable_code(arg)) end end @@ -1169,7 +1169,7 @@ function parse_costs!(costs, dict, body) Base.remove_linenums!(body) for arg in body.args push!(costs, arg) - push!(dict[:costs], readable_code.(costs)...) + push!(dict[:costs], readable_code(arg)) end end From 821b4c45c216b72dc66e3d9dac15722fec74735d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 13 Jun 2025 15:27:25 +0530 Subject: [PATCH 3/3] test: test correct costs and constraints in `@mtkmodel` metadata --- test/model_parsing.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index fbbca9c7ea..c48628b007 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -1042,4 +1042,6 @@ end @test isequal(constrs[1], EvalAt(0.3)(ex.x) ~ 3) @test isequal(constrs[2], ex.y ≲ 4) @test ModelingToolkit.get_consolidate(ex)([1, 2], [3, 4]) ≈ 8 + log(2) + @test Example.structure[:constraints] == ["(EvalAt(0.3))(x) ~ 3", "y ≲ 4"] + @test Example.structure[:costs] == ["x + y", "(EvalAt(1))(y) ^ 2"] end