Skip to content

Commit

Permalink
rm old discrete system
Browse files Browse the repository at this point in the history
rm more stuff
  • Loading branch information
baggepinnen committed Jan 5, 2024
1 parent 1bd482d commit 71cc9f6
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 880 deletions.
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 @@ function ODAEProblem{iip}(sys,
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(::Nothing, x) = merge_cb(x, nothing)
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 @@ function process_events(sys; callback = nothing, has_difference = false, kwargs.
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
74 changes: 10 additions & 64 deletions src/systems/diffeqs/abstractodesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ function generate_function(sys::AbstractODESystem, dvs = states(sys), ps = param
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 @@ function generate_function(sys::AbstractODESystem, dvs = states(sys), ps = param
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 @@ function delay_to_function(expr, iv, sts, ps, h)
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,11 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map =
check_length = true,
kwargs...) where {iip, specialize}
has_difference = any(isdifferenceeq, equations(sys))
has_difference && error("The operators Difference and DiscreteUpdate are deprecated. Use ShiftIndex instead.")

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
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 899 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L899

Added line #L899 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 +955,17 @@ function DiffEqBase.DAEProblem{iip}(sys::AbstractODESystem, du0map, u0map, tspan
parammap = DiffEqBase.NullParameters();
check_length = true, kwargs...) where {iip}
has_difference = any(isdifferenceeq, equations(sys))
has_difference && error("The operators Difference and DiscreteUpdate are deprecated. Use ShiftIndex instead.")

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

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L958

Added line #L958 was not covered by tests
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,
DAEProblem{iip}(f, du0, u0, tspan, p; differential_vars = differential_vars,

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

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L967

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

function generate_history(sys::AbstractODESystem, u0; kwargs...)
Expand All @@ -1036,15 +982,15 @@ function DiffEqBase.DDEProblem{iip}(sys::AbstractODESystem, u0map = [],
check_length = true,
kwargs...) where {iip}
has_difference = any(isdifferenceeq, equations(sys))
has_difference && error("The operators Difference and DiscreteUpdate are deprecated. Use ShiftIndex instead.")

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

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L985

Added line #L985 was not covered by tests
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 993 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L993

Added line #L993 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 +1035,16 @@ function DiffEqBase.SDDEProblem{iip}(sys::AbstractODESystem, u0map = [],
sparsenoise = nothing,
kwargs...) where {iip}
has_difference = any(isdifferenceeq, equations(sys))
has_difference && error("The operators Difference and DiscreteUpdate are deprecated. Use ShiftIndex instead.")

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

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1038

Added line #L1038 was not covered by tests
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 1047 in src/systems/diffeqs/abstractodesystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1047

Added line #L1047 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

0 comments on commit 71cc9f6

Please sign in to comment.