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

rm old discrete system #2413

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 2 additions & 6 deletions src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ include("systems/optimization/modelingtoolkitize.jl")
include("systems/pde/pdesystem.jl")

include("systems/sparsematrixclil.jl")
include("systems/discrete_system/discrete_system.jl")

include("systems/unit_check.jl")
include("systems/validation.jl")
include("systems/dependency_graphs.jl")
Expand Down Expand Up @@ -198,7 +198,7 @@ export NonlinearProblem, BlockNonlinearProblem, NonlinearProblemExpr
export OptimizationProblem, OptimizationProblemExpr, constraints
export AutoModelingToolkit
export SteadyStateProblem, SteadyStateProblemExpr
export JumpProblem, DiscreteProblem
export JumpProblem
export NonlinearSystem, OptimizationSystem, ConstraintsSystem
export alias_elimination, flatten
export connect, domain_connect, @connector, Connection, Flow, Stream, instream
Expand All @@ -216,9 +216,6 @@ export SymScope, LocalScope, ParentScope, DelayParentScope, GlobalScope
export independent_variable, equations, controls,
observed, structure, full_equations
export structural_simplify, expand_connections, linearize, linearization_function
export DiscreteSystem,
DiscreteProblem, DiscreteProblemExpr, DiscreteFunction,
DiscreteFunctionExpr

export calculate_jacobian, generate_jacobian, generate_function
export calculate_control_jacobian, generate_control_jacobian
Expand All @@ -229,7 +226,6 @@ export calculate_hessian, generate_hessian
export calculate_massmatrix, generate_diffusion_function
export stochastic_integral_transform
export TearingState, StateSelectionState
export generate_difference_cb

export BipartiteGraph, equation_dependencies, variable_dependencies
export eqeq_dependencies, varvar_dependencies
Expand Down
2 changes: 1 addition & 1 deletion src/clock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ end
has_time_domain(x::Num) = has_time_domain(value(x))
has_time_domain(x) = false

for op in [Differential, Difference]
for op in [Differential]
@eval input_timedomain(::$op, arg = nothing) = Continuous()
@eval output_timedomain(::$op, arg = nothing) = Continuous()
end
Expand Down
3 changes: 1 addition & 2 deletions src/structural_transformation/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,7 @@
u0 = ModelingToolkit.varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true)
p = ModelingToolkit.varmap_to_vars(parammap, ps; defaults = defs, tofloat, use_union)

has_difference = any(isdifferenceeq, eqs)
cbs = process_events(sys; callback, has_difference, kwargs...)
cbs = process_events(sys; callback, kwargs...)

Check warning on line 546 in src/structural_transformation/codegen.jl

View check run for this annotation

Codecov / codecov/patch

src/structural_transformation/codegen.jl#L546

Added line #L546 was not covered by tests

kwargs = filter_kwargs(kwargs)
if cbs === nothing
Expand Down
7 changes: 2 additions & 5 deletions src/systems/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@
merge_cb(x, ::Nothing) = x
merge_cb(x, y) = CallbackSet(x, y)

function process_events(sys; callback = nothing, has_difference = false, kwargs...)
function process_events(sys; callback = nothing, kwargs...)

Check warning on line 501 in src/systems/callbacks.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/callbacks.jl#L501

Added line #L501 was not covered by tests
if has_continuous_events(sys)
contin_cb = generate_rootfinding_callback(sys; kwargs...)
else
Expand All @@ -509,9 +509,6 @@
else
discrete_cb = nothing
end
difference_cb = has_difference ? generate_difference_cb(sys; kwargs...) : nothing

cb = merge_cb(contin_cb, difference_cb)
cb = merge_cb(cb, callback)
(discrete_cb === nothing) ? cb : CallbackSet(cb, discrete_cb...)
(discrete_cb === nothing) ? contin_cb : CallbackSet(contin_cb, discrete_cb...)

Check warning on line 513 in src/systems/callbacks.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/callbacks.jl#L513

