Skip to content
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

fix: change variable defaults to guesses #281

Closed
Closed
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
10 changes: 5 additions & 5 deletions src/Blocks/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Initial value of integrator state ``x`` can be set with `x`
@mtkmodel Integrator begin
@extend u, y = siso = SISO()
@variables begin
x(t) = 0.0, [description = "State of Integrator"]
x(t), [guess = 0.0, description = "State of Integrator"]
Copy link
Contributor

@SebastianM-C SebastianM-C May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also had the same suggestion in #292, but #292 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Integrators always start at zero though?

Most of the time this is what users want, but it is occasionally useful to have the system solve for an integrator value that makes an initial control signal have a particular value to avoid initial transients, so forcing the user to specify the initial state explicitly might not be too bad

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the user can not easily remove a default value that has been specified in the library? If they cannot remove the value, we must not provide it since then it cannot be solved for

Copy link
Contributor

@baggepinnen baggepinnen Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we now have the capability to remove a default, the explicit state variables, like those in Integrator, Derivative, FirstOrder, SecondOrder should keep their defaults, but y,u in SISO etc. should not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I am probably going to make a new PR, since almost everything in this is not correct :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChrisRackauckas We can probably close this without merging since this PR has more incorrect than correct things, I'll make a new PR for the things that I would like to fix and the docs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

