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

Initialization on non-DAE models #2512

Merged
merged 27 commits into from
Mar 1, 2024
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
66c29ef
WIP: Initialization on non-DAE models
ChrisRackauckas Feb 29, 2024
e61a0f9
use new ODE solver changes
ChrisRackauckas Feb 29, 2024
fbd3e1f
format
ChrisRackauckas Feb 29, 2024
a905587
Handle empty u0map
ChrisRackauckas Feb 29, 2024
186302f
late binding initialization_eqs
ChrisRackauckas Feb 29, 2024
fad548f
make sure u0map isn't a vector of numbers
ChrisRackauckas Feb 29, 2024
82cb87c
format
ChrisRackauckas Feb 29, 2024
7fb84b3
fix a few tests
ChrisRackauckas Feb 29, 2024
e7511d7
format
ChrisRackauckas Feb 29, 2024
d08fefe
fix condition
ChrisRackauckas Feb 29, 2024
59eae13
don't initialize SDEs
ChrisRackauckas Feb 29, 2024
3938441
fix a typo from tests
ChrisRackauckas Feb 29, 2024
d20095a
handle static arrays
ChrisRackauckas Feb 29, 2024
752f9e3
format
ChrisRackauckas Feb 29, 2024
6d5b6dd
Fix up a few tests / examples
ChrisRackauckas Feb 29, 2024
11d096f
format
ChrisRackauckas Feb 29, 2024
6542bba
Handle dummy derivative u0's and throw custom incomplete init error
ChrisRackauckas Feb 29, 2024
746386c
format
ChrisRackauckas Feb 29, 2024
7002310
don't filter empty u0maps
ChrisRackauckas Feb 29, 2024
4e5e723
handle arrays
ChrisRackauckas Feb 29, 2024
2147dc6
Handle the scalar u0map case
ChrisRackauckas Mar 1, 2024
674b8c4
fix filter
ChrisRackauckas Mar 1, 2024
1274908
fix components test
ChrisRackauckas Mar 1, 2024
49d8119
Handle steady state initializations
ChrisRackauckas Mar 1, 2024
96bfc35
format
ChrisRackauckas Mar 1, 2024
8f2c780
Fix some odd test choices
ChrisRackauckas Mar 1, 2024
a477f43
fix a few last tests
ChrisRackauckas Mar 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/systems/diffeqs/abstractodesystem.jl
Original file line number Diff line number Diff line change
@@ -889,7 +889,7 @@

zerovars = setdiff(unknowns(sys), keys(defaults(sys))) .=> 0.0
trueinit = identity.([zerovars; u0map])
u0map isa StaticArraysCore.StaticArray &&

Check warning on line 892 in src/systems/diffeqs/abstractodesystem.jl

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L892

