diff --git a/src/ansys/mapdl/core/post.py b/src/ansys/mapdl/core/post.py index c802f32ba7..b2d4b596b2 100644 --- a/src/ansys/mapdl/core/post.py +++ b/src/ansys/mapdl/core/post.py @@ -351,7 +351,7 @@ def element_values(self, item, comp="", option="AVG") -> np.ndarray: Returns ------- numpy.ndarray - Numpy array containing the requested element values for ta + Numpy array containing the requested element values for a given item and component. Notes @@ -632,27 +632,63 @@ def _plot_cell_scalars(self, scalars, show_elem_numbering=False, **kwargs): surf = self._mapdl.mesh._surf - # as ``disp`` returns the result for all nodes, we need all node numbers + # as ``disp`` returns the result for all nodes/elems, we need all node/elem numbers # and to index to the output node numbers if hasattr(self._mapdl.mesh, "enum_all"): - enum = self._mapdl.mesh.enum + enum = self._mapdl.mesh.enum_all else: enum = self._all_enum - # it's possible that there are duplicated element numbers, - # therefore we need to get the unique values and a reverse index + ####################################################################### + # Bool operations + # =============== + # I'm going to explain this clearly because it can be confusing for the + # future developers (me). + # This explanation is based in scalars (`element_values`) NOT having the + # full elements (selected and not selected) size. + # + # First, it's possible that there are duplicated element numbers, + # in the surf object returned by Pyvista. + # Therefore we need to get the unique values and a reverse index, to + # later convert the MAPDL values to Pyvista values. uni, ridx = np.unique(surf["ansys_elem_num"], return_inverse=True) - mask = np.isin(enum, uni, assume_unique=True) - - if scalars.size != mask.size: - scalars = scalars[self.selected_elements] - scalars = scalars[mask][ridx] + # which means that, uni is the id of mapdl elements in the polydata + # object. These elements does not need to be in order, and there can be + # duplicated! + # Hence: + # uni[ridx] = surf["ansys_elem_num"] + # + # Let's notice that: + # * enum[self.selected_elements] is mapdl selected elements ids in MAPDL notation. + # + # Theoretical approach + # -------------------- + # The theoretical approach will be using an intermediate array of the + # size of the MAPDL total number of elements (we do not care about selected). + # + values = np.zeros(enum.shape) + # + # Then assign the MAPDL values for the selected element (scalars) + # + values[self.selected_elements] = scalars + # + # Because values are in order, but with python notation, then we can do: + # + surf_values = values[ + uni - 1 + ] # -1 to set MAPDL element indexing to python indexing + # + # Then to account for the original Pyvista object: + # + surf_values = surf_values[ridx] + # + ####################################################################### meshes = [ { "mesh": surf.copy(deep=False), # deep=False for ipyvtk-simple "scalar_bar_args": {"title": kwargs.pop("stitle", "")}, - "scalars": scalars, + "scalars": surf_values, } ] diff --git a/tests/test_post.py b/tests/test_post.py index fdbf24d702..387f662fa6 100644 --- a/tests/test_post.py +++ b/tests/test_post.py @@ -765,13 +765,44 @@ def test_plot_incomplete_element_selection(mapdl, contact_solve): mapdl.esel("S", "ELEM", "", 1, mapdl.mesh.n_elem // 2) assert mapdl.post_processing.plot_element_displacement() is None + mapdl.nsel("S", "NODE", "", 1, mapdl.mesh.n_elem // 2, 2) + assert mapdl.post_processing.plot_element_displacement() is None + + mapdl.nsel("S", "NODE", "", 5, mapdl.mesh.n_elem // 2, 2) + assert mapdl.post_processing.plot_element_displacement() is None + + mapdl.vsel("s", "", "", 1) + mapdl.eslv("s") + assert mapdl.post_processing.plot_element_displacement() is None + + mapdl.vsel("s", "", "", 2) + mapdl.eslv("s") + assert mapdl.post_processing.plot_element_displacement() is None + @requires("pyvista") -@pytest.mark.xfail(strict=False, reason="The image regression is failing. See #2435") -def test_plot_incomplete_nodal_selection(mapdl, contact_solve): +def test_plot_incomplete_nodal_selection(mapdl, contact_solve, verify_image_cache): + verify_image_cache.skip = True + mapdl.nsel("S", "NODE", "", 1, mapdl.mesh.n_node // 2) assert mapdl.post_processing.plot_nodal_displacement() is None + mapdl.nsel("S", "NODE", "", 1, mapdl.mesh.n_node // 2, 2) + assert mapdl.post_processing.plot_nodal_displacement() is None + + mapdl.nsel("S", "NODE", "", 5, mapdl.mesh.n_node // 2, 2) + assert mapdl.post_processing.plot_nodal_displacement() is None + + mapdl.vsel("s", "", "", 1) + mapdl.eslv("S") + mapdl.nsle("S") + assert mapdl.post_processing.plot_nodal_displacement() is None + + mapdl.vsel("s", "", "", 2) + mapdl.eslv("S") + mapdl.nsle("S") + assert mapdl.post_processing.plot_nodal_displacement() is None + @requires("pyvista") def test_general_plotter_returns(mapdl, static_solve, verify_image_cache):