-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcahn_hilliard.py
98 lines (73 loc) · 2.11 KB
/
cahn_hilliard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2019, sumeet92k
# All rights reserved. Please read the "license.txt" for license terms.
#
# Project Title: Materials Modeling Course Tutorials
#
# Developer: Sumeet Khanna
# Contact Info: https://github.com/sumeet92k
"""
TUTORIAL 10: Cahn-Hilliard Equation
Solves the Cahn-Hilliard equation for different cases of initialization of the
initial composition (c). Plots the composition profile with time.
"""
import numpy as np
import matplotlib.pyplot as plt
W, M, K = 1, 1, 1
mesh_x = 200
dx = 1
dt = 0.005
t_every = 20000
timesteps = 200000
def free_energy(c):
return W*c**2*(1-c)**2 #+ c**2*(3-2*c)
def d_free_energy(c):
return 2*W*c*(c-1)*(2*c-1) #+ 6*c*(1-c)
def calc_laplacian(c):
laplacian_c = np.zeros(mesh_x)
for i in range(1, mesh_x-2):
laplacian_c[i] = (c[i+1] -2*c[i] + c[i-1])/(dx**2)
return laplacian_c
def calc_surface_energy(c):
ana_SE = np.sqrt(K*W)/3
grad_c = np.zeros(mesh_x)
for i in range(1, mesh_x-1):
grad_c[i] = (c[i+1] - c[i-1])/(2*dx)
grad_SE = 2*K*np.sum(grad_c**2)*dx
free_energy_SE = 2*np.sum( free_energy(c) )*dx
print(t, ana_SE, grad_SE, free_energy_SE)
def impose_PBC(c):
c[0] = c[mesh_x-4]
c[1] = c[mesh_x-3]
c[mesh_x-2] = c[2]
c[mesh_x-1] = c[3]
return c
def initialize(c):
amplitude = 0.1
wavelength = mesh_x/6
for i in range(mesh_x):
c[i] = 0.5 + amplitude*np.sin(np.pi*i/wavelength)
return c
def initialize_random(c):
amplitude = 0.1
for i in range(mesh_x):
c[i] = 0.5 + amplitude*np.random.random()
return c
c = np.zeros(mesh_x)
#c[0:mesh_x//2] = 1 # initialize c
# c = initialize(c)
c = initialize_random(c)
c = impose_PBC(c)
for t in range(timesteps):
laplacian_c = calc_laplacian(c)
mu = d_free_energy(c) - 2*K*laplacian_c
laplacian_mu = calc_laplacian(mu)
dc_dt = M*laplacian_mu
c = c + dc_dt*dt
c = impose_PBC(c)
if t%t_every == 0:
plt.plot(c, '-^', label='t=' + str(t) )
calc_surface_energy(c)
plt.legend()
plt.show()