-
-
Notifications
You must be signed in to change notification settings - Fork 46
Hydraulic library updated to use macros #363
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
Changes from 8 commits
7dd7cba
59432e6
418c060
82c6951
5394538
6fad3a0
96371e9
644d620
21b80b6
c064fda
7b0fbf9
4494224
21c6340
d5c56dc
b65ded4
e25b17c
1fe68dd
f4088b5
49a0262
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,55 +1,59 @@ | ||||||
|
|
||||||
| """ | ||||||
| Cap(; p_int, name) | ||||||
| Cap(; name) | ||||||
|
|
||||||
| Caps a hydraulic port to prevent mass flow in or out. | ||||||
|
|
||||||
| # Parameters: | ||||||
| - `p_int`: [Pa] initial pressure (set by `p_int` argument) | ||||||
|
|
||||||
| # Connectors: | ||||||
| - `port`: hydraulic port | ||||||
| """ | ||||||
| @component function Cap(; name) | ||||||
| vars = @variables p(t), [guess = 0] | ||||||
| @mtkmodel Cap begin | ||||||
|
|
||||||
| systems = @named begin | ||||||
| @parameters begin | ||||||
| end | ||||||
|
||||||
|
|
||||||
| @variables begin | ||||||
| p(t), [guess = 0] | ||||||
| end | ||||||
|
|
||||||
| @components begin | ||||||
| port = HydraulicPort() | ||||||
| end | ||||||
|
|
||||||
| eqs = [port.p ~ p | ||||||
| port.dm ~ 0] | ||||||
| @equations begin | ||||||
| port.p ~ p | ||||||
| port.dm ~ 0 | ||||||
| end | ||||||
|
|
||||||
| ODESystem(eqs, t, vars, []; name, systems) | ||||||
| end | ||||||
|
|
||||||
| """ | ||||||
| Open(; p_int, name) | ||||||
| Open(; name) | ||||||
|
|
||||||
| Provides an "open" boundary condition for a hydraulic port such that mass flow `dm` is non-zero. This is opposite from an un-connected hydraulic port or the `Cap` boundary component which sets the mass flow `dm` to zero. | ||||||
|
|
||||||
| # Parameters: | ||||||
| - `p_int`: [Pa] initial pressure (set by `p_int` argument) | ||||||
|
|
||||||
| # Connectors: | ||||||
| - `port`: hydraulic port | ||||||
| """ | ||||||
| @component function Open(; name) | ||||||
| pars = [] | ||||||
| @mtkmodel Open begin | ||||||
|
|
||||||
| @parameters begin | ||||||
| end | ||||||
|
||||||
|
|
||||||
| vars = @variables begin | ||||||
| @variables begin | ||||||
| p(t), [guess = 0] | ||||||
| dm(t), [guess = 0] | ||||||
| end | ||||||
|
|
||||||
| systems = @named begin | ||||||
| @components begin | ||||||
| port = HydraulicPort() | ||||||
| end | ||||||
|
|
||||||
| eqs = [port.p ~ p | ||||||
| port.dm ~ dm] | ||||||
| @equations begin | ||||||
| port.p ~ p | ||||||
| port.dm ~ dm | ||||||
| end | ||||||
|
|
||||||
| ODESystem(eqs, t, vars, pars; name, systems) | ||||||
| end | ||||||
|
|
||||||
| """ | ||||||
|
|
@@ -226,45 +230,44 @@ end | |||||
| @deprecate Pipe Tube | ||||||
|
|
||||||
| """ | ||||||
| FlowDivider(;p_int, n, name) | ||||||
| FlowDivider(; n, name) | ||||||
|
|
||||||
| Reduces the flow from `port_a` to `port_b` by `n`. Useful for modeling parallel tubes efficiently by placing a `FlowDivider` on each end of a tube. | ||||||
|
|
||||||
| # Parameters: | ||||||
| - `p_int`: [Pa] initial pressure | ||||||
| - `n`: divide flow from `port_a` to `port_b` by `n` | ||||||
|
|
||||||
| # Connectors: | ||||||
| - `port_a`: full flow hydraulic port | ||||||
| - `port_b`: part flow hydraulic port | ||||||
| """ | ||||||
| @component function FlowDivider(; n, name) | ||||||
| @mtkmodel FlowDivider begin | ||||||
|
|
||||||
| #TODO: assert n >= 1 | ||||||
|
|
||||||
| pars = @parameters begin | ||||||
| @parameters begin | ||||||
| n = n | ||||||
| end | ||||||
|
|
||||||
| vars = @variables begin | ||||||
| @variables begin | ||||||
| dm_a(t), [guess = 0] | ||||||
| dm_b(t), [guess = 0] | ||||||
| end | ||||||
|
|
||||||
| systems = @named begin | ||||||
| @components begin | ||||||
| port_a = HydraulicPort() | ||||||
| port_b = HydraulicPort() | ||||||
| open = Open() | ||||||
| end | ||||||
|
|
||||||
| eqs = [connect(port_a, port_b, open.port) | ||||||
| dm_a ~ port_a.dm | ||||||
| dm_b ~ dm_a / n | ||||||
| open.dm ~ dm_a - dm_b # extra flow dumps into an open port | ||||||
| # port_b.dm ~ dm_b # divided flow goes to port_b | ||||||
| ] | ||||||
| @equations begin | ||||||
| connect(port_a, port_b, open.port) | ||||||
| dm_a ~ port_a.dm | ||||||
| dm_b ~ dm_a / n | ||||||
| open.dm ~ dm_a - dm_b # extra flow dumps into an open port | ||||||
| # port_b.dm ~ dm_b # divided flow goes to port_b | ||||||
| end | ||||||
|
|
||||||
| ODESystem(eqs, t, vars, pars; name, systems) | ||||||
| end | ||||||
|
|
||||||
| @component function ValveBase( | ||||||
|
|
@@ -313,14 +316,11 @@ end | |||||
| end | ||||||
|
|
||||||
| """ | ||||||
| Valve(reversible = false; p_a_int, p_b_int, area_int, Cd, Cd_reverse = Cd, minimum_area = 0, name) | ||||||
| Valve(reversible = false; Cd, Cd_reverse = Cd, minimum_area = 0, name) | ||||||
|
|
||||||
| Valve with `area` input and discharge coefficient `Cd` defined by https://en.wikipedia.org/wiki/Discharge_coefficient. The `Cd_reverse` parameter allows for directional flow restriction, making it possible to define a check valve. | ||||||
|
|
||||||
| # Parameters: | ||||||
| - `p_a_int`: [Pa] initial pressure for `port_a` | ||||||
| - `p_b_int`: [Pa] initial pressure for `port_b` | ||||||
| - `area_int`: [m^2] initial valve opening | ||||||
| - `Cd`: discharge coefficient flowing from `a → b` | ||||||
| - `Cd_reverse`: discharge coefficient flowing from `b → a` | ||||||
| - `minimum_area`: when `reversible = false` applies a forced minimum area | ||||||
|
|
@@ -357,36 +357,37 @@ Valve with `area` input and discharge coefficient `Cd` defined by https://en.wik | |||||
| ODESystem(eqs, t, vars, pars; name, systems) | ||||||
| end | ||||||
|
|
||||||
| @component function VolumeBase(; area, dead_volume = 0, Χ1 = 1, Χ2 = 1, | ||||||
| name) | ||||||
| pars = @parameters begin | ||||||
| @mtkmodel VolumeBase begin | ||||||
|
|
||||||
| @parameters begin | ||||||
| area = area | ||||||
| dead_volume = dead_volume | ||||||
| x1 = 1 | ||||||
| x2 = 1 | ||||||
|
||||||
| end | ||||||
|
|
||||||
| systems = @named begin | ||||||
| port = HydraulicPort() | ||||||
| end | ||||||
|
|
||||||
| vars = @variables begin | ||||||
| @variables begin | ||||||
| x(t) | ||||||
| dx(t), [guess = 0] | ||||||
| rho(t), [guess = liquid_density(port)] | ||||||
| drho(t), [guess = 0] | ||||||
| vol(t) | ||||||
| end | ||||||
|
|
||||||
| # let | ||||||
| dm = port.dm | ||||||
| p = port.p | ||||||
| @components begin | ||||||
| port = HydraulicPort() | ||||||
| end | ||||||
|
|
||||||
| eqs = [vol ~ dead_volume + area * x | ||||||
| D(x) ~ dx | ||||||
| D(rho) ~ drho | ||||||
| rho ~ full_density(port, p) | ||||||
| dm ~ drho * vol * Χ1 + rho * area * dx * Χ2] | ||||||
| @equations begin | ||||||
| dm = port.dm | ||||||
| p = port.p | ||||||
|
||||||
| vol ~ dead_volume + area * x | ||||||
| D(x) ~ dx | ||||||
| D(rho) ~ drho | ||||||
| rho ~ full_density(port, p) | ||||||
| dm ~ drho * vol * Χ1 + rho * area * dx * Χ2 | ||||||
|
||||||
| dm ~ drho * vol * Χ1 + rho * area * dx * Χ2 | |
| dm ~ drho * vol * x1 + rho * area * dx * x2 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Volume | |
| vol |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be added to a
begin
...
end
(outside @equations)
As dm and p are not defined as variables, these equations will throw errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like accessing values from subcomponent can't be added to begin...end. My bad.
Alternative fix would be, define dm and p as @variables.
And move these back to @equations .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems simplest?
@equations begin
D(rho) ~ drho
rho ~ full_density(port, port.p)
port.dm ~ drho * vol
end
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
direction could be of type @structurtal_parameters.
Is this intentionally promoted to a @parameters?
While here, could you change +1 to 1?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaults = [rho => liquid_density(port)]
can be added via the @defaults block https://docs.sciml.ai/ModelingToolkit/stable/basics/MTKLanguage/#@defaults-begin-block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this correct?
@defaults begin
rho => liquid_density(port)
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a new line at the end (to keep Git-diff rendering happy).