Skip to content

Commit b6acf8b

Browse files
Repair documentation example setup (#481)
* Fix callable interpolation builder symtype Register CachedInterpolation's symbolic result as a callable Real-to-Real value, derived through public SymbolicUtils APIs. Add a regression that calls the bound interpolator before building and solving the ODEProblem. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> * Repair documentation example setup Supply the position-domain initialization equations, bind OnePort's extended names explicitly, and make OrdinaryDiffEqSDIRK a direct documentation dependency for ImplicitEuler. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> --------- Co-authored-by: ChrisRackauckas-Claude <accounts@chrisrackauckas.com>
1 parent d369822 commit b6acf8b

6 files changed

Lines changed: 20 additions & 4 deletions

File tree

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
77
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
88
ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739"
99
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
10+
OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf"
1011
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1112
SciCompDSL = "91a8cdf1-4ca6-467b-a780-87fda3fff15e"
1213

@@ -19,5 +20,6 @@ IfElse = "0.1"
1920
ModelingToolkit = "11"
2021
ModelingToolkitStandardLibrary = "2.28.0"
2122
OrdinaryDiffEq = "6, 7"
23+
OrdinaryDiffEqSDIRK = "1, 2"
2224
Plots = "1.36"
2325
SciCompDSL = "1"

docs/src/connectors/connections.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ nothing # hide
191191
As can be seen, we get exactly the same result. The only difference here is that we are solving an extra equation, which allows us to plot the body position as well.
192192

193193
```@example connections
194-
prob = ODEProblem(sys, [], (0, 10.0), fully_determined = true)
194+
prob = ODEProblem(
195+
sys, [], (0, 10.0); initialization_eqs = [sys.body.s ~ 0, sys.body.v ~ 1])
195196
sol_p = solve(prob)
196197
197198
p1 = plot(sol_p, idxs = [body.v])

docs/src/tutorials/custom_component.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ this can almost be directly translated to the syntax of `ModelingToolkit`.
3636

3737
```@example components
3838
@mtkmodel NonlinearResistor begin
39-
@extend OnePort()
39+
@extend v, i = oneport = OnePort()
4040
@parameters begin
4141
Ga
4242
Gb
@@ -58,7 +58,7 @@ nothing # hide
5858
Since the non-linear resistor is essentially a standard electrical component with two ports, we can extend from the `OnePort` component of the library.
5959

6060
```julia
61-
@extend OnePort()
61+
@extend v, i = oneport = OnePort()
6262
```
6363

6464
This extends `OnePort` and unpacks `v` and `i` variables.

src/Blocks/Blocks.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Blocks
55
using ModelingToolkitBase: ModelingToolkitBase, @component, @connector, @named,
66
@parameters, @unpack, System, compose, connect, extend,
77
getdefault, t_nounits as t, D_nounits as D
8+
using SymbolicUtils: @syms, symtype
89
using Symbolics: Symbolics, @register_symbolic, @variables, Differential, Equation
910
import Base: ifelse
1011
import ..@symcheck

src/Blocks/sources.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,15 @@ end
864864

865865
Base.nameof(::CachedInterpolation) = :CachedInterpolation
866866

867-
@register_symbolic (f::CachedInterpolation)(u::AbstractArray, x::AbstractArray, args::Tuple)
867+
const _INTERPOLATOR_SYMTYPE = let
868+
@syms interpolator(::Real)::Real
869+
symtype(interpolator)
870+
end
871+
872+
# Calling the builder returns the interpolation function, not an interpolated scalar.
873+
@register_symbolic (f::CachedInterpolation)(
874+
u::AbstractArray, x::AbstractArray, args::Tuple
875+
)::_INTERPOLATOR_SYMTYPE
868876

869877
"""
870878
ParametrizedInterpolation(interp_type, u, x, args...; name, t = ModelingToolkitBase.t_nounits)

test/sources.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ using SymbolicIndexingInterface
1414
using SciMLStructures: SciMLStructures, Tunable
1515
using ForwardDiff
1616
using ADTypes
17+
using SymbolicUtils: symtype
1718

1819
@testset "Constant" begin
1920
@named src = Constant(k = 2)
@@ -650,6 +651,9 @@ end
650651

651652
@testset "LinearInterpolation" begin
652653
@named i = ParametrizedInterpolation(LinearInterpolation, u, x)
654+
interpolator = only(values(bindings(i)))
655+
@test symtype(interpolator(0.0)) === Real
656+
653657
eqs = [i.input.u ~ t, D(y) ~ i.output.u]
654658

655659
@named model = System(eqs, t, systems = [i])

0 commit comments

Comments
 (0)