From c1e8103d2b34f65116108be5ed7c1ed8690f1824 Mon Sep 17 00:00:00 2001 From: Pierric Mora <101342078+pierricmora@users.noreply.github.com> Date: Thu, 8 Aug 2024 09:42:46 +0200 Subject: [PATCH 1/7] add checks of cell tag consistency in gmshio --- python/dolfinx/io/gmshio.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/python/dolfinx/io/gmshio.py b/python/dolfinx/io/gmshio.py index f804e8b0717..2a844fd0278 100644 --- a/python/dolfinx/io/gmshio.py +++ b/python/dolfinx/io/gmshio.py @@ -152,6 +152,9 @@ def extract_topology_and_markers(model, name: typing.Optional[str] = None): # Create marker array of length of number of tagged cells marker = np.full_like(entity_tags[0], tag) + # Keep track of entity tags to check tag consistency + entity_tags = entity_tags[0] + # Group element topology and markers of the same entity type entity_type = entity_types[0] if entity_type in topologies.keys(): @@ -161,8 +164,14 @@ def extract_topology_and_markers(model, name: typing.Optional[str] = None): topologies[entity_type]["cell_data"] = np.hstack( [topologies[entity_type]["cell_data"], marker] ) + topologies[entity_type]["entity_tags"] = np.hstack( + [topologies[entity_type]["entity_tags"], entity_tags] + ) else: - topologies[entity_type] = {"topology": topology, "cell_data": marker} + topologies[entity_type] = {"topology": topology, + "cell_data": marker, + "entity_tags": entity_tags, + } return topologies @@ -257,6 +266,20 @@ def model_to_mesh( # Sort elements by ascending dimension perm_sort = np.argsort(cell_dimensions) + # Check that all cells are tagged once + _d = model.getDimension() + assert _d in topologies.keys(), 'All cells are expected to be tagged once; none found' + _elementTypes, _elementTags, _nodeTags = model.mesh.getElements(dim=_d, tag=-1) + # assert only one type of elements + # assert len(_elementTypes) == 1 # NOTE: already checked in extract_topology_and_markers + nbcells = len(_elementTags[0]) + nbcells_tagged = len(topologies[_d]["entity_tags"]) + assert nbcells == nbcells_tagged, \ + f'All cells are expected to be tagged once; found: {nbcells_tagged}, expected: {nbcells}' + nbcells_tagged_once = len(np.unique(topologies[_d]['entity_tags'])) + assert nbcells_tagged == nbcells_tagged_once, \ + 'All cells are expected to be tagged once; found duplicates' + # Broadcast cell type data and geometric dimension cell_id = cell_information[perm_sort[-1]]["id"] tdim = cell_information[perm_sort[-1]]["dim"] From 35531da432553cc39f59527f2ef3aa1564872737 Mon Sep 17 00:00:00 2001 From: Pierric Mora <101342078+pierricmora@users.noreply.github.com> Date: Thu, 8 Aug 2024 11:42:06 +0200 Subject: [PATCH 2/7] RuntimeError rather than assert --- python/dolfinx/io/gmshio.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/python/dolfinx/io/gmshio.py b/python/dolfinx/io/gmshio.py index 2a844fd0278..f568f03a0c1 100644 --- a/python/dolfinx/io/gmshio.py +++ b/python/dolfinx/io/gmshio.py @@ -268,17 +268,20 @@ def model_to_mesh( # Check that all cells are tagged once _d = model.getDimension() - assert _d in topologies.keys(), 'All cells are expected to be tagged once; none found' + if not(_d in topologies.keys()): + raise RuntimeError('All cells are expected to be tagged once; none found') _elementTypes, _elementTags, _nodeTags = model.mesh.getElements(dim=_d, tag=-1) # assert only one type of elements # assert len(_elementTypes) == 1 # NOTE: already checked in extract_topology_and_markers nbcells = len(_elementTags[0]) nbcells_tagged = len(topologies[_d]["entity_tags"]) - assert nbcells == nbcells_tagged, \ - f'All cells are expected to be tagged once; found: {nbcells_tagged}, expected: {nbcells}' + if nbcells != nbcells_tagged: + e = f'All cells are expected to be tagged once; found: {nbcells_tagged}, expected: {nbcells}' + raise RuntimeError(e) nbcells_tagged_once = len(np.unique(topologies[_d]['entity_tags'])) - assert nbcells_tagged == nbcells_tagged_once, \ - 'All cells are expected to be tagged once; found duplicates' + if nbcells_tagged != nbcells_tagged_once: + e = 'All cells are expected to be tagged once; found duplicates' + raise RuntimeError(e) # Broadcast cell type data and geometric dimension cell_id = cell_information[perm_sort[-1]]["id"] From 97a20233bfc9ecdfb1e24a054939a5a8423a6431 Mon Sep 17 00:00:00 2001 From: Pierric Mora <101342078+pierricmora@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:08:31 +0200 Subject: [PATCH 3/7] E501 --- python/dolfinx/io/gmshio.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/python/dolfinx/io/gmshio.py b/python/dolfinx/io/gmshio.py index f568f03a0c1..14b6c5e6ec5 100644 --- a/python/dolfinx/io/gmshio.py +++ b/python/dolfinx/io/gmshio.py @@ -276,7 +276,10 @@ def model_to_mesh( nbcells = len(_elementTags[0]) nbcells_tagged = len(topologies[_d]["entity_tags"]) if nbcells != nbcells_tagged: - e = f'All cells are expected to be tagged once; found: {nbcells_tagged}, expected: {nbcells}' + e = ( + 'All cells are expected to be tagged once;' + f'found: {nbcells_tagged}, expected: {nbcells}' + ) raise RuntimeError(e) nbcells_tagged_once = len(np.unique(topologies[_d]['entity_tags'])) if nbcells_tagged != nbcells_tagged_once: From 37436e2c856729265de1890591417a7e517268ea Mon Sep 17 00:00:00 2001 From: Pierric Mora <101342078+pierricmora@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:23:34 +0200 Subject: [PATCH 4/7] E713 --- python/dolfinx/io/gmshio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/dolfinx/io/gmshio.py b/python/dolfinx/io/gmshio.py index 14b6c5e6ec5..6c0b7164c41 100644 --- a/python/dolfinx/io/gmshio.py +++ b/python/dolfinx/io/gmshio.py @@ -268,7 +268,7 @@ def model_to_mesh( # Check that all cells are tagged once _d = model.getDimension() - if not(_d in topologies.keys()): + if _d not in topologies.keys(): raise RuntimeError('All cells are expected to be tagged once; none found') _elementTypes, _elementTags, _nodeTags = model.mesh.getElements(dim=_d, tag=-1) # assert only one type of elements From 3dafc470895a9cf14e76bbe1feb41cca8a07acf4 Mon Sep 17 00:00:00 2001 From: Pierric Mora <101342078+pierricmora@users.noreply.github.com> Date: Tue, 24 Sep 2024 14:21:59 +0200 Subject: [PATCH 5/7] ruff --- python/dolfinx/io/gmshio.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/python/dolfinx/io/gmshio.py b/python/dolfinx/io/gmshio.py index 13771d2da48..47e783384e8 100644 --- a/python/dolfinx/io/gmshio.py +++ b/python/dolfinx/io/gmshio.py @@ -168,10 +168,11 @@ def extract_topology_and_markers(model, name: typing.Optional[str] = None): [topologies[entity_type]["entity_tags"], entity_tags] ) else: - topologies[entity_type] = {"topology": topology, - "cell_data": marker, - "entity_tags": entity_tags, - } + topologies[entity_type] = { + "topology": topology, + "cell_data": marker, + "entity_tags": entity_tags, + } return topologies @@ -269,7 +270,7 @@ def model_to_mesh( # Check that all cells are tagged once _d = model.getDimension() if _d not in topologies.keys(): - raise RuntimeError('All cells are expected to be tagged once; none found') + raise RuntimeError("All cells are expected to be tagged once; none found") _elementTypes, _elementTags, _nodeTags = model.mesh.getElements(dim=_d, tag=-1) # assert only one type of elements # assert len(_elementTypes) == 1 # NOTE: already checked in extract_topology_and_markers @@ -277,13 +278,13 @@ def model_to_mesh( nbcells_tagged = len(topologies[_d]["entity_tags"]) if nbcells != nbcells_tagged: e = ( - 'All cells are expected to be tagged once;' - f'found: {nbcells_tagged}, expected: {nbcells}' - ) + "All cells are expected to be tagged once;" + f"found: {nbcells_tagged}, expected: {nbcells}" + ) raise RuntimeError(e) - nbcells_tagged_once = len(np.unique(topologies[_d]['entity_tags'])) + nbcells_tagged_once = len(np.unique(topologies[_d]["entity_tags"])) if nbcells_tagged != nbcells_tagged_once: - e = 'All cells are expected to be tagged once; found duplicates' + e = "All cells are expected to be tagged once; found duplicates" raise RuntimeError(e) # Broadcast cell type data and geometric dimension From 4e8ad33708ec13c2db6002ed7dfebb82d856b23d Mon Sep 17 00:00:00 2001 From: Pierric Mora <101342078+pierricmora@users.noreply.github.com> Date: Tue, 24 Sep 2024 15:14:21 +0200 Subject: [PATCH 6/7] bug fix: read the appropriate key of topologies --- python/dolfinx/io/gmshio.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/python/dolfinx/io/gmshio.py b/python/dolfinx/io/gmshio.py index 47e783384e8..bcfb6cdbc87 100644 --- a/python/dolfinx/io/gmshio.py +++ b/python/dolfinx/io/gmshio.py @@ -269,20 +269,21 @@ def model_to_mesh( # Check that all cells are tagged once _d = model.getDimension() - if _d not in topologies.keys(): - raise RuntimeError("All cells are expected to be tagged once; none found") _elementTypes, _elementTags, _nodeTags = model.mesh.getElements(dim=_d, tag=-1) # assert only one type of elements # assert len(_elementTypes) == 1 # NOTE: already checked in extract_topology_and_markers + _elementType_dim = _elementType[0] + if _elementType_dim not in topologies.keys(): + raise RuntimeError("All cells are expected to be tagged once; none found") nbcells = len(_elementTags[0]) - nbcells_tagged = len(topologies[_d]["entity_tags"]) + nbcells_tagged = len(topologies[_elementType_dim]["entity_tags"]) if nbcells != nbcells_tagged: e = ( "All cells are expected to be tagged once;" f"found: {nbcells_tagged}, expected: {nbcells}" ) raise RuntimeError(e) - nbcells_tagged_once = len(np.unique(topologies[_d]["entity_tags"])) + nbcells_tagged_once = len(np.unique(topologies[_elementType_dim]["entity_tags"])) if nbcells_tagged != nbcells_tagged_once: e = "All cells are expected to be tagged once; found duplicates" raise RuntimeError(e) From 815ee7f39c82f5e5c825f10dca64cb71822e48cf Mon Sep 17 00:00:00 2001 From: Pierric Mora <101342078+pierricmora@users.noreply.github.com> Date: Tue, 24 Sep 2024 15:16:01 +0200 Subject: [PATCH 7/7] typo --- python/dolfinx/io/gmshio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/dolfinx/io/gmshio.py b/python/dolfinx/io/gmshio.py index bcfb6cdbc87..a0fe792ca9c 100644 --- a/python/dolfinx/io/gmshio.py +++ b/python/dolfinx/io/gmshio.py @@ -272,7 +272,7 @@ def model_to_mesh( _elementTypes, _elementTags, _nodeTags = model.mesh.getElements(dim=_d, tag=-1) # assert only one type of elements # assert len(_elementTypes) == 1 # NOTE: already checked in extract_topology_and_markers - _elementType_dim = _elementType[0] + _elementType_dim = _elementTypes[0] if _elementType_dim not in topologies.keys(): raise RuntimeError("All cells are expected to be tagged once; none found") nbcells = len(_elementTags[0])