@@ -201,7 +201,6 @@ function solve_poisson(N=100, da_refine=0; solver_opts...)
201201 # Set a constant nullspace on the matrix for Neumann BCs
202202 nullspace = LibPETSc. MatNullSpaceCreate (petsclib, MPI. COMM_WORLD, LibPETSc. PETSC_TRUE, 0 , LibPETSc. PetscVec[])
203203 LibPETSc. MatSetNullSpace (petsclib, jac, nullspace)
204- LibPETSc. MatNullSpaceDestroy (petsclib, nullspace)
205204 # Don't destroy nullspace here - let the matrix manage it
206205
207206 return 0
@@ -238,8 +237,6 @@ function solve_poisson(N=100, da_refine=0; solver_opts...)
238237 PETSc. assemble! (b_vec)
239238
240239 # Remove the nullspace from the RHS to make it consistent for the singular matrix
241- LibPETSc. VecAssemblyBegin (petsclib, b_vec)
242- LibPETSc. VecAssemblyEnd (petsclib, b_vec)
243240 nullspace = LibPETSc. MatNullSpaceCreate (petsclib, MPI. COMM_WORLD, LibPETSc. PETSC_TRUE, 0 , LibPETSc. PetscVec[])
244241 LibPETSc. MatNullSpaceRemove (petsclib, nullspace, b_vec)
245242 LibPETSc. MatNullSpaceDestroy (petsclib, nullspace)
@@ -264,18 +261,18 @@ function solve_poisson(N=100, da_refine=0; solver_opts...)
264261
265262 sol = PETSc. get_solution (ksp)
266263
267- # Get local solution array for analysis
264+ # Get local solution array for analysis and error computation
268265 sol2D = nothing
269- PETSc. withlocalarray! (sol; read= true ) do s
270- sol2D = copy (s)
271- sol2D = reshape (sol2D, Int64 (corners. size[1 ]), Int64 (corners. size[2 ]))
272- end
273-
274266 l2_error = 0.0
275267 PETSc. withlocalarray! (sol; read= true ) do s
276268 nx, ny = corners. size[1 : 2 ]
277269 s2D = reshape (s, Int64 (corners. size[1 ]), Int64 (corners. size[2 ]))
278270
271+ # Copy solution for return value
272+ sol2D = copy (s)
273+ sol2D = reshape (sol2D, Int64 (corners. size[1 ]), Int64 (corners. size[2 ]))
274+
275+ # Compute L2 error
279276 l2_local = 0.0
280277 sum_diff_local = 0.0
281278 count_local = 0
@@ -314,13 +311,11 @@ function solve_poisson(N=100, da_refine=0; solver_opts...)
314311 end
315312 l2_global = MPI. Allreduce (l2_local, MPI. SUM, comm)
316313 l2_error = sqrt (l2_global)
317-
318- if MPI. Comm_rank (comm) == 0
319- @printf (" L2 norm of error: %.6e\n " , l2_error)
320- end
321314 end
322315
323- # Clean up
316+ if MPI. Comm_rank (comm) == 0
317+ @printf (" L2 norm of error: %.6e\n " , l2_error)
318+ end
324319 # Note: When using KSP(da), the KSP takes ownership of the DM reference
325320 # so we only need to destroy the KSP, not the DA
326321 PETSc. destroy (ksp)
0 commit comments