Skip to content

Commit a86ec74

Browse files
authored
Change from vector to petsc_vec (#206)
1 parent f00b05d commit a86ec74

12 files changed

+38
-38
lines changed

chapter2/diffusion_code.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@
222222
"source": [
223223
"## Updating the solution and right hand side per time step\n",
224224
"To be able to solve the variation problem at each time step, we have to assemble the right hand side and apply the boundary condition before calling\n",
225-
"`solver.solve(b, uh.vector)`. We start by resetting the values in `b` as we are reusing the vector at every time step.\n",
225+
"`solver.solve(b, uh.x.petsc_vec)`. We start by resetting the values in `b` as we are reusing the vector at every time step.\n",
226226
"The next step is to assemble the vector calling `dolfinx.fem.petsc.assemble_vector(b, L)`, which means that we are assembling the linear form `L(v)` into the vector `b`. Note that we do not supply the boundary conditions for assembly, as opposed to the left hand side.\n",
227227
"This is because we want to use lifting to apply the boundary condition, which preserves symmetry of the matrix $A$ in the bilinear form $a(u,v)=a(v,u)$ without Dirichlet boundary conditions.\n",
228228
"When we have applied the boundary condition, we can solve the linear system and update values that are potentially shared between processors.\n",
@@ -249,7 +249,7 @@
249249
" set_bc(b, [bc])\n",
250250
"\n",
251251
" # Solve linear problem\n",
252-
" solver.solve(b, uh.vector)\n",
252+
" solver.solve(b, uh.x.petsc_vec)\n",
253253
" uh.x.scatter_forward()\n",
254254
"\n",
255255
" # Update solution at previous time step (u_n)\n",

chapter2/diffusion_code.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def initial_condition(x, a=5):
142142

143143
# ## Updating the solution and right hand side per time step
144144
# To be able to solve the variation problem at each time step, we have to assemble the right hand side and apply the boundary condition before calling
145-
# `solver.solve(b, uh.vector)`. We start by resetting the values in `b` as we are reusing the vector at every time step.
145+
# `solver.solve(b, uh.x.petsc_vec)`. We start by resetting the values in `b` as we are reusing the vector at every time step.
146146
# The next step is to assemble the vector calling `dolfinx.fem.petsc.assemble_vector(b, L)`, which means that we are assembling the linear form `L(v)` into the vector `b`. Note that we do not supply the boundary conditions for assembly, as opposed to the left hand side.
147147
# This is because we want to use lifting to apply the boundary condition, which preserves symmetry of the matrix $A$ in the bilinear form $a(u,v)=a(v,u)$ without Dirichlet boundary conditions.
148148
# When we have applied the boundary condition, we can solve the linear system and update values that are potentially shared between processors.
@@ -162,7 +162,7 @@ def initial_condition(x, a=5):
162162
set_bc(b, [bc])
163163

164164
# Solve linear problem
165-
solver.solve(b, uh.vector)
165+
solver.solve(b, uh.x.petsc_vec)
166166
uh.x.scatter_forward()
167167

168168
# Update solution at previous time step (u_n)

chapter2/heat_code.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242
" set_bc(b, [bc])\n",
243243
"\n",
244244
" # Solve linear problem\n",
245-
" solver.solve(b, uh.vector)\n",
245+
" solver.solve(b, uh.x.petsc_vec)\n",
246246
" uh.x.scatter_forward()\n",
247247
"\n",
248248
" # Update solution at previous time step (u_n)\n",

chapter2/heat_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def __call__(self, x):
134134
set_bc(b, [bc])
135135

136136
# Solve linear problem
137-
solver.solve(b, uh.vector)
137+
solver.solve(b, uh.x.petsc_vec)
138138
uh.x.scatter_forward()
139139

140140
# Update solution at previous time step (u_n)

chapter2/linearelasticity_code.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@
314314
}
315315
],
316316
"source": [
317-
"warped.cell_data[\"VonMises\"] = stresses.vector.array\n",
317+
"warped.cell_data[\"VonMises\"] = stresses.x.petsc_vec.array\n",
318318
"warped.set_active_scalars(\"VonMises\")\n",
319319
"p = pyvista.Plotter()\n",
320320
"p.add_mesh(warped)\n",

chapter2/linearelasticity_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def sigma(u):
157157

158158
# In the previous sections, we have only visualized first order Lagrangian functions. However, the Von Mises stresses are piecewise constant on each cell. Therefore, we modify our plotting routine slightly. The first thing we notice is that we now set values for each cell, which has a one to one correspondence with the degrees of freedom in the function space.
159159

160-
warped.cell_data["VonMises"] = stresses.vector.array
160+
warped.cell_data["VonMises"] = stresses.x.petsc_vec.array
161161
warped.set_active_scalars("VonMises")
162162
p = pyvista.Plotter()
163163
p.add_mesh(warped)

chapter2/ns_code1.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@
494494
" apply_lifting(b1, [a1], [bcu])\n",
495495
" b1.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)\n",
496496
" set_bc(b1, bcu)\n",
497-
" solver1.solve(b1, u_.vector)\n",
497+
" solver1.solve(b1, u_.x.petsc_vec)\n",
498498
" u_.x.scatter_forward()\n",
499499
"\n",
500500
" # Step 2: Pressure corrrection step\n",
@@ -504,15 +504,15 @@
504504
" apply_lifting(b2, [a2], [bcp])\n",
505505
" b2.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)\n",
506506
" set_bc(b2, bcp)\n",
507-
" solver2.solve(b2, p_.vector)\n",
507+
" solver2.solve(b2, p_.x.petsc_vec)\n",
508508
" p_.x.scatter_forward()\n",
509509
"\n",
510510
" # Step 3: Velocity correction step\n",
511511
" with b3.localForm() as loc_3:\n",
512512
" loc_3.set(0)\n",
513513
" assemble_vector(b3, L3)\n",
514514
" b3.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)\n",
515-
" solver3.solve(b3, u_.vector)\n",
515+
" solver3.solve(b3, u_.x.petsc_vec)\n",
516516
" u_.x.scatter_forward()\n",
517517
" # Update variable with solution form this time step\n",
518518
" u_n.x.array[:] = u_.x.array[:]\n",
@@ -524,7 +524,7 @@
524524
"\n",
525525
" # Compute error at current time-step\n",
526526
" error_L2 = np.sqrt(mesh.comm.allreduce(assemble_scalar(L2_error), op=MPI.SUM))\n",
527-
" error_max = mesh.comm.allreduce(np.max(u_.vector.array - u_ex.vector.array), op=MPI.MAX)\n",
527+
" error_max = mesh.comm.allreduce(np.max(u_.x.petsc_vec.array - u_ex.x.petsc_vec.array), op=MPI.MAX)\n",
528528
" # Print error only every 20th step and at the last step\n",
529529
" if (i % 20 == 0) or (i == num_steps - 1):\n",
530530
" print(f\"Time {t:.2f}, L2-error {error_L2:.2e}, Max error {error_max:.2e}\")\n",

chapter2/ns_code1.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def u_exact(x):
283283
apply_lifting(b1, [a1], [bcu])
284284
b1.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)
285285
set_bc(b1, bcu)
286-
solver1.solve(b1, u_.vector)
286+
solver1.solve(b1, u_.x.petsc_vec)
287287
u_.x.scatter_forward()
288288

289289
# Step 2: Pressure corrrection step
@@ -293,15 +293,15 @@ def u_exact(x):
293293
apply_lifting(b2, [a2], [bcp])
294294
b2.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)
295295
set_bc(b2, bcp)
296-
solver2.solve(b2, p_.vector)
296+
solver2.solve(b2, p_.x.petsc_vec)
297297
p_.x.scatter_forward()
298298

299299
# Step 3: Velocity correction step
300300
with b3.localForm() as loc_3:
301301
loc_3.set(0)
302302
assemble_vector(b3, L3)
303303
b3.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)
304-
solver3.solve(b3, u_.vector)
304+
solver3.solve(b3, u_.x.petsc_vec)
305305
u_.x.scatter_forward()
306306
# Update variable with solution form this time step
307307
u_n.x.array[:] = u_.x.array[:]
@@ -313,7 +313,7 @@ def u_exact(x):
313313

314314
# Compute error at current time-step
315315
error_L2 = np.sqrt(mesh.comm.allreduce(assemble_scalar(L2_error), op=MPI.SUM))
316-
error_max = mesh.comm.allreduce(np.max(u_.vector.array - u_ex.vector.array), op=MPI.MAX)
316+
error_max = mesh.comm.allreduce(np.max(u_.x.petsc_vec.array - u_ex.x.petsc_vec.array), op=MPI.MAX)
317317
# Print error only every 20th step and at the last step
318318
if (i % 20 == 0) or (i == num_steps - 1):
319319
print(f"Time {t:.2f}, L2-error {error_L2:.2e}, Max error {error_max:.2e}")

chapter2/ns_code2.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@
655655
" apply_lifting(b1, [a1], [bcu])\n",
656656
" b1.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)\n",
657657
" set_bc(b1, bcu)\n",
658-
" solver1.solve(b1, u_s.vector)\n",
658+
" solver1.solve(b1, u_s.x.petsc_vec)\n",
659659
" u_s.x.scatter_forward()\n",
660660
"\n",
661661
" # Step 2: Pressure corrrection step\n",
@@ -665,26 +665,26 @@
665665
" apply_lifting(b2, [a2], [bcp])\n",
666666
" b2.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)\n",
667667
" set_bc(b2, bcp)\n",
668-
" solver2.solve(b2, phi.vector)\n",
668+
" solver2.solve(b2, phi.x.petsc_vec)\n",
669669
" phi.x.scatter_forward()\n",
670670
"\n",
671-
" p_.vector.axpy(1, phi.vector)\n",
671+
" p_.x.petsc_vec.axpy(1, phi.x.petsc_vec)\n",
672672
" p_.x.scatter_forward()\n",
673673
"\n",
674674
" # Step 3: Velocity correction step\n",
675675
" with b3.localForm() as loc:\n",
676676
" loc.set(0)\n",
677677
" assemble_vector(b3, L3)\n",
678678
" b3.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)\n",
679-
" solver3.solve(b3, u_.vector)\n",
679+
" solver3.solve(b3, u_.x.petsc_vec)\n",
680680
" u_.x.scatter_forward()\n",
681681
"\n",
682682
" # Write solutions to file\n",
683683
" vtx_u.write(t)\n",
684684
" vtx_p.write(t)\n",
685685
"\n",
686686
" # Update variable with solution form this time step\n",
687-
" with u_.vector.localForm() as loc_, u_n.vector.localForm() as loc_n, u_n1.vector.localForm() as loc_n1:\n",
687+
" with u_.x.petsc_vec.localForm() as loc_, u_n.x.petsc_vec.localForm() as loc_n, u_n1.x.petsc_vec.localForm() as loc_n1:\n",
688688
" loc_n.copy(loc_n1)\n",
689689
" loc_.copy(loc_n)\n",
690690
"\n",

chapter2/ns_code2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def __call__(self, x):
418418
apply_lifting(b1, [a1], [bcu])
419419
b1.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)
420420
set_bc(b1, bcu)
421-
solver1.solve(b1, u_s.vector)
421+
solver1.solve(b1, u_s.x.petsc_vec)
422422
u_s.x.scatter_forward()
423423

424424
# Step 2: Pressure corrrection step
@@ -428,26 +428,26 @@ def __call__(self, x):
428428
apply_lifting(b2, [a2], [bcp])
429429
b2.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)
430430
set_bc(b2, bcp)
431-
solver2.solve(b2, phi.vector)
431+
solver2.solve(b2, phi.x.petsc_vec)
432432
phi.x.scatter_forward()
433433

434-
p_.vector.axpy(1, phi.vector)
434+
p_.x.petsc_vec.axpy(1, phi.x.petsc_vec)
435435
p_.x.scatter_forward()
436436

437437
# Step 3: Velocity correction step
438438
with b3.localForm() as loc:
439439
loc.set(0)
440440
assemble_vector(b3, L3)
441441
b3.ghostUpdate(addv=PETSc.InsertMode.ADD_VALUES, mode=PETSc.ScatterMode.REVERSE)
442-
solver3.solve(b3, u_.vector)
442+
solver3.solve(b3, u_.x.petsc_vec)
443443
u_.x.scatter_forward()
444444

445445
# Write solutions to file
446446
vtx_u.write(t)
447447
vtx_p.write(t)
448448

