Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6b0f03c
Created change of variable for SDE
May 29, 2025
24ebd99
Merge branch 'iss141'
May 29, 2025
e0bb5d2
Implement and fix change of variable for ODE
Jun 3, 2025
86411c2
Merge branch 'SciML:master' into master
fchen121 Jun 3, 2025
22ab624
Implement change of variables for sde
Jun 4, 2025
8ace5e7
Merge branch 'SciML:master' into master
fchen121 Jun 4, 2025
50a4fc3
Fix Linear transformation to diagonal system test
Jun 5, 2025
a1e9944
Update Project.toml
Jun 5, 2025
7b3ea99
Merge branch 'master' of https://github.com/fchen121/ModelingToolkit.jl
Jun 5, 2025
e33bcc1
Change backward subs from observed to equations
Jun 5, 2025
192872c
Change of variables for multiple Brownian SDE
Jun 5, 2025
9a53121
Improve change of variables test
Jun 5, 2025
8140b62
Combined change of variable function for ODE and SDE
Jun 6, 2025
c0453f4
Change of variable for non-simplified SDE
Jun 9, 2025
166b0f8
Update test/changeofvariables.jl
ChrisRackauckas Jun 10, 2025
0884e59
Update src/systems/diffeqs/basic_transformations.jl
ChrisRackauckas Jun 11, 2025
70a5b39
Update Project.toml
ChrisRackauckas Jun 11, 2025
2d5c9e7
Update Project.toml
ChrisRackauckas Jun 11, 2025
2b32ad4
Update Project.toml
ChrisRackauckas Jun 11, 2025
2b48648
Update Project.toml
ChrisRackauckas Jun 11, 2025
07affe6
Update Project.toml
ChrisRackauckas Jun 11, 2025
7d5e915
Update src/ModelingToolkit.jl
ChrisRackauckas Jun 11, 2025
5367f60
Merge branch 'SciML:master' into master
fchen121 Jun 12, 2025
96609b3
Update runtests.jl
ChrisRackauckas Jun 12, 2025
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
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8"
OrdinaryDiffEqDefault = "50262376-6c5a-4cf5-baba-aaf4f84d72d7"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Expand All @@ -59,9 +61,12 @@ SimpleNonlinearSolve = "727e6d20-b764-4bd8-a329-72de5adea6c7"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0"
SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
TermInterface = "8ea1fca8-c5ef-4a55-8b96-4e9afe9c9a3c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
Expand Down Expand Up @@ -159,6 +164,8 @@ StochasticDiffEq = "6.72.1"
SymbolicIndexingInterface = "0.3.39"
SymbolicUtils = "3.26.1"
Symbolics = "6.40"
TermInterface = "2.0.0"
Test = "1.11.0"
URIs = "1"
UnPack = "0.1, 1.0"
Unitful = "1.1"
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
add_accumulations, noise_to_brownians, Girsanov_transform, changeofvariables, change_of_variable_SDE
export PDESystem
export Differential, expand_derivatives, @derivatives
export Equation, ConstrainedEquation
Expand Down
118 changes: 118 additions & 0 deletions src/systems/diffeqs/basic_transformations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,124 @@ function liouville_transform(sys::System; kwargs...)
)
end

"""
$(TYPEDSIGNATURES)

Generates the set of ODEs after change of variables.


Example:

```julia
using ModelingToolkit, OrdinaryDiffEq, Test

# Change of variables: z = log(x)
# (this implies that x = exp(z) is automatically non-negative)

@parameters t α
@variables x(t)
D = Differential(t)
eqs = [D(x) ~ α*x]

tspan = (0., 1.)
u0 = [x => 1.0]
p = [α => -0.5]

@named sys = ODESystem(eqs; defaults=u0)
prob = ODEProblem(sys, [], tspan, p)
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)
@test equations(new_sys)[1] == (D(z) ~ α)

new_prob = ODEProblem(new_sys, [], tspan, p)
new_sol = solve(new_prob, Tsit5())

@test isapprox(new_sol[x][end], sol[x][end], atol=1e-4)
```

"""
function changeofvariables(
sys::System, iv, forward_subs, backward_subs;
simplify=true, t0=missing, isSDE=false
)
if !iscomplete(sys)
sys = mtkcompile(sys)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably best to apply this before simplification so you already have the brownians?

t = iv

old_vars = first.(backward_subs)
new_vars = last.(forward_subs)

# use: f = Y(t, X)
# use: dY = (∂f/∂t + μ∂f/∂x + (1/2)*σ^2*∂2f/∂x2)dt + σ∂f/∂xdW
old_eqs = equations(sys)
neqs = get_noise_eqs(sys)
if neqs !== nothing
isSDE = true
neqs = [neqs[i,:] for i in 1:size(neqs,1)]

