diff --git a/watertap/conftest.py b/watertap/conftest.py index 7fffca5f17..c699c8dc9a 100644 --- a/watertap/conftest.py +++ b/watertap/conftest.py @@ -45,12 +45,16 @@ def for_item(cls, item: Item) -> Container["MarkerSpec"]: def _handle_requires_idaes_solver( solver: Optional = None, action: Optional[Callable[[str], None]] = pytest.xfail ) -> None: + from pyomo.contrib.solver.common.base import SolverBase from watertap.core.solvers import get_solver from idaes.config import bin_directory solver = solver or get_solver() idaes_bin_dir = Path(bin_directory).resolve() - solver_bin_path = Path(solver.executable()).resolve() + if isinstance(solver, SolverBase): + solver_bin_path = Path(str(solver.config.executable)).resolve() + else: + solver_bin_path = Path(solver.executable()).resolve() if not idaes_bin_dir in solver_bin_path.parents: action(f"This test is known to be failing with {solver_bin_path}") diff --git a/watertap/core/solvers.py b/watertap/core/solvers.py index 887e686b59..f598097b6c 100644 --- a/watertap/core/solvers.py +++ b/watertap/core/solvers.py @@ -14,4 +14,54 @@ "get_solver", ] -from watertap_solvers import get_solver +# from watertap_solvers import get_solver +from idaes.core.solvers import get_solver as idaes_get_solver + + +def get_solver( + solver="ipopt_v2", + solver_options: dict = None, + writer_config: dict = None, + options: dict = None, +): + if options is not None: + if solver_options is not None: + raise RuntimeError() + else: + solver_options = options + + if solver_options is None: + solver_options = {} + + if writer_config is None: + writer_config = {} + + solver = "ipopt_v2" + + solver_options["nlp_scaling_method"] = "gradient-based" + + if "tol" not in solver_options: + solver_options["tol"] = 1e-08 + if "constr_viol_tol" not in solver_options: + solver_options["constr_viol_tol"] = 1e-08 + if "acceptable_constr_viol_tol" not in solver_options: + solver_options["acceptable_constr_viol_tol"] = 1e-08 + if "bound_relax_factor" not in solver_options: + solver_options["bound_relax_factor"] = 0.0 + if "honor_original_bounds" not in solver_options: + solver_options["honor_original_bounds"] = "no" + if "linear_solver" not in solver_options: + solver_options["linear_solver"] = "ma27" + if "max_iter" not in solver_options: + solver_options["max_iter"] = 3000 + + if "scale_model" not in writer_config: + writer_config["scale_model"] = True + if "linear_presolve" not in writer_config: + writer_config["linear_presolve"] = False + if "skip_trivial_constraints" not in writer_config: + writer_config["skip_trivial_constraints"] = False + + return idaes_get_solver( + solver=solver, solver_options=solver_options, writer_config=writer_config + )