Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added tests for SDEFunctionExpr #3313

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
52 changes: 52 additions & 0 deletions test/sdesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -868,3 +868,55 @@ end
@test length(ModelingToolkit.get_noiseeqs(sys)) == 1
@test length(observed(sys)) == 1
end

@testset "SDEFunctionExpr" begin
@parameters σ ρ β
@variables x(tt) y(tt) z(tt)

eqs = [D(x) ~ σ * (y - x),
D(y) ~ x * (ρ - z) - y,
D(z) ~ x * y - β * z]

noiseeqs = [0.1 * x,
0.1 * y,
0.1 * z]

@named sys = ODESystem(eqs, tt, [x, y, z], [σ, ρ, β])

@named de = SDESystem(eqs, noiseeqs, tt, [x, y, z], [σ, ρ, β], tspan = (0.0, 10.0))
de = complete(de)

f = SDEFunctionExpr(de)
@test f isa Expr

@testset "Configuration Tests" begin
# Test with `tgrad`
f_tgrad = SDEFunctionExpr(de; tgrad = true)
@test f_tgrad isa Expr

# Test with `jac`
f_jac = SDEFunctionExpr(de; jac = true)
@test f_jac isa Expr

# Test with sparse Jacobian
f_sparse = SDEFunctionExpr(de; sparse = true)
@test f_sparse isa Expr
end

@testset "Mass Matrix Handling" begin
# Test with default mass matrix
@test eval(SDEFunctionExpr(de).args[7]) == UniformScaling{Bool}(true)

# Test with non-trivial mass matrix
u0 = [1.0, 2.0, 3.0]
Copy link
Member

Choose a reason for hiding this comment

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

Why would this have a non-trivial mass matrix?

Copy link
Contributor Author

@sivasathyaseeelan sivasathyaseeelan Jan 12, 2025

Choose a reason for hiding this comment

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

non-trival mass matrix will test

_M = (u0 === nothing || M == I) ? M : ArrayInterface.restructure(u0 .* u0', M)

the above condition in SDEFunctionExpr

Copy link
Member

Choose a reason for hiding this comment

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

But this case doesn't have a non-trivial mass matrix. It's still identity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh got it

And I think we don't need to test Mass Matrix handling. As the above condition will be executed by default

f_mass = SDEFunctionExpr(de, u0 = u0)
@test f_mass isa Expr
end

@testset "Ordering Tests" begin
dvs = [z, y, x]
ps = [β, ρ, σ]
f_order = SDEFunctionExpr(de, dvs, ps)
@test f_order isa Expr
end
end
Loading