-
Notifications
You must be signed in to change notification settings - Fork 562
Define an as_quantity() to evaluate a Pyomo expression to a pint Quantity
#2222
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
Conversation
Codecov Report
@@ Coverage Diff @@
## main #2222 +/- ##
==========================================
+ Coverage 82.74% 82.77% +0.02%
==========================================
Files 607 607
Lines 76792 76843 +51
==========================================
+ Hits 63544 63608 +64
+ Misses 13248 13235 -13
Continue to review full report at Codecov.
|
| } | ||
|
|
||
| def as_quantity(expr): | ||
| return _QuantityVisitor().dfs_postorder_stack(expr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a comment, not a required change - should we standardize this at some point to change all instances that use dfs_postorder_stack to walk_expression?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually? Yes. But that will require reworking the ExpressionValueVisitor to be based on the StreamBasedExpressionVisitor.
carldlaird
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks reasonable to me.
| return "(" + _str + ")" | ||
| else: | ||
| return str(self) | ||
| return _str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this mostly for debugging, or is it used somewhere else?
| if hasattr(node, 'get_units'): | ||
| unit = node.get_units() | ||
| if unit is not None: | ||
| return True, value(node) * unit._pint_unit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how this works. If a Var has units that are themselves an expression (e.g., kg*m), then that expression won't have "_pint_unit"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After discussions with @jsiirola I guess we create compound units instead of keeping the expression. Let's make sure there are tests that include Vars with units that are expressions.
carldlaird
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes a broader question for the team. This PR is officially exposing pint from behind the Pyomo units. Do we want to do this?
I have no real concerns, however, we did quite a bit of work to make sure that pint was abstracted away (so we could replace pint with something else later if we wanted to). This decision may have been more about being unfamiliar with pint and not "ready to commit" to it. If we are ready to commit to pint, then there may be other simplifications we can make.
Just thoughts for discussion.
|
@carldlaird we discussed your question at the developers meeting this week and the consensus was that we didn't have any concerns with exposing more of pint. Given the currently available alternatives to pint we think the likelihood of replacing it with something else is pretty slim. |
Fixes # .
Summary/Motivation:
This PR adds a
as_quantity()function that will accept numeric-like objects (int / float / None / Var / Param / expression / etc) and return the value as a nativepintQuantity(value + units).This is useful for applications like user interfaces, and was requested by IDAES (@andrewlee94 and @dangunter)
Changes proposed in this PR:
as_quantity()function to evaluate an expression to a pint Quantityunits_container)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: