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

Validate types of state variables in XSystem construction #3340

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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 src/systems/diffeqs/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ struct ODESystem <: AbstractODESystem
check_parameters(ps, iv)
check_equations(deqs, iv)
check_equations(equations(cevents), iv)
check_var_types(ODESystem, dvs)
end
if checks == true || (checks & CheckUnits) > 0
u = __get_unit_type(dvs, ps, iv)
Expand Down
1 change: 1 addition & 0 deletions src/systems/diffeqs/sdesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ struct SDESystem <: AbstractODESystem
check_parameters(ps, iv)
check_equations(deqs, iv)
check_equations(neqs, dvs)
check_var_types(SDESystem, dvs)
if size(neqs, 1) != length(deqs)
throw(ArgumentError("Noise equations ill-formed. Number of rows must match number of drift equations. size(neqs,1) = $(size(neqs,1)) != length(deqs) = $(length(deqs))"))
end
Expand Down
1 change: 1 addition & 0 deletions src/systems/discrete_system/discrete_system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ struct DiscreteSystem <: AbstractTimeDependentSystem
check_independent_variables([iv])
check_variables(dvs, iv)
check_parameters(ps, iv)
check_var_types(DiscreteSystem, dvs)
end
if checks == true || (checks & CheckUnits) > 0
u = __get_unit_type(dvs, ps, iv)
Expand Down
1 change: 1 addition & 0 deletions src/systems/jumps/jumpsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ struct JumpSystem{U <: ArrayPartition} <: AbstractTimeDependentSystem
check_independent_variables([iv])
check_variables(unknowns, iv)
check_parameters(ps, iv)
check_var_types(JumpSystem, unknowns)
end
if checks == true || (checks & CheckUnits) > 0
u = __get_unit_type(unknowns, ps, iv)
Expand Down
3 changes: 3 additions & 0 deletions src/systems/nonlinear/nonlinearsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ struct NonlinearSystem <: AbstractTimeIndependentSystem
tearing_state = nothing, substitutions = nothing,
complete = false, index_cache = nothing, parent = nothing,
isscheduled = false; checks::Union{Bool, Int} = true)
if checks == true || (checks & CheckComponents) > 0
check_var_types(NonlinearSystem, unknowns)
end
if checks == true || (checks & CheckUnits) > 0
u = __get_unit_type(unknowns, ps)
check_units(u, eqs)
Expand Down
3 changes: 3 additions & 0 deletions src/systems/optimization/optimizationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ struct OptimizationSystem <: AbstractOptimizationSystem
gui_metadata = nothing, complete = false, index_cache = nothing, parent = nothing,
isscheduled = false;
checks::Union{Bool, Int} = true)
if checks == true || (checks & CheckComponents) > 0
check_var_types(OptimizationSystem, unknowns)
end
if checks == true || (checks & CheckUnits) > 0
u = __get_unit_type(unknowns, ps)
unwrap(op) isa Symbolic && check_units(u, op)
Expand Down
3 changes: 3 additions & 0 deletions src/systems/pde/pdesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem
checks::Union{Bool, Int} = true,
description = "",
name)
if checks == true || (checks & CheckComponents) > 0
check_var_types(PDESystem, dvs)
end
if checks == true || (checks & CheckUnits) > 0
u = __get_unit_type(dvs, ivs, ps)
check_units(u, eqs)
Expand Down
12 changes: 12 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ function check_variables(dvs, iv)
end
end

function check_var_types(sys_type::Type{T}, dvs) where T <: AbstractSystem
if any(u -> !(symtype(u) <: Number || eltype(symtype(u)) <: Number), dvs)
error("The type of unknown variables must be a numeric type.")
elseif any(u -> (symtype(u) !== symtype(dvs[1])), dvs)
error("The type of all the unknown variables in a system must all be the same.")
elseif sys_type == ODESystem || sys_type == SDESystem || sys_type == PDESystem
any(u -> !(symtype(u) == Real || eltype(symtype(u)) == Real), dvs) && error("The type of unknown variables for an SDESystem, PDESystem, or ODESystem should not be a concrete numeric type.")
end
nothing
end

function check_lhs(eq::Equation, op, dvs::Set)
v = unwrap(eq.lhs)
_iszero(v) && return
Expand Down Expand Up @@ -1182,3 +1193,4 @@ function guesses_from_metadata!(guesses, vars)
guesses[vars[i]] = varguesses[i]
end
end

9 changes: 8 additions & 1 deletion test/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,13 @@ end
@test all(x -> any(isapprox(x, atol = 1e-6), sol2.t), expected_tstops)
end

@testset "Validate input types" begin
@parameters p d
@variables X(t)::Int64
eq = D(X) ~ p - d*X
@test_throws Exception @mtkbuild osys = ODESystem([eq], t)
end

@testset "dae_order_lowering basic test" begin
@parameters a
@variables x(t) y(t) z(t)
Expand Down Expand Up @@ -1608,4 +1615,4 @@ end

prob = ODEProblem{false}(lowered_dae_sys; u0_constructor = x -> SVector(x...))
@test prob.u0 isa SVector
end
end
9 changes: 9 additions & 0 deletions test/sdesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -868,3 +868,12 @@ end
@test length(ModelingToolkit.get_noiseeqs(sys)) == 1
@test length(observed(sys)) == 1
end

# Test validating types of states
@testset "Validate input types" begin
@parameters p d
@variables X(t)::Int64
@brownian z
eq2 = D(X) ~ p - d*X + z
@test_throws Exception @mtkbuild ssys = System([eq2], t)
end
Loading