From 4cf5fb262965b3974b1e9bd6f78d7754f223425d Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Mon, 18 Mar 2024 10:28:14 -0400 Subject: [PATCH] Moving registration of external functions --- idaes/__init__.py | 24 ++++++++++++++++++++++++ idaes/core/util/scaling.py | 25 ------------------------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/idaes/__init__.py b/idaes/__init__.py index 5dc49dbe87..d513749be4 100644 --- a/idaes/__init__.py +++ b/idaes/__init__.py @@ -22,6 +22,7 @@ import os import copy import logging +from typing import Optional, List from . import config from .ver import __version__ # noqa @@ -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) diff --git a/idaes/core/util/scaling.py b/idaes/core/util/scaling.py index fcfe34f0fe..c0ec998ac7 100644 --- a/idaes/core/util/scaling.py +++ b/idaes/core/util/scaling.py @@ -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 @@ -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: