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

Fix calculate_derivatives for PETSc #1342

Merged
merged 14 commits into from
Mar 1, 2024

Conversation

dallan-keylogic
Copy link
Contributor

Fixes

Addresses #1327

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.

Comment on lines +1147 to +1148
# TODO I don't understand how a value is getting assigned here
# assert m.dxdt[2].value is None
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eslickj , could you help me understand why we're getting derivative values populated here? It's definitely not through the calculate_derivatives function, but something being returned by the PETSc solver interface.

Copy link
Member

Choose a reason for hiding this comment

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

I think the problem is that with the forward finite difference discretization you should technically be deactivating m.diffeq[2] however this runs afoul of the representative_time infrastructure in the petsc solver. Seems like there is an assumption baked into the petsc solver logic that the model will be discretized using an implicit approach. Which makes sense since our petsc docs explicitly mention that only "implicit TS solvers are supported".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried deactivating m.diffeq[2], and it still gets populated. It isn't populated by the calculate_derivatives function, but directly from the solver.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wait, no, you're right, it still looks for a differential equation at t=2 and throws an error if it can't find one. I will note that the value returned for dxdt[2] isn't what you'd get with that differential equation, though.

Copy link

codecov bot commented Feb 19, 2024

Codecov Report

Attention: Patch coverage is 79.31034% with 6 lines in your changes are missing coverage. Please review.

Project coverage is 77.59%. Comparing base (28e9ae6) to head (c904006).
Report is 1 commits behind head on main.

Files Patch % Lines
idaes/core/solvers/petsc.py 79.31% 2 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1342      +/-   ##
==========================================
- Coverage   77.60%   77.59%   -0.01%     
==========================================
  Files         391      391              
  Lines       64315    64330      +15     
  Branches    14235    14244       +9     
==========================================
+ Hits        49913    49920       +7     
- Misses      11829    11835       +6     
- Partials     2573     2575       +2     

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

@ksbeattie
Copy link
Member

@eslickj, any chance you could review this?

idaes/core/solvers/petsc.py Outdated Show resolved Hide resolved
idaes/core/solvers/petsc.py Show resolved Hide resolved
idaes/core/solvers/petsc.py Outdated Show resolved Hide resolved
Comment on lines +1147 to +1148
# TODO I don't understand how a value is getting assigned here
# assert m.dxdt[2].value is None
Copy link
Member

Choose a reason for hiding this comment

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

I think the problem is that with the forward finite difference discretization you should technically be deactivating m.diffeq[2] however this runs afoul of the representative_time infrastructure in the petsc solver. Seems like there is an assumption baked into the petsc solver logic that the model will be discretized using an implicit approach. Which makes sense since our petsc docs explicitly mention that only "implicit TS solvers are supported".

if t == between.first() or t == between.last():
# Reset deriv value to old value
if disc_eq[t].active and not deriv[t].fixed:
deriv[t].value = old_value
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lbianchi-lbl Do you know what Pylint is complaining about here? We can only get to this logic if old_value has already been assigned in the try block.

Copy link
Contributor

Choose a reason for hiding this comment

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

From a quick look I'd guess it might be because of old_value is defined inside the if block on L764?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved it outside the if block, still getting the pylint error. I'm guessing it's because Pylint doesn't know that accessing deriv[t].value is only going to raise a KeyError in that context, and will never raise a ValueError. I assume this is a case for some Pylint ignore statement.

The test failures appear to be unrelated to this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess it might work if old_value = deriv[t].value is placed at the top of the for t in time: loop block, i.e. on line 756 (before the if t < between.first() ... line)?

If that doesn't work either, I agree that a Pylint disable directive seems warranted.

@lbianchi-lbl lbianchi-lbl added the CI:run-integration triggers_workflow: Integration label Feb 29, 2024
@idaes-build idaes-build removed the CI:run-integration triggers_workflow: Integration label Feb 29, 2024
@dallan-keylogic
Copy link
Contributor Author

@andrewlee94 @lbianchi-lbl Could you rubber stamp this so integration tests can run?

@andrewlee94
Copy link
Member

@dallan-keylogic You can use the CI:run_integration tag to manually trigger the integration tests.

@lbianchi-lbl lbianchi-lbl added the CI:run-integration triggers_workflow: Integration label Feb 29, 2024
@idaes-build idaes-build removed the CI:run-integration triggers_workflow: Integration label Feb 29, 2024
Copy link
Member

@eslickj eslickj 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 as far as I can tell. Not sure if my approval matters anymore, but I approved anyway.

@ksbeattie
Copy link
Member

Looks good as far as I can tell. Not sure if my approval matters anymore, but I approved anyway.

@eslickj your approval always matters to us!

@lbianchi-lbl lbianchi-lbl enabled auto-merge (squash) February 29, 2024 23:15
@lbianchi-lbl lbianchi-lbl merged commit 9de79c6 into IDAES:main Mar 1, 2024
45 checks passed
@dallan-keylogic dallan-keylogic deleted the petsc_calculate_derivatives branch March 4, 2024 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority:High High Priority Issue or PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants