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

Option for raising warning if not converged #92

Merged
merged 5 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 54 additions & 21 deletions movement/monge_ampere.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ def l2_projector(self):
bbc = None # Periodic case
else:
warn(
"Have you checked that all straight line segments are uniquely tagged?"
"Have you checked that all straight line segments are uniquely"
" tagged?"
)
corners = [(i, j) for i in edges for j in edges.difference([i])]
bbc = firedrake.DirichletBC(self.P1_vec, 0, corners)
Expand Down Expand Up @@ -358,13 +359,30 @@ def equidistributor(self):
return self._equidistributor

@PETSc.Log.EventDecorator()
def move(self):
"""
def move(self, raise_errors=True):
r"""
Run the relaxation method to convergence and update the mesh.

:kwarg raise_errors: convergence error handling behaviour: if `True` then
:class:`~.ConvergenceError`\s are raised, else warnings are raised and the
program is allowed to continue
:kwarg raise_errors: :class:`bool`
:return: the iteration count
:rtype: :class:`int`
"""
if not raise_errors:
warn(
f"{type(self)}.move called with raise_errors=False. Beware: this option"
" can produce poor quality meshes!"
)

def report_conv_err(msg):
if raise_errors:
raise firedrake.ConvergenceError(msg)
else:
raise Warning(msg)

# Take iterations of the relaxed system until reaching convergence
for i in range(self.maxiter):
self.l2_projector.solve()
self._update_coordinates()
Expand Down Expand Up @@ -393,19 +411,17 @@ def move(self):
PETSc.Sys.Print(f"Converged in {i+1} iteration{plural}.")
break
if residual > self.dtol * initial_norm:
raise firedrake.ConvergenceError(
f"Diverged after {i+1} iteration{plural}."
)
report_conv_err(f"Diverged after {i+1} iteration{plural}.")
if i == self.maxiter - 1:
raise firedrake.ConvergenceError(
f"Failed to converge in {i+1} iteration{plural}."
)
report_conv_err(f"Failed to converge in {i+1} iteration{plural}.")

# Apply pseudotimestepper and equidistributor
self.pseudotimestepper.solve()
self.equidistributor.solve()
self.phi_old.assign(self.phi)
self.sigma_old.assign(self.sigma)

# Update mesh coordinates accordingly
self._update_coordinates()
return i

Expand Down Expand Up @@ -551,23 +567,40 @@ def monitor(snes, i, rnorm):
return self._equidistributor

@PETSc.Log.EventDecorator()
def move(self):
"""
def move(self, raise_errors=True):
r"""
Run the quasi-Newton method to convergence and update the mesh.

:kwarg raise_errors: convergence error handling behaviour: if `True` then
:class:`~.ConvergenceError`\s are raised, else warnings are raised and the
program is allowed to continue
:kwarg raise_errors: :class:`bool`
:return: the iteration count
:rtype: :class:`int`
"""
if not raise_errors:
warn(
f"{type(self)}.move called with raise_errors=False. Beware: this option"
" can produce poor quality meshes!"
)

def count_iterations():
i = self.snes.getIterationNumber()
return f"{i} iteration{'s' if i != 1 else ''}"

# Solve equidistribution problem, handling convergence errors according to
# desired behaviour
try:
self.equidistributor.solve()
i = self.snes.getIterationNumber()
plural = "s" if i != 1 else ""
PETSc.Sys.Print(f"Converged in {i} iteration{plural}.")
except firedrake.ConvergenceError:
i = self.snes.getIterationNumber()
plural = "s" if i != 1 else ""
raise firedrake.ConvergenceError(
f"Failed to converge in {i} iteration{plural}."
)
PETSc.Sys.Print(f"Converged in {count_iterations()}.")
except firedrake.ConvergenceError as conv_err:
if raise_errors:
raise firedrake.ConvergenceError(
f"Failed to converge in {count_iterations()}."
) from conv_err
else:
warn(f"Failed to converge in {count_iterations()}.")

# Update mesh coordinates accordingly
self._update_coordinates()
return i
return self.snes.getIterationNumber()
10 changes: 6 additions & 4 deletions test/test_monge_ampere.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,16 @@ def test_maxiter_convergenceerror(self, method):
mover.move()
self.assertEqual(str(cm.exception), "Failed to converge in 1 iteration.")

def test_divergence_convergenceerror(self):
@parameterized.expand([(True,), (False,)])
def test_divergence_convergenceerror(self, raise_errors):
"""
Test that the mesh mover raises a :class:`~.ConvergenceError` if it diverges.
Test that divergence of the mesh mover raises a :class:`~.ConvergenceError` if
`raise_errors=True` and a :class:`~.Warning` otherwise.
"""
mesh = self.mesh(2, n=4)
mover = MongeAmpereMover_Relaxation(mesh, ring_monitor, dtol=1.0e-08)
with self.assertRaises(ConvergenceError) as cm:
mover.move()
with self.assertRaises(ConvergenceError if raise_errors else Warning) as cm:
mover.move(raise_errors=raise_errors)
self.assertEqual(str(cm.exception), "Diverged after 1 iteration.")

def test_initial_guess_valueerror(self):
Expand Down
Loading