Thanks to visit codestin.com
Credit goes to github.com

Skip to content

update model_reduction to allow input/output selection + unstable warning (vs exception) #1074

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

Merged
merged 8 commits into from
Dec 7, 2024

Conversation

murrayrm
Copy link
Member

@murrayrm murrayrm commented Dec 4, 2024

This PR updates the model_reduction function to add some additional functionality. From the docstring:

This function produces a reduced-order model of a system by eliminating specified inputs, outputs, and/or states from the original system. The specific states, inputs, or outputs that are eliminated can be specified by either listing the states, inputs, or outputs to be eliminated or those to be kept.

Specific updates:

  • Changed the ELIM positional parameter to elim_states (positional or keyword).
  • Added keep_states to allow specifying the list of states to keep instead of those to eliminate.
  • Added elim_inputs, elim_outputs, keep_inputs, keep_outputs to allow selection of inputs and outputs.
  • Specifications of states, inputs, and outputs can be done by a list of integers (original implementation) as well as slices or lists of signal names (new).
  • In the case of eliminating states, inputs, or outputs from an unstable system, a warning is issued (versus an exception) and this warning can be suppressed using warn_unstable=False.
  • Removed the copyright info from the top of modelsimp.py since we know just use the LICENSE file.
  • Add unit tests and documentation updates.
  • Cleaned up import order (isort -m2) and removed extraneous spaces.

This PR was motivated by an example from FBS2e (see steering.py), where I wanted to extract the dynamics of a subsystem (and the original system had an eigenvalue at the origin). It is possible to accomplish the same result by using the old implementation (assuming the exception is changed to a warning) and then indexing the system directly to handle the inputs and outputs, but it seemed natural to include in a single function call.

@coveralls
Copy link

coveralls commented Dec 4, 2024

Coverage Status

coverage: 94.756% (+0.07%) from 94.689%
when pulling eff6c40 on murrayrm:modred_unstable-28Nov2024
into 69efbbe on python-control:main.

@slivingston slivingston self-requested a review December 5, 2024 17:06
Copy link
Member

@slivingston slivingston left a comment

Choose a reason for hiding this comment

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

Good improvement! I made several stylistic and misprint-related comments. Else, ready to merge.

eliminated or those to be kept.

Two methods of state reduction are possible: 'truncate' removes the
states marked for elimination, while 'matchdc' replaces the the
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
states marked for elimination, while 'matchdc' replaces the the
states marked for elimination, while 'matchdc' replaces the

Comment on lines 144 to 146
unstable eigenvalues, since in those situations the stability reduced
order model may be different that the stability of the full model. No
other checking is done, so users to be careful not to render a system
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
unstable eigenvalues, since in those situations the stability reduced
order model may be different that the stability of the full model. No
other checking is done, so users to be careful not to render a system
unstable eigenvalues, since in those situations the stability of the reduced
order model may be different than the stability of the full model. No
other checking is done, so users must be careful not to render a system

dico = 'C'
else:
raise NotImplementedError("Function not implemented in discrete time")
States, inputs, and outputs can be specified using integer offers or
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
States, inputs, and outputs can be specified using integer offers or
States, inputs, and outputs can be specified using integer offsets or

({'elim_inputs': [0, 1, 2]}, 5, 3, 0), # no inputs
({'elim_outputs': [0, 1, 2]}, 5, 0, 3), # no outputs
({'elim_states': [0, 1, 2, 3, 4]}, 0, 3, 3), # no states
({'elim_states': [0, 1], 'keep_states': [1, 2]}, None, None, None)
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
({'elim_states': [0, 1], 'keep_states': [1, 2]}, None, None, None)
({'elim_states': [0, 1], 'keep_states': [1, 2]}, None, None, None),

Trailing comma because, then, adding items in the future will not cause a diff on this line, as recommended by PEP 8 and enforced by Black.

See Also
--------
balanced_reduction : Eliminate states using Hankel singular values.
minimal_realization : Eliminate unreachable or unobseravble states.
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
minimal_realization : Eliminate unreachable or unobseravble states.
minimal_realization : Eliminate unreachable or unobservable states.

warnings.warn("System is unstable; reduction may be meaningless")

# Utility function to process keep/elim keywords
def _process_elim_or_keep(elim, keep, labels, allow_both=False):
Copy link
Member

Choose a reason for hiding this comment

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

Any reason to keep allow_both? _process_elim_or_keep() is a function defined only in the scope of model_reduction(), and all calls to it have the default allow_both value (allow_both=False).

method : string
Method of removing states in `ELIM`: either 'truncate' or
'matchdc'.
Method of removing states in `elim`: either 'truncate' or
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
Method of removing states in `elim`: either 'truncate' or
Method of removing inputs, outputs, and/or states: either 'truncate' or

This is confusing because there is no elim parameter and because inputs and outputs can be removed as well, in addition to states.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will update, but this only applies to the way states are removed.


"""
if not isinstance(sys, StateSpace):
raise TypeError("system must be a a StateSpace system")
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
raise TypeError("system must be a a StateSpace system")
raise TypeError("system must be a StateSpace system")

@murrayrm murrayrm merged commit e2c0ff8 into python-control:main Dec 7, 2024
23 checks passed
@murrayrm murrayrm added this to the 0.10.2 milestone Feb 19, 2025
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