Repository navigation
Simplify arc creation through connect_to method on port - #3921
avdudchenko wants to merge 14 commits into
Conversation
|
At the dev call we discussed splitting this PR in half, keeping the We tossed around an idea of exploring an alternate implementation of get_connections by creating a utility (class) that would take a model and interrogate all the Arcs to collect the full port/arc connectivity graph. That object could then support the get_connections() (and potentially other logic like stream tearing), We are adopting a new review process where we convert PRs that are "waiting on the author" back to "draft" (to signal the PR state to both the author and the dev team). Please use the "Ready for review" button to signal the developers when it is time to get the PR back into the review queue. |
|
removed the get_connections method, and updated the connect_to method to also accept a name in case user wants to name it manually, since we do not provide to access the arc, the function now returns the created arc. |
|
@avdudchenko could you run |
|
@avdudchenko - Hello hello! Can you please run |
jsiirola
left a comment
There was a problem hiding this comment.
Some comments on the implementation are below, but I have a more fundamental question as to the motivation for this functionality. Apart from some "semantic sugar", does this provide anything that we can't already do with indexed Arcs and/or add_component? Does that semantic sugar warrant the increased technical debt?
| block = self.parent_block() | ||
| if block is None: | ||
| raise ValueError( | ||
| "Cannot connect Port '%s' to Port '%s' because neither port has a parent block. Please specify a block to construct the Arc on." |
There was a problem hiding this comment.
This message does not quite match the code: the code only checks the source port (not the destination) and the user-specified block.
There was a problem hiding this comment.
Fixed it by referring to primary port explicitly.
| arc_name = f"{current_port_name}_to_{dest_port_name}" | ||
|
|
||
| block.add_component(arc_name, Arc(source=self, destination=port)) | ||
| # verify its created as add_component can fail silently |
There was a problem hiding this comment.
I do not understand this comment. I don't see a code path where add_component fails without raising an exception.
There was a problem hiding this comment.
If the string name is not a valid python name - it will not fail (or at least it used to not fail (e.g. m.add_component('$invalid$,name", Var) will not raise an error. For example:
import pyomo.environ as pyo
m = pyo.ConcreteModel()
bad_names = ["my var", "1x", "a.b", "x[1]", "a-b", "class", ""]
for name in bad_names:
try:
m.add_component(name, pyo.Var(initialize=1.0))
print(f"add_component({name!r:10}) -> accepted silently, isidentifier={name.isidentifier()}")
except Exception as e:
print(f"add_component({name!r:10}) -> raised {type(e).__name__}: {e}")
print("\nComponents on model:")
for c in m.component_objects(pyo.Var, descend_into=False):
print(f" name={c.name!r:12} local_name={c.local_name!r}")
print("\nConsequences:")
print(" getattr works only via string:", getattr(m, "my var").value)
for name in ["a.b", "x[1]"]:
print(f" find_component({name!r}) -> {m.find_component(name)!r} (expected the Var named {name!r})")Output:
add_component('my var' ) -> accepted silently, isidentifier=False
add_component('1x' ) -> accepted silently, isidentifier=False
add_component('a.b' ) -> accepted silently, isidentifier=False
add_component('x[1]' ) -> accepted silently, isidentifier=False
add_component('a-b' ) -> accepted silently, isidentifier=False
add_component('class' ) -> accepted silently, isidentifier=True
add_component('' ) -> accepted silently, isidentifier=False
...
find_component('a.b') -> None (expected the Var named 'a.b')
find_component('x[1]') -> None (expected the Var named 'x[1]')
This is really just meant to make it easier to build models and connect things by abstracting away manual arc creation and management. I think small amount of technical debt can be worth it for cleaner/easier to use API... although with age of AI coming maybe I am wrong... If you think this does not belong in pyomo we can close the PR, but I would push to ward making friendlier/easier to use API especially if it does not add complex technical debt. |
Fixes # .
connect_tomethod to port to simplify arc creation in modelsSummary/Motivation:
Currently in many models we use Arcs to connect multiple units/blocks together. In most IDAES/WaterTAP flowsheets or models this becomes very tedious as user must create, name, and track arcs as well as manually propagate them.
For example below shows an example, but one key thing to remember is in real flowsheets. the location of arc names and definitions would be separated by a hundreds of lines of code between arc creation and actual propagation occurs (example here:
arc creation and arc propagation by which point user probably forgot what the actual arcs are for and where they are connected (especially new users or developers)
The
connect_tomethod is intended to simplify arc creation and management work flow where user can simply connect two ports with simpleconnect_tocall. This is not intended to replace Arc method, but be a parallel approach to connecting ports.New approach
Example of this workflow can be seen here
The objective of this PR is start a discussion on this workflow and get feedback on issues that might need to be addressed before implementation.
Some known concerns:
ArcimportPortmethod, to circumvent this, connect_to import Arc at call. This is not ideal but impossible to address with out major reorganization of Arc or Port methods. I also do not wish to create a new "Port" object so we do not have to rebuild upstream models to access this method.Changes proposed in this PR:
Legal Acknowledgement
By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution: