Skip to content

Commit ce8694d

Browse files
committed
started AC part for TOMS
1 parent 614f082 commit ce8694d

File tree

3 files changed

+365
-21
lines changed

3 files changed

+365
-21
lines changed

pySDC/implementations/problem_classes/AllenCahn_2D_FD.py

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# noinspection PyUnusedLocal
1414
class allencahn_fullyimplicit(ptype):
1515
"""
16-
Example implementing the Allen-Cahn equation in 2D with finite differences
16+
Example implementing the Allen-Cahn equation in 2D with finite differences and periodic BC
1717
1818
Attributes:
1919
A: second-order FD discretization of the 2D laplace operator
@@ -42,17 +42,22 @@ def __init__(self, problem_params, dtype_u, dtype_f):
4242
raise ProblemError('this is a 2d example, got %s' % problem_params['nvars'])
4343
if problem_params['nvars'][0] != problem_params['nvars'][1]:
4444
raise ProblemError('need a square domain, got %s' % problem_params['nvars'])
45-
# if (problem_params['nvars'][0] + 1) % 2 != 0:
46-
# raise ProblemError('the setup requires nvars = 2^p per dimension')
45+
if problem_params['nvars'][0] % 2 != 0:
46+
raise ProblemError('the setup requires nvars = 2^p per dimension')
4747

4848
# invoke super init, passing number of dofs, dtype_u and dtype_f
4949
super(allencahn_fullyimplicit, self).__init__(problem_params['nvars'], dtype_u, dtype_f, problem_params)
5050

5151
# compute dx and get discretization matrix A
52-
self.dx = 1.0 / (self.params.nvars[0] - 0)
52+
self.dx = 1.0 / self.params.nvars[0]
5353
self.A = self.__get_A(self.params.nvars, self.dx)
5454
self.xvalues = np.array([i * self.dx - 0.5 for i in range(self.params.nvars[0])])
5555

56+
self.newton_itercount = 0
57+
self.lin_itercount = 0
58+
self.newton_ncalls = 0
59+
self.lin_ncalls = 0
60+
5661
@staticmethod
5762
def __get_A(N, dx):
5863
"""
@@ -74,11 +79,6 @@ def __get_A(N, dx):
7479
doffsets = np.concatenate((offsets, np.delete(offsets, zero_pos - 1) - N[0]))
7580

7681
A = sp.diags(dstencil, doffsets, shape=(N[0], N[0]), format='csc')
77-
# A = sp.diags(stencil, [-1, 0, 1], shape=(N[0], N[0]), format='csc')
78-
# A[0, 1] = 2
79-
# A[-1, -2] = 2
80-
# print(A.todense())
81-
# exit()
8282
A = sp.kron(A, sp.eye(N[0])) + sp.kron(sp.eye(N[1]), A)
8383
A *= 1.0 / (dx ** 2)
8484

@@ -136,6 +136,9 @@ def solve_system(self, rhs, factor, u0, t):
136136
me = self.dtype_u(self.init)
137137
me.values = u.reshape(self.params.nvars)
138138

139+
self.newton_ncalls += 1
140+
self.newton_itercount += n
141+
139142
return me
140143

141144
def eval_f(self, u, t):
@@ -173,17 +176,14 @@ def u_exact(self, t):
173176
for j in range(self.params.nvars[1]):
174177
r2 = self.xvalues[i] ** 2 + self.xvalues[j] ** 2
175178
me.values[i, j] = np.tanh((self.params.radius - np.sqrt(r2)) / self.params.eps)
176-
# if r2 <= self.params.radius ** 2:
177-
# me.values[i, j] = 1.0
178-
# else:
179-
# me.values[i, j] = 0.0
179+
180180
return me
181181

182182

183183
# noinspection PyUnusedLocal
184184
class allencahn_semiimplicit(allencahn_fullyimplicit):
185185
"""
186-
Example implementing the Allen-Cahn equation in 2D with finite differences
186+
Example implementing the Allen-Cahn equation in 2D with finite differences, SDC standard splitting
187187
188188
Attributes:
189189
A: second-order FD discretization of the 2D laplace operator
@@ -224,20 +224,31 @@ def solve_system(self, rhs, factor, u0, t):
224224
dtype_u: solution as mesh
225225
"""
226226

227+
class context:
228+
num_iter = 0
229+
230+
def callback(xk):
231+
context.num_iter += 1
232+
return context.num_iter
233+
227234
me = self.dtype_u(self.init)
228235

229236
Id = sp.eye(self.params.nvars[0] * self.params.nvars[1])
230237

231238
me.values = cg(Id - factor * self.A, rhs.values.flatten(), x0=u0.values.flatten(), tol=self.params.lin_tol,
232-
maxiter=self.params.lin_maxiter)[0]
239+
maxiter=self.params.lin_maxiter, callback=callback)[0]
233240
me.values = me.values.reshape(self.params.nvars)
241+
242+
self.lin_ncalls += 1
243+
self.lin_itercount += context.num_iter
244+
234245
return me
235246

236247

237248
# noinspection PyUnusedLocal
238249
class allencahn_semiimplicit_v2(allencahn_fullyimplicit):
239250
"""
240-
Example implementing the Allen-Cahn equation in 2D with finite differences
251+
Example implementing the Allen-Cahn equation in 2D with finite differences, AC splitting
241252
242253
Attributes:
243254
A: second-order FD discretization of the 2D laplace operator
@@ -315,13 +326,16 @@ def solve_system(self, rhs, factor, u0, t):
315326
me = self.dtype_u(self.init)
316327
me.values = u.reshape(self.params.nvars)
317328

329+
self.newton_ncalls += 1
330+
self.newton_itercount += n
331+
318332
return me
319333

320334

321335
# noinspection PyUnusedLocal
322336
class allencahn_multiimplicit(allencahn_fullyimplicit):
323337
"""
324-
Example implementing the Allen-Cahn equation in 2D with finite differences
338+
Example implementing the Allen-Cahn equation in 2D with finite differences, SDC standard splitting
325339
326340
Attributes:
327341
A: second-order FD discretization of the 2D laplace operator
@@ -362,13 +376,24 @@ def solve_system_1(self, rhs, factor, u0, t):
362376
dtype_u: solution as mesh
363377
"""
364378

379+
class context:
380+
num_iter = 0
381+
382+
def callback(xk):
383+
context.num_iter += 1
384+
return context.num_iter
385+
365386
me = self.dtype_u(self.init)
366387

367388
Id = sp.eye(self.params.nvars[0] * self.params.nvars[1])
368389

369390
me.values = cg(Id - factor * self.A, rhs.values.flatten(), x0=u0.values.flatten(), tol=self.params.lin_tol,
370-
maxiter=self.params.lin_maxiter)[0]
391+
maxiter=self.params.lin_maxiter, callback=callback)[0]
371392
me.values = me.values.reshape(self.params.nvars)
393+
394+
self.lin_ncalls += 1
395+
self.lin_itercount += context.num_iter
396+
372397
return me
373398

374399
def solve_system_2(self, rhs, factor, u0, t):
@@ -422,13 +447,16 @@ def solve_system_2(self, rhs, factor, u0, t):
422447
me = self.dtype_u(self.init)
423448
me.values = u.reshape(self.params.nvars)
424449

450+
self.newton_ncalls += 1
451+
self.newton_itercount += n
452+
425453
return me
426454

427455

428456
# noinspection PyUnusedLocal
429457
class allencahn_multiimplicit_v2(allencahn_fullyimplicit):
430458
"""
431-
Example implementing the Allen-Cahn equation in 2D with finite differences
459+
Example implementing the Allen-Cahn equation in 2D with finite differences, AC splitting
432460
433461
Attributes:
434462
A: second-order FD discretization of the 2D laplace operator
@@ -506,6 +534,9 @@ def solve_system_1(self, rhs, factor, u0, t):
506534
me = self.dtype_u(self.init)
507535
me.values = u.reshape(self.params.nvars)
508536

537+
self.newton_ncalls += 1
538+
self.newton_itercount += n
539+
509540
return me
510541

511542
def solve_system_2(self, rhs, factor, u0, t):
@@ -524,8 +555,6 @@ def solve_system_2(self, rhs, factor, u0, t):
524555

525556
me = self.dtype_u(self.init)
526557

527-
# Id = sp.eye(self.params.nvars[0] * self.params.nvars[1])
528-
529558
me.values = 1.0 / (1.0 - factor * 1.0 / self.params.eps ** 2) * rhs.values
530559
me.values = me.values.reshape(self.params.nvars)
531560
return me

0 commit comments

Comments
 (0)