Skip to content

Cahn-Hilliard phase separation (System B)

The Cahn-Hilliard equation, System B.

  • Class: CahnHilliardSystem
  • Import: from skcomplex.systems.physics.pattern_formation import CahnHilliardSystem
  • Field: physics/pattern_formation
  • Topics: field, phase separation, coarsening, conserved, system b, spectral, pde, conserved noise
  • References:
    • Brauns and Marchetti 2024
    • Allen and Cahn 1979
    • Cahn and Hilliard 1958
    • Bray 1994

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.physics.pattern_formation import CahnHilliardSystem

trajectory = CahnHilliardSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
shape (64, 64)
length 64.0
mean 0.0
kappa 1.0
temperature 0.0
random_state 0
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time continuous
Space lattice
Laws rate
Randomness deterministic
Domains real
Structure fixed

Class

The source of the class shows how it builds its components from its model parameters.

class CahnHilliardSystem(BaseSystem):
    """The Cahn-Hilliard equation, System B.

    It is the conserved relaxation of an order parameter in a double well.

    The order parameter follows `phi_t = laplacian(-phi + phi^3 - kappa laplacian(phi))`, a diffusion down the
    gradient of the chemical potential of the same free energy as the Allen-Cahn equation. Inside the spinodal,
    `|mean| < 1 / sqrt(3)`, small noise decomposes into domains of `phi` near 1 and -1, which coarsen by diffusion
    through the bulk so that their size grows as `t^(1/3)`, while the mean of `phi` stays fixed.

    A positive temperature adds the thermal noise of System B, `div(sqrt(2 temperature) eta)`, which conserves the mean
    of `phi` as well. The field then uses finite differences, and an SDE solver such as `EulerMaruyamaSolver` integrates
    it, with a step below `spacing^4 / (32 kappa)`. The system has the field `mixture.phi`.

    Args:
        shape:
            The number of sites along every axis of the periodic lattice.

        length:
            The side of the domain.

        mean:
            The mean of the order parameter, which the dynamics conserve.

        kappa:
            The stiffness of the interfaces.

        temperature:
            The temperature of the conserved thermal noise, or 0 for the deterministic equation.

        random_state:
            The seed or generator of the initial noise of amplitude 0.05.

        label:
            The label of the system, which names it when it is nested in another one.

        solver:
            The solver of the problem, or `'auto'` for exponential steps of 0.25 without noise.

        backend:
            The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.

    Examples:
        >>> from skcomplex.systems.physics.pattern_formation import CahnHilliardSystem
        >>> CahnHilliardSystem((8, 8), mean=0.3).prepare().system_.interactions[0].conserved
        True
        >>> system = CahnHilliardSystem((8, 8), temperature=0.1).prepare().system_
        >>> [interaction.label for interaction in system.interactions]
        ['energy', 'noise']
    """

    field = 'physics/pattern_formation'
    topics = ('field', 'phase separation', 'coarsening', 'conserved', 'system b', 'spectral', 'pde', 'conserved noise')
    references = REFERENCES
    until = 1000.0
    dt = 10.0

    def __init__(
        self,
        shape: Sequence[int] = (64, 64),
        *,
        length: float = 64.0,
        mean: float = 0.0,
        kappa: float = 1.0,
        temperature: float = 0.0,
        random_state: int | np.random.Generator | None = 0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.shape = shape
        self.length = length
        self.mean = mean
        self.kappa = kappa
        self.temperature = temperature
        self.random_state = random_state

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of a phase field that relaxes and conserves its mean.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        self.system_ = AllenCahnSystem._get_ordering(
            self.label,
            shape=self.shape,
            length=self.length,
            mean=self.mean,
            kappa=self.kappa,
            random_state=self.random_state,
            conserved=True,
            step=0.25,
            temperature=self.temperature,
        )

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

Interactions:

class PhaseFieldElement(Element):
    """Sites of a phase field.

    Every site holds the order parameter `phi` that free energies and noise move.

    Args:
        label:
            The label of the field.

        phi:
            The initial order parameter, one value per site or one for all.

        space:
            The lattice of the sites, or `None` for one site.

    Examples:
        >>> from skcomplex.systems.physics.pattern_formation._ordering import PhaseFieldElement
        >>> PhaseFieldElement('site', phi=0.5).phi
        0.5
    """

    def __init__(self, label: str, *, phi: object = 0.0, space: BaseSpace | None = None) -> None:
        super().__init__(label, space=space)
        self.phi = phi

class LandauEnergyInteraction(GradientInteraction):
    """Gradient flow of a field `phi` in the Landau free energy of a double well.

    The energy has a cost of gradients.

    The free energy is `F = integral -phi^2 / 2 + phi^4 / 4 + kappa |grad phi|^2 / 2`. Its functional derivative is
    the chemical potential `mu = -phi + phi^3 - kappa laplacian(phi)`. A non-conserved field relaxes as `phi_t = -mu`,
    the Allen-Cahn equation of system A, and a conserved field as `phi_t = laplacian(mu)`, the Cahn-Hilliard equation of
    system B, whose total stays constant.

    Args:
        label:
            The label of the interaction, unique among the components of its system.

        target:
            The path of the field.

        state:
            The name of the state of the field that holds the order parameter.

        kappa:
            The cost of gradients, the square of the width of the interfaces.

        conserved:
            Whether the order parameter is conserved.

    Examples:
        >>> from types import SimpleNamespace
        >>> from sage.all__sagemath_symbolics import var
        >>> from skcomplex.systems.physics.pattern_formation._ordering import LandauEnergyInteraction
        >>> energy = LandauEnergyInteraction('energy', target='mixture')
        >>> namespace = SimpleNamespace(target=SimpleNamespace(phi=var('phi')), kappa=var('kappa'))
        >>> [law.value for law in energy.prepare(namespace).laws_]
        [-phi^3 + kappa*laplacian(phi) + phi]
    """

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        state: str = 'phi',
        kappa: Annotated[float, RealSet.open(0, oo)] = 1.0,
        conserved: bool = False,
    ) -> None:
        super().__init__(label, target=target, states=state, variational=True, conserved=conserved)
        self.state = state
        self.kappa = kappa

    def _get_potential(self, namespace: SimpleNamespace) -> Expression:
        """Return the density `-phi^2 / 2 + phi^4 / 4 - kappa phi laplacian(phi) / 2`.

        The gradient term is taken by parts.

        Args:
            namespace:
                The namespace of the symbols the density can use.

        Returns:
            density:
                The density of the free energy.
        """
        phi = getattr(namespace.target, self.state)
        return -(phi**2) / 2 + phi**4 / 4 - namespace.kappa * phi * laplacian(phi) / 2