Added line #L513 was not covered by tests
end
2 changes: 1 addition & 1 deletion src/systems/connectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ end

function generate_connection_equations_and_stream_connections(csets::AbstractVector{
<:ConnectionSet,
})
})
eqs = Equation[]
stream_connections = ConnectionSet[]

Expand Down
80 changes: 15 additions & 65 deletions src/systems/diffeqs/abstractodesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@
ddvs = implicit_dae ? map(Differential(get_iv(sys)), dvs) :
nothing,
isdde = false,
has_difference = false,
kwargs...)
if isdde
eqs = delay_to_function(sys)
Expand All @@ -167,8 +166,7 @@
if isdde
build_function(rhss, u, DDE_HISTORY_FUN, p, t; kwargs...)
else
pre, sol_states = get_substitutions_and_solved_states(sys,
no_postprocess = has_difference)
pre, sol_states = get_substitutions_and_solved_states(sys)

Check warning on line 169 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L169

Added line #L169 was not covered by tests

if implicit_dae
build_function(rhss, ddvs, u, p, t; postprocess_fbody = pre,
Expand Down Expand Up @@ -226,52 +224,6 @@
end
end

function generate_difference_cb(sys::ODESystem, dvs = states(sys), ps = parameters(sys);
kwargs...)
eqs = equations(sys)
check_operator_variables(eqs, Difference)

var2eq = Dict(arguments(eq.lhs)[1] => eq for eq in eqs if isdifference(eq.lhs))

u = map(x -> time_varying_as_func(value(x), sys), dvs)
p = map(x -> time_varying_as_func(value(x), sys), ps)
t = get_iv(sys)

body = map(dvs) do v
eq = get(var2eq, v, nothing)
eq === nothing && return v
d = operation(eq.lhs)
d.update ? eq.rhs : eq.rhs + v
end

pre = get_postprocess_fbody(sys)
cpre = get_preprocess_constants(body)
pre2 = x -> pre(cpre(x))
f_oop, f_iip = build_function(body, u, p, t; expression = Val{false},
postprocess_fbody = pre2, kwargs...)

cb_affect! = let f_oop = f_oop, f_iip = f_iip
function cb_affect!(integ)
if DiffEqBase.isinplace(integ.sol.prob)
tmp, = DiffEqBase.get_tmp_cache(integ)
f_iip(tmp, integ.u, integ.p, integ.t) # aliasing `integ.u` would be bad.
copyto!(integ.u, tmp)
else
integ.u = f_oop(integ.u, integ.p, integ.t)
end
return nothing
end
end

getdt(eq) = operation(eq.lhs).dt
deqs = values(var2eq)
dt = getdt(first(deqs))
all(dt == getdt(eq) for eq in deqs) ||
error("All difference variables should have same time steps.")

PeriodicCallback(cb_affect!, first(dt))
end

function calculate_massmatrix(sys::AbstractODESystem; simplify = false)
eqs = [eq for eq in equations(sys) if !isdifferenceeq(eq)]
dvs = states(sys)
Expand Down Expand Up @@ -940,11 +892,12 @@
check_length = true,
kwargs...) where {iip, specialize}
has_difference = any(isdifferenceeq, equations(sys))
has_difference &&

Check warning on line 895 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L895

Added line #L895 was not covered by tests
error("The operators Difference and DiscreteUpdate are deprecated. Use ShiftIndex instead.")
f, u0, p = process_DEProblem(ODEFunction{iip, specialize}, sys, u0map, parammap;
t = tspan !== nothing ? tspan[1] : tspan,
has_difference = has_difference,
check_length, kwargs...)
cbs = process_events(sys; callback, has_difference, kwargs...)
cbs = process_events(sys; callback, kwargs...)

Check warning on line 900 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L900

