Skip to content

Commit b5b01b4

Browse files
Merge pull request #372 from ven-k/vkb/hydraulic
Drop ref to non-existant `p_int` and fix format
2 parents e14e633 + 6a038b0 commit b5b01b4

File tree

12 files changed

+59
-84
lines changed

12 files changed

+59
-84
lines changed

docs/src/tutorials/input_component.md

+13-17
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ function MassSpringDamper(; name)
3838
@variables f(t)=0 x(t)=0 dx(t)=0 ddx(t)=0
3939
@parameters m=10 k=1000 d=1
4040
41-
eqs = [
42-
f ~ input.u
41+
eqs = [f ~ input.u
4342
ddx * 10 ~ k * x + d * dx + f
4443
D(x) ~ dx
4544
D(dx) ~ ddx]
@@ -52,10 +51,8 @@ function MassSpringDamperSystem(data, time; name)
5251
@named clk = ContinuousClock()
5352
@named model = MassSpringDamper()
5453
55-
eqs = [
56-
connect(src.input, clk.output)
57-
connect(src.output, model.input)
58-
]
54+
eqs = [connect(src.input, clk.output)
55+
connect(src.output, model.input)]
5956
6057
ODESystem(eqs, t, [], []; name, systems = [src, clk, model])
6158
end
@@ -100,11 +97,10 @@ using Plots
10097
10198
function MassSpringDamper(; name)
10299
@named input = RealInput()
103-
vars = @variables f(t) x(t)=0 dx(t) [guess=0] ddx(t)
100+
vars = @variables f(t) x(t)=0 dx(t) [guess = 0] ddx(t)
104101
pars = @parameters m=10 k=1000 d=1
105102
106-
eqs = [
107-
f ~ input.u
103+
eqs = [f ~ input.u
108104
ddx * 10 ~ k * x + d * dx + f
109105
D(x) ~ dx
110106
D(dx) ~ ddx]
@@ -117,10 +113,8 @@ function MassSpringDamperSystem(data, time; name)
117113
@named clk = ContinuousClock()
118114
@named model = MassSpringDamper()
119115
120-
eqs = [
121-
connect(model.input, src.output)
122-
connect(src.input, clk.output)
123-
]
116+
eqs = [connect(model.input, src.output)
117+
connect(src.input, clk.output)]
124118
125119
ODESystem(eqs, t; name, systems = [src, clk, model])
126120
end
@@ -143,13 +137,15 @@ plot(sol)
143137
```
144138

145139
If we want to run a new data set, this requires only remaking the problem and solving again
140+
146141
```@example parametrized_interpolation
147142
prob2 = remake(prob, p = [sys.src.data => ones(length(df.data))])
148143
sol2 = solve(prob2)
149144
plot(sol2)
150145
```
151146

152147
!!! note
148+
153149
Note that when changing the data, the length of the new data must be the same as the length of the original data.
154150

155151
## Custom Component with External Data
@@ -224,7 +220,7 @@ using ModelingToolkitStandardLibrary.Blocks
224220
using OrdinaryDiffEq
225221

226222
function System(; name)
227-
src = SampledData(Float64, name=:src)
223+
src = SampledData(Float64, name = :src)
228224

229225
vars = @variables f(t)=0 x(t)=0 dx(t)=0 ddx(t)=0
230226
pars = @parameters m=10 k=1000 d=1
@@ -238,22 +234,22 @@ function System(; name)
238234
end
239235

240236
@named system = System()
241-
sys = structural_simplify(system, split=false)
237+
sys = structural_simplify(system, split = false)
242238
s = complete(system)
243239

244240
dt = 4e-4
245241
time = 0:dt:0.1
246242
data1 = sin.(2 * pi * time * 100)
247243
data2 = cos.(2 * pi * time * 50)
248244

249-
prob = ODEProblem(sys, [], (0, time[end]); split=false, tofloat = false, use_union=true)
245+
prob = ODEProblem(sys, [], (0, time[end]); split = false, tofloat = false, use_union = true)
250246
defs = ModelingToolkit.defaults(sys)
251247

252248
function get_prob(data)
253249
defs[s.src.buffer] = Parameter(data, dt)
254250
# ensure p is a uniform type of Vector{Parameter{Float64}} (converting from Vector{Any})
255251
p = Parameter.(ModelingToolkit.varmap_to_vars(defs, parameters(sys); tofloat = false))
256-
remake(prob; p, build_initializeprob=false)
252+
remake(prob; p, build_initializeprob = false)
257253
end
258254

259255
prob1 = get_prob(data1)

src/Blocks/Blocks.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ using ModelingToolkit: getdefault, t_nounits as t, D_nounits as D
1010
export RealInput, RealInputArray, RealOutput, RealOutputArray, SISO
1111
include("utils.jl")
1212

13-
export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division, Power, Modulo, UnaryMinus, Floor, Ceil
13+
export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division, Power, Modulo,
14+
UnaryMinus, Floor, Ceil
1415
export Abs, Sign, Sqrt, Sin, Cos, Tan, Asin, Acos, Atan, Atan2, Sinh, Cosh, Tanh, Exp
1516
export Log, Log10
1617
include("math.jl")

src/Blocks/math.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ Output the exponential with base as the first input and exponent as second input
226226
output = RealOutput()
227227
end
228228
@equations begin
229-
output.u ~ base.u ^ exponent.u
229+
output.u ~ base.u^exponent.u
230230
end
231231
end
232232

src/Hydraulic/IsothermalCompressible/components.jl

+7-25
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11

22
"""
3-
Cap(; p_int, name)
3+
Cap(; name)
44
55
Caps a hydraulic port to prevent mass flow in or out.
66
7-
# Parameters:
8-
- `p_int`: [Pa] initial pressure (set by `p_int` argument)
9-
107
# Connectors:
118
- `port`: hydraulic port
129
"""
1310
@mtkmodel Cap begin
14-
1511
@variables begin
1612
p(t), [guess = 0]
17-
end
13+
end
1814

1915
@components begin
2016
port = HydraulicPort()
@@ -24,22 +20,17 @@ Caps a hydraulic port to prevent mass flow in or out.
2420
port.p ~ p
2521
port.dm ~ 0
2622
end
27-
2823
end
2924

3025
"""
31-
Open(; p_int, name)
26+
Open(; name)
3227
33-
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.
34-
35-
# Parameters:
36-
- `p_int`: [Pa] initial pressure (set by `p_int` argument)
28+
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.
3729
3830
# Connectors:
3931
- `port`: hydraulic port
4032
"""
4133
@mtkmodel Open begin
42-
4334
@variables begin
4435
p(t), [guess = 0]
4536
dm(t), [guess = 0]
@@ -53,7 +44,6 @@ Provides an "open" boundary condition for a hydraulic port such that mass flow `
5344
port.p ~ p
5445
port.dm ~ dm
5546
end
56-
5747
end
5848

5949
"""
@@ -230,12 +220,11 @@ end
230220
@deprecate Pipe Tube
231221

232222
"""
233-
FlowDivider(;p_int, n, name)
223+
FlowDivider(; n, name)
234224
235225
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.
236226
237227
# Parameters:
238-
- `p_int`: [Pa] initial pressure
239228
- `n`: divide flow from `port_a` to `port_b` by `n`
240229
241230
# Connectors:
@@ -268,7 +257,6 @@ Reduces the flow from `port_a` to `port_b` by `n`. Useful for modeling parallel
268257
open.dm ~ dm_a - dm_b # extra flow dumps into an open port
269258
# port_b.dm ~ dm_b # divided flow goes to port_b
270259
end
271-
272260
end
273261

274262
@component function ValveBase(
@@ -362,7 +350,6 @@ Valve with `area` input and discharge coefficient `Cd` defined by https://en.wik
362350
end
363351

364352
@mtkmodel VolumeBase begin
365-
366353
@structural_parameters begin
367354
Χ1 = 1
368355
Χ2 = 1
@@ -392,7 +379,6 @@ end
392379
rho ~ full_density(port, port.p)
393380
port.dm ~ drho * vol * Χ1 + rho * area * dx * Χ2
394381
end
395-
396382
end
397383

398384
"""
@@ -407,7 +393,6 @@ Fixed fluid volume.
407393
- `port`: hydraulic port
408394
"""
409395
@mtkmodel FixedVolume begin
410-
411396
@parameters begin
412397
vol
413398
end
@@ -426,7 +411,6 @@ Fixed fluid volume.
426411
rho ~ full_density(port, port.p)
427412
port.dm ~ drho * vol
428413
end
429-
430414
end
431415

432416
"""
@@ -468,7 +452,6 @@ dm ────► │ │ area
468452
See also [`FixedVolume`](@ref), [`DynamicVolume`](@ref)
469453
"""
470454
@mtkmodel Volume begin
471-
472455
@structural_parameters begin
473456
direction = 1
474457
end
@@ -512,7 +495,6 @@ See also [`FixedVolume`](@ref), [`DynamicVolume`](@ref)
512495
@defaults begin
513496
rho => liquid_density(port)
514497
end
515-
516498
end
517499

518500
"""
@@ -738,7 +720,7 @@ end
738720
"""
739721
SpoolValve2Way(reversible = false; p_s_int, p_a_int, p_b_int, p_r_int, m, g, x_int, Cd, d, name)
740722
741-
2-ways spool valve with 4 ports and spool mass. Fluid flow direction S → A and B → R when `x` is positive and S → B and A → R when `x` is negative.
723+
2-ways spool valve with 4 ports and spool mass. Fluid flow direction S → A and B → R when `x` is positive and S → B and A → R when `x` is negative.
742724
743725
# Parameters:
744726
- `p_s_int`: [Pa] initial pressure for `port_s`
@@ -819,7 +801,7 @@ end
819801
Cd = 1e4,
820802
Cd_reverse = Cd,
821803
name)
822-
804+
823805
Actuator made of two DynamicVolumes connected in opposite direction with body mass attached.
824806
825807
# Features:

src/Hydraulic/IsothermalCompressible/sources.jl

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
"""
2-
MassFlow(; name, p_int)
2+
MassFlow(; name)
33
44
Hydraulic mass flow input source
55
66
# Connectors:
77
88
- `port`: hydraulic port
9-
- `dm`: real input
9+
- `dm`: real input
1010
"""
1111
@mtkmodel MassFlow begin
12-
1312
@components begin
1413
port = HydraulicPort()
1514
dm = RealInput()
@@ -32,7 +31,6 @@ Fixed pressure source
3231
- `port`: hydraulic port
3332
"""
3433
@mtkmodel FixedPressure begin
35-
3634
@parameters begin
3735
p
3836
end
@@ -44,7 +42,6 @@ Fixed pressure source
4442
@equations begin
4543
port.p ~ p
4644
end
47-
4845
end
4946
@deprecate Source FixedPressure
5047

@@ -55,10 +52,9 @@ input pressure source
5552
5653
# Connectors:
5754
- `port`: hydraulic port
58-
- `p`: real input
55+
- `p`: real input
5956
"""
6057
@mtkmodel Pressure begin
61-
6258
@components begin
6359
port = HydraulicPort()
6460
p = RealInput()
@@ -67,6 +63,5 @@ input pressure source
6763
@equations begin
6864
port.p ~ p.u
6965
end
70-
7166
end
7267
@deprecate InputSource Pressure

src/Hydraulic/IsothermalCompressible/utils.jl

+5-9
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ regPow(x, a, delta = 0.01) = x * (x * x + delta * delta)^((a - 1) / 2);
44
regRoot(x, delta = 0.01) = regPow(x, 0.5, delta)
55

66
"""
7-
HydraulicPort(;p_int, name)
7+
HydraulicPort(; name)
88
99
Connector port for hydraulic components.
1010
11-
# Arguments:
12-
13-
- `p_int`: [Pa] initial gauge pressure
14-
1511
# States:
1612
- `p`: [Pa] gauge total pressure
1713
- `dm`: [kg/s] mass flow
@@ -38,15 +34,15 @@ end
3834
"""
3935
HydraulicFluid(; density = 997, bulk_modulus = 2.09e9, viscosity = 0.0010016, gas_density = 0.0073955, gas_pressure = -1000, n = 1, let_gas = 1, name)
4036
41-
Fluid parameter setter for isothermal compressible fluid domain. Defaults given for water at 20°C and 0Pa gage (1atm absolute) reference pressure. Density is modeled using the Tait equation of state. For pressures below the reference pressure, density is linearly interpolated to the gas state (when `let_gas` is set to 1), this helps prevent pressures from going below the reference pressure.
37+
Fluid parameter setter for isothermal compressible fluid domain. Defaults given for water at 20°C and 0Pa gage (1atm absolute) reference pressure. Density is modeled using the Tait equation of state. For pressures below the reference pressure, density is linearly interpolated to the gas state (when `let_gas` is set to 1), this helps prevent pressures from going below the reference pressure.
4238
4339
# Parameters:
4440
4541
- `ρ`: [kg/m^3] fluid density at 0Pa reference gage pressure (set by `density` argument)
4642
- `Β`: [Pa] fluid bulk modulus describing the compressibility (set by `bulk_modulus` argument)
4743
- `μ`: [Pa*s] or [kg/m-s] fluid dynamic viscosity (set by `viscosity` argument)
4844
- `n`: density exponent
49-
- `let_gas`: set to 1 to allow fluid to transition from liquid to gas (for density calculation only)
45+
- `let_gas`: set to 1 to allow fluid to transition from liquid to gas (for density calculation only)
5046
- `ρ_gas`: [kg/m^3] density of fluid in gas state at reference gage pressure `p_gas` (set by `gas_density` argument)
5147
- `p_gas`: [Pa] reference pressure (set by `gas_pressure` argument)
5248
"""
@@ -80,12 +76,12 @@ f_turbulent(shape_factor, Re) = (shape_factor / 64) / (0.79 * log(Re) - 1.64)^2
8076
"""
8177
friction_factor(dm, area, d_h, viscosity, shape_factor)
8278
83-
Calculates the friction factor ``f`` for fully developed flow in a tube such that ``Δp = f \\cdot \\rho \\frac{u^2}{2} \\frac{l}{d_h}`` where
79+
Calculates the friction factor ``f`` for fully developed flow in a tube such that ``Δp = f \\cdot \\rho \\frac{u^2}{2} \\frac{l}{d_h}`` where
8480
8581
- ``Δp``: [Pa] is the pressure difference over the tube length ``l``
8682
- ``\\rho``: [kg/m^3] is the average fluid density
8783
- ``u``: [m/s] is the average fluid velocity
88-
- ``l``: [m] is the tube length
84+
- ``l``: [m] is the tube length
8985
9086
The friction factor is calculated for laminar and turbulent flow with a transition region between Reynolds number 2000 to 3000. Turbulent flow equation is for smooth tubes, valid for the Reynolds number range up to 5e6.
9187

src/Thermal/HeatTransfer/sources.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ the component FixedHeatFlow is connected, if parameter `Q_flow` is positive.
2828
end
2929

3030
@equations begin
31-
port.Q_flow ~ ifelse(alpha == 0.0,
32-
-Q_flow, # Simplified equation when alpha is 0
33-
-Q_flow * (1 + alpha * (port.T - T_ref)))
31+
port.Q_flow ~ ifelse(alpha == 0.0,
32+
-Q_flow, # Simplified equation when alpha is 0
33+
-Q_flow * (1 + alpha * (port.T - T_ref)))
3434
end
3535
end
3636

src/Thermal/utils.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
@connector function HeatPort(; T = nothing, T_guess = 273.15 + 20, Q_flow = nothing, Q_flow_guess = 0.0, name)
1+
@connector function HeatPort(;
2+
T = nothing, T_guess = 273.15 + 20, Q_flow = nothing, Q_flow_guess = 0.0, name)
23
pars = @parameters begin
34
T_guess = T_guess
45
Q_flow_guess = Q_flow_guess

0 commit comments

Comments
 (0)