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

Hydraulic library updated to use macros #363

Merged
merged 19 commits into from
Mar 17, 2025

Conversation

matthew-kapp
Copy link
Contributor

@matthew-kapp matthew-kapp commented Mar 5, 2025

Checklist

  • Appropriate tests were added | No new functionality
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    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.

Changes include:

- sources.jl
  - MassFlow(; name, p_int)
  - FixedPressure(; p, name)
  - Pressure(; name)
Changes include:

- sources.jl
  - MassFlow(; name, p_int)
  - FixedPressure(; p, name)
  - Pressure(; name)
- components.jl
  - Cap(; p_int, name)
  - Open(; p_int, name)
  -
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
@matthew-kapp
Copy link
Contributor Author

@ven-k Does this commit interfere/conflict with #367 ?

@ven-k
Copy link
Member

ven-k commented Mar 6, 2025

Yes; you could just include all the changed in 367 to this PR or if that merges earlier, rebase this onto the new main

@matthew-kapp
Copy link
Contributor Author

@ven-k #367 and your suggestion implemented here additionally

Copy link
Member

@ven-k ven-k left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.

I have foll. comments:

Comment on lines 13 to 17
@parameters begin
end

@variables begin
end
Copy link
Member

Choose a reason for hiding this comment

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

These are not required; mtkmodel handles that robustly.

Comment on lines 46 to 47
@variables begin
end
Copy link
Member

Choose a reason for hiding this comment

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

This can be dropped.

Comment on lines 70 to 75

@parameters begin
end

systems = @named begin
@variables begin
end
Copy link
Member

Choose a reason for hiding this comment

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

Can be dropped.

.gitignore Outdated
@@ -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
Copy link
Member

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

Comment on lines 12 to 13
@parameters begin
end
Copy link
Member

Choose a reason for hiding this comment

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

Can be dropped. @parameters and @variable blocks aren't compulsory; mtkmodel handles such cases robustly.

systems = @named begin
port = HydraulicPort(;)
@parameters begin
Volume
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Volume
vol

Comment on lines 420 to 421
dm = port.dm
p = port.p
Copy link
Member

@ven-k ven-k Mar 7, 2025

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.

Copy link
Member

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 .

Copy link
Contributor Author

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

area = area
@parameters begin
area
direction = +1
Copy link
Member

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?


ODESystem(eqs, t, vars, pars; name, systems, defaults = [rho => liquid_density(port)])
Copy link
Member

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

Copy link
Contributor Author

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

Comment on lines 365 to 366
x1 = 1
x2 = 1
Copy link
Member

Choose a reason for hiding this comment

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

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.

@matthew-kapp
Copy link
Contributor Author

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

@ven-k
Copy link
Member

ven-k commented Mar 7, 2025

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

@matthew-kapp
Copy link
Contributor Author

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.

@matthew-kapp
Copy link
Contributor Author

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

@matthew-kapp
Copy link
Contributor Author

@ven-k back to 572 tests passed

@matthew-kapp
Copy link
Contributor Author

@ven-k Thanks for all the input - much appreciated. I believe it is ready now for review?

Copy link
Member

@ven-k ven-k left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks.

Formatter failure is related; the test failures are not related.

@ChrisRackauckas ChrisRackauckas merged commit e14e633 into SciML:main Mar 17, 2025
7 of 12 checks passed
@matthew-kapp matthew-kapp deleted the hydraulic-macros branch March 24, 2025 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants