Skip to content

Commit d660269

Browse files
committed
Test change_independent_variable with registered functions and callable parameters
1 parent 0e41e04 commit d660269

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

test/basic_transformations.jl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ModelingToolkit, OrdinaryDiffEq, Test
1+
using ModelingToolkit, OrdinaryDiffEq, DataInterpolations, Test
22

33
@independent_variables t
44
D = Differential(t)
@@ -143,6 +143,34 @@ end
143143
])
144144
end
145145

146+
@testset "Change independent variable (registered function / callable parameter)" begin
147+
@independent_variables t
148+
D = Differential(t)
149+
@variables x(t) y(t)
150+
@parameters f::LinearInterpolation (fc::LinearInterpolation)(..) # non-callable and callable
151+
callme(interp::LinearInterpolation, input) = interp(input)
152+
@register_symbolic callme(interp::LinearInterpolation, input)
153+
M1 = ODESystem([
154+
D(x) ~ 2*t
155+
D(y) ~ 1*fc(t) + 2*fc(x) + 3*fc(y) + 1*callme(f, t) + 2*callme(f, x) + 3*callme(f, y)
156+
], t; name = :M)
157+
158+
# Ensure that interpolations are called with the same variables
159+
M2 = change_independent_variable(M1, x, [t ~ (x)])
160+
@variables x y(x) t(x)
161+
Dx = Differential(x)
162+
@test Set(equations(M2)) == Set([
163+
t ~ (x)
164+
2*t*Dx(y) ~ 1*fc(t) + 2*fc(x) + 3*fc(y) + 1*callme(f, t) + 2*callme(f, x) + 3*callme(f, y)
165+
])
166+
167+
_f = LinearInterpolation([1.0, 1.0], [-100.0, +100.0]) # constant value 1
168+
M2s = structural_simplify(M2; allow_symbolic = true)
169+
prob = ODEProblem(M2s, [M2s.y => 0.0], (1.0, 4.0), [fc => _f, f => _f])
170+
sol = solve(prob, Tsit5(); abstol = 1e-5)
171+
@test isapprox(sol(4.0, idxs=M2.y), 12.0; atol = 1e-5) # Anal solution is D(y) ~ 12 => y(t) ~ 12*t + C => y(x) ~ 12*√(x) + C. With y(x=1)=0 => 12*(√(x)-1), so y(x=4) ~ 12
172+
end
173+
146174
@testset "Change independent variable (errors)" begin
147175
@variables x(t) y z(y) w(t) v(t)
148176
M = ODESystem([D(x) ~ 0, v ~ x], t; name = :M)

0 commit comments

Comments
 (0)