end
@parameters begin
k = 1, [description = "Gain"]
Expand Down Expand Up @@ -65,7 +65,7 @@ Initial value of the state ``x`` can be set with `x`.
@mtkmodel Derivative begin
@extend u, y = siso = SISO()
@variables begin
x(t) = 0.0, [description = "Derivative-filter state"]
x(t), [guess = 0.0, description = "Derivative-filter state"]
end
@parameters begin
T = T, [description = "Time constant"]
Expand Down Expand Up @@ -122,7 +122,7 @@ See also [`SecondOrder`](@ref)
lowpass = true
end
@variables begin
x(t) = 0.0, [description = "State of FirstOrder filter"]
x(t), [guess = 0.0, description = "State of FirstOrder filter"]
end
@parameters begin
T = T, [description = "Time constant"]
Expand Down Expand Up @@ -545,7 +545,7 @@ linearized around the operating point `x₀, u₀`, we have `y0, u0 = h(x₀, u
end
@named input = RealInput(nin = nu)
@named output = RealOutput(nout = ny)
@variables x(t)[1:nx]=x [
@variables x(t)[1:nx]=x [ # FIXME: should it be guess = x, not default?
description = "State variables of StateSpace system $name"
]
# pars = @parameters A=A B=B C=C D=D # This is buggy
Expand Down Expand Up @@ -618,7 +618,7 @@ See also [`StateSpace`](@ref) which handles MIMO systems, as well as [ControlSys
@parameters a_end = ifelse(a[end] > 100 * symbolic_eps(sqrt(a' * a)), a[end], 1.0)

pars = [collect(b); a; collect(bb); d; a_end]
@variables begin
@variables begin # FIXME: should it be guesses, not default?
x(t)[1:nx] = zeros(nx),
[description = "State of transfer function on controller canonical form"]
x_scaled(t)[1:nx] = collect(x) * a_end, [description = "Scaled vector x"]
Expand Down
68 changes: 45 additions & 23 deletions src/Blocks/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,52 +115,74 @@ Connector with an array of output signals of type Real.
""" RealOutputArray

"""
SISO(;name, u_start = 0.0, y_start = 0.0)
SISO(;name, u_guess = 0.0, y_guess = 0.0)

Single input single output (SISO) continuous system block.

# Parameters:

- `u_start`: Initial value for the input
- `y_start`: Initial value for the output
- `u_guess`: Initial value for the input
- `y_guess`: Initial value for the output
"""
@mtkmodel SISO begin
@parameters begin
u_start = 0.0
y_start = 0.0
@component function SISO(; name, u_start = nothing, y_start = nothing, u_guess = 0.0, y_guess = 0.0)
if u_start !== nothing
Base.depwarn(
"The keyword argument `u_start` is deprecated. Use `u_guess` instead.", :u_start)
u_guess = u_start
end
@variables begin
u(t) = u_start, [description = "Input of SISO system"]
y(t) = y_start, [description = "Output of SISO system"]
if y_start !== nothing
Base.depwarn(
"The keyword argument `y_start` is deprecated. Use `u_guess` instead.", :y_start)
y_guess = y_start
end
@components begin
input = RealInput(guess = u_start)
output = RealOutput(guess = y_start)
pars = @parameters begin
u_guess = u_guess
y_guess = y_guess
end
@equations begin
vars = @variables begin
u(t), [guess = u_guess, description = "Input of SISO system"]
y(t), [guess = y_guess, description = "Output of SISO system"]
end

@named input = RealInput(guess = u_guess)
@named output = RealOutput(guess = y_guess)

eqs = [
u ~ input.u
y ~ output.u
end
]
return ODESystem(eqs, t, vars, pars; name = name, systems = [input, output])
end

"""
MIMO(; name, nin = 1, nout = 1, u_start = zeros(nin), y_start = zeros(nout))
MIMO(; name, nin = 1, nout = 1, u_guess = zeros(nin), y_guess = zeros(nout))

Base class for a multiple input multiple output (MIMO) continuous system block.

# Parameters:

- `nin`: Input dimension
- `nout`: Output dimension
- `u_start`: Initial value for the input
- `u_guess`: Initial value for the input
- `y_start`: Initial value for the output
"""
@component function MIMO(; name, nin = 1, nout = 1, u_start = zeros(nin),
y_start = zeros(nout))
@named input = RealInput(nin = nin, guess = u_start)
@named output = RealOutput(nout = nout, guess = y_start)
@variables(u(t)[1:nin]=u_start, [description = "Input of MIMO system $name"],
y(t)[1:nout]=y_start, [description = "Output of MIMO system $name"],)
@component function MIMO(; name, nin = 1, nout = 1, u_start = nothing, y_start = nothing, u_guess = nin > 1 ? zeros(nin) : 0.0, y_guess = nout > 1 ? zeros(nout) : 0.0)
if u_start !== nothing
Base.depwarn(
"The keyword argument `u_start` is deprecated. Use `u_guess` instead.", :u_start)
u_guess = u_start
end
if y_start !== nothing
Base.depwarn(
"The keyword argument `y_start` is deprecated. Use `y_guess` instead.", :y_start)
y_guess = y_start
end
@named input = RealInputArray(nin = nin, guess = u_guess)
@named output = RealOutputArray(nout = nout, guess = y_guess)
@variables begin
u(t)[1:nin], [guess = u_guess, description = "Input of MIMO system $name"]
y(t)[1:nout], [guess = y_guess, description = "Output of MIMO system $name"]
end
eqs = [
[u[i] ~ input.u[i] for i in 1:nin]...,
[y[i] ~ output.u[i] for i in 1:nout]...
Expand Down
4 changes: 2 additions & 2 deletions src/Thermal/HeatTransfer/ideal_components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Lumped thermal element storing heat
C, [description = "Heat capacity of element"]
end
@variables begin
T(t) = 273.15 + 20
der_T(t)
T(t), [guess = 273.15 + 20]
der_T(t), [guess = 0.0]
end

@equations begin
Expand Down
10 changes: 5 additions & 5 deletions src/Thermal/utils.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@connector HeatPort begin
T(t) = 273.15 + 20.0
Q_flow(t), [connect = Flow]
Q_flow(t) = 0.0, [connect = Flow]
end
Base.@doc """
HeatPort(; name, T = 273.15 + 20.0, Q_flow = 0.0)
Expand Down Expand Up @@ -35,8 +35,8 @@ flow rate through the element from `port_a` to `port_b`, `Q_flow`.
port_b = HeatPort()
end
@variables begin
dT(t)
Q_flow(t)
dT(t) = 0.0
Q_flow(t) = 0.0
end
@equations begin
dT ~ port_a.T - port_b.T
Expand Down Expand Up @@ -69,8 +69,8 @@ flow rate through the element from `solid` to `fluid`, `Q_flow`.
fluid = HeatPort()
end
@variables begin
dT(t)
Q_flow(t)
dT(t) = 0.0
Q_flow(t) = 0.0
end
@equations begin
dT ~ solid.T - fluid.T
Expand Down
Loading