brownvars = map([Symbol(:B, :_, i) for i in 1:length(neqs[1])]) do name
unwrap(only(@brownian $name))
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AayushSabharwal if the system isn't simplified, what's a quick function to find the brownians?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the other way around - any system with noise_eqs won't have brownians. If a system isn't simplified and has brownian terms in the equations, brownians(sys) contains the list of brownian variables.

Copy link
Contributor Author

@fchen121 fchen121 Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to isolate the diffusion coefficients of the brownians before simplifying?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manually, with Symbolics.linear_expansion. See

Is = Int[]
Js = Int[]
vals = Num[]
new_eqs = copy(eqs)
dvar2eq = Dict{Any, Int}()
for (v, dv) in enumerate(var_to_diff)
dv === nothing && continue
deqs = 𝑑neighbors(graph, dv)
if length(deqs) != 1
error("$(eqs[deqs]) is not handled.")
end
dvar2eq[fullvars[dv]] = only(deqs)
end
for (j, bj) in enumerate(brown_vars), i in 𝑑neighbors(graph, bj)
push!(Is, i)
push!(Js, j)
eq = new_eqs[i]
brown = fullvars[bj]
(coeff, residual, islinear) = Symbolics.linear_expansion(eq, brown)
islinear || error("$brown isn't linear in $eq")
new_eqs[i] = 0 ~ residual
push!(vals, coeff)
end
g = Matrix(sparse(Is, Js, vals))
sys = state.sys
@set! sys.eqs = new_eqs
@set! sys.unknowns = [v
for (i, v) in enumerate(fullvars)
if !iszero(new_idxs[i]) &&
invview(var_to_diff)[i] === nothing]
ode_sys = mtkcompile(
sys; simplify, inputs, outputs, disturbance_inputs, kwargs...)
eqs = equations(ode_sys)
sorted_g_rows = zeros(Num, length(eqs), size(g, 2))
for (i, eq) in enumerate(eqs)
dvar = eq.lhs
# differential equations always precede algebraic equations
_iszero(dvar) && break
g_row = get(dvar2eq, dvar, 0)
iszero(g_row) && error("$dvar isn't handled.")
g_row > size(g, 1) && continue
@views copyto!(sorted_g_rows[i, :], g[g_row, :])
end
# Fix for https://github.com/SciML/ModelingToolkit.jl/issues/2490
if sorted_g_rows isa AbstractMatrix && size(sorted_g_rows, 2) == 1
# If there's only one brownian variable referenced across all the equations,
# we get a Nx1 matrix of noise equations, which is a special case known as scalar noise
noise_eqs = reshape(sorted_g_rows[:, 1], (:, 1))
is_scalar_noise = true
elseif __num_isdiag_noise(sorted_g_rows)
# If each column of the noise matrix has either 0 or 1 non-zero entry, then this is "diagonal noise".
# In this case, the solver just takes a vector column of equations and it interprets that to
# mean that each noise process is independent
noise_eqs = __get_num_diag_noise(sorted_g_rows)
is_scalar_noise = false
else
noise_eqs = sorted_g_rows
is_scalar_noise = false
end
.

else
neqs = ones(1, length(old_eqs))
end

# df/dt = ∂f/∂x dx/dt + ∂f/∂t
dfdt = Symbolics.derivative( first.(forward_subs), t )
∂f∂x = [Symbolics.derivative( first(f_sub), old_var ) for (f_sub, old_var) in zip(forward_subs, old_vars)]
∂2f∂x2 = Symbolics.derivative.( ∂f∂x, old_vars )
new_eqs = Equation[]

for (new_var, ex, first, second) in zip(new_vars, dfdt, ∂f∂x, ∂2f∂x2)
for (eqs, neq) in zip(old_eqs, neqs)
if occursin(value(eqs.lhs), value(ex))
ex = substitute(ex, eqs.lhs => eqs.rhs)
if isSDE
for (noise, B) in zip(neq, brownvars)
ex = ex + 1/2 * noise^2 * second + noise*first*B
end
end
end
end
ex = substitute(ex, Dict(forward_subs))
ex = substitute(ex, Dict(backward_subs))
if simplify
ex = Symbolics.simplify(ex, expand=true)
end
push!(new_eqs, Differential(t)(new_var) ~ ex)
end

defs = get_defaults(sys)
new_defs = Dict()
for f_sub in forward_subs
ex = substitute(first(f_sub), defs)
if !ismissing(t0)
ex = substitute(ex, t => t0)
end
new_defs[last(f_sub)] = ex
end
for para in parameters(sys)
if haskey(defs, para)
new_defs[para] = defs[para]
end
end

