Skip to content

Add sgnsqr external function f(X) = sgn(x)*x^2 #3622

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

eslickj
Copy link
Contributor

@eslickj eslickj commented Jun 9, 2025

Fixes None

Summary/Motivation:

This adds a signed square function to the contributed external function demonstration.

Here is an example of where this can be useful.

Say you have a pressure-flow relationship that says the mass flow is proportional to the square root of pressure drop, where pa is inlet pressure and pb is outlet pressure. Like this: mdot^2 = k(pb - pa).

The direction of flow is forward if the pressure drop is positive or backward if the pressure drop is negative. There are both positive and negative solutions to the equation. If we use the proposed sgnsqr function, there is now only one solution to the equation, and the sign of flow matches the sign of pressure drop.

The sign squared function is still smooth and replacing square with sgnsqr still gives us solutions to the original equation. It just limits us to one correct solution instead of two possibilities. I expect there could be many situations where it would be useful to limit the solutions of y = x^2 to a single solution.

Changes proposed in this PR:

  • add sgnsqr external function to the ampl_external_demo library.

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:

  1. I agree my contributions are submitted under the BSD license.
  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.

@jsiirola
Copy link
Member

Overall, I don't see a huge problem with supporting a library of useful external functions; however, some concerns with this implementation:

  • the ampl_function_demo module is really meant to be lightweight documentation for how to write and build AMPL external functions that are compatible with the Pyomo build system. I think if we are going to start supporting "real" external functions, then that should be in a separate module.
  • this implementation is in C1 but not in C2. That could cause some solvers (e.g., ipopt) that assume continuous second derivatives to have trouble. @carldlaird should weigh in here.
  • If we decide to go down the path of creating a library of "useful" external functions, we should consider including others (e.g., Add sinc(x) function #323).
  • I think the package would need appropriate user documentation (at the very least to include caveats so that users understand the limitations of the function implementations).

@eslickj
Copy link
Contributor Author

eslickj commented Jun 18, 2025

@jsiirola, I agree that if we decide that there are useful external functions we should add a new library. I was thinking that but then decided to see if I could get away with a short cut. The fact that the function isn't second order smooth is bothersome, but then again so is getting a solution with the wrong sign. I'm still not sure if this is a good idea or not. I know we could also do things with bounds and constraints, but I do a fair number of straight forward square simulations where bounds and constraints are harder to manage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants