From f7bf33ac394c5379cc229592e0974777f53badff Mon Sep 17 00:00:00 2001 From: Christopher Tessum Date: Sun, 13 Oct 2024 14:10:41 -0500 Subject: [PATCH 1/3] Don't check units during initialization ...because initialization problems don't correctly handle units and units have already been checked during system initialization. --- src/systems/problem_utils.jl | 4 ++-- test/initializationsystem.jl | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 3e50cc41cc..695e489db0 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -396,7 +396,7 @@ Keyword arguments: - `eval_module`: If `eval_expression == true`, the module to `eval` into. Otherwise, the module in which to generate the `RuntimeGeneratedFunction`. - `fully_determined`: Override whether the initialization system is fully determined. -- `check_units`: Enable or disable unit checks. +- `check_units`: Enable or disable unit checks when constructing the initialization problem. - `tofloat`, `use_union`: Passed to [`better_varmap_to_vars`](@ref) for building `u0` (and possibly `p`). - `u0_constructor`: A function to apply to the `u0` value returned from `better_varmap_to_vars` @@ -414,7 +414,7 @@ function process_SciMLProblem( implicit_dae = false, t = nothing, guesses = AnyDict(), warn_initialize_determined = true, initialization_eqs = [], eval_expression = false, eval_module = @__MODULE__, fully_determined = false, - check_units = true, tofloat = true, use_union = false, + check_units = false, tofloat = true, use_union = false, u0_constructor = identity, du0map = nothing, check_length = true, symbolic_u0 = false, kwargs...) dvs = unknowns(sys) ps = parameters(sys) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 3eddba2b78..c9783c9659 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -3,6 +3,7 @@ using ForwardDiff using SymbolicIndexingInterface, SciMLStructures using SciMLStructures: Tunable using ModelingToolkit: t_nounits as t, D_nounits as D +using DynamicQuantities @parameters g @variables x(t) y(t) [state_priority = 10] λ(t) @@ -860,3 +861,19 @@ end integ = init(prob) @test integ[x] ≈ [1.0, 3.0] end + +@testset "units" begin + t = ModelingToolkit.t + D = ModelingToolkit.D + @parameters g [unit = u"m/s^2"] L=1 [unit = u"m^2"] + @variables x(t) [unit = u"m"] y(t) [unit = u"m" state_priority = 10] λ(t) [unit = u"s^-2"] + eqs = [D(D(x)) ~ λ * x + D(D(y)) ~ λ * y - g + x^2 + y^2 ~ L] + @mtkbuild pend = ODESystem(eqs, t) + + prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], + guesses = ModelingToolkit.missing_variable_defaults(pend)) + sol = solve(prob, Rodas5P()) + @test SciMLBase.successful_retcode(sol) +end From 0f5cf1a9970b266a4f8c4ff963cd79e0b9a883c3 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Oct 2024 14:49:00 +0530 Subject: [PATCH 2/3] refactor: rename `check_units` to `check_initialization_units` --- src/systems/problem_utils.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 695e489db0..f7f13b9e19 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -396,7 +396,8 @@ Keyword arguments: - `eval_module`: If `eval_expression == true`, the module to `eval` into. Otherwise, the module in which to generate the `RuntimeGeneratedFunction`. - `fully_determined`: Override whether the initialization system is fully determined. -- `check_units`: Enable or disable unit checks when constructing the initialization problem. +- `check_initialization_units`: Enable or disable unit checks when constructing the + initialization problem. - `tofloat`, `use_union`: Passed to [`better_varmap_to_vars`](@ref) for building `u0` (and possibly `p`). - `u0_constructor`: A function to apply to the `u0` value returned from `better_varmap_to_vars` @@ -414,7 +415,7 @@ function process_SciMLProblem( implicit_dae = false, t = nothing, guesses = AnyDict(), warn_initialize_determined = true, initialization_eqs = [], eval_expression = false, eval_module = @__MODULE__, fully_determined = false, - check_units = false, tofloat = true, use_union = false, + check_initialization_units = false, tofloat = true, use_union = false, u0_constructor = identity, du0map = nothing, check_length = true, symbolic_u0 = false, kwargs...) dvs = unknowns(sys) ps = parameters(sys) @@ -464,7 +465,7 @@ function process_SciMLProblem( !isempty(initialization_equations(sys))) && t !== nothing initializeprob = ModelingToolkit.InitializationProblem( sys, t, u0map, pmap; guesses, warn_initialize_determined, - initialization_eqs, eval_expression, eval_module, fully_determined, check_units) + initialization_eqs, eval_expression, eval_module, fully_determined, check_units = check_initialization_units) initializeprobmap = getu(initializeprob, unknowns(sys)) punknowns = [p From 616e5d1f3eae00bedb12071b979af99e685bdccc Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Mon, 28 Oct 2024 15:03:25 +0530 Subject: [PATCH 3/3] refactor: format --- src/systems/problem_utils.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index f7f13b9e19..977f637d47 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -465,7 +465,8 @@ function process_SciMLProblem( !isempty(initialization_equations(sys))) && t !== nothing initializeprob = ModelingToolkit.InitializationProblem( sys, t, u0map, pmap; guesses, warn_initialize_determined, - initialization_eqs, eval_expression, eval_module, fully_determined, check_units = check_initialization_units) + initialization_eqs, eval_expression, eval_module, fully_determined, + check_units = check_initialization_units) initializeprobmap = getu(initializeprob, unknowns(sys)) punknowns = [p