Skip to content

Phase Field Model (Cahn-Hilliard)

Optional dependency

Phase Field Model requires calphad. Plotting/visualization support additionally requires plots.

uv add "materialsframework[calphad]"

# For plotting/visualization support
uv add "materialsframework[calphad,plots]"
pip install "materialsframework[calphad]"

# For plotting/visualization support
pip install "materialsframework[calphad,plots]"

PhaseFieldModel

PhaseFieldModel(
    material_properties: MaterialParameters,
    simulation_grid: SimulationGrid | None = None,
    wrt_cycle: int = 5000,
    stop_iter: int = 50000,
    seed: int = 42,
)

Implements the Cahn-Hilliard solver with output visualization.

Initializes the phase field model with simulation grid and material properties.

Parameters:

Name Type Description Default
material_properties MaterialParameters

Material properties for the simulation.

required
simulation_grid SimulationGrid | None

The grid for the simulation. Defaults to None, meaning a new SimulationGrid with default parameters is created.

None
wrt_cycle int

Frequency of writing output files. Defaults to 5000.

5000
stop_iter int

Number of iterations to run the simulation. Defaults to 50000.

50000
seed int

Seed for the random number generator. Defaults to 42.

42

laplacian

laplacian(field: ndarray) -> np.ndarray

Computes the discrete Laplacian using a 9-point stencil.

Parameters:

Name Type Description Default
field ndarray

The field for which to compute the Laplacian.

required

Returns:

Type Description
ndarray

np.ndarray: The computed Laplacian of the field.

free_energy

free_energy(phi: ndarray) -> np.ndarray

Computes the free energy derivative based on the polynomial coefficients.

Parameters:

Name Type Description Default
phi ndarray

The phase field variable.

required

Returns:

Type Description
ndarray

np.ndarray: The computed free energy derivative.

evolve

evolve() -> None

Evolves the phase field using the Cahn-Hilliard equation.

save_plot

save_plot(iteration: int) -> None

Saves the current phase field as an image.

Parameters:

Name Type Description Default
iteration int

The current iteration number.

required

run_simulation

run_simulation(plot: bool = False) -> None

Runs the simulation for a specified number of iterations.

Parameters:

Name Type Description Default
plot bool

Whether to save plots of the phase field. Default is False.

False

SimulationGrid

SimulationGrid(
    nx: int = 128,
    ny: int = 128,
    lx: float = 2e-06,
    ly: float = 2e-06,
    dt: float = 1e-12,
)

Handles the simulation grid and phase field variables.

Initializes the simulation grid with given parameters.

Parameters:

Name Type Description Default
nx int

Number of grid points in x-direction. Defaults to 128.

128
ny int

Number of grid points in y-direction. Defaults to 128.

128
lx float

Length of the grid in x-direction. Defaults to 2e-6.

2e-06
ly float

Length of the grid in y-direction. Defaults to 2e-6.

2e-06
dt float

Time step for the simulation. Defaults to 1e-12.

1e-12

MaterialParameters

MaterialParameters(
    db: Database | str,
    temperature: float,
    component: str,
    composition: float,
    elements: list[str] | None = None,
    phase: str | None = None,
)

Stores material parameters such as energy and kinetic properties.

Initializes material parameters with given composition and potential values.

Parameters:

Name Type Description Default
db Database | str

pycalphad Database object or path to the database file.

required
temperature float

Temperature in Kelvin.

required
component str

Component name.

required
composition float

Composition value.

required
elements list[str] | None

List of elements. Defaults to None.

None
phase str | None

Phase name. Defaults to None.

None

Raises:

Type Description
ValueError

If multiple phases are found in the database and no phase is specified.