Skip to content

Commit

Permalink
Addressing some issue raised by @stefanozampini
Browse files Browse the repository at this point in the history
Signed-off-by: Umberto Zerbinati <[email protected]>
  • Loading branch information
Umberto Zerbinati committed Jul 1, 2024
1 parent bf5088e commit 0d032b5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
7 changes: 3 additions & 4 deletions ngsPETSc/ksp.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class KrylovSolver():
"""
def __init__(self, a, dofsDescr, p=None, nullspace=None, optionsPrefix="",

Check failure on line 164 in ngsPETSc/ksp.py

View workflow job for this annotation

GitHub Actions / lint

W0102

ngsPETSc/ksp.py:164:4: W0102 Dangerous default value {} as argument

Check failure on line 164 in ngsPETSc/ksp.py

View workflow job for this annotation

GitHub Actions / lint

W0102

ngsPETSc/ksp.py:164:4: W0102 Dangerous default value {} as argument
solverParameters=None):
solverParameters={}):
# Grabbing dofs information
if isinstance(dofsDescr, FESpace):
freeDofs = dofsDescr.FreeDofs()
Expand Down Expand Up @@ -198,9 +198,8 @@ def __init__(self, a, dofsDescr, p=None, nullspace=None, optionsPrefix="",
self.mapping = VectorMapping((dofs,freeDofs,{"bsize":entrysize}))
#Fixing PETSc options
options_object = PETSc.Options()
if solverParameters is not None:
for optName, optValue in solverParameters.items():
options_object[optName] = optValue
for optName, optValue in solverParameters.items():
options_object[optName] = optValue

#Setting PETSc Options
pscA.setOptionsPrefix(optionsPrefix)
Expand Down
7 changes: 3 additions & 4 deletions ngsPETSc/snes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class NonLinearSolver:
this fuction is used only if the argument a is None.
'''
def __init__(self, fes, a=None, residual=None, objective=None, jacobian=None,

Check failure on line 31 in ngsPETSc/snes.py

View workflow job for this annotation

GitHub Actions / lint

W0102

ngsPETSc/snes.py:31:4: W0102 Dangerous default value {} as argument

Check failure on line 31 in ngsPETSc/snes.py

View workflow job for this annotation

GitHub Actions / lint

W0102

ngsPETSc/snes.py:31:4: W0102 Dangerous default value {} as argument
solverParameters=None, optionsPrefix=None):
solverParameters={}, optionsPrefix=""):
self.fes = fes
dofs = fes.ParallelDofs()
self.second_order = False
Expand All @@ -39,9 +39,8 @@ def __init__(self, fes, a=None, residual=None, objective=None, jacobian=None,
self.snes = PETSc.SNES().create(comm=dofs.comm.mpi4py)
#Setting up the options
options_object = PETSc.Options()
if solverParameters is not None:
for optName, optValue in solverParameters.items():
options_object[optName] = optValue
for optName, optValue in solverParameters.items():
options_object[optName] = optValue
self.snes.setOptionsPrefix(optionsPrefix)
self.snes.setFromOptions()
#Setting up utility for mappings
Expand Down
43 changes: 26 additions & 17 deletions ngsPETSc/ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,40 @@ class TimeStepper:
mass matrix for explicit time-stepping schemes and compute the
Jacobian using the variational form F.
"""
def __init__(self, fes, info, G=None, F=None, residual=None,
jacobian=None, solverParameters=None, optionsPrefix=None):
def __init__(self, fes, variationalInfo, timeInfo, G=None, F=None, residual=None,

Check failure on line 16 in ngsPETSc/ts.py

View workflow job for this annotation

GitHub Actions / lint

W0102

ngsPETSc/ts.py:16:4: W0102 Dangerous default value {} as argument

Check failure on line 16 in ngsPETSc/ts.py

View workflow job for this annotation

GitHub Actions / lint

W0102

ngsPETSc/ts.py:16:4: W0102 Dangerous default value {} as argument
jacobian=None, solverParameters={}, optionsPrefix=""):
self.fes = fes
dofs = fes.ParallelDofs()
self.second_order = False
self.trial = info[0]
self.t = info[1]
self.set = 0
if isinstance(timeInfo, (list,tuple)) and len(timeInfo) == 3:
self.trial, self.t = variationalInfo
elif isinstance(timeInfo, dict):
self.trial = timeInfo["trial"]
self.t = timeInfo["t"]
else:
raise ValueError("variationalInfo must be a list/tuple or a dict")
if isinstance(timeInfo, (list,tuple)) and len(timeInfo) == 3:
self.t0, self.tf, self.dt = timeInfo
elif isinstance(timeInfo, dict):
self.t0 = timeInfo["t0"]
self.tf = timeInfo["tf"]
self.dt = timeInfo["dt"]
else:
raise ValueError("timeInfo must be a list/tuple or a dict")
self.F = F
self.G = G
if "ngs_jacobian_mat_type" not in solverParameters:
solverParameters["ngs_jacobian_mat_type"] = "aij"
jacobianMatType = solverParameters["ngs_jacobian_mat_type"]
self.ts = PETSc.TS().create(comm=dofs.comm.mpi4py)
#Deafult TS setup
self.ts.setExactFinalTime(3)
self.ts.setExactFinalTime(PETSc.TS.ExactFinalTime.MATCHSTEP)
self.ts.setMaxSNESFailures(-1)
#Setting up the options
options_object = PETSc.Options()
if solverParameters is not None:
for optName, optValue in solverParameters.items():
options_object[optName] = optValue
for optName, optValue in solverParameters.items():
options_object[optName] = optValue
self.ts.setOptionsPrefix(optionsPrefix)
self.ts.setFromOptions()
#Setting up utility for mappings
Expand Down Expand Up @@ -76,18 +89,11 @@ def jacobian(x, t, xdot): #pylint: disable=E0102,E0213
self.jacobianMatType = jacobianMatType
else:
raise ValueError("You need to provide a jacobian function or a variational form F.")
def setup(self, timeInfo):
self.set = 1
def setup(self):
'''
This is method is used to setup the PETSc TS object
'''
if isinstance(timeInfo, (list,tuple)) and len(timeInfo) == 3:
self.t0, self.tf, self.dt = timeInfo
elif isinstance(timeInfo, dict):
self.t0 = timeInfo["t0"]
self.tf = timeInfo["tf"]
self.dt = timeInfo["dt"]
else:
raise ValueError("timeInfo must be a list/tuple or a dict")
ngsGridFucntion = GridFunction(self.fes)
pIVec = self.vectorMapping.petscVec(ngsGridFucntion.vec)
pEVec = self.vectorMapping.petscVec(ngsGridFucntion.vec)
Expand All @@ -102,12 +108,15 @@ def setup(self, timeInfo):
self.ts.setTimeStep(self.dt)
self.ts.setMaxTime(self.tf)
self.ts.setMaxSteps(int((self.tf-self.t0)/self.dt))
self.set = 2

def solve(self, x0):
"""
This method is used to solve the time-stepping problem
:arg x0: initial data as an NGSolve grid function
"""
if self.set == 1:
self.setup()
pscx0 = self.vectorMapping.petscVec(x0.vec)
self.ts.solve(pscx0)
self.vectorMapping.ngsVec(pscx0, ngsVec=x0.vec)
Expand Down

0 comments on commit 0d032b5

Please sign in to comment.