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

Petsc DAE Solver Utils #552

Merged
merged 99 commits into from
Jan 15, 2022
Merged

Petsc DAE Solver Utils #552

merged 99 commits into from
Jan 15, 2022

Conversation

eslickj
Copy link
Member

@eslickj eslickj commented Oct 14, 2021

Summary/Motivation:

This adds utilities mainly for the PETSc time stepping (TS) solvers. I'll attach examples for now and write some clean official examples for the examples repo.

This depends on Pyomo/pyomo#2152 (which has been merged, but you will need a recent development version of Pyomo).

Examples

  • car_example1.zip modified version of the car example from Pyomo DAE
  • chemakzo.zip from: https://archimede.dm.uniba.it/~testset/report/chemakzo.pdf, this one is a little more interesting. Some stuff happens very fast and it would be harder to solve in the fully discretized form without some careful tuning.
  • pid.zip IDAES model, this is the test for the PID controller solved with the PETSc solver. Full IDAES model with a few units, controller, and external function calls for steam properties.
  • pendulum.zip
    Used to test constraint and variable scaling in DAEs.

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.

@eslickj eslickj self-assigned this Oct 14, 2021
@eslickj eslickj marked this pull request as draft October 14, 2021 00:33
@ksbeattie ksbeattie added the Priority:Normal Normal Priority Issue or PR label Oct 15, 2021
@codecov
Copy link

codecov bot commented Nov 8, 2021

Codecov Report

Merging #552 (c87818e) into main (8134c40) will increase coverage by 0.14%.
The diff coverage is 85.46%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #552      +/-   ##
==========================================
+ Coverage   64.23%   64.37%   +0.14%     
==========================================
  Files         362      363       +1     
  Lines       58340    58744     +404     
  Branches    10660    10744      +84     
==========================================
+ Hits        37473    37816     +343     
- Misses      18975    19014      +39     
- Partials     1892     1914      +22     
Impacted Files Coverage Δ
idaes/util/download_bin.py 47.40% <14.28%> (-1.82%) ⬇️
idaes/core/solvers/petsc.py 84.08% <84.08%> (ø)
idaes/config.py 81.65% <100.00%> (+1.25%) ⬆️
idaes/core/solvers/features.py 96.46% <100.00%> (+10.74%) ⬆️
idaes/ver.py 61.53% <0.00%> (-4.62%) ⬇️
idaes/ui/fsvis/model_server.py 83.78% <0.00%> (-1.63%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8134c40...c87818e. Read the comment docs.

@eslickj
Copy link
Member Author

eslickj commented Nov 9, 2021

I added another example based on the steam tank PID controller test (files attached to PR description under examples). So IDAES models seem to work. I'm ready to try some more, if anyone has something to try.

@Robbybp
Copy link
Member

Robbybp commented Nov 18, 2021

@eslickj Is the example on the steam tank part of this PR? I don't see it. I'd like an example/test I can run so I don't break anything when I update the "set_dae_suffixes" method.

@eslickj
Copy link
Member Author

eslickj commented Nov 19, 2021

@Robbybp, it's not in the PR yet, but it's attached to the PR description (the pid.zip link https://github.com/IDAES/idaes-pse/files/7506724/pid.zip).

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.

Remind me what happens when a derivative variable is fixed to a nonzero value?

idaes/core/solvers/tests/test_petsc.py Outdated Show resolved Hide resolved
@eslickj
Copy link
Member Author

eslickj commented Jan 13, 2022

Remind me what happens when a derivative variable is fixed to a nonzero value?

It would turn the differential equation into an algebraic equation whether the derivative is 0 and any other value.

_sub_problem_scaling_suffix(m, t_block)
with idaeslog.solver_log(solve_log, idaeslog.INFO) as slc:
res = solver_snes.solve(t_block, tee=slc.tee)
res_list.append(res)
Copy link
Member

Choose a reason for hiding this comment

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

Should this be indented twice more? To be inside the if len(constraints) > 0 branch?

Robbybp
Robbybp previously approved these changes Jan 13, 2022
Copy link
Member

@Robbybp Robbybp left a comment

Choose a reason for hiding this comment

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

I think this is good to go.

Comment on lines 480 to 486
# We need to check if there are derivatives in the problem before
# sending this to the solver. We'll assume that if you are using
# this and don't have any differential equations, you are making a
# mistake.
if len(differential_vars) < 1:
raise RuntimeError(
"No differential equations found, you do not need a DAE solver.")
Copy link
Member

Choose a reason for hiding this comment

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

This sounds good, but should this check happen earlier? E.g. at the end of _get_derivative_differential_data_map?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking that too, but unfortunately a derivative could be fixed for one time step and not another. We could possibly write the check up front, but I'm not sure it's worth the overhead.

Copy link
Member Author

@eslickj eslickj Jan 13, 2022

Choose a reason for hiding this comment

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

Wait maybe it's not that hard. I should be able to check that there is at least one derivative for each time point. I think I can improve this. I'll also add a test for this.

Copy link
Member

Choose a reason for hiding this comment

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

What happens if you send a model with no derivatives to Petsc? Does it just solve the square system?

Copy link
Member

Choose a reason for hiding this comment

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

With your comment in mind, I'm fine with the check where it is, but I'll leave it up to you.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay changed my mind again. It's probably not worth the trouble of checking this earlier.

Copy link
Member Author

Choose a reason for hiding this comment

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

What happens if you send a model with no derivatives to Petsc? Does it just solve the square system?

It should but it crashes. Most likely I messed up and assumed I had at least one derivative. I’ll sort that out in the solver part eventually. For now this check will stop it from getting there.

@eslickj
Copy link
Member Author

eslickj commented Jan 13, 2022

I added tests for a few more cases. I have 5 tests based on @Robbybp's example. 2 work and 3 are caught with errors and raise exceptions. I think this is ready, at least really close.

Once we get this in, I'll put out new binaries with a few very minor improvements and a couple example notebooks in the examples repo.

Copy link
Member

@Robbybp Robbybp left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks for all the work on this interface, John. This will be very useful.

Comment on lines +120 to +138
def rp_example5():
m = pyo.ConcreteModel()

m.time = pyodae.ContinuousSet(initialize=(0.0, 10.0))
m.x = pyo.Var(m.time, initialize=1)
m.u = pyo.Var(m.time, initialize=1)
m.dxdt = pyodae.DerivativeVar(m.x, wrt=m.time)

def diff_eq1_rule(m, t):
return m.dxdt[t] == m.x[t]**2 - m.u[t]
m.diff_eq1 = pyo.Constraint(m.time, rule=diff_eq1_rule)

discretizer = pyo.TransformationFactory('dae.finite_difference')
discretizer.apply_to(m,nfe=1,scheme='BACKWARD')

m.u[0].fix(1.0)
m.dxdt[:].fix(2)

return m
Copy link
Member

Choose a reason for hiding this comment

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

What's the difference between rp_example5 and rp_example3?

Copy link
Member Author

Choose a reason for hiding this comment

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

You know, I don't see a difference and can't remember. I think I must have copied it and meant to change it to test another case, and now I cannot recall what it was for. Doing to many things at once I guess. If the tests pass I'll merge this and make another small PR to update or remove the test if I still can't remember why it's there.

@eslickj
Copy link
Member Author

eslickj commented Jan 15, 2022

Looks great! Thanks for all the work on this interface, John. This will be very useful.

Thanks! I think now that this is squared away, we can use the same method to hook up other integrators. We can come up with a standard for explicit and implicit-explicit methods too. I have some ideas I may propose if I find time.

@eslickj eslickj added approved This PR has been approved CI:run-integration triggers_workflow: Integration labels Jan 15, 2022
@idaes-build idaes-build removed the CI:run-integration triggers_workflow: Integration label Jan 15, 2022
@eslickj eslickj enabled auto-merge January 15, 2022 01:39
@eslickj eslickj merged commit ef82f17 into IDAES:main Jan 15, 2022
@eslickj eslickj deleted the petsc_dae branch February 2, 2022 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved This PR has been approved Priority:Normal Normal Priority Issue or PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants