pyomo

Use of a DLL in a Pyomo model


I am new to pyomo, so this is a basic question about the capabilities/restrictions of pyomo as a whole. I have read most of the "Optimization Modeling in Python" textbook about using pyomo, and I believe that pyomo would be a great tool for me to use for a modeling project I am currently working on. However, I do have one concern. In many equations in my model, I need to access a Fortran library linked through a DLL (it's a commercial equation of state). The functions in this Fortran library are very non-linear. From what I understand, non-linear pyomo equations must be represented through the non-linear functions provided from the pyomo library. Is there any way to use pyomo while using this DLL, or would I need to rebuild all of the functions in the Fortran library in python with pyomo functions?


Solution

  • Pyomo can support models that have external function calls through two mechanisms (but both come with a long list of caveats):

    1. the ExternalFunction Component
      • This only supports scalar-valued functions.
      • This is only supported by solvers that support general nonlinear problems through the "NL file" interface (and don't require the ability to construct convex underestimators).
      • The external function library must adhere to the ASL standard for user-defined functions.
        • The interface requires that you provide both function and derivative evaluations.
        • This usually requires compiling your own DLL / library.
      • See https://github.com/Pyomo/pyomo/tree/main/pyomo/contrib/ampl_function_demo for an example for how to build external functions.
    2. The ExternalGreyBox Component (in pyomo.contrib.pynumero).
      • This is functionality that is still "in development" (part of pyomo.contrib). The API may change with little notice as pynumero is further developed.
      • This supports vector-valued functions.
      • The external function can be any Python function (including Python functions that call out to external libraries).
      • The functions must still provide both function and derivative evaluations.
      • The only solver that currently (August 2024) supports ExternalGreyBox components is cyipopt.

    In addition to these options, @AirSquid is correct that other options exist (and can frequently provide good results), including approximations (surrogate models), or translating the underlying mathematics into Pyomo.

    Finally, you might consider looking at the IDAES library (https://github.com/IDAES/idaes-pse), which has implemented equations of state in Pyomo (including the direct implementation route, custom external functions, and calls out to third-party packages [like CoolProp]).