Skip to content

Commit b8e1f63

Browse files
committed
fix: more tests passing
1 parent b956fe6 commit b8e1f63

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

src/structural_transformation/utils.jl

+1
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ end
551551
function _distribute_shift(expr, shift)
552552
if iscall(expr)
553553
op = operation(expr)
554+
(op isa Pre || op isa Initial) && return expr
554555
args = arguments(expr)
555556

556557
if ModelingToolkit.isvariable(expr)

src/systems/callbacks.jl

+16-8
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ make_affect(affect::Affect; kwargs...) = affect
242242

243243
function make_affect(affect::Vector{Equation}; iv = nothing, algeeqs = Equation[])
244244
isempty(affect) && return nothing
245-
isempty(algeeqs) && @warn "No algebraic equations were found. If the system has no algebraic equations, this can be disregarded. Otherwise pass in `algeeqs` to the SymbolicContinuousCallback constructor."
245+
isempty(algeeqs) && @warn "No algebraic equations were found for the callback defined by $(join(affect, ", ")). If the system has no algebraic equations, this can be disregarded. Otherwise pass in `algeeqs` to the SymbolicContinuousCallback constructor."
246246

247247
explicit = true
248248
affect = scalarize(affect)
@@ -259,6 +259,8 @@ function make_affect(affect::Vector{Equation}; iv = nothing, algeeqs = Equation[
259259
collect_vars!(dvs, params, eq, iv)
260260
explicit = false
261261
end
262+
any(isirreducible, dvs) && (explicit = false)
263+
262264
if isnothing(iv)
263265
iv = isempty(dvs) ? iv : only(arguments(dvs[1]))
264266
isnothing(iv) && @warn "No independent variable specified and could not be inferred. If the iv appears in an affect equation explicitly, like x ~ t + 1, then it must be specified as an argument to the SymbolicContinuousCallback or SymbolicDiscreteCallback constructor. Otherwise this warning can be disregarded."
@@ -858,16 +860,19 @@ function compile_equational_affect(aff::Union{AffectSystem, Vector{Equation}}, s
858860
t = get_iv(sys)
859861

860862
u_idxs = indexin((@view lhss[.!is_p]), dvs)
861-
p_idxs = if has_index_cache(sys) && (get_index_cache(sys) !== nothing)
862-
[parameter_index(sys, p) for p in lhss[is_p]]
863+
864+
wrap_mtkparameters = has_index_cache(sys) && (get_index_cache(sys) !== nothing)
865+
p_idxs = if wrap_mtkparameters
866+
[parameter_index(sys, p) for (i, p) in enumerate(lhss)
867+
if is_p[i]]
863868
else
864869
indexin((@view lhss[is_p]), ps)
865870
end
866871
_ps = reorder_parameters(sys, ps)
867872
integ = gensym(:MTKIntegrator)
868873

869-
u_up, u_up! = build_function_wrapper(sys, (@view rhss[.!is_p]), dvs, _ps..., t; wrap_code = add_integrator_header(sys, integ, :u), expression = Val{false}, outputidxs = u_idxs, wrap_mtkparameters = false)
870-
p_up, p_up! = build_function_wrapper(sys, (@view rhss[is_p]), dvs, _ps..., t; wrap_code = add_integrator_header(sys, integ, :p), expression = Val{false}, outputidxs = p_idxs, wrap_mtkparameters = false)
874+
u_up, u_up! = build_function_wrapper(sys, (@view rhss[.!is_p]), dvs, _ps..., t; wrap_code = add_integrator_header(sys, integ, :u), expression = Val{false}, outputidxs = u_idxs, wrap_mtkparameters)
875+
p_up, p_up! = build_function_wrapper(sys, (@view rhss[is_p]), dvs, _ps..., t; wrap_code = add_integrator_header(sys, integ, :p), expression = Val{false}, outputidxs = p_idxs, wrap_mtkparameters)
871876

872877
return function explicit_affect!(integ)
873878
u_up!(integ)
@@ -883,9 +888,12 @@ function compile_equational_affect(aff::Union{AffectSystem, Vector{Equation}}, s
883888
pval = isparameter(p) ? integ.ps[p] : integ[p]
884889
push!(pmap, pre_p => pval)
885890
end
886-
guesses = Pair[u => integ[aff_map[u]] for u in unknowns(affsys)]
887-
affprob = ImplicitDiscreteProblem(affsys, Pair[], (integ.t, integ.t), pmap; guesses, build_initializeprob = false)
888-
891+
u0 = Pair[]
892+
for u in unknowns(affsys)
893+
uval = isparameter(aff_map[u]) ? integ.ps[u] : integ[u]
894+
push!(u0, u => uval)
895+
end
896+
affprob = ImplicitDiscreteProblem(affsys, u0, (integ.t, integ.t), pmap; build_initializeprob = false, check_length = false)
889897
affsol = init(affprob, SimpleIDSolve())
890898
for u in dvs_to_update
891899
integ[u] = affsol[sys_map[u]]

src/systems/discrete_system/implicit_discrete_system.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs)
292292
v = u0map[k]
293293
if !((op = operation(k)) isa Shift)
294294
isnothing(getunshifted(k)) &&
295-
error("Initial conditions must be for the past state of the unknowns. Instead of providing the condition for $k, provide the condition for $(Shift(iv, -1)(k)).")
295+
@warn "Initial condition given in term of current state of the unknown. If `build_initializeprob = false, this may be overriden by the implicit discrete solver."
296296

297297
updated[k] = v
298298
elseif op.steps > 0

src/systems/jumps/jumpsystem.jl

-2
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ function generate_affect_function(js::JumpSystem, affect)
283283
csubs = Dict(c => getdefault(c) for c in consts)
284284
affect = substitute(affect, csubs)
285285
end
286-
@show dump(affect[1])
287286
compile_equational_affect(affect, js; expression = Val{true}, checkvars = false)
288287
end
289288

@@ -535,7 +534,6 @@ function JumpProcesses.JumpProblem(js::JumpSystem, prob,
535534

536535
majpmapper = JumpSysMajParamMapper(js, p; jseqs = eqs, rateconsttype = invttype)
537536
majs = isempty(eqs.x[1]) ? nothing : assemble_maj(eqs.x[1], unknowntoid, majpmapper)
538-
@show eqs.x[2]
539537
crjs = ConstantRateJump[assemble_crj(js, j, unknowntoid; eval_expression, eval_module)
540538
for j in eqs.x[2]]
541539
vrjs = VariableRateJump[assemble_vrj(js, j, unknowntoid; eval_expression, eval_module)

test/symbolic_events.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,9 @@ end
12321232
@variables x(t) [irreducible = true] y(t) [irreducible = true]
12331233
eqs = [x ~ y, D(x) ~ -1]
12341234
cb = [x ~ 0.0] => [x ~ 0, y ~ 1]
1235-
@test_throws Exception @mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb])
1235+
@mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb])
1236+
prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x])
1237+
@test_broken !SciMLBase.successful_retcode(solve(prob, Rodas5()))
12361238

12371239
cb = [x ~ 0.0] => [y ~ 1]
12381240
@mtkbuild pend = ODESystem(eqs, t; continuous_events = [cb])

0 commit comments

Comments
 (0)