Skip to content

Commit

Permalink
cleanup paropt import
Browse files Browse the repository at this point in the history
  • Loading branch information
ewu63 committed Mar 17, 2024
1 parent 1fc3dfd commit e72769a
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 31 deletions.
1 change: 0 additions & 1 deletion pyoptsparse/pyIPOPT/pyIPOPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import numpy as np

# Local modules
from ..pyOpt_error import Error
from ..pyOpt_optimizer import Optimizer
from ..pyOpt_utils import (
ICOL,
Expand Down
2 changes: 1 addition & 1 deletion pyoptsparse/pyOpt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,9 @@ def try_import_compiled_module_from_path(module_name: str, path: str|None = None
If importable, the imported module is returned.
If not importable, the error message is instead returned.
"""
path = os.path.abspath(os.path.expandvars(os.path.expanduser(path)))
orig_path = sys.path
if path is not None:
path = os.path.abspath(os.path.expandvars(os.path.expanduser(path)))
sys.path = [path]
try:
module = importlib.import_module(module_name)
Expand Down
38 changes: 13 additions & 25 deletions pyoptsparse/pyParOpt/ParOpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,22 @@
# External modules
import numpy as np

# isort: off
# Attempt to import mpi4py.
# Local modules
from ..pyOpt_optimizer import Optimizer
from ..pyOpt_utils import INFINITY, try_import_compiled_module_from_path

# Attempt to import ParOpt/mpi4py
# If PYOPTSPARSE_REQUIRE_MPI is set to a recognized positive value, attempt import
# and raise exception on failure. If set to anything else, no import is attempted.
if "PYOPTSPARSE_REQUIRE_MPI" in os.environ:
if os.environ["PYOPTSPARSE_REQUIRE_MPI"].lower() in ["always", "1", "true", "yes"]:
try:
from paropt import ParOpt as _ParOpt
from mpi4py import MPI
except ImportError:
_ParOpt = None
else:
_ParOpt = None
if "PYOPTSPARSE_REQUIRE_MPI" in os.environ and os.environ["PYOPTSPARSE_REQUIRE_MPI"].lower() not in ["always", "1", "true", "yes"]:
_ParOpt = "ParOpt was not imported, as requested by the environment variable 'PYOPTSPARSE_REQUIRE_MPI'"
MPI = "mpi4py was not imported, as requested by the environment variable 'PYOPTSPARSE_REQUIRE_MPI'"
# If PYOPTSPARSE_REQUIRE_MPI is unset, attempt to import mpi4py.
# Since ParOpt requires mpi4py, if either _ParOpt or mpi4py is unavailable
# we disable the optimizer.
else:
try:
from paropt import ParOpt as _ParOpt
from mpi4py import MPI
except ImportError:
_ParOpt = None
# isort: on

# Local modules
from ..pyOpt_error import Error
from ..pyOpt_optimizer import Optimizer
from ..pyOpt_utils import INFINITY

_ParOpt = try_import_compiled_module_from_path("paropt.ParOpt")
MPI = try_import_compiled_module_from_path("mpi4py.MPI")

class ParOpt(Optimizer):
"""
Expand All @@ -48,8 +35,9 @@ class ParOpt(Optimizer):
def __init__(self, raiseError=True, options={}):
name = "ParOpt"
category = "Local Optimizer"
if _ParOpt is None and raiseError:
raise ImportError("There was an error importing ParOpt")
for mod in [_ParOpt, MPI]:
if isinstance(mod, str) and raiseError:
raise ImportError(mod)

# Create and fill-in the dictionary of default option values
self.defOpts = {}
Expand Down
1 change: 0 additions & 1 deletion pyoptsparse/pySLSQP/pySLSQP.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import numpy as np

# Local modules
from ..pyOpt_error import Error
from ..pyOpt_optimizer import Optimizer
from ..pyOpt_utils import try_import_compiled_module_from_path

Expand Down
1 change: 0 additions & 1 deletion tests/test_snopt_bugfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

# First party modules
from pyoptsparse import SNOPT, Optimization
from pyoptsparse.pyOpt_error import Error


def objfunc(xdict):
Expand Down
1 change: 0 additions & 1 deletion tests/test_user_termination.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

# First party modules
from pyoptsparse import OPT, Optimization
from pyoptsparse.pyOpt_error import Error


class TerminateComp:
Expand Down
1 change: 0 additions & 1 deletion tests/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

# First party modules
from pyoptsparse import OPT, History
from pyoptsparse.pyOpt_error import Error


def assert_optProb_size(optProb, nObj, nDV, nCon):
Expand Down

0 comments on commit e72769a

Please sign in to comment.