Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion to PETSc with complex #381

Merged
merged 71 commits into from
Mar 14, 2024
Merged

Conversion to PETSc with complex #381

merged 71 commits into from
Mar 14, 2024

Conversation

e-moral-sanchez
Copy link
Contributor

@e-moral-sanchez e-moral-sanchez commented Feb 27, 2024

Fix the functions to cast Psydac format to PETSc format and back:

  • Identify the correct installation procedure for PETSc and petsc4py;
  • Make functions work on complex-valued vectors and matrices;
  • Make functions work in the serial case (where comm=None);
  • Add more extensive unit testing;
  • Update unit test workflow;
  • Update README file.

This allows the use of high-performance PETSc solvers:

  • PETSc must be installed with the complex configuration (see the instructions in the README).
  • StencilVector, BlockVector, StencilMatrix and BlockLinearOperator have a topetsc() method.
  • The function petsc_to_psydac in psydac.linalg.utilities.py allows us to convert a PETSc.Vec object (for instance, the vector solution after solving a system with PETSc) back to Psydac StencilVector or BlockVector format.

How to use it:
Let A be a StencilMatrix or BlockLinearOperator and b a StencilVector. We want to solve Ax=b with PETSc CG:

from psydac.linalg.utilities import petsc_to_psydac
from petsc4py import PETSC

Ap = A.topetsc() #converts operator to PETSc.Mat format

bp = b.topetsc() #converts vector to PETSc.Vec format

x = bp.duplicate() #x will be both the initial guess and where the solution is stored.
# If we have a particular initial guess in Psydac format, just convert it to PETSc.Vec format x = x.topetsc()

ksp = PETSc.KSP() # create Krylov solver type object
ksp.create(comm=Ap.comm) # pass communicator from PETSc.Mat object
opts = PETSc.Options() # create object to pass options to solver
opts["ksp_type"] = "cg" # set type of solver ("gmres" is default)
opts["pc_type"] = "none" # set preconditioner
opts["ksp_rtol"] = 1e-13
opts["ksp_atol"] = 1e-13
ksp.setFromOptions() # pass options to solver ksp
ksp.setOperators(Ap) # set system matrix. Preconditioner can also be passed here

ksp.solve(bp, x) # solve system

sol = petsc_to_psydac(x, V.vector_space) # convert solution to Psydac StencilVector format

Note: With this PR, the conversion from PETSC format to StencilVector format is not efficient, see issue #389.

@e-moral-sanchez e-moral-sanchez changed the title Pet sc tests Conversion to PETSc with complex Feb 27, 2024
Copy link
Member

@yguclu yguclu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job!

@yguclu yguclu merged commit cdfb337 into devel Mar 14, 2024
6 checks passed
@yguclu yguclu deleted the PETSc_tests branch March 14, 2024 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants