FEniCS / dolfinx

Next generation FEniCS problem solving environment
https://fenicsproject.org
GNU Lesser General Public License v3.0
757 stars 182 forks source link

A simple eigenvalue solver #1078

Closed K-Karo closed 4 years ago

K-Karo commented 4 years ago

Hi, I am trying to write a simple eigenvalue solver code in dolfin-x, but I have a problem in how to define a matrix as a PETScMatrix() and assembling the form into it. In the following, there is a problem in order to define an eigensolver.

There is a proposed code in dolfin which I am trying to write it in dolfin-x:

A = PETScMatrix() assemble(a, tensor=A) eigensolver = SLEPcEigenSolver(A) eigensolver.solve() r, c, rx, cx = eigensolver.get_eigenpair(0)

adeebkor commented 4 years ago

You can use dolfinx.fem.assemble_matrix() to get a PETSc matrix from the variational form. The PETSc matrix can then be directly use in the SLEPc eigensolver.

garth-wells commented 4 years ago

Following on the comment by @adeebkor , SLEPcEigenSolver has been removed in DOLFINX in favour of working directly with slepc4py.

K-Karo commented 4 years ago

dear @adeebkor and @garth-wells Thanks for your reply, I am trying to use your guidance, but it is steel confusing to me. This is a proposed code by using dolfin:

from dolfin import *
mesh = …….
V = FunctionSpace(mesh, "Lagrange", 1)
u = TrialFunction(V)
v = TestFunction(V)
a = dot(grad(u), grad(v))*dx

# Assemble stiffness form
A = PETScMatrix()                                                      
assemble(a, tensor=A)

# Create eigensolver
eigensolver = SLEPcEigenSolver(A)

# Compute all eigenvalues
eigensolver.solve()

# Extract largest (first) eigenpair
r, c, rx, cx = eigensolver.get_eigenpair(0)

print "Largest eigenvalue: ", r

Achieving the (r, c, rx, cx) values, help me to analyze waveguides (chapter 34 of FEniCS book). There is an ambiguity how to create eigensolver and compute the eigenvalues in DOLFIN-X, and how to use the PETSc matrix directly in the SLEPc eigensolver? Thanks in advance.

garth-wells commented 4 years ago

Please post your question to https://fenicsproject.discourse.group/.