"# Standard Cahn-Hilliard Example that runs with fenics 2019.1\n",
"# Examples 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\n",
"# The resulting .pvd file can be opened using default settings in ParaView\n",
# Standard Cahn-Hilliard Example that runs with fenics 2019.1
# Examples 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
# The resulting .pvd file can be opened using default settings in ParaView
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):
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-14# time step
theta=0.5# time stepping family, e.g. theta=1 -> backward Euler, theta=0.5 -> Crank-Nicolson