-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOKLibrary.py
349 lines (282 loc) · 10.4 KB
/
OKLibrary.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import random
import sympy as sym
from dolfin import *
# import matplotlib
# matplotlib.use('TkAgg')
# from Tkinter import *
import matplotlib.pyplot as plt
import numpy as np
import scipy.io as io
# Class for interfacing with the Newton solver
class OKsystem(NonlinearProblem):
def __init__(self, a, L):
NonlinearProblem.__init__(self)
self.L = L
self.a = a
def F(self, b, x):
assemble(self.L, tensor=b)
def J(self, A, x):
assemble(self.a, tensor=A)
def OKuniform(epssi, m, sigma, mesh, dt, Tfinal, ICs, outputfile=""):
# Function to simulation the OKDE with uniform time step as specified.
# Returns the relavent data to a number of results presented in the special topic report, and the solution at Tfinal
# Build function spaxe and mixed function space for solution of O-K system
P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
P2 = FunctionSpace(mesh, P1)
ME = FunctionSpace(mesh, P1*P1)
# Define test functions
q, v = TestFunctions(ME)
# Define functions
y = Function(ME) # current solution
y0 = Function(ME) # solution from previous converged step
# Split mixed functions
u, w = split(y)
u0, w0 = split(y0)
# Define Function space, function and trial and test functions needed to solve the Energy Subproblem
V = FiniteElement("Lagrange", mesh.ufl_cell(), 1) # Linear Lagrange Element
R = FiniteElement("Real",mesh.ufl_cell(),0) # Real Element
W = FunctionSpace(mesh, V*R)
r, c = TrialFunction(W)
p1, p2 = TestFunctions(W)
ww = Function(W)
# IC's
ICs = interpolate(ICs,ME)
y.assign(ICs)
y0.assign(ICs)
# Compute dphi/du
u = variable(u)
phi = 0.25*(1-(u**2))**2
dphidu = diff(phi, u)
# Optimise
parameters["form_compiler"]["optimize"] = True
parameters["form_compiler"]["cpp_optimize"] = True
# Weak form of the equations
L1 = inner(u,q)*dx - inner(u0,q)*dx + dt*dot(grad(w), grad(q))*dx + dt*sigma*inner(u-m,q)*dx
L2 = inner(w,v)*dx - (epssi**2)*dot(grad(u), grad(v))*dx - inner(dphidu,v)*dx
L = L1 + L2
# Compute derivstive for nonlinear solver
a = derivative(L, y)
# Create nonlinear problem and Newton solver
problem = OKsystem(a, L)
solver = NewtonSolver()
#Set Solver Parameters
solver.parameters["linear_solver"] = "lu"
solver.parameters["convergence_criterion"] = "incremental"
solver.parameters["relative_tolerance"] = 1e-6
# Output file if specified to do so
if (outputfile != ""):
output = File(outputfile, "compressed")
output << (y.split()[0],0.0)
# Set up time-stepping
t = 0.0
N = int((1.0*Tfinal)/dt)
# Arrays to track Energy, Mass and time
Energy = np.zeros([1,N+3])
Mass = np.zeros([1,N+3])
time = np.zeros([1,N+3])
# Calculate |Omega| (though trivial for the unit square or unit interval)
unity = interpolate(Constant(1.0),P2)
measure = assemble(unity*dx)
# Energy at time 0 (not physically menaningful)
a = (inner(grad(r),grad(p1)) + c*p1 + r*p2 )*dx
L = - inner(u-m,p1)*dx
solve(a == L, ww)
(rr, cc) = ww.split()
# calculate three parts individually for easy modification to investigate the relationship between and dynamics of them separately
e_part1 = ((epssi**2)/2.0)*dot(grad(u), grad(u))*dx(mesh)
e_part2 = 0.25*(1-(u**2))**2*dx(mesh)
e_part3 = (sigma/2.0)*rr*(u-m)*dx(mesh)
e1 = assemble(e_part1)
e2 = assemble(e_part2)
e3 = assemble(e_part3)
E = e1+e2+e3;
time[0,0] = t
Energy[0,0] = E
Mass[0,0] = assemble(u*dx)/measure
count = 1
## Time loop
while (t <= Tfinal):
t += dt
## Solve O-K system
y0.vector()[:] = y.vector() #Update solution at time step n-1
solver.solve(problem, y.vector()) #Solve system at time step n
if (outputfile != ""):
output << (y.split()[0], t) #Add to file
## Track Free Energy at each step
a = (inner(grad(r),grad(p1)) + c*p1 + r*p2 )*dx
L = - inner(u-m,p1)*dx
solve(a == L, ww)
(rr, cc) = ww.split()
e_part1 = ((epssi**2)/2.0)*dot(grad(u), grad(u))*dx(mesh)
e_part2 = 0.25*(1-(u**2))**2*dx(mesh)
e_part3 = (sigma/2.0)*rr*(u-m)*dx(mesh)
e1 = assemble(e_part1)
e2 = assemble(e_part2)
e3 = assemble(e_part3)
E = e1+e2+e3;
time[0,count] = t
Energy[0,count] = E
## Track mass at each step
Mass[0,count] = assemble(u*dx)/measure
count+=1
## Output U vector (without dof mapping for now)
u = project(u,P2)
uvec = u.vector().array()
data = {"Energy": Energy, "t": time, "m": Mass, "U": uvec} #Return as dictionary for easy save to .mat file
return y, data
def OKadaptive(epssi, m, sigma, mesh, n, Tfinal, ICs, tol, outputfile=""):
# Function to simulate the adaptive time stepping algorithm, for a user-specified tolerance parameter.
# dt_min and dt_max must be change in the body of the function
h = 1.0/n
# Set up time stepping
t = 0.0
dtmin = 0.5*(h**2)
dtmax = 50*(h**2)
dt = dtmin
N = 10000 #max Number of timesteps
# Build mixed function space for solution of O-K system
V = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
P2 = FunctionSpace(mesh, V)
ME = FunctionSpace(mesh, V*V)
#Define test functions
q, v = TestFunctions(ME)
# Define functions
y = Function(ME) # current solution
y0 = Function(ME) # solution from previous converged step
# Split mixed functions
u, w = split(y)
u0, w0 = split(y0)
# Define Function space, function and trial and test function needed to solve for Energy
# V = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
R = FiniteElement("Real",mesh.ufl_cell(),0)
W = FunctionSpace(mesh, V*R)
r, c = TrialFunction(W)
p1, p2 = TestFunctions(W)
ww = Function(W)
# IC's
ICs = interpolate(ICs,ME)
y.assign(ICs)
y0.assign(ICs)
# Compute dphi/du
u = variable(u)
phi = 0.25*(1-(u**2))**2
dphidu = diff(phi, u)
#Optimise
parameters["form_compiler"]["optimize"] = True
parameters["form_compiler"]["cpp_optimize"] = True
# Weak form of the equations
L1 = inner(u,q)*dx - inner(u0,q)*dx + dt*dot(grad(w), grad(q))*dx + dt*sigma*inner(u-m,q)*dx
L2 = inner(w,v)*dx - (epssi**2)*dot(grad(u), grad(v))*dx - inner(dphidu,v)*dx
L = L1 + L2
# Compute derivative
a = derivative(L, y)
# Create nonlinear problem and Newton solver
problem = OKsystem(a, L)
solver = NewtonSolver()
solver.parameters["linear_solver"] = "lu"
solver.parameters["convergence_criterion"] = "incremental"
solver.parameters["relative_tolerance"] = 1e-6
# Output file
if (outputfile != ""):
output = File(outputfile, "compressed")
# Arrays to track Energy, Mass and time
Energy = np.zeros([1,N+2])
Mass = np.zeros([1,N+2])
time = np.zeros([1,N+2])
InfNorm = np.zeros([1,N+2])
DTs = np.zeros([1,N+2])
# Calculate |Omega|
unity = interpolate(Constant(1.0),P2)
measure = assemble(unity*dx)
count = 0
dt = dtmin
while (t <= Tfinal):
time[0,count] = t
## Solve O-K system
y0.vector()[:] = y.vector() #Update solution at time step n-1
solver.solve(problem, y.vector()) #Solve system at time step n
if (outputfile != ""):
output << (y.split()[0], t) #Add to file
## Track Free Energy at each step
# Solve Energy subproblem
a = (inner(grad(r),grad(p1)) + c*p1 + r*p2 )*dx
L = - inner(u-m,p1)*dx
solve(a == L, ww)
# Calculate energy
(rr, cc) = ww.split()
e_part1 = ((epssi**2)/2.0)*dot(grad(u), grad(u))*dx(mesh)
e_part2 = 0.25*(1-(u**2))**2*dx(mesh)
e_part3 = (sigma/2.0)*rr*(u-m)*dx(mesh)
e1 = assemble(e_part1)
e2 = assemble(e_part2)
e3 = assemble(e_part3)
E = e1+e2+e3
Energy[0,count] = E
## Track mass at each step
Mass[0,count] = assemble(u*dx)/measure
## Calculate new time step
change = project(u-u0,P2)
inf_e_norm = max(abs(change.vector().array()))
InfNorm[0,count] = inf_e_norm
dt = min(max((tol*1.0)/inf_e_norm , dtmin), dtmax)
print(dt)
DTs[0,count] = dt
t += dt
count+=1
u = project(u,P2)
uvec = u.vector().array()
data = {"E": Energy, "t": time, "m": Mass, "inf_norm": InfNorm, "Dt": DTs, "U": uvec}
return y, data
def OKuniformIMX(epssi, m, sigma, mesh, dt, Tfinal, ICs, outputfile=""):
# Build mixed function space for solution of O-K system
V = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
P2 = FunctionSpace(mesh, V)
ME = FunctionSpace(mesh, V*V)
# Define trial and test functions
dy = TrialFunction(ME)
q, v = TestFunctions(ME)
# Define functions
y = Function(ME) # current solution
y0 = Function(ME) # solution from previous converged step
# Split mixed functions
du, dw = split(dy)
u, w = split(y)
u0, w0 = split(y0)
# Define Function space, function and trial and test function needed to solve for Energy
# V = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
R = FiniteElement("Real",mesh.ufl_cell(),0)
W = FunctionSpace(mesh, V*R)
r, c = TrialFunction(W)
p1, p2 = TestFunctions(W)
ww = Function(W)
# IC's
ICs = interpolate(ICs,ME)
y.assign(ICs)
y0.assign(ICs)
# Form compiler options
parameters["form_compiler"]["optimize"] = True
parameters["form_compiler"]["cpp_optimize"] = True
# Weak form of the equations
L1 = inner(u,q)*dx - inner(u0,q)*dx + dt*dot(grad(w), grad(q))*dx + dt*sigma*inner(u-m,q)*dx
L2 = inner(w,v)*dx - (epssi**2)*dot(grad(u), grad(v))*dx - inner(u0**3,v)*dx + inner(u,v)*dx
L = L1 + L2
# Output file
if (outputfile != ""):
output = File(outputfile, "compressed")
output << (y.split()[0],0.0)
# Set up time stepping
t = 0.0
N = int(Tfinal//dt)
count = 0
while (t <= Tfinal):
t += dt
## Solve O-K system
y0.vector()[:] = y.vector() #Update solution at time step n-1
solve(L == 0, y) #Solve system at time step n
if (outputfile != ""):
output << (y.split()[0], t) #Add to file
count+=1
u = project(u,P2)
uvec = u.vector().array()
data = {"U": uvec}
return y, data