{
"cells": [
{
"cell_type": "markdown",
"id": "cell-00",
"metadata": {},
"source": [
"# Methane ASL properties\n",
"\n",
"**Evidence: numerical verification.** Evaluate the compiled pressure-property callback at 298.15 K and 100,000 Pa, methane vapor.\n",
"\n",
"\n",
"Gross and Sadowski (2001), Table 2\n",
"([paper](https://doi.org/10.1021/ie0003887)), supplies the methane coefficients\n",
"in `gross-2001-methane-ethane/3`. McBride, Zehe and Gordon (2002),\n",
"[NASA/TP-2002-211556](https://ntrs.nasa.gov/citations/20020085330), Appendix D,\n",
"supplies the NASA9 methane record in `nasa-glenn-2002-hda-species/1`.\n",
"The packet preserves the source gas constant, 8.314510 J/(mol K), and\n",
"100,000 Pa standard pressure; `ThermochemistryPacket` performs the native\n",
"gas-constant conversion. The first methane interval is 200–1000 K.\n",
"Zotero parent: `AQDUURW4`; PDF: `SYJLCAXZ`.\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:30.441415Z",
"iopub.status.busy": "2026-10-20T20:17:30.441323Z",
"iopub.status.idle": "2026-10-20T20:17:30.726860Z",
"shell.execute_reply": "2026-10-20T20:17:30.726342Z"
}
},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import pandas as pd\n",
"from IPython.display import display\n",
"from epcsaft import (\n",
" Parameters,\n",
" Pure,\n",
" ThermochemistryPacket,\n",
" PropertyObservable,\n",
" PressureActionDirection,\n",
")\n",
"from epcsaft import _core\n",
"from epcsaft.eos import property_asl_record\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"
]
},
{
"cell_type": "markdown",
"id": "cell-03",
"metadata": {},
"source": [
"## Load the physical records\n",
"\n",
"An ASL record serializes the installed model for an external function.\n",
"Locate `libepcsaft_asl.so` beside the installed `_core` extension.\n",
"This example needs Pyomo and IDAES, but no external Ipopt executable.\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "cell-04",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-20T20:17:30.728582Z",
"iopub.status.busy": "2026-10-20T20:17:30.728390Z",
"iopub.status.idle": "2026-10-20T20:17:30.732193Z",
"shell.execute_reply": "2026-10-20T20:17:30.731365Z"
}
},
"outputs": [],
"source": [
"p = Parameters.from_json(\n",
" PACKETS / \"gross-2001-methane-ethane/3/parameters/parameters.json\",\n",
" components=[\"methane\"],\n",
")\n",
"thermal = ThermochemistryPacket(\n",
" ROOT / \"data/packets/nasa-glenn-2002-hda-species/1/parameters/nasa9-records.json\"\n",
").select([\"methane\"])\n",
"model = Pure(p, thermochemistry=thermal)\n"
]
},