Two droplets merge: Cahn–Hilliard–Navier–Stokes
Two liquid drops are thrown towards each other. They touch, fuse into a dumbbell, ring, and settle into one round drop. Two boundaries become one, and that needs no special machinery here, because the interface is not a mesh boundary: it is a field.

The computed phase field on the adapted mesh each step ran on; the black line is the interface \(\phi=0\).
The problem
A phase field \(\phi\), \(+1\) in the liquid and \(-1\) outside, rides beside the flow (Jacqmin 1999; Yue, Feng, Liu & Shen 2004):
The chemical potential \(\mu\) splits the fourth-order operator into two second-order equations, and \(\mu\nabla\phi\) is the capillary force.
The jNO code
Four ordinary unknowns, and each equation a weak form:
u = d.unknown.vector(2, order=2) # P2 velocity, P1 pressure: Taylor-Hood
p, phi, mu = (d.unknown.scalar(name=n) for n in ("p", "phi", "mu")) # pressure, phase field, chemical potential
v, q, psi, chi = u.test(), p.test(), phi.test(), mu.test()
momentum = ( # rho (u_t + u.grad u) = -grad p + eta lap u + mu grad phi, weakly
RHO * (dot(dt(u), v) + dot(dot(grad(u), u), v))
+ ETA * ddot(grad(u), grad(v))
- p * jno.np.trace(grad(v))
- mu * dot(grad(phi), v) # the capillary force
)
cahn_hilliard = ( # phi_t + u.grad phi = div(M grad mu), weakly
(dt(phi) + dot(u, grad(phi))) * psi + MOBILITY * dot(grad(mu), grad(psi))
)
chemical_potential = mu * chi - LAM * (phi**3 - phi) / EPS**2 * chi - LAM * dot(grad(phi), grad(chi))
The mesh follows the interface: remesh(criterion=...) refines on any traced field, here \(1-\phi^2\), evaluated on the
live state at every remesh with a fixed budget of unknowns:
phi_now = phi.bind(x=x, y=y)
traj = fem.solve(
adapt=jno.solve.remesh(criterion=1.0 - phi_now * phi_now, every=12, max_dofs=fem.dofs), # follow the interface
nonlinear=jno.solve.newton(direct=True), # a sparse-direct Newton per step: the saddle needs it
)
Result
17,747 unknowns, 150 steps, twelve remeshes. The bridge opens at \(t=0.13\); the merged drop is stretched sideways, overshoots to upright and is round to 2 % from \(t=0.7\) on.
| start | end | |
|---|---|---|
| \(\phi\) midway between the drops | \(-0.901\) | \(+1.033\) (one drop) |
| width / height of the liquid | 2.54 | 1.00 |
| liquid area \(\int(1+\phi)/2\) | 0.15116 | 0.15285 (+1.12 %) |
Cahn–Hilliard conserves \(\int\phi\) exactly on a fixed mesh: between remeshes the area is constant to \(6\times10^{-17}\), so the 1.12 % is the twelve state transfers.
References. D. Jacqmin, J. Comput. Phys. 155 (1999) 96–127. P. Yue, J. J. Feng, C. Liu & J. Shen, J. Fluid Mech. 515 (2004) 293–317.
Full script
The whole script, as it runs in the tests
"""Two separate droplets collide and merge in a Cahn-Hilliard-Navier-Stokes model, on a mesh that follows the interface."""
import jax
import numpy as np
import jno
jax.config.update("jax_enable_x64", True)
run = jno.setup(__file__)
SIGMA, ETA, RHO = 1.0, 0.04, 1.0 # surface tension, viscosity, density
EPS, MOBILITY = 0.03, 1e-2 # interface width, Cahn-Hilliard mobility
LAM = 3.0 * SIGMA * EPS / (2.0 * np.sqrt(2.0)) # sigma = (2 sqrt 2 / 3) lam / eps
R, CX1, CX2, CY = 0.15, 0.27, 0.73, 0.5 # two drops whose rims are 0.16 (~3 interface widths) apart
U0 = 1.5 # the drops start moving toward each other at +-U0
T_END, N_STEPS = 1.2, 150
def near_the_rims(x, y, z): # gmsh calls a size field with three coordinates, in 2-D as well
rim = min(abs(np.hypot(x - CX1, y - CY) - R), abs(np.hypot(x - CX2, y - CY) - R))
return 0.008 + 0.05 * min(1.0, rim / 0.12) # fine at the interfaces, coarse in the bulk
d = jno.shape.rect(0, 0, 1, 1, size=near_the_rims).domain(time=(0.0, T_END, N_STEPS + 1))
x, y, t = d.variable("interior", split=True)
b, ci = d.variable("boundary", split=True), d.variable("initial", split=True)
u = d.unknown.vector(2, order=2) # P2 velocity, P1 pressure: Taylor-Hood
p, phi, mu = (d.unknown.scalar(name=n) for n in ("p", "phi", "mu")) # pressure, phase field, chemical potential
v, q, psi, chi = u.test(), p.test(), phi.test(), mu.test()
grad = lambda w: jno.np.jacobian(w, [x, y])
dot, ddot = jno.np.inner, lambda a, b: jno.np.inner(a, b, n_contract=2)
dt = lambda w: w.bind(x=x, y=y, t=t).t
momentum = ( # rho (u_t + u.grad u) = -grad p + eta lap u + mu grad phi, weakly
RHO * (dot(dt(u), v) + dot(dot(grad(u), u), v))
+ ETA * ddot(grad(u), grad(v))
- p * jno.np.trace(grad(v))
- mu * dot(grad(phi), v) # the capillary force
)
continuity = -q * jno.np.trace(grad(u))
cahn_hilliard = ( # phi_t + u.grad phi = div(M grad mu), weakly
(dt(phi) + dot(u, grad(phi))) * psi + MOBILITY * dot(grad(mu), grad(psi))
)
chemical_potential = mu * chi - LAM * (phi**3 - phi) / EPS**2 * chi - LAM * dot(grad(phi), grad(chi))
drop = lambda cx: jno.np.tanh((R - jno.np.sqrt((ci[0] - cx) ** 2 + (ci[1] - CY) ** 2)) / (np.sqrt(2.0) * EPS))
approach = (drop(CX1) - drop(CX2)) / 2.0 # +1 in the left drop, -1 in the right one: they move toward each other
fem = jno.fem(
[
momentum,
continuity,
cahn_hilliard,
chemical_potential,
u(*b) - (0.0, 0.0), # no-slip walls
p.pin(), # the pressure is defined up to a constant
u(*ci) - (U0 * approach, 0.0), # the drops are thrown toward each other
phi(*ci) - (drop(CX1) + drop(CX2) + 1.0), # two separate drops
]
)
print(f"CHNS droplet merger: {fem.dofs} DOFs on {len(np.asarray(d.mesh.points))} vertices")
phi_now = phi.bind(x=x, y=y)
traj = fem.solve(
adapt=jno.solve.remesh(criterion=1.0 - phi_now * phi_now, every=12, max_dofs=fem.dofs), # follow the interface
nonlinear=jno.solve.newton(direct=True), # a sparse-direct Newton per step: the saddle needs it
)
ic = fem.block_index(phi)
def phase(k):
"""phi at frame k on its own nodes and cells, and the liquid area int (1 + phi)/2 by the P1 vertex rule."""
lay = traj.layouts[k]
pts = np.asarray(lay["field_points"][ic])[:, :2]
val = np.asarray(traj.states[k])[lay["offsets"][ic] : lay["offsets"][ic + 1]]
tri = np.asarray(lay["cells_f"][ic])[:, :3]
e1, e2 = pts[tri[:, 1]] - pts[tri[:, 0]], pts[tri[:, 2]] - pts[tri[:, 0]]
area = 0.5 * np.abs(e1[:, 0] * e2[:, 1] - e1[:, 1] * e2[:, 0])
return pts, val, tri, float(np.sum(area * (1.0 + val[tri]).mean(axis=1) / 2.0))
frames = [phase(k) for k in range(len(traj))]
areas = np.array([f[3] for f in frames])
gap = lambda pts, val: float(val[np.argmin(np.hypot(pts[:, 0] - 0.5, pts[:, 1] - CY))]) # phi midway between the drops
aspect = lambda pts, val: float(np.ptp(pts[val > 0, 0]) / np.ptp(pts[val > 0, 1])) # width / height of the liquid
remeshes = sum(bool(h.get("remeshed")) for h in fem.adapt_history)
# Cahn-Hilliard conserves int phi exactly on a fixed mesh, so any drift comes from the state transfer at a remesh
same_mesh = [k for k in range(len(traj) - 1) if traj.meshes[k] is traj.meshes[k + 1]]
between = max(abs(areas[k + 1] - areas[k]) for k in same_mesh)
print(f" gap phi: {gap(*frames[0][:2]):+.3f} -> {gap(*frames[-1][:2]):+.3f} (two drops -> one)")
print(f" width/height: {aspect(*frames[0][:2]):.2f} -> {aspect(*frames[-1][:2]):.2f} (relaxing toward a circle)")
print(
f" liquid area: {areas[0]:.5f} -> {areas[-1]:.5f} ({(areas[-1] / areas[0] - 1) * 100:+.2f} %, over {remeshes} remeshes)"
)
print(f" largest change between remeshes: {between:.1e}")
assert gap(*frames[0][:2]) < 0.0 < 0.9 < gap(*frames[-1][:2]) # the two drops merged
assert aspect(*frames[-1][:2]) < 0.8 * aspect(*frames[0][:2]) # and relax toward a circle
assert between < 1e-8 and abs(areas[-1] / areas[0] - 1.0) < 2e-2 and remeshes >= 2 # conservation; the mesh followed