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 docs/src/API/model_building.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ symbolic analysis.

```@docs
liouville_transform
change_of_variables
stochastic_integral_transform
Girsanov_transform
change_independent_variable
Expand Down
2 changes: 1 addition & 1 deletion src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions src/systems/diffeqs/basic_transformations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
12 changes: 6 additions & 6 deletions test/changeofvariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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(σ)

Expand All @@ -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))
Expand All @@ -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))
Loading