Skip to content

Commit

Permalink
Merge pull request #166 from sblauth/hotfix/mesh_quality_tolerance
Browse files Browse the repository at this point in the history
Fix a bug where the mesh quality was not checked when importing the mesh
  • Loading branch information
sblauth authored Jan 25, 2023
2 parents b1f5e54 + bb22eef commit 02fb57e
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions cashocs/geometry/mesh_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ def _remove_gmsh_parametrizations(mesh_file: str) -> None:
fenics.MPI.barrier(fenics.MPI.comm_world)


def check_mesh_quality_tolerance(mesh_quality: float, tolerance: float) -> None:
"""Compares the current mesh quality with the (upper) tolerance.
This function raises an appropriate exception, when the mesh quality is not
sufficiently high.
Args:
mesh_quality: The current mesh quality.
tolerance: The upper mesh quality tolerance.
"""
if mesh_quality < tolerance:
fail_msg = (
"The quality of the mesh file you have specified is not "
"sufficient for computing the shape gradient.\n "
+ f"It currently is {mesh_quality:.3e} but has to "
f"be at least {tolerance:.3e}."
)
raise _exceptions.InputError(
"cashocs.geometry.import_mesh", "input_arg", fail_msg
)


class _MeshHandler:
"""Handles the mesh for shape optimization problems.
Expand Down Expand Up @@ -142,6 +165,10 @@ def __init__(
self.mesh, self.mesh_quality_type, self.mesh_quality_measure
)

check_mesh_quality_tolerance(
self.current_mesh_quality, self.mesh_quality_tol_upper
)

self.options_frobenius: _typing.KspOption = {
"ksp_type": "preonly",
"pc_type": "jacobi",
Expand Down Expand Up @@ -630,27 +657,4 @@ def _check_imported_mesh_quality(self, solver: OptimizationAlgorithm) -> None:
mesh_quality_measure,
)

failed = False
fail_msg = None
if current_mesh_quality < mesh_quality_tol_lower:
failed = True
fail_msg = (
"The quality of the mesh file you have specified is not "
"sufficient for evaluating the cost functional.\n"
f"It currently is {current_mesh_quality:.3e} but has to "
f"be at least {mesh_quality_tol_lower:.3e}."
)

if current_mesh_quality < mesh_quality_tol_upper:
failed = True
fail_msg = (
"The quality of the mesh file you have specified is not "
"sufficient for computing the shape gradient.\n "
+ f"It currently is {current_mesh_quality:.3e} but has to "
f"be at least {mesh_quality_tol_lower:.3e}."
)

if failed:
raise _exceptions.InputError(
"cashocs.geometry.import_mesh", "input_arg", fail_msg
)
check_mesh_quality_tolerance(current_mesh_quality, mesh_quality_tol_upper)

0 comments on commit 02fb57e

Please sign in to comment.