Tranpose a PETSc Matrix

Hi!
I’m a new user and I try to calculate adjoints modes of Linear Navier-Stokes equations.
This is the code to calculate the direct modes.

J = derivative(a,w)
JM = PETScMatrix()
waux=Function(W)

dummy = inner(Constant((1.0, 1.0)), v) * dx
assemble_system(J, dummy, bcs, A_tensor = JM)

eigensolver = SLEPcEigenSolver(JM)

#eigensolver.parameters[“spectrum”] = “target imaginary”
eigensolver.parameters[“spectral_transform”] = “shift-and-invert”
eigensolver.parameters[“spectral_shift”] = 50.0
N=50
eigensolver.solve(N)

I want the Adjoint matrix of J or the adjoint of JM but I don’t know. Can anyone help me?
Thanks!

To transpose a PETScMatrix, use petsc4py, the petsc python interface.
You can also transpose the form by taking the adjoint of it and assembling it. The two approaches are shown below

from dolfin import *
from petsc4py import PETSc
import numpy as np

# Create mesh an unsymmetric form
mesh = UnitSquareMesh(100,100)
V = FunctionSpace(mesh, "CG", 1)
u, v= TrialFunction(V), TestFunction(V)
vec = as_vector((1,2))
a = inner(u,v)*dx + u*inner(vec, grad(v))*dx + inner(u,v)*ds
b = inner(1, v)*dx

A = assemble(a)

# Using dolfin
a_adj = adjoint(a)
A_adj_dolfin = assemble(a_adj)
Aadj_dolfin_np = A_adj_dolfin.array()

A_np = A.array()
assert(not np.allclose(A_np, Aadj_dolfin_np))
assert(np.allclose(A_np, Aadj_dolfin_np.T))


# Using PETSC
B = A.copy()
petsc_mat = as_backend_type(B).mat()
petsc_mat.transpose()
A_adj_petsc = PETScMatrix(petsc_mat)
Aadj_petsc_np = A_adj_petsc.array()
assert(np.allclose(Aadj_petsc_np, Aadj_dolfin_np))
assert(not np.allclose(Aadj_petsc_np, A_np))
2 Likes

It works! Thank you so much!!