Skip to content

Commit

Permalink
Solver now can be an operator in matrix free form
Browse files Browse the repository at this point in the history
Signed-off-by: Umberto Zerbinati <[email protected]>
  • Loading branch information
Umberto Zerbinati committed Jun 19, 2024
1 parent 89846dd commit 578bf58
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions ngsPETSc/ksp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'''
from petsc4py import PETSc

from ngsolve import la, GridFunction
from ngsolve import la, GridFunction, BaseMatrix

from ngsPETSc import Matrix, VectorMapping

Expand Down Expand Up @@ -45,7 +45,7 @@ def __init__(self, a, fes, p=None, solverParameters=None, optionsPrefix=None, nu
for optName, optValue in solverParameters.items():
options_object[optName] = optValue

#Creating the PETSc Matrix
#Creating the PETSc Matrix
A = Matrix(Amat, fes).mat
A.setOptionsPrefix(optionsPrefix)
A.setFromOptions()
Expand Down Expand Up @@ -90,3 +90,44 @@ def view(self):
'''
self.ksp.view()

def asOperator(self, ngsMat):

Check failure on line 94 in ngsPETSc/ksp.py

View workflow job for this annotation

GitHub Actions / lint

C0116

ngsPETSc/ksp.py:94:4: C0116 Missing function or method docstring
return self.Operator(self.ksp, ngsMat)

class Operator(BaseMatrix):

Check failure on line 97 in ngsPETSc/ksp.py

View workflow job for this annotation

GitHub Actions / lint

C0115

ngsPETSc/ksp.py:97:4: C0115 Missing class docstring
def __init__(self, ksp, ngsMat):
self.ksp = ksp
self.ngsMat = ngsMat
def Shape(self):
'''
Shape of the BaseMatrix
'''
return self.ngsMat.shape

Check failure on line 106 in ngsPETSc/ksp.py

View workflow job for this annotation

GitHub Actions / lint

C0303

ngsPETSc/ksp.py:106:36: C0303 Trailing whitespace

def CreateVector(self,col):
'''
Create vector corresponding to the matrix
:arg col: True if one want a column vector
'''
return self.ngsMat.CreateVector(not col)

def Mult(self,x,y):
'''
BaseMatrix multiplication Ax = y
:arg x: vector we are multiplying
:arg y: vector we are storeing the result in
'''
self.ksp.solve(x,y)

def MultTrans(self,x,y):
'''
BaseMatrix multiplication A^T x = y
:arg x: vector we are multiplying
:arg y: vector we are storeing the result in
'''
raise NotImplementedError("Transpose multiplication not implemented")

0 comments on commit 578bf58

Please sign in to comment.