Skip to content

Add units and missing descriptions to components #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Blocks/Blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ using ModelingToolkit, Symbolics
import IfElse: ifelse
import ..@symcheck
using ModelingToolkit: getdefault
using ..DynamicQuantities: @u_str

@parameters t
@parameters t [unit = u"s"]
D = Differential(t)

export RealInput, RealOutput, SISO
Expand Down
14 changes: 7 additions & 7 deletions src/Blocks/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ Output the product of a gain matrix with the input signal vector.
- `output`
"""
@mtkmodel MatrixGain begin
@parameters begin
K, [description = "Matrix gain"]
@structural_parameters begin
K
end
begin
nout = size(getdefault(K), 1)
nin = size(getdefault(K), 2)
nout = size(K, 1)
nin = size(K, 2)
end
@components begin
input = RealInput(; nin = size(K, 2))
output = RealOutput(; nout = size(K, 1))
input = RealInput(; nin = nin)
output = RealOutput(; nout = nout)
end
@equations begin
[(@info i, j; output.u[i] ~ sum(getdefault(K)[i, j] * input.u[j])) for j in 1:nin
[(output.u[i] ~ sum(K[i, j] * input.u[j])) for j in 1:nin
for i in 1:nout]...
end
end
Expand Down
3 changes: 1 addition & 2 deletions src/Blocks/nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,14 @@ Initial value of state `Y` can be set with `int.y`
rising = 1.0, [description = "Maximum rising slew rate of SlewRateLimiter"]
falling = -rising, [description = "Derivative time constant of SlewRateLimiter"]
Td = 0.001, [description = "Derivative time constant"]
y_start
end
begin
getdefault(rising) ≥ getdefault(falling) ||
throw(ArgumentError("`rising` must be smaller than `falling`"))
getdefault(Td) > 0 ||
throw(ArgumentError("Time constant `Td` must be strictly positive"))
end
@extend u, y = siso = SISO(y_start = y_start)
@extend u, y = siso = SISO(; y_start)
@equations begin
D(y) ~ max(min((u - y) / Td, rising), falling)
end
Expand Down
46 changes: 26 additions & 20 deletions src/Blocks/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Generate constant signal.
"""
@mtkmodel Constant begin
@components begin
output = RealOutput()
output = RealOutput(; unit)
end
@parameters begin
k = 0.0, [description = "Constant output value of block"]
Expand All @@ -101,7 +101,7 @@ The input variable `t` can be changed by passing a different variable as the key
f
end
@components begin
output = RealOutput()
output = RealOutput(; unit)
end
@equations begin
output.u ~ first(getdefault(f))(t)
Expand Down Expand Up @@ -135,8 +135,9 @@ Generate sine signal.
phase = 0,
offset = 0,
start_time = 0,
smooth = false)
@named output = RealOutput()
smooth = false,
output__unit = nothing)
@named output = RealOutput(; unit = output__unit)
pars = @parameters offset=offset start_time=start_time amplitude=amplitude frequency=frequency phase=phase
equation = if smooth == false
offset + ifelse(t < start_time, 0,
Expand Down Expand Up @@ -178,8 +179,9 @@ Cosine signal.
phase = 0,
offset = 0,
start_time = 0,
smooth = false)
@named output = RealOutput()
smooth = false,
output__unit = nothing)
@named output = RealOutput(; unit = output__unit)
pars = @parameters offset=offset start_time=start_time amplitude=amplitude frequency=frequency phase=phase
equation = if smooth == false
offset + ifelse(t < start_time, zero(t),
Expand Down Expand Up @@ -209,8 +211,8 @@ Generate current time signal.

- `output`
"""
@component function ContinuousClock(; name, offset = 0, start_time = 0)
@named output = RealOutput()
@component function ContinuousClock(; name, offset = 0, start_time = 0, output__unit = nothing)
@named output = RealOutput(; unit = output__unit)
pars = @parameters offset=offset start_time=start_time
eqs = [
output.u ~ offset + ifelse(t < start_time, zero(t), t - start_time),
Expand Down Expand Up @@ -242,8 +244,9 @@ Generate ramp signal.
duration = 1,
offset = 0,
start_time = 0,
smooth = false)
@named output = RealOutput()
smooth = false,
output__unit = nothing)
@named output = RealOutput(; unit = output__unit)
pars = @parameters offset=offset start_time=start_time height=height duration=duration
equation = if smooth == false
offset + ifelse(t < start_time, 0,
Expand Down Expand Up @@ -280,8 +283,9 @@ Generate smooth square signal.
- `output`
"""
@component function Square(; name, frequency = 1.0, amplitude = 1.0,
offset = 0.0, start_time = 0.0, smooth = false)
@named output = RealOutput()
offset = 0.0, start_time = 0.0, smooth = false,
output__unit = nothing)
@named output = RealOutput(; unit = output__unit)
pars = @parameters begin
frequency = frequency
amplitude = amplitude
Expand Down Expand Up @@ -322,8 +326,8 @@ Generate step signal.
- `output`
"""
@component function Step(; name, height = 1, offset = 0, start_time = 0, duration = Inf,
smooth = 1e-5)
@named output = RealOutput()
smooth = 1e-5, output__unit = nothing)
@named output = RealOutput(; unit = output__unit)
duration_numeric = duration
pars = @parameters offset=offset start_time=start_time height=height duration=duration
equation = if smooth == false # use comparison in case smooth is a float
Expand Down Expand Up @@ -372,8 +376,9 @@ Exponentially damped sine signal.
phase = 0,
offset = 0,
start_time = 0,
smooth = false)
@named output = RealOutput()
smooth = false,
output__unit = nothing)
@named output = RealOutput(; unit = output__unit)
pars = @parameters offset=offset start_time=start_time amplitude=amplitude frequency=frequency phase=phase damping=damping

equation = if smooth == false
Expand Down Expand Up @@ -413,8 +418,9 @@ Generate smooth triangular signal for frequencies less than or equal to 25 Hz
- `output`
"""
@component function Triangular(; name, amplitude = 1.0, frequency = 1.0,
offset = 0.0, start_time = 0.0, smooth = false)
@named output = RealOutput()
offset = 0.0, start_time = 0.0, smooth = false,
output__unit = nothing)
@named output = RealOutput(; unit = output__unit)
pars = @parameters begin
amplitude = amplitude
frequency = frequency
Expand Down Expand Up @@ -604,13 +610,13 @@ data input component.
# Connectors:
- `output`
"""
@component function SampledData(; name, buffer)
@component function SampledData(; name, buffer, unit = nothing)
pars = @parameters begin
buffer = buffer
end
vars = []
systems = @named begin
output = RealOutput()
output = RealOutput(; unit)
end
eqs = [
output.u ~ get_sampled_data(t, buffer),
Expand Down
70 changes: 51 additions & 19 deletions src/Blocks/utils.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
@connector function RealInput(; name, nin = 1, u_start = nin > 1 ? zeros(nin) : 0.0)
@connector function RealInput(; name, nin = 1, u_start = nin > 1 ? zeros(nin) : 0.0, unit = nothing)
if nin == 1
@variables u(t)=u_start [
input = true,
description = "Inner variable in RealInput $name",
]
if unit !== nothing
@variables u(t)=u_start [
input = true,
description = "Inner variable in RealInput $name",
unit = unit
]
else
@variables u(t)=u_start [
input = true,
description = "Inner variable in RealInput $name",
]
end
else
@variables u(t)[1:nin]=u_start [
input = true,
description = "Inner variable in RealInput $name",
]
if unit !== nothing
@variables u(t)[1:nin]=u_start [
input = true,
description = "Inner variable in RealInput $name",
unit = unit
]
else
@variables u(t)[1:nin]=u_start [
input = true,
description = "Inner variable in RealInput $name",
]
end
u = collect(u)
end
ODESystem(Equation[], t, [u...], []; name = name)
Expand All @@ -26,18 +42,34 @@ Connector with one input signal of type Real.
- `u`: Value of the connector; if nin=1 this is a scalar
""" RealInput

@connector function RealOutput(; name, nout = 1, u_start = nout > 1 ? zeros(nout) : 0.0)
@connector function RealOutput(; name, nout = 1, u_start = nout > 1 ? zeros(nout) : 0.0, unit = nothing)
if nout == 1
@variables u(t)=u_start [
output = true,
description = "Inner variable in RealOutput $name",
]
if unit !== nothing
@variables u(t)=u_start [
output = true,
description = "Inner variable in RealOutput $name",
unit = unit
]
else
@variables u(t)=u_start [
output = true,
description = "Inner variable in RealOutput $name",
]
end
else
@variables u(t)[1:nout]=u_start [
output = true,
description = "Inner variable in RealOutput $name",
]
u = collect(u)
if unit !== nothing
@variables u(t)[1:nout]=u_start [
output = true,
description = "Inner variable in RealOutput $name",
unit = unit
]
else
@variables u(t)[1:nout]=u_start [
output = true,
description = "Inner variable in RealOutput $name",
]
end
u = collect(u)
end
ODESystem(Equation[], t, [u...], []; name = name)
end
Expand Down
38 changes: 13 additions & 25 deletions src/Electrical/Analog/ideal_components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ See [OnePort](@ref)
@mtkmodel Resistor begin
@extend v, i = oneport = OnePort()
@parameters begin
R, [description = "Resistance"]
R, [description = "Resistance", unit = u"Ω"]
end
@equations begin
v ~ i * R
Expand Down Expand Up @@ -66,7 +66,7 @@ See [OnePort](@ref)
@mtkmodel Conductor begin
@extend v, i = oneport = OnePort()
@parameters begin
G, [description = "Conductance"]
G, [description = "Conductance", unit = u"S"]
end
@equations begin
i ~ v * G
Expand All @@ -93,13 +93,10 @@ See [OnePort](@ref)
- `C`: [`F`] Capacitance
"""
@mtkmodel Capacitor begin
@extend v, i = oneport = OnePort(; v = 0.0)
@parameters begin
C, [description = "Capacitance"]
C, [description = "Capacitance",unit = u"F"]
end
@variables begin
v
end
@extend v, i = oneport = OnePort(; v = v)
@equations begin
D(v) ~ i / C
end
Expand All @@ -125,13 +122,10 @@ See [OnePort](@ref)
- `L`: [`H`] Inductance
"""
@mtkmodel Inductor begin
@extend v, i = oneport = OnePort(; i = 0.0)
@parameters begin
L, [description = "Inductance"]
end
@variables begin
i
L, [description = "Inductance", unit = u"H"]
end
@extend v, i = oneport = OnePort(; i = i)
@equations begin
D(i) ~ 1 / L * v
end
Expand Down Expand Up @@ -211,9 +205,9 @@ Temperature dependent electrical resistor
heat_port = HeatPort()
end
@parameters begin
R_ref = 1.0, [description = "Reference resistance"]
T_ref = 300.15, [description = "Reference temperature"]
alpha = 0, [description = "Temperature coefficient of resistance"]
R_ref = 1.0, [description = "Reference resistance", unit = u"Ω"]
T_ref = 300.15, [description = "Reference temperature", unit = u"K"]
alpha = 0, [description = "Temperature coefficient of resistance", unit = u"1/K"]
end
@variables begin
R(t) = R_ref
Expand Down Expand Up @@ -248,25 +242,19 @@ Electromotoric force (electric/mechanic transformer)
- `k`: [`N⋅m/A`] Transformation coefficient
"""
@mtkmodel EMF begin
@extend OnePort()
@components begin
p = Pin()
n = Pin()
flange = Flange()
support = Support()
end
@parameters begin
k, [description = "Transformation coefficient"]
k, [description = "Transformation coefficient", unit = u"N*m/A"]
end
@variables begin
v(t) = 0.0
i(t) = 0.0
phi(t) = 0.0
w(t) = 0.0
phi(t) = 0.0, [description = "Rotation angle", unit = u"rad"]
w(t) = 0.0, [description = "Angular velocity", unit = u"rad/s"]
end
@equations begin
v ~ p.v - n.v
0 ~ p.i + n.i
i ~ p.i
phi ~ flange.phi - support.phi
D(phi) ~ w
k * w ~ v
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy