Skip to content

Melting gallium by conduction: the Stefan problem

FEM gallium_melting_stefan.py

A slab of solid gallium sits just below its melting point of 29.6 °C, and one wall is held 9.7 K hotter than the other. A melt front starts at the hot wall and moves in, slowed by the latent heat it has to supply. The front is a moving boundary that the mesh does not follow: the unknown is the enthalpy, which holds both the sensible and the latent heat.

The melting of gallium by conduction, in the units of the model: above, the computed temperature of a thin strip heated from the left wall, with the melting point as a white line; below, the computed temperature along the strip (orange markers) on the exact solution (dashed), with the melting range shaded. The melt front moves in as the square root of time, and the temperature keeps its place on the exact curve.

Above: the computed temperature of the strip, with the melting point as a white line. Below: the temperature along the strip against the exact solution of the same model.

The problem

The two-phase Stefan problem as one fixed-mesh equation for the enthalpy \(h\), with the liquid fraction \(f\) rising across a melting range from \(T_s\) to \(T_\ell\):

\[\partial_t h=\nabla^2T,\qquad h=T+\frac{f(T)}{\mathrm{Ste}},\qquad f=\operatorname{clip}\!\Big(\frac{T-T_s}{T_\ell-T_s},0,1\Big),\]

nondimensional, with the Stefan number \(\mathrm{Ste}=c\,\Delta T/L=0.0462\) and the melting point 0.1526 of the way from the cold to the hot wall (gallium's values, from the benchmark of Hannoun, Alexiades & Mai, 2003). For a half-space the model has a similarity solution in \(\eta=x/(2\sqrt t)\): liquid for \(\eta<\eta_\ell\), the melting range, where the latent heat acts as an extra heat capacity, and solid beyond, with the edges fixed by a continuous gradient at both, \(\eta_\ell=0.1076\) and \(\eta_s=0.3349\).

The jNO code

The law \(f(h)\) is written once, and the enthalpy is one P1 unknown on a long thin strip:

liquid = lambda e: jno.np.minimum(jno.np.maximum((e - TH_S) / DH, 0.0), 1.0)  # f(h)
h = d.unknown.scalar(name="h")  # the enthalpy, P1

\(\nabla T=(\mathrm{d}T/\mathrm{d}h)\,\nabla h\), with the slope 1 outside the melting range and small inside it, frozen within each Newton step by jno.lag. The coefficient jumps inside a cell, so the quadrature is high:

dT = 1 - jno.np.where((hq > TH_S) * (hq < TH_S + DH), 1.0, 0.0) / (STE * DH)  # dT/dh: 1, or small inside the range

fem = jno.fem(
    [
        h.bind(x=x, y=y, t=t).t * s + jno.lag(dT) * dot(grad(hq), grad(s)),  # h_t = div grad T, with grad T = dT/dh grad h
        h(*left) - (1 + 1 / STE),  # the hot wall: T = 1 and liquid
        h(*right) - 0.0,  # the cold wall; top and bottom are insulated
        h(*ci) - 0.0,  # solid gallium at the cold temperature
    ],
    quad_degree=8,  # the law has a kink inside a cell: integrate it finely
)
traj = np.asarray(fem.solve(nonlinear=jno.solve.newton(direct=True), linear=jno.solve.lu(backend="host")).eval())

Result

478 unknowns, 120 steps to \(t=0.3\). The liquid length, the integral of \(f\), follows \(\sqrt t\):

check result
liquid length at \(t=0.3\) 0.1607 (exact 0.1603)
worst gap over \(0.1\le t\le0.3\) 1.6 %
temperature at the end against the exact profile within 0.0079 of the wall difference

Full script

The whole script, as it runs in the tests
"""Melting of gallium by conduction: a slab heated from one side, the two-phase Stefan problem in enthalpy form."""

import jax
import numpy as np
from scipy.integrate import quad
from scipy.optimize import fsolve
from scipy.special import erf, erfc

import jno

jax.config.update("jax_enable_x64", True)
run = jno.setup(__file__)
# gallium (Hannoun, Alexiades and Mai 2003, Table 1): heat capacity, latent heat, melting point; the walls' temperatures
CP, L_FUS, T_F, T_HOT, T_COLD = 381.5, 8.016e4, 302.78, 311.0, 301.3
# lengths in a unit l, time in l^2 / alpha, temperature (T - T_cold) / (T_hot - T_cold): then only two numbers remain
STE, TH_F = CP * (T_HOT - T_COLD) / L_FUS, (T_F - T_COLD) / (T_HOT - T_COLD)  # Stefan number, melting point
# enthalpy h = T + f / Ste (sensible plus latent heat): the liquid fraction f rises linearly with T across a melting range D_TH
D_TH = 0.3
TH_S, DH = TH_F - D_TH / 2, D_TH + 1 / STE  # solidus; the width of the melting range in h
L, HEIGHT, T_END, STEPS = 2.5, 0.04, 0.3, 120  # a long thin strip, so that the problem is one-dimensional
liquid = lambda e: jno.np.minimum(jno.np.maximum((e - TH_S) / DH, 0.0), 1.0)  # f(h)

mesh_size = lambda x, y, z: min(0.1, 0.01 + 0.12 * max(0.0, x - 0.6))  # fine where the front travels
d = jno.shape.rect(0, 0, L, HEIGHT, size=mesh_size).domain(time=(0.0, T_END, STEPS + 1))
x, y, t = d.variable("interior", split=True)
left, right, ci = (d.variable(s, split=True) for s in ("left", "right", "initial"))
h = d.unknown.scalar(name="h")  # the enthalpy, P1
s = h.test()

grad = lambda w: jno.np.jacobian(w, [x, y])
dot = jno.np.inner
hq = h.bind(x=x, y=y)
dT = 1 - jno.np.where((hq > TH_S) * (hq < TH_S + DH), 1.0, 0.0) / (STE * DH)  # dT/dh: 1, or small inside the range

fem = jno.fem(
    [
        h.bind(x=x, y=y, t=t).t * s + jno.lag(dT) * dot(grad(hq), grad(s)),  # h_t = div grad T, with grad T = dT/dh grad h
        h(*left) - (1 + 1 / STE),  # the hot wall: T = 1 and liquid
        h(*right) - 0.0,  # the cold wall; top and bottom are insulated
        h(*ci) - 0.0,  # solid gallium at the cold temperature
    ],
    quad_degree=8,  # the law has a kink inside a cell: integrate it finely
)
traj = np.asarray(fem.solve(nonlinear=jno.solve.newton(direct=True), linear=jno.solve.lu(backend="host")).eval())
# the liquid area of the strip: the rows of f * s sum to its integral
area = lambda state: np.asarray(fem.eval(liquid(hq) * s, state)).sum()

# the reference: the exact solution of the same model, a similarity solution in eta = x / (2 sqrt(t)), liquid | melting range | solid
T_LIQ, T_SOL = TH_F + D_TH / 2, TH_F - D_TH / 2
A_M = 1 / (1 + 1 / (STE * D_TH))  # diffusivity in the range: the latent heat acts as extra heat capacity
slope = lambda a, e: 2 / np.sqrt(np.pi) * np.exp(-(e**2) / a) / np.sqrt(a)  # d erf(e / sqrt(a)) / de
amp = lambda e: (T_LIQ - T_SOL) / (erf(e[0] / np.sqrt(A_M)) - erf(e[1] / np.sqrt(A_M)))  # T = T_LIQ + amp (erf - erf)
# the gradient of T is continuous at the two edges of the range
edges = lambda e: [
    -(1 - T_LIQ) * slope(1, e[0]) / erf(e[0]) - amp(e) * slope(A_M, e[0]),
    amp(e) * slope(A_M, e[1]) + T_SOL * slope(1, e[1]) / erfc(e[1]),
]
e_l, e_s = fsolve(edges, [0.1, 0.3])
mush = lambda e: T_LIQ + amp((e_l, e_s)) * (erf(e / np.sqrt(A_M)) - erf(e_l / np.sqrt(A_M)))
exact_T = lambda eta: np.where(
    eta < e_l, 1 - (1 - T_LIQ) * erf(eta) / erf(e_l), np.where(eta < e_s, mush(eta), T_SOL * erfc(eta) / erfc(e_s))
)
ts = np.linspace(0.0, T_END, STEPS + 1)
front = np.array([area(c) for c in traj]) / HEIGHT  # the liquid length
front_exact = 2 * np.sqrt(ts) * (e_l + quad(lambda e: (mush(e) - T_SOL) / (T_LIQ - T_SOL), e_l, e_s)[0])
late = ts >= 0.1
front_err = np.abs(front / np.where(late, front_exact, 1) - 1)[late].max()
ib = fem.block_index(h)
pts = np.asarray(fem.field_points[ib])[:, :2]
temperature = lambda e: e - np.clip((e - TH_S) / DH, 0.0, 1.0) / STE  # the same law on the node values
T_end = temperature(traj[-1][fem.blocks[ib]])
T_err = np.abs(T_end - exact_T(pts[:, 0] / (2 * np.sqrt(T_END)))).max()
print(f"{fem.dofs} DOFs, {STEPS} steps to t={T_END}; the melting range is x/(2 sqrt t) = {e_l:.4f} .. {e_s:.4f}")
print(
    f"liquid length at t={T_END}: {front[-1]:.4f} vs exact {front_exact[-1]:.4f}; worst gap for t >= 0.1: {front_err:.1%}; temperature gap {T_err:.4f}"
)
assert front_err < 0.03 and T_err < 0.03  # the front within 3 % and the temperature within 0.03 of the hot-cold difference