"plt.plot(xdata, tanh_fit(xdata, a, b, c, d))# np.tanh((xdata-52)/12.4)*0.36+0.5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
...
...
%% Cell type:code id: tags:
``` python
# Extended Standard Cahn-Hilliard Example to Binary Flory Huggins as discussed on 28/11/2019
# Example can be found at https://bitbucket.org/fenics-project/dolfin/src/master/python/demo/documented/cahn-hilliard/demo_cahn-hilliard.py.rst#rst-header-id1
# Runs with fenics 2019.01
# The resulting .pvd file can be opened using default settings in ParaView
importmatplotlib.pyplotasplt
importnumpyasnp
fromscipy.optimizeimportcurve_fit
importrandom
fromdolfinimport*
# Class representing the intial conditions
classInitialConditions(UserExpression):
def__init__(self,**kwargs):
random.seed(2+MPI.rank(MPI.comm_world))
super().__init__(**kwargs)
defeval(self,values,x):
ifx[0]>0.5:
if (x[0]>0.48)&(x[0]<0.52)&(x[1]>0.48)&(x[1]<0.52):
# values[0] = 0.63 + 0.02*(0.5 - random.random())
values[0]=0.6
else:
values[0]=0.35
values[1]=0.0
# values[0] = 0.63 + 0.02*(0.5 - random.random())
values[1]=0.0
defvalue_shape(self):
return (2,)
# Class for interfacing with the Newton solver
classCahnHilliardEquation(NonlinearProblem):
def__init__(self,a,L):
NonlinearProblem.__init__(self)
self.L=L
self.a=a
defF(self,b,x):
assemble(self.L,tensor=b)
defJ(self,A,x):
assemble(self.a,tensor=A)
# Model parameters
lmbda=1.0e-02# surface parameter
dt=5.0e-03# time step
dt=5.0e-02# time step
theta=0.5# time stepping family, e.g. theta=1 -> backward Euler, theta=0.5 -> Crank-Nicolson