6
6
# extension: .py
7
7
# format_name: light
8
8
# format_version: '1.5'
9
- # jupytext_version: 1.14.7
9
+ # jupytext_version: 1.15.2
10
10
# kernelspec:
11
11
# display_name: Python 3 (ipykernel)
12
12
# language: python
20
20
#
21
21
# 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:
22
22
#
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 )$$
24
24
#
25
25
# where $J_F$ is the Jacobian matrix of $F$.
26
26
#
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}$,
28
28
#
29
29
# $$
30
- # J_F(x_k )\delta x_k = - F(x_k )
30
+ # J_F(u_k )\delta u_k = - F(u_k )
31
31
# $$
32
32
#
33
33
# and
34
34
#
35
35
# $$
36
- # x_ {k+1} = x_k + \delta x_k .
36
+ # u_ {k+1} = u_k + \delta u_k .
37
37
# $$
38
38
39
39
# ## Problem specification
@@ -95,11 +95,11 @@ def root_1(x):
95
95
A = dolfinx .fem .petsc .create_matrix (jacobian )
96
96
L = dolfinx .fem .petsc .create_vector (residual )
97
97
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 `.
99
99
100
100
solver = PETSc .KSP ().create (mesh .comm )
101
101
solver .setOperators (A )
102
- dx = dolfinx .fem .Function (V )
102
+ du = dolfinx .fem .Function (V )
103
103
104
104
# We would like to monitor the evolution of `uh` for each iteration. Therefore, we get the dof coordinates, and sort them in increasing order.
105
105
@@ -129,14 +129,14 @@ def root_1(x):
129
129
L .ghostUpdate (addv = PETSc .InsertMode .INSERT_VALUES , mode = PETSc .ScatterMode .FORWARD )
130
130
131
131
# 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
136
136
i += 1
137
137
138
138
# Compute norm of update
139
- correction_norm = dx .vector .norm (0 )
139
+ correction_norm = du .vector .norm (0 )
140
140
print (f"Iteration { i } : Correction norm { correction_norm } " )
141
141
if correction_norm < 1e-10 :
142
142
break
@@ -213,29 +213,29 @@ def u_exact(x):
213
213
jacobian = dolfinx .fem .form (J )
214
214
215
215
# -
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 `
217
217
218
- dx = dolfinx .fem .Function (V )
218
+ du = dolfinx .fem .Function (V )
219
219
A = dolfinx .fem .petsc .create_matrix (jacobian )
220
220
L = dolfinx .fem .petsc .create_vector (residual )
221
221
solver = PETSc .KSP ().create (mesh .comm )
222
222
solver .setOperators (A )
223
223
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.
225
225
# We previously had that we wanted to solve the system:
226
226
#
227
227
# $$
228
228
# \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
231
231
# \end{align}
232
232
# $$
233
233
#
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 $
236
236
#
237
237
# $$
238
- # \delta x_k \vert_{bc} = u_D-x_k \vert_{bc}
238
+ # \delta u_k \vert_{bc} = u_D-u_k \vert_{bc}
239
239
# $$
240
240
#
241
241
# We therefore arrive at the following Newton scheme
@@ -244,7 +244,7 @@ def u_exact(x):
244
244
i = 0
245
245
error = dolfinx .fem .form (ufl .inner (uh - u_ufl , uh - u_ufl ) * ufl .dx (metadata = {"quadrature_degree" : 4 }))
246
246
L2_error = []
247
- dx_norm = []
247
+ du_norm = []
248
248
while i < max_iterations :
249
249
# Assemble Jacobian and residual
250
250
with L .localForm () as loc_L :
@@ -258,30 +258,30 @@ def u_exact(x):
258
258
259
259
# Compute b - J(u_D-u_(i-1))
260
260
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
262
262
dolfinx .fem .petsc .set_bc (L , [bc ], uh .vector , 1.0 )
263
263
L .ghostUpdate (addv = PETSc .InsertMode .INSERT_VALUES , mode = PETSc .ScatterMode .FORWARD )
264
264
265
265
# 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 ()
268
268
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
271
271
i += 1
272
272
273
273
# Compute norm of update
274
- correction_norm = dx .vector .norm (0 )
274
+ correction_norm = du .vector .norm (0 )
275
275
276
276
# Compute L2 error comparing to the analytical solution
277
277
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 )
279
279
280
280
print (f"Iteration { i } : Correction norm { correction_norm } , L2 error: { L2_error [- 1 ]} " )
281
281
if correction_norm < 1e-10 :
282
282
break
283
283
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
285
285
286
286
fig = plt .figure (figsize = (15 , 8 ))
287
287
plt .subplot (121 )
@@ -293,12 +293,12 @@ def u_exact(x):
293
293
plt .ylabel (r"$L^2$-error" )
294
294
plt .grid ()
295
295
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 )
298
298
ax = plt .gca ()
299
299
ax .set_yscale ('log' )
300
300
plt .xlabel ("Iterations" )
301
- plt .ylabel (r"$\vert\vert \delta x \vert\vert$" )
301
+ plt .ylabel (r"$\vert\vert \delta u \vert\vert$" )
302
302
plt .grid ()
303
303
304
304
# We compute the max error and plot the solution
0 commit comments