{
"cells": [
{
"cell_type": "markdown",
"id": "cell-00",
"metadata": {},
"source": [
"# Water–isobutanol solved-state actions\n",
"\n",
"**Evidence: numerical verification.** Differentiate log activity of isobutanol in the water-rich phase.\n",
"\n",
"\n",
"The canonical input is the neutral water/isobutanol subset of\n",
"`khudaida-2026-figure-2-electrolyte-lle/7`, at 293.15 K and 100,000 Pa.\n",
"Khudaida et al. (2026), Tables 2, 5–7\n",
"([paper](https://doi.org/10.1021/acs.jced.5c00780)), and the packet's cited\n",
"association rules own the inputs. Version 7 retains the Wolbach–Sandler\n",
"unlike association size factor. The feed is 0.6 mol water + 0.4 mol isobutanol.\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:38.204881Z",
"iopub.status.busy": "2026-10-20T20:17:38.204745Z",
"iopub.status.idle": "2026-10-20T20:17:38.479303Z",
"shell.execute_reply": "2026-10-20T20:17:38.478806Z"
}
},
"outputs": [],
"source": [
"from pathlib import Path\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, regression as rg\n",
"\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",
"\n",
"\n",
"def equilibrium_values(central_state):\n",
" \"\"\"Keep the returned physical state and the equation residuals together.\"\"\"\n",
" assert central_state.success, central_state.message\n",
" assert central_state.requested_tolerance_met\n",
" assert central_state.classified_residuals\n",
" residual = max(map(abs, central_state.classified_residuals), default=0)\n",
" assert residual <= 1e-10\n",
" return dict(\n",
" temperature_K=central_state.temperature,\n",
" pressure_Pa=central_state.pressure,\n",
" amounts_mol=central_state.amounts,\n",
" mole_fractions=central_state.mole_fractions,\n",
" molar_densities_mol_m3=central_state.molar_densities,\n",
" residual=residual,\n",
" linear_solver=central_state.linear_solver,\n",
" )\n"
]
},
{
"cell_type": "markdown",
"id": "cell-03",
"metadata": {},
"source": [
"## Compile once and request exact actions\n",
"\n",
"Activate the isobutanol a/b self-association energy divided by Boltzmann's\n",
"constant, measured in K. The two directions are **1 K temperature** and\n",
"**1 K association energy/k** increments. Solve the central state, compile the\n",
"same defining equations, then request first contractions and one mixed\n",
"contraction. Each contraction of log activity is dimensionless because its\n",
"path variable is dimensionless.\n",
"\n",
"Divide by the physical increments to obtain partial derivatives in K⁻¹ and\n",
"K⁻². Re-solve at T ± 0.01 K and T ± 0.005 K only to verify the exact\n",
"temperature partial. Require the refined difference below 10⁻⁷ K⁻¹.\n",
"The native central and perturbed solves must retain residuals at most 10⁻¹⁰.\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "cell-04",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-20T20:17:38.480756Z",
"iopub.status.busy": "2026-10-20T20:17:38.480620Z",
"iopub.status.idle": "2026-10-20T20:17:38.501887Z",
"shell.execute_reply": "2026-10-20T20:17:38.501248Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Quantity | \n",
" Value | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" ln activity | \n",
" -1.329822 | \n",
"
\n",
" \n",
" | 1 | \n",
" Temperature action | \n",
" 0.067119 | \n",
"
\n",
" \n",
" | 2 | \n",
" Association-energy action | \n",
" -0.001595 | \n",
"
\n",
" \n",
" | 3 | \n",
" Mixed action | \n",
" 0.000017 | \n",
"
\n",
" \n",
" | 4 | \n",
" Temperature partial / K^-1 | \n",
" 0.067119 | \n",
"
\n",
" \n",
" | 5 | \n",
" Association-energy partial / K^-1 | \n",
" -0.001595 | \n",
"
\n",
" \n",
" | 6 | \n",
" Mixed partial / K^-2 | \n",
" 0.000017 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Quantity Value\n",
"0 ln activity -1.329822\n",
"1 Temperature action 0.067119\n",
"2 Association-energy action -0.001595\n",
"3 Mixed action 0.000017\n",
"4 Temperature partial / K^-1 0.067119\n",
"5 Association-energy partial / K^-1 -0.001595\n",
"6 Mixed partial / K^-2 0.000017"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"parameters = Parameters.from_json(\n",
" PACKETS / \"khudaida-2026-figure-2-electrolyte-lle/7/parameters/parameters.json\",\n",
" components=[\"water\", \"isobutanol\"],\n",
")\n",
"pair = rg.active_parameter(\n",
" parameters, \"association_energy_over_k\", ((\"isobutanol\", \"a\"), (\"isobutanol\", \"b\"))\n",
")\n",
"model = Mixture(parameters, active_parameters=[pair])\n",
"problem = eq.Problem(\n",
" T=293.15,\n",
" P=100000.0,\n",
" feed=eq.Amounts({\"water\": 0.6, \"isobutanol\": 0.4}),\n",
" phases=[\n",
" eq.Phase(\"Lw\", kind=\"liquid\", composition_guess=(0.999, 0.001)),\n",
" eq.Phase(\"Lo\", kind=\"liquid\", composition_guess=(0.3, 0.7)),\n",
" ],\n",
")\n",
"central_state = eq.solve_equilibrium(model, problem)\n",
"compiled_problem = eq.compile_problem(model, problem)\n",
"observables = [\n",
" eq.SolvedStateObservable(eq.SolvedStateObservableKind.PhaseLogActivity, 0, 1)\n",
"]\n",
"directions = [\n",
" eq.SolvedStateActionDirection(1, 0, [0, 0], [0]),\n",
" eq.SolvedStateActionDirection(0, 0, [0, 0], [1]),\n",
"]\n",
"gradient = eq.solved_state_first_actions(\n",
" compiled_problem, central_state, observables, directions\n",
")\n",
"mixed = eq.solved_state_actions(\n",
" compiled_problem,\n",
" central_state,\n",
" eq.SolvedStateActionRequest(observables, directions),\n",
")\n",
"assert all(\n",
" (\n",
" b.results[0].status == eq.SolvedStateActionStatus.Available\n",
" for b in [*gradient, mixed]\n",
" )\n",
")\n",
"\n",
"\n",
"def value(t):\n",
" declaration = eq.Problem(T=t, P=100000.0, feed=problem.feed, phases=problem.phases)\n",
" perturbed_state = eq.solve_equilibrium(model, declaration)\n",
" equilibrium_values(perturbed_state)\n",
" perturbed_actions = eq.solved_state_first_actions(\n",
" eq.compile_problem(model, declaration),\n",
" perturbed_state,\n",
" observables,\n",
" [directions[0]],\n",
" )[0]\n",
" assert perturbed_actions.results[0].status == eq.SolvedStateActionStatus.Available\n",
" return perturbed_actions.results[0].value\n",
"\n",
"\n",
"differences = [(value(293.15 + h) - value(293.15 - h)) / (2 * h) for h in (0.01, 0.005)]\n",
"temperature_increment_K = 1.0\n",
"parameter_increment_K = 1.0\n",
"temperature_partial = gradient[0].results[0].action / temperature_increment_K\n",
"parameter_partial = gradient[1].results[0].action / parameter_increment_K\n",
"mixed_partial = mixed.results[0].action / (\n",
" temperature_increment_K * parameter_increment_K\n",
")\n",
"assert abs(differences[1] - temperature_partial) < 1e-07\n",
"values = dict(\n",
" equilibrium=equilibrium_values(central_state),\n",
" log_activity=gradient[0].results[0].value,\n",
" first_actions=[b.results[0].action for b in gradient],\n",
" mixed_action=mixed.results[0].action,\n",
" temperature_difference_checks=differences,\n",
")\n",
"values[\"temperature_partial_K^-1\"] = temperature_partial\n",
"values[\"parameter_partial_K^-1\"] = parameter_partial\n",
"values[\"mixed_partial_K^-2\"] = mixed_partial\n",
"values[\"finite_difference_steps_K\"] = [0.01, 0.005]\n",
"values[\"refined_error_K^-1\"] = differences[1] - temperature_partial\n",
"values[\"rank\"] = mixed.diagnostics.rank\n",
"values[\"dimension\"] = mixed.diagnostics.state_dimension\n",
"values[\"linear_residual\"] = mixed.diagnostics.maximum_linear_residual\n",
"display(\n",
" pd.DataFrame(\n",
" {\n",
" \"Quantity\": [\n",
" \"ln activity\",\n",
" \"Temperature action\",\n",
" \"Association-energy action\",\n",
" \"Mixed action\",\n",
" \"Temperature partial / K^-1\",\n",
" \"Association-energy partial / K^-1\",\n",
" \"Mixed partial / K^-2\",\n",
" ],\n",
" \"Value\": [\n",
" values[\"log_activity\"],\n",
" *values[\"first_actions\"],\n",
" values[\"mixed_action\"],\n",
" temperature_partial,\n",
" parameter_partial,\n",
" mixed_partial,\n",
" ],\n",
" }\n",
" )\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cell-06",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-20T20:17:38.503046Z",
"iopub.status.busy": "2026-10-20T20:17:38.502881Z",
"iopub.status.idle": "2026-10-20T20:17:38.505303Z",
"shell.execute_reply": "2026-10-20T20:17:38.504872Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Temperature / K | \n",
" Pressure / Pa | \n",
" Phase amounts / mol | \n",
" Mole fractions / 1 | \n",
" Phase densities / mol m^-3 | \n",
" Maximum classified residual / 1 | \n",
" Linear solver | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 293.15 | \n",
" 100000.0 | \n",
" [0.2853182931107769, 0.7146817068892232] | \n",
" [0.9943748683039002, 0.005625131696099895, 0.4425559753585558, 0.5574440246414442] | \n",
" [54198.3600462764, 16912.29929663858] | \n",
" 2.073248e-14 | \n",
" | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Temperature / K Pressure / Pa Phase amounts / mol \\\n",
"0 293.15 100000.0 [0.2853182931107769, 0.7146817068892232] \n",
"\n",
" Mole fractions / 1 \\\n",
"0 [0.9943748683039002, 0.005625131696099895, 0.4425559753585558, 0.5574440246414442] \n",
"\n",
" Phase densities / mol m^-3 Maximum classified residual / 1 \\\n",
"0 [54198.3600462764, 16912.29929663858] 2.073248e-14 \n",
"\n",
" Linear solver \n",
"0 "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Finite-difference step / K | \n",
" Temperature partial check / K^-1 | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 0.010 | \n",
" 0.067119 | \n",
"
\n",
" \n",
" | 1 | \n",
" 0.005 | \n",
" 0.067119 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Finite-difference step / K Temperature partial check / K^-1\n",
"0 0.010 0.067119\n",
"1 0.005 0.067119"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Refined difference error / K^-1 | \n",
" Rank / 1 | \n",
" State dimension / 1 | \n",
" Maximum linear residual / 1 | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 3.765263e-11 | \n",
" 10 | \n",
" 10 | \n",
" 1.242394e-16 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Refined difference error / K^-1 Rank / 1 State dimension / 1 \\\n",
"0 3.765263e-11 10 10 \n",
"\n",
" Maximum linear residual / 1 \n",
"0 1.242394e-16 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(pd.DataFrame([values[\"equilibrium\"]]).rename(columns={\n",
" \"temperature_K\": \"Temperature / K\",\n",
" \"pressure_Pa\": \"Pressure / Pa\",\n",
" \"amounts_mol\": \"Phase amounts / mol\",\n",
" \"mole_fractions\": \"Mole fractions / 1\",\n",
" \"molar_densities_mol_m3\": \"Phase densities / mol m^-3\",\n",
" \"residual\": \"Maximum classified residual / 1\",\n",
" \"linear_solver\": \"Linear solver\",\n",
" }))\n",
"display(pd.DataFrame({\n",
" \"Finite-difference step / K\": values[\"finite_difference_steps_K\"],\n",
" \"Temperature partial check / K^-1\": values[\"temperature_difference_checks\"],\n",
"}))\n",
"display(pd.DataFrame([{\n",
" \"Refined difference error / K^-1\": values[\"refined_error_K^-1\"],\n",
" \"Rank / 1\": values[\"rank\"],\n",
" \"State dimension / 1\": values[\"dimension\"],\n",
" \"Maximum linear residual / 1\": values[\"linear_residual\"],\n",
"}]))\n"
]
},
{
"cell_type": "markdown",
"id": "cell-05",
"metadata": {},
"source": [
"## Meaning and limits\n",
"\n",
"\n",
"These derivatives apply to the smooth declared branch with unchanged supports\n",
"and distinct liquid phases. Singular, coalesced or disappearing phases need\n",
"their returned diagnostic rather than a substituted derivative. Finite\n",
"differences verify exact native actions here; they do not supply derivatives\n",
"for an optimizer or extend the source validity range. No physical validation\n",
"or parameter-uncertainty estimate follows from the derivative comparison.\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
}