{"spec_id":"phase-diagram-pt","library":"muix","language":"javascript","code":"// anyplot.ai\n// phase-diagram-pt: Thermodynamic Phase Diagram (Pressure-Temperature)\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 89/100 | Updated: 2026-11-08\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst PHASE_LABELS = { solid: \"Solid\", liquid: \"Liquid\", gas: \"Gas\", supercritical: [\"Supercritical\", \"fluid\"] };\nconst TRIPLE_LABEL = \"Triple point\";\nconst CRITICAL_LABEL = \"Critical point\";\nconst SUPERSCRIPT = { \"-\": \"⁻\", 0: \"⁰\", 1: \"¹\", 2: \"²\", 3: \"³\", 4: \"⁴\", 5: \"⁵\", 6: \"⁶\", 7: \"⁷\", 8: \"⁸\", 9: \"⁹\" };\nconst X_PAD_LEFT = 0.04; // share of the temperature span\nconst X_PAD_RIGHT = 0.12; // wider on the right, where the supercritical region lies\nconst Y_PAD_DECADES = 0.05; // share of the log-pressure span\nconst TITLE_H = 48;\nconst SUBTITLE_H = 28;\nconst LEGEND_H = 40;\n\n// Columns: set these to your data's column names\nconst TEMPERATURE = \"Temperature (K)\"; // role: temperature\nconst PRESSURE = \"Pressure (Pa)\"; // role: pressure\nconst BOUNDARY = \"Phase boundary\"; // role: phase_boundary\n\n// Data: replace the two points and the records with your own, one object per row\n// Water (IAPWS triple and critical point). Vaporization follows the Wagner-Pruss saturation\n// equation (IAPWS 1992), smooth from the triple point and exact at the critical point;\n// sublimation follows Clausius-Clapeyron; the Simon-Glatzel melting equation for ice Ih, whose\n// negative slope is water's anomaly, runs up to 209.9 MPa, where ice Ih gives way to ice III\nconst TRIPLE_POINT = { [TEMPERATURE]: 273.16, [PRESSURE]: 611.657 }; // role: triple_point\nconst CRITICAL_POINT = { [TEMPERATURE]: 647.096, [PRESSURE]: 22.064e6 }; // role: critical_point\nconst GAS_CONSTANT = 8.314; // J/(mol K)\nconst ICE_IH_LIMIT = 209.9e6; // Pa, the ice Ih–ice III–liquid triple point\nconst WAGNER_PRUSS = [\n  [-7.85951783, 1],\n  [1.84408259, 1.5],\n  [-11.7866497, 3],\n  [22.6807411, 3.5],\n  [-15.9618719, 4],\n  [1.80122502, 7.5],\n]; // [coefficient, exponent of 1 − T/Tc]\nconst TT = TRIPLE_POINT[TEMPERATURE];\nconst PT = TRIPLE_POINT[PRESSURE];\nconst TC = CRITICAL_POINT[TEMPERATURE];\nconst PC = CRITICAL_POINT[PRESSURE];\nconst N_CURVE = 60; // samples per boundary curve\nconst melting = Array.from({ length: N_CURVE }, (_, i) => {\n  const p = 10 ** (Math.log10(PT) + (i * (Math.log10(ICE_IH_LIMIT) - Math.log10(PT))) / (N_CURVE - 1));\n  return { [BOUNDARY]: \"Solid–liquid (melting)\", [TEMPERATURE]: TT * (1 + (p - PT) / -395.2e6) ** (1 / 9), [PRESSURE]: p };\n});\nconst vaporization = Array.from({ length: N_CURVE }, (_, i) => {\n  const temp = TT + (i * (TC - TT)) / (N_CURVE - 1);\n  const tau = Math.max(0, 1 - temp / TC); // never below 0, where the fractional powers are undefined\n  const lnRatio = (TC / temp) * WAGNER_PRUSS.reduce((sum, [a, e]) => sum + a * tau ** e, 0);\n  return { [BOUNDARY]: \"Liquid–gas (vaporization)\", [TEMPERATURE]: temp, [PRESSURE]: PC * Math.exp(lnRatio) };\n});\nconst sublimation = Array.from({ length: N_CURVE }, (_, i) => {\n  const temp = 200 + (i * (TT - 200)) / (N_CURVE - 1);\n  return { [BOUNDARY]: \"Solid–gas (sublimation)\", [TEMPERATURE]: temp, [PRESSURE]: PT * Math.exp((-51000 / GAS_CONSTANT) * (1 / temp - 1 / TT)) };\n});\nconst data = [...melting, ...vaporization, ...sublimation];\n\n// Plot: reads only data and the constants above\nconst boundaryNames = [...new Set(data.map((d) => d[BOUNDARY]))];\nconst boundaries = boundaryNames.map((name) => {\n  const rows = data.filter((d) => d[BOUNDARY] === name).sort((a, b) => a[TEMPERATURE] - b[TEMPERATURE]);\n  return { name, temperature: rows.map((d) => d[TEMPERATURE]), pressure: rows.map((d) => d[PRESSURE]) };\n});\n\nconst allT = [...data.map((d) => d[TEMPERATURE]), TRIPLE_POINT[TEMPERATURE], CRITICAL_POINT[TEMPERATURE]];\nconst allLogP = [...data.map((d) => d[PRESSURE]), TRIPLE_POINT[PRESSURE], CRITICAL_POINT[PRESSURE]].map((p) => Math.log10(p));\nconst tSpan = Math.max(...allT) - Math.min(...allT);\nconst xMin = Math.min(...allT) - tSpan * X_PAD_LEFT;\nconst xMax = Math.max(...allT) + tSpan * X_PAD_RIGHT;\nconst logSpan = Math.max(...allLogP) - Math.min(...allLogP);\nconst logMin = Math.min(...allLogP) - logSpan * Y_PAD_DECADES;\nconst logMax = Math.max(...allLogP) + logSpan * Y_PAD_DECADES;\nconst decades = Array.from({ length: Math.floor(logMax) - Math.ceil(logMin) + 1 }, (_, i) => 10 ** (Math.ceil(logMin) + i));\n\nconst tUnit = (TEMPERATURE.match(/\\(([^)]+)\\)/) ?? [\"\", \"\"])[1];\nconst pUnit = (PRESSURE.match(/\\(([^)]+)\\)/) ?? [\"\", \"\"])[1];\n\n// Region label anchors from the two points and the axis extents (log midpoints in pressure)\nconst logPt = Math.log10(PT);\nconst logPc = Math.log10(PC);\nconst regionAnchors = [\n  { text: PHASE_LABELS.solid, t: (xMin + TT) / 2, p: 10 ** ((logPt + logMax) / 2) },\n  { text: PHASE_LABELS.liquid, t: (TT + TC) / 2, p: 10 ** ((logPc + logMax) / 2) },\n  { text: PHASE_LABELS.gas, t: (TT + TC) / 2, p: 10 ** ((logMin + logPt) / 2) },\n];\n\nconst TITLE = \"Water · phase-diagram-pt · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${data.length} boundary points on ${boundaries.length} curves · pressure on a log scale over ${Math.round(logMax - logMin)} decades`;\n\nfunction PhaseAnnotations() {\n  const xScale = useXScale(\"x-0\");\n  const yScale = useYScale(\"pressure\");\n  const tripleX = xScale(TRIPLE_POINT[TEMPERATURE]);\n  const tripleY = yScale(TRIPLE_POINT[PRESSURE]);\n  const criticalX = xScale(CRITICAL_POINT[TEMPERATURE]);\n  const criticalY = yScale(CRITICAL_POINT[PRESSURE]);\n  const guide = { stroke: t.inkSoft, strokeDasharray: \"8 6\", strokeWidth: 1.5 };\n  const superX = (criticalX + xScale(xMax)) / 2;\n  const superY = yScale(10 ** ((logPc + logMax) / 2));\n\n  return (\n    <g>\n      <line x1={criticalX} x2={criticalX} y1={criticalY} y2={yScale(10 ** logMax)} {...guide} />\n      <line x1={criticalX} x2={xScale(xMax)} y1={criticalY} y2={criticalY} {...guide} />\n\n      {regionAnchors.map((r) => (\n        <text key={r.text} x={xScale(r.t)} y={yScale(r.p)} fill={t.inkSoft} fontSize={24} fontWeight={600} textAnchor=\"middle\">\n          {r.text}\n        </text>\n      ))}\n      <text x={superX} y={superY} fill={t.inkSoft} fontSize={17} fontWeight={600} textAnchor=\"middle\">\n        <tspan x={superX} dy=\"0\">{PHASE_LABELS.supercritical[0]}</tspan>\n        <tspan x={superX} dy=\"1.2em\">{PHASE_LABELS.supercritical[1]}</tspan>\n      </text>\n\n      <circle cx={tripleX} cy={tripleY} r={9} fill={t.ink} stroke={t.pageBg} strokeWidth={2} />\n      <text x={tripleX + 16} y={tripleY + 24} fill={t.ink} fontSize={16} fontWeight={600}>\n        {TRIPLE_LABEL}\n      </text>\n      <text x={tripleX + 16} y={tripleY + 44} fill={t.inkSoft} fontSize={14}>\n        {TRIPLE_POINT[TEMPERATURE]} {tUnit}, {TRIPLE_POINT[PRESSURE].toLocaleString(\"en-US\")} {pUnit}\n      </text>\n\n      <rect\n        x={criticalX - 9}\n        y={criticalY - 9}\n        width={18}\n        height={18}\n        transform={`rotate(45 ${criticalX} ${criticalY})`}\n        fill={t.ink}\n        stroke={t.pageBg}\n        strokeWidth={2}\n      />\n      <text x={criticalX - 18} y={criticalY - 36} fill={t.ink} fontSize={16} fontWeight={600} textAnchor=\"end\">\n        {CRITICAL_LABEL}\n      </text>\n      <text x={criticalX - 18} y={criticalY - 16} fill={t.inkSoft} fontSize={14} textAnchor=\"end\">\n        {CRITICAL_POINT[TEMPERATURE]} {tUnit}, {CRITICAL_POINT[PRESSURE].toLocaleString(\"en-US\")} {pUnit}\n      </text>\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <div style={{ height: TITLE_H, display: \"flex\", alignItems: \"flex-end\", justifyContent: \"center\", fontSize: 22, fontWeight: 600, color: t.ink }}>\n        {TITLE}\n      </div>\n      <div style={{ height: SUBTITLE_H, display: \"flex\", alignItems: \"center\", justifyContent: \"center\", fontSize: 15, color: t.inkSoft }}>\n        {SUBTITLE}\n      </div>\n      <LineChart\n        width={width}\n        height={height - TITLE_H - SUBTITLE_H - LEGEND_H}\n        skipAnimation\n        margin={{ top: 16, right: 48, bottom: 8, left: 16 }}\n        grid={{ horizontal: true }}\n        xAxis={boundaries.map((b, i) => ({\n          id: `x-${i}`,\n          data: b.temperature,\n          scaleType: \"linear\",\n          min: xMin,\n          max: xMax,\n          position: i === 0 ? \"bottom\" : \"none\",\n          label: TEMPERATURE,\n          height: 64,\n          labelStyle: { fontSize: 16, fill: t.ink },\n          tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n        }))}\n        yAxis={[\n          {\n            id: \"pressure\",\n            scaleType: \"log\",\n            min: 10 ** logMin,\n            max: 10 ** logMax,\n            tickInterval: decades,\n            valueFormatter: (v) =>\n              `10${String(Math.round(Math.log10(v)))\n                .split(\"\")\n                .map((ch) => SUPERSCRIPT[ch])\n                .join(\"\")}`,\n            label: `${PRESSURE}, log scale`,\n            width: 84,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n          },\n        ]}\n        series={boundaries.map((b, i) => ({\n          id: `boundary-${i}`,\n          xAxisId: `x-${i}`,\n          yAxisId: \"pressure\",\n          data: b.pressure,\n          label: b.name,\n          color: t.palette[i],\n          showMark: false,\n          curve: \"linear\",\n        }))}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"bottom\", horizontal: \"center\" },\n            sx: { fontSize: 15, color: t.inkSoft },\n          },\n        }}\n        sx={{\n          \"& .MuiLineChart-line\": { strokeWidth: 3.5 },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n        }}\n      >\n        <PhaseAnnotations />\n      </LineChart>\n    </div>\n  );\n}\n"}