-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpf_circle.py
198 lines (144 loc) · 5.14 KB
/
pf_circle.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import numpy as np
import dolfin as df
import mshr
from mpi4py import MPI
import math
def make_circle_mesh(R, res=40.):
domain = mshr.Circle(df.Point(0., 0.), 1.)
mesh = mshr.generate_mesh(domain, res)
mesh.coordinates()[:] *= R
return mesh
def dfdphi(phi, c, lamda):
return (1.-phi**2)*(phi-c*lamda)
def mag(x):
return df.sqrt(df.dot(x, x))
def comp_corr(p, P, V):
grad_p = df.project(df.grad(p), V)
mag_grad_p = mag(grad_p)
kappa = df.div(grad_p/mag_grad_p)
return df.project(kappa*mag_grad_p, P)
class Around(df.SubDomain):
def inside(self, x, on_boundary):
return on_boundary
class PhaseFieldEquation(df.NonlinearProblem):
def __init__(self, a, L):
df.NonlinearProblem.__init__(self)
self.L = L
self.a = a
def F(self, b, x):
df.assemble(self.L, tensor=b)
def J(self, A, x):
df.assemble(self.L, tensor=A)
def main():
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
# Form compiler options
df.parameters["form_compiler"]["optimize"] = True
df.parameters["form_compiler"]["cpp_optimize"] = True
df.set_log_level(df.WARNING)
ffc_options = {"optimize": True,
"eliminate_zeros": True,
"precompute_basis_const": True,
"precompute_ip_const": True}
res = 150.
R = 200.
R_0 = 6.
c_inf = 1.0
Pe_PF = 1.
# Da = 100.
T = 5000.
# lamda = Pe_PF / (5./3. + math.sqrt(2)/Da)
lamda = 3.*Pe_PF/5.
mesh = make_circle_mesh(R, res)
mesh.init()
h5f_mesh = df.HDF5File(mesh.mpi_comm(), "circle_mesh.h5", "w")
h5f_mesh.write(mesh, "mesh")
h5f_mesh.close()
P = df.FunctionSpace(mesh, "CG", 1)
V = df.VectorFunctionSpace(mesh, "CG", 1)
domains = df.CellFunction("size_t", mesh)
boundaries = df.FacetFunction("size_t", mesh)
around = Around()
boundaries.set_all(0)
around.mark(boundaries, 1)
bcs_phi = df.DirichletBC(P, df.Constant(1.), boundaries, 1)
bcs_c = df.DirichletBC(P, df.Constant(c_inf), boundaries, 1)
dx = df.Measure('dx', domain=mesh, subdomain_data=domains)
ds = df.Measure('ds', domain=mesh, subdomain_data=boundaries)
num_cells_in = np.array(float(mesh.num_entities(2)))
num_cells = np.zeros(1)
comm.Allreduce(num_cells_in, num_cells, op=MPI.SUM)
dt = 0.9*np.sqrt(np.pi*R**2/num_cells[0])
nt = int(T/dt)
if rank == 0:
print "dt =", dt
print "nt =", nt
phi_init = df.Expression("tanh((sqrt(x[0]*x[0]+x[1]*x[1]) - "
"R_0 + a*sin(n*atan2(x[0], x[1])))/delta)",
R_0=R_0,
delta=1.,
a=.0, n=6., degree=1)
phi_init = df.interpolate(phi_init, P)
# Define nonlinear variational problem
dphi = df.TrialFunction(P) #
phi = df.Function(P) # current solution
phi_0 = df.Function(P) # solution from previous step
q = df.TestFunction(P)
kappa = df.Function(P) # curvature, based on previous
kappa.vector()[:] = 0.
phi.interpolate(phi_init)
phi_0.interpolate(phi_init)
c_test = df.TestFunction(P)
c_trial = df.TrialFunction(P)
c_0 = df.Function(P)
c_0.vector()[:] = c_inf * 0.5*(phi_init.vector()[:]+1.)
c = df.Function(P)
c.interpolate(c_0)
# df.plot(c, interactive=True)
F = (Pe_PF*q*(phi - phi_0)/dt*dx
+ df.dot(df.grad(phi), df.grad(q))*dx
- q*(dfdphi(phi, lamda, c_0))*dx)
J = df.derivative(F, phi, dphi)
F_c = c_test*(c_trial-c_0)/dt*dx + \
df.dot(df.grad(c_trial), df.grad(c_test))*dx
a, L = df.lhs(F_c), df.rhs(F_c)
# df.solve(a == L, c, bcs_c)
# df.plot(c, interactive=True)
c_prob = df.LinearVariationalProblem(a, L, c, bcs_c)
c_solver = df.LinearVariationalSolver(c_prob)
# problem = PhaseFieldEquation(F, J)
# solver = df.NewtonSolver()
# solver.parameters["linear_solver"] = "lu"
# solver.parameters["convergence_criterion"] = "incremental"
# solver.parameters["relative_tolerance"] = 1e-6
progress = df.Progress("Time-steppin'")
# df.set_log_level(df.PROGRESS)
field_names = ["phi", "c", "corr"]
xf = dict()
for field in field_names:
xf[field] = df.XDMFFile(mesh.mpi_comm(), field + ".xdmf")
xf[field].parameters["rewrite_function_mesh"] = False
xf[field].parameters["flush_output"] = True
phi_corr = df.Function(P)
t = 0.
for it in xrange(nt):
if rank == 0:
print "Step", it
t += dt
c_0.vector()[:] = c.vector()
phi_0.vector()[:] = phi.vector()
df.solve(F == 0, phi, bcs_phi, J=J,
form_compiler_parameters=ffc_options)
phi_corr.assign(comp_corr(phi, P, V))
phi.vector()[:] -= phi_corr.vector()[:]*dt
c_solver.solve()
c.vector()[:] += (phi.vector()[:] - phi_0.vector()[:])/dt
if it % 10 == 0:
xf["phi"].write(phi, t)
xf["c"].write(c, t)
xf["corr"].write(phi_corr, t)
progress.update(t/T)
# df.plot(phi, title="phi", interactive=True)
if __name__ == "__main__":
main()