449449
# Update variable with solution form this time step
450-
with u_.vector.localForm() as loc_, u_n.vector.localForm() as loc_n, u_n1.vector.localForm() as loc_n1:
450+
with u_.x.petsc_vec.localForm() as loc_, u_n.x.petsc_vec.localForm() as loc_n, u_n1.x.petsc_vec.localForm() as loc_n1:
451451
loc_n.copy(loc_n1)
452452
loc_.copy(loc_n)
453453

chapter4/newton-solver.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,14 @@
258258
" L.ghostUpdate(addv=PETSc.InsertMode.INSERT_VALUES, mode=PETSc.ScatterMode.FORWARD)\n",
259259
"\n",
260260
" # Solve linear problem\n",
261-
" solver.solve(L, du.vector)\n",
261+
" solver.solve(L, du.x.petsc_vec)\n",
262262
" du.x.scatter_forward()\n",
263263
" # Update u_{i+1} = u_i + delta u_i\n",
264264
" uh.x.array[:] += du.x.array\n",
265265
" i += 1\n",
266266
"\n",
267267
" # Compute norm of update\n",
268-
" correction_norm = du.vector.norm(0)\n",
268+
" correction_norm = du.x.petsc_vec.norm(0)\n",
269269
" print(f\"Iteration {i}: Correction norm {correction_norm}\")\n",
270270
" if correction_norm < 1e-10:\n",
271271
" break\n",
@@ -516,21 +516,21 @@
516516
" L.scale(-1)\n",
517517
"\n",
518518
" # Compute b - J(u_D-u_(i-1))\n",
519-
" dolfinx.fem.petsc.apply_lifting(L, [jacobian], [[bc]], x0=[uh.vector], scale=1)\n",
519+
" dolfinx.fem.petsc.apply_lifting(L, [jacobian], [[bc]], x0=[uh.x.petsc_vec], scale=1)\n",
520520
" # Set du|_bc = u_{i-1}-u_D\n",
521-
" dolfinx.fem.petsc.set_bc(L, [bc], uh.vector, 1.0)\n",
521+
" dolfinx.fem.petsc.set_bc(L, [bc], uh.x.petsc_vec, 1.0)\n",
522522
" L.ghostUpdate(addv=PETSc.InsertMode.INSERT_VALUES, mode=PETSc.ScatterMode.FORWARD)\n",
523523
"\n",
524524
" # Solve linear problem\n",
525-
" solver.solve(L, du.vector)\n",
525+
" solver.solve(L, du.x.petsc_vec)\n",
526526
" du.x.scatter_forward()\n",
527527
"\n",
528528
" # Update u_{i+1} = u_i + delta u_i\n",
529529
" uh.x.array[:] += du.x.array\n",
530530
" i += 1\n",
531531
"\n",
532532
" # Compute norm of update\n",
533-
" correction_norm = du.vector.norm(0)\n",
533+
" correction_norm = du.x.petsc_vec.norm(0)\n",
534534
"\n",
535535
" # Compute L2 error comparing to the analytical solution\n",
536536
" L2_error.append(np.sqrt(mesh.comm.allreduce(dolfinx.fem.assemble_scalar(error), op=MPI.SUM)))\n",

chapter4/newton-solver.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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, du.vector)
132+
solver.solve(L, du.x.petsc_vec)
133133
du.x.scatter_forward()
134134
# Update u_{i+1} = u_i + delta u_i
135135
uh.x.array[:] += du.x.array
136136
i += 1
137137

138138
# Compute norm of update
139-
correction_norm = du.vector.norm(0)
139+
correction_norm = du.x.petsc_vec.norm(0)
140140
print(f"Iteration {i}: Correction norm {correction_norm}")
141141
if correction_norm < 1e-10:
142142
break
@@ -257,21 +257,21 @@ def u_exact(x):
257257
L.scale(-1)
258258

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

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

269269
# Update u_{i+1} = u_i + delta u_i
270270
uh.x.array[:] += du.x.array
271271
i += 1
272272

273273
# Compute norm of update
274-
correction_norm = du.vector.norm(0)
274+
correction_norm = du.x.petsc_vec.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)))

0 commit comments

Comments
 (0)