diff --git a/Project.toml b/Project.toml index 8125aadd2..8d91fe245 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelingToolkitStandardLibrary" uuid = "16a59e39-deab-5bd0-87e4-056b12336739" authors = ["Chris Rackauckas and Julia Computing"] -version = "1.3.1" +version = "1.4.0" [deps] IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173" diff --git a/src/Electrical/Analog/sources.jl b/src/Electrical/Analog/sources.jl index c9901d7dc..09318f3bc 100644 --- a/src/Electrical/Analog/sources.jl +++ b/src/Electrical/Analog/sources.jl @@ -1,744 +1,33 @@ -# Define and register smooth functions -_cos_wave(t, f, A, st, ϕ) = A*cos(2*π*f*(t - st) + ϕ) -_sin_wave(t, f, A, st, ϕ) = A*sin(2*π*f*(t - st) + ϕ) -_damped_sine_wave(t, f, A, st, ϕ, d) = exp((st-t)*d)*A*sin(2*π*f*(t-st) + ϕ) -_ramp(t, δ, st, et, h) = h/(et-st)*(_xH(t, δ, st) - _xH(t, δ, et)) -_square_wave(t, δ, f, A, st) = A*2atan(sin(2π*(t-st)*f)/δ)/π -_step(t, δ, h, a) = h*(atan((t-a)/δ)/π + 0.5) -_triangular_wave(t, δ, f, A, st) = A*(1-2acos((1 - δ)sin(2π*(t-st)*f))/π) -_xH(t, δ, tₒ) = (t-tₒ)*(1+((t-tₒ)/sqrt((t-tₒ)^2+δ^2)))/2 - -@register_symbolic _cos_wave(t, f, A, st, ϕ) -@register_symbolic _sin_wave(t, f, A, st, ϕ) -@register_symbolic _damped_sine_wave(t, f, A, st, ϕ, damping) -@register_symbolic _ramp(t, δ, st, et, h) -@register_symbolic _square_wave(t, δ, f, A, st) -@register_symbolic _step(t, δ, h, a) -@register_symbolic _triangular_wave(t, δ, f, A, st) - -""" -```julia -function ConstantVoltage(; name, V) -``` - -The source for an ideal constant voltage. - -# States -- `v(t)`: [`V`] - The voltage across this source, given by `p.v - n.v` and is always constant - -# Connectors -- `p` - Positive pin -- `n` - Negative pin - -# Parameters: -- `V`: [`V`] - The constant voltage across the terminals of this source -""" -function ConstantVoltage(;name, V) - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters V=V - eqs = [ - v ~ V - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -""" -```julia -function CosineVoltage(; name, offset=0.0, amplitude=1.0, frequency=1.0, starttime=0.0, phase=0.0) -``` - -A source in which the voltage across its terminals is a cosine function of time. - - -# States -- `v(t)`: [`V`] - The voltage across this source, given by `p.v - n.v` - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Observables -- `offset`: [`V`] - A constant offset added to the voltage output -- `amplitude`: [`V`] - The amplitude of the cosine function -- `frequency`: [`Hz`] - The frequency of the cosine function -- `starttime`: [`s`] - The time at which the source starts functioning. Before this time, the voltage across - its terminals is 0. -- `phase`: [`rad`] - The phase offset of the cosine function -""" -function CosineVoltage(;name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0, phase=0.0) - δ = 0.00001 - - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters begin - offset=offset - amplitude=amplitude - frequency=frequency - start_time=start_time - phase=phase - end - eqs = [ - v ~ _cos_wave(t, frequency, amplitude, start_time, phase) * _step(t, δ, 1.0, start_time) + offset - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -""" -```julia -function ExpSineVoltage(; name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0, phase=0.0, damping=0.0) -``` - -A source in which the voltage across its terminals is a damped sine function of time. - -# States -- `v(t)`: [`V`] - The voltage across this source, given by `p.v - n.v` - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Parameters -- `offset`: [`V`] - A constant offset added to the voltage output -- `amplitude`: [`V`] - The amplitude of the damped sine function -- `frequency`: [`Hz`] - The frequency of the damped sine function -- `start_time`: [`s`] - The time at which the source starts functioning. Before this time, the voltage across - its terminals is `offset`. -- `phase`: [`rad`] - The phase offset of the damped sine function -- `damping_coef`: [`1/s`] - Damping coefficient of the damped sine function -""" -function ExpSineVoltage(;name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0, phase=0.0, damping=0.0) - δ = 0.00001 - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters begin - offset=offset - amplitude=amplitude - frequency=frequency - start_time=start_time - phase=phase - damping=damping - end - eqs = [ - v ~ _damped_sine_wave(t, frequency, amplitude, start_time, phase, damping) * _step(t, δ, 1.0, start_time) - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -""" -```julia -function RampVoltage(; name, offset=0.0, start_time=0.0, duration=1.0, height=1.0) -``` - -A source in which the voltage across grows linearly from `offset` to `offset+height` over -the time interval `duration` starting at `start_time` - -# States -- `v(t)`: [`V`] - The voltage across this source, given by `p.v - n.v` - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Parameters -- `offset`: [`V`] - A constant offset added to the voltage output -- `start_time`: [`s`] - The time at which the voltage starts growing -- `duration`: [`s`] - The duration of the ramp (`0.0` gives a step) -- `height`: [`V`] - The amount that the voltage grows in the time interval -""" -function RampVoltage(;name, offset=0.0, start_time=0.0, duration=1.0, height=1.0) - δ = 0.00001 - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters begin - offset=offset - height=height - start_time=start_time - duration=duration - end - eqs = [ - v ~ _ramp(t, δ, start_time, start_time + duration, height) + offset - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -""" -```julia -function SineVoltage(; name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0, phase=0.0) -``` - -A source in which the voltage across its terminals is a sine function of time. - - - -# States -- `v(t)`: [`V`] - The voltage across this source, given by `p.v - n.v` - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Parameters -- `offset`: [`V`] - A constant offset added to the voltage output -- `amplitude`: [`V`] - The amplitude of the sine function -- `frequency`: [`Hz`] - The frequency of the sine function -- `start_time`: [`s`] - The time at which the source starts functioning. Before this time, the voltage across - its terminals is `offset`. -- `phase`: [`rad`] - The phase offset of the sine function -""" -function SineVoltage(;name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0, phase=0.0) - δ = 0.00001 - - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters begin - offset=offset - amplitude=amplitude - frequency=frequency - start_time=start_time - phase=phase - end - eqs = [ - v ~ _sin_wave(t, frequency, amplitude, start_time, phase) * _step(t, δ, 1.0, start_time) + offset - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -""" -```julia -function SquareVoltage(; name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0) -``` - -A source in which the voltage across its terminals is a square function of time. - -# States -- `v(t)`: [`V`] - The voltage across this source, given by `p.v - n.v` - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Parameters -- `offset`: [`V`] - A constant offset added to the voltage output -- `amplitude`: [`V`] - The amplitude of the square wave function -- `frequency`: [`Hz`] - The frequency of the square wave function -- `start_time`: [`s`] - The time at which the source starts functioning. Before this time, the voltage across - its terminals is `offset`. -""" -function SquareVoltage(; name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0) - δ = 0.0001 - - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters begin - offset=offset - amplitude=amplitude - start_time=start_time - end - eqs = [ - v ~ _square_wave(t, δ, frequency, amplitude, start_time) * _step(t, δ, 1.0, start_time) + offset - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -""" -```julia -function StepVoltage(; name, offset=0.0, start_time=0.0, height=1.0) -``` - -A source in which the voltage across its terminals increases from `offset` to `offset+height` at -`start_time` - -# States -- `v(t)`: [`V`] - The voltage across this source, given by `p.v - n.v` - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Observables -- `offset`: [`V`] - A constant offset added to the voltage output -- `start_time`: [`s`] - The time at which the source starts functioning, and the voltage jumps -- `height`: [`V`] - Magnitude of increase in voltage -""" -function StepVoltage(;name, offset=0.0, start_time=0.0, height=1.0) - δ = 0.0001 - - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters begin - offset=offset - height=height - start_time=start_time - end - eqs = [ - v ~ _step(t, δ, height, start_time) + offset - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -""" -```julia -function TriangularVoltage(; name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0) -``` - -A source in which the voltage across its terminals is a triangular function of time. - -# States -- `v(t)`: [`V`] - The voltage across this source, given by `p.v - n.v` - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Observables -- `offset`: [`V`] - A constant offset added to the voltage output -- `amplitude`: [`V`] - Amplitude of the triangular wave function -- `frequency`: [`Hz`] - Frequency of the triangular wave function -- `start_time`: [`s`] - The time at which the source starts functioning. Before this, the output of the source is - `offset` -""" -function TriangularVoltage(; name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0) - δ = 0.00001 - - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters begin - offset=offset - amplitude=amplitude - frequency=frequency - start_time=start_time - end - eqs = [ - v ~ _triangular_wave(t, δ, frequency, amplitude, start_time) * _step(t, δ, 1.0, start_time) + offset - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -# Current Sources ###################################################################################################### -""" -```julia -function ConstantCurrent(; name, I = 1.0) -``` - -The source for an ideal constant current. - -# States -- `i(t)`: [`A`] - The current through this source, which is always constant - -# Connectors -- `p` - Positive pin -- `n` - Negative pin - -# Parameters: -- `I`: [`A`] - The constant current through the terminals of this source -""" -function ConstantCurrent(;name, I = 1.0) - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters I=I - eqs = [ - i ~ I - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -""" -```julia -function CosineCurrent(; name, offset=0.0, amplitude=1.0, frequency=1.0, starttime=0.0, phase=0.0) -``` - -A source in which the current through its terminals is a cosine function of time. - - -# States -- `i(t)`: [`A`] - The current through this source - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Observables -- `offset`: [`A`] - A constant offset added to the current output -- `amplitude`: [`A`] - The amplitude of the cosine function -- `frequency`: [`Hz`] - The frequency of the cosine function -- `starttime`: [`s`] - The time at which the source starts functioning. Before this time, the current through - its terminals is 0. -- `phase`: [`rad`] - The phase offset of the cosine function -""" -function CosineCurrent(;name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0, phase=0.0) - δ = 0.00001 - - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters begin - offset=offset - amplitude=amplitude - frequency=frequency - start_time=start_time - phase=phase - end - eqs = [ - i ~ _cos_wave(t, frequency, amplitude, start_time, phase) * _step(t, δ, 1.0, start_time) + offset - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -""" -```julia -function ExpSineCurrent(; name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0, phase=0.0, damping=0.0) -``` - -A source in which the current through its terminals is a damped sine function of time. - -# States -- `i(t)`: [`A`] - The current through this source - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Parameters -- `offset`: [`A`] - A constant offset added to the current output -- `amplitude`: [`A`] - The amplitude of the damped sine function -- `frequency`: [`Hz`] - The frequency of the damped sine function -- `start_time`: [`s`] - The time at which the source starts functioning. Before this time, the current through - its terminals is `offset`. -- `phase`: [`rad`] - The phase offset of the damped sine function -- `damping_coef`: [`1/s`] - Damping coefficient of the damped sine function -""" -function ExpSineCurrent(;name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0, phase=0.0, damping=0.0) - δ = 0.00001 - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters begin - offset=offset - amplitude=amplitude - frequency=frequency - start_time=start_time - phase=phase - damping=damping - end - eqs = [ - i ~ _damped_sine_wave(t, frequency, amplitude, start_time, phase, damping) * _step(t, δ, 1.0, start_time) - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -""" -```julia -function RampCurrent(; name, offset=0.0, start_time=0.0, duration=1.0, height=1.0) -``` - -A source in which the current grows linearly from `offset` to `offset+height` over -the time interval `duration` starting at `start_time` - -# States -- `i(t)`: [`A`] - The current through this source - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Parameters -- `offset`: [`A`] - A constant offset added to the current output -- `start_time`: [`s`] - The time at which the current starts growing -- `duration`: [`s`] - The duration of the ramp (`0.0` gives a step) -- `height`: [`A`] - The amount that the current grows in the time interval -""" -function RampCurrent(;name, offset=0.0, start_time=0.0, duration=1.0, height=1.0) - δ = 0.00001 - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters begin - offset=offset - height=height - start_time=start_time - duration=duration - end - eqs = [ - i ~ _ramp(t, δ, start_time, start_time + duration, height) + offset - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - """ -```julia -function SineCurrent(; name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0, phase=0.0) -``` - -A source in which the current through its terminals is a sine function of time. - + Voltage(;name) +Acts as voltage signal. -# States -- `i(t)`: [`A`] - The current through this source - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Parameters -- `offset`: [`A`] - A constant offset added to the current output -- `amplitude`: [`V`] - The amplitude of the sine function -- `frequency`: [`Hz`] - The frequency of the sine function -- `start_time`: [`s`] - The time at which the source starts functioning. Before this time, the current through - its terminals is `offset`. -- `phase`: [`rad`] - The phase offset of the sine function """ -function SineCurrent(;name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0, phase=0.0) - δ = 0.00001 - +function Voltage(;name) @named oneport = OnePort() @unpack v, i = oneport - pars = @parameters begin - offset=offset - amplitude=amplitude - frequency=frequency - start_time=start_time - phase=phase - end + @named V = RealInput() eqs = [ - i ~ _sin_wave(t, frequency, amplitude, start_time, phase) * _step(t, δ, 1.0, start_time) + offset + v ~ V.u ] - extend(ODESystem(eqs, t, [], pars; name=name), oneport) + extend(ODESystem(eqs, t, [], []; name=name, systems=[V]), oneport) end """ -```julia -function SquareCurrent(; name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0) -``` + Current(;name) -A source in which the current through its terminals is a square function of time. +Acts as current signal. -# States -- `i(t)`: [`A`] - The current through this source - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Parameters -- `offset`: [`A`] - A constant offset added to the current output -- `amplitude`: [`A`] - The amplitude of the square wave function -- `frequency`: [`Hz`] - The frequency of the square wave function -- `start_time`: [`s`] - The time at which the source starts functioning. Before this time, the current through - its terminals is `offset`. """ -function SquareCurrent(; name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0) - δ = 0.0001 - - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters begin - offset=offset - amplitude=amplitude - start_time=start_time - end - eqs = [ - i ~ _square_wave(t, δ, frequency, amplitude, start_time) * _step(t, δ, 1.0, start_time) + offset - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -""" -```julia -function StepCurrent(; name, offset=0.0, start_time=0.0, height=1.0) -``` - -A source in which the current through its terminals increases from `offset` to `offset+height` at -`start_time` - -# States -- `i(t)`: [`A`] - The current through this source - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Observables -- `offset`: [`A`] - A constant offset added to the current output -- `start_time`: [`s`] - The time at which the source starts functioning, and the current jumps -- `height`: [`A`] - Magnitude of increase in current -""" -function StepCurrent(;name, offset=0.0, start_time=0.0, height=1.0) - δ = 0.0001 - - @named oneport = OnePort() - @unpack v, i = oneport - pars = @parameters begin - offset=offset - height=height - start_time=start_time - end - eqs = [ - i ~ _step(t, δ, height, start_time) + offset - ] - - extend(ODESystem(eqs, t, [], pars; name=name), oneport) -end - -""" -```julia -function TriangularCurrent(; name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0) -``` - -A source in which the current through its terminals is a triangular function of time. - -# States -- `i(t)`: [`A`] - The current through this source - -# Connectors -- `p` - Positive port -- `n` - Negative port - -# Observables -- `offset`: [`A`] - A constant offset added to the current output -- `amplitude`: [`A`] - Amplitude of the triangular wave function -- `frequency`: [`Hz`] - Frequency of the triangular wave function -- `start_time`: [`s`] - The time at which the source starts functioning. Before this, the output of the source is - `offset` -""" -function TriangularCurrent(; name, offset=0.0, amplitude=1.0, frequency=1.0, start_time=0.0) - δ = 0.00001 - +function Current(;name) @named oneport = OnePort() @unpack v, i = oneport - pars = @parameters begin - offset=offset - amplitude=amplitude - frequency=frequency - start_time=start_time - end + @named I = RealInput() eqs = [ - i ~ _triangular_wave(t, δ, frequency, amplitude, start_time) * _step(t, δ, 1.0, start_time) + offset + i ~ I.u ] - extend(ODESystem(eqs, t, [], pars; name=name), oneport) + extend(ODESystem(eqs, t, [], []; name=name, systems=[I]), oneport) end diff --git a/src/Electrical/Electrical.jl b/src/Electrical/Electrical.jl index 08f6c056b..9cb25f8e8 100644 --- a/src/Electrical/Electrical.jl +++ b/src/Electrical/Electrical.jl @@ -10,6 +10,8 @@ using OffsetArrays @parameters t D = Differential(t) +using ..Blocks: RealInput, RealOutput + include("utils.jl") include("Analog/ideal_components.jl") include("Analog/sensors.jl") @@ -19,7 +21,7 @@ include("Analog/sources.jl") # include("Digital/tables.jl") # include("Digital/sources.jl") -# TODO: +# TODO: # - digital # - machines # - multi-phase @@ -32,14 +34,9 @@ export #Interface # Analog Sensors CurrentSensor, PotentialSensor, VoltageSensor, PowerSensor, MultiSensor, - #Analog Sources - ConstantVoltage, SineVoltage, StepVoltage, RampVoltage, - SquareVoltage, TriangularVoltage, - CosineVoltage, ExpSineVoltage, - ConstantCurrent, SineCurrent, StepCurrent, RampCurrent, - SquareCurrent, TriangularCurrent, - CosineCurrent, ExpSineCurrent - + # Analog Sources + Voltage, Current + # # Digital Gates # And, Or, Not, Xor, Nand, Nor, Xnor, diff --git a/test/Electrical/analog.jl b/test/Electrical/analog.jl index fc7184d72..92dff0db4 100644 --- a/test/Electrical/analog.jl +++ b/test/Electrical/analog.jl @@ -1,12 +1,13 @@ using ModelingToolkitStandardLibrary.Electrical, ModelingToolkit, OrdinaryDiffEq, Test -using ModelingToolkitStandardLibrary.Electrical: _step, _square_wave, _triangular_wave, _cos_wave, _damped_sine_wave, _ramp - +using ModelingToolkitStandardLibrary.Blocks: Step, Constant, Sine, Cosine, ExpSine, Ramp, Square, Triangular +using ModelingToolkitStandardLibrary.Blocks: square, triangular # using Plots @parameters t @testset "sensors" begin - @named source = ConstantVoltage(V=10) + @named source = Sine(offset=1, amplitude=10, frequency=5) + @named voltage = Voltage() @named resistor = Resistor(R=1) @named capacitor = Capacitor(C=1) @named ground = Ground() @@ -16,18 +17,19 @@ using ModelingToolkitStandardLibrary.Electrical: _step, _square_wave, _triangula @named power_sensor = PowerSensor() connections = [ - connect(source.p, resistor.p) + connect(source.output, voltage.V) + connect(voltage.p, resistor.p) connect(resistor.n, current_sensor.p) connect(current_sensor.n, power_sensor.pc) connect(power_sensor.nc, capacitor.p) - connect(capacitor.n, source.n, ground.g) + connect(capacitor.n, voltage.n, ground.g) connect(capacitor.p, voltage_sensor.p) connect(capacitor.n, voltage_sensor.n) connect(capacitor.p, power_sensor.pv) connect(capacitor.n, power_sensor.nv) ] - @named model = ODESystem(connections, t; systems=[resistor, capacitor, source, ground, voltage_sensor, current_sensor, power_sensor]) + @named model = ODESystem(connections, t; systems=[resistor, capacitor, source, voltage, ground, voltage_sensor, current_sensor, power_sensor]) sys = structural_simplify(model) prob = ODAEProblem(sys, Pair[], (0.0, 10.0)) sol = solve(prob, Tsit5()) @@ -42,18 +44,20 @@ end # simple voltage divider @testset "voltage divider" begin - @named source = ConstantVoltage(V=10) + @named source = Constant(k=10) + @named voltage = Voltage() @named R1 = Resistor(R=1e3) @named R2 = Resistor(R=1e3) @named ground = Ground() connections = [ - connect(source.p, R1.p) + connect(source.output, voltage.V) + connect(voltage.p, R1.p) connect(R1.n, R2.p) - connect(R2.n, source.n, ground.g) + connect(R2.n, voltage.n, ground.g) ] - @named model = ODESystem(connections, t, systems=[R1, R2, source, ground]) + @named model = ODESystem(connections, t, systems=[R1, R2, source,voltage, ground]) sys = structural_simplify(model) prob = ODEProblem(sys, Pair[], (0, 2.0)) sol = solve(prob, Rodas4()) # has no state; does not work with Tsit5 @@ -64,18 +68,20 @@ end # simple RC @testset "RC" begin - @named source = ConstantVoltage(V=10) + @named source = Constant(k=10) + @named voltage = Voltage() @named resistor = Resistor(R=1) @named capacitor = Capacitor(C=1) @named ground = Ground() connections = [ - connect(source.p, resistor.p) + connect(source.output, voltage.V) + connect(voltage.p, resistor.p) connect(resistor.n, capacitor.p) - connect(capacitor.n, source.n, ground.g) + connect(capacitor.n, voltage.n, ground.g) ] - @named model = ODESystem(connections, t; systems=[resistor, capacitor, source, ground]) + @named model = ODESystem(connections, t; systems=[resistor, capacitor, source, voltage, ground]) sys = structural_simplify(model) prob = ODAEProblem(sys, [capacitor.v => 0.0], (0.0, 10.0)) sol = solve(prob, Tsit5()) @@ -86,18 +92,20 @@ end # simple RL @testset "RL" begin - @named source = ConstantVoltage(V=10) + @named source = Constant(k=10) + @named voltage = Voltage() @named resistor = Resistor(R=1) @named inductor = Inductor(L=1.0) @named ground = Ground() connections = [ - connect(source.p, resistor.p) + connect(source.output, voltage.V) + connect(voltage.p, resistor.p) connect(resistor.n, inductor.p) - connect(inductor.n, source.n, ground.g) + connect(inductor.n, voltage.n, ground.g) ] - @named model = ODESystem(connections, t; systems=[resistor, inductor, source, ground]) + @named model = ODESystem(connections, t; systems=[resistor, inductor, source, voltage, ground]) sys = structural_simplify(model) prob = ODAEProblem(sys, [inductor.i => 0.0], (0.0, 10.0)) sol = solve(prob, Tsit5()) @@ -106,97 +114,63 @@ end @test sol[inductor.i][end] ≈ 10 atol=1e-3 end -# RC with different voltage sources @testset "RC with voltage sources" begin - @named source_const = ConstantVoltage(V=10) - @named source_sin = SineVoltage(offset=1, amplitude=10, frequency=2, start_time=0.5, phase=0) - @named source_step = StepVoltage(offset=1, height=10, start_time=0.5) - @named source_tri = TriangularVoltage(offset=1, start_time=0.5, amplitude=10, frequency=2) - @named source_dsin = ExpSineVoltage(offset=1, amplitude=10, frequency=2, start_time=0.5, phase=0, damping=0.5) - @named source_ramp = RampVoltage(offset=1, height=10, start_time=0.5, duration=1) + R, C = 1, 1 + @named voltage = Voltage() + @named source_const = Constant(k=10) + @named source_sin = Sine(offset=1, amplitude=10, frequency=2, start_time=0.5, phase=0) + @named source_step = Step(offset=1, height=10, start_time=0.5) + @named source_tri = Triangular(offset=1, start_time=0.5, amplitude=10, frequency=2) + @named source_dsin = ExpSine(offset=1, amplitude=10, frequency=2, start_time=0.5, phase=0, damping=0.5) + @named source_ramp = Ramp(offset=1, height=10, start_time=0.5, duration=1) sources = [source_const, source_sin, source_step, source_tri, source_dsin, source_ramp] - @named resistor = Resistor(R=1) - @named capacitor = Capacitor(C=1) + @named resistor = Resistor(; R) + @named capacitor = Capacitor(; C) @named ground = Ground() for source in sources connections = [ - connect(source.p, resistor.p) + connect(source.output, voltage.V) + connect(voltage.p, resistor.p) connect(resistor.n, capacitor.p) - connect(capacitor.n, source.n, ground.g) + connect(capacitor.n, voltage.n, ground.g) ] - @named model = ODESystem(connections, t; systems=[resistor, capacitor, source, ground]) + @named model = ODESystem(connections, t; systems=[resistor, capacitor, source, ground, voltage]) sys = structural_simplify(model) - prob = ODAEProblem(sys, [capacitor.v => 0.0], (0.0, 10.0)) + prob = ODAEProblem(sys, [capacitor.v => 10.0], (0.0, 10.0)) + @test_nowarn sol = solve(prob, Rodas5()) @test_nowarn sol = solve(prob, Tsit5()) - @test_nowarn sol = solve(prob, Rodas4()) - # Plots.plot(sol; vars=[source.v, capacitor.v]) + # Plots.plot(sol; vars=[voltage.v, capacitor.v]) end end -# RL with different voltage sources -@testset "RL with voltage sources" begin - @named source_const = ConstantVoltage(V=10) - @named source_sin = SineVoltage(offset=1, amplitude=10, frequency=2, start_time=0.5, phase=0) - @named source_step = StepVoltage(offset=1, height=10, start_time=0.5) - @named source_tri = TriangularVoltage(offset=1, start_time=0.5, amplitude=10, frequency=2) - @named source_dsin = ExpSineVoltage(offset=1, amplitude=10, frequency=2, start_time=0.5, phase=0, damping=0.5) - @named source_ramp = RampVoltage(offset=1, height=10, start_time=0.5, duration=1) - sources = [source_const, source_sin, source_step, source_tri, source_dsin, source_ramp] - - @named resistor = Resistor(R=1.0) - @named inductor = Inductor(L=1.0) - @named ground = Ground() - - for source in sources - connections = [ - connect(source.p, resistor.p) - connect(resistor.n, inductor.p) - connect(inductor.n, source.n, ground.g) - ] - @named model = ODESystem(connections, t; systems=[resistor, inductor, source, ground]) - sys = structural_simplify(model) - prob = ODAEProblem(sys, [inductor.i => 0.0], (0.0, 10.0)) - @test_nowarn sol = solve(prob, Tsit5()) - @test_nowarn sol = solve(prob, Rodas4()) - - # Plots.plot(sol; vars=[source.i, inductor.i]) - end -end - -# RC with different current sources +# RC with current sources @testset "RC with current sources" begin - @named source_const = ConstantCurrent(I=10) - @named source_sin = SineCurrent(offset=1, amplitude=10, frequency=2, start_time=0.5, phase=0) - @named source_step = StepCurrent(offset=1, height=10, start_time=0.5) - @named source_tri = TriangularCurrent(offset=1, start_time=0.5, amplitude=10, frequency=2) - @named source_dsin = ExpSineCurrent(offset=1, amplitude=10, frequency=2, start_time=0.5, phase=0, damping=0.5) - @named source_ramp = RampCurrent(offset=1, height=10, start_time=0.5, duration=1) - sources = [source_const, source_sin, source_step, source_tri, source_dsin, source_ramp] - + start_time = 2 + @named current = Current() + @named source = Step(start_time=2) @named resistor = Resistor(R=1) @named capacitor = Capacitor(C=1) @named ground = Ground() - for source in sources - connections = [ - connect(source.p, resistor.p) - connect(resistor.n, capacitor.p) - connect(capacitor.n, source.n, ground.g) - ] + connections = [ + connect(source.output, current.I) + connect(current.p, resistor.n) + connect(capacitor.n, resistor.p) + connect(capacitor.p, current.n, ground.g) + ] - @named model = ODESystem(connections, t; systems=[resistor, capacitor, source, ground]) - sys = structural_simplify(model) - prob = ODAEProblem(sys, [capacitor.v => 0.0], (0.0, 10.0)) - @test_nowarn sol = solve(prob, Tsit5()) - @test_broken sol = solve(prob, Rodas4()) + @named model = ODESystem(connections, t; systems=[ground, resistor, current, capacitor, source]) + sys = structural_simplify(model) + prob = ODAEProblem(sys, [capacitor.v => 0.0], (0.0, 10.0)) + sol = solve(prob, Tsit5()) + y(x, st) = (x .> st) .* abs.(collect(x) .- st) + @test sum(reduce(vcat, sol.u) .- y(sol.t, start_time)) ≈ 0 atol=1e-2 - # Plots.plot(sol; vars=[source.v, capacitor.v]) - end end @testset "Integrator" begin @@ -208,18 +182,20 @@ end @named R2 = Resistor(R=100*R) @named C1 = Capacitor(C=1/(2 * pi * f * R)) @named opamp = IdealOpAmp() - @named square = SquareVoltage(amplitude=Vin) + @named square = Square(amplitude=Vin) + @named voltage = Voltage() @named sensor = VoltageSensor() - + connections = [ - connect(square.p, R1.p) + connect(square.output, voltage.V) + connect(voltage.p, R1.p) connect(R1.n, C1.n, R2.p, opamp.n1) connect(opamp.p2, C1.p, R2.n) - connect(opamp.p1, ground.g, opamp.n2, square.n) + connect(opamp.p1, ground.g, opamp.n2, voltage.n) connect(opamp.p2, sensor.p) connect(sensor.n, ground.g) ] - @named model = ODESystem(connections, t, systems = [R1, R2, opamp, square, C1, ground, sensor]) + @named model = ODESystem(connections, t, systems = [R1, R2, opamp, square, voltage, C1, ground, sensor]) sys = structural_simplify(model) u0 = [ C1.v => 0.0 @@ -228,106 +204,119 @@ end prob = ODEProblem(sys, u0, (0, 100.0)) sol = solve(prob, Rodas4()) @test sol[opamp.v2] == sol[C1.v] # Not a great one however. Rely on the plot - @test sol[opamp.p2.v] == sol[sensor.v] + @test sol[opamp.p2.v] == sol[sensor.v] # plot(sol, vars=[sensor.v, square.v, C1.v]) end +_step(x, h, st) = ifelse(x < st, 0, h) +_cos_wave(x, f, A, st, ϕ) = A*cos(2*π*f*(x-st) + ϕ) +_ramp(x, st, d, h) = ifelse(x < st, 0, + ifelse(x < (st + d), (x - st) * h / d, h)) +_sine_wave(x, f, A, st, ϕ) = A*sin(2*π*f*(x - st) + ϕ) +_damped_sine_wave(x, f, A, st, ϕ, d) = exp((st-x)*d)*A*sin(2*π*f*(x-st) + ϕ) + @testset "Voltage function generators" begin - st, o, h, f, A, et, ϕ, d, δ = 0.7, 1.25, 3, 2, 2.5, 2.5, π/4, 0.1, 0.0001 + st, o, h, f, A, et, ϕ, d, δ = 0.7, 1.25, 3, 2, 2.5, 2, π/4, 0.1, 0.0001 @named res = Resistor(R=1) @named cap = Capacitor(C=1) @named ground = Ground() + @named voltage = Voltage() @named voltage_sensor = VoltageSensor() - @named vstep = StepVoltage(start_time=st, offset=o, height=h) - @named vsquare = SquareVoltage(offset=o, start_time=st, amplitude=A, frequency=f) - @named vtri = TriangularVoltage(offset=o, start_time=st, amplitude=A, frequency=f) - # @named vsawtooth = SawToothVoltage(amplitude=A, start_time=st, frequency=f, offset=o) - @named vcosine = CosineVoltage(offset=o, amplitude=A, frequency=f, start_time=st, phase=ϕ) - @named vdamped_sine = ExpSineVoltage(offset=o, amplitude=A, frequency=f, start_time=st, phase=ϕ, damping=d) - @named vramp = RampVoltage(offset=o, start_time=st, duration=et-st, height=h) - - vsources = [vtri, vsquare, vstep, vcosine, vdamped_sine, vramp] - waveforms(i, x) = getindex([o .+ (x .> st) .* _triangular_wave.(x, δ, f, A, st), - o .+ (x .> st) .* _square_wave.(x, δ, f, A, st), - o .+ _step.(x, δ, h, st), - # o .+ (x .> st). * _sawtooth_wave.(x, δ, f, A, st), + @named step = Step(start_time=st, offset=o, height=h) + @named cosine = Cosine(offset=o, amplitude=A, frequency=f, start_time=st, phase=ϕ) + @named sine = Sine(offset=o, amplitude=A, frequency=f, start_time=st, phase=ϕ) + @named damped_sine = ExpSine(offset=o, amplitude=A, frequency=f, start_time=st, phase=ϕ, damping=d) + @named ramp = Ramp(offset=o, start_time=st, duration=et-st, height=h) + @named vsquare = Square(offset=o, start_time=st, amplitude=A, frequency=f) + @named tri = Triangular(offset=o, start_time=st, amplitude=A, frequency=f) + # @named vsawtooth = SawTooth(amplitude=A, start_time=st, frequency=f, offset=o) + + sources = [step, cosine, sine, damped_sine, ramp, tri, vsquare] #, vsawtooth] + waveforms(i, x) = getindex([o .+ _step.(x, h, st), o .+ (x .> st) .* _cos_wave.(x, f, A, st, ϕ), + o .+ (x .> st) .* _sine_wave.(x, f, A, st, ϕ), o .+ (x .> st) .* _damped_sine_wave.(x, f, A, st, ϕ, d), - o .+ _ramp.(x, δ, st, et, h)], i) - for i in 1:length(vsources) - vsource = vsources[i] - # @info Symbolics.getname(vsource) + o .+ _ramp.(x, st, (et-st), h), + triangular.(x, f, A, o, st), + square.(x, f, A, o, st)], i) + # o .+ (x .> st). * _sawtooth_wave.(x, δ, f, A, st), + + for i in 1:length(sources) + source = sources[i] + @info "Testing Voltage with $(source.name) source" eqs = [ - connect(vsource.p, voltage_sensor.p, res.p) + connect(source.output, voltage.V) + connect(voltage.p, voltage_sensor.p, res.p) connect(res.n, cap.p) - connect(ground.g, voltage_sensor.n, vsource.n, cap.n) + connect(ground.g, voltage_sensor.n, voltage.n, cap.n) ] - @named vmodel = ODESystem(eqs, t, systems = [voltage_sensor, res, cap, vsource, ground]) + @named vmodel = ODESystem(eqs, t, systems = [voltage_sensor, res, cap, source, voltage, ground]) vsys = structural_simplify(vmodel) - u0 = [ - vsource.v => 1 - res.v => 1 - ] + u0 = [] prob = ODAEProblem(vsys, u0, (0, 10.0)) sol = solve(prob, dt=0.1, Tsit5()) - @test sol[vsource.v][1150:end] ≈ waveforms(i, sol.t)[1150:end] atol=1e-1 + @test sol[voltage.V.u] ≈ waveforms(i, sol.t) atol=1e-1 + @test sol[voltage.p.v] ≈ sol[voltage.V.u] # For visual inspection - # plt = plot(sol; vars=[vsource.v]) - # savefig(plt, "test_voltage_$(Symbolics.getname(vsource))") + # plt = plot(sol; vars=[voltage.v]) + # savefig(plt, "test_voltage_$(source.name)") end end @testset "Current function generators" begin - st, o, h, f, A, et, ϕ, d, δ = 0.7, 1.25, 3, 2, 2.5, 2.5, π/4, 0.1, 0.0001 + st, o, h, f, A, et, ϕ, d, δ = 0.7, 1.25, 3, 2, 2.5, 2, π/4, 0.1, 0.0001 @named ground = Ground() @named res = Resistor(R=1.0) @named cap = Capacitor(C=1) @named current_sensor = CurrentSensor() - @named istep = StepCurrent(start_time=st, offset=o, height=h) - @named isquare = SquareCurrent(offset=o, start_time=st, amplitude=A, frequency=f) - @named itri = TriangularCurrent(offset=o, start_time=st, amplitude=A, frequency=f) - # @named isawtooth = SawToothCurrent(amplitude=A, start_time=st, frequency=f, offset=o) - @named icosine = CosineCurrent(offset=o, amplitude=A, frequency=f, start_time=st, phase=ϕ) - @named idamped_sine = ExpSineCurrent(offset=o, amplitude=A, frequency=f, start_time=st, phase=ϕ, damping=d) - @named iramp = RampCurrent(offset=o, start_time=st, duration=et-st, height=h) - - isources = [itri, isquare, istep, icosine, idamped_sine, iramp] - waveforms(i, x) = getindex([o .+ (x .> st) .* _triangular_wave.(x, δ, f, A, st), - o .+ (x .> st) .* _square_wave.(x, δ, f, A, st), - o .+ _step.(x, δ, h, st), - # o .+ (x .> st). * _sawtooth_wave.(x, δ, f, A, st), + @named current = Current() + @named step = Step(start_time=st, offset=o, height=h) + @named cosine = Cosine(offset=o, amplitude=A, frequency=f, start_time=st, phase=ϕ) + @named sine = Sine(offset=o, amplitude=A, frequency=f, start_time=st, phase=ϕ) + @named damped_sine = ExpSine(offset=o, amplitude=A, frequency=f, start_time=st, phase=ϕ, damping=d) + @named ramp = Ramp(offset=o, start_time=st, duration=et-st, height=h) + @named vsquare = Square(offset=o, start_time=st, amplitude=A, frequency=f) + @named tri = Triangular(offset=o, start_time=st, amplitude=A, frequency=f) + # @named isawtooth = SawTooth(amplitude=A, start_time=st, frequency=f, offset=o) + + sources = [step, cosine, sine, damped_sine, ramp, tri, vsquare] #, idamped_sine] + waveforms(i, x) = getindex([o .+ _step.(x, h, st), o .+ (x .> st) .* _cos_wave.(x, f, A, st, ϕ), + o .+ (x .> st) .* _sine_wave.(x, f, A, st, ϕ), o .+ (x .> st) .* _damped_sine_wave.(x, f, A, st, ϕ, d), - o .+ _ramp.(x, δ, st, et, h)], i) - - for i in 1:length(isources) - isource = isources[i] + o .+ _ramp.(x, st, (et-st), h), + triangular.(x, f, A, o, st), + square.(x, f, A, o, st)], i) + # # o .+ (x .> st). * _sawtooth_wave.(x, δ, f, A, st) + + for i in 1:length(sources) + source = sources[i] + @info "Testing Current with $(source.name) source" eqs = [ - connect(isource.p, current_sensor.n) + connect(source.output, current.I) + connect(current.p, current_sensor.n) connect(current_sensor.p, res.p) connect(res.n, cap.p) - connect(isource.n, ground.g, cap.n) + connect(current.n, ground.g, cap.n) ] - @named model = ODESystem(eqs, t, systems = [current_sensor, isource, res, cap, ground]) + @named model = ODESystem(eqs, t, systems = [current_sensor, source, current, res, cap, ground]) isys = structural_simplify(model) - u0 = [ - isource.i => 1.0 - res.v => 1.0 - cap.v => 0.0 - ] + u0 = [] + prob = ODAEProblem(isys, u0, (0, 10.0)) - sol = solve(prob, Tsit5()) + sol = solve(prob, dt=0.1, Tsit5()) - @test sol[isource.i][1150:end] ≈ waveforms(i, sol.t)[1150:end] atol=1e-1 + @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 # plt = plot(sol) - # savefig(plt, "test_current_$(Symbolics.getname(isource))") + # savefig(plt, "test_current_$(source.name)") end end \ No newline at end of file diff --git a/test/Magnetic/magnetic.jl b/test/Magnetic/magnetic.jl index 7de2ad712..004ca0fb4 100644 --- a/test/Magnetic/magnetic.jl +++ b/test/Magnetic/magnetic.jl @@ -1,6 +1,7 @@ using ModelingToolkitStandardLibrary.Magnetic, ModelingToolkit, OrdinaryDiffEq, Test import ModelingToolkitStandardLibrary.Electrical +import ModelingToolkitStandardLibrary.Blocks import ModelingToolkitStandardLibrary.Magnetic using ModelingToolkit, OrdinaryDiffEq, Test # using Plots @@ -14,7 +15,8 @@ using ModelingToolkit, OrdinaryDiffEq, Test l_Fe = 4*0.065 a = b = 0.25 - @named source = Electrical.SineVoltage(amplitude=230*sqrt(2), frequency=50, phase=pi/2) + @named source = Blocks.Sine(amplitude=230*sqrt(2), frequency=50, phase=pi/2) + @named voltage = Electrical.Voltage() @named r = Electrical.Resistor(R=7.5) @named ground = Electrical.Ground() @named coil = Magnetic.FluxTubes.ElectroMagneticConverter(N=600) @@ -23,18 +25,19 @@ using ModelingToolkit, OrdinaryDiffEq, Test @named r_mFe = Magnetic.FluxTubes.ConstantReluctance(R_m=a * b * l_Fe * mu_Fe) @named r_mLeak = Magnetic.FluxTubes.ConstantReluctance(R_m=1.2e6) connections = [ - connect(source.p, r.p) + connect(source.output, voltage.V) + connect(voltage.p, r.p) connect(r.n, coil.p) - connect(source.n, coil.n) + connect(voltage.n, coil.n) connect(coil.port_p, r_mLeak.port_p) connect(r_mLeak.port_p, r_mAirPar.port_p) connect(r_mAirPar.port_n, r_mFe.port_p) connect(r_mFe.port_n, r_mLeak.port_n) connect(r_mFe.port_n, coil.port_n) - connect(ground.g, source.n) + connect(ground.g, voltage.n) connect(ground_m.port, r_mFe.port_n) ] - @named model = ODESystem(connections, t, systems=[source, r, ground, coil, ground_m, r_mAirPar, r_mFe, r_mLeak]) + @named model = ODESystem(connections, t, systems=[source, r, ground, coil, ground_m, r_mAirPar, r_mFe, r_mLeak, voltage]) sys = structural_simplify(model) prob = ODEProblem(sys, Pair[], (0, 0.1)) sol = solve(prob, Rodas4()) diff --git a/test/Mechanical/rotational.jl b/test/Mechanical/rotational.jl index f620f7392..79ca99014 100644 --- a/test/Mechanical/rotational.jl +++ b/test/Mechanical/rotational.jl @@ -30,9 +30,9 @@ D = Differential(t) end @testset "two inertias with driving torque" begin - amplitude=10 # Amplitude of driving torque - frequency=5 # Frequency of driving torque - J_motor=0.1 # Motor inertia + amplitude = 10 # Amplitude of driving torque + frequency = 5 # Frequency of driving torque + J_motor = 0.1 # Motor inertia @named fixed = Fixed() @named torque = Torque(use_support=true) @@ -66,12 +66,12 @@ end # see: https://doc.modelica.org/Modelica%204.0.0/Resources/helpWSM/Modelica/Modelica.Mechanics.Rotational.Examples.First.html @testset "first example" begin - amplitude=10 # Amplitude of driving torque - frequency=5 # Frequency of driving torque - J_motor=0.1 # Motor inertia - J_load=2 # Load inertia - ratio=10 # Gear ratio - damping=10 # Damping in bearing of gear + amplitude = 10 # Amplitude of driving torque + frequency = 5 # Frequency of driving torque + J_motor = 0.1 # Motor inertia + J_load = 2 # Load inertia + ratio = 10 # Gear ratio + damping = 10 # Damping in bearing of gear @named fixed = Fixed() @named torque = Torque(use_support=true) diff --git a/test/Thermal/thermal.jl b/test/Thermal/thermal.jl index 794493fd9..a16d2f6ec 100644 --- a/test/Thermal/thermal.jl +++ b/test/Thermal/thermal.jl @@ -6,13 +6,13 @@ D = Differential(t) @testset "Heat systems" begin T, C, G = 10.0, 10.0, 10.0 @variables final_T(t) - @named mass1 = HeatCapacitor(C=C) - @named mass2 = HeatCapacitor(C=C) - @named th_conductor = ThermalConductor(G=G) + @named mass1 = HeatCapacitor(C=C) + @named mass2 = HeatCapacitor(C=C) + @named th_conductor = ThermalConductor(G=G) @named reltem_sensor = RelativeTemperatureSensor() - @named T_sensor1 = TemperatureSensor() - @named T_sensor2 = TemperatureSensor() - @named tem_src = FixedTemperature(T=T) + @named T_sensor1 = TemperatureSensor() + @named T_sensor2 = TemperatureSensor() + @named tem_src = FixedTemperature(T=T) @info "Building a single-body system..." eqs = [ @@ -65,13 +65,13 @@ end # Test HeatFlowSensor, FixedHeatFlow, ThermalResistor, ThermalConductor @testset "Heat flow system" begin C, G, R = 10, 10, 10 - @named flow_src = FixedHeatFlow(Q_flow=50, alpha=100) - @named mass1 = HeatCapacitor(C=C) - @named hf_sensor1 = HeatFlowSensor() - @named hf_sensor2 = HeatFlowSensor() + @named flow_src = FixedHeatFlow(Q_flow=50, alpha=100) + @named mass1 = HeatCapacitor(C=C) + @named hf_sensor1 = HeatFlowSensor() + @named hf_sensor2 = HeatFlowSensor() @named th_conductor = ThermalConductor(G=G) - @named th_resistor = ThermalResistor(R=R) - @named th_ground = FixedTemperature(T=0) + @named th_resistor = ThermalResistor(R=R) + @named th_ground = FixedTemperature(T=0) @info "Building a heat-flow system..." eqs = [ @@ -85,7 +85,7 @@ end sys = structural_simplify(h2) u0 = [ - mass1.T => 10.0 + mass1.T => 10.0 th_resistor.Q_flow => 1.0 mass1.der_T => 1.0 ] @@ -105,10 +105,10 @@ end Tᵧ, Tᵪ = 1000, 10 # ᵧ -> gas and ᵪ -> coolant Rᵧ, Rᵪ = 50e-4, 10e-4 # R = 1/h; h is convection co-efficient R_wall = 1.5e-4 - @named coolant = ConvectiveResistor(R=Rᵪ) - @named gas = ConvectiveResistor(R=Rᵧ) - @named wall = ThermalResistor(R=R_wall) - @named gas_tem = FixedTemperature(T=Tᵧ) + @named coolant = ConvectiveResistor(R=Rᵪ) + @named gas = ConvectiveResistor(R=Rᵧ) + @named wall = ThermalResistor(R=R_wall) + @named gas_tem = FixedTemperature(T=Tᵧ) @named coolant_tem = FixedTemperature(T=Tᵪ) @info "Building a piston-cylinder..." @@ -122,7 +122,7 @@ end sys = structural_simplify(piston) u0 = [ - coolant.dT => 5.0 + coolant.dT => 5.0 wall.Q_flow => 10.0 ] prob = DAEProblem(sys, D.(states(sys)) .=> 0.0, u0, (0, 3.0)) @@ -138,14 +138,14 @@ end Tᵧ, Tᵪ = 1000, 10 # ᵧ -> gas and ᵪ -> coolant R_wall = 10 G = 0.04 - σ = 5.6703744191844294e-8 # Stefan-Boltzmann constant + σ = 5.6703744191844294e-8 # Stefan-Boltzmann constant - @named base = ThermalResistor(R=R_wall) - @named gas_tem = FixedTemperature(T=Tᵧ) + @named base = ThermalResistor(R=R_wall) + @named gas_tem = FixedTemperature(T=Tᵧ) @named coolant_tem = FixedTemperature(T=Tᵪ) - @named radiator = BodyRadiation(G=G) - @named dissipator = ConvectiveConductor(G=10) - @named mass = HeatCapacitor(C=10) + @named radiator = BodyRadiation(G=G) + @named dissipator = ConvectiveConductor(G=10) + @named mass = HeatCapacitor(C=10) @info "Building a radiator..." eqs = [ @@ -169,13 +169,13 @@ end end @testset "Thermal Collector" begin - @named flow_src = FixedHeatFlow(Q_flow=50, alpha=100) - @named hf_sensor = HeatFlowSensor() - @named th_ground = FixedTemperature(T=0) - @named collector = ThermalCollector(m=2) + @named flow_src = FixedHeatFlow(Q_flow=50, alpha=100) + @named hf_sensor = HeatFlowSensor() + @named th_ground = FixedTemperature(T=0) + @named collector = ThermalCollector(m=2) @named th_resistor = ThermalResistor(R=10) - @named tem_src = FixedTemperature(T=10) - @named mass = HeatCapacitor(C=10) + @named tem_src = FixedTemperature(T=10) + @named mass = HeatCapacitor(C=10) @info "Building a heat collector..." eqs = [ diff --git a/test/demo.jl b/test/demo.jl index 42b7c0856..4944b1e2c 100644 --- a/test/demo.jl +++ b/test/demo.jl @@ -1,4 +1,5 @@ using ModelingToolkitStandardLibrary.Electrical, ModelingToolkit, OrdinaryDiffEq #, Plots +using ModelingToolkitStandardLibrary.Blocks: Constant using Test @testset "RC demo" begin @@ -6,18 +7,20 @@ using Test C = 1.0 V = 1.0 @parameters t - @named resistor = Resistor(R=R) + @named resistor = Resistor(R=R) @named capacitor = Capacitor(C=C) - @named source = ConstantVoltage(V=V) - @named ground = Ground() + @named voltage = Voltage() + @named ground = Ground() + @named source = Constant() rc_eqs = [ - connect(source.p, resistor.p) + connect(source.output, voltage.V) + connect(voltage.p, resistor.p) connect(resistor.n, capacitor.p) - connect(capacitor.n, source.n, ground.g) + connect(capacitor.n, voltage.n, ground.g) ] - @named rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, source, ground]) + @named rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, source, voltage, ground]) sys = structural_simplify(rc_model) prob = ODAEProblem(sys, Pair[], (0, 10.0)) sol = solve(prob, Tsit5())