Hello David,

Thanks for the idea.  In your example you had your a matrix and then m matrix.  I presume the second is the mass matrix.

In my case I do have a and then m.  I suppose that even if I had it mixed up, I would get the inverse of the eigenvalue. 

In case anyone has any ideas I am including two lines.

The first is my firedrake code which does not work.

https://github.com/francispoulin/firedrakeQG/blob/master/basin_modes_qg.py

The second is a fenics version of the code which does work.

https://github.com/francispoulin/firedrakeQG/blob/master/basin_modes_qg_fenics.py

To my non-expert eyes they seem to be doing the same thing but clearly they are not.

Any suggestions would be greatly appreciated.

If I can get this working I would be happy to put together an eigenvalues demo, if people might find this interesting, so that others can benefit from my pitfalls.

Cheers, Francis


------------------
Francis Poulin                    
Associate Professor
Department of Applied Mathematics
University of Waterloo

email:           fpoulin@uwaterloo.ca
Web:            https://uwaterloo.ca/poulin-research-group/
Telephone:  +1 519 888 4567 x32637


From: firedrake-bounces@imperial.ac.uk [firedrake-bounces@imperial.ac.uk] on behalf of David Bernstein [David.Bernstein@meliorinnovations.com]
Sent: Wednesday, November 30, 2016 3:50 PM
To: firedrake@imperial.ac.uk
Subject: Re: [firedrake] eigenvalue problems with SLEPc

Ok, I don’t know your problem but is it possible that you have the mass and stiffness matrices switched? It looks like you have defined “a” to be the mass matrix and “m” the stiffness. In that case they should be switched in the setOperators line.



Also in my code I apply 
On Nov 30, 2016, at 11:30 AM, Francis Poulin <fpoulin@uwaterloo.ca> wrote:

Hello David,

Thanks again!

I modified your code an think I have set it up correctly.  

One issue is that you define your petsc_a/m using M.handle.  I don't know what that means but I presume I don't change it.

Also, since I only integrate by parts on the mass matrix I impose those Dirichlet BCs on that matrix.  If I impose it on both I get an eigenvalue of 1. Not sure why but there is probably a good reason to explain that.

The good news is that it runs, the bad news is that it does not converge to any eigenvalues. 

Besides that I like the setup.  Seems generic enough that one can apply it to a variety of problems.

If you have any thoughts I'd be curios to hear them.

Francis

# Set up Eigenvalue Problem: QG Basin modes
a =  beta*phi*psi.dx(0)*dx 
m = -inner(grad(psi), grad(phi))*dx - F*psi*phi*dx

petsc_a = assemble(a).M.handle
petsc_m = assemble(m, bcs=bc).M.handle

# Create solver
es = SLEPc.EPS().create(comm=COMM_WORLD)
es.ProblemType.GHEP                                 # Generalized eigenvalue problem
es.setWhichEigenpairs(es.Which.SMALLEST_IMAGINARY)  # type of eigenvalues to find
num_eigenvalues = 1                                 # Number of eigenvalues
es.setDimensions(num_eigenvalues)
es.setOperators(petsc_a, petsc_m)                   # Build Operators
es.solve()                                          # solve system

nconv = es.getConverged()
print nconv



------------------
Francis Poulin                    
Associate Professor
Department of Applied Mathematics
University of Waterloo

email:           fpoulin@uwaterloo.ca
Web:            https://uwaterloo.ca/poulin-research-group/
Telephone:  +1 519 888 4567 x32637


From: firedrake-bounces@imperial.ac.uk [firedrake-bounces@imperial.ac.uk] on behalf of David Bernstein [David.Bernstein@meliorinnovations.com]
Sent: Wednesday, November 30, 2016 2:03 PM
To: firedrake@imperial.ac.uk
Subject: Re: [firedrake] eigenvalue problems with SLEPc

Sorry, insert last line “where a is …” below second line.

On Nov 30, 2016, at 11:01 AM, David Bernstein <david.bernstein@meliorinnovations.com> wrote:

The way I do it in my code is

petsc_a = assemble(a, bcs=bcs).M.handle

I have both standard and generalized problems, for the generalized problem do

es.ProblemType.GHEP)

and for standard:

es.ProblemType.HEP)

(HEP = hermitian eigenvalue problem, the actual problem type depends on your form and bcs of course)

Yes, for the generalized problem the matrices are set by

es.setOperators(petsc_a, petsc_m)

where petsc_a and petsc_m are the stiffness and mass matrices respectively.

To solve:
es.solve()

I am not entirely sure what the line

vr, vi = petsc_a.getVecs()

does other than return some initialized vectors in the right format. I copied and pasted it from somewhere, it seems to be the right thing but I have no idea why!

Dave

where a is a Firedrake form and bcs is a list of Dirichlet conditions
On Nov 30, 2016, at 10:50 AM, Francis Poulin <fpoulin@uwaterloo.ca> wrote:

Hello David,

Yes, that is very helpful.  Thanks for getting back to me.  That is very helpful!

I am tried to copy what you've done, see below.   But I do have a few questions.

1) Do you have a topetsc function defined like what I have at the top?

2) When you define the petsc matrix, petsc_a, how did you define that?

3) I guess you are solving a regular eigenvalue problem, or is it a generalized eigenvalue problem? If it's in Finite elements i presume it's generalized?

In the case of a generalized eigenvalue problem I wonder if I should have

es.setOperators(A,M)

where A and M are petsc matrices.

4) If yes, then when you have 

vr, vi = petsc_A.getVec

I guess this is solving the eigenvalue problem but should you give it an A and M?

Sorry to bother you but I feel like I'm close, just missing an important detail.

Cheers, Francis

def topetsc(A): 
    A.force_evaluation()
    return A.petscmat

# Set up Eigenvalue Problem: QG Basin modes
a =  beta*phi*psi.dx(0)*dx 
m = -inner(grad(psi), grad(phi))*dx - F*psi*phi*dx

# Assemble Weak Form into a PETSc Matrix
# Impose Boundary Conditions on Mass Matrix
A = topetsc(assemble(a))
M = topetsc(assemble(m, bcs=[bc]))

# Create solver
es = SLEPc.EPS().create(comm=COMM_WORLD)

# Set type of eigenvalues
es.setWhichEigenpairs(es.Which.SMALLEST_IMAGINARY)

# Set number of eigenvalues to compute
num_eigenvalues = 5
es.setDimensions(num_eigenvalues)

#setting the matrix (petsc_a is a PETSc matrix)
es.setOperators(petsc_a)

# Fin eigenvalues an eigenvectors
vr, vi = petsc_a.getVecs()
for i in range(num_eigenvalues):
    lam = es.getEigenpair(i, vr, vi)
    eigenvalue[i] = lam.real
    eigenfunction[i].vector()[:] = vr





------------------
Francis Poulin                    
Associate Professor
Department of Applied Mathematics
University of Waterloo

email:           fpoulin@uwaterloo.ca
Web:            https://uwaterloo.ca/poulin-research-group/
Telephone:  +1 519 888 4567 x32637


From: firedrake-bounces@imperial.ac.uk [firedrake-bounces@imperial.ac.uk] on behalf of David Bernstein [David.Bernstein@meliorinnovations.com]
Sent: Wednesday, November 30, 2016 1:26 PM
To: firedrake@imperial.ac.uk
Subject: Re: [firedrake] eigenvalue problems with SLEPc

Hi Francis, I’m working on a project involving a SLEPc eigensolver in Firedrake so I’ve done some of what you mentioned.

For instance: creating an eigensolver:

es = SLEPc.EPS().create(comm=COMM_WORLD)

setting type of eigenvalues:

es.setWhichEigenpairs(self.eigensolver.Which.SMALLEST_REAL)

setting number of eigenvalues to compute:

es.setDimensions(self.num_eigenvalues)

setting the matrix (petsc_a is a PETSc matrix)
es.setOperators(petsc_a)

getting eigenvectors (in my case the eigenvalues are all real, petsc_a is the matrix)

vr, vi = petsc_a.getVecs()
for i in range(num_eigenvalues):
lmbda = es.getEigenpair(i, vr, vi)
eigenvalue[i] = lmbda.real
eigenfunction[i].vector()[:] = vr

Here eigenfunction is an array of Firedrake functions in the right space that has already been allocated.

Hope that helps,
Dave


On Nov 30, 2016, at 2:02 AM, Lawrence Mitchell <lawrence.mitchell@imperial.ac.uk> wrote:

Dear Francis,

On 30/11/16 03:23, Francis Poulin wrote:
Hello,

A while ago someone pointed me towards test_slepc.py for an example
on how to solve an eigenvalue problem.  I have been using this as a
model to compute Quasi-Geostrophic basin modes in a closed basin.
We have a version working in FENicS and I am very keen to get it
working in Firedrake as well.

I have a good start, which can be found on github,

https://github.com/francispoulin/firedrakeQG/blob/master/basin_modes_qg.py

Unfortunately, I don't know how to  do a lot of things.  For
example specify number of eigenvalues to determine, specify a
target eigenvalue, what method to use, or even just look at the
eigenvector.

Doing this is strictly in the realm of SLEPc, rather than Firedrake
itself.  We're "just" providing the operators.  The SLEPc
documentation is therefore probably helpful:

http://slepc.upv.es/documentation/manual.htm

It describes everything in terms of the C API, but fortunately,
slepc4py has  somewhat useful documentation strings (type
help(some_slepc_function) in ipython).  The translation from C
functions to slepc4py functions is quite mechanical:

EPSFooBar(eps, ...)

translates to:

eps.fooBar(...)

and so forth.

Thanks,

Lawrence

©Melior Innovations Inc, 2016 all rights reserved. This e-mail is covered by the Electronic Communication Privacy Act, 18 U.S.C. Section 2510-2521 and may be legally privileged. If you have received this transmission in error, please notify the original sender immediately by return e-mail or by telephone at the telephone number provided above and delete/trash the original message from your system. Thank you for your assistance. The information contained herein is confidential information provided only for the use of the individual or entity for whom it was intended. If the reader of this message received it in error, is not the intended recipient, or if an attachment is made in error, the reader is hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited.
_______________________________________________
firedrake mailing list
firedrake@imperial.ac.uk
https://mailman.ic.ac.uk/mailman/listinfo/firedrake

©Melior Innovations Inc, 2016 all rights reserved. This e-mail is covered by the Electronic Communication Privacy Act, 18 U.S.C. Section 2510-2521 and may be legally privileged. If you have received this transmission in error, please notify the original sender immediately by return e-mail or by telephone at the telephone number provided above and delete/trash the original message from your system. Thank you for your assistance. The information contained herein is confidential information provided only for the use of the individual or entity for whom it was intended. If the reader of this message received it in error, is not the intended recipient, or if an attachment is made in error, the reader is hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. 
©Melior Innovations Inc, 2016 all rights reserved. This e-mail is covered by the Electronic Communication Privacy Act, 18 U.S.C. Section 2510-2521 and may be legally privileged. If you have received this transmission in error, please notify the original sender immediately by return e-mail or by telephone at the telephone number provided above and delete/trash the original message from your system. Thank you for your assistance. The information contained herein is confidential information provided only for the use of the individual or entity for whom it was intended. If the reader of this message received it in error, is not the intended recipient, or if an attachment is made in error, the reader is hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. _______________________________________________
firedrake mailing list
firedrake@imperial.ac.uk
https://mailman.ic.ac.uk/mailman/listinfo/firedrake


©Melior Innovations Inc, 2016 all rights reserved. This e-mail is covered by the Electronic Communication Privacy Act, 18 U.S.C. Section 2510-2521 and may be legally privileged. If you have received this transmission in error, please notify the original sender immediately by return e-mail or by telephone at the telephone number provided above and delete/trash the original message from your system. Thank you for your assistance. The information contained herein is confidential information provided only for the use of the individual or entity for whom it was intended. If the reader of this message received it in error, is not the intended recipient, or if an attachment is made in error, the reader is hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. 
©Melior Innovations Inc, 2016 all rights reserved. This e-mail is covered by the Electronic Communication Privacy Act, 18 U.S.C. Section 2510-2521 and may be legally privileged. If you have received this transmission in error, please notify the original sender immediately by return e-mail or by telephone at the telephone number provided above and delete/trash the original message from your system. Thank you for your assistance. The information contained herein is confidential information provided only for the use of the individual or entity for whom it was intended. If the reader of this message received it in error, is not the intended recipient, or if an attachment is made in error, the reader is hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited._______________________________________________
firedrake mailing list
firedrake@imperial.ac.uk
https://mailman.ic.ac.uk/mailman/listinfo/firedrake

©Melior Innovations Inc, 2016 all rights reserved. This e-mail is covered by the Electronic Communication Privacy Act, 18 U.S.C. Section 2510-2521 and may be legally privileged. If you have received this transmission in error, please notify the original sender immediately by return e-mail or by telephone at the telephone number provided above and delete/trash the original message from your system. Thank you for your assistance. The information contained herein is confidential information provided only for the use of the individual or entity for whom it was intended. If the reader of this message received it in error, is not the intended recipient, or if an attachment is made in error, the reader is hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited.