|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +from matplotlib.collections import PolyCollection |
| 3 | + |
| 4 | + |
| 5 | +def plot_quadrangle_mesh(mesh, solps_data=None, ax=None): |
| 6 | + """ |
| 7 | + Plots the quadrangle mesh grid geometry to a matplotlib figure. |
| 8 | +
|
| 9 | + If solps_data is provided, it is used to colour the faces of the quadrangles in the mesh. |
| 10 | + If matplotlib axes are provided the collection is added to them them, |
| 11 | + otherwise a new figure and axes are created. |
| 12 | +
|
| 13 | + :param mesh: SOLPSMesh object |
| 14 | + :param solps_data: Data array defined on the SOLPS mesh (optional) |
| 15 | + :param ax: matplotlib axes (optional) |
| 16 | + :return: matplotlib axes |
| 17 | + """ |
| 18 | + if ax is None: |
| 19 | + _, ax = plt.subplots(constrained_layout=True) |
| 20 | + |
| 21 | + if solps_data is None: |
| 22 | + collection_mesh = create_quadrangle_polycollection(mesh, facecolor="none", edgecolor='b', linewidth=0.5) |
| 23 | + else: |
| 24 | + collection_mesh = create_quadrangle_polycollection(mesh, solps_data) |
| 25 | + ax.add_collection(collection_mesh) |
| 26 | + |
| 27 | + ax = _format_matplotlib_axes(ax, mesh) |
| 28 | + return ax |
| 29 | + |
| 30 | + |
| 31 | +def plot_triangle_mesh(mesh, solps_data=None, ax=None): |
| 32 | + """ |
| 33 | + Plots the triangle mesh grid geometry to a matplotlib figure. |
| 34 | +
|
| 35 | + If solps_data is provided, it is used to colour the faces of the triangles in the mesh. |
| 36 | + If matplotlib axes are provided the collection is added to them them, |
| 37 | + otherwise a new figure and axes are created. |
| 38 | +
|
| 39 | + :param mesh: SOLPSMesh object |
| 40 | + :param solps_data: Data array defined on the SOLPS mesh |
| 41 | + :param ax: matplotlib axes (optional) |
| 42 | + :return: matplotlib axes |
| 43 | + """ |
| 44 | + if ax is None: |
| 45 | + _, ax = plt.subplots(constrained_layout=True) |
| 46 | + |
| 47 | + if solps_data is None: |
| 48 | + collection_mesh = create_triangle_polycollection(mesh, facecolor="none", edgecolor='b', linewidth=0.5) |
| 49 | + else: |
| 50 | + collection_mesh = create_triangle_polycollection(mesh, solps_data) |
| 51 | + ax.add_collection(collection_mesh) |
| 52 | + |
| 53 | + ax = _format_matplotlib_axes(ax, mesh) |
| 54 | + return ax |
| 55 | + |
| 56 | + |
| 57 | +def create_quadrangle_polycollection(mesh, solps_data=None, **collection_kw): |
| 58 | + """ |
| 59 | + Creates a matplotlib PolyCollection object from the quadrangle mesh. |
| 60 | +
|
| 61 | + If solps_data is provided, it is used to colour the faces of the quadrangles in the mesh. |
| 62 | +
|
| 63 | + :param mesh: SOLPSMesh object |
| 64 | + :param solps_data: Data array defined on the SOLPS mesh |
| 65 | + :param collection_kw: Keyword arguments for the PolyCollection |
| 66 | + :return: matplotlib.collections.PolyCollection |
| 67 | + """ |
| 68 | + verts = mesh.vertex_coordinates[mesh.quadrangles] |
| 69 | + collection_mesh = PolyCollection(verts, **collection_kw) |
| 70 | + if solps_data is not None: |
| 71 | + collection_mesh.set_array(solps_data[mesh.quadrangle_to_grid_map[:, 0], mesh.quadrangle_to_grid_map[:, 1]]) |
| 72 | + return collection_mesh |
| 73 | + |
| 74 | + |
| 75 | +def create_triangle_polycollection(mesh, solps_data=None, **collection_kw): |
| 76 | + """ |
| 77 | + Creates a matplotlib PolyCollection object from the triangle mesh. |
| 78 | +
|
| 79 | + If solps_data is provided, it is used to colour the faces of the triangles in the mesh. |
| 80 | +
|
| 81 | + :param mesh: SOLPSMesh object |
| 82 | + :param solps_data: Data array defined on the SOLPS mesh |
| 83 | + :param collection_kw: Keyword arguments for the PolyCollection |
| 84 | + :return: matplotlib.collections.PolyCollection |
| 85 | + """ |
| 86 | + verts = mesh.vertex_coordinates[mesh.triangles] |
| 87 | + collection_mesh = PolyCollection(verts, **collection_kw) |
| 88 | + if solps_data is not None: |
| 89 | + collection_mesh.set_array(solps_data[mesh.triangle_to_grid_map[:, 0], mesh.triangle_to_grid_map[:, 1]]) |
| 90 | + return collection_mesh |
| 91 | + |
| 92 | + |
| 93 | +def _format_matplotlib_axes(ax, mesh=None): |
| 94 | + """ |
| 95 | + Formats the matplotlib axes for a SOLPS mesh plot. |
| 96 | +
|
| 97 | + Sets aspect and labels for the axes. |
| 98 | + If a SOLPSMesh object is provided, sets the limits of the axes to the mesh extent. |
| 99 | +
|
| 100 | + :param ax: matplotlib axes |
| 101 | + :param mesh: SOLPSMesh object (optional) |
| 102 | + :return: matplotlib axes |
| 103 | + """ |
| 104 | + ax.set_aspect(1) |
| 105 | + ax.set_xlabel("R [m]") |
| 106 | + ax.set_ylabel("z [m]") |
| 107 | + if mesh is not None: |
| 108 | + ax.set_xlim(mesh.mesh_extent["minr"], mesh.mesh_extent["maxr"]) |
| 109 | + ax.set_ylim(mesh.mesh_extent["minz"], mesh.mesh_extent["maxz"]) |
| 110 | + return ax |
0 commit comments