Skip to content

Commit 4d8463c

Browse files
refactor: default parameter derivatives to zero, allow opting-out
1 parent 4935f3b commit 4d8463c

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/systems/systemstructure.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,14 @@ function TearingState(sys; quick_cancel = false, check = true)
292292
end
293293
if iscall(eq′.lhs) && (op = operation(eq′.lhs)) isa Differential &&
294294
isequal(op.x, iv) && is_time_dependent_parameter(only(arguments(eq′.lhs)), iv)
295-
param_derivative_map[eq′.lhs] = eq′.rhs
295+
# parameter derivatives are opted out by specifying `D(p) ~ missing`, but
296+
# we want to store `nothing` in the map because that means `fast_substitute`
297+
# will ignore the rule. We will this identify the presence of `eq′.lhs` in
298+
# the differentiated expression and error.
299+
param_derivative_map[eq′.lhs] = coalesce(eq′.rhs, nothing)
296300
eqs_to_retain[i] = false
301+
# change the equation if the RHS is `missing` so the rest of this loop works
302+
eq′ = eq′.lhs ~ coalesce(eq′.rhs, 0.0)
297303
end
298304
if _iszero(eq′.lhs)
299305
rhs = quick_cancel ? quick_cancel_expr(eq′.rhs) : eq′.rhs
@@ -311,9 +317,9 @@ function TearingState(sys; quick_cancel = false, check = true)
311317
(iscall(_var) && isparameter(operation(_var)) || isconstant(_var))
312318
if is_time_dependent_parameter(_var, iv) &&
313319
!haskey(param_derivative_map, Differential(iv)(_var))
314-
# default to `nothing` since it is ignored during substitution,
315-
# so `D(_var)` is retained in the expression.
316-
param_derivative_map[Differential(iv)(_var)] = nothing
320+
# Parameter derivatives default to zero - they stay constant
321+
# between callbacks
322+
param_derivative_map[Differential(iv)(_var)] = 0.0
317323
end
318324
continue
319325
end

0 commit comments

Comments
 (0)