@named new_sys = System(vcat(new_eqs, first.(backward_subs) .~ last.(backward_subs)), t;
defaults=new_defs,
observed=observed(sys)
)
if simplify
return mtkcompile(new_sys)
end
return new_sys
end

"""
change_independent_variable(
sys::System, iv, eqs = [];
Expand Down
142 changes: 142 additions & 0 deletions test/changeofvariables.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using ModelingToolkit, OrdinaryDiffEq, StochasticDiffEq
using Test, LinearAlgebra


# Change of variables: z = log(x)
# (this implies that x = exp(z) is automatically non-negative)
@independent_variables t
@variables z(t)[1:2, 1:2]
D = Differential(t)
eqs = [D(D(z)) ~ ones(2, 2)]
@mtkcompile sys = System(eqs, t)
@test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0))

@parameters α
@variables x(t)
D = Differential(t)
eqs = [D(x) ~ α*x]

tspan = (0., 1.)
def = [x => 1.0, α => -0.5]

@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)]
new_sys = changeofvariables(sys, t, forward_subs, backward_subs)
@test equations(new_sys)[1] == (D(z) ~ α)

new_prob = ODEProblem(new_sys, [], tspan)
new_sol = solve(new_prob, Tsit5())

@test isapprox(new_sol[x][end], sol[x][end], atol=1e-4)



# Riccati equation
@parameters α
@variables x(t)
D = Differential(t)
eqs = [D(x) ~ t^2 + α - x^2]
def = [x=>1., α => 1.]
@mtkcompile sys = System(eqs, t; defaults=def)

@variables z(t)
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.)
# output should be equivalent to
# t^2 + α - z^2 + 2 (but this simplification is not found automatically)

tspan = (0., 1.)
prob = ODEProblem(sys,[],tspan)
new_prob = ODEProblem(new_sys,[],tspan)

sol = solve(prob, Tsit5())
new_sol = solve(new_prob, Tsit5())

@test isapprox(sol[x][end], new_sol[x][end], rtol=1e-4)


# Linear transformation to diagonal system
@independent_variables t
@variables x(t)[1:3]
x = reshape(x, 3, 1)
D = Differential(t)
A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.]
right = A*x
eqs = vec(D.(x) .~ right)

tspan = (0., 10.)
u0 = [x[1] => 1.0, x[2] => 2.0, x[3] => -1.0]

@mtkcompile sys = System(eqs, t; defaults=u0)
prob = ODEProblem(sys,[],tspan)
sol = solve(prob, Tsit5())

T = eigen(A).vectors
T_inv = inv(T)

@variables z(t)[1:3]
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_prob = ODEProblem(new_sys, [], tspan)
new_sol = solve(new_prob, Tsit5())

# test RHS
new_rhs = [eq.rhs for eq in equations(new_sys)]
new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z))
A = diagm(eigen(A).values)
A = sortslices(A, dims=1)
new_A = sortslices(new_A, dims=1)
@test isapprox(A, new_A, rtol = 1e-10)
@test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4)

# Change of variables for sde
noise_eqs = ModelingToolkit.get_noise_eqs
value = ModelingToolkit.value

@independent_variables t
@brownian B
@parameters μ σ
@variables x(t) y(t)
D = Differential(t)
eqs = [D(x) ~ μ*x + σ*x*B]

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)
@test equations(new_sys)[1] == (D(y) ~ μ - 1/2*σ^2)
@test noise_eqs(new_sys)[1] === value(σ)

#Multiple Brownian and equations
@independent_variables t
@brownian Bx By
@parameters μ σ α
@variables x(t) y(t) z(t) w(t) u(t) v(t)
D = Differential(t)
eqs = [D(x) ~ μ*x + σ*x*Bx, D(y) ~ α*By, D(u) ~ μ*u + σ*u*Bx + α*u*By]
def = [x=>0., y=> 0., u=>0., μ => 2., σ=>1., α=>3.]
@mtkcompile sys = System(eqs, t; defaults=def)
forward_subs = [log(x) => z, y^2 => w, log(u) => v]
backward_subs = [x => exp(z), y => w^.5, u => exp(v)]
new_sys = changeofvariables(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))
@test noise_eqs(new_sys)[1,1] === value(σ)
@test noise_eqs(new_sys)[1,2] === value(0)
@test noise_eqs(new_sys)[2,1] === value(0)
@test noise_eqs(new_sys)[2,2] === value(substitute(2*α*y, backward_subs[2]))
@test noise_eqs(new_sys)[3,1] === value(σ)
@test noise_eqs(new_sys)[3,2] === value(α)