-
Notifications
You must be signed in to change notification settings - Fork 238
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
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
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. |
@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. |
@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). |
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.
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. |
idaes/core/solvers/petsc.py
Outdated
_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) |
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.
Should this be indented twice more? To be inside the if len(constraints) > 0
branch?
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 think this is good to go.
idaes/core/solvers/petsc.py
Outdated
# 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.") |
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 sounds good, but should this check happen earlier? E.g. at the end of _get_derivative_differential_data_map
?
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 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.
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.
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.
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.
What happens if you send a model with no derivatives to Petsc? Does it just solve the square system?
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.
With your comment in mind, I'm fine with the check where it is, but I'll leave it up to you.
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.
Okay changed my mind again. It's probably not worth the trouble of checking this earlier.
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.
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.
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. |
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.
Looks great! Thanks for all the work on this interface, John. This will be very useful.
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 |
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.
What's the difference between rp_example5
and rp_example3
?
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.
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.
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. |
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
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: