Skip to content

Commit

Permalink
test: use SciMLBase.successful_retcode(sol)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianM-C committed Oct 18, 2024
1 parent 9f7f000 commit fb9e3f2
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
10 changes: 5 additions & 5 deletions test/Blocks/nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using OrdinaryDiffEq: ReturnCode.Success
prob = ODEProblem(sys, [int.x => 1.0], (0.0, 1.0))

sol = solve(prob, Rodas4())
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[int.output.u][end] 2
@test sol[sat.output.u][end] 0.8
end
Expand All @@ -42,7 +42,7 @@ using OrdinaryDiffEq: ReturnCode.Success
prob = ODEProblem(sys, unknowns(sys) .=> 0.0, (0.0, 10.0))

sol = solve(prob, Rodas4())
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test all(abs.(sol[lim.output.u]) .<= 0.5)
@test all(isapprox.(sol[lim.output.u], _clamp.(sol[source.output.u], y_min, y_max),
atol = 1e-2))
Expand All @@ -69,7 +69,7 @@ end
prob = ODEProblem(sys, [int.x => 1.0], (0.0, 1.0))
sol = solve(prob, Rodas4())

@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test all(sol[int.output.u][end] .≈ 2)
end

Expand All @@ -89,7 +89,7 @@ end
prob = ODEProblem(sys, [int.x => 1.0], (0.0, 10.0))
sol = solve(prob, Rodas4())

@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test all(sol[dz.output.u] .<= 2)
@test all(sol[dz.output.u] .>= -1)
@test all(isapprox.(sol[dz.output.u],
Expand All @@ -115,7 +115,7 @@ end

tS = 0.01
sol = solve(prob, Rodas4(), saveat = tS, abstol = 1e-10, reltol = 1e-10)
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test all(abs.(sol[rl.output.u]) .<= 0.51)
@test all(-1 - 1e-5 .<= diff(sol[rl.output.u]) ./ tS .<= 1 + 1e-5) # just an approximation
end
20 changes: 10 additions & 10 deletions test/Electrical/analog.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ using OrdinaryDiffEq: ReturnCode.Success
# Plots.plot(sol; vars=[capacitor.v, voltage_sensor.v])
# Plots.plot(sol; vars=[power_sensor.power, capacitor.i * capacitor.v])
# Plots.plot(sol; vars=[resistor.i, current_sensor.i])
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[capacitor.v]sol[voltage_sensor.v] atol=1e-3
@test sol[power_sensor.power]sol[capacitor.i * capacitor.v] atol=1e-3
@test sol[resistor.i]sol[current_sensor.i] atol=1e-3
Expand All @@ -75,7 +75,7 @@ end
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[R2.i => 0.0], (0, 2.0))
sol = solve(prob, Rodas4()) # has no state; does not work with Tsit5
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[short.v] == sol[R0.v] == zeros(length(sol.t))
@test sol[R0.i] == zeros(length(sol.t))
@test sol[R1.p.v][end]10 atol=1e-3
Expand Down Expand Up @@ -103,7 +103,7 @@ end
sol = solve(prob, Tsit5())

# Plots.plot(sol; vars=[source.v, capacitor.v])
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[capacitor.v][end]10 atol=1e-3
end

Expand All @@ -127,7 +127,7 @@ end
sol = solve(prob, Tsit5())

# Plots.plot(sol; vars=[inductor.i, inductor.i])
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[inductor.i][end]10 atol=1e-3
end

Expand Down Expand Up @@ -160,9 +160,9 @@ end
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
sol = solve(prob, Tsit5())
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
sol = solve(prob, Rodas4())
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)

# Plots.plot(sol; vars=[voltage.v, capacitor.v])
end
Expand All @@ -188,7 +188,7 @@ end
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
sol = solve(prob, Tsit5())
y(x, st) = (x .> st) .* abs.(collect(x) .- st)
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sum(reduce(vcat, sol[capacitor.v]) .- y(sol.t, start_time))0 atol=1e-2
end

Expand Down Expand Up @@ -228,7 +228,7 @@ end
R1.v => 0.0]
prob = ODEProblem(sys, u0, (0, 100.0))
sol = solve(prob, Rodas4())
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[opamp.v2] == sol[C1.v] # Not a great one however. Rely on the plot
@test sol[opamp.p2.v] == sol[sensor.v]

Expand Down Expand Up @@ -298,7 +298,7 @@ _damped_sine_wave(x, f, A, st, ϕ, d) = exp((st - x) * d) * A * sin(2 * π * f *
prob = ODEProblem(vsys, u0, (0, 10.0))
sol = solve(prob, dt = 0.1, Tsit5())

@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[voltage.V.u]waveforms(i, sol.t) atol=1e-1
@test sol[voltage.p.v] sol[voltage.V.u]
# For visual inspection
Expand Down Expand Up @@ -364,7 +364,7 @@ end
prob = ODEProblem(isys, u0, (0, 10.0))
sol = solve(prob, dt = 0.1, Tsit5())

@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[current.I.u]waveforms(i, sol.t) atol=1e-1
@test sol[current.I.u]sol[current.p.i] atol=1e-1
# For visual inspection
Expand Down
2 changes: 1 addition & 1 deletion test/Magnetic/magnetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ using OrdinaryDiffEq: ReturnCode.Success
# Plots.plot(sol; vars=[r.i])
# Plots.plot(sol; vars=[r_mFe.V_m, r_mFe.Phi])

@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[r_mFe.Phi] == sol[r_mAirPar.Phi]
@test all(sol[coil.port_p.Phi] + sol[r_mLeak.Phi] + sol[r_mAirPar.Phi] .== 0)
end
2 changes: 1 addition & 1 deletion test/Thermal/demo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ using OrdinaryDiffEq: ReturnCode.Success
sys = structural_simplify(model)
prob = ODEProblem(sys, [mass1.der_T => 1.0, mass2.der_T => 1.0], (0, 3.0))
sol = solve(prob, Tsit5())
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
end
2 changes: 1 addition & 1 deletion test/Thermal/motor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ using ModelingToolkitStandardLibrary.Blocks
sol = solve(prob)

# plot(sol; vars=[T_winding.T, T_core.T])
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[motor.T_winding.T] == sol[motor.winding.T]
@test sol[motor.T_core.T] == sol[motor.core.T]
@test sol[-motor.core.port.Q_flow]
Expand Down
2 changes: 1 addition & 1 deletion test/Thermal/piston.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ using ModelingToolkitStandardLibrary.Blocks

# Heat-flow-rate is equal in magnitude
# and opposite in direction
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
# The initial value doesn't add up to absolute zero, while the rest do. To avoid
# tolerance on the latter, the test is split in two parts.
@test sol[piston.gas.Q_flow][1] + sol[piston.coolant.Q_flow][1]0 atol=1e-6
Expand Down
10 changes: 5 additions & 5 deletions test/Thermal/thermal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using OrdinaryDiffEq: ReturnCode.Success

# Check if Relative temperature sensor reads the temperature of heat capacitor
# when connected to a thermal conductor and a fixed temperature source
@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[reltem_sensor.T] + sol[tem_src.port.T] == sol[mass1.T] + sol[th_conductor.dT]

@info "Building a two-body system..."
Expand All @@ -49,7 +49,7 @@ using OrdinaryDiffEq: ReturnCode.Success
prob = ODEProblem(sys, u0, (0, 3.0))
sol = solve(prob, Tsit5())

@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
m1, m2 = sol.u[end]
@test m1m2 atol=1e-1
mass_T = reduce(hcat, sol.u)
Expand Down Expand Up @@ -85,7 +85,7 @@ end
prob = ODEProblem(sys, u0, (0, 3.0))
sol = solve(prob, Tsit5())

@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[th_conductor.dT] .* G == sol[th_conductor.Q_flow]
@test sol[th_conductor.Q_flow] sol[hf_sensor1.Q_flow] + sol[flow_src.port.Q_flow]

Expand Down Expand Up @@ -127,7 +127,7 @@ end
prob = ODEProblem(sys, u0, (0, 3.0))
sol = solve(prob, Rodas4())

@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[dissipator.dT] == sol[radiator.port_a.T] - sol[radiator.port_b.T]
rad_Q_flow = G * σ * (T_gas^4 - T_coolant^4)
@test sol[radiator.Q_flow] == fill(rad_Q_flow, length(sol[radiator.Q_flow]))
Expand Down Expand Up @@ -158,7 +158,7 @@ end
prob = ODEProblem(sys, u0, (0, 3.0))
sol = solve(prob, Rodas4())

@test sol.retcode == Success
@test SciMLBase.successful_retcode(sol)
@test sol[collector.port_b.Q_flow] + sol[collector.port_a1.Q_flow] +
sol[collector.port_a2.Q_flow] ==
zeros(length(sol[collector.port_b.Q_flow]))
Expand Down

0 comments on commit fb9e3f2

Please sign in to comment.