Skip to content

Commit 66c29ef

Browse files
WIP: Initialization on non-DAE models
#2508 pointed out that the heuristic of `implicit_dae || calculate_massmatrix(sys) !== I`, i.e. "only do initialization on DAEs", while sensible for the DAE model, doesn't quite fit in the MTK context. Instead, what we need to do is check whether the varmap is correct and consistent for the chosen ODE. If it's not consistent for the selected ODE, then we still need to run an initialization step for the resulting ODE solve. This needs a few changes in the ODE solver as though, since currently only DAE solvers will run the initialization step. Thus the test case: ```julia using ModelingToolkit, OrdinaryDiffEq, Test using ModelingToolkit: t_nounits as t, D_nounits as D function System(;name) vars = @variables begin dx(t), [guess=0] ddx(t), [guess=0] end eqs = [ D(dx) ~ ddx 0 ~ ddx + dx + 1 ] return ODESystem(eqs, t, vars, []; name) end @mtkbuild sys = System() prob = ODEProblem(sys, [sys.dx => 1], (0,1)) # OK prob = ODEProblem(sys, [sys.ddx => -2], (0,1), guesses = [sys.dx => 1]) sol = solve(prob, Rodas5P()) sol = solve(prob, Tsit5()) ``` gives an erroneous success as it skips initialization, using `prob.u0 == [0.0]`, the sentinel value since initialization is only run on DAEs. We will need to override that so that initialization is always run on any system that provides an initializeprob.
1 parent c5bff6a commit 66c29ef

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,14 +862,18 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap;
862862
ps = full_parameters(sys)
863863
iv = get_iv(sys)
864864

865+
varmap = merge(defaults, todict(u0map))
866+
varlist = collect(map(unwrap, dvs))
867+
missingvars = setdiff(varlist, collect(keys(varmap)))
868+
865869
# Append zeros to the variables which are determined by the initialization system
866870
# This essentially bypasses the check for if initial conditions are defined for DAEs
867871
# since they will be checked in the initialization problem's construction
868872
# TODO: make check for if a DAE cheaper than calculating the mass matrix a second time!
869873
ci = infer_clocks!(ClockInference(TearingState(sys)))
870874
# TODO: make it work with clocks
871875
# ModelingToolkit.get_tearing_state(sys) !== nothing => Requires structural_simplify first
872-
if (implicit_dae || calculate_massmatrix(sys) !== I) &&
876+
if (implicit_dae || !isempty(missingvars)) &&
873877
all(isequal(Continuous()), ci.var_domain) &&
874878
ModelingToolkit.get_tearing_state(sys) !== nothing
875879
if eltype(u0map) <: Number

0 commit comments

Comments
 (0)