Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions lib/ModelingToolkitBase/src/ModelingToolkitBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ const set_scalar_metadata = setmetadata
@public check_mutable_cache, store_to_mutable_cache!, should_invalidate_mutable_cache_entry
@public convert_bindings_for_time_independent_system, get_w
@public Both
@public SymbolicADDisallowed, check_symbolic_ad_allowed

for prop in [SYS_PROPS; [:continuous_events, :discrete_events]]
getter = Symbol(:get_, prop)
Expand Down
2 changes: 2 additions & 0 deletions lib/ModelingToolkitBase/src/systems/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ Calculate the gradient of the equations of `sys` with respect to the independent
`simplify` is forwarded to `Symbolics.expand_derivatives`.
"""
function calculate_tgrad(sys::System; simplify = false)
check_symbolic_ad_allowed(sys)
# We need to remove explicit time dependence on the unknown because when we
# have `u(t) * t` we want to have the tgrad to be `u(t)` instead of `u'(t) *
# t + u(t)`.
Expand Down Expand Up @@ -198,6 +199,7 @@ function calculate_jacobian(
sys::System;
sparse = false, simplify = false, dvs = unknowns(sys)
)
check_symbolic_ad_allowed(sys)
eqs = full_equations(sys)
rhs = SymbolicT[]
sizehint!(rhs, length(eqs))
Expand Down
31 changes: 31 additions & 0 deletions lib/ModelingToolkitBase/src/systems/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,37 @@ Utility metadata key for adding miscellaneous/one-off metadata to systems.
"""
abstract type MiscSystemData end

"""
$TYPEDEF

Metadata key used to mark a system as incompatible with symbolic automatic differentiation.
When set on a system via `setmetadata(sys, SymbolicADDisallowed, reason)`, any attempt to
perform symbolic AD on the equations of that system (e.g. via `calculate_jacobian`,
`calculate_tgrad`, `linearize_symbolic`, or during structural simplification) will throw
an error. The value associated with this key should be a descriptive `String` explaining
why symbolic AD is unsupported, or `true` if no explanation is available.

See also: [`check_symbolic_ad_allowed`](@ref).
"""
abstract type SymbolicADDisallowed end

"""
check_symbolic_ad_allowed(sys::AbstractSystem)

Check whether `sys` supports symbolic automatic differentiation. Throws an `ArgumentError`
if the system has been marked with [`SymbolicADDisallowed`](@ref).
"""
function check_symbolic_ad_allowed(sys::AbstractSystem)
if SymbolicUtils.hasmetadata(sys, SymbolicADDisallowed)
reason = SymbolicUtils.getmetadata(sys, SymbolicADDisallowed, nothing)
msg = "System $(nameof(sys)) does not support symbolic automatic differentiation."
if reason isa AbstractString && !isempty(reason)
msg *= " $reason"
end
throw(ArgumentError(msg))
end
end

"""
$(TYPEDEF)

Expand Down
1 change: 1 addition & 0 deletions src/linearization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ function linearize_symbolic(
kwargs...
)
sys = mtkcompile(sys; inputs, outputs, simplify, split, kwargs...)
check_symbolic_ad_allowed(sys)
diff_idxs, alge_idxs = eq_idxs(sys)
sts = unknowns(sys)
t = get_iv(sys)
Expand Down
1 change: 1 addition & 0 deletions src/problems/semilinearodeproblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)

if jac
check_symbolic_ad_allowed(sys)
Cjac = (C === nothing || !stiff_nonlinear) ? nothing : Symbolics.jacobian(C, dvs)
_jac = generate_semiquadratic_jacobian(
sys, A, B, C, Cjac; sparse, expression,
Expand Down
Loading