Stokes taylor hood example

Hi everyone, dolfinx has an stokes taylor hood example and I have a question to ask. It has a code to solve the problem:

(u, p) = ufl.TrialFunctions(W)
(v, q) = ufl.TestFunctions(W)
f = Function(W0)
a = (inner(grad(u), grad(v)) + inner(p, div(v)) + inner(div(u), q)) * dx
L = inner(f, v) * dx

# Assemble LHS matrix and RHS vector
A = dolfinx.fem.assemble_matrix(a, bcs)
A.assemble()
b = dolfinx.fem.assemble.assemble_vector(L)

dolfinx.fem.assemble.apply_lifting(b, [a], [bcs])
b.ghostUpdate(addv=PETSc.InsertMode.ADD, mode=PETSc.ScatterMode.REVERSE)

# Set Dirichlet boundary condition values in the RHS
dolfinx.fem.assemble.set_bc(b, bcs)

# Create and configure solver
ksp = PETSc.KSP().create(mesh.mpi_comm())
ksp.setOperators(A)
ksp.setType("preonly")
ksp.getPC().setType("lu")
ksp.getPC().setFactorSolverType("superlu_dist")

# Compute the solution
U = Function(W)
ksp.solve(b, U.vector)

I have tried to use “solve(a==L, U, bcs)” to solve the problem instead and it gave a different result. It confuses me on when to use solve function. Thank you.

First of all, you need to clarify which solution is correct. By just saying it gives a different result, which no measure in difference, there is not much one can do.

Secondly, it is not recommended to use the solve function (It will probably be removed or completly rewritten before a stable release).

Dear dokken, I computed the soluton norm for the two cases. The method used in the example gives 17.43540717471062, while the method “solve(a==L, U, bcs)” gives “inf”. Just now I tried to choose the same type with “solve(a==L, U, bcs=bcs, petsc_options={“ksp_type”: “preonly”, “pc_type”: “lu”})”, it also gives 17.43540717471062. It still brings up the question on which pc_type to choose to solve various problems of different types.

u = U.sub(0).collapse()
p = U.sub(1).collapse()
norm_u_3 = u.vector.norm()

As i said in the previous post, i advice you to use the petsc solver directly, and set the appropriate options for your problem. The solve command is just hiding all the options.

OK, thanks for your reply