The example program provided with Illuminator solves the Cahn-Hilliard equation in 3-D. The implementation is split into files cahnhill.c (appendix H) containing all of the free energy functions, derivatives, etc.; and chts.c (appendix G) with main(), initialization, callbacks--essentially, the overhead of the program; and a small header with the data structure in cahnhill.h (currently not included in the documentation because of a bug in the cxref documentation generation system). The calls to Illuminator library functions are contained in chts.c.
The idea behind Cahn-Hilliard is that we have a concentration field , with
associated thermodynamic free energy density
given by
The total free energy of the system occupying the body is
The interface thickness is on the order of
,
and the surface energy
on the order of
, so we can
set
and
, with constants which
we'll worry about later.
Returning to equation 4.5, it expands to
Now we turn to the PETSc implementation, using distributed arrays and
TS timestepping solvers. The nonlinear timestepping code uses
runtime-selectable timestepping solvers (explicit, implicit, or default
Crank-Nicholson) to solve the equation
Starting with the term, the Laplacian
at
can
be approximated using the standard 5-node finite difference stencil:
For the -term, the Laplacian of the Laplacian is a bit more messy.
Using the notation in equation 4.11 but only in the first quadrant
(the coefficients are symmetric), the term will be
times:
These functions are calculated by the function ch_residual_2d()
(appendix H.1.9) and assembled into a PETSc vector in ch_residual_vector_2d() (appendix
H.1.5). The derivatives are calculated
in two parts: the
term's derivative matrix is built at the start of
the run in ch_jacobian_alpha_2d() (appendix
H.1.3), and with each nonlinear iteration,
the
term's derivative matrix is added to that in ch_jacobian_2d() (appendix H.1.1).
Note that this is all in 2-D, the 3-D version is left as an exercise to the reader; though it's already coded in the corresponding _3d functions in cahnhill.c.