Worked solutions to the three exercises of Lecture 10. Each solution is computed here and checked with assert statements against an exact identity or an independent calculation. The first code cell sets up the notebook and runs Demonstration 1 of the lecture without showing its output, because some of the solutions use its variables.
try: # use the installed package, or the copy in this repository
import engmaterials
except ImportError:
import pathlib, subprocess, sys
for _root in [pathlib.Path.cwd(), *pathlib.Path.cwd().parents]:
if (_root / "engmaterials" / "__init__.py").exists():
sys.path.insert(0, str(_root))
break
else: # on Google Colab: install from GitHub
subprocess.run([sys.executable, "-m", "pip", "install", "-q",
"git+https://github.com/ktwyw/engmaterials.git"], check=True)
import engmaterials
import inspect
import numpy as np
import matplotlib.pyplot as plt
from engmaterials import (atomistics, constants, diffraction, elasticity, kinetics_selection,
lattices, phase_field, phase_thermodynamics)
from engmaterials.plotting import label_panels, panel_label, use_style
use_style()
def show_source(obj):
"""Return the source code of a library function or class."""
return inspect.getsource(obj)
def heading(text):
print(f"\n{text}\n" + "=" * len(text))
# Demonstration 1 of the lecture, run without output: the solutions use its variables.
import contextlib, io
_show, plt.show = plt.show, lambda *args, **kwargs: None
_stdout = contextlib.redirect_stdout(io.StringIO()); _stdout.__enter__()
heading("Demonstration 1: Exact Regular-Solution Binodal vs Spinodal & Chemical-Potential Equality")
omega_b = 18000.0
Tc_b = phase_thermodynamics.regular_solution_critical_temp(omega_b)
print(f"Omega = {omega_b:.0f} J/mol | Tc = {Tc_b:.2f} K")
print(f"{'T [K]':>7s} | {'x_b1':>9s} | {'x_s1':>9s} | {'x_s2':>9s} | {'x_b2':>9s} | {'|dmu_A1-dmu_A2|':>15s} | {'Metastable Width':>16s}")
print("-" * 94)
for tk in [550.0, 700.0, 850.0, 1000.0]:
xb1, xb2 = phase_thermodynamics.regular_solution_binodal(tk, omega_b)
xs1, xs2 = phase_thermodynamics.regular_solution_spinodal(tk, omega_b)
r1 = phase_thermodynamics.regular_solution_free_energy(xb1, tk, omega_b)
r2 = phase_thermodynamics.regular_solution_free_energy(xb2, tk, omega_b)
dmu_err = max(abs(r1["dmu_A"] - r2["dmu_A"]), abs(r1["dmu_B"] - r2["dmu_B"]))
print(f"{tk:7.1f} | {xb1:9.5f} | {xs1:9.5f} | {xs2:9.5f} | {xb2:9.5f} | {dmu_err:15.2e} | {xs1 - xb1:16.5f}")
assert 0.0 < xb1 < xs1 < 0.5 < xs2 < xb2 < 1.0
assert dmu_err < 1e-8
_stdout.__exit__(None, None, None); plt.show = _show; plt.close("all")Exercise 1¶
Problem. Verify on an asymmetric Redlich–Kister curve () that common_tangent_binodal yields mu_A_eq and mu_B_eq matching analytical dmu_A, dmu_B at both tie-line endpoints, and verify Seltz’s partition identities and across Cu–Ni to 10-14.
heading("Exercise 1: Common-Tangent Chemical-Potential Intercepts & Seltz Partition Identities")
xg = np.linspace(0.0005, 0.9995, 25000)
rk_curve = phase_thermodynamics.redlich_kister_free_energy(xg, 800.0, L0=20000.0, L1=-4500.0)
hull = phase_thermodynamics.common_tangent_binodal(xg, rk_curve["dG_mix"])
tl = hull["tie_lines"][0]
ep_l = phase_thermodynamics.redlich_kister_free_energy(tl["x_left"], 800.0, L0=20000.0, L1=-4500.0)
ep_r = phase_thermodynamics.redlich_kister_free_energy(tl["x_right"], 800.0, L0=20000.0, L1=-4500.0)
print(f"Skewed tie-line: x_left = {tl['x_left']:.5f}, x_right = {tl['x_right']:.5f}, slope = {tl['tangent_slope']:+.2f} J/mol")
print(f"mu_A_eq = {tl['mu_A_eq']:+.2f} vs dmu_A(left) = {ep_l['dmu_A']:+.2f}, dmu_A(right) = {ep_r['dmu_A']:+.2f}")
assert abs(tl["mu_A_eq"] - ep_l["dmu_A"]) < 1.5 and abs(ep_l["dmu_A"] - ep_r["dmu_A"]) < 1.5
assert abs(tl["mu_B_eq"] - ep_r["dmu_B"]) < 1.5 and abs(ep_l["dmu_B"] - ep_r["dmu_B"]) < 1.5
T_vec = np.linspace(1380.0, 1710.0, 50)
iso = phase_thermodynamics.isomorphous_phase_boundaries(T_vec, 1357.77, 13260.0, 1728.0, 17480.0)
ca = np.exp((13260.0 / constants.R) * (1.0 / T_vec - 1.0 / 1357.77))
cb = np.exp((17480.0 / constants.R) * (1.0 / T_vec - 1.0 / 1728.0))
assert np.allclose((1.0 - iso["x_solidus"]) / (1.0 - iso["x_liquidus"]), ca, rtol=1e-14, atol=1e-14)
assert np.allclose(iso["x_solidus"] / iso["x_liquidus"], cb, rtol=1e-14, atol=1e-14)
print("Seltz A and B chemical-potential partition ratios verified across 50 temperatures to 1e-14")
Exercise 1: Common-Tangent Chemical-Potential Intercepts & Seltz Partition Identities
=====================================================================================
Skewed tie-line: x_left = 0.12694, x_right = 0.96989, slope = +587.78 J/mol
mu_A_eq = -761.30 vs dmu_A(left) = -761.39, dmu_A(right) = -762.54
Seltz A and B chemical-potential partition ratios verified across 50 temperatures to 1e-14
Explanation. A line tangent to at both and satisfies and , demonstrating geometrically why equal chemical potentials require a shared non-horizontal tangent whenever is asymmetric.
Exercise 2¶
Problem. Demonstrate the local-minima fallacy on : find the two local minima where , compare them with the true common_tangent_binodal tie-line, and show that the common tangent yields a strictly lower two-phase Gibbs energy at .
heading("Exercise 2: Diagnostic Audit — Local-Minima Fallacy (dG/dx = 0) vs Global Common Tangent")
xg = np.linspace(0.001, 0.999, 10000)
rk = phase_thermodynamics.redlich_kister_free_energy(xg, 700.0, L0=22000.0, L1=-3000.0)
G_tot = 2000.0 * xg + rk["dG_mix"]
dG_dx = 2000.0 + rk["dG_dx"]
# Find both distinct local minima where dG_dx crosses zero from negative to positive:
min_indices = [i for i in range(xg.size - 1) if dG_dx[i] <= 0.0 and dG_dx[i + 1] > 0.0]
assert len(min_indices) == 2
x_min1, g_min1 = float(xg[min_indices[0]]), float(G_tot[min_indices[0]])
x_min2, g_min2 = float(xg[min_indices[1]]), float(G_tot[min_indices[1]])
tl_true = phase_thermodynamics.common_tangent_binodal(xg, G_tot)["tie_lines"][0]
x0 = 0.50
# Linear lever-rule mixture energy at x0 = 0.50 for false local-minima split vs true common-tangent split:
g_mix_false = g_min1 + (g_min2 - g_min1) * (x0 - x_min1) / (x_min2 - x_min1)
g_mix_true = tl_true["g_left"] + (tl_true["g_right"] - tl_true["g_left"]) * (x0 - tl_true["x_left"]) / (tl_true["x_right"] - tl_true["x_left"])
comp_err = max(abs(x_min1 - tl_true["x_left"]), abs(x_min2 - tl_true["x_right"]))
print(f"False local minima (dG/dx=0): x1 = {x_min1:.4f}, x2 = {x_min2:.4f} | G_mix(0.50) = {g_mix_false:+.2f} J/mol")
print(f"True common tangent : x1 = {tl_true['x_left']:.4f}, x2 = {tl_true['x_right']:.4f} | G_mix(0.50) = {g_mix_true:+.2f} J/mol")
print(f"Gibbs energy penalty of local-minima fallacy = {g_mix_false - g_mix_true:+.2f} J/mol | Max solvus error = {comp_err:.4f}")
assert g_mix_true < g_mix_false - 10.0
assert comp_err > 0.015
Exercise 2: Diagnostic Audit — Local-Minima Fallacy (dG/dx = 0) vs Global Common Tangent
========================================================================================
False local minima (dG/dx=0): x1 = 0.0301, x2 = 0.9757 | G_mix(0.50) = +848.23 J/mol
True common tangent : x1 = 0.0464, x2 = 0.9846 | G_mix(0.50) = +834.85 J/mol
Gibbs energy penalty of local-minima fallacy = +13.38 J/mol | Max solvus error = 0.0163
Explanation. Adding a linear reference-state offset shifts without changing physical mixing thermodynamics at all, yet it shifts the zeros of and leaves an unrelaxed Gibbs free-energy penalty. By contrast, the common-tangent compositions are 100% invariant to adding any linear function to .
Exercise 3¶
Problem. Implement hull_isomorphous_tie_line(T_K, Tm_A, dHm_A, Tm_B, dHm_B, n_grid=4000) by constructing and and calling common_tangent_binodal(x_grid, G_L, G_S). Verify agreement with isomorphous_phase_boundaries across 6 Cu–Ni temperatures to < 1e-3.
heading("Exercise 3: Implement It Yourself — Unifying Convex-Hull CALPHAD with Analytical Seltz Isomorphous Lens")
def hull_isomorphous_tie_line(T_K: float, Tm_A: float, dHm_A: float, Tm_B: float, dHm_B: float, n_grid: int = 4000) -> dict:
"""Solve binary ideal isomorphous liquidus/solidus via lower convex hull of G^L(x, T) and G^S(x, T)."""
xg = np.linspace(1e-5, 1.0 - 1e-5, n_grid)
g_liq = constants.R * T_K * (xg * np.log(xg) + (1.0 - xg) * np.log(1.0 - xg))
dg_mA = -dHm_A * (1.0 - T_K / Tm_A)
dg_mB = -dHm_B * (1.0 - T_K / Tm_B)
g_sol = (1.0 - xg) * dg_mA + xg * dg_mB + g_liq
tl = phase_thermodynamics.common_tangent_binodal(xg, g_liq, g_sol)["tie_lines"][0]
x_liq = tl["x_left"] if tl["phase_left"] == 0 else tl["x_right"]
x_sol = tl["x_right"] if tl["phase_right"] == 1 else tl["x_left"]
return {"x_liquidus": float(x_liq), "x_solidus": float(x_sol)}
print(f"{'T [K]':>8s} | {'xL (Hull)':>10s} | {'xL (Seltz)':>10s} | {'xS (Hull)':>10s} | {'xS (Seltz)':>10s} | {'Max |Err|':>10s}")
print("-" * 72)
for T_val in [1400.0, 1460.0, 1520.0, 1580.0, 1640.0, 1690.0]:
h_res = hull_isomorphous_tie_line(T_val, 1357.77, 13260.0, 1728.0, 17480.0)
s_res = phase_thermodynamics.isomorphous_phase_boundaries(T_val, 1357.77, 13260.0, 1728.0, 17480.0)
err = max(abs(h_res["x_liquidus"] - s_res["x_liquidus"]), abs(h_res["x_solidus"] - s_res["x_solidus"]))
print(f"{T_val:8.1f} | {h_res['x_liquidus']:10.5f} | {s_res['x_liquidus']:10.5f} | {h_res['x_solidus']:10.5f} | {s_res['x_solidus']:10.5f} | {err:10.2e}")
assert err < 6e-4
Exercise 3: Implement It Yourself — Unifying Convex-Hull CALPHAD with Analytical Seltz Isomorphous Lens
=======================================================================================================
T [K] | xL (Hull) | xL (Seltz) | xS (Hull) | xS (Seltz) | Max |Err|
------------------------------------------------------------------------
1400.0 | 0.09553 | 0.09547 | 0.12704 | 0.12695 | 8.50e-05
1460.0 | 0.23982 | 0.23985 | 0.29983 | 0.29986 | 3.31e-05
1520.0 | 0.39410 | 0.39409 | 0.46537 | 0.46548 | 1.18e-04
1580.0 | 0.55789 | 0.55782 | 0.62515 | 0.62516 | 6.65e-05
1640.0 | 0.73068 | 0.73067 | 0.77994 | 0.77996 | 2.09e-05
1690.0 | 0.88146 | 0.88144 | 0.90597 | 0.90588 | 8.48e-05
Summary. Constructing explicit and curves and taking their lower convex hull reproduces Seltz’s analytical Cu–Ni liquidus and solidus within half a grid step (), showing how modern CALPHAD software generalizes seamlessly to arbitrary Redlich–Kister non-ideality and multi-phase competition.