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

Do not attempt to promote to Union #3381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

baggepinnen
Copy link
Contributor

Sometimes symtype returns a union like Union{Float64, Int64}

julia> @variables x::Float64
1-element Vector{Num}:
 x

julia> ModelingToolkit.symtype(ifelse(x > 0, x, 0))
Union{Float64, Int64}

and then

buf = get!(() -> Any[], cachevars, T)

fails.

This is (very) likely to occur with JSML, which appears to type things with Float64

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.

Sometimes `symtype` returns a union like `Union{Float64, Int64}`
```julia
julia> @variables x::Float64
1-element Vector{Num}:
 x

julia> ModelingToolkit.symtype(ifelse(x > 0, x, 0))
Union{Float64, Int64}
```


and then 
```julia
buf = get!(() -> Any[], cachevars, T)
```
fails.

This is (very) likely to occur with JSML, which appears to type things with `Float64`
@ChrisRackauckas
Copy link
Member

This is (very) likely to occur with JSML, which appears to type things with Float64

It shouldn't since that's just wrong.

@AayushSabharwal
Copy link
Member

Yeah ::Float64 annotations might cause problems with putting duals in some places.

@AayushSabharwal
Copy link
Member

Also, isn't ifelse(x > 0, x, 0) inherently type-unstable? Regardless of symbolic stuff it would be better to write ifelse(x > 0, x, 0.0) anyway (or zero(x)).

@ChrisRackauckas
Copy link
Member

We should enforce the type checks soon. If we do that, then code like this will just error during autodiff, as the user should expect because the assertion would fail.

@baggepinnen
Copy link
Contributor Author

baggepinnen commented Feb 11, 2025

It is type unstable, but a component definition like this

@component function Ramp(; name, start_time=0, offset=0, duration=nothing, height=nothing)
  params = @parameters begin
    (start_time::Float64 = start_time)
    (offset::Float64 = offset)
    (duration::Float64 = duration)
    (height::Float64 = height)
  end
  vars = @variables begin
    y(t), [output = true]
  end
  eqs = Equation[
    y ~ ifelse(start_time < t, ifelse(t < start_time + duration, offset + (t - start_time) * height / duration, offset + height), offset)
  ]
  return ODESystem(eqs, t, vars, params; systems = [], name)
end

appears to result in this error where the default values for the parameters, 0::Int, somehow appears inside the ifelse expression despite the declaration offset::Float64

@AayushSabharwal
Copy link
Member

If a constant appears inside the expression instead of the symbolic expression, that's a much bigger problem with a different solution 😅 since that means the model is incorrectly parsed and changing offset in the ODEProblem won't actually change the ifelse argument.

@baggepinnen
Copy link
Contributor Author

baggepinnen commented Feb 11, 2025

@AayushSabharwal The WaterTreatmentPlant scirpt I pointed you to should reproduce the error that prompted this PR as well

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.

3 participants