Skip to content

feat: allow specifying problem_type using system metadata #3685

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

Merged
merged 2 commits into from
Jun 3, 2025
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
3 changes: 2 additions & 1 deletion src/problems/nonlinearproblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ end
check_length, check_compatibility, expression, kwargs...)

kwargs = process_kwargs(sys; kwargs...)
args = (; f, u0, p, ptype = StandardNonlinearProblem())
ptype = getmetadata(sys, ProblemTypeCtx, StandardNonlinearProblem())
args = (; f, u0, p, ptype)

return maybe_codegen_scimlproblem(expression, NonlinearProblem{iip}, args; kwargs...)
end
Expand Down
3 changes: 2 additions & 1 deletion src/problems/odeproblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ end
kwargs = process_kwargs(
sys; expression, callback, eval_expression, eval_module, kwargs...)

args = (; f, u0, tspan, p, ptype = StandardODEProblem())
ptype = getmetadata(sys, ProblemTypeCtx, StandardODEProblem())
args = (; f, u0, tspan, p, ptype)
maybe_codegen_scimlproblem(expression, ODEProblem{iip}, args; kwargs...)
end

Expand Down
10 changes: 10 additions & 0 deletions src/systems/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,16 @@ function SymbolicUtils.setmetadata(sys::AbstractSystem, k::DataType, v)
@set sys.metadata = meta
end

"""
$(TYPEDSIGNATURES)

Metadata key for systems containing the `problem_type` to be passed to the problem
constructor, where applicable. For example, if `getmetadata(sys, ProblemTypeCtx, nothing)`
is `CustomType()` then `ODEProblem(sys, ...).problem_type` will be `CustomType()` instead
of `StandardODEProblem`.
"""
struct ProblemTypeCtx end

"""
$(TYPEDSIGNATURES)
"""
Expand Down
8 changes: 8 additions & 0 deletions test/nonlinearsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,11 @@ end
@test resid == [0.0]
@test resid isa SVector
end

@testset "`ProblemTypeCtx`" begin
@variables x
@mtkcompile sys = System(
[0 ~ x^2 - 4x + 4]; metadata = [ModelingToolkit.ProblemTypeCtx => "A"])
prob = NonlinearProblem(sys, [x => 1.0])
@test prob.problem_type == "A"
end
8 changes: 8 additions & 0 deletions test/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1566,3 +1566,11 @@ end
@test !process_running(proc)
kill(proc, Base.SIGKILL)
end

@testset "`ProblemTypeCtx`" begin
@variables x(t)
@mtkcompile sys = System(
[D(x) ~ x], t; metadata = [ModelingToolkit.ProblemTypeCtx => "A"])
prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0))
@test prob.problem_type == "A"
end
Loading