Skip to content

Commit d2a0aa6

Browse files
committed
WIP
1 parent 40491ea commit d2a0aa6

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ authors = ["AnHeuermann"]
77
BaseModelica = "a17d5099-185d-4ff5-b5d3-51aa4569e56d"
88
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
99
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
10+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1011
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
1112
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
1213
OMJulia = "0f4fe800-344e-11e9-2949-fb537ad918e1"
@@ -17,6 +18,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1718
ZMQ = "c2297ded-f4af-51ae-bb23-16f91089e4e1"
1819

1920
[compat]
21+
LinearAlgebra = "1.12.0"
2022
OMJulia = "0.3.3"
2123
julia = "1.10.10, 1.12.5"
2224

src/simulate.jl

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# ── Phase 3: ODE simulation with DifferentialEquations / MTK ──────────────────
22

33
import DifferentialEquations
4+
import LinearAlgebra
45
import OrdinaryDiffEqBDF
56
import Logging
67
import ModelingToolkit
78
import Printf: @sprintf
89

910
"""Module-level default simulation settings. Modify via `configure_simulate!`."""
10-
const _SIM_SETTINGS = SimulateSettings(solver = DifferentialEquations.Rodas5Pr())
11+
const _SIM_SETTINGS = SimulateSettings(solver = DifferentialEquations.Rodas5P())
1112

1213
"""
1314
configure_simulate!(; solver, saveat_n) → SimulateSettings
@@ -82,21 +83,50 @@ function run_simulate(ode_prob,
8283
# For stateless models (no unknowns) the adaptive solver takes no
8384
# internal steps and sol.t would be empty with saveat=[].
8485
# Supply explicit time points so observed variables can be evaluated.
85-
sys = ode_prob.f.sys
86-
saveat = isempty(ModelingToolkit.unknowns(sys)) ?
87-
collect(range(ode_prob.tspan[1], ode_prob.tspan[end]; length = settings.saveat_n)) :
88-
Float64[]
89-
kwargs = (saveat = saveat, dense = true)
86+
sys = ode_prob.f.sys
87+
M = ode_prob.f.mass_matrix
88+
unknowns = ModelingToolkit.unknowns(sys)
89+
n_unknowns = length(unknowns)
90+
n_diff = if M isa LinearAlgebra.UniformScaling
91+
n_unknowns
92+
else
93+
count(!iszero, LinearAlgebra.diag(M))
94+
end
95+
96+
@show n_diff
97+
98+
kwargs = if n_unknowns == 0
99+
# No unknowns at all (e.g. BusUsage): the solver takes no
100+
# internal steps with saveat=[], leaving sol.t empty.
101+
# Use a fixed grid + adaptive=false so observed variables
102+
# can be evaluated.
103+
t0_s, t1_s = ode_prob.tspan
104+
saveat_s = collect(range(t0_s, t1_s; length = settings.saveat_n))
105+
dt_s = saveat_s[2] - saveat_s[1]
106+
(saveat = saveat_s, adaptive = false, dt = dt_s, dense = false)
107+
elseif n_diff == 0
108+
# Algebraic unknowns only (e.g. CharacteristicIdealDiodes):
109+
# the solver must take adaptive steps to track discontinuities.
110+
# Keep saveat=[] + dense=true so the solver drives its own
111+
# step selection; dense output is unreliable but the solution
112+
# values at each step are correct.
113+
(saveat = Float64[], dense = true)
114+
else
115+
(saveat = Float64[], dense = true)
116+
end
90117

91118
# Log solver settings — init returns NullODEIntegrator (no .opts)
92119
# when the problem has no unknowns (u::Nothing), so only inspect
93120
# opts when a real integrator is returned.
121+
# Use our own `saveat` vector for the log: integ.opts.saveat is a
122+
# BinaryHeap which does not support iterate/minimum/maximum.
94123
integ = DifferentialEquations.init(ode_prob, solver; kwargs...)
124+
saveat = kwargs.saveat
95125
solver_settings_string = if hasproperty(integ, :opts)
96-
sv = integ.opts.saveat
126+
sv_str = isempty(saveat) ? "[]" : "$(length(saveat)) points in [$(first(saveat)), $(last(saveat))]"
97127
"""
98128
Solver $(parentmodule(typeof(solver))).$(nameof(typeof(solver)))
99-
saveat: $(isempty(sv) ? "[]" : "$(length(sv)) points in [$(first(sv)), $(last(sv))]")
129+
saveat: $sv_str
100130
abstol: $(@sprintf("%.2e", integ.opts.abstol))
101131
reltol: $(@sprintf("%.2e", integ.opts.reltol))
102132
adaptive: $(integ.opts.adaptive)

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ const TEST_MODEL_CHUA = "Modelica.Electrical.Analog.Examples.ChuaCircuit"
3131
include("unit_helpers.jl")
3232
include("chua_circuit.jl")
3333
include("bus_usage.jl")
34+
include("characteristic_ideal_diodes.jl")
3435
include("amplifier_with_op_amp.jl")

0 commit comments

Comments
 (0)