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

Symbolic defaults for sub-components and base system kwargs + set base sys kwargs as component's kwargs #2226

Merged
merged 4 commits into from
Sep 4, 2023

Conversation

ven-k
Copy link
Member

@ven-k ven-k commented Aug 14, 2023

  1. Kwargs of sub-components and base systems (in extend) can now have symbolic defaults. This allows scenarios like:
@mtkmodel A begin
    @parameters begin
            a1 = 1
    end
    @components begin
           bb = B(; b1 = a1)
    end
end

Earlier, this would be A(; a1 = nothing, B.b1 = a1), where b1 would have default value set as a1 arguement.
Now, it is A(; a1 = nothing, B.b1 = nothing) and internally, default value of b1 is set to parameter a1.

  1. While extending components, the kwargs of base system are promoted as kwargs of the main sys.
@mtkmodel D begin
     @extend e1, e2 = ee = E(; ek)
end

Earlier D would have a kwarg ee__ek (as if ee was a subcomponent). Now D will have ek as the kwarg. This avoids having to access partial components of a model to set parameters.

The model-parsing tests are updated to cover both the above changes.

Copy link

@ai-maintainer ai-maintainer bot left a comment

Choose a reason for hiding this comment

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

AI-Maintainer Review for PR - Symbolic defaults for sub-components and base system kwargs + set base sys kwargs as component's kwargs

Title and Description 👍

The Title and Description are clear and concise
The title and description of the pull request are clear and concise. They effectively communicate the purpose of the changes, which is to introduce symbolic defaults for sub-components and base system kwargs, and to set base sys kwargs as component's kwargs. The description provides examples and explains how the new functionality works.

Scope of Changes 👍

The changes are narrowly focused
The changes appear to be narrowly focused on addressing specific issues related to the handling of kwargs in sub-components and base systems. The author is not trying to resolve multiple issues simultaneously but rather introducing new functionality and improving the behavior of the code in a targeted manner.

Documentation and Comments 👍

All new functions and methods have appropriate docstrings
All newly added functions, classes, or methods have docstrings describing their behavior, arguments, and return values. There are no functions, classes, or methods in the provided diff that need docstrings.

Testing 👎

Testing details are missing
The description does not mention how the author tested the changes. It would be helpful for the author to include information about the testing approach and any specific test cases used to verify the changes.

Suggested Changes

  • Please provide details on how the changes were tested. This could include the specific test cases used to verify the changes and any testing frameworks used.

Reviewed with AI Maintainer

@ven-k
Copy link
Member Author

ven-k commented Aug 14, 2023

@ven-k
Copy link
Member Author

ven-k commented Aug 14, 2023

The Ai4EComponentLib, StructuralIdentifiability integration test failures are unrelated to the changes introduced here.

MTK test failure and MSL integration test failure is fixed by SciML/ModelingToolkitStandardLibrary.jl#209. However that needs this to be merged and released.

This is ready for review @YingboMa @ChrisRackauckas.

@baggepinnen
Copy link
Contributor

In your first example, could the default value for b1 be an expression (not just a parameter), like

B(; b1 = a1^2)

?

Thanks for this PR, I have been missing this feature :)

@ven-k
Copy link
Member Author

ven-k commented Aug 21, 2023

Expressions without conditional part work. (I'm working on the latter in a follow up PR)

So B(; b1 = a1^2) gives:

julia> @named aa = A(; a1 = 1)
Model aa with 0 equations
States (0):
Parameters (2):
  a1 [defaults to 1]
  bb₊b1 [defaults to a1^2]

julia> getdefault(aa.bb.b1)
a1^2

Something like B(; b1 = true ? a1 : 1) doesn't work yet.

@ven-k ven-k force-pushed the vkb/extend+dsl-component-defaults branch 2 times, most recently from 8763cd0 to a7fc669 Compare August 29, 2023 18:54
@ven-k
Copy link
Member Author

ven-k commented Aug 29, 2023

Additional to above changes, a new @structural_parameters is added to @mtkmodel. This allows arguments that aren't either parameters or variables, aka boolean flags, functions etc...

@mtkmodel A begin
    @structural_parameters begin
         a = 1
         b
    end
    @parameters begin
        c = 0
    end
end

A's arg list will be: A(; name, a = 1, b, c = nothing).

@YingboMa I've updated the tests to include this and rebased on current master. So this PR is ready for review.

@ven-k ven-k force-pushed the vkb/extend+dsl-component-defaults branch 2 times, most recently from 5d6e27f to 4e2549c Compare August 29, 2023 19:21
@ven-k
Copy link
Member Author

ven-k commented Aug 29, 2023

… of depending on the numerical value of it

- this change allows user to set a parameter with initial value and pass that as default value of kwargs of sub components.
@ven-k ven-k force-pushed the vkb/extend+dsl-component-defaults branch from 4e2549c to c62f84f Compare September 1, 2023 08:09
- This will allow to provide arguments that aren't parameters/variables like int, function...
- Adds tests for the same
@ven-k ven-k force-pushed the vkb/extend+dsl-component-defaults branch from c62f84f to a9fa244 Compare September 1, 2023 08:23
@ven-k
Copy link
Member Author

ven-k commented Sep 1, 2023

The branch is rebased wrt to master.

And note that any test involving MSL fail for now and will need

*To test locally use url = "https://github.com/ven-k/ModelingToolkitStandardLibrary.jl", rev = "vkb/extend-kwargs"

@YingboMa YingboMa merged commit 61e12ba into SciML:master Sep 4, 2023
23 of 33 checks passed
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