Skip to content
Merged
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
30 changes: 13 additions & 17 deletions docs/src/tutorials/input_component.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ function MassSpringDamper(; name)
@variables f(t)=0 x(t)=0 dx(t)=0 ddx(t)=0
@parameters m=10 k=1000 d=1

eqs = [
f ~ input.u
eqs = [f ~ input.u
ddx * 10 ~ k * x + d * dx + f
D(x) ~ dx
D(dx) ~ ddx]
Expand All @@ -52,10 +51,8 @@ function MassSpringDamperSystem(data, time; name)
@named clk = ContinuousClock()
@named model = MassSpringDamper()

eqs = [
connect(src.input, clk.output)
connect(src.output, model.input)
]
eqs = [connect(src.input, clk.output)
connect(src.output, model.input)]

ODESystem(eqs, t, [], []; name, systems = [src, clk, model])
end
Expand Down Expand Up @@ -100,11 +97,10 @@ using Plots

function MassSpringDamper(; name)
@named input = RealInput()
vars = @variables f(t) x(t)=0 dx(t) [guess=0] ddx(t)
vars = @variables f(t) x(t)=0 dx(t) [guess = 0] ddx(t)
pars = @parameters m=10 k=1000 d=1

eqs = [
f ~ input.u
eqs = [f ~ input.u
ddx * 10 ~ k * x + d * dx + f
D(x) ~ dx
D(dx) ~ ddx]
Expand All @@ -117,10 +113,8 @@ function MassSpringDamperSystem(data, time; name)
@named clk = ContinuousClock()
@named model = MassSpringDamper()

eqs = [
connect(model.input, src.output)
connect(src.input, clk.output)
]
eqs = [connect(model.input, src.output)
connect(src.input, clk.output)]

ODESystem(eqs, t; name, systems = [src, clk, model])
end
Expand All @@ -143,13 +137,15 @@ plot(sol)
```

If we want to run a new data set, this requires only remaking the problem and solving again

```@example parametrized_interpolation
prob2 = remake(prob, p = [sys.src.data => ones(length(df.data))])
sol2 = solve(prob2)
plot(sol2)
```

!!! note

Note that when changing the data, the length of the new data must be the same as the length of the original data.

## Custom Component with External Data
Expand Down Expand Up @@ -224,7 +220,7 @@ using ModelingToolkitStandardLibrary.Blocks
using OrdinaryDiffEq

function System(; name)
src = SampledData(Float64, name=:src)
src = SampledData(Float64, name = :src)

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

@named system = System()
sys = structural_simplify(system, split=false)
sys = structural_simplify(system, split = false)
s = complete(system)

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

prob = ODEProblem(sys, [], (0, time[end]); split=false, tofloat = false, use_union=true)
prob = ODEProblem(sys, [], (0, time[end]); split = false, tofloat = false, use_union = true)
defs = ModelingToolkit.defaults(sys)

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

prob1 = get_prob(data1)
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ using ModelingToolkit: getdefault, t_nounits as t, D_nounits as D
export RealInput, RealInputArray, RealOutput, RealOutputArray, SISO
include("utils.jl")

export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division, Power, Modulo, UnaryMinus, Floor, Ceil
export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division, Power, Modulo,
UnaryMinus, Floor, Ceil
export Abs, Sign, Sqrt, Sin, Cos, Tan, Asin, Acos, Atan, Atan2, Sinh, Cosh, Tanh, Exp
export Log, Log10
include("math.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/Blocks/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ Output the exponential with base as the first input and exponent as second input
output = RealOutput()
end
@equations begin
output.u ~ base.u ^ exponent.u
output.u ~ base.u^exponent.u
end
end

Expand Down
32 changes: 7 additions & 25 deletions src/Hydraulic/IsothermalCompressible/components.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@

"""
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
"""
@mtkmodel Cap begin

@variables begin
p(t), [guess = 0]
end
end

@components begin
port = HydraulicPort()
Expand All @@ -24,22 +20,17 @@ Caps a hydraulic port to prevent mass flow in or out.
port.p ~ p
port.dm ~ 0
end

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)
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.

# Connectors:
- `port`: hydraulic port
"""
@mtkmodel Open begin

@variables begin
p(t), [guess = 0]
dm(t), [guess = 0]
Expand All @@ -53,7 +44,6 @@ Provides an "open" boundary condition for a hydraulic port such that mass flow `
port.p ~ p
port.dm ~ dm
end

end

"""
Expand Down Expand Up @@ -230,12 +220,11 @@ 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:
Expand Down Expand Up @@ -268,7 +257,6 @@ Reduces the flow from `port_a` to `port_b` by `n`. Useful for modeling parallel
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

end

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

@mtkmodel VolumeBase begin

@structural_parameters begin
Χ1 = 1
Χ2 = 1
Expand Down Expand Up @@ -392,7 +379,6 @@ end
rho ~ full_density(port, port.p)
port.dm ~ drho * vol * Χ1 + rho * area * dx * Χ2
end

end

"""
Expand All @@ -407,7 +393,6 @@ Fixed fluid volume.
- `port`: hydraulic port
"""
@mtkmodel FixedVolume begin

@parameters begin
vol
end
Expand All @@ -426,7 +411,6 @@ Fixed fluid volume.
rho ~ full_density(port, port.p)
port.dm ~ drho * vol
end

end

"""
Expand Down Expand Up @@ -468,7 +452,6 @@ dm ────► │ │ area
See also [`FixedVolume`](@ref), [`DynamicVolume`](@ref)
"""
@mtkmodel Volume begin

@structural_parameters begin
direction = 1
end
Expand Down Expand Up @@ -512,7 +495,6 @@ See also [`FixedVolume`](@ref), [`DynamicVolume`](@ref)
@defaults begin
rho => liquid_density(port)
end

end

"""
Expand Down Expand Up @@ -738,7 +720,7 @@ end
"""
SpoolValve2Way(reversible = false; p_s_int, p_a_int, p_b_int, p_r_int, m, g, x_int, Cd, d, name)

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.
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.

# Parameters:
- `p_s_int`: [Pa] initial pressure for `port_s`
Expand Down Expand Up @@ -819,7 +801,7 @@ end
Cd = 1e4,
Cd_reverse = Cd,
name)

Actuator made of two DynamicVolumes connected in opposite direction with body mass attached.

# Features:
Expand Down
11 changes: 3 additions & 8 deletions src/Hydraulic/IsothermalCompressible/sources.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""
MassFlow(; name, p_int)
MassFlow(; name)

Hydraulic mass flow input source

# Connectors:

- `port`: hydraulic port
- `dm`: real input
- `dm`: real input
"""
@mtkmodel MassFlow begin

@components begin
port = HydraulicPort()
dm = RealInput()
Expand All @@ -32,7 +31,6 @@ Fixed pressure source
- `port`: hydraulic port
"""
@mtkmodel FixedPressure begin

@parameters begin
p
end
Expand All @@ -44,7 +42,6 @@ Fixed pressure source
@equations begin
port.p ~ p
end

end
@deprecate Source FixedPressure

Expand All @@ -55,10 +52,9 @@ input pressure source

# Connectors:
- `port`: hydraulic port
- `p`: real input
- `p`: real input
"""
@mtkmodel Pressure begin

@components begin
port = HydraulicPort()
p = RealInput()
Expand All @@ -67,6 +63,5 @@ input pressure source
@equations begin
port.p ~ p.u
end

end
@deprecate InputSource Pressure
14 changes: 5 additions & 9 deletions src/Hydraulic/IsothermalCompressible/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ regPow(x, a, delta = 0.01) = x * (x * x + delta * delta)^((a - 1) / 2);
regRoot(x, delta = 0.01) = regPow(x, 0.5, delta)

"""
HydraulicPort(;p_int, name)
HydraulicPort(; name)

Connector port for hydraulic components.

# Arguments:

- `p_int`: [Pa] initial gauge pressure

# States:
- `p`: [Pa] gauge total pressure
- `dm`: [kg/s] mass flow
Expand All @@ -38,15 +34,15 @@ end
"""
HydraulicFluid(; density = 997, bulk_modulus = 2.09e9, viscosity = 0.0010016, gas_density = 0.0073955, gas_pressure = -1000, n = 1, let_gas = 1, name)

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.
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.

# Parameters:

- `ρ`: [kg/m^3] fluid density at 0Pa reference gage pressure (set by `density` argument)
- `Β`: [Pa] fluid bulk modulus describing the compressibility (set by `bulk_modulus` argument)
- `μ`: [Pa*s] or [kg/m-s] fluid dynamic viscosity (set by `viscosity` argument)
- `n`: density exponent
- `let_gas`: set to 1 to allow fluid to transition from liquid to gas (for density calculation only)
- `let_gas`: set to 1 to allow fluid to transition from liquid to gas (for density calculation only)
- `ρ_gas`: [kg/m^3] density of fluid in gas state at reference gage pressure `p_gas` (set by `gas_density` argument)
- `p_gas`: [Pa] reference pressure (set by `gas_pressure` argument)
"""
Expand Down Expand Up @@ -80,12 +76,12 @@ f_turbulent(shape_factor, Re) = (shape_factor / 64) / (0.79 * log(Re) - 1.64)^2
"""
friction_factor(dm, area, d_h, viscosity, shape_factor)

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
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

- ``Δp``: [Pa] is the pressure difference over the tube length ``l``
- ``\\rho``: [kg/m^3] is the average fluid density
- ``u``: [m/s] is the average fluid velocity
- ``l``: [m] is the tube length
- ``l``: [m] is the tube length

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.

Expand Down
6 changes: 3 additions & 3 deletions src/Thermal/HeatTransfer/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ the component FixedHeatFlow is connected, if parameter `Q_flow` is positive.
end

@equations begin
port.Q_flow ~ ifelse(alpha == 0.0,
-Q_flow, # Simplified equation when alpha is 0
-Q_flow * (1 + alpha * (port.T - T_ref)))
port.Q_flow ~ ifelse(alpha == 0.0,
-Q_flow, # Simplified equation when alpha is 0
-Q_flow * (1 + alpha * (port.T - T_ref)))
end
end

Expand Down
3 changes: 2 additions & 1 deletion src/Thermal/utils.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@connector function HeatPort(; T = nothing, T_guess = 273.15 + 20, Q_flow = nothing, Q_flow_guess = 0.0, name)
@connector function HeatPort(;
T = nothing, T_guess = 273.15 + 20, Q_flow = nothing, Q_flow_guess = 0.0, name)
pars = @parameters begin
T_guess = T_guess
Q_flow_guess = Q_flow_guess
Expand Down
Loading
Loading