From 1a1eefbb54b69da8fec8a0fe4cfd998b1b462e22 Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Fri, 20 Nov 2026 17:14:19 +0100 Subject: [PATCH 1/2] feat(metadata): the grid of a problem by modeling, coarser for ModelingToolkit (#435, T5 of #449) grid_size_data(problem, backend): the entry of the optional field :grid_sizes, else the default of the modeling, the grid of the problem but for ModelingToolkit (at most 50, solved by shooting). The JuMP models, the convergence test and the problem pages use it; the linter checks :grid_sizes. Co-Authored-By: Claude Opus 5.5 --- docs/problems.jl | 4 +-- ext/JuMPModels/balanced_field.jl | 2 +- ext/JuMPModels/beam.jl | 2 +- ext/JuMPModels/bioreactor.jl | 2 +- ext/JuMPModels/brachistochrone.jl | 2 +- ext/JuMPModels/bryson_denham.jl | 2 +- ext/JuMPModels/cart_pendulum.jl | 2 +- ext/JuMPModels/chain.jl | 2 +- ext/JuMPModels/dielectrophoretic_particle.jl | 2 +- ext/JuMPModels/double_oscillator.jl | 2 +- ext/JuMPModels/ducted_fan.jl | 2 +- ext/JuMPModels/electric_vehicle.jl | 2 +- ext/JuMPModels/glider.jl | 2 +- ext/JuMPModels/insurance.jl | 2 +- ext/JuMPModels/jackson.jl | 2 +- ext/JuMPModels/moonlander.jl | 2 +- ext/JuMPModels/mountain_car.jl | 2 +- ext/JuMPModels/robbins.jl | 2 +- ext/JuMPModels/robot.jl | 2 +- ext/JuMPModels/rocket.jl | 2 +- ext/JuMPModels/space_shuttle.jl | 2 +- ext/JuMPModels/steering.jl | 2 +- ext/JuMPModels/vanderpol.jl | 2 +- ext/README.md | 7 ++-- src/Metadata/Metadata.jl | 3 +- src/Metadata/access.jl | 38 ++++++++++++++++++++ src/Metadata/loading.jl | 2 +- test/common/modeling_tests.jl | 2 +- test/suite/core/test_lint.jl | 13 ++++++- test/suite/core/test_parameters.jl | 27 ++++++++++++++ tools/lint/ProblemLint.jl | 17 ++++++++- tools/new/templates/JuMP.jl | 2 +- 32 files changed, 127 insertions(+), 32 deletions(-) diff --git a/docs/problems.jl b/docs/problems.jl index c16830b97..4a11a1863 100644 --- a/docs/problems.jl +++ b/docs/problems.jl @@ -117,7 +117,7 @@ function parameters_section(problem::Symbol) isnothing(params) && return "### [Parameters](@id $(anchor(problem, :parameters)))\n\nThe problem has no parameters.\n" info = get(OCP.problem_metadata(problem), :parameter_info, nothing) - unit(k) = (u = info[k].unit; isnothing(u) ? "–" : u) + unit(k) = (u=info[k].unit; isnothing(u) ? "–" : u) rows = if isnothing(info) join(("| `$(k)` | `$(v)` | | |" for (k, v) in pairs(params)), "\n") else @@ -250,7 +250,7 @@ function modeling_panel(problem::Symbol, spec, adapter, refs) status = OCP.status(problem)[spec.name] file = problem_file(problem, spec) text = read(joinpath(ROOT, file), String) - grid_size = OCP.grid_size_data(problem) + grid_size = OCP.grid_size_data(problem, spec.backend()) notes = get(OCP.problem_metadata(problem), :notes, nothing) draft = if status === :draft """ diff --git a/ext/JuMPModels/balanced_field.jl b/ext/JuMPModels/balanced_field.jl index 22a5a519f..04fa355fd 100644 --- a/ext/JuMPModels/balanced_field.jl +++ b/ext/JuMPModels/balanced_field.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.balanced_field( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:balanced_field), + grid_size::Int=grid_size_data(:balanced_field, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/beam.jl b/ext/JuMPModels/beam.jl index f64db5d91..97eea6576 100644 --- a/ext/JuMPModels/beam.jl +++ b/ext/JuMPModels/beam.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.beam( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:beam), + grid_size::Int=grid_size_data(:beam, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/bioreactor.jl b/ext/JuMPModels/bioreactor.jl index fe839dd2c..17226e506 100644 --- a/ext/JuMPModels/bioreactor.jl +++ b/ext/JuMPModels/bioreactor.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.bioreactor( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:bioreactor), + grid_size::Int=grid_size_data(:bioreactor, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/brachistochrone.jl b/ext/JuMPModels/brachistochrone.jl index 1e63a3c56..be76e30e2 100644 --- a/ext/JuMPModels/brachistochrone.jl +++ b/ext/JuMPModels/brachistochrone.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.brachistochrone( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:brachistochrone), + grid_size::Int=grid_size_data(:brachistochrone, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/bryson_denham.jl b/ext/JuMPModels/bryson_denham.jl index f1320a67f..9db758eab 100644 --- a/ext/JuMPModels/bryson_denham.jl +++ b/ext/JuMPModels/bryson_denham.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.bryson_denham( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:bryson_denham), + grid_size::Int=grid_size_data(:bryson_denham, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/cart_pendulum.jl b/ext/JuMPModels/cart_pendulum.jl index 4215988d8..41926e66c 100644 --- a/ext/JuMPModels/cart_pendulum.jl +++ b/ext/JuMPModels/cart_pendulum.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.cart_pendulum( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:cart_pendulum), + grid_size::Int=grid_size_data(:cart_pendulum, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/chain.jl b/ext/JuMPModels/chain.jl index 24d4496b0..2458f37fd 100644 --- a/ext/JuMPModels/chain.jl +++ b/ext/JuMPModels/chain.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.chain( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:chain), + grid_size::Int=grid_size_data(:chain, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/dielectrophoretic_particle.jl b/ext/JuMPModels/dielectrophoretic_particle.jl index 7d78637b1..66a09cd30 100644 --- a/ext/JuMPModels/dielectrophoretic_particle.jl +++ b/ext/JuMPModels/dielectrophoretic_particle.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.dielectrophoretic_particle( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:dielectrophoretic_particle), + grid_size::Int=grid_size_data(:dielectrophoretic_particle, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/double_oscillator.jl b/ext/JuMPModels/double_oscillator.jl index faa68ae85..1b49a0fd7 100644 --- a/ext/JuMPModels/double_oscillator.jl +++ b/ext/JuMPModels/double_oscillator.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.double_oscillator( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:double_oscillator), + grid_size::Int=grid_size_data(:double_oscillator, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/ducted_fan.jl b/ext/JuMPModels/ducted_fan.jl index eb3a7bde3..f5462540c 100644 --- a/ext/JuMPModels/ducted_fan.jl +++ b/ext/JuMPModels/ducted_fan.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.ducted_fan( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:ducted_fan), + grid_size::Int=grid_size_data(:ducted_fan, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/electric_vehicle.jl b/ext/JuMPModels/electric_vehicle.jl index 0996bf04f..a49acdeb5 100644 --- a/ext/JuMPModels/electric_vehicle.jl +++ b/ext/JuMPModels/electric_vehicle.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.electric_vehicle( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:electric_vehicle), + grid_size::Int=grid_size_data(:electric_vehicle, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/glider.jl b/ext/JuMPModels/glider.jl index 7c6883063..cce354e77 100644 --- a/ext/JuMPModels/glider.jl +++ b/ext/JuMPModels/glider.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.glider( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:glider), + grid_size::Int=grid_size_data(:glider, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/insurance.jl b/ext/JuMPModels/insurance.jl index 35c6652f2..037b133e8 100644 --- a/ext/JuMPModels/insurance.jl +++ b/ext/JuMPModels/insurance.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.insurance( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:insurance), + grid_size::Int=grid_size_data(:insurance, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/jackson.jl b/ext/JuMPModels/jackson.jl index 1a95356fd..fabb531f2 100644 --- a/ext/JuMPModels/jackson.jl +++ b/ext/JuMPModels/jackson.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.jackson( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:jackson), + grid_size::Int=grid_size_data(:jackson, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/moonlander.jl b/ext/JuMPModels/moonlander.jl index a25bbb269..030ba4d0a 100644 --- a/ext/JuMPModels/moonlander.jl +++ b/ext/JuMPModels/moonlander.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.moonlander( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:moonlander), + grid_size::Int=grid_size_data(:moonlander, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/mountain_car.jl b/ext/JuMPModels/mountain_car.jl index 765924bfc..97acf7d19 100644 --- a/ext/JuMPModels/mountain_car.jl +++ b/ext/JuMPModels/mountain_car.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.mountain_car( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:mountain_car), + grid_size::Int=grid_size_data(:mountain_car, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/robbins.jl b/ext/JuMPModels/robbins.jl index a11a048f9..c612bcae7 100644 --- a/ext/JuMPModels/robbins.jl +++ b/ext/JuMPModels/robbins.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.robbins( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:robbins), + grid_size::Int=grid_size_data(:robbins, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/robot.jl b/ext/JuMPModels/robot.jl index 53d708b45..7d1216662 100644 --- a/ext/JuMPModels/robot.jl +++ b/ext/JuMPModels/robot.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.robot( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:robot), + grid_size::Int=grid_size_data(:robot, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/rocket.jl b/ext/JuMPModels/rocket.jl index 486420839..5ad79712f 100644 --- a/ext/JuMPModels/rocket.jl +++ b/ext/JuMPModels/rocket.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.rocket( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:rocket), + grid_size::Int=grid_size_data(:rocket, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/space_shuttle.jl b/ext/JuMPModels/space_shuttle.jl index da9c80cab..32f1ede5d 100644 --- a/ext/JuMPModels/space_shuttle.jl +++ b/ext/JuMPModels/space_shuttle.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.space_shuttle( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:space_shuttle), + grid_size::Int=grid_size_data(:space_shuttle, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/steering.jl b/ext/JuMPModels/steering.jl index efd1629db..6a26fc380 100644 --- a/ext/JuMPModels/steering.jl +++ b/ext/JuMPModels/steering.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.steering( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:steering), + grid_size::Int=grid_size_data(:steering, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/JuMPModels/vanderpol.jl b/ext/JuMPModels/vanderpol.jl index 7bd636de9..8b0fcc037 100644 --- a/ext/JuMPModels/vanderpol.jl +++ b/ext/JuMPModels/vanderpol.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.vanderpol( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:vanderpol), + grid_size::Int=grid_size_data(:vanderpol, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, diff --git a/ext/README.md b/ext/README.md index b6f417b5d..23e36b2b7 100644 --- a/ext/README.md +++ b/ext/README.md @@ -41,8 +41,11 @@ ext/ - `:status`, one entry per modeling where the problem has a file, `:draft` to start with; - `:init`, the initial guess shared by every modeling, a function of the parameters returning `(state=…, control=…, variable=…)`; - - optionally `:notes` (why a modeling is a draft) and `:reference_solution` (the analytic - solution of the continuous problem, see `src/Metadata/solutions.jl`). + - optionally `:notes` (why a modeling is a draft), `:reference_solution` (the analytic + solution of the continuous problem, see `src/Metadata/solutions.jl`) and `:grid_sizes` + (the grid in a modeling where the default of the modeling does not suit the problem: + `grid_size_data(problem, backend)` reads `:grid_size`, but ModelingToolkit, solved by + shooting, takes a coarser one). The schema is `METADATA_SCHEMA` in `src/Metadata/loading.jl`, checked when the package loads. diff --git a/src/Metadata/Metadata.jl b/src/Metadata/Metadata.jl index 2dc522456..95f0a9838 100644 --- a/src/Metadata/Metadata.jl +++ b/src/Metadata/Metadata.jl @@ -4,13 +4,14 @@ The metadata of the problems: the list of the problems (one file per problem in them: `problem_metadata`, `grid_size_data`, `parameters_data`, `initial_guess_data`. The analytic solutions used by some metadata files (`reference_solution`) are defined here too. -Depends on no other submodule. +Depends on `Modelings`: the grid of a problem depends on the modeling (`grid_size_data`). """ module Metadata using CTBase: CTBase using DocStringExtensions: TYPEDSIGNATURES using OrderedCollections: OrderedDict +using ..Modelings: Modelings include(joinpath(@__DIR__, "discovery.jl")) include(joinpath(@__DIR__, "solutions.jl")) diff --git a/src/Metadata/access.jl b/src/Metadata/access.jl index c12c9eb4d..f02d14ec6 100644 --- a/src/Metadata/access.jl +++ b/src/Metadata/access.jl @@ -23,6 +23,11 @@ The following keys are valid: ```julia :grid_size => 500, ``` +- `grid_sizes::NamedTuple` (optional): the number of steps of the problem in some modelings, + where it differs from the default of the modeling (see `grid_size_data`). For example: +```julia +:grid_sizes => (ModelingToolkit=100,), +``` - `parameters::Union{Nothing,NamedTuple}`: the list of parameters. For example: ```julia :parameters => ( @@ -95,6 +100,39 @@ function grid_size_data(problem::Symbol) return problem_metadata(problem)[:grid_size] end +""" +The default number of steps of a ModelingToolkit problem: it is solved by direct shooting +(Corleone), one integration per control interval, which needs a coarser grid than a +collocation (#435). At most the grid of the problem. +""" +const MODELING_TOOLKIT_GRID_SIZE = 50 + +_grid_size(problem::Symbol, ::Modelings.AbstractModelBackend) = grid_size_data(problem) +function _grid_size(problem::Symbol, ::Modelings.ModelingToolkitBackend) + return min(grid_size_data(problem), MODELING_TOOLKIT_GRID_SIZE) +end + +""" +$(TYPEDSIGNATURES) + +Return the default number of steps of `problem` in the modeling of `backend`: the entry of +the modeling in the field `:grid_sizes` of the metadata when there is one, otherwise the +default of the modeling, the grid of the problem (`grid_size_data(problem)`) but for +ModelingToolkit, solved by shooting on a coarser grid (`MODELING_TOOLKIT_GRID_SIZE`). + +# Example + +```julia-repl +julia> grid_size_data(:beam, JuMPBackend()) +500 +``` +""" +function grid_size_data(problem::Symbol, backend::Modelings.AbstractModelBackend) + grid_sizes = get(problem_metadata(problem), :grid_sizes, (;)) + name = Modelings._modeling_spec(backend).name + return haskey(grid_sizes, name) ? grid_sizes[name] : _grid_size(problem, backend) +end + """ $(TYPEDSIGNATURES) diff --git a/src/Metadata/loading.jl b/src/Metadata/loading.jl index 70bfd1b49..4dcec4e99 100644 --- a/src/Metadata/loading.jl +++ b/src/Metadata/loading.jl @@ -14,7 +14,7 @@ const METADATA_SCHEMA = ( status=NamedTuple, init=Function, ), - optional=(notes=AbstractString, reference_solution=Function), + optional=(notes=AbstractString, reference_solution=Function, grid_sizes=NamedTuple), generated=(reference_objective=NamedTuple,), ) const METADATA_STORAGE = OrderedDict{Symbol,OrderedDict}() diff --git a/test/common/modeling_tests.jl b/test/common/modeling_tests.jl index 349a0aa65..0be9196b3 100644 --- a/test/common/modeling_tests.jl +++ b/test/common/modeling_tests.jl @@ -128,7 +128,7 @@ function test_convergence(backend, adapter) test = "test_convergence $(Main.TestConfigs.label(config))" Main.TestStatus.for_each_problem(test, backend) do problem pb = getfield(OCP, problem)(backend) - N = OCP.grid_size_data(problem) + N = OCP.grid_size_data(problem, backend) scheme = adapter.scheme(config) warmup = get(adapter, :warmup, nothing) if isnothing(warmup) diff --git a/test/suite/core/test_lint.jl b/test/suite/core/test_lint.jl index d813111d2..56a7a7d28 100644 --- a/test/suite/core/test_lint.jl +++ b/test/suite/core/test_lint.jl @@ -64,13 +64,16 @@ end info(names...) = NamedTuple{names}(Tuple((unit=nothing, meaning="m") for _ in names)) -function metadata(; status=(OptimalControl=:valid,), parameter_info=:default) +function metadata(; + status=(OptimalControl=:valid,), parameter_info=:default, grid_sizes=nothing +) parameters = (t0=0, tf=1, x_t0=0, x_u=1, guess=0.5) data = Dict{Symbol,Any}(:grid_size => 100, :parameters => parameters, :status => status) parameter_info === nothing || ( data[:parameter_info] = parameter_info === :default ? info(keys(parameters)...) : parameter_info ) + isnothing(grid_sizes) || (data[:grid_sizes] = grid_sizes) return data end @@ -149,6 +152,14 @@ function test_lint() Test.@test fails(example(; parameter_info=bad), "not guess") end + Test.@testset "grid per modeling" begin + Test.@test !fails(example(; grid_sizes=(OptimalControl=50,)), "`:grid_sizes`") + Test.@test fails(example(; grid_sizes=(JuMP=50,)), "`:grid_sizes`: a positive") + Test.@test fails( + example(; grid_sizes=(OptimalControl=0,)), "not OptimalControl" + ) + end + Test.@testset "template of a model" begin for (needle, what) in ( ("\"\"\"The foo problem.\"\"\"\n" => "", "a docstring"), diff --git a/test/suite/core/test_parameters.jl b/test/suite/core/test_parameters.jl index 4e94a7ae5..b7ef2ae23 100644 --- a/test/suite/core/test_parameters.jl +++ b/test/suite/core/test_parameters.jl @@ -32,6 +32,33 @@ function test_parameters() delete!(storage, :_no_parameters) end end + Test.@testset "grid_size_data by modeling" verbose = VERBOSE showtiming = SHOWTIMING begin + OCP = OptimalControlProblems + mtk = OCP.Metadata.MODELING_TOOLKIT_GRID_SIZE + for problem in OCP.problems(; status=:all) + haskey(OCP.problem_metadata(problem), :grid_sizes) && continue + N = OCP.grid_size_data(problem) + Test.@test OCP.grid_size_data(problem, OCP.JuMPBackend()) == N + Test.@test OCP.grid_size_data(problem, OCP.OptimalControlBackend()) == N + Test.@test OCP.grid_size_data(problem, OCP.ModelingToolkitBackend()) == + min(N, mtk) + end + # the grid of a problem in a modeling, given in its metadata: no problem of the + # collection has one, so one is added for the test + storage = OCP.Metadata.METADATA_STORAGE + storage[:_grid_sizes] = OCP.Metadata.OrderedDict{Symbol,Any}( + :grid_size => 500, :grid_sizes => (ModelingToolkit=120, JuMP=200) + ) + try + Test.@test OCP.grid_size_data(:_grid_sizes) == 500 + Test.@test OCP.grid_size_data(:_grid_sizes, OCP.ModelingToolkitBackend()) == 120 + Test.@test OCP.grid_size_data(:_grid_sizes, OCP.JuMPBackend()) == 200 + Test.@test OCP.grid_size_data(:_grid_sizes, OCP.OptimalControlBackend()) == 500 + finally + delete!(storage, :_grid_sizes) + end + end + Test.@testset "problem_metadata" verbose = VERBOSE showtiming = SHOWTIMING begin problem = first(OptimalControlProblems.problems(; status=:all)) data = OptimalControlProblems.problem_metadata(problem) diff --git a/tools/lint/ProblemLint.jl b/tools/lint/ProblemLint.jl index 2fcee96ce..c8c01cdd1 100644 --- a/tools/lint/ProblemLint.jl +++ b/tools/lint/ProblemLint.jl @@ -150,7 +150,10 @@ function template(modeling::Symbol, problem::Symbol, backend::AbstractString) elseif modeling === :JuMP push!( items, - ("the keyword `grid_size`", Regex("grid_size::Int=grid_size_data\\(:$p\\)")), + ( + "the keyword `grid_size`", + Regex("grid_size::Int=grid_size_data\\(:$p, JuMPBackend\\(\\)\\)"), + ), ) push!(items, ("the keyword `scheme`", r"scheme::Symbol=:trapeze")) end @@ -270,6 +273,18 @@ function checks(src::ProblemSource) ) end + # the metadata: a grid per modeling only where the problem has a model + grid_sizes = get(src.metadata, :grid_sizes, (;)) + odd = [ + m for (m, n) in pairs(grid_sizes) if !haskey(statuses, m) || !(n isa Int && n > 0) + ] + add( + isempty(odd), + "metadata", + "`:grid_sizes`: a positive `Int` per modeling with a status" * + (isempty(odd) ? "" : ": not $(join(odd, ", "))"), + ) + # the metadata: the unit and meaning of every parameter parameters = src.metadata[:parameters] names = isnothing(parameters) ? String[] : String.(collect(keys(parameters))) diff --git a/tools/new/templates/JuMP.jl b/tools/new/templates/JuMP.jl index 72a5ae966..2cff6f8d5 100644 --- a/tools/new/templates/JuMP.jl +++ b/tools/new/templates/JuMP.jl @@ -18,7 +18,7 @@ Default parameters and the modeling status are available through function OptimalControlProblems.{{problem}}( ::JuMPBackend, args...; - grid_size::Int=grid_size_data(:{{problem}}), + grid_size::Int=grid_size_data(:{{problem}}, JuMPBackend()), scheme::Symbol=:trapeze, ordering::Symbol=:interleaved, parameters::Union{Nothing,NamedTuple}=nothing, From 35a7102b746356281860862c040b702993802949 Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Fri, 20 Nov 2026 18:15:17 +0100 Subject: [PATCH 2/2] docs(changelog): grid_size_data by modeling Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ed478761..4d7196660 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,10 @@ See [BREAKING.md](BREAKING.md). `objective(sol)` and `iterations(sol)` work on it. The JuMP costate uses the sign convention of `rules/signs.md` (`-JuMP.dual`, the same as OptimalControl, #326) and is stamped at the step midpoints. +- `grid_size_data(problem, backend)`: the default grid of a problem in a modeling, the grid + of the problem but for ModelingToolkit, solved by shooting on a coarser one (at most 50 + steps); a problem can set it per modeling with the optional metadata field `:grid_sizes` + (#435). ### 🛠 Enhancements