Added line #L900 was not covered by tests
if has_discrete_subsystems(sys) && (dss = get_discrete_subsystems(sys)) !== nothing
affects, clocks, svs = ModelingToolkit.generate_discrete_affect(dss...)
discrete_cbs = map(affects, clocks, svs) do affect, clock, sv
Expand Down Expand Up @@ -1003,23 +956,18 @@
parammap = DiffEqBase.NullParameters();
check_length = true, kwargs...) where {iip}
has_difference = any(isdifferenceeq, equations(sys))
has_difference &&

Check warning on line 959 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L959

Added line #L959 was not covered by tests
error("The operators Difference and DiscreteUpdate are deprecated. Use ShiftIndex instead.")
f, du0, u0, p = process_DEProblem(DAEFunction{iip}, sys, u0map, parammap;
implicit_dae = true, du0map = du0map,
has_difference = has_difference, check_length,
implicit_dae = true, du0map = du0map, check_length,
kwargs...)
diffvars = collect_differential_variables(sys)
sts = states(sys)
differential_vars = map(Base.Fix2(in, diffvars), sts)
kwargs = filter_kwargs(kwargs)

if has_difference
DAEProblem{iip}(f, du0, u0, tspan, p;
difference_cb = generate_difference_cb(sys; kwargs...),
differential_vars = differential_vars, kwargs...)
else
DAEProblem{iip}(f, du0, u0, tspan, p; differential_vars = differential_vars,
kwargs...)
end
DAEProblem{iip}(f, du0, u0, tspan, p; differential_vars = differential_vars,

Check warning on line 969 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L969

Added line #L969 was not covered by tests
kwargs...)
end

function generate_history(sys::AbstractODESystem, u0; kwargs...)
Expand All @@ -1036,15 +984,16 @@
check_length = true,
kwargs...) where {iip}
has_difference = any(isdifferenceeq, equations(sys))
has_difference &&

Check warning on line 987 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L987

Added line #L987 was not covered by tests
error("The operators Difference and DiscreteUpdate are deprecated. Use ShiftIndex instead.")
f, u0, p = process_DEProblem(DDEFunction{iip}, sys, u0map, parammap;
t = tspan !== nothing ? tspan[1] : tspan,
has_difference = has_difference,
symbolic_u0 = true,
check_length, kwargs...)
h_oop, h_iip = generate_history(sys, u0)
h = h_oop
u0 = h(p, tspan[1])
cbs = process_events(sys; callback, has_difference, kwargs...)
cbs = process_events(sys; callback, kwargs...)

Check warning on line 996 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L996

Added line #L996 was not covered by tests
if has_discrete_subsystems(sys) && (dss = get_discrete_subsystems(sys)) !== nothing
affects, clocks, svs = ModelingToolkit.generate_discrete_affect(dss...)
discrete_cbs = map(affects, clocks, svs) do affect, clock, sv
Expand Down Expand Up @@ -1089,16 +1038,17 @@
sparsenoise = nothing,
kwargs...) where {iip}
has_difference = any(isdifferenceeq, equations(sys))
has_difference &&

Check warning on line 1041 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1041

Added line #L1041 was not covered by tests
error("The operators Difference and DiscreteUpdate are deprecated. Use ShiftIndex instead.")
f, u0, p = process_DEProblem(SDDEFunction{iip}, sys, u0map, parammap;
t = tspan !== nothing ? tspan[1] : tspan,
has_difference = has_difference,
symbolic_u0 = true,
check_length, kwargs...)
h_oop, h_iip = generate_history(sys, u0)
h(out, p, t) = h_iip(out, p, t)
h(p, t) = h_oop(p, t)
u0 = h(p, tspan[1])
cbs = process_events(sys; callback, has_difference, kwargs...)
cbs = process_events(sys; callback, kwargs...)

Check warning on line 1051 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1051

Added line #L1051 was not covered by tests
if has_discrete_subsystems(sys) && (dss = get_discrete_subsystems(sys)) !== nothing
affects, clocks, svs = ModelingToolkit.generate_discrete_affect(dss...)
discrete_cbs = map(affects, clocks, svs) do affect, clock, sv
Expand Down
Loading
Loading