{ "cells": [ { "cell_type": "markdown", "id": "cell-00", "metadata": {}, "source": [ "# IDAES two-liquid settler\n", "\n", "\n", "**Evidence: numerical verification.** Optimize a nonreactive liquid settler\n", "at 293.15 K and 100,000 Pa using a declared aqueous/organic branch. IDAES/Pyomo\n", "owns the process equations and outer optimization; every thermodynamic\n", "equilibrium evaluation and its exact derivatives run through the Engine's\n", "compiled ASL callback. This is the process-equation exception to the native\n", "calculation rule.\n", "\n", "Use `khudaida-2026-figure-2-electrolyte-lle/7`, Khudaida et al. (2026),\n", "Tables 2, 5–7 ([paper](https://doi.org/10.1021/acs.jced.5c00780)). The retained\n", "live-settler analysis supplies the process declaration and numerical checks.\n", "This tutorial runs one start rather than its full qualification campaign.\n", "\n", "Install the optional IDAES extensions as external **build-time-only** tools,\n", "as described in the installation guide. Ipopt must remain outside the checkout,\n", "Engine build, site outputs, downloads and shared caches; preserve the upstream\n", "notices. The Engine and outer Ipopt both select MUMPS. The upstream executable\n", "also contains HSL: HSL, a collection of Fortran codes for large-scale scientific\n", "computation. See https://www.hsl.rl.ac.uk/.\n" ] }, { "cell_type": "markdown", "id": "cell-01", "metadata": {}, "source": [ "## Read the inputs\n", "\n", "Use a repository checkout with the environment described in the installation\n", "guide. The wheel alone does not contain these data. All density, association,\n", "equilibrium and parameter-fit iterations below run in the native Engine.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "cell-02", "metadata": { "execution": { "iopub.execute_input": "2026-10-20T20:17:24.588210Z", "iopub.status.busy": "2026-10-20T20:17:24.588098Z", "iopub.status.idle": "2026-10-20T20:17:24.876823Z", "shell.execute_reply": "2026-10-20T20:17:24.876138Z" } }, "outputs": [], "source": [ "from pathlib import Path\n", "import math\n", "import pandas as pd\n", "from IPython.display import display\n", "from epcsaft import (\n", " Parameters,\n", " Mixture,\n", ")\n", "from epcsaft import equilibrium as eq\n", "\n", "# Start the kernel anywhere within the documented repository checkout.\n", "pd.set_option(\"display.max_columns\", None)\n", "pd.set_option(\"display.max_colwidth\", None)\n", "\n", "ROOT = next(\n", " folder\n", " for folder in (Path.cwd(), *Path.cwd().parents)\n", " if (folder / \"ARCHITECTURE.yaml\").is_file()\n", ")\n", "PACKETS = ROOT / \"data\" / \"packets\"\n" ] }, { "cell_type": "markdown", "id": "cell-03", "metadata": {}, "source": [ "## Declare formula flows and their explicit-species map\n", "\n", "The four formula flows `(water, ethanol, isobutanol, NaCl)` sum to 1 mol/s.\n", "Map them to `(water, ethanol, isobutanol, Na⁺, Cl⁻)` as\n", "`(Fw, Fe, Fi, Fs, Fs)`. Explicit-species flow sums to **1 + Fs** mol/s.\n", "Each callback value uses the same numerical basis as an amount in mol;\n", "the steady process interprets that value as flow in mol/s.\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "cell-04", "metadata": { "execution": { "iopub.execute_input": "2026-10-20T20:17:24.878498Z", "iopub.status.busy": "2026-10-20T20:17:24.878264Z", "iopub.status.idle": "2026-10-20T20:17:26.356939Z", "shell.execute_reply": "2026-10-20T20:17:26.356468Z" } }, "outputs": [], "source": [ "import os\n", "import re\n", "import time\n", "import numpy as np\n", "from epcsaft import _core\n", "from idaes.core import FlowsheetBlock, UnitModelBlockData, declare_process_block_class\n", "from idaes.core.solvers import get_solver\n", "from idaes.core.util.model_statistics import degrees_of_freedom\n", "from pyomo.common.config import ConfigValue\n", "from pyomo.contrib.pynumero.interfaces.ampl_nlp import AslNLP\n", "from pyomo.environ import (\n", " ConcreteModel,\n", " Constraint,\n", " Expression,\n", " ExternalFunction,\n", " NonNegativeReals,\n", " Objective,\n", " Suffix,\n", " Var,\n", " check_optimal_termination,\n", " value,\n", ")\n", "from pyomo.environ import units as u\n", "from pyomo.network import Port\n", "import idaes\n", "\n", "assert \"EPCSAFT_HSL_LIBRARY\" not in os.environ\n", "assert \"EPCSAFT_IPOPT_OPTIONS_FILE\" not in os.environ\n", "assert _core.linear_solver() == \"mumps\"\n", "ipopt = Path(idaes.bin_directory) / \"ipopt\"\n", "assert ipopt.is_file(), (\n", " \"Install the optional IDAES extensions described in the installation guide.\"\n", ")\n", "assert not ipopt.resolve().is_relative_to(ROOT)\n", "library = Path(_core.__file__).resolve().with_name(\"libepcsaft_asl.so\")\n", "output = ROOT / \"build/docs/settler\"\n", "output.mkdir(parents=True, exist_ok=True)\n", "COMPONENTS = (\"water\", \"ethanol\", \"isobutanol\", \"sodium-cation\", \"chloride-anion\")\n", "PHASES = (\"aqueous\", \"organic\")\n", "GAP = 0.001\n", "F0 = np.array(\n", " (0.7198946513021484, 0.03377833329223333, 0.21864518731791907, 0.02768182808769922)\n", ")\n", "assert abs(sum(F0) - 1) < 1e-12\n", "FLOW = u.mol / u.s\n", "TP = (293.15, 100000) # fixed temperature / K and pressure / Pa\n", "NAME = \"epcsaft_equilibrium_phase_component_amount\"\n", "OPTIONS = eq.Continuation(\n", " initial_step=0.25, minimum_step=0.25, max_steps=4, max_retries_per_step=1\n", ")\n", "\n", "\n", "def feed(F):\n", " return (*F[:3], F[3], F[3])\n", "\n", "\n", "def engine_problem(flows):\n", " return eq.Problem(\n", " T=TP[0],\n", " P=TP[1],\n", " feed=eq.Amounts(dict(zip(COMPONENTS, flows))),\n", " phases=[\n", " eq.Phase(\n", " \"phase-0\",\n", " kind=\"liquid\",\n", " support=COMPONENTS,\n", " composition_guess=(0.8, 0.05, 0.05, 0.05, 0.05),\n", " ),\n", " eq.Phase(\n", " \"phase-1\",\n", " kind=\"liquid\",\n", " support=COMPONENTS,\n", " composition_guess=(0.2, 0.1, 0.6998, 0.0001, 0.0001),\n", " ),\n", " ],\n", " row_tolerance=1e-10,\n", " )\n", "\n", "\n", "def phase_data(model, central, flows):\n", " assert central.success, central.message\n", " assert central.requested_tolerance_met\n", " assert central.phase_count == 2\n", " x = np.array(central.mole_fractions).reshape(2, 5)\n", " material = np.max(abs(np.array(central.amounts) @ x - flows))\n", " charge = np.max(abs(x[:, 3] - x[:, 4]))\n", " pressure_errors = []\n", " for p in range(2):\n", " state = model.state(293.15, rho=central.molar_densities[p], x=x[p].tolist())\n", " assert state.pressure_density_derivative > 0\n", " pressure_errors.append(abs(state.pressure / 100000 - 1))\n", " classified = max(map(abs, central.classified_residuals))\n", " assert material <= 1e-10 and charge <= 1e-10\n", " assert max(pressure_errors) <= 1e-08 and classified <= 1e-10\n", " assert (\n", " math.isfinite(central.reduced_hessian_minimum)\n", " and central.reduced_hessian_minimum > 0\n", " )\n", " assert x[0, 0] - x[1, 0] > GAP\n", " diagnostics = dict(\n", " classified=classified,\n", " hessian=central.reduced_hessian_minimum,\n", " material=float(material),\n", " charge=float(charge),\n", " pressure=max(pressure_errors),\n", " )\n", " return diagnostics\n" ] }, { "cell_type": "markdown", "id": "cell-05", "metadata": {}, "source": [ "## Solve the anchor and check exact derivative transport\n", "\n", "The record carries the explicit affine feed map, phase supports, continuation\n", "controls and the aqueous water-fraction identity gap above 10⁻³. Check the\n", "anchor's component/charge closure, pressure closure and classified residuals,\n", "then compare all ten component values, four feed gradients and ten symmetric\n", "second actions per value with ASL at 10⁻⁹ absolute. Unit feed increments are\n", "1 mol for native amounts and 1 mol/s for the process coordinates; divide by\n", "these increments to interpret partial derivatives. No thermal direction is used.\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "cell-06", "metadata": { "execution": { "iopub.execute_input": "2026-10-20T20:17:26.360949Z", "iopub.status.busy": "2026-10-20T20:17:26.360593Z", "iopub.status.idle": "2026-10-20T20:17:26.479891Z", "shell.execute_reply": "2026-10-20T20:17:26.479325Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "epcsaft_equilibrium prepared record 1 of at most 16\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
classifiedhessianmaterialchargepressurepublic_asl_max_abs
01.027800e-140.000111.110223e-160.04.775735e-120.0
\n", "
" ], "text/plain": [ " classified hessian material charge pressure \\\n", "0 1.027800e-14 0.00011 1.110223e-16 0.0 4.775735e-12 \n", "\n", " public_asl_max_abs \n", "0 0.0 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def encode_record(model, problem, options, anchor):\n", " return eq.asl_record(\n", " model,\n", " problem,\n", " options,\n", " [0.0] * 5,\n", " np.array([feed(row) for row in np.eye(4)]).T.tolist(),\n", " list(anchor),\n", " 0,\n", " GAP,\n", " )\n", "\n", "\n", "class Reference:\n", " def __init__(self):\n", " self.model = Mixture(\n", " Parameters.from_json(\n", " PACKETS\n", " / \"khudaida-2026-figure-2-electrolyte-lle/7/parameters/parameters.json\",\n", " components=COMPONENTS,\n", " )\n", " )\n", " self.problem = engine_problem(feed(F0))\n", " self.anchor = eq.solve_equilibrium(self.model, self.problem)\n", " self.record = encode_record(self.model, self.problem, OPTIONS, F0)\n", " self.observables = [\n", " eq.SolvedStateObservable(\n", " eq.SolvedStateObservableKind.PhaseComponentAmount, p, i\n", " )\n", " for p in range(2)\n", " for i in range(5)\n", " ]\n", " self.feed_increment_mol = 1.0\n", " self.directions = [\n", " eq.SolvedStateActionDirection(\n", " 0.0, 0.0, feed(self.feed_increment_mol * row), []\n", " )\n", " for row in np.eye(4)\n", " ]\n", " self.library = library\n", " self.external = ExternalFunction(\n", " library=str(self.library),\n", " function=NAME,\n", " units=FLOW,\n", " arg_units=[u.K, u.Pa] + [FLOW] * 4 + [None] * 3,\n", " )\n", "\n", " def public(self, F, order=2):\n", " problem = engine_problem(feed(F))\n", " continuation = (\n", " None\n", " if np.array_equal(F, F0)\n", " else eq.continue_equilibrium(\n", " self.model, self.problem, problem, OPTIONS, start_result=self.anchor\n", " )\n", " )\n", " if continuation is not None and (\n", " not continuation.success\n", " or continuation.stop != eq.ContinuationStop.reached_target\n", " ):\n", " raise RuntimeError(\n", " f\"continuation: {continuation.stop}: {continuation.message}\"\n", " )\n", " central = self.anchor if continuation is None else continuation.states[-1]\n", " diagnostics = phase_data(self.model, central, feed(F))\n", " if central.mole_fractions[0] - central.mole_fractions[5] <= GAP:\n", " raise RuntimeError(f\"branch/certificate: {diagnostics}\")\n", " compiled = eq.compile_problem(self.model, problem)\n", " y, g, h, statuses = (np.zeros(10), np.zeros((10, 4)), np.zeros((10, 4, 4)), [])\n", " pairs = [(-1,)] if order == 0 else [(i,) for i in range(4)]\n", " if order == 2:\n", " pairs += [(i, j) for j in range(4) for i in range(j + 1)]\n", " for indices in pairs:\n", " directions = (\n", " [eq.SolvedStateActionDirection(0.0, 0.0, [0.0] * 5, [])]\n", " if indices == (-1,)\n", " else [self.directions[i] for i in indices]\n", " )\n", " batch = eq.solved_state_actions(\n", " compiled,\n", " central,\n", " eq.SolvedStateActionRequest(self.observables, directions),\n", " )\n", " for row, item in enumerate(batch.results):\n", " statuses.append(item.status.name)\n", " if (\n", " item.status != eq.SolvedStateActionStatus.Available\n", " or item.value is None\n", " or item.action is None\n", " or (not np.isfinite([item.value, item.action]).all())\n", " ):\n", " raise RuntimeError(f\"unavailable E5 action: {item.status}\")\n", " y[row] = item.value\n", " if indices == (-1,):\n", " continue\n", " if len(indices) == 1:\n", " g[row, indices[0]] = item.action / self.feed_increment_mol\n", " else:\n", " i, j = indices\n", " h[row, i, j] = h[row, j, i] = (\n", " item.action / self.feed_increment_mol**2\n", " )\n", " assert batch.diagnostics.rank == batch.diagnostics.state_dimension\n", " return (\n", " (y, g, h),\n", " {\n", " \"rank\": batch.diagnostics.rank,\n", " \"dimension\": batch.diagnostics.state_dimension,\n", " \"statuses\": sorted(set(statuses)),\n", " \"classified_residual\": diagnostics[\"classified\"],\n", " \"reduced_hessian_minimum\": diagnostics[\"hessian\"],\n", " \"steps\": 0 if continuation is None else continuation.accepted_steps,\n", " \"requested_tolerance_met\": central.requested_tolerance_met,\n", " \"water_gap\": central.mole_fractions[0] - central.mole_fractions[5],\n", " },\n", " )\n", "\n", " def asl(self, F, order=2, rows=range(10)):\n", " y, g, h = (np.zeros(10), np.zeros((10, 4)), np.zeros((10, 4, 4)))\n", " for row in rows:\n", " y[row], gradient, packed = self.external.evaluate_fgh(\n", " [*TP, *F, self.record, str(row // 5), str(row % 5)], fgh=order\n", " )\n", " if order >= 1:\n", " g[row] = gradient[2:6]\n", " if order == 2:\n", " h[row] = [\n", " [\n", " packed[min(i, j) + max(i, j) * (max(i, j) + 1) // 2]\n", " for j in range(2, 6)\n", " ]\n", " for i in range(2, 6)\n", " ]\n", " if not all((np.isfinite(array).all() for array in (y, g, h))):\n", " raise RuntimeError(\"nonfinite ASL response\")\n", " return (y, g, h)\n", "\n", "\n", "reference = Reference()\n", "anchor_checks = phase_data(reference.model, reference.anchor, feed(F0))\n", "public, anchor_actions = reference.public(F0)\n", "transported = reference.asl(F0)\n", "public_asl_error = max((float(abs(a - b).max()) for a, b in zip(public, transported)))\n", "assert public_asl_error <= 1e-09\n", "display(pd.DataFrame([anchor_checks | {\"public_asl_max_abs\": public_asl_error}]))\n" ] }, { "cell_type": "markdown", "id": "cell-07", "metadata": {}, "source": [ "## Declare an IDAES unit and a live flowsheet\n", "\n", "The inlet, aqueous and organic ports carry five component flows, temperature\n", "and pressure. Ten outlet equations call the Engine at each requested feed.\n", "Isobutanol and NaCl feed scales range from 0.85 to 1.15 and start at 1.\n", "Minimize their sum while organic ethanol recovery is at least 0.965.\n", "Temperature and pressure are fixed; no energy equation or reacting process\n", "derivative is implied. Two free process variables remain before optimization.\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "cell-08", "metadata": { "execution": { "iopub.execute_input": "2026-10-20T20:17:26.481069Z", "iopub.status.busy": "2026-10-20T20:17:26.480953Z", "iopub.status.idle": "2026-10-20T20:17:26.489179Z", "shell.execute_reply": "2026-10-20T20:17:26.488608Z" } }, "outputs": [], "source": [ "@declare_process_block_class(\"TwoLiquidSettler\")\n", "class TwoLiquidSettlerData(UnitModelBlockData):\n", " CONFIG = UnitModelBlockData.CONFIG()\n", " CONFIG.declare(\"inlet_flows\", ConfigValue(default=()))\n", "\n", " def build(self):\n", " super().build()\n", " inlet = self.config.inlet_flows\n", " self.flow_mol_in = Var(\n", " COMPONENTS,\n", " domain=NonNegativeReals,\n", " units=FLOW,\n", " initialize=dict(zip(COMPONENTS, inlet)),\n", " )\n", " for name, amount in zip(COMPONENTS, inlet):\n", " self.flow_mol_in[name].fix(amount)\n", " self.temperature_in = Var(units=u.K, initialize=293.15)\n", " self.pressure_in = Var(units=u.Pa, initialize=100000)\n", " self.temperature_in.fix()\n", " self.pressure_in.fix()\n", " self.flow_mol_aqueous = Var(\n", " COMPONENTS, domain=NonNegativeReals, units=FLOW, initialize=0.5\n", " )\n", " self.flow_mol_organic = Var(\n", " COMPONENTS, domain=NonNegativeReals, units=FLOW, initialize=0.5\n", " )\n", " self.temperature_out = Var(PHASES, units=u.K, initialize=293.15)\n", " self.pressure_out = Var(PHASES, units=u.Pa, initialize=100000)\n", " self.inlet, self.aqueous, self.organic = (Port(), Port(), Port())\n", " for port, flow, temperature, pressure in (\n", " (self.inlet, self.flow_mol_in, self.temperature_in, self.pressure_in),\n", " (\n", " self.aqueous,\n", " self.flow_mol_aqueous,\n", " self.temperature_out[\"aqueous\"],\n", " self.pressure_out[\"aqueous\"],\n", " ),\n", " (\n", " self.organic,\n", " self.flow_mol_organic,\n", " self.temperature_out[\"organic\"],\n", " self.pressure_out[\"organic\"],\n", " ),\n", " ):\n", " port.add(flow, \"flow_mol\")\n", " port.add(temperature, \"temperature\")\n", " port.add(pressure, \"pressure\")\n", " self.temperature_mapping = Constraint(\n", " PHASES, rule=lambda b, p: b.temperature_out[p] == 293.15 * u.K\n", " )\n", " self.pressure_mapping = Constraint(\n", " PHASES, rule=lambda b, p: b.pressure_out[p] == 100000 * u.Pa\n", " )\n", " self.component_balance_residual = Expression(\n", " COMPONENTS,\n", " rule=lambda b, i: (\n", " b.flow_mol_in[i] - b.flow_mol_aqueous[i] - b.flow_mol_organic[i]\n", " ),\n", " )\n", " self.charge_residual = Expression(\n", " PHASES,\n", " rule=lambda b, p: (\n", " (b.flow_mol_aqueous if p == \"aqueous\" else b.flow_mol_organic)[\n", " \"sodium-cation\"\n", " ]\n", " - (b.flow_mol_aqueous if p == \"aqueous\" else b.flow_mol_organic)[\n", " \"chloride-anion\"\n", " ]\n", " ),\n", " )\n", "\n", "\n", "def stage(reference, start, optimize):\n", " F = F0 * np.array((1.0, 1.0, *start))\n", " (y, _, _), _ = reference.public(F, 0)\n", " m = ConcreteModel()\n", " m.fs = FlowsheetBlock(dynamic=False)\n", " m.fs.settler = TwoLiquidSettler(\n", " dynamic=False, has_holdup=False, inlet_flows=feed(F0)\n", " )\n", " b = m.fs.settler\n", " m.si, m.ss = (\n", " Var(bounds=(0.85, 1.15), initialize=start[0]),\n", " Var(bounds=(0.85, 1.15), initialize=start[1]),\n", " )\n", " if not optimize:\n", " m.si.fix()\n", " m.ss.fix()\n", " for i in (2, 3, 4):\n", " b.flow_mol_in[COMPONENTS[i]].unfix()\n", " b.flow_mol_in[COMPONENTS[i]].set_value(feed(F)[i])\n", " b.feed_links = Constraint(\n", " (2, 3, 4),\n", " rule=lambda b, i: (\n", " b.flow_mol_in[COMPONENTS[i]]\n", " == (F0[2] * m.si if i == 2 else F0[3] * m.ss) * FLOW\n", " ),\n", " )\n", " m.response = ExternalFunction(\n", " library=str(reference.library),\n", " function=NAME,\n", " units=FLOW,\n", " arg_units=[u.K, u.Pa] + [FLOW] * 4 + [None] * 3,\n", " )\n", " flows = [b.flow_mol_aqueous, b.flow_mol_organic]\n", " b.live_outlets = Constraint(\n", " range(2),\n", " range(5),\n", " rule=lambda b, p, i: (\n", " (\n", " flows[p][COMPONENTS[i]]\n", " - m.response(\n", " TP[0] * u.K,\n", " TP[1] * u.Pa,\n", " *[b.flow_mol_in[c] for c in COMPONENTS[:4]],\n", " reference.record,\n", " str(p),\n", " str(i),\n", " )\n", " )\n", " / FLOW\n", " == 0\n", " ),\n", " )\n", " for row in range(10):\n", " flows[row // 5][COMPONENTS[row % 5]].set_value(y[row])\n", " m.recovery = Constraint(\n", " expr=0.965 - b.flow_mol_organic[\"ethanol\"] / b.flow_mol_in[\"ethanol\"] <= 0\n", " )\n", " m.objective = Objective(expr=m.si + m.ss)\n", " for name in (\"dual\", \"ipopt_zL_out\", \"ipopt_zU_out\"):\n", " setattr(m, name, Suffix(direction=Suffix.IMPORT))\n", " assert degrees_of_freedom(m) == (2 if optimize else 0)\n", " return m\n" ] }, { "cell_type": "markdown", "id": "cell-09", "metadata": {}, "source": [ "## Retain independent optimality and NL derivative checks\n", "\n", "Write a named NL problem without linear presolve or scaling so the original\n", "process rows remain inspectable. Re-evaluate its Jacobian and Lagrangian\n", "Hessian through PyNumero, compare the live rows with public exact actions,\n", "and reconstruct stationarity, feasibility, multiplier sign and complementarity.\n", "The sign convention uses the negated Ipopt row duals for the NL calculation.\n", "\n", "Require stationarity and complementarity at most 10⁻⁷, feasibility at most\n", "10⁻⁸, multiplier-sign error at most 10⁻⁸ and derivative transport at most\n", "10⁻⁹. These dimensionless residual limits belong to this scaled declaration.\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "cell-10", "metadata": { "execution": { "iopub.execute_input": "2026-10-20T20:17:26.490262Z", "iopub.status.busy": "2026-10-20T20:17:26.490118Z", "iopub.status.idle": "2026-10-20T20:17:26.500524Z", "shell.execute_reply": "2026-10-20T20:17:26.499929Z" } }, "outputs": [], "source": [ "def named_file_rows(path: Path) -> tuple[list[str], list[str]]:\n", " columns = path.with_suffix(\".col\").read_text(encoding=\"utf-8\").splitlines()\n", " rows = path.with_suffix(\".row\").read_text(encoding=\"utf-8\").splitlines()\n", " return (columns, rows)\n", "\n", "\n", "def nl_checks(m, path, reference, compare=True):\n", " os.environ[\"AMPLFUNC\"] = str(reference.library)\n", " nlp = AslNLP(str(path))\n", " columns, rows = named_file_rows(path)\n", " variables = [m.find_component(name) for name in columns]\n", " constraints = [m.find_component(name) for name in rows[: nlp.n_constraints()]]\n", " x = np.array([value(v) for v in variables])\n", " lam = -np.array([m.dual.get(c, 0.0) for c in constraints])\n", " lower, upper = (nlp.primals_lb(), nlp.primals_ub())\n", " zl, zu = (\n", " np.array([m.ipopt_zL_out.get(v, 0.0) for v in variables]),\n", " -np.array([m.ipopt_zU_out.get(v, 0.0) for v in variables]),\n", " )\n", " nlp.set_primals(x)\n", " nlp.set_duals(lam)\n", " nlp.set_obj_factor(1.0)\n", " c, jac = (nlp.evaluate_constraints(), nlp.evaluate_jacobian().toarray())\n", " cl, cu = (nlp.constraints_lb(), nlp.constraints_ub())\n", " masks = (np.isfinite(lower), np.isfinite(upper), np.isfinite(cl), np.isfinite(cu))\n", " comp = np.r_[\n", " zl[masks[0]] * (x - lower)[masks[0]],\n", " zu[masks[1]] * (upper - x)[masks[1]],\n", " lam[masks[3]] * (cu - c)[masks[3]],\n", " ]\n", " metrics = {\n", " \"stationarity\": float(\n", " np.max(abs(nlp.evaluate_grad_objective() + jac.T @ lam - zl + zu))\n", " ),\n", " \"feasibility\": float(\n", " max(\n", " np.max(np.maximum(cl - c, 0)),\n", " np.max(np.maximum(c - cu, 0)),\n", " np.max(np.maximum(lower - x, 0)),\n", " np.max(np.maximum(x - upper, 0)),\n", " )\n", " ),\n", " \"multiplier_sign\": float(\n", " max(\n", " 0.0,\n", " np.max(-zl),\n", " np.max(-zu),\n", " *[\n", " -lam[i]\n", " for i in range(len(c))\n", " if cl[i] < cu[i] and np.isfinite(cu[i])\n", " ],\n", " )\n", " ),\n", " \"complementarity\": float(np.max(abs(comp))),\n", " }\n", " metrics.update(\n", " {\n", " \"lambda\": dict(zip(rows[: len(lam)], lam.tolist())),\n", " \"zL\": dict(zip(columns, zl.tolist())),\n", " \"zU\": dict(zip(columns, zu.tolist())),\n", " }\n", " )\n", " if compare:\n", " F = np.array([value(m.fs.settler.flow_mol_in[i]) for i in COMPONENTS[:4]])\n", " (y, g, h), _ = reference.public(F)\n", " D = np.diag(F0[2:4])\n", " P = np.zeros((len(columns), 2))\n", " for name, axis, factor in (\n", " (\"si\", 0, 1.0),\n", " (\"ss\", 1, 1.0),\n", " (\"fs.settler.flow_mol_in[isobutanol]\", 0, F0[2]),\n", " (\"fs.settler.flow_mol_in[sodium-cation]\", 1, F0[3]),\n", " (\"fs.settler.flow_mol_in[chloride-anion]\", 1, F0[3]),\n", " ):\n", " P[columns.index(name), axis] = factor\n", " weighted = np.zeros((2, 2))\n", " errors = []\n", " for row in range(10):\n", " index = rows.index(f\"fs.settler.live_outlets[{row // 5},{row % 5}]\")\n", " outlet = (\n", " m.fs.settler.flow_mol_aqueous\n", " if row < 5\n", " else m.fs.settler.flow_mol_organic\n", " )\n", " expected = np.zeros(len(columns))\n", " expected[columns.index(outlet[COMPONENTS[row % 5]].name)] = 1.0\n", " expected[\n", " [columns.index(f\"fs.settler.flow_mol_in[{i}]\") for i in COMPONENTS[2:4]]\n", " ] = -g[row, 2:4]\n", " errors.append(float(np.max(abs(jac[index] - expected))))\n", " errors.extend(\n", " (\n", " abs(c[index] - (value(outlet[COMPONENTS[row % 5]]) - y[row])),\n", " float(np.linalg.norm(jac[index] @ P + g[row, 2:4] @ D, ord=np.inf)),\n", " )\n", " )\n", " weighted -= lam[index] * D @ h[row, 2:4, 2:4] @ D\n", " dual = np.zeros(len(c))\n", " dual[index] = 1.0\n", " nlp.set_duals(dual)\n", " nlp.set_obj_factor(0.0)\n", " errors.append(\n", " float(\n", " np.linalg.norm(\n", " P.T @ nlp.evaluate_hessian_lag().toarray() @ P\n", " + D @ h[row, 2:4, 2:4] @ D,\n", " ord=np.inf,\n", " )\n", " )\n", " )\n", " nlp.set_duals(lam)\n", " nlp.set_obj_factor(1.0)\n", " errors.append(\n", " float(\n", " np.linalg.norm(\n", " P.T @ nlp.evaluate_hessian_lag().toarray() @ P - weighted,\n", " ord=np.inf,\n", " )\n", " )\n", " )\n", " metrics[\"nl_transport_max_abs\"] = max(errors)\n", " assert max(errors) <= 1e-09\n", " assert all(\n", " (\n", " metrics[k] <= tolerance\n", " for k, tolerance in (\n", " (\"stationarity\", 1e-07),\n", " (\"feasibility\", 1e-08),\n", " (\"multiplier_sign\", 1e-08),\n", " (\"complementarity\", 1e-07),\n", " )\n", " )\n", " ), metrics\n", " return metrics\n" ] }, { "cell_type": "markdown", "id": "cell-11", "metadata": {}, "source": [ "## Solve once with MUMPS and exact Hessians\n", "\n", "The outer second-order derivative checker must report no errors, MUMPS must\n", "appear in the applied solver options, and exact Hessian/Jacobian evaluations\n", "must occur. Require component/charge closure within 10⁻¹⁰ mol/s and recovery\n", "at least 0.965 within 10⁻⁸. The retained scale baselines are compared within\n", "10⁻⁷ absolute. A termination word alone does not satisfy these checks.\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "cell-12", "metadata": { "execution": { "iopub.execute_input": "2026-10-20T20:17:26.501927Z", "iopub.status.busy": "2026-10-20T20:17:26.501760Z", "iopub.status.idle": "2026-10-20T20:17:29.352414Z", "shell.execute_reply": "2026-10-20T20:17:29.351926Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ComponentInlet / mol s^-1Aqueous / mol s^-1Organic / mol s^-1
0water0.7198950.5414650.178430
1ethanol0.0337780.0011820.032596
2isobutanol0.2318520.0007330.231119
3sodium-cation0.0253350.0252040.000131
4chloride-anion0.0253350.0252040.000131
\n", "
" ], "text/plain": [ " Component Inlet / mol s^-1 Aqueous / mol s^-1 Organic / mol s^-1\n", "0 water 0.719895 0.541465 0.178430\n", "1 ethanol 0.033778 0.001182 0.032596\n", "2 isobutanol 0.231852 0.000733 0.231119\n", "3 sodium-cation 0.025335 0.025204 0.000131\n", "4 chloride-anion 0.025335 0.025204 0.000131" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sissobjectiverecoverymaterialchargestationarityfeasibilitymultiplier_signcomplementaritynl_transport_max_abspublic_asl_max_abs
01.0604010.9152391.9756390.9655.551115e-170.02.230670e-126.994405e-150.09.091158e-112.464695e-130.0
\n", "
" ], "text/plain": [ " si ss objective recovery material charge \\\n", "0 1.060401 0.915239 1.975639 0.965 5.551115e-17 0.0 \n", "\n", " stationarity feasibility multiplier_sign complementarity \\\n", "0 2.230670e-12 6.994405e-15 0.0 9.091158e-11 \n", "\n", " nl_transport_max_abs public_asl_max_abs \n", "0 2.464695e-13 0.0 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "NL_IO_OPTIONS = {\n", " \"symbolic_solver_labels\": True,\n", " \"linear_presolve\": False,\n", " \"scale_model\": False,\n", " \"skip_trivial_constraints\": False,\n", "}\n", "SOLVER_OPTIONS = {\n", " \"hessian_approximation\": \"exact\",\n", " \"point_perturbation_radius\": 0,\n", " \"bound_relax_factor\": 0,\n", " \"nlp_scaling_method\": \"none\",\n", " \"tol\": 1e-09,\n", " \"constr_viol_tol\": 1e-08,\n", " \"dual_inf_tol\": 1e-07,\n", " \"compl_inf_tol\": 1e-07,\n", " \"acceptable_iter\": 0,\n", " \"max_iter\": 200,\n", "}\n", "DERIVATIVE_TEST_OPTIONS = {\n", " \"derivative_test\": \"second-order\",\n", " \"derivative_test_perturbation\": 1e-06,\n", " \"derivative_test_tol\": 0.0001,\n", "}\n", "SOLVER_OPTIONS[\"linear_solver\"] = \"mumps\"\n", "\n", "\n", "def solve(reference, start, output, ipopt, optimize=True):\n", " output.mkdir(parents=True, exist_ok=True)\n", " m = stage(reference, start, optimize)\n", " path = output / \"stage.nl\"\n", " m.write(str(path), format=\"nl\", io_options=NL_IO_OPTIONS)\n", " options = {\n", " **SOLVER_OPTIONS,\n", " \"print_user_options\": \"yes\",\n", " \"file_print_level\": 5,\n", " \"output_file\": str(output / \"ipopt.log\"),\n", " }\n", " if optimize:\n", " options.update(DERIVATIVE_TEST_OPTIONS)\n", " option_file = output / \"ipopt.opt\"\n", " option_file.write_text(\"\".join((f\"{key} {v}\\n\" for key, v in options.items())))\n", " solver = get_solver(solver_options={\"option_file_name\": str(option_file)})\n", " solver.set_executable(str(ipopt))\n", " os.environ[\"AMPLFUNC\"] = str(reference.library)\n", " started = time.monotonic()\n", " result = solver.solve(\n", " str(path),\n", " load_solutions=False,\n", " suffixes=[\"dual\", \"ipopt_zL_out\", \"ipopt_zU_out\"],\n", " )\n", " assert check_optimal_termination(result), str(result.solver)\n", " columns, rows = named_file_rows(path)\n", " for index, name in enumerate(columns):\n", " v = m.find_component(name)\n", " record = result.solution[0].variable[f\"v{index}\"]\n", " v.set_value(record[\"Value\"])\n", " for suffix in (\"ipopt_zL_out\", \"ipopt_zU_out\"):\n", " getattr(m, suffix)[v] = record.get(suffix, 0.0)\n", " for index, name in enumerate(rows[:-1]):\n", " m.dual[m.find_component(name)] = (\n", " result.solution[0].constraint[f\"c{index}\"].get(\"Dual\", 0.0)\n", " )\n", " text = (output / \"ipopt.log\").read_text()\n", " applied = {\n", " m[0]: m[1]\n", " for m in re.findall(\"^\\\\s*(\\\\w+)\\\\s*=\\\\s*(\\\\S+)\\\\s+yes\\\\s*$\", text, re.M)\n", " }\n", " for key, expected_option in options.items():\n", " assert key in applied and (\n", " float(applied[key]) == expected_option\n", " if isinstance(expected_option, (int, float))\n", " else applied[key] == expected_option\n", " ), (key, applied)\n", " if optimize:\n", " assert \"No errors detected by derivative checker.\" in text\n", " for quantity in (\n", " \"Number of Lagrangian Hessian evaluations\",\n", " \"Number of equality constraint Jacobian evaluations\",\n", " ):\n", " assert any(\n", " (\n", " quantity in line and int(line.rsplit(None, 1)[-1]) > 0\n", " for line in text.splitlines()\n", " )\n", " )\n", " b = m.fs.settler\n", " y = np.array(\n", " [\n", " value(v[i])\n", " for v in (b.flow_mol_aqueous, b.flow_mol_organic)\n", " for i in COMPONENTS\n", " ]\n", " )\n", " metrics = {\n", " \"si\": value(m.si),\n", " \"ss\": value(m.ss),\n", " \"objective\": value(m.objective),\n", " \"recovery\": y[6] / F0[1],\n", " \"material\": max((abs(value(v)) for v in b.component_balance_residual.values())),\n", " \"charge\": max((abs(value(v)) for v in b.charge_residual.values())),\n", " \"elapsed_s\": time.monotonic() - started,\n", " \"flows\": y.tolist(),\n", " \"termination\": str(result.solver.termination_condition),\n", " \"options\": {\n", " **options,\n", " \"output_file\": (output / \"ipopt.log\").relative_to(ROOT).as_posix(),\n", " },\n", " **nl_checks(m, path, reference, optimize),\n", " }\n", " assert (\n", " metrics[\"material\"] <= 1e-10\n", " and metrics[\"charge\"] <= 1e-10\n", " and (metrics[\"recovery\"] >= 0.965 - 1e-08)\n", " )\n", " return metrics\n", "\n", "\n", "values = solve(reference, (1.0, 1.0), output, ipopt)\n", "values[\"public_asl_max_abs\"] = public_asl_error\n", "values[\"anchor_checks\"] = anchor_checks\n", "assert abs(values[\"si\"] - 1.060400841356) < 1e-07\n", "assert abs(values[\"ss\"] - 0.915238531399) < 1e-07\n", "log = (output / \"ipopt.log\").read_text()\n", "assert \"running with linear solver mumps.\" in log\n", "assert \"No errors detected by derivative checker.\" in log\n", "inlet = np.array(feed(F0 * np.array((1, 1, values[\"si\"], values[\"ss\"]))))\n", "display(\n", " pd.DataFrame(\n", " {\n", " \"Component\": COMPONENTS,\n", " \"Inlet / mol s^-1\": inlet,\n", " \"Aqueous / mol s^-1\": values[\"flows\"][:5],\n", " \"Organic / mol s^-1\": values[\"flows\"][5:],\n", " }\n", " )\n", ")\n", "display(\n", " pd.DataFrame(\n", " [\n", " {\n", " key: values[key]\n", " for key in (\n", " \"si\",\n", " \"ss\",\n", " \"objective\",\n", " \"recovery\",\n", " \"material\",\n", " \"charge\",\n", " \"stationarity\",\n", " \"feasibility\",\n", " \"multiplier_sign\",\n", " \"complementarity\",\n", " \"nl_transport_max_abs\",\n", " \"public_asl_max_abs\",\n", " )\n", " }\n", " ]\n", " )\n", ")\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "cell-14", "metadata": { "execution": { "iopub.execute_input": "2026-10-20T20:17:29.353877Z", "iopub.status.busy": "2026-10-20T20:17:29.353773Z", "iopub.status.idle": "2026-10-20T20:17:29.356276Z", "shell.execute_reply": "2026-10-20T20:17:29.355791Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Anchor maximum classified residual / 1Anchor reduced Hessian minimum / 1Anchor material balance error / molAnchor charge fraction error / 1Anchor relative pressure error / 1
01.027800e-140.000111.110223e-160.04.775735e-12
\n", "
" ], "text/plain": [ " Anchor maximum classified residual / 1 Anchor reduced Hessian minimum / 1 \\\n", "0 1.027800e-14 0.00011 \n", "\n", " Anchor material balance error / mol Anchor charge fraction error / 1 \\\n", "0 1.110223e-16 0.0 \n", "\n", " Anchor relative pressure error / 1 \n", "0 4.775735e-12 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Solve time / sTermination
02.685771optimal
\n", "
" ], "text/plain": [ " Solve time / s Termination\n", "0 2.685771 optimal" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Solver optionSetting
0hessian_approximationexact
1point_perturbation_radius0
2bound_relax_factor0
3nlp_scaling_methodnone
4tol0.0
5constr_viol_tol0.0
6dual_inf_tol0.0
7compl_inf_tol0.0
8acceptable_iter0
9max_iter200
10linear_solvermumps
11print_user_optionsyes
12file_print_level5
13output_filebuild/docs/settler/ipopt.log
14derivative_testsecond-order
15derivative_test_perturbation0.000001
16derivative_test_tol0.0001
\n", "
" ], "text/plain": [ " Solver option Setting\n", "0 hessian_approximation exact\n", "1 point_perturbation_radius 0\n", "2 bound_relax_factor 0\n", "3 nlp_scaling_method none\n", "4 tol 0.0\n", "5 constr_viol_tol 0.0\n", "6 dual_inf_tol 0.0\n", "7 compl_inf_tol 0.0\n", "8 acceptable_iter 0\n", "9 max_iter 200\n", "10 linear_solver mumps\n", "11 print_user_options yes\n", "12 file_print_level 5\n", "13 output_file build/docs/settler/ipopt.log\n", "14 derivative_test second-order\n", "15 derivative_test_perturbation 0.000001\n", "16 derivative_test_tol 0.0001" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ConstraintConstraint multiplier / 1
0fs.settler.live_outlets[0,0]1.678940e-10
1fs.settler.live_outlets[0,1]7.689552e-08
2fs.settler.live_outlets[0,2]1.241078e-07
3fs.settler.live_outlets[0,3]3.606912e-09
4fs.settler.live_outlets[0,4]3.606912e-09
5fs.settler.live_outlets[1,0]5.094925e-10
6fs.settler.live_outlets[1,1]6.208608e+02
7fs.settler.live_outlets[1,2]3.933417e-10
8fs.settler.live_outlets[1,3]6.923239e-07
9fs.settler.live_outlets[1,4]6.923239e-07
10recovery2.097164e+01
11fs.settler.temperature_mapping[aqueous]-0.000000e+00
12fs.settler.temperature_mapping[organic]-0.000000e+00
13fs.settler.pressure_mapping[aqueous]-0.000000e+00
14fs.settler.pressure_mapping[organic]-0.000000e+00
15fs.settler.feed_links[2]4.573620e+00
16fs.settler.feed_links[3]3.612478e+01
17fs.settler.feed_links[4]3.588218e-09
\n", "
" ], "text/plain": [ " Constraint Constraint multiplier / 1\n", "0 fs.settler.live_outlets[0,0] 1.678940e-10\n", "1 fs.settler.live_outlets[0,1] 7.689552e-08\n", "2 fs.settler.live_outlets[0,2] 1.241078e-07\n", "3 fs.settler.live_outlets[0,3] 3.606912e-09\n", "4 fs.settler.live_outlets[0,4] 3.606912e-09\n", "5 fs.settler.live_outlets[1,0] 5.094925e-10\n", "6 fs.settler.live_outlets[1,1] 6.208608e+02\n", "7 fs.settler.live_outlets[1,2] 3.933417e-10\n", "8 fs.settler.live_outlets[1,3] 6.923239e-07\n", "9 fs.settler.live_outlets[1,4] 6.923239e-07\n", "10 recovery 2.097164e+01\n", "11 fs.settler.temperature_mapping[aqueous] -0.000000e+00\n", "12 fs.settler.temperature_mapping[organic] -0.000000e+00\n", "13 fs.settler.pressure_mapping[aqueous] -0.000000e+00\n", "14 fs.settler.pressure_mapping[organic] -0.000000e+00\n", "15 fs.settler.feed_links[2] 4.573620e+00\n", "16 fs.settler.feed_links[3] 3.612478e+01\n", "17 fs.settler.feed_links[4] 3.588218e-09" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
VariableLower bound multiplier / 1Upper bound multiplier / 1
0fs.settler.flow_mol_in[isobutanol]3.920999e-10-0.000000e+00
1fs.settler.flow_mol_in[sodium-cation]3.588219e-09-0.000000e+00
2si4.320728e-101.014636e-09
3ss1.393519e-093.872378e-10
4fs.settler.flow_mol_organic[water]5.094934e-10-0.000000e+00
5fs.settler.flow_mol_organic[ethanol]2.788957e-09-0.000000e+00
6fs.settler.flow_mol_organic[isobutanol]3.933426e-10-0.000000e+00
7fs.settler.flow_mol_organic[sodium-cation]6.923239e-07-0.000000e+00
8fs.settler.flow_mol_organic[chloride-anion]6.923239e-07-0.000000e+00
9fs.settler.temperature_out[aqueous]0.000000e+00-0.000000e+00
10fs.settler.temperature_out[organic]0.000000e+00-0.000000e+00
11fs.settler.pressure_out[aqueous]0.000000e+00-0.000000e+00
12fs.settler.pressure_out[organic]0.000000e+00-0.000000e+00
13fs.settler.flow_mol_in[chloride-anion]3.588219e-09-0.000000e+00
14fs.settler.flow_mol_aqueous[water]1.678949e-10-0.000000e+00
15fs.settler.flow_mol_aqueous[ethanol]7.689552e-08-0.000000e+00
16fs.settler.flow_mol_aqueous[isobutanol]1.241078e-07-0.000000e+00
17fs.settler.flow_mol_aqueous[sodium-cation]3.606913e-09-0.000000e+00
18fs.settler.flow_mol_aqueous[chloride-anion]3.606913e-09-0.000000e+00
\n", "
" ], "text/plain": [ " Variable Lower bound multiplier / 1 \\\n", "0 fs.settler.flow_mol_in[isobutanol] 3.920999e-10 \n", "1 fs.settler.flow_mol_in[sodium-cation] 3.588219e-09 \n", "2 si 4.320728e-10 \n", "3 ss 1.393519e-09 \n", "4 fs.settler.flow_mol_organic[water] 5.094934e-10 \n", "5 fs.settler.flow_mol_organic[ethanol] 2.788957e-09 \n", "6 fs.settler.flow_mol_organic[isobutanol] 3.933426e-10 \n", "7 fs.settler.flow_mol_organic[sodium-cation] 6.923239e-07 \n", "8 fs.settler.flow_mol_organic[chloride-anion] 6.923239e-07 \n", "9 fs.settler.temperature_out[aqueous] 0.000000e+00 \n", "10 fs.settler.temperature_out[organic] 0.000000e+00 \n", "11 fs.settler.pressure_out[aqueous] 0.000000e+00 \n", "12 fs.settler.pressure_out[organic] 0.000000e+00 \n", "13 fs.settler.flow_mol_in[chloride-anion] 3.588219e-09 \n", "14 fs.settler.flow_mol_aqueous[water] 1.678949e-10 \n", "15 fs.settler.flow_mol_aqueous[ethanol] 7.689552e-08 \n", "16 fs.settler.flow_mol_aqueous[isobutanol] 1.241078e-07 \n", "17 fs.settler.flow_mol_aqueous[sodium-cation] 3.606913e-09 \n", "18 fs.settler.flow_mol_aqueous[chloride-anion] 3.606913e-09 \n", "\n", " Upper bound multiplier / 1 \n", "0 -0.000000e+00 \n", "1 -0.000000e+00 \n", "2 1.014636e-09 \n", "3 3.872378e-10 \n", "4 -0.000000e+00 \n", "5 -0.000000e+00 \n", "6 -0.000000e+00 \n", "7 -0.000000e+00 \n", "8 -0.000000e+00 \n", "9 -0.000000e+00 \n", "10 -0.000000e+00 \n", "11 -0.000000e+00 \n", "12 -0.000000e+00 \n", "13 -0.000000e+00 \n", "14 -0.000000e+00 \n", "15 -0.000000e+00 \n", "16 -0.000000e+00 \n", "17 -0.000000e+00 \n", "18 -0.000000e+00 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(pd.DataFrame([values[\"anchor_checks\"]]).rename(columns={\n", " \"classified\": \"Anchor maximum classified residual / 1\",\n", " \"hessian\": \"Anchor reduced Hessian minimum / 1\",\n", " \"material\": \"Anchor material balance error / mol\",\n", " \"charge\": \"Anchor charge fraction error / 1\",\n", " \"pressure\": \"Anchor relative pressure error / 1\",\n", "}))\n", "display(pd.DataFrame([{\n", " \"Solve time / s\": values[\"elapsed_s\"],\n", " \"Termination\": values[\"termination\"],\n", "}]))\n", "display(pd.DataFrame(values[\"options\"].items(), columns=[\"Solver option\", \"Setting\"]))\n", "display(pd.DataFrame({\n", " \"Constraint\": list(values[\"lambda\"]),\n", " \"Constraint multiplier / 1\": list(values[\"lambda\"].values()),\n", "}))\n", "display(pd.DataFrame({\n", " \"Variable\": list(values[\"zL\"]),\n", " \"Lower bound multiplier / 1\": list(values[\"zL\"].values()),\n", " \"Upper bound multiplier / 1\": list(values[\"zU\"].values()),\n", "}))\n" ] }, { "cell_type": "markdown", "id": "cell-13", "metadata": {}, "source": [ "## Meaning and limits\n", "\n", "\n", "The optimum is local on the declared nonreactive two-liquid branch. The\n", "component flows, balances and derivative comparisons verify this process\n", "calculation; they do not establish a global optimum or independent physical\n", "validation of the source parameters. Reacting-process and thermal derivative\n", "qualification remain outside this example. Text logs and numerical outputs\n", "remain under `build/docs/settler`; no solver binary is copied there.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.14" } }, "nbformat": 4, "nbformat_minor": 5 }