
{
"cell_type": "markdown",
"id": "cell-05",
"metadata": {},
"source": [
"## Compare callback values and exact derivatives\n",
"\n",
"Rows are molar density, total enthalpy and ln fugacity coefficient. Numerical\n",
"arguments are `(T / K, P / Pa, w / 1)` with pure-component weight w = 1.\n",
"Compare values, T/P gradients and TT/TP/PP Hessian entries with native actions\n",
"normalized by 1 K and 1 Pa increments. The criterion is 10⁻¹⁰ relative with\n",
"a 10⁻²² absolute floor. Hessians are packed column-wise as TT, TP, PP, Tw,\n",
"Pw, ww. Gradient units are observable-unit per K or Pa; second derivatives\n",
"divide by the two corresponding units. The complete arrays are printed.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cell-06",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-20T20:17:30.733459Z",
"iopub.status.busy": "2026-10-20T20:17:30.733342Z",
"iopub.status.idle": "2026-10-20T20:17:30.945480Z",
"shell.execute_reply": "2026-10-20T20:17:30.944902Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"epcsaft_property prepared record 1 of at most 16\n"
]
},
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Quantity | \n",
" Value | \n",
" Maximum absolute transport error | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Density / mol m^-3 | \n",
" 40.415468 | \n",
" 0.0 | \n",
"
\n",
" \n",
" | 1 | \n",
" Total enthalpy / J mol^-1 | \n",
" -74616.276249 | \n",
" 0.0 | \n",
"
\n",
" \n",
" | 2 | \n",
" ln fugacity coefficient / 1 | \n",
" -0.001879 | \n",
" 0.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Quantity Value Maximum absolute transport error\n",
"0 Density / mol m^-3 40.415468 0.0\n",
"1 Total enthalpy / J mol^-1 -74616.276249 0.0\n",
"2 ln fugacity coefficient / 1 -0.001879 0.0"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from pyomo.environ import ExternalFunction\n",
"import idaes\n",
"\n",
"text = property_asl_record(model)\n",
"fn = ExternalFunction(\n",
" library=str(Path(_core.__file__).resolve().with_name(\"libepcsaft_asl.so\")),\n",
" function=\"epcsaft_pressure_property\",\n",
")\n",
"temperature_increment_K = 1.0\n",
"pressure_increment_Pa = 1.0\n",
"t, p = (\n",
" PressureActionDirection(temperature=temperature_increment_K),\n",
" PressureActionDirection(pressure=pressure_increment_Pa),\n",
")\n",
"obs = [PropertyObservable.TotalEnthalpy, PropertyObservable.LogFugacityCoefficient]\n",
"batches = [\n",
" model.pressure_actions(\n",
" 298.15, 100000.0, phase=\"vapor\", observables=obs, directions=ds\n",
" )\n",
" for ds in ([t], [p], [t, t], [t, p], [p, p])\n",
"]\n",
"native = [\n",
" [\n",
" batches[0].molar_density,\n",
" *(b.density_action[0] for b in batches[:2]),\n",
" *(b.density_mixed_action for b in batches[2:]),\n",
" ]\n",
"]\n",
"native += [\n",
" [batches[0].results[k].value, *(b.results[k].action for b in batches)]\n",
" for k in range(2)\n",
"]\n",
"physical_increments = (\n",
" 1.0,\n",
" temperature_increment_K,\n",
" pressure_increment_Pa,\n",
" temperature_increment_K**2,\n",
" temperature_increment_K * pressure_increment_Pa,\n",
" pressure_increment_Pa**2,\n",
")\n",
"native = [\n",
" [entry / increment for entry, increment in zip(row, physical_increments)]\n",
" for row in native\n",
"]\n",
"rows = []\n",
"for row, expected in enumerate(native):\n",
" value, gradient, hessian = fn.evaluate_fgh(\n",
" (298.15, 100000.0, 1.0, text, str(row), \"1\")\n",
" )\n",
" actual = [value, *gradient[:2], *hessian[:3]]\n",
" assert all(\n",
" (abs(a - b) <= 1e-10 * max(abs(b), 1e-12) for a, b in zip(actual, expected))\n",
" )\n",
" rows.append(\n",
" dict(\n",
" value=value,\n",
" gradient=gradient[:3],\n",
" hessian=hessian[:6],\n",
" maximum_absolute_error=max((abs(a - b) for a, b in zip(actual, expected))),\n",
" )\n",
" )\n",
"values = dict(rows=rows, idaes_version=idaes.__version__, record_characters=len(text))\n",
"display(\n",
" pd.DataFrame(\n",
" {\n",
" \"Quantity\": [\n",
" \"Density / mol m^-3\",\n",
" \"Total enthalpy / J mol^-1\",\n",
" \"ln fugacity coefficient / 1\",\n",
" ],\n",
" \"Value\": [row[\"value\"] for row in rows],\n",
" \"Maximum absolute transport error\": [\n",
" row[\"maximum_absolute_error\"] for row in rows\n",
" ],\n",
" }\n",
" )\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "cell-08",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-20T20:17:30.947059Z",
"iopub.status.busy": "2026-10-20T20:17:30.946922Z",
"iopub.status.idle": "2026-10-20T20:17:30.949173Z",
"shell.execute_reply": "2026-10-20T20:17:30.948769Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Quantity | \n",
" T partial / (mol m^-3) K^-1 | \n",
" P partial / (mol m^-3) Pa^-1 | \n",
" x methane partial / (mol m^-3) | \n",
" T,T partial / (mol m^-3) K^-2 | \n",
" T,P partial / (mol m^-3) K^-1 Pa^-1 | \n",
" P,P partial / (mol m^-3) Pa^-2 | \n",
" T,x partial / (mol m^-3) K^-1 | \n",
" P,x partial / (mol m^-3) Pa^-1 | \n",
" x,x partial / (mol m^-3) | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Density | \n",
" -0.136446 | \n",
" 0.000405 | \n",
" 0.0 | \n",
" 0.000926 | \n",
" -0.000001 | \n",
" 1.522167e-11 | \n",
" -0.0 | \n",
" -0.0 | \n",
" -0.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Quantity T partial / (mol m^-3) K^-1 P partial / (mol m^-3) Pa^-1 \\\n",
"0 Density -0.136446 0.000405 \n",
"\n",
" x methane partial / (mol m^-3) T,T partial / (mol m^-3) K^-2 \\\n",
"0 0.0 0.000926 \n",
"\n",
" T,P partial / (mol m^-3) K^-1 Pa^-1 P,P partial / (mol m^-3) Pa^-2 \\\n",
"0 -0.000001 1.522167e-11 \n",
"\n",
" T,x partial / (mol m^-3) K^-1 P,x partial / (mol m^-3) Pa^-1 \\\n",
"0 -0.0 -0.0 \n",
"\n",
" x,x partial / (mol m^-3) \n",
"0 -0.0 "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Quantity | \n",
" T partial / (J mol^-1) K^-1 | \n",
" P partial / (J mol^-1) Pa^-1 | \n",
" x methane partial / (J mol^-1) | \n",
" T,T partial / (J mol^-1) K^-2 | \n",
" T,P partial / (J mol^-1) K^-1 Pa^-1 | \n",
" P,P partial / (J mol^-1) Pa^-2 | \n",
" T,x partial / (J mol^-1) K^-1 | \n",
" P,x partial / (J mol^-1) Pa^-1 | \n",
" x,x partial / (J mol^-1) | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Total enthalpy | \n",
" 35.784846 | \n",
" -0.000163 | \n",
" 0.0 | \n",
" 0.03634 | \n",
" 9.387477e-07 | \n",
" -2.174993e-12 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Quantity T partial / (J mol^-1) K^-1 P partial / (J mol^-1) Pa^-1 \\\n",
"0 Total enthalpy 35.784846 -0.000163 \n",
"\n",
" x methane partial / (J mol^-1) T,T partial / (J mol^-1) K^-2 \\\n",
"0 0.0 0.03634 \n",
"\n",
" T,P partial / (J mol^-1) K^-1 Pa^-1 P,P partial / (J mol^-1) Pa^-2 \\\n",
"0 9.387477e-07 -2.174993e-12 \n",
"\n",
" T,x partial / (J mol^-1) K^-1 P,x partial / (J mol^-1) Pa^-1 \\\n",
"0 0.0 0.0 \n",
"\n",
" x,x partial / (J mol^-1) \n",
"0 0.0 "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Quantity | \n",
" T partial / (1) K^-1 | \n",
" P partial / (1) Pa^-1 | \n",
" x methane partial / (1) | \n",
" T,T partial / (1) K^-2 | \n",
" T,P partial / (1) K^-1 Pa^-1 | \n",
" P,P partial / (1) Pa^-2 | \n",
" T,x partial / (1) K^-1 | \n",
" P,x partial / (1) Pa^-1 | \n",
" x,x partial / (1) | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" ln fugacity coefficient | \n",
" 0.000022 | \n",
" -1.878545e-08 | \n",
" 0.0 | \n",
" -2.742543e-07 | \n",
" 2.203641e-10 | \n",
" 1.230322e-16 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Quantity T partial / (1) K^-1 P partial / (1) Pa^-1 \\\n",
"0 ln fugacity coefficient 0.000022 -1.878545e-08 \n",
"\n",
" x methane partial / (1) T,T partial / (1) K^-2 \\\n",
"0 0.0 -2.742543e-07 \n",
"\n",
" T,P partial / (1) K^-1 Pa^-1 P,P partial / (1) Pa^-2 \\\n",
"0 2.203641e-10 1.230322e-16 \n",
"\n",
" T,x partial / (1) K^-1 P,x partial / (1) Pa^-1 x,x partial / (1) \n",
"0 0.0 0.0 0.0 "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" IDAES version | \n",
" ASL record characters / 1 | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 2.12.0 | \n",
" 1590 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" IDAES version ASL record characters / 1\n",
"0 2.12.0 1590"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"for row, quantity, unit in zip(\n",
" rows,\n",
" (\"Density\", \"Total enthalpy\", \"ln fugacity coefficient\"),\n",
" (\"mol m^-3\", \"J mol^-1\", \"1\"),\n",
"):\n",
" gradient = row[\"gradient\"]\n",
" hessian = row[\"hessian\"]\n",
" display(pd.DataFrame([{\n",
" \"Quantity\": quantity,\n",
" f\"T partial / ({unit}) K^-1\": gradient[0],\n",
" f\"P partial / ({unit}) Pa^-1\": gradient[1],\n",
" f\"x methane partial / ({unit})\": gradient[2],\n",
" f\"T,T partial / ({unit}) K^-2\": hessian[0],\n",
" f\"T,P partial / ({unit}) K^-1 Pa^-1\": hessian[1],\n",
" f\"P,P partial / ({unit}) Pa^-2\": hessian[2],\n",
" f\"T,x partial / ({unit}) K^-1\": hessian[3],\n",
" f\"P,x partial / ({unit}) Pa^-1\": hessian[4],\n",
" f\"x,x partial / ({unit})\": hessian[5],\n",
" }]))\n",
"display(pd.DataFrame([{\n",
" \"IDAES version\": values[\"idaes_version\"],\n",
" \"ASL record characters / 1\": values[\"record_characters\"],\n",
"}]))\n"
]
},
{
"cell_type": "markdown",
"id": "cell-07",
"metadata": {},
"source": [
"## Meaning and limits\n",
"\n",
"\n",
"The comparison verifies value and derivative transport for this neutral\n",
"pressure chart. It neither solves a flowsheet nor qualifies ionic or reacting\n",
"process derivatives. See the two-liquid settler example for a live IDAES outer\n",
"optimization. Thermodynamic accuracy retains the limits of the underlying\n",
"parameter and ideal thermal records.\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
}