SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.43k stars 209 forks source link

Jacobean of comparison operator #2177

Open ArnoStrouwen opened 1 year ago

ArnoStrouwen commented 1 year ago

docs recommend doing branching using comparison operator https://docs.sciml.ai/ModelingToolkit/stable/basics/FAQ/#How-do-I-handle-if-statements-in-my-symbolic-forms but I think the Jacobean is not generated correctly?

using ModelingToolkit
using OrdinaryDiffEq
@variables t
@variables a(t) = 1.0
D = Differential(t)
@named sys = ODESystem([D(a) ~ -a*(a>0) + a*(a<0)])
prob = ODEProblem(sys, [], (0.0,1.0),jac=true)
RuntimeGeneratedFunction(#=in ModelingToolkit=#, #=using ModelingToolkit=#, :((ˍ₋out, ˍ₋arg1, ˍ₋arg2, t)->begin
          #= C:\Users\arno\.julia\packages\SymbolicUtils\H684H\src\code.jl:350 =#
          #= C:\Users\arno\.julia\packages\SymbolicUtils\H684H\src\code.jl:351 =#
          #= C:\Users\arno\.julia\packages\SymbolicUtils\H684H\src\code.jl:352 =#
          begin
              begin
                  begin
                      #= C:\Users\arno\.julia\packages\Symbolics\3jLt1\src\build_function.jl:520 =#
                      #= C:\Users\arno\.julia\packages\SymbolicUtils\H684H\src\code.jl:399 =# @inbounds begin
                              #= C:\Users\arno\.julia\packages\SymbolicUtils\H684H\src\code.jl:395 =#
                              ˍ₋out[1] = (+)((+)((+)((*)(-1, (>)(ˍ₋arg1[1], 0)), (*)(ˍ₋arg1[1], (Differential(a(t)))((<)(ˍ₋arg1[1], 0)))), (*)((*)(-1, ˍ₋arg1[1]), (Differential(a(t)))((>)(ˍ₋arg1[1], 0)))), (<)(ˍ₋arg1[1], 0))
                              #= C:\Users\arno\.julia\packages\SymbolicUtils\H684H\src\code.jl:397 =#
                              nothing
                          end
                  end
              end
          end
      end))
ChrisRackauckas commented 1 year ago

Can you clarify what looks wrong here?

baggepinnen commented 1 year ago

It still has

(Differential(a(t)))((<)(ˍ

In the generated code

ChrisRackauckas commented 1 year ago

Ahh yeah, that's just done for derivatives which aren't defined so it should error. We could make that throw a nicer error earlier.

ChrisRackauckas commented 9 months ago

Differential was removed so this was handled.

baggepinnen commented 9 months ago

Differential hasn't been removed, and this issue is still present on v9.0.1 Chris, maybe you were referring to Difference?

julia> prob.f.jac.jac_oop
RuntimeGeneratedFunction(#=in ModelingToolkit=#, #=using ModelingToolkit=#, :((ˍ₋arg1, t)->begin
          #= /home/fredrikb/.julia/packages/SymbolicUtils/r1pzW/src/code.jl:373 =#
          #= /home/fredrikb/.julia/packages/SymbolicUtils/r1pzW/src/code.jl:374 =#
          #= /home/fredrikb/.julia/packages/SymbolicUtils/r1pzW/src/code.jl:375 =#
          begin
              begin
                  begin
                      #= /home/fredrikb/.julia/packages/SymbolicUtils/r1pzW/src/code.jl:468 =#
                      (SymbolicUtils.Code.create_array)(typeof(ˍ₋arg1), nothing, Val{2}(), Val{(1, 1)}(), (+)((+)((+)((*)(-1, (>)(ˍ₋arg1[1], 0)), (<)(ˍ₋arg1[1], 0)), (*)((*)(-1, ˍ₋arg1[1]), (Differential(a(t)))((>)(ˍ₋arg1[1], 0)))), (*)(ˍ₋arg1[1], (Differential(a(t)))((<)(ˍ₋arg1[1], 0)))))
                  end
              end
          end
      end))
ChrisRackauckas commented 9 months ago

I think I closed the wrong tab.