Skip to content

Latest commit

 

History

History
49 lines (35 loc) · 1.95 KB

ch.md

File metadata and controls

49 lines (35 loc) · 1.95 KB

cahn-hilliard phase separation

the cahn-hilliard equation is a model of phase separation from theoretical materials science.

generally, given an initial concentration field, it describes how particles will diffuse along a free energy gradient. david eyre developed a stable numerical integration scheme to solve the cahn-hillard partial differential equation. glitchcraft ports over eyre's solver from matlab. the implementation takes a single-channel or grayscale image as an initial concentration gradient and sets up a generator that yields the successive system state when called.

example:

take this initial sample image:

run the solver:

from glitchcraft.cahn_hillard import integrate
from pathlib import Path
from PIL import Image


def main(input_path, output_dir):
    src = Image.open(input_path).convert('L')
    output_dir.mkdir(exist_ok=True)
    
    filename_pattern = str(output_dir/"frame_{0:03d}.png")
    state = integrate(src)
    
    for i in progress(250):
        plt.imsave(filename_pattern.format(i), next(state), cmap="Greys")


if __name__ == "__main__":
    base = Path(__file__).parent
    input_path = base/"../artifacts/input/stream_square.png"
    output_dir = base/"../artifacts/output/ch_evolution"
    main(input_path, output_dir)

merge the individual frames into a gif:

ffmpeg -t 5 -pattern_type glob -i "*.png" -vf "scale=512:512:flags=lanczos" ch_evolution.gif

voilà