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

Scaler for equilibrium reactor and saponification properties #1500

Merged
merged 84 commits into from
Oct 22, 2024

Conversation

andrewlee94
Copy link
Member

Part of new scaling deployment

Summary/Motivation:

This PR adds a new scaler for the Equilibrium Reactor unit model and the saponification property packages. It also fixes a couple of bugs found whilst writing these.

Changes proposed in this PR:

  • New EquilibriumReactorScaler
  • New SaponificationThermoScaler
  • New SaponificationReactionScaler

Legal Acknowledgement

By contributing to this software project, I agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the license terms described in the LICENSE.txt file at the top level of this directory.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

@codecov-commenter
Copy link

codecov-commenter commented Oct 11, 2024

Codecov Report

Attention: Patch coverage is 82.35294% with 15 lines in your changes missing coverage. Please review.

Project coverage is 76.99%. Comparing base (7369728) to head (cd82ad8).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ls/properties/examples/saponification_reactions.py 61.53% 4 Missing and 6 partials ⚠️
idaes/models/unit_models/equilibrium_reactor.py 89.28% 0 Missing and 3 partials ⚠️
idaes/core/base/property_base.py 83.33% 1 Missing ⚠️
idaes/core/base/reaction_base.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1500   +/-   ##
=======================================
  Coverage   76.98%   76.99%           
=======================================
  Files         382      382           
  Lines       61911    61993   +82     
  Branches    10126    10146   +20     
=======================================
+ Hits        47663    47730   +67     
- Misses      11848    11856    +8     
- Partials     2400     2407    +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@dallan-keylogic dallan-keylogic left a comment

Choose a reason for hiding this comment

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

Looks good for a first pass.

else:
# Hopefully temperature has been scaled, so we can get the nominal value of k_rxn
# by walking the expression in the constraint.
nominals = self.get_expression_nominal_values(model.arrhenius_eqn)
Copy link
Contributor

Choose a reason for hiding this comment

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

Saponification has a rather tight range of temperatures. This is probably overkill.

Copy link
Member Author

Choose a reason for hiding this comment

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

For this specific case probably, but this also stands as an example of how to do it for a more general case.

Comment on lines +150 to +154
self.set_variable_scaling_factor(
model.k_rxn,
1 / nominal,
overwrite=overwrite,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Order of magnitude scaling is probably not appropriate here. Based on heuristic analysis, a fixed scaling factor of 1/3 would work.

Comment on lines +156 to +158
if model.is_property_constructed("reaction_rate"):
for j in model.reaction_rate.values():
self.scale_variable_by_default(j, overwrite=overwrite)
Copy link
Contributor

Choose a reason for hiding this comment

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

If the user provides a default, we should do that, but we can get a decent scaling factor by multiplying the maximum scaling factor among the reactants provided by some period of time (I used 3600 s). (Equivalent to dividing the minimum default value by that period of time).

def variable_scaling_routine(
self, model, overwrite: bool = False, submodel_scalers: dict = None
):
self.scale_variable_by_default(model.flow_vol, overwrite=overwrite)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way for the user to provide a default value, rather than just using whatever happens to be the default default value?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, although it might not be the best way to do it. The default values are in a class attribute (dict) which users can interact with. That said, that is probably not the most obvious API for it (we could have a method to update the defaults).

Copy link
Contributor

@bpaul4 bpaul4 left a comment

Choose a reason for hiding this comment

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

I have one question, which is mainly for my own understanding of the code. The changes and additions look good.

@@ -111,12 +113,77 @@ def define_metadata(cls, obj):
)


class SaponificationReactionScaler(CustomScalerBase):
DEFAULT_SCALING_FACTORS = {"reaction_rate": 1e2}
Copy link
Contributor

Choose a reason for hiding this comment

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

How does the scaling framework know to use this value?

Copy link
Member Author

Choose a reason for hiding this comment

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

When you tell it to scale_variable_by_default or scale_constraint_by_default.

@andrewlee94 andrewlee94 merged commit 96237b9 into IDAES:main Oct 22, 2024
37 checks passed
@andrewlee94 andrewlee94 deleted the requil_scaling branch October 22, 2024 19:17
@ksbeattie ksbeattie added the Priority:Normal Normal Priority Issue or PR label Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority:Normal Normal Priority Issue or PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants