From b79d83bf695789ebba17430b7ab94b0d7fbed1c3 Mon Sep 17 00:00:00 2001 From: Valentin Kaisermayer Date: Fri, 19 Aug 2022 16:41:30 +0200 Subject: [PATCH 1/2] fixes demo and add it to tests --- docs/src/tutorials/custom_component.md | 2 +- test/chua_circuit.jl | 55 ++++++++++++++++++++++++++ test/runtests.jl | 1 + 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 test/chua_circuit.jl diff --git a/docs/src/tutorials/custom_component.md b/docs/src/tutorials/custom_component.md index a6fea63d9..a22cd161f 100644 --- a/docs/src/tutorials/custom_component.md +++ b/docs/src/tutorials/custom_component.md @@ -99,7 +99,7 @@ connections = [ connect(Ro.n, Gnd.g) ] -@named model = ODESystem(connections, t, systems=[L, Ro, G, C1, C2, Nr]) +@named model = ODESystem(connections, t, systems=[L, Ro, G, C1, C2, Nr, Gnd]) nothing # hide ``` diff --git a/test/chua_circuit.jl b/test/chua_circuit.jl new file mode 100644 index 000000000..d9588a771 --- /dev/null +++ b/test/chua_circuit.jl @@ -0,0 +1,55 @@ +using ModelingToolkit +using ModelingToolkitStandardLibrary.Electrical +using ModelingToolkitStandardLibrary.Electrical: OnePort +using OrdinaryDiffEq +using IfElse: ifelse + +@testset "Chua Circuit" begin + @parameters t + + function NonlinearResistor(;name, Ga, Gb, Ve) + @named oneport = OnePort() + @unpack v, i = oneport + pars = @parameters Ga=Ga Gb=Gb Ve=Ve + eqs = [ + i ~ ifelse(v < -Ve, + Gb*(v + Ve) - Ga*Ve, + ifelse(v > Ve, + Gb*(v - Ve) + Ga*Ve, + Ga*v, + ), + ) + ] + extend(ODESystem(eqs, t, [], pars; name=name), oneport) + end + + @named L = Inductor(L=18) + @named Ro = Resistor(R=12.5e-3) + @named G = Conductor(G=0.565) + @named C1 = Capacitor(C=10, v_start=4) + @named C2 = Capacitor(C=100) + @named Nr = NonlinearResistor( + Ga = -0.757576, + Gb = -0.409091, + Ve=1) + @named Gnd = Ground() + + connections = [ + connect(L.p, G.p) + connect(G.n, Nr.p) + connect(Nr.n, Gnd.g) + connect(C1.p, G.n) + connect(L.n, Ro.p) + connect(G.p, C2.p) + connect(C1.n, Gnd.g) + connect(C2.n, Gnd.g) + connect(Ro.n, Gnd.g) + ] + + @named model = ODESystem(connections, t, systems=[L, Ro, G, C1, C2, Nr, Gnd]) + sys = structural_simplify(model) + prob = ODEProblem(sys, Pair[], (0, 5e4), saveat=0.01) + sol = solve(prob, Rodas4()) + + @test sol.retcode == :Success +end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index f606e70f1..7ecd62366 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -10,6 +10,7 @@ using SafeTestsets @safetestset "Analog Circuits" begin include("Electrical/analog.jl") end #@safetestset "Digital Circuits" begin include("Electrical/digital.jl") end @safetestset "RC Circuit Demo" begin include("demo.jl") end +@safetestset "Chua Circuit Demo" begin include("chua_circuit.jl") end # Thermal @safetestset "Thermal Circuits" begin include("Thermal/thermal.jl") end From 94f9c389929d702bf74a5136faad6d1350de0f7c Mon Sep 17 00:00:00 2001 From: Valentin Kaisermayer Date: Fri, 19 Aug 2022 17:15:44 +0200 Subject: [PATCH 2/2] auto formatting --- test/Mechanical/rotational.jl | 3 +- test/chua_circuit.jl | 61 ++++++++++++++++------------------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/test/Mechanical/rotational.jl b/test/Mechanical/rotational.jl index fa2e19816..bf152c635 100644 --- a/test/Mechanical/rotational.jl +++ b/test/Mechanical/rotational.jl @@ -60,7 +60,8 @@ end sine, ]) sys = structural_simplify(model) - prob = DAEProblem(sys, D.(states(sys)) .=> 0.0, [D(D(inertia2.phi)) => 1.0; D.(states(model)) .=> 0.0], (0, 10.0)) + prob = DAEProblem(sys, D.(states(sys)) .=> 0.0, + [D(D(inertia2.phi)) => 1.0; D.(states(model)) .=> 0.0], (0, 10.0)) sol = solve(prob, DFBDF()) # Plots.plot(sol; vars=[inertia1.w, -inertia2.w*2]) diff --git a/test/chua_circuit.jl b/test/chua_circuit.jl index d9588a771..5f5a0d658 100644 --- a/test/chua_circuit.jl +++ b/test/chua_circuit.jl @@ -3,53 +3,48 @@ using ModelingToolkitStandardLibrary.Electrical using ModelingToolkitStandardLibrary.Electrical: OnePort using OrdinaryDiffEq using IfElse: ifelse - + @testset "Chua Circuit" begin @parameters t - function NonlinearResistor(;name, Ga, Gb, Ve) + function NonlinearResistor(; name, Ga, Gb, Ve) @named oneport = OnePort() @unpack v, i = oneport pars = @parameters Ga=Ga Gb=Gb Ve=Ve eqs = [ - i ~ ifelse(v < -Ve, - Gb*(v + Ve) - Ga*Ve, - ifelse(v > Ve, - Gb*(v - Ve) + Ga*Ve, - Ga*v, - ), - ) + i ~ ifelse(v < -Ve, + Gb * (v + Ve) - Ga * Ve, + ifelse(v > Ve, + Gb * (v - Ve) + Ga * Ve, + Ga * v)), ] - extend(ODESystem(eqs, t, [], pars; name=name), oneport) + extend(ODESystem(eqs, t, [], pars; name = name), oneport) end - @named L = Inductor(L=18) - @named Ro = Resistor(R=12.5e-3) - @named G = Conductor(G=0.565) - @named C1 = Capacitor(C=10, v_start=4) - @named C2 = Capacitor(C=100) - @named Nr = NonlinearResistor( - Ga = -0.757576, - Gb = -0.409091, - Ve=1) + @named L = Inductor(L = 18) + @named Ro = Resistor(R = 12.5e-3) + @named G = Conductor(G = 0.565) + @named C1 = Capacitor(C = 10, v_start = 4) + @named C2 = Capacitor(C = 100) + @named Nr = NonlinearResistor(Ga = -0.757576, + Gb = -0.409091, + Ve = 1) @named Gnd = Ground() - connections = [ - connect(L.p, G.p) - connect(G.n, Nr.p) - connect(Nr.n, Gnd.g) - connect(C1.p, G.n) - connect(L.n, Ro.p) - connect(G.p, C2.p) - connect(C1.n, Gnd.g) - connect(C2.n, Gnd.g) - connect(Ro.n, Gnd.g) - ] + connections = [connect(L.p, G.p) + connect(G.n, Nr.p) + connect(Nr.n, Gnd.g) + connect(C1.p, G.n) + connect(L.n, Ro.p) + connect(G.p, C2.p) + connect(C1.n, Gnd.g) + connect(C2.n, Gnd.g) + connect(Ro.n, Gnd.g)] - @named model = ODESystem(connections, t, systems=[L, Ro, G, C1, C2, Nr, Gnd]) + @named model = ODESystem(connections, t, systems = [L, Ro, G, C1, C2, Nr, Gnd]) sys = structural_simplify(model) - prob = ODEProblem(sys, Pair[], (0, 5e4), saveat=0.01) + prob = ODEProblem(sys, Pair[], (0, 5e4), saveat = 0.01) sol = solve(prob, Rodas4()) @test sol.retcode == :Success -end \ No newline at end of file +end