|
1 |
| -using ModelingToolkit, OrdinaryDiffEq, Test |
| 1 | +using ModelingToolkit, OrdinaryDiffEq, DataInterpolations, Test |
2 | 2 |
|
3 | 3 | @independent_variables t
|
4 | 4 | D = Differential(t)
|
|
143 | 143 | ])
|
144 | 144 | end
|
145 | 145 |
|
| 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 | + |
146 | 174 | @testset "Change independent variable (errors)" begin
|
147 | 175 | @variables x(t) y z(y) w(t) v(t)
|
148 | 176 | M = ODESystem([D(x) ~ 0, v ~ x], t; name = :M)
|
|
0 commit comments