Added line #L892 was not covered by tests
(trueinit = SVector{length(trueinit)}(trueinit))
else
initializeprob = nothing
@@ -1546,13 +1546,13 @@
variables:
"""

struct IncompleteInitializationError <: Exception
uninit
struct IncompleteInitializationError <: Exception
uninit::Any
end

function Base.showerror(io::IO, e::IncompleteInitializationError)
println(io, INCOMPLETE_INITIALIZATION_MESSAGE)
println(io, e.uninit)

Check warning on line 1555 in src/systems/diffeqs/abstractodesystem.jl

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1553-L1555

Added lines #L1553 - L1555 were not covered by tests
end

function InitializationProblem{iip, specialize}(sys::AbstractODESystem,
@@ -1575,9 +1575,9 @@
generate_initializesystem(sys; u0map); fully_determined = false)
end

uninit = setdiff(unknowns(sys),[unknowns(isys); getfield.(observed(isys),:lhs)])
uninit = setdiff(unknowns(sys), [unknowns(isys); getfield.(observed(isys), :lhs)])
if !isempty(uninit)
throw(IncompleteInitializationError(uninit))

Check warning on line 1580 in src/systems/diffeqs/abstractodesystem.jl

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1578-L1580

Added lines #L1578 - L1580 were not covered by tests
end

neqs = length(equations(isys))
2 changes: 1 addition & 1 deletion src/systems/nonlinear/initializesystem.jl
Original file line number Diff line number Diff line change
@@ -22,18 +22,18 @@
set_full_states = Set(full_states)
guesses = todict(guesses)
schedule = getfield(sys, :schedule)

if schedule !== nothing

Check warning on line 26 in src/systems/nonlinear/initializesystem.jl

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L26

Added line #L26 was not covered by tests
guessmap = [x[2] => get(guesses, x[1], default_dd_value)
for x in schedule.dummy_sub]
dd_guess = Dict(filter(x -> !isnothing(x[1]), guessmap))
filtered_u0 = todict([get(schedule.dummy_sub, x[1], x[1]) => x[2] for x in u0map])

Check warning on line 30 in src/systems/nonlinear/initializesystem.jl

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L29-L30

Added lines #L29 - L30 were not covered by tests
else
dd_guess = Dict()
filtered_u0 = u0map

Check warning on line 33 in src/systems/nonlinear/initializesystem.jl

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L32-L33

Added lines #L32 - L33 were not covered by tests
end

defs = merge(defaults(sys), filtered_u0)

Check warning on line 36 in src/systems/nonlinear/initializesystem.jl

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L36

Added line #L36 was not covered by tests
guesses = merge(get_guesses(sys), todict(guesses), dd_guess)

for st in full_states
@@ -57,7 +57,7 @@
end

pars = [parameters(sys); get_iv(sys)]
nleqs = [eqs_ics; get_initialization_eqs(sys); observed(sys)]

Check warning on line 60 in src/systems/nonlinear/initializesystem.jl

Codecov / codecov/patch

src/systems/nonlinear/initializesystem.jl#L60

Added line #L60 was not covered by tests

sys_nl = NonlinearSystem(nleqs,
full_states,
3 changes: 2 additions & 1 deletion test/initializationsystem.jl
Original file line number Diff line number Diff line change
@@ -331,7 +331,8 @@ p = [σ => 28.0,
β => 8 / 3]

tspan = (0.0, 100.0)
@test_throws ModelingToolkit.IncompleteInitializationError prob=ODEProblem(sys, u0, tspan, p, jac = true)
@test_throws ModelingToolkit.IncompleteInitializationError prob=ODEProblem(
sys, u0, tspan, p, jac = true)

# DAE Initialization on ODE with nonlinear system for initial conditions
# https://github.com/SciML/ModelingToolkit.jl/issues/2508

Unchanged files with check annotations Beta

the `parameter_dependencies` keyword argument of `ODESystem`, `SDESystem` and `JumpSystem`. The dependent
parameters are updated whenever other parameters are modified, e.g. in callbacks.
- Support for `IfElse.jl` has been dropped. `Base.ifelse` can be used instead.
- DAE initailization and the solving for consistent initial conditions has been changed to use a customized

Check warning on line 53 in NEWS.md

GitHub Actions / Spell Check with Typos

"initailization" should be "initialization".
initialization solve. This change adds `guess` semantics which are clearly delinated from the behavior of
the defaults, where `default` (and `u0`) is designed to be always satisfied and error if unsatisfiable,
while `guess` is an initial guess to the initializer. In previous iterations, initialization with the
using Symbolics: StateMachineOperator
isconnection(_) = false

Check warning on line 2 in src/systems/connectors.jl

GitHub Actions / Spell Check with Typos

"isconnection" should be "isconnected".
isconnection(_::Connection) = true

Check warning on line 3 in src/systems/connectors.jl

GitHub Actions / Spell Check with Typos

"isconnection" should be "isconnected".
"""
domain_connect(sys1, sys2, syss...)
else
if lhs isa Connection && get_systems(lhs) === :domain
connection2set!(domain_csets, namespace, get_systems(rhs), isouter)
elseif isconnection(rhs)

Check warning on line 335 in src/systems/connectors.jl

GitHub Actions / Spell Check with Typos

"isconnection" should be "isconnected".
push!(cts, get_systems(rhs))
else
# split connections and equations