Skip to content

Commit

Permalink
Moving registration of external functions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlee94 committed Mar 18, 2024
1 parent a4a84dd commit 4cf5fb2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
24 changes: 24 additions & 0 deletions idaes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os
import copy
import logging
from typing import Optional, List

from . import config
from .ver import __version__ # noqa
Expand Down Expand Up @@ -83,6 +84,29 @@ def _handle_optional_compat_activation(
_log.debug("'idaes' logger debug test")


# TODO: Remove once AMPL bug is fixed
# TODO: https://github.com/ampl/asl/issues/13
# There appears to be a bug in the ASL which causes terminal failures
# if you try to create multiple ASL structs with different external
# functions in the same process. This causes pytest to crash during testing.
# To avoid this, register all known external functions at initialization.
def _ensure_external_functions_libs_in_env(
ext_funcs: List[str], var_name: str = "AMPLFUNC", sep: str = "\n"
):
libraries_str = os.environ.get(var_name, "")
libraries = [lib for lib in libraries_str.split(sep) if lib.strip()]
for func_name in ext_funcs:
lib: Optional[str] = os.path.join(bin_directory, func_name)
if lib is not None and lib not in libraries:
libraries.append(lib)
os.environ[var_name] = sep.join(libraries)


_ensure_external_functions_libs_in_env(
["cubic_roots.so", "general_helmholtz_external.so", "functions.so"]
)


def _create_data_dir():
"""Create the IDAES directory to store data files in."""
config.create_dir(data_directory)
Expand Down
25 changes: 0 additions & 25 deletions idaes/core/util/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
__author__ = "John Eslick, Tim Bartholomew, Robert Parker, Andrew Lee"

import math
import os
import sys
from typing import Optional, List

import scipy.sparse.linalg as spla
import scipy.linalg as la
Expand All @@ -51,35 +49,12 @@
from pyomo.core import expr as EXPR
from pyomo.common.numeric_types import native_types
from pyomo.core.base.units_container import _PyomoUnit
from pyomo.common.fileutils import find_library

import idaes.logger as idaeslog

_log = idaeslog.getLogger(__name__)


# There appears to be a bug in the ASL which causes terminal failures
# if you try to create multiple ASL structs with different external
# functions in the same process. This causes pytest to crash during testing.
# To avoid this, register all known external functions before we call
# PyNumero.
def _ensure_external_functions_libs_in_env(
ext_funcs: List[str], var_name: str = "AMPLFUNC", sep: str = "\n"
):
libraries_str = os.environ.get(var_name, "")
libraries = [lib for lib in libraries_str.split(sep) if lib.strip()]
for func_name in ext_funcs:
lib: Optional[str] = find_library(func_name)
if lib is not None and lib not in libraries:
libraries.append(lib)
os.environ[var_name] = sep.join(libraries)


_ensure_external_functions_libs_in_env(
["cubic_roots", "general_helmholtz_external", "functions"]
)


def __none_left_mult(x, y):
"""PRIVATE FUNCTION, If x is None return None, else return x * y"""
if x is not None:
Expand Down

0 comments on commit 4cf5fb2

Please sign in to comment.