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

feat: improve error message when system contains array equations #3081

Merged

Conversation

AayushSabharwal
Copy link
Member

Close #2597

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

@ChrisRackauckas
Copy link
Member

Why would this be required? The codegen doesn't require it if it's a true ODE.

@AayushSabharwal
Copy link
Member Author

AayushSabharwal commented Sep 30, 2024

It doesn't really work very reliably.

@variables x(t)[1:2]
@named sys = ODESystem(D(x) ~ x, t)
sys = complete(sys)
prob = ODEProblem(sys, ...)

This errors in calculate_massmatrix because that can't handle array equations and needs them scalarized. If I hack around that and comment out check_eqs_u0 this errors in generate_function because check_lhs finds that the variable inside the derivative D(x) isn't in the list of unknowns.

I could make these cases work, but all of the fixes boil down to scalarizing the equations and/or unknowns.

@AayushSabharwal
Copy link
Member Author

AayushSabharwal commented Sep 30, 2024

And if there are multiple array unknowns or a mix of array/scalars then codegen also won't work without scalarizing the equations.

@ChrisRackauckas
Copy link
Member

calculate_massmatrix needs to semantically scalarize and show it's constant.

And if there are multiple array unknowns or a mix of array/scalars then codegen also won't work without scalarizing the equations.

Is that really the case? Symbolics.jl handled this awhile back.

@ChrisRackauckas
Copy link
Member

I see no issue with a temporary error message that told people as a temporary workaround this is required, though I think we should look at some of the codegen here since Symbolics should already handle these cases.

@AayushSabharwal
Copy link
Member Author

AayushSabharwal commented Sep 30, 2024

The problem with the codegen is that this system:

julia> @named sys = ODESystem([D(x) ~ x, D(y) ~ t], t, [x..., y])

Generates this code:

RuntimeGeneratedFunction(#=in ModelingToolkit=#, #=using ModelingToolkit=#, :((ˍ₋out, ˍ₋arg1, t)->begin
          #= /Users/aayush/.julia/packages/SymbolicUtils/ij6YM/src/code.jl:385 =#
          #= /Users/aayush/.julia/packages/SymbolicUtils/ij6YM/src/code.jl:386 =#
          #= /Users/aayush/.julia/packages/SymbolicUtils/ij6YM/src/code.jl:387 =#
          begin
              begin
                  var"x(t)" = view(ˍ₋arg1, 1:2)
                  begin
                      begin
                          #= /Users/aayush/.julia/packages/Symbolics/XnDVB/src/build_function.jl:546 =#
                          #= /Users/aayush/.julia/packages/SymbolicUtils/ij6YM/src/code.jl:434 =# @inbounds begin
                                  #= /Users/aayush/.julia/packages/SymbolicUtils/ij6YM/src/code.jl:430 =#
                                  ˍ₋out[1] = var"x(t)"
                                  ˍ₋out[2] = t
                                  #= /Users/aayush/.julia/packages/SymbolicUtils/ij6YM/src/code.jl:432 =#
                                  nothing
                              end
                      end
                  end
              end
          end
      end))

Which assumes the first element of u is a Vector and the second a float.

Which again points to the fact that the system needs to be scalarized

@AayushSabharwal
Copy link
Member Author

Also without structural_simplify it's up to the user to ensure the equations and unknowns are in the correct order.

@ChrisRackauckas
Copy link
Member

Okay we should fix that, it's fixable. For now, mention that a temporary workaround is to scalarize or structural simplify.

@AayushSabharwal AayushSabharwal force-pushed the as/array-equations-error branch 2 times, most recently from bdfcba5 to f47c5d6 Compare October 4, 2024 06:20
@ChrisRackauckas ChrisRackauckas merged commit 0916a02 into SciML:master Oct 5, 2024
21 of 25 checks passed
@ChrisRackauckas
Copy link
Member

@AayushSabharwal AayushSabharwal deleted the as/array-equations-error branch October 5, 2024 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Symbolic-array system bug in counting equations and variables
2 participants