In [1]:
using Plots, QuadGK, Roots

Van der Waals EoS¶

useful resources:

  • python from Christian Hill

  • Heat and Thermodynamics (ch.11) (Zemansky and Dittman)

We study the pressure of a non-ideal gas as a function of volume per particle, $v=V/N$, and temperature $T$: $$ P(v,T)=\frac{8T}{3v-1}-\frac{3}{v^2}. $$ All quantities are in reduced units, with critical temperature $T_c=1$.

In [2]:
let

	function Pf(vv, T)
		return 8/3 * T /(vv-1/3) - 3/vv^2
	end

	v_arr = range(1/3+1E-3, 5.0, length=250)

    fig1 = plot(ylim=(-0.2, 2), xlabel="v", ylabel="P isotherms", palette = :rainbow)

    tv = [0.88, 0.90, 0.95, 1.0, 1.05, 1.2, 1.3]

    for tt in tv
        plot!(fig1, v_arr, vv -> Pf(vv, tt), label="T=$tt")
    end

    fig1

end
Out[2]:
No description has been provided for this image

Maxwell's construction: the prescription¶

Below $T_c$, the pressure isotherm bends down, then up, then down again. For a range of pressures, the equation of state gives three possible volumes. The middle branch, between $v_A$ and $v_B$, has $$ \left(\frac{\partial P}{\partial v}\right)_T>0. $$ Here, compression lowers the pressure, whereas a stable gas resists compression by increasing its pressure. This branch is mechanically unstable.

At a given temperature, liquid and gas coexist at a pressure $P_{\rm av}$, with volumes per particle $v_1$ and $v_3$. During compression at fixed temperature, gas converts into liquid while the pressure stays constant.

Maxwell's construction finds this pressure by replacing the curved section between $v_1$ and $v_3$ with a horizontal line. Its height is chosen so that the areas above and below the line are equal: $$ \int_{v_1}^{v_3}\bigl[P(v,T)-P_{\rm av}\bigr]\,dv=0. $$ The endpoints must also lie on the original isotherm: $$ P(v_1,T)=P(v_3,T)=P_{\rm av}. $$ The equation $P(v,T)=P_{\rm av}$ has three roots, $v_1<v_2<v_3$. We choose the smallest and largest as the liquid and gas volumes; the middle root lies on the unstable branch.

Together, these conditions determine $P_{\rm av}$, $v_1$, and $v_3$. The endpoint conditions ensure equal pressure, while the equal-area condition ensures equal chemical potential. Thus, neither phase is favored over the other.

The coexistence interval is wider than the unstable interval: $$ v_1<v_A<v_B<v_3. $$ The sections between $v_1$ and $v_A$, and between $v_B$ and $v_3$, describe metastable homogeneous states.

In [3]:
let
	# for a given T, do the Maxwell's construction:

    Tnow = 0.78
	
	function pfunc(vv)
		return 8/3 * Tnow /(vv-1/3) - 3/vv^2
	end

	# dp/dv is also useful
	dpfunc = vv -> imag(pfunc(vv+im*0.01))/0.01

	# range of p for maxwell's construction
    # unstable region: dP/dv > 0
	vA, vB = find_zeros(dpfunc, 1/3+1E-3, 5)
	pmin, pmax = pfunc(vA), pfunc(vB)
	
	function vroot(pval)
		# given pval, work out the three intercept
		_f(vv) = pval * (vv-1/3) * vv^2 - (8/3 * Tnow * vv^2 - 3 * (vv-1/3))
		return find_zeros(_f, 1/3+1E-3, 15)
	end
		
	function test_func(pav)
		
		# given pav, work out v1, v2, v3
		vols = vroot(pav)
        v1 = vols[1]
        v3 = vols[end]

		# work out the integral
        return quadgk( v -> pfunc(v)-pav, v1, v3)[1]
        
	end

    # compute the key obs
	pav = find_zeros(test_func, pmin, pmax)[end]
    vols = vroot(pav)
    v1, v3 = vols[1], vols[end]
   
	v_arr = range(1/3 + 1E-3, 6.0, length=80)

	p1 = plot(v_arr, pfunc, label="T=$Tnow", xlabel="V", ylabel="P isotherms", ylim=(-1.3,2))
    # vA,bB; v1,v2,v3
	scatter!([vA, vB], pfunc.([vA, vB]), label="")
	plot!(vols, pfunc.(vols), label="", color="gray", line=(dash=:dash))
	scatter!(vols, pfunc.(vols), label="")

	p2 = plot(range(pmin, pmax, step=0.01), p->test_func(p), label="")
	scatter!([pav], [test_func(pav)], xlabel="pav", ylabel="int dV (p-pav)", label="")

	plot(p1, p2, layout=(2,1), size=(500, 600))

end
Out[3]:
No description has been provided for this image

Signatures of a first-order phase transition¶

  • Metastable states: a phase can resist small disturbances even when another phase has lower free energy. Examples include superheated water and supercooled liquids. Even diamond is metastable at ordinary conditions!

  • Coexistence of phases: two phases can coexist at the same temperature and pressure, as liquid water and steam do during boiling.

  • A phase boundary: a coexistence line separates the phases in the pressure–temperature diagram.

Does crossing the line mean the whole system changes phase instantly? No. When heating water at fixed pressure, its temperature rises until boiling begins. Further heat converts liquid into steam while the temperature stays constant. Once all the liquid has vaporized, the temperature can rise again.

The coexistence curve $P_{\rm av}(T)$ tells us where the transition occurs, but does not by itself show the differences between the phases:

  • The volume per particle has a jump $\Delta v=v_3-v_1$.
  • The entropy per particle has a jump $\Delta s=s_3-s_1$.
  • The latent heat per particle is $L=T\Delta s$.

These jumps compare the two phases at the same coexistence temperature and pressure. They do not describe how quickly the transition happens.

For the van der Waals gas, the homogeneous isotherm contains an unstable region: $$ \left(\frac{\partial P}{\partial v}\right)_T>0. $$ Here, compression lowers the pressure and amplifies density disturbances. The equilibrium description replaces the curved section between $v_1$ and $v_3$ with a horizontal coexistence segment. Along this segment, liquid and gas have the same pressure, while their proportions can vary. Maxwell's construction determines this pressure.

Note: Reaching equilibrium involves mechanisms beyond Maxwell's construction, including nucleation, interface motion, and the growth of unstable density fluctuations. Instability can trigger conversion, but nucleation can also start it while the initial phase remains metastable. Surface tension creates a barrier to forming droplets or bubbles; describing these processes requires information beyond the bulk equation $P(v,T)$.

In [4]:
function coexist(Tnow)	
	
	function pfunc(vv)
		return 8/3 * Tnow /(vv-1/3) - 3/vv^2
	end

	# dp/dv is also useful
	dpfunc = vv -> imag(pfunc(vv+im*0.01))/0.01

	# range of p for maxwell's construction
    # unstable region: dP/dv > 0
	vA, vB = find_zeros(dpfunc, 1/3+1E-3, 5)
	pmin, pmax = pfunc(vA), pfunc(vB)
	
	function vroot(pval)
		# given pval, work out the three intercept
        
        # formulate in terms of u = 1/v to deal with large v
        _fu = u -> 8Tnow*u - 3u^2*(3 - u) - pval*(3 - u)
        
		uvec = find_zeros(_fu, 1E-8, 3.0-1E-5)
        return sort(1 ./ uvec)
	end
		
	function test_func(pav)
		
		# given pav, work out v1, v2, v3
		vols = vroot(pav)

        # guard
        if length(vols) < 2 return NaN end
        
        v1 = vols[1]
        v3 = vols[end]

		# work out the integral
        return quadgk( v -> pfunc(v)-pav, v1, v3)[1]
        
	end

    # compute the key obs

    pav = find_zeros(test_func, pmin, pmax)[end]

    vols = vroot(pav)
    if length(vols) != 3 return (NaN, NaN, NaN) end

	return pav, vols[1], vols[end]
	
end
Out[4]:
coexist (generic function with 1 method)
In [5]:
let

    N = 28
	Tv = range(0.75, 1.0-1E-5, length=N)
    dT = Tv[2]-Tv[1]

    data = zeros(N, 4)

	for (i1, tt) in enumerate(Tv)
		pav, v1, v3 = coexist(tt)
		data[i1, 1:4] = [tt, pav, v1, v3]
	end

    pav_arr = data[:, 2]
    v1_arr = data[:, 3]
    v3_arr = data[:, 4]

    dV_arr = v3_arr .- v1_arr

    # dP/dT 2nd order finite difference
    dpav_arr = similar(pav_arr)
    dpav_arr[1]   = (-3pav_arr[1] + 4pav_arr[2] - pav_arr[3]) / (2dT)
    dpav_arr[end] = (3pav_arr[end] - 4pav_arr[end-1] + pav_arr[end-2]) / (2dT)
    for i1 in 2:N-1
        dpav_arr[i1] = (pav_arr[i1+1]-pav_arr[i1-1])/(2dT)
    end

	dQ = @. dpav_arr * Tv * dV_arr

    fig = plot(xlabel="T", title="coexistence curves", alpha=0.5)
	scatter!(fig, data[:, 1], pav_arr, label="Pav")
	plot!(fig, data[:, 1], v1_arr, label="v1")
    plot!(fig, data[:, 1], v3_arr, label="v3")
	scatter!(fig, data[:, 1], dQ, label="latent heat")
    scatter!(fig, [1.0], [1.0], marker=:xcross, color="black", markersize=10.0, label="CEP")
		
end
Out[5]:
No description has been provided for this image

graphs of v(T) at fixed P and v(p) at fixed T¶

In [6]:
let
    
	# constant pressure: isobars
	pnow = 0.68
	vv0 = 0.386521

	# entropy function (for fixed T)
	S(vv) = 8/3 * log(vv-1/3) - 8/3 * log(vv0-1/3)

	# use a fine grid to extract physics at Td
	T_arr = range(0.8, 1.11, step=0.005)

	function vroots(tt, pp)
        # v = 1/u
		_fu = u -> 8*tt*u - 3*u^2*(3 - u) - pp*(3 - u)
        uvec = find_zeros(_fu, 1E-8, 3.0-1E-5)
        return sort(1 ./ uvec)
    end

	# collect v1 at various T
    V1_arr = similar(T_arr)
    V3_arr = similar(T_arr)
    for (i1, tt) in enumerate(T_arr)
        roots = vroots(tt, pnow)
        V1_arr[i1] = isempty(roots) ? NaN : roots[1]
        V3_arr[i1] = isempty(roots) ? NaN : roots[end]
    end

	# Given Pav = pnow, find the corresponding T=Tco
    function T_finder(pp)
        
    	function _f(tt)
    		pav, v1, v3 = coexist(tt)
    		return pav - pp
    	end
        
    	return find_zero(_f, (0.7, 0.999), Bisection())
    end

    Tco = T_finder(pnow)
	pav, v1, v3 = coexist(Tco)

    p1 = plot(xlabel="T", ylabel="[v, Ts] vs T at p=$pnow", legend=:right)
	plot!(p1, T_arr, V1_arr, label="v1(T)")
    plot!(p1, T_arr, V3_arr, label="v3(T)")
	plot!(p1, T_arr, T_arr .* S.(V1_arr), label="T*s1(T)")
    plot!(p1, T_arr, T_arr .* S.(V3_arr), label="T*s3(T)")

	scatter!(p1, [Tco, Tco], [v1, v3], label="coexist V")
	scatter!(p1, [Tco, Tco], [Tco*S(v1), Tco*S(v3)], label="coexist Ts")

    # 2nd graph
	# reference: vol at constant T
	Tnow = 0.9106
    pav, v1, v3 = coexist(Tnow)

	v_arr = range(1/3+0.18, 7.5, length=80)
    pfunc = vv -> 8/3 * Tnow /(vv-1/3) - 3/vv^2

	p1_arr = range(0.18, 1.24, step = 0.05)
    vj_arr = fill(NaN, length(p1_arr), 3)

    for (i,pp) in enumerate(p1_arr)
        roots = vroots(Tnow, pp)
        # if roots is empty, it will not run
        for (j, vv) in enumerate(roots)
            vj_arr[i,j] = vv
        end
    end
    
    p2 = plot(xlabel="P", ylabel="v(p) at T=$Tnow", ylim=(0, 7.5), xlim=(0,1.3), legend=:topright)
	plot!(p2, pfunc.(v_arr), v_arr, label="")
    for j in 1:3
        scatter!(p2, p1_arr, vj_arr[:,j], label="root $j", alpha=0.5)
    end
    scatter!(p2, [pav, pav], [v1, v3], marker=:xcross, color="black", alpha=1.0, label="coexist")

	plot(p1, p2, layout=(2,1), size=(500, 600))
	
end
Out[6]:
No description has been provided for this image

Check it out¶

  • At coexistence, liquid and gas have different values of $v$ and $Ts$. These jumps are signatures of a first-order transition. The curve $P_{\rm av}(T)$ shows where coexistence occurs, while the two phases share the same pressure there.

  • On heating at fixed pressure, the smallest-root curve can follow the metastable liquid branch above $T_{\rm co}$. Its jump occurs when the liquid root disappears, at a higher temperature than coexistence. The jumps read from this curve therefore need not match the equilibrium jumps at $T_{\rm co}$.

  • The difference between the two coexistence values of $Ts$ gives the latent heat per particle, $L=T_{\rm co}(s_3-s_1)$. This agrees with the latent heat obtained earlier from the Clapeyron equation.

Theory: Gibbs free energy and phase coexistence¶

Gibbs free energy is useful for a system at fixed temperature $T$, pressure $P$, and total particle number $N$: $$ dG=V\,dP-S\,dT+\mu\,dN. $$

We consider a pure substance that can separate into two macroscopic phases, $a$ and $b$. Particles can move freely between them. We neglect the contribution of the interface to the free-energy change during particle transfer and use the approximation $$ G_{\rm total}\simeq G_a(T,P,N_a)+G_b(T,P,N_b), \qquad N_a+N_b=N. $$

At fixed $T,P,N$, equilibrium minimizes $G_{\rm total}$. Transferring particles from phase $a$ to phase $b$ gives $$ dN_b=-dN_a, $$ so, within this approximation, $$ dG_{\rm total} =\mu_a\,dN_a+\mu_b\,dN_b =(\mu_b-\mu_a)\,dN_b. $$ If the chemical potentials differ, transfer toward the phase with lower chemical potential reduces $G_{\rm total}$. When both phases are present, particles can move in either direction. At equilibrium, the first-order change under this transfer must vanish: $$ dG_{\rm total}=0 \quad\Longrightarrow\quad \mu_a=\mu_b. $$

The coexistence curve and latent heat¶

For each phase $i=a,b$, the chemical potential equals the Gibbs free energy per particle: $$ \mu_i=\frac{G_i}{N_i}, \qquad d\mu_i=v_i\,dP-s_i\,dT. $$

The coexistence pressure $P_{\rm av}(T)$ is the pressure found by Maxwell's construction at each temperature. Along this curve, $\mu_a=\mu_b$ remains true as $T$ and $P$ vary, so $d\mu_a=d\mu_b$. Therefore, $$ (v_b-v_a)\,dP_{\rm av}=(s_b-s_a)\,dT. $$ This gives the Clapeyron equation: $$ \frac{dP_{\rm av}}{dT} =\frac{s_b-s_a}{v_b-v_a}. $$

For the liquid–gas transition, we use the Maxwell root labels $1$ for liquid and $3$ for gas. The latent heat per particle for vaporization is $$ L=T(s_3-s_1) =T\frac{dP_{\rm av}}{dT}(v_3-v_1). $$

Maxwell's constructions¶

At fixed temperature, equal chemical potentials and equal pressures give the equal-area condition: $$ \int_{v_1}^{v_3}[P(v,T)-P_{\rm av}]\,dv=0, $$ with $$ P(v_1,T)=P(v_3,T)=P_{\rm av}. $$

At fixed pressure, the corresponding condition in the $T$–$s$ plane is $$ \int_{s_1}^{s_3}[T(s,P)-T_{\rm av}]\,ds=0, $$ where $T_{\rm av}$ is the coexistence temperature at that pressure.

These constructions use the homogeneous-state curves to find the equilibrium coexistence states.

A Maxwell relation for entropy¶

Helmholtz free energy satisfies $$ dF=-P\,dV-S\,dT+\mu\,dN. $$ Equality of mixed derivatives gives $$ \left(\frac{\partial P}{\partial T}\right)_{V,N} = \left(\frac{\partial S}{\partial V}\right)_{T,N}. $$ Therefore, $$ S(T,V,N) = S(T,V_0,N) + \int_{V_0}^{V} \left(\frac{\partial P(T,V',N)}{\partial T}\right)_{V',N} \,dV'. $$ The pressure equation determines the volume dependence of entropy. The reference term $S(T,V_0,N)$ can vary with temperature and requires additional thermal information. It cancels when we compare the entropies of the two phases at the same temperature.

Temperature VS Entropy graph¶

No description has been provided for this image * see [wiki water phase diagram](https://en.wikipedia.org/wiki/Temperature%E2%80%93entropy_diagram)

Useful plots to study¶

  • At fixed T:

    • mu VS P; P VS mu
    • P VS V; V VS P
  • At fixed P:

    • mu VS T; T VS mu
    • T VS S; S VS T
  • Coexistence physics:

    • dV VS T
    • dS or latent heat VS T

Thermodynamic Potentials: from extensive to intensive variables¶

$$ \begin{align} E &= -PV + ST + \mu N \\ F &= E - ST = -PV + \mu N \\ \Omega &= F - \mu N = -P V \\ H &= E + PV = S T + \mu N \\ G &= H - ST = \mu N. \end{align} $$

$$ \begin{align} dE(V, S, N) &= -P dV + T dS + \mu dN \\ dF(V, T, N) &= -P dV - S dT + \mu dN \\ d\Omega(V,T,\mu) &= -P dV - S dT - N d \mu \\ dH(P, S, N) &= V dP + T dS + \mu dN \\ dG(P, T, N) &= V dP - S dT + \mu dN. \end{align} $$

Other forms of non-ideal gas¶

$$ \begin{align} (P + \frac{a}{v^2}) (v-b) = T \\ (P \, e^{a/T/v}) (v-b) = T \\ (P + \frac{a}{v^2 T}) (v-b) = T \\ (P + \frac{a}{(v+c)^2 T}) (v-b) = T \\ P v = T ( 1 + \frac{B}{v} + \frac{C}{v^2}+\ldots) \end{align} $$

Further topics¶

  • Spinodal lines, Ginzburg Landau Mean field theory
  • Legendre Transform
  • Lagrange Multiplier
  • Statistical Mechanics