Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed May 27, 2024
1 parent ee0fed0 commit b07631a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/systems/diffeqs/abstractodesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1091,17 +1091,16 @@ function DiffEqBase.ODEProblem{iip, specialize}(sys::AbstractODESystem, u0map =
if has_discrete_subsystems(sys) && (dss = get_discrete_subsystems(sys)) !== nothing
affects, inits, clocks, svs = ModelingToolkit.generate_discrete_affect(sys, dss...)
discrete_cbs = map(affects, clocks, svs) do affect, clock, sv
daffect = DiscreteSaveAffect(affect, sv)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1094

Added line #L1094 was not covered by tests
if clock isa Clock
PeriodicCallback(DiscreteSaveAffect(affect, sv), clock.dt;
PeriodicCallback(daffect, clock.dt;

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

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1096

Added line #L1096 was not covered by tests
final_affect = true, initial_affect = true)
elseif clock isa SolverStepClock
affect = DiscreteSaveAffect(affect, sv)
DiscreteCallback(Returns(true), affect,
initialize = (c, u, t, integrator) -> affect(integrator))
DiscreteCallback(Returns(true), daffect,
initialize = (c, u, t, integrator) -> daffect(integrator))
elseif clock isa EventClock
tempsys = @set sys.continuous_events = [SymbolicContinuousCallback(clock.cond)]
cb = generate_rootfinding_callback(tempsys)
daffect = DiscreteSaveAffect(affect, sv)
@set! cb.affect! = daffect
@set! cb.affect_neg! = daffect
cb

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

View check run for this annotation

Codecov / codecov/patch

src/systems/diffeqs/abstractodesystem.jl#L1099-L1106

Added lines #L1099 - L1106 were not covered by tests
Expand Down
10 changes: 5 additions & 5 deletions test/clock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -561,17 +561,17 @@ end
c2 = ModelingToolkit.EventClock(t, fo.x ~ 0.1)
end
@equations begin
# counter.u ~ Sample(c2)(fo.x)
counter.u ~ Sample(t, 0.1)(fo.x)
counter.u ~ Sample(c2)(fo.x)
# counter.u ~ Sample(t, 0.1)(fo.x)
end
end

@mtkbuild model = FirstOrderWithCrossingCounter()
prob = ODEProblem(model, [], (0.0, 30.0))
prob = ODEProblem(model, [model.counter.count(k - 1)=>0], (0.0, 30.0))
sol = solve(prob, Tsit5(), kwargshandle = KeywordArgSilent)

@test all(x -> isapprox(0.1, x[], rtol = 1e-6),
sol.prob.kwargs[:disc_saved_values][1].saveval[2:end]) # omit first value due to initial value of count in count(k+1) ~ u
@test_broken all(x -> isapprox(0.1, x[], rtol = 1e-6),
sol.prob.kwargs[:disc_saved_values][1].saveval) # Values are not saved
@test length(sol.prob.kwargs[:disc_saved_values][1].t) == 10 # number of crossings of 0.1

# plot(sol)
Expand Down

0 comments on commit b07631a

Please sign in to comment.