Skip to content

Commit 1b64530

Browse files
authored
Change dx to du (#177)
1 parent 530d360 commit 1b64530

File tree

2 files changed

+84
-84
lines changed

2 files changed

+84
-84
lines changed

chapter4/newton-solver.ipynb

+52-52
Large diffs are not rendered by default.

chapter4/newton-solver.py

+32-32
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# extension: .py
77
# format_name: light
88
# format_version: '1.5'
9-
# jupytext_version: 1.14.7
9+
# jupytext_version: 1.15.2
1010
# kernelspec:
1111
# display_name: Python 3 (ipykernel)
1212
# language: python
@@ -20,20 +20,20 @@
2020
#
2121
# Given a function $F:\mathbb{R}^M\mapsto \mathbb{R}^M$, we have that $u_k, u_{k+1}\in \mathbb{R}^M$ is related as:
2222
#
23-
# $$x_{k+1} = x_{k} - J_F(x_k)^{-1} F(x_k)$$
23+
# $$u_{k+1} = u_{k} - J_F(u_k)^{-1} F(u_k)$$
2424
#
2525
# where $J_F$ is the Jacobian matrix of $F$.
2626
#
27-
# We can rewrite this equation as $\delta x_k = x_{k+1} - x_{k}$,
27+
# We can rewrite this equation as $\delta u_k = u_{k+1} - u_{k}$,
2828
#
2929
# $$
30-
# J_F(x_k)\delta x_k = - F(x_k)
30+
# J_F(u_k)\delta u_k = - F(u_k)
3131
# $$
3232
#
3333
# and
3434
#
3535
# $$
36-
# x_{k+1} = x_k + \delta x_k.
36+
# u_{k+1} = u_k + \delta u_k.
3737
# $$
3838

3939
# ## Problem specification
@@ -95,11 +95,11 @@ def root_1(x):
9595
A = dolfinx.fem.petsc.create_matrix(jacobian)
9696
L = dolfinx.fem.petsc.create_vector(residual)
9797

98-
# Next, we create the linear solver and the vector to hold `dx`.
98+
# Next, we create the linear solver and the vector to hold `du`.
9999

100100
solver = PETSc.KSP().create(mesh.comm)
101101
solver.setOperators(A)
102-
dx = dolfinx.fem.Function(V)
102+
du = dolfinx.fem.Function(V)
103103

104104
# We would like to monitor the evolution of `uh` for each iteration. Therefore, we get the dof coordinates, and sort them in increasing order.
105105

@@ -129,14 +129,14 @@ def root_1(x):
129129
L.ghostUpdate(addv=PETSc.InsertMode.INSERT_VALUES, mode=PETSc.ScatterMode.FORWARD)
130130

131131
# Solve linear problem
132-
solver.solve(L, dx.vector)
133-
dx.x.scatter_forward()
134-
# Update u_{i+1} = u_i + delta x_i
135-
uh.x.array[:] += dx.x.array
132+
solver.solve(L, du.vector)
133+
du.x.scatter_forward()
134+
# Update u_{i+1} = u_i + delta u_i
135+
uh.x.array[:] += du.x.array
136136
i += 1
137137

138138
# Compute norm of update
139-
correction_norm = dx.vector.norm(0)
139+
correction_norm = du.vector.norm(0)
140140
print(f"Iteration {i}: Correction norm {correction_norm}")
141141
if correction_norm < 1e-10:
142142
break
@@ -213,29 +213,29 @@ def u_exact(x):
213213
jacobian = dolfinx.fem.form(J)
214214

215215
# -
216-
# Next, we define the matrix `A`, right hand side vector `L` and the correction function `dx`
216+
# Next, we define the matrix `A`, right hand side vector `L` and the correction function `du`
217217

218-
dx = dolfinx.fem.Function(V)
218+
du = dolfinx.fem.Function(V)
219219
A = dolfinx.fem.petsc.create_matrix(jacobian)
220220
L = dolfinx.fem.petsc.create_vector(residual)
221221
solver = PETSc.KSP().create(mesh.comm)
222222
solver.setOperators(A)
223223

224-
# As we for this problem has strong Dirichlet conditions, we need to apply lifting to the right hand side of our Newton problem.
224+
# Since this problem has strong Dirichlet conditions, we need to apply lifting to the right hand side of our Newton problem.
225225
# We previously had that we wanted to solve the system:
226226
#
227227
# $$
228228
# \begin{align}
229-
# J_F(x_k)\delta x_k &= - F(x_k)\\
230-
# x_{k+1} &= x_k + \delta x_k
229+
# J_F(u_k)\delta u_k &= - F(u_k)\\
230+
# u_{k+1} &= u_k + \delta u_k
231231
# \end{align}
232232
# $$
233233
#
234-
# we want $x_{k+1}\vert_{bc}= u_D$. However, we do not know if $x_k\vert_{bc}=u_D$.
235-
# Therefore, we want to apply the following boundary condition for our correction $\delta x_k$
234+
# we want $u_{k+1}\vert_{bc}= u_D$. However, we do not know if $u_k\vert_{bc}=u_D$.
235+
# Therefore, we want to apply the following boundary condition for our correction $\delta u_k$
236236
#
237237
# $$
238-
# \delta x_k\vert_{bc} = u_D-x_k\vert_{bc}
238+
# \delta u_k\vert_{bc} = u_D-u_k\vert_{bc}
239239
# $$
240240
#
241241
# We therefore arrive at the following Newton scheme
@@ -244,7 +244,7 @@ def u_exact(x):
244244
i = 0
245245
error = dolfinx.fem.form(ufl.inner(uh - u_ufl, uh - u_ufl) * ufl.dx(metadata={"quadrature_degree": 4}))
246246
L2_error = []
247-
dx_norm = []
247+
du_norm = []
248248
while i < max_iterations:
249249
# Assemble Jacobian and residual
250250
with L.localForm() as loc_L:
@@ -258,30 +258,30 @@ def u_exact(x):
258258

259259
# Compute b - J(u_D-u_(i-1))
260260
dolfinx.fem.petsc.apply_lifting(L, [jacobian], [[bc]], x0=[uh.vector], scale=1)
261-
# Set dx|_bc = u_{i-1}-u_D
261+
# Set du|_bc = u_{i-1}-u_D
262262
dolfinx.fem.petsc.set_bc(L, [bc], uh.vector, 1.0)
263263
L.ghostUpdate(addv=PETSc.InsertMode.INSERT_VALUES, mode=PETSc.ScatterMode.FORWARD)
264264

265265
# Solve linear problem
266-
solver.solve(L, dx.vector)
267-
dx.x.scatter_forward()
266+
solver.solve(L, du.vector)
267+
du.x.scatter_forward()
268268

269-
# Update u_{i+1} = u_i + delta x_i
270-
uh.x.array[:] += dx.x.array
269+
# Update u_{i+1} = u_i + delta u_i
270+
uh.x.array[:] += du.x.array
271271
i += 1
272272

273273
# Compute norm of update
274-
correction_norm = dx.vector.norm(0)
274+
correction_norm = du.vector.norm(0)
275275

276276
# Compute L2 error comparing to the analytical solution
277277
L2_error.append(np.sqrt(mesh.comm.allreduce(dolfinx.fem.assemble_scalar(error), op=MPI.SUM)))
278-
dx_norm.append(correction_norm)
278+
du_norm.append(correction_norm)
279279

280280
print(f"Iteration {i}: Correction norm {correction_norm}, L2 error: {L2_error[-1]}")
281281
if correction_norm < 1e-10:
282282
break
283283

284-
# We plot the $L^2$-error and the residual norm ($\delta x$) per iteration
284+
# We plot the $L^2$-error and the residual norm ($\delta u$) per iteration
285285

286286
fig = plt.figure(figsize=(15, 8))
287287
plt.subplot(121)
@@ -293,12 +293,12 @@ def u_exact(x):
293293
plt.ylabel(r"$L^2$-error")
294294
plt.grid()
295295
plt.subplot(122)
296-
plt.title(r"Residual of $\vert\vert\delta x_i\vert\vert$")
297-
plt.plot(np.arange(i), dx_norm)
296+
plt.title(r"Residual of $\vert\vert\delta u_i\vert\vert$")
297+
plt.plot(np.arange(i), du_norm)
298298
ax = plt.gca()
299299
ax.set_yscale('log')
300300
plt.xlabel("Iterations")
301-
plt.ylabel(r"$\vert\vert \delta x\vert\vert$")
301+
plt.ylabel(r"$\vert\vert \delta u\vert\vert$")
302302
plt.grid()
303303

304304
# We compute the max error and plot the solution

0 commit comments

Comments
 (0)