Skip to content

Commit

Permalink
improve visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
simbilod committed Nov 25, 2024
1 parent 9c7fee3 commit dc6a1ea
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 42 deletions.
4 changes: 3 additions & 1 deletion docs/intro_gmsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@

# %%
mesh = meshio.read("physicals.msh")
plot2D(mesh)
plot2D(mesh, ignore_lines=True)

# %% [markdown]
# ## The sharp bits
Expand All @@ -167,3 +167,5 @@
# %% [markdown]
# ### Keeping track of integers
# Whenever entities are created / transformed (e.g. when healing interfaces), there can be reassignment of the integer tags used to label them. In the official tutorials, entity tags are re-identified based on some characteristic like bounding extent to later assign to physical groups.

# %%
95 changes: 54 additions & 41 deletions meshwell/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@
]


def plot2D(mesh, physicals=None, wireframe: bool = False, title: str | None = None):
def plot2D(
mesh,
physicals=None,
wireframe: bool = False,
title: str | None = None,
ignore_lines: bool = False,
):
# Create a plotly figure
fig = go.Figure()

Expand Down Expand Up @@ -82,6 +88,10 @@ def plot2D(mesh, physicals=None, wireframe: bool = False, title: str | None = No
# Get color for this group (cycle through colors if more groups than colors)
color = colors[i % len(colors)]

# Get group name for legend
group_name = ", ".join(id_to_name[group]) if group in id_to_name else "mesh"
first_triangle = True

# Plot each triangle in the group with larger points
for triangle in group_cells:
x = mesh.points[triangle, 0]
Expand All @@ -98,52 +108,55 @@ def plot2D(mesh, physicals=None, wireframe: bool = False, title: str | None = No
marker=dict(size=5) if wireframe else None,
fill="toself" if not wireframe else None,
fillcolor=color if not wireframe else None,
showlegend=False,
name=", ".join(id_to_name[group])
if "gmsh:physical" in mesh.cell_data_dict
else "mesh",
showlegend=first_triangle, # Only show legend for first triangle of group
name=group_name,
)
)
first_triangle = False

j = i
# Plot lines for each 1D physical group
for i, group in enumerate(physical_groups_1D):
i += j
# Skip if physicals specified and this group not in them
if physicals is not None and "gmsh:physical" in mesh.cell_data_dict:
if id_to_name[group] not in physicals:
continue
# Get cells for this physical group
if (
"gmsh:physical" in mesh.cell_data_dict
and "line" in mesh.cell_data_dict["gmsh:physical"]
):
group_cells = mesh.cells_dict["line"][
mesh.cell_data_dict["gmsh:physical"]["line"] == group
]
else:
group_cells = mesh.cells_dict["line"]

# Get color for this group (cycle through colors if more groups than colors)
color = colors[i % len(colors)]

# Plot each line in the group
for line in group_cells:
x = mesh.points[line, 0]
y = mesh.points[line, 1]
fig.add_trace(
go.Scatter(
x=x,
y=y,
mode="lines+markers" if wireframe else "lines",
line=dict(color=color),
marker=dict(size=5) if wireframe else None,
showlegend=False,
name=", ".join(id_to_name[group])
if "gmsh:physical" in mesh.cell_data_dict
else "mesh",
if not ignore_lines:
for i, group in enumerate(physical_groups_1D):
i += j
# Skip if physicals specified and this group not in them
if physicals is not None and "gmsh:physical" in mesh.cell_data_dict:
if id_to_name[group] not in physicals:
continue
# Get cells for this physical group
if (
"gmsh:physical" in mesh.cell_data_dict
and "line" in mesh.cell_data_dict["gmsh:physical"]
):
group_cells = mesh.cells_dict["line"][
mesh.cell_data_dict["gmsh:physical"]["line"] == group
]
else:
group_cells = mesh.cells_dict["line"]

# Get color for this group (cycle through colors if more groups than colors)
color = colors[i % len(colors)]

# Get group name for legend
group_name = ", ".join(id_to_name[group]) if group in id_to_name else "mesh"
first_line = True

# Plot each line in the group
for line in group_cells:
x = mesh.points[line, 0]
y = mesh.points[line, 1]
fig.add_trace(
go.Scatter(
x=x,
y=y,
mode="lines+markers" if wireframe else "lines",
line=dict(color=color),
marker=dict(size=5) if wireframe else None,
showlegend=first_line, # Only show legend for first line of group
name=group_name,
)
)
)
first_line = False

# Update layout with interactive features
fig.update_layout(
Expand Down

0 comments on commit dc6a1ea

Please sign in to comment.