Dear All,
I solved very simple eigenvalue in dolfin and dolfinx. I need to use complex build of slepc in dolfinx for future projects. Hence, I am switching over. I have a dolfin code which gives the eigenvectors I want;
import dolfin as dolf
from math import pi
import numpy as np
from slepc4py import SLEPc
mesh = dolf.UnitIntervalMesh(5)
dx = dolf.Measure('dx', domain=mesh)
V = dolf.FunctionSpace(mesh, 'CG', 1)
u = dolf.TrialFunction(V)
v = dolf.TestFunction(V)
a_ = dolf.dot(dolf.grad(u), dolf.grad(v)) * dx
c_ = u * v * dx
A = dolf.PETScMatrix()
dolf.assemble(a_, tensor=A)
A = dolf.as_backend_type(A).mat()
C = dolf.PETScMatrix()
dolf.assemble(c_, tensor=C)
C = dolf.as_backend_type(C).mat()
E = SLEPc.EPS().create()
E.setOperators(A, C)
E.setTarget(np.pi)
E.setWhichEigenpairs(SLEPc.EPS.Which.TARGET_MAGNITUDE) # TARGET_REAL or TARGET_IMAGINARY
E.setFromOptions()
E.solve()
vr, vi = A.getVecs()
omega = E.getEigenpair(1, vr, vi)
print(omega,vr[:],vi[:])
Output of this code is;
(10.198390006583926+0j)
[ 0.53452248 0.43243777 0.16517653 -0.16517653 -0.43243777 -0.53452248]
[0. 0. 0. 0. 0. 0.]
This is expected output since we have real eigenvalue and then zero imaginary part of eigenvector.
And I have the code which does the same thing in dolfinx with complex PETSc and SLEPc;
from dolfinx import Function, FunctionSpace, UnitIntervalMesh
from dolfinx.fem.assemble import assemble_matrix
from mpi4py import MPI
from ufl import Measure, FacetNormal, TestFunction, TrialFunction, dx, grad, inner
import numpy as np
from slepc4py import SLEPc
mesh = UnitIntervalMesh(MPI.COMM_WORLD, 5)
dx = Measure('dx', domain=mesh)
V = FunctionSpace(mesh, ('CG', 1))
u = TrialFunction(V)
v = TestFunction(V)
a = inner(grad(u), grad(v)) * dx
c = inner(u,v) * dx
A = assemble_matrix(a)
A.assemble()
C = assemble_matrix(c)
C.assemble()
E = SLEPc.EPS().create()
E.setOperators(A, C)
E.setTarget(np.pi)
E.setWhichEigenpairs(SLEPc.EPS.Which.TARGET_MAGNITUDE) # TARGET_REAL or TARGET_IMAGINARY
E.setFromOptions()
E.solve()
vr, vi = A.getVecs()
omega = E.getEigenpair(1, vr, vi)
print(omega,vr[:],vi[:])
However, dolfinx script gives eigenvector values which has nonzero complex parts in its entries even if it has same eigenvalue with dolfin script;
(10.19839000658394-2.090963493473814e-15j)
[ 0.22892832+0.36687089j 0.28297096+0.45347736j 0.08744284+0.14013221j
-0.08744284-0.14013221j -0.22892832-0.36687089j -0.28297096-0.45347736j]
[0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]
I cannot figure out why they give different eigenvectors? Is there anybody who can say what is going on under the hood for complex SLEPc and PETSc builds?
Thanks in advance.