Repository: SciML/ModelingToolkitStandardLibrary.jl
Pull request #363: Hydraulic library updated to use macros
Author: login: matthew-kapp
Created: 2025-03-05T12:03:58Z | Merged: 2025-03-17T07:22:44Z

## Checklist

- [ ] Appropriate tests were added | No new functionality
- [x] Any code changes were done in a way that does not break public API
- [x] All documentation related to code changes were updated
- [x] The new code follows the
  [contributor guidelines](https://github.com/SciML/.github/blob/master/CONTRIBUTING.md), in particular the [SciML Style Guide](https://github.com/SciML/SciMLStyle) and
  [COLPRAC](https://github.com/SciML/COLPRAC).
- [ ] Any new documentation only uses public API | No new documentation
  
## Additional context

Reviewer @ven-k comments implemented

The models in the Hydraulic library were modified to use @mtkmodel macro instead of @component function. The functionality of the models should remain the same.

Only non-breaking changes were made. Changes include:

- sources.jl
  - MassFlow
  - FixedPressure
  - Pressure
- components.jl
  - Cap
  - Open
  - FlowDivider
  - VolumeBase
  - FixedVolume
  - Volume

The following were NOT changed, as the changes would be breaking:

- components.jl
  - TubeBase
  - Tube
  - ValveBase
  - Valve
  - DynamicVolume
  - SpoolValve
  - SpoolValve2Way
  - Actuator

Additionally, the docstrings were altered to remove P_int and its derivatives, which are no longer used.

The package was tested and only the original fail of VariableResistor occurs.


Diff:
diff --git a/src/Hydraulic/IsothermalCompressible/components.jl b/src/Hydraulic/IsothermalCompressible/components.jl
index be891ee4c..f7512bc48 100644
--- a/src/Hydraulic/IsothermalCompressible/components.jl
+++ b/src/Hydraulic/IsothermalCompressible/components.jl
@@ -10,17 +10,21 @@ Caps a hydraulic port to prevent mass flow in or out.
 # Connectors:
 - `port`: hydraulic port
 """
-@component function Cap(; name)
-    vars = @variables p(t), [guess = 0]
+@mtkmodel Cap begin
 
-    systems = @named begin
+    @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
 
 """
@@ -34,22 +38,22 @@ Provides an "open" boundary condition for a hydraulic port such that mass flow `
 # Connectors:
 - `port`: hydraulic port
 """
-@component function Open(; name)
-    pars = []
+@mtkmodel Open begin
 
-    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
 
 """
@@ -238,33 +242,33 @@ Reduces the flow from `port_a` to `port_b` by `n`.  Useful for modeling parallel
 - `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(
@@ -357,18 +361,23 @@ 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
-        area = area
-        dead_volume = dead_volume
+@mtkmodel VolumeBase begin
+
+    @structural_parameters begin
+        Χ1 = 1
+        Χ2 = 1
     end
 
-    systems = @named begin
+    @parameters begin
+        area
+        dead_volume
+    end
+
+    @components begin
         port = HydraulicPort()
     end
 
-    vars = @variables begin
+    @variables begin
         x(t)
         dx(t), [guess = 0]
         rho(t), [guess = liquid_density(port)]
@@ -376,17 +385,14 @@ end
         vol(t)
     end
 
-    # let
-    dm = port.dm
-    p = port.p
-
-    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
+        vol ~ dead_volume + area * x
+        D(x) ~ dx
+        D(rho) ~ drho
+        rho ~ full_density(port, port.p)
+        port.dm ~ drho * vol * Χ1 + rho * area * dx * Χ2
+    end
 
-    ODESystem(eqs, t, vars, pars; name, systems)
 end
 
 """
@@ -400,33 +406,31 @@ Fixed fluid volume.
 # Connectors:
 - `port`: hydraulic port
 """
-@component function FixedVolume(; vol, name)
-    pars = @parameters begin
-        vol = vol
+@mtkmodel FixedVolume begin
+
+    @parameters begin
+        vol
     end
 
-    systems = @named begin
+    @components begin
         port = HydraulicPort(;)
     end
 
-    vars = @variables begin
+    @variables begin
         rho(t), [guess = liquid_density(port)]
         drho(t), [guess = 0]
     end
 
-    # let
-    dm = port.dm
-    p = port.p
-
-    eqs = [D(rho) ~ drho
-           rho ~ full_density(port, p)
-           dm ~ drho * vol]
+    @equations begin
+        D(rho) ~ drho
+        rho ~ full_density(port, port.p)
+        port.dm ~ drho * vol
+    end
 
-    ODESystem(eqs, t, vars, pars; name, systems)
 end
 
 """
-    Volume(; x, dx=0, p, drho=0, dm=0, area, direction = +1, name)
+    Volume(; x, dx=0, p, drho=0, dm=0, area, direction = 1, name)
 
 Volume with moving wall with `flange` connector for converting hydraulic energy to 1D mechanical.  The `direction` argument aligns the mechanical port with the hydraulic port, useful when connecting two dynamic volumes together in oppsing directions to create an actuator.
 
@@ -463,16 +467,17 @@ dm ────►               │  │ area
 
 See also [`FixedVolume`](@ref), [`DynamicVolume`](@ref)
 """
-@component function Volume(;
+@mtkmodel Volume begin
 
-        #parameters
-        area,
-        direction = +1, name)
-    pars = @parameters begin
-        area = area
+    @structural_parameters begin
+        direction = 1
     end
 
-    vars = @variables begin
+    @parameters begin
+        area
+    end
+
+    @variables begin
         x(t)
         dx(t)
         p(t)
@@ -482,32 +487,36 @@ See also [`FixedVolume`](@ref), [`DynamicVolume`](@ref)
         dm(t)
     end
 
-    systems = @named begin
+    @components begin
         port = HydraulicPort()
         flange = MechanicalPort()
     end
 
-    eqs = [
-           # connectors
-           port.p ~ p
-           port.dm ~ dm
-           flange.v * direction ~ dx
-           flange.f * direction ~ -f
+    @equations begin
+        # connectors
+        port.p ~ p
+        port.dm ~ dm
+        flange.v * direction ~ dx
+        flange.f * direction ~ -f
 
-           # differentials
-           D(x) ~ dx
-           D(rho) ~ drho
+        # differentials
+        D(x) ~ dx
+        D(rho) ~ drho
 
-           # physics
-           rho ~ liquid_density(port, p)
-           f ~ p * area
-           dm ~ drho * x * area + rho * dx * area]
+        # physics
+        rho ~ liquid_density(port, p)
+        f ~ p * area
+        dm ~ drho * x * area + rho * dx * area
+    end
+
+    @defaults begin
+        rho => liquid_density(port)
+    end
 
-    ODESystem(eqs, t, vars, pars; name, systems, defaults = [rho => liquid_density(port)])
 end
 
 """
-    DynamicVolume(N, add_inertia=true; p_int,  area, x_int = 0, x_max, x_min = 0, x_damp = x_min, direction = +1, perimeter = 2 * sqrt(area * pi), shape_factor = 64, head_factor = 1, Cd = 1e2, Cd_reverse = Cd, name)
+    DynamicVolume(N, add_inertia=true, reversible=false; area, x_int = 0, x_max, x_min = 0, x_damp = x_min, direction = +1, perimeter = 2 * sqrt(area * pi), shape_factor = 64, head_factor = 1, Cd = 1e2, Cd_reverse = Cd, name)
 
 Volume with moving wall with `flange` connector for converting hydraulic energy to 1D mechanical.  The `direction` argument aligns the mechanical port with the hydraulic port, useful when connecting two dynamic volumes together in oppsing directions to create an actuator.
 
diff --git a/src/Hydraulic/IsothermalCompressible/sources.jl b/src/Hydraulic/IsothermalCompressible/sources.jl
index ee21959d7..64b182f11 100644
--- a/src/Hydraulic/IsothermalCompressible/sources.jl
+++ b/src/Hydraulic/IsothermalCompressible/sources.jl
@@ -8,20 +8,16 @@ Hydraulic mass flow input source
   - `port`: hydraulic port
   - `dm`: real input 
 """
-@component function MassFlow(; name)
-    pars = []
+@mtkmodel MassFlow begin
 
-    systems = @named begin
+    @components begin
         port = HydraulicPort()
         dm = RealInput()
     end
 
-    vars = []
-    eqs = [
+    @equations begin
         port.dm ~ -dm.u
-    ]
-
-    ODESystem(eqs, t, vars, pars; name, systems)
+    end
 end
 
 """
@@ -35,22 +31,20 @@ Fixed pressure source
 # Connectors:
 - `port`: hydraulic port
 """
-@component function FixedPressure(; p, name)
-    pars = @parameters begin
-        p = p
-    end
+@mtkmodel FixedPressure begin
 
-    vars = []
+    @parameters begin
+        p
+    end
 
-    systems = @named begin
+    @components begin
         port = HydraulicPort()
     end
 
-    eqs = [
+    @equations begin
         port.p ~ p
-    ]
+    end
 
-    ODESystem(eqs, t, vars, pars; name, systems)
 end
 @deprecate Source FixedPressure
 
@@ -63,19 +57,16 @@ input pressure source
 - `port`: hydraulic port
 - `p`: real input 
 """
-@component function Pressure(; name)
-    pars = []
-    vars = []
+@mtkmodel Pressure begin
 
-    systems = @named begin
+    @components begin
         port = HydraulicPort()
         p = RealInput()
     end
 
-    eqs = [
+    @equations begin
         port.p ~ p.u
-    ]
+    end
 
-    ODESystem(eqs, t, vars, pars; name, systems)
 end
 @deprecate InputSource Pressure


Reviews:
user: login: ven-k
body: Thanks for the PR.

I have foll. comments:
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-03-07T07:15:42Z
user: login: matthew-kapp
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-07T10:08:22Z
user: login: ven-k
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-03-07T13:49:18Z
user: login: matthew-kapp
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-07T13:56:11Z
user: login: ven-k
body: LGTM. Thanks.

Formatter failure is related; the test failures are not related. 
state: APPROVED
author_association: MEMBER
submitted_at: 2025-03-17T06:03:20Z

Review comments:
diff_hunk: @@ -8,20 +8,22 @@ Hydraulic mass flow input source
   - `port`: hydraulic port
   - `dm`: real input 
 """
-@component function MassFlow(; name)
-    pars = []
+@mtkmodel MassFlow begin
+    
+    @parameters begin
+    end
+
+    @variables begin
+    end
path: src/Hydraulic/IsothermalCompressible/sources.jl
user: login: ven-k
body: These are not required; mtkmodel handles that robustly.
created_at: 2025-03-07T06:52:49Z
author_association: MEMBER
diff_hunk: @@ -35,22 +37,23 @@ Fixed pressure source
 # Connectors:
 - `port`: hydraulic port
 """
-@component function FixedPressure(; p, name)
-    pars = @parameters begin
-        p = p
+@mtkmodel FixedPressure begin
+
+    @parameters begin
+        p
     end
 
-    vars = []
+    @variables begin
+    end
path: src/Hydraulic/IsothermalCompressible/sources.jl
user: login: ven-k
body: This can be dropped.
created_at: 2025-03-07T06:53:16Z
author_association: MEMBER
diff_hunk: @@ -63,19 +66,22 @@ input pressure source
 - `port`: hydraulic port
 - `p`: real input 
 """
-@component function Pressure(; name)
-    pars = []
-    vars = []
+@mtkmodel Pressure begin
+
+    @parameters begin
+    end
 
-    systems = @named begin
+    @variables begin
+    end
path: src/Hydraulic/IsothermalCompressible/sources.jl
user: login: ven-k
body: Can be dropped.
created_at: 2025-03-07T06:53:43Z
author_association: MEMBER
diff_hunk: @@ -21,4 +21,4 @@ docs/site/
 # It records a fixed state of all packages used by the project. As such, it should not be
 # committed for packages, but should be committed for applications that require a static
 # environment.
-Manifest.toml
+Manifest.toml
path: .gitignore
user: login: ven-k
body: Needs a new line at the end (to keep Git-diff rendering happy).
created_at: 2025-03-07T06:54:26Z
author_association: MEMBER
diff_hunk: @@ -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
path: src/Hydraulic/IsothermalCompressible/components.jl
user: login: ven-k
body: Can be dropped. `@parameters` and `@variable` blocks aren't compulsory; mtkmodel handles such cases robustly.
created_at: 2025-03-07T06:55:22Z
author_association: MEMBER
diff_hunk: @@ -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
path: src/Hydraulic/IsothermalCompressible/components.jl
user: login: ven-k
body: Can be dropped
created_at: 2025-03-07T06:55:35Z
author_association: MEMBER
diff_hunk: @@ -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
path: src/Hydraulic/IsothermalCompressible/components.jl
user: login: ven-k
body: This can be added to a 
```julia
begin
...
end
```
(outside `@equations`)
As dm and p are not defined as variables, these equations will throw errors.
created_at: 2025-03-07T06:57:51Z
author_association: MEMBER
diff_hunk: @@ -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
path: src/Hydraulic/IsothermalCompressible/components.jl
user: login: ven-k
body: Typo: 
```suggestion
        dm ~ drho * vol * x1 + rho * area * dx * x2
```
created_at: 2025-03-07T06:58:31Z
author_association: MEMBER
diff_hunk: @@ -400,29 +401,29 @@ Fixed fluid volume.
 # Connectors:
 - `port`: hydraulic port
 """
-@component function FixedVolume(; vol, name)
-    pars = @parameters begin
-        vol = vol
-    end
+@mtkmodel FixedVolume begin
 
-    systems = @named begin
-        port = HydraulicPort(;)
+    @parameters begin
+        Volume
path: src/Hydraulic/IsothermalCompressible/components.jl
user: login: ven-k
body: ```suggestion
        vol
```

created_at: 2025-03-07T06:59:35Z
author_association: MEMBER
diff_hunk: @@ -400,29 +401,29 @@ Fixed fluid volume.
 # Connectors:
 - `port`: hydraulic port
 """
-@component function FixedVolume(; vol, name)
-    pars = @parameters begin
-        vol = vol
-    end
+@mtkmodel FixedVolume begin
 
-    systems = @named begin
-        port = HydraulicPort(;)
+    @parameters begin
+        Volume
     end
 
-    vars = @variables begin
+    @variables begin
         rho(t), [guess = liquid_density(port)]
         drho(t), [guess = 0]
     end
 
-    # let
-    dm = port.dm
-    p = port.p
+    @components begin
+        port = HydraulicPort()
+    end
 
-    eqs = [D(rho) ~ drho
-           rho ~ full_density(port, p)
-           dm ~ drho * vol]
+    @equations begin
+        dm = port.dm
+        p = port.p
path: src/Hydraulic/IsothermalCompressible/components.jl
user: login: ven-k
body: ~This can be added to a~
```
begin
...
end
```
~(outside `@equations`)~

~As dm and p are not defined as variables, these equations will throw errors.~
created_at: 2025-03-07T07:00:26Z
author_association: MEMBER
diff_hunk: @@ -463,16 +464,14 @@ dm ────►               │  │ area
 
 See also [`FixedVolume`](@ref), [`DynamicVolume`](@ref)
 """
-@component function Volume(;
+@mtkmodel Volume begin
 
-        #parameters
-        area,
-        direction = +1, name)
-    pars = @parameters begin
-        area = area
+    @parameters begin
+        area
+        direction = +1
path: src/Hydraulic/IsothermalCompressible/components.jl
user: login: ven-k
body: `direction` could be of type `@structurtal_parameters`.

Is this intentionally promoted to a `@parameters`?

While here, could you change +1 to 1?
created_at: 2025-03-07T07:03:06Z
author_association: MEMBER
diff_hunk: @@ -482,32 +481,33 @@ See also [`FixedVolume`](@ref), [`DynamicVolume`](@ref)
         dm(t)
     end
 
-    systems = @named begin
+    @components begin
         port = HydraulicPort()
         flange = MechanicalPort()
     end
 
-    eqs = [
-           # connectors
-           port.p ~ p
-           port.dm ~ dm
-           flange.v * direction ~ dx
-           flange.f * direction ~ -f
+    @equations begin
+        # connectors
+        port.p ~ p
+        port.dm ~ dm
+        flange.v * direction ~ dx
+        flange.f * direction ~ -f
 
-           # differentials
-           D(x) ~ dx
-           D(rho) ~ drho
+        # differentials
+        D(x) ~ dx
+        D(rho) ~ drho
+
+        # physics
+        rho ~ liquid_density(port, p)
+        f ~ p * area
+        dm ~ drho * x * area + rho * dx * area
+    end
 
-           # physics
-           rho ~ liquid_density(port, p)
-           f ~ p * area
-           dm ~ drho * x * area + rho * dx * area]
 
-    ODESystem(eqs, t, vars, pars; name, systems, defaults = [rho => liquid_density(port)])
path: src/Hydraulic/IsothermalCompressible/components.jl
user: login: ven-k
body: `defaults = [rho => liquid_density(port)]`

can be added via the `@defaults` block https://docs.sciml.ai/ModelingToolkit/stable/basics/MTKLanguage/#@defaults-begin-block
created_at: 2025-03-07T07:12:02Z
author_association: MEMBER
diff_hunk: @@ -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
path: src/Hydraulic/IsothermalCompressible/components.jl
user: login: ven-k
body: x1 x2 were `@structural_parameters`. These could be just `@parameters`. However, that would need fixing these scoping here: https://github.com/SciML/ModelingToolkitStandardLibrary.jl/blob/main/src/Hydraulic/IsothermalCompressible/components.jl#L648-L650 in DynamicVolume

Probably adding them as structural_parameters is easier.
created_at: 2025-03-07T07:15:07Z
author_association: MEMBER
diff_hunk: @@ -482,32 +481,33 @@ See also [`FixedVolume`](@ref), [`DynamicVolume`](@ref)
         dm(t)
     end
 
-    systems = @named begin
+    @components begin
         port = HydraulicPort()
         flange = MechanicalPort()
     end
 
-    eqs = [
-           # connectors
-           port.p ~ p
-           port.dm ~ dm
-           flange.v * direction ~ dx
-           flange.f * direction ~ -f
+    @equations begin
+        # connectors
+        port.p ~ p
+        port.dm ~ dm
+        flange.v * direction ~ dx
+        flange.f * direction ~ -f
 
-           # differentials
-           D(x) ~ dx
-           D(rho) ~ drho
+        # differentials
+        D(x) ~ dx
+        D(rho) ~ drho
+
+        # physics
+        rho ~ liquid_density(port, p)
+        f ~ p * area
+        dm ~ drho * x * area + rho * dx * area
+    end
 
-           # physics
-           rho ~ liquid_density(port, p)
-           f ~ p * area
-           dm ~ drho * x * area + rho * dx * area]
 
-    ODESystem(eqs, t, vars, pars; name, systems, defaults = [rho => liquid_density(port)])
path: src/Hydraulic/IsothermalCompressible/components.jl
user: login: matthew-kapp
body: Is this correct?

```julia
    @defaults begin
        rho => liquid_density(port)
    end
```
created_at: 2025-03-07T10:08:22Z
author_association: CONTRIBUTOR
diff_hunk: @@ -400,29 +401,29 @@ Fixed fluid volume.
 # Connectors:
 - `port`: hydraulic port
 """
-@component function FixedVolume(; vol, name)
-    pars = @parameters begin
-        vol = vol
-    end
+@mtkmodel FixedVolume begin
 
-    systems = @named begin
-        port = HydraulicPort(;)
+    @parameters begin
+        Volume
     end
 
-    vars = @variables begin
+    @variables begin
         rho(t), [guess = liquid_density(port)]
         drho(t), [guess = 0]
     end
 
-    # let
-    dm = port.dm
-    p = port.p
+    @components begin
+        port = HydraulicPort()
+    end
 
-    eqs = [D(rho) ~ drho
-           rho ~ full_density(port, p)
-           dm ~ drho * vol]
+    @equations begin
+        dm = port.dm
+        p = port.p
path: src/Hydraulic/IsothermalCompressible/components.jl
user: login: ven-k
body: 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` . 
created_at: 2025-03-07T13:49:18Z
author_association: MEMBER
diff_hunk: @@ -400,29 +401,29 @@ Fixed fluid volume.
 # Connectors:
 - `port`: hydraulic port
 """
-@component function FixedVolume(; vol, name)
-    pars = @parameters begin
-        vol = vol
-    end
+@mtkmodel FixedVolume begin
 
-    systems = @named begin
-        port = HydraulicPort(;)
+    @parameters begin
+        Volume
     end
 
-    vars = @variables begin
+    @variables begin
         rho(t), [guess = liquid_density(port)]
         drho(t), [guess = 0]
     end
 
-    # let
-    dm = port.dm
-    p = port.p
+    @components begin
+        port = HydraulicPort()
+    end
 
-    eqs = [D(rho) ~ drho
-           rho ~ full_density(port, p)
-           dm ~ drho * vol]
+    @equations begin
+        dm = port.dm
+        p = port.p
path: src/Hydraulic/IsothermalCompressible/components.jl
user: login: matthew-kapp
body: This seems simplest?

```julia
    @equations begin
        D(rho) ~ drho
        rho ~ full_density(port, port.p)
        port.dm ~ drho * vol
    end
```
created_at: 2025-03-07T13:56:11Z
author_association: CONTRIBUTOR

PR comments:
user: login: matthew-kapp
created_at: 2025-03-06T06:49:12Z
body: @ven-k Does this commit interfere/conflict with #367 ?
author_association: CONTRIBUTOR
user: login: ven-k
created_at: 2025-03-06T12:01:00Z
body: Yes; you could just include all the changed in 367 to this PR or if that merges earlier, rebase this onto the new main
author_association: MEMBER
user: login: matthew-kapp
created_at: 2025-03-06T13:20:47Z
body: @ven-k #367 and your suggestion implemented here additionally
author_association: CONTRIBUTOR
user: login: matthew-kapp
created_at: 2025-03-07T10:20:26Z
body: > Thanks for the PR.
> 
> I have foll. comments:

@ven-k comments implemented. Everything exactly as you said. Only thing I am unsure about is default, which is implemented as follows:

    @defaults begin
        rho => liquid_density(port)
    end
author_association: CONTRIBUTOR
user: login: ven-k
created_at: 2025-03-07T13:58:09Z
body: Thanks for all the updates.
`@defaults` looks correct.

I see that there are few Hydraulic test failures.
1. It appears `begin...end` doesn't work while accessing values from subcomponents due to scoping issue. My bad. 
Alternative fix would be to promote dm and p as variables. (Without this promoting them to equations will fail) https://github.com/SciML/ModelingToolkitStandardLibrary.jl/pull/363/files#r1985093021
2. VolumeBase had `X1` and `X2` as args; which is now changed to `x1` and `x2` in some but not all places. That should be fixed. (Changing all mentions to x1, x2 would be preferable.)
author_association: MEMBER
user: login: matthew-kapp
created_at: 2025-03-07T14:09:51Z
body: > Thanks for all the updates. `@defaults` looks correct.
> 
> I see that there are few Hydraulic test failures.
> 
> 1. It appears `begin...end` doesn't work while accessing values from subcomponents due to scoping issue. My bad.
>    Alternative fix would be to promote dm and p as variables. (Without this promoting them to equations will fail) https://github.com/SciML/ModelingToolkitStandardLibrary.jl/pull/363/files#r1985093021
> 2. VolumeBase had `X1` and `X2` as args; which is now changed to `x1` and `x2` in some but not all places. That should be fixed. (Changing all mentions to x1, x2 would be preferable.)

Apologies - I should have tested this locally.

I would have never ever realized that X is different to symbol(X). Thanks @ven-k for pointing this out.
author_association: CONTRIBUTOR
user: login: matthew-kapp
created_at: 2025-03-07T18:25:16Z
body: @ven-k thanks for the input. I apologize, it's 100% breaking in the tests. I'll need to take my time to fix and debug.
author_association: CONTRIBUTOR
user: login: matthew-kapp
created_at: 2025-03-11T14:24:57Z
body: @ven-k back to 572 tests passed
author_association: CONTRIBUTOR
user: login: matthew-kapp
created_at: 2025-03-14T10:52:22Z
body: @ven-k Thanks for all the input - much appreciated. I believe it is ready now for review?
author_association: CONTRIBUTOR