diff --git a/docs/src/API/model_building.md b/docs/src/API/model_building.md index a72be8a4c6..cbdf72edae 100644 --- a/docs/src/API/model_building.md +++ b/docs/src/API/model_building.md @@ -196,6 +196,7 @@ symbolic analysis. ```@docs liouville_transform +change_of_variables stochastic_integral_transform Girsanov_transform change_independent_variable diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index 3b86a55548..fb21494bc4 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -297,7 +297,7 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc hasunit, getunit, hasconnect, getconnect, hasmisc, getmisc, state_priority export liouville_transform, change_independent_variable, substitute_component, - add_accumulations, noise_to_brownians, Girsanov_transform, changeofvariables + add_accumulations, noise_to_brownians, Girsanov_transform, change_of_variables export PDESystem export Differential, expand_derivatives, @derivatives export Equation, ConstrainedEquation diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 65dc4177a0..9312966528 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -67,34 +67,33 @@ using ModelingToolkit, OrdinaryDiffEq, Test # Change of variables: z = log(x) # (this implies that x = exp(z) is automatically non-negative) -@parameters t α +@independent_variables t +@parameters α @variables x(t) D = Differential(t) eqs = [D(x) ~ α*x] tspan = (0., 1.) -u0 = [x => 1.0] -p = [α => -0.5] +def = [x => 1.0, α => -0.5] -@named sys = ODESystem(eqs; defaults=u0) -prob = ODEProblem(sys, [], tspan, p) +@mtkcompile sys = System(eqs, t;defaults=def) +prob = ODEProblem(sys, [], tspan) sol = solve(prob, Tsit5()) @variables z(t) forward_subs = [log(x) => z] backward_subs = [x => exp(z)] - -@named new_sys = changeofvariables(sys, forward_subs, backward_subs) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(z) ~ α) -new_prob = ODEProblem(new_sys, [], tspan, p) +new_prob = ODEProblem(new_sys, [], tspan) new_sol = solve(new_prob, Tsit5()) @test isapprox(new_sol[x][end], sol[x][end], atol=1e-4) ``` """ -function changeofvariables( +function change_of_variables( sys::System, iv, forward_subs, backward_subs; simplify=true, t0=missing, isSDE=false ) diff --git a/test/changeofvariables.jl b/test/changeofvariables.jl index 776d44f7e7..abc773ed21 100644 --- a/test/changeofvariables.jl +++ b/test/changeofvariables.jl @@ -26,7 +26,7 @@ sol = solve(prob, Tsit5()) @variables z(t) forward_subs = [log(x) => z] backward_subs = [x => exp(z)] -new_sys = changeofvariables(sys, t, forward_subs, backward_subs) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(z) ~ α) new_prob = ODEProblem(new_sys, [], tspan) @@ -48,7 +48,7 @@ def = [x=>1., α => 1.] forward_subs = [t + α/(x+t) => z ] backward_subs = [ x => α/(z-t) - t] -new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.) # output should be equivalent to # t^2 + α - z^2 + 2 (but this simplification is not found automatically) @@ -86,7 +86,7 @@ z = reshape(z, 3, 1) forward_subs = vec(T_inv*x .=> z) backward_subs = vec(x .=> T*z) -new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify=true) new_prob = ODEProblem(new_sys, [], tspan) new_sol = solve(new_prob, Tsit5()) @@ -115,7 +115,7 @@ def = [x=>0., μ => 2., σ=>1.] @mtkcompile sys = System(eqs, t; defaults=def) forward_subs = [log(x) => y] backward_subs = [x => exp(y)] -new_sys = changeofvariables(sys, t, forward_subs, backward_subs) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(y) ~ μ - 1/2*σ^2) @test noise_eqs(new_sys)[1] === value(σ) @@ -131,7 +131,7 @@ forward_subs = [log(x) => z, y^2 => w, log(u) => v] backward_subs = [x => exp(z), y => w^.5, u => exp(v)] @mtkcompile sys = System(eqs, t; defaults=def) -new_sys = changeofvariables(sys, t, forward_subs, backward_subs) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs) @test equations(new_sys)[1] == (D(z) ~ μ - 1/2*σ^2) @test equations(new_sys)[2] == (D(w) ~ α^2) @test equations(new_sys)[3] == (D(v) ~ μ - 1/2*(α^2 + σ^2)) @@ -144,7 +144,7 @@ new_sys = changeofvariables(sys, t, forward_subs, backward_subs) # Test for Brownian instead of noise @named sys = System(eqs, t; defaults=def) -new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=false) +new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify=false) @test simplify(equations(new_sys)[1]) == simplify((D(z) ~ μ - 1/2*σ^2 + σ*Bx)) @test simplify(equations(new_sys)[2]) == simplify((D(w) ~ α^2 + 2*α*w^.5*By)) @test simplify(equations(new_sys)[3]) == simplify((D(v) ~ μ - 1/2*(α^2 + σ^2) + σ*Bx + α*By)) \ No newline at end of file