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

Fix a bug where the mesh quality was not checked when importing the mesh #166

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Changes from all 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
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)