From b4964d477777ea3ec3f2ceec951ec6e0cb11a342 Mon Sep 17 00:00:00 2001 From: germa89 <28149841+germa89@users.noreply.github.com> Date: Thu, 27 Nov 2025 12:46:39 +0100 Subject: [PATCH 1/6] refactor: moving tests to classes to speed up execution --- tests/test_plotting.py | 1793 ++++++++++++++++++++-------------------- 1 file changed, 917 insertions(+), 876 deletions(-) diff --git a/tests/test_plotting.py b/tests/test_plotting.py index d486246e73a..fb5c6b5ccd1 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -45,72 +45,6 @@ ALL_LABELS.extend(DISPL_LABELS) -@pytest.fixture -def boundary_conditions_example(mapdl, cleared): - mapdl.et("", 189) - - mapdl.n(1, 0, 0, 0) - mapdl.n(2, 1, 0, 0) - mapdl.n(3, 2, 0, 0) - mapdl.n(4, 0, 0, 1) - mapdl.n(5, 1, 0, 1) - mapdl.n(6, 2, 0, 1) - - mapdl.nsel("s", "node", "", 1) - mapdl.f("all", "FX", 0) - mapdl.nsel("s", "node", "", 2) - mapdl.f("all", "FY", 0) - mapdl.nsel("s", "node", "", 3) - mapdl.f("all", "FZ", 0) - - mapdl.nsel("s", "node", "", 4) - mapdl.d("all", "UX", 0) - mapdl.nsel("s", "node", "", 5) - mapdl.d("all", "UY", 0) - mapdl.nsel("s", "node", "", 6) - mapdl.d("all", "UZ", 0) - - mapdl.n(7, 0, 1, 0) - mapdl.n(8, 1, 1, 0) - mapdl.n(9, 2, 1, 0) - mapdl.n(10, 0, 2, 0) - mapdl.n(11, 1, 2, 0) - mapdl.n(12, 2, 2, 0) - - mapdl.nsel("s", "node", "", 7) - mapdl.f("all", "FX", 10) - mapdl.nsel("s", "node", "", 8) - mapdl.f("all", "FY", 20) - mapdl.nsel("s", "node", "", 9) - mapdl.f("all", "FZ", 30) - - mapdl.nsel("s", "node", "", 10) - mapdl.d("all", "UX", 20) - mapdl.nsel("s", "node", "", 11) - mapdl.d("all", "UY", 20) - mapdl.nsel("s", "node", "", 12) - mapdl.d("all", "UZ", 20) - - mapdl.nsel("all") - - -@pytest.fixture -def block_example_coupled(mapdl, cleared): - mapdl.et(1, 226) - mapdl.keyopt(1, 1, 1011) # Thermal-Piezoelectric - - # Disp - # UX, UY, UZ, - # TEMP, VOLT - - # Force - # FX, FY, FZ, - # HEAT, CHRG - mapdl.n(1, 0, 0, 0) - mapdl.n(2, 1, 0, 0) - mapdl.n(3, 2, 0, 0) - - def check_geometry(mapdl, function): prev_knum = mapdl.geometry.knum prev_lnum = mapdl.geometry.lnum @@ -148,6 +82,923 @@ def check_geometry(mapdl, function): return out +class TestPlottingBoundaryConditions: + + @staticmethod + @pytest.fixture(scope="class") + def boundary_conditions_example(mapdl): + from conftest import clear + + clear(mapdl) + + mapdl.et("", 189) + + mapdl.n(1, 0, 0, 0) + mapdl.n(2, 1, 0, 0) + mapdl.n(3, 2, 0, 0) + mapdl.n(4, 0, 0, 1) + mapdl.n(5, 1, 0, 1) + mapdl.n(6, 2, 0, 1) + + mapdl.nsel("s", "node", "", 1) + mapdl.f("all", "FX", 0) + mapdl.nsel("s", "node", "", 2) + mapdl.f("all", "FY", 0) + mapdl.nsel("s", "node", "", 3) + mapdl.f("all", "FZ", 0) + + mapdl.nsel("s", "node", "", 4) + mapdl.d("all", "UX", 0) + mapdl.nsel("s", "node", "", 5) + mapdl.d("all", "UY", 0) + mapdl.nsel("s", "node", "", 6) + mapdl.d("all", "UZ", 0) + + mapdl.n(7, 0, 1, 0) + mapdl.n(8, 1, 1, 0) + mapdl.n(9, 2, 1, 0) + mapdl.n(10, 0, 2, 0) + mapdl.n(11, 1, 2, 0) + mapdl.n(12, 2, 2, 0) + + mapdl.nsel("s", "node", "", 7) + mapdl.f("all", "FX", 10) + mapdl.nsel("s", "node", "", 8) + mapdl.f("all", "FY", 20) + mapdl.nsel("s", "node", "", 9) + mapdl.f("all", "FZ", 30) + + mapdl.nsel("s", "node", "", 10) + mapdl.d("all", "UX", 20) + mapdl.nsel("s", "node", "", 11) + mapdl.d("all", "UY", 20) + mapdl.nsel("s", "node", "", 12) + mapdl.d("all", "UZ", 20) + + mapdl.nsel("all") + + @staticmethod + @pytest.mark.parametrize("return_plotter", [True, False]) + @pytest.mark.parametrize("plot_bc_legend", [True, False]) + @pytest.mark.parametrize("plot_bc_labels", [True, False]) + def test_bc_plot_options( + mapdl, + boundary_conditions_example, + verify_image_cache, + return_plotter, + plot_bc_legend, + plot_bc_labels, + bc_labels_font_size=50, + ): + + if plot_bc_legend or plot_bc_labels: + # The legend and labels generate highly variance than other tests + verify_image_cache.high_variance_test = True + + p = mapdl.nplot( + return_plotter=return_plotter, + plot_bc=True, + plot_bc_legend=plot_bc_legend, + plot_bc_labels=plot_bc_labels, + title="", + ) + + if return_plotter: + assert isinstance(p, MapdlPlotter) + p.show() + else: + assert p is None + + @staticmethod + @pytest.mark.parametrize( + "bc_labels", # Added second part of the argument to avoid image cache name clashing. + # See https://github.com/pyvista/pytest-pyvista/issues/93 + [ + ["Mechanical", "Title case"], + ["mechanical", "lower case"], + ["meCHANICAL", "Mixed case"], + ["ux", "Lower case"], + ["UX", "Upper case"], + [["UX", "UY"], "List of displacements"], + ["CSGZ", "Magnetic forces"], + ], + ) + def test_bc_plot_bc_labels(mapdl, boundary_conditions_example, bc_labels): + p = mapdl.nplot( + return_plotter=True, + plot_bc=True, + plot_bc_labels=True, + bc_labels=bc_labels[0], + title="", + ) + assert isinstance(p, MapdlPlotter), bc_labels[1] + p.show() # plotting for catching + + @staticmethod + @pytest.mark.parametrize( + "bc_labels", + [ + "error", + ["UX", "error"], + ], + ) + def test_bc_plot_bc_labels_error(mapdl, boundary_conditions_example, bc_labels): + with pytest.raises(ValueError): + mapdl.nplot( + return_plotter=True, + plot_bc=True, + plot_bc_labels=True, + bc_labels=bc_labels, + title="", + ) + + @staticmethod + @pytest.mark.parametrize( + "bc_target", + [ + ["Nodes", "Title case"], + ["NOdes", "Mixed case"], + ], + ) + def test_bc_plot_bc_target(mapdl, boundary_conditions_example, bc_target): + p = mapdl.nplot( + return_plotter=True, + plot_bc=True, + plot_bc_labels=True, + bc_target=bc_target[0], + title="", + ) + assert isinstance(p, MapdlPlotter), bc_target[1] + p.show() # plotting for catching + + @staticmethod + @pytest.mark.parametrize( + "bc_target", + [ + ["NOsdes"], + "error", + ["error"], + {"error": "Not accepting dicts"}, + ], + ) + def test_bc_plot_bc_target_error(mapdl, boundary_conditions_example, bc_target): + with pytest.raises(ValueError): + mapdl.nplot( + return_plotter=True, + plot_bc=True, + plot_bc_labels=True, + bc_target=bc_target, + title="", + ) + + @staticmethod + def test_bc_only_one_node(mapdl, boundary_conditions_example): + mapdl.nsel("s", "node", "", 1) + mapdl.nplot( + plot_bc=True, + title="", + ) + + @staticmethod + def test_bc_glyph(mapdl, boundary_conditions_example): + mapdl.nplot(plot_bc=True, bc_glyph_size=19) + with pytest.raises(ValueError): + mapdl.nplot( + plot_bc=True, + bc_glyph_size="big", + title="", + ) + + @staticmethod + def test_bc_bc_labels(mapdl, boundary_conditions_example, verify_image_cache): + """Test values for 'bc_labels' keyword argument.""" + verify_image_cache.skip = True # skipping image verification + + mapdl.nplot(plot_bc=True, bc_labels="UX") + mapdl.nplot(plot_bc=True, bc_labels=["Ux", "uy", "VOLT"]) + + with pytest.raises(ValueError): + mapdl.nplot(plot_bc=True, bc_labels=["big"]) + + with pytest.raises(ValueError): + mapdl.nplot(plot_bc=True, bc_labels={"not": "valid"}) + + with pytest.raises(ValueError): + mapdl.nplot(plot_bc=True, bc_labels=["UX", {"not": "valid"}]) + + @staticmethod + def test_all_same_values(mapdl, boundary_conditions_example): + """Test the BC glyph size when all the BC have same magnitude.""" + mapdl.nsel("all") + mapdl.f("all", "FX", 0) + mapdl.nplot( + plot_bc=True, + bc_labels="FX", + title="", + ) + + +class TestPlottingBoundaryConditionsCoupled: + + @staticmethod + @pytest.fixture(scope="class") + def block_example_coupled(mapdl): + from conftest import clear + + clear(mapdl) + + mapdl.et(1, 226) + mapdl.keyopt(1, 1, 1011) # Thermal-Piezoelectric + # Disp + # UX, UY, UZ, + # TEMP, VOLT + + # Force + # FX, FY, FZ, + # HEAT, CHRG + mapdl.n(1, 0, 0, 0) + mapdl.n(2, 1, 0, 0) + mapdl.n(3, 2, 0, 0) + + @staticmethod + @pytest.mark.parametrize("field", ALL_LABELS) + @pytest.mark.parametrize( + "loads", [[0, 0, 0], [10, 10, 10], [10, 20, 30], [10, 100, 1000]] + ) + def test_bc_plot_options_fields( + mapdl, block_example_coupled, verify_image_cache, field, loads + ): + mapdl.prep7() + for i in range(len(field)): + mapdl.nsel("s", "node", "", i + 1) + if field[i] in [x for group in FORCE_LABELS for x in group]: + mapdl.f("all", field[i], loads[i]) + else: + mapdl.d("all", field[i], loads[i]) + + mapdl.nsel("s", "node", "", 1, 3) + + p = mapdl.nplot( + plot_bc=True, + plot_bc_legend=True, + plot_bc_labels=True, + title="", + ) + + assert p is None + + +class TestPlottingMeshing: + + @pytest.fixture(scope="class") + def make_block(self): + pass + + @staticmethod + @pytest.mark.parametrize( + "backend", [GraphicsBackend.PYVISTA, GraphicsBackend.MAPDL, None] + ) + def test_eplot(mapdl, make_block, backend): + init_elem = mapdl.mesh.n_elem + mapdl.aplot() # check aplot and verify it doesn't mess up the element plotting + mapdl.eplot(show_node_numbering=True, background="w", color="b") + mapdl.eplot( + graphics_backend=backend, + show_node_numbering=True, + background="w", + color="b", + ) + mapdl.aplot() # check aplot and verify it doesn't mess up the element plotting + assert mapdl.mesh.n_elem == init_elem + + @staticmethod + def test_eplot_savefig(mapdl, make_block, tmpdir): + filename = str(tmpdir.mkdir("tmpdir").join("tmp.png")) + mapdl.eplot( + background="w", + show_edges=True, + smooth_shading=True, + window_size=[1920, 1080], + savefig=filename, + ) + assert os.path.isfile(filename) + + @staticmethod + @pytest.mark.parametrize( + "selection", + ["S", "R", "A", "U"], + ) + def test_pick_nodes(mapdl, make_block, selection, verify_image_cache): + # Cleaning the model a bit + mapdl.modmsh("detach") # detaching geom and fem + mapdl.edele("all") + mapdl.nsel("s", "node", "", 1) + mapdl.nsel("a", "node", "", 2) + mapdl.nsel("inver") + mapdl.ndele("all") + + def debug_orders(pl, point): + pl = pl.scene + pl.show(auto_close=False) + pl.window_size = (100, 100) + width, height = pl.window_size + if pl._picking_right_clicking_observer is None: + pl.iren._mouse_left_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_left_button_release(width, height) + else: + pl.iren._mouse_right_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_right_button_release(width, height) + pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) + + mapdl.nsel("S", "node", "", 1) + if selection == "R" or selection == "U": + point = (285 / 1024, 280 / 800) + mapdl.nsel("a", "node", "", 2) + elif selection == "A": + point = (285 / 1024, 280 / 800) + else: + point = (0.5, 0.5) + + selected = mapdl.nsel( + selection, + "P", + _debug=lambda x: debug_orders(x, point=point), + tolerance=1, + ) # Selects node 2 + + assert isinstance(selected, (list, np.ndarray)) + if isinstance(selected, np.ndarray): + assert selected.all(), "Array is empty" + else: + assert selected, "List is empty" + assert len(selected) > 0, "The result has length zero" + + if selection != "U": + assert sorted(selected) == sorted( + mapdl._get_selected_("node") + ), "Order does not match" + + if selection in ["S", "R"]: + assert selected == [2], "Second node is not selected, nor the only one" + elif selection == "A": + assert 1 in selected, "Node 1 is not selected" + assert len(selected) > 1, "There should be at least two nodes" + elif selection == "U": + assert 2 not in selected, "Node 2 should not be selected." + assert 1 in selected, "Node 1 should be selected." + + mapdl.nplot() + + @staticmethod + @pytest.mark.skip(reason="Issues on CI/CD - will be addressed in another PR") + @pytest.mark.parametrize( + "selection", + ["S", "R", "A", "U"], + ) + def test_pick_kp(mapdl, make_block, selection): + # Cleaning the model a bit + mapdl.modmsh("detach") # detaching geom and fem + mapdl.vdele("all") + mapdl.adele("all") + mapdl.ldele("all") + mapdl.ksel("u", "kp", "", 1) + mapdl.ksel("u", "kp", "", 2) + mapdl.kdele("all") + mapdl.ksel("all") + + def debug_orders(pl, point): + pl = pl.scene + pl.show(auto_close=False) + pl.window_size = (100, 100) + width, height = pl.window_size + if pl._picking_right_clicking_observer is None: + pl.iren._mouse_left_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_left_button_release(width, height) + else: + pl.iren._mouse_right_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_right_button_release(width, height) + + pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) + + mapdl.ksel("S", "KP", "", 1) + if selection == "R" or selection == "U": + point = (285 / 1024, 280 / 800) + mapdl.ksel("a", "kp", "", 2) # Selects node 2 + elif selection == "A": + point = (285 / 1024, 280 / 800) + else: + point = (0.5, 0.5) + + selected = mapdl.ksel( + selection, + "P", + _debug=lambda x: debug_orders(x, point=point), + tolerance=1.0, + ) + + assert isinstance(selected, (list, np.ndarray)) + if isinstance(selected, np.ndarray): + assert selected.all() + else: + assert selected + assert len(selected) > 0 + if selection != "U": + assert sorted(selected) == sorted(mapdl._get_selected_("kp")) + + if selection == "S": + assert selected == [1] + elif selection == "R": + assert selected == [2] + elif selection == "A": + assert 1 in selected + assert 2 in selected + elif selection == "U": + assert 2 not in selected + assert 1 in selected + + @staticmethod + def test_pick_node_failure(mapdl, make_block): + # it should work for the KP too. + # Cleaning the model a bit + mapdl.modmsh("detach") # detaching geom and fem + mapdl.edele("all") + mapdl.nsel("s", "node", "", 1) + mapdl.nsel("a", "node", "", 2) + mapdl.nsel("inver") + mapdl.ndele("all") + + with pytest.raises(ValueError): + mapdl.nsel("X", "P") + + with pytest.raises(ValueError): + mapdl.nsel("S", "node", "", [1, 2, 3], 1) + + with pytest.raises(ValueError): + mapdl.nsel("S", "node", "", [1, 2, 3], "", 1) + + @staticmethod + def test_nsel_ksel_iterable_input(mapdl, make_block): + # Testing using iterable (list/tuple/array) as vmin + assert np.allclose( + mapdl.nsel("S", "node", "", [1, 2, 3], "", ""), np.array([1, 2, 3]) + ) + + # Special cases where the iterable is empty + # empty list + output = mapdl.nsel("S", "node", "", []) + assert output is not None # it should select nothing + if isinstance(output, np.ndarray): + assert output.size == 0 + elif isinstance(output, list): + assert len(output) == 0 + assert len(mapdl._get_selected_("node")) == 0 + + # empty tuple + output = mapdl.nsel("S", "node", "", ()) + assert output is not None # it should select nothing + if isinstance(output, np.ndarray): + assert output.size == 0 + elif isinstance(output, list): + assert len(output) == 0 + + assert len(mapdl._get_selected_("node")) == 0 + + # empty array + output = mapdl.nsel("S", "node", "", np.empty((0))) + assert output is not None # it should select nothing + if isinstance(output, np.ndarray): + assert output.size == 0 + elif isinstance(output, list): + assert len(output) == 0 + assert len(mapdl._get_selected_("node")) == 0 + + @staticmethod + def test_pick_node_special_cases(mapdl, make_block): + # Cleaning the model a bit + mapdl.modmsh("detach") # detaching geom and fem + mapdl.edele("all") + mapdl.nsel("s", "node", "", 1) + mapdl.nsel("a", "node", "", 2) + mapdl.nsel("inver") + mapdl.ndele("all") + + # we pick nothing + def debug_orders_0(pl, point): + pl = pl.scene + pl.show(auto_close=False) + pl.window_size = (100, 100) + width, height = pl.window_size + pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) + + mapdl.nsel("S", "node", "", 1) + point = (285 / 1024, 280 / 800) + mapdl.nsel("a", "node", "", 2) + selected = mapdl.nsel( + "S", "P", _debug=lambda x: debug_orders_0(x, point=point), tolerance=0.2 + ) # Selects node 2 + assert selected == [] + assert np.allclose(mapdl._get_selected_("node"), [1, 2]) + + # we pick something already picked + # we just make sure the number is not repeated and there is no error. + def debug_orders_1(pl, point): + pl = pl.scene + pl.show(auto_close=False) + pl.window_size = (100, 100) + width, height = pl.window_size + # First click + pl.iren._mouse_left_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_left_button_release(width, height) + # Second click + pl.iren._mouse_left_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_left_button_release(width, height) + pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) + + mapdl.nsel("S", "node", "", 1) + point = (285 / 1024, 280 / 800) + mapdl.nsel("a", "node", "", 2) + selected = mapdl.nsel( + "S", "P", _debug=lambda x: debug_orders_1(x, point=point), tolerance=0.1 + ) # Selects node 2 + assert selected is not None + + @staticmethod + def test_pick_node_select_unselect_with_mouse(mapdl, make_block): + # Cleaning the model a bit + mapdl.modmsh("detach") # detaching geom and fem + mapdl.edele("all") + mapdl.nsel("s", "node", "", 1) + mapdl.nsel("a", "node", "", 2) + mapdl.nsel("inver") + mapdl.ndele("all") + + # we pick something already picked + # we just make sure the number is not repeated and there is no error. + def debug_orders_1(pl, point): + pl = pl.scene + pl.show(auto_close=False) + pl.window_size = (100, 100) + width, height = pl.window_size + # First click- selecting + pl.iren._mouse_left_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_left_button_release(width, height) + + pl.iren._simulate_keypress("u") # changing to unselecting + pl._inver_mouse_click_selection = True # making sure + + # Second click- unselecting + pl.iren._mouse_left_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_left_button_release(width, height) + pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) + # so we have selected nothing + + mapdl.nsel("S", "node", "", 1) + point = (285 / 1024, 280 / 800) + mapdl.nsel("a", "node", "", 2) + selected = mapdl.nsel( + "S", "P", _debug=lambda x: debug_orders_1(x, point=point), tolerance=0.1 + ) # Selects node 2 + assert selected == [] + + @staticmethod + @pytest.mark.parametrize( + "selection", + ["S", "R", "A", "U"], + ) + def test_pick_areas(mapdl, make_block, selection): + # Cleaning the model a bit + mapdl.modmsh("detach") # detaching geom and fem + mapdl.edele("all") + mapdl.asel("s", "area", "", 1) + mapdl.asel("a", "area", "", 2) + + def debug_orders(pl, point): + pl = pl.scene + pl.show(auto_close=False) + pl.window_size = (100, 100) + width, height = pl.window_size + if pl._picking_right_clicking_observer is None: + pl.iren._mouse_left_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_left_button_release(width, height) + else: + pl.iren._mouse_right_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_right_button_release(width, height) + pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) + + mapdl.asel("S", "area", "", 1) + if selection == "R" or selection == "U": + point_to_pick = (285 / 1024, 280 / 800) + mapdl.asel("a", "area", "", 2) + elif selection == "A": + point_to_pick = (285 / 1024, 280 / 800) + else: + point_to_pick = (0.5, 0.5) + + selected = mapdl.asel( + selection, + "P", + "area", + _debug=lambda x: debug_orders(x, point=point_to_pick), + tolerance=0.2, + ) # Selects node 2 + + assert isinstance(selected, (list, np.ndarray)) + if isinstance(selected, np.ndarray): + assert selected.all() + else: + assert selected + assert len(selected) > 0 + + if selection != "U": + assert sorted(selected) == sorted(mapdl._get_selected_("area")) + + if selection == "S": + assert selected == [2] # area where the point clicks is area 2. + elif selection == "R": + assert selected == [1] # area where the point clicks is area 282. + elif selection == "A": + assert 6 in selected + assert len(selected) > 1 + elif selection == "U": + assert 282 not in selected + assert 2 in selected + + @staticmethod + @requires("pyvista") + def test_plotter_input(mapdl, make_block): + import pyvista as pv + + pl = MapdlPlotter() + pl2 = mapdl.eplot(return_plotter=True, plotter=pl) + assert pl is pl2 + pl2.show() # plotting for catching + + # invalid plotter type + with pytest.raises(TypeError): + pl2 = mapdl.eplot(return_plotter=True, plotter=[]) + + pl_pv = pv.Plotter() + pl3 = mapdl.eplot(return_plotter=True, plotter=pl_pv) + assert pl3.scene is pl_pv + pl3.show() + + @staticmethod + def test_cpos_input(mapdl, make_block): + cpos = [ + (0.3914, 0.4542, 0.7670), + (0.0243, 0.0336, -0.0222), + (-0.2148, 0.8998, -0.3796), + ] + + cpos1 = mapdl.eplot(cpos=cpos, return_cpos=True) + assert np.allclose( + np.array(cpos), np.array([each for each in cpos1]), rtol=1e-4 + ) + + @staticmethod + def test_show_bounds(mapdl, make_block): + default_bounds = [-1.0, 1.0, -1.0, 1.0, -1.0, 1.0] + pl = mapdl.eplot(show_bounds=True, return_plotter=True) + + assert pl.scene.bounds + assert len(pl.scene.bounds) == 6 + assert pl.scene.bounds != default_bounds + pl.show() # plotting for catching + + @staticmethod + def test_background(mapdl, make_block): + default_color = "#4c4c4cff" + pl = mapdl.eplot(background="red", return_plotter=True) + assert pl.scene.background_color != default_color + assert pl.scene.background_color == "red" + pl.show() # plotting for catching + + @staticmethod + def test_plot_nodal_values(mapdl, make_block): + assert mapdl.post_processing.plot_nodal_values("U", "X") is None + assert mapdl.post_processing.plot_nodal_values("U", "Y") is None + assert mapdl.post_processing.plot_nodal_values("U", "Z") is None + + @staticmethod + def test_lsel_iterable(mapdl, make_block): + assert np.allclose( + mapdl.lsel("S", "line", "", [1, 2, 3], "", ""), np.array([1, 2, 3]) + ) + + @staticmethod + def test_asel_iterable(mapdl, make_block): + assert np.allclose( + mapdl.asel("S", "area", "", [1, 2, 3], "", ""), np.array([1, 2, 3]) + ) + + @staticmethod + def test_vsel_iterable_and_kswp(mapdl, make_block): + mapdl.run("VGEN, 5, 1, , , 100, , , , , ") + assert np.allclose( + mapdl.vsel("S", "volu", "", [1, 2, 4], "", ""), np.array([1, 2, 4]) + ) + mapdl.vsel("S", "volu", "", [1], "", "", kswp=1) + assert np.allclose(mapdl.geometry.vnum, [1]) and np.allclose( + mapdl.geometry.anum, [1, 2, 3, 4, 5, 6] + ) + + @staticmethod + def test_color_areas(mapdl, make_block): + mapdl.aplot(color_areas=True) + + @staticmethod + @pytest.mark.parametrize( + "color_areas", + [ + ["red", "green", "blue", "yellow", "white", "purple"], + [ + [255, 255, 255], + [255, 255, 0], + [255, 0, 0], + [0, 255, 0], + [0, 255, 255], + [0, 0, 0], + ], + 255 + * np.array( + [[1, 1, 1], [1, 1, 0], [1, 0, 0], [0, 1, 0], [0, 1, 1], [0, 0, 0]] + ), + ], + ) + def test_color_areas_individual(mapdl, make_block, color_areas): + # we do rely on the `pytest-pyvista` extension to deal with the differences + mapdl.aplot(color_areas=color_areas) + + @staticmethod + def test_color_areas_error(mapdl, make_block): + color_areas = ["red", "green", "blue"] + with pytest.raises(ValueError): + mapdl.aplot(color_areas=color_areas) + + @staticmethod + def test_WithInterativePlotting(mapdl, make_block): + mapdl.eplot(graphics_backend=GraphicsBackend.MAPDL) + jobname = mapdl.jobname.upper() + + def filtering(file_name): + file_name = file_name.upper() + if file_name.startswith(jobname) and file_name.endswith(".PNG"): + return True + else: + return False + + list_files = sorted([each for each in mapdl.list_files() if filtering(each)]) + last_png = list_files[0] + + if mapdl.is_local: + last_png = mapdl.directory / last_png + else: + mapdl.download(last_png) + + # the file size will be 3kb if the image is empty. + assert os.path.getsize(last_png) // 1024 > 4 # kbs + + # cleaning + os.remove(last_png) + + @staticmethod + @pytest.mark.parametrize("entity", ["KP", "LINE", "AREA", "VOLU", "NODE", "ELEM"]) + def test_cmplot_individual(mapdl, make_block, entity): + mapdl.allsel() + mapdl.cm("tmp_cm", entity=entity) + mapdl.cmplot("tmp_cm") + + @staticmethod + @pytest.mark.parametrize("label", ["N", "P"]) + def test_cmplot_label_error(mapdl, make_block, label): + with pytest.raises(ValueError): + mapdl.cmplot(label) + + @staticmethod + def test_cmplot_entity_error(mapdl, make_block): + with pytest.raises(ValueError): + mapdl.cmplot("all", "non_valid_entity") + + @staticmethod + def test_cmplot_incorrect_entity(mapdl, make_block): + mapdl.allsel() + mapdl.cm("tmp_cm", entity="NODE") + with pytest.raises(ValueError): + mapdl.cmplot("tmp_cm", "KP") + + @staticmethod + def test_cmplot_component_not_exist(mapdl, make_block): + with pytest.raises(ComponentDoesNotExits): + mapdl.cmplot("not_exist") + + @staticmethod + @pytest.mark.parametrize("entity", ["KP", "NODE"]) + def test_cmplot_all(mapdl, make_block, entity): + mapdl.allsel() + ids = np.array([1, 2, 3, 4]) + if entity == "KP": + ent = mapdl.geometry.get_keypoints(return_as_array=True) + func_sel = mapdl.ksel + else: + ent = mapdl.mesh.nodes + func_sel = mapdl.nsel + + func_sel("S", vmin=ids[:2]) + mapdl.cm("tmp_cm1", entity=entity) + + func_sel("S", vmin=ids[2:]) + mapdl.cm("tmp_cm2", entity=entity) + + pl = mapdl.cmplot("all", entity, return_plotter=True) + + assert np.allclose(pl.meshes[0].points, ent[ids - 1]) + pl.show() + + @staticmethod + @pytest.mark.parametrize("background", ["white", "black", "green", "red"]) + def test_labels_colors_background(mapdl, make_block, background): + # Test if the labels change color according background + mapdl.nplot(background=background, nnum=True) + + @staticmethod + def test_vplot_show_volume_numbering(mapdl, make_block): + mapdl.vplot(show_volume_numbering=True) + + @staticmethod + def test_vplot_area_numbering(mapdl, make_block): + mapdl.vplot(show_area_numbering=True) + + @staticmethod + def test_vplot_line_numbering(mapdl, make_block): + mapdl.vplot(show_line_numbering=True) + + @staticmethod + def test_vplot_multi_numbering(mapdl, make_block): + mapdl.vplot( + show_area_numbering=True, + show_line_numbering=True, + show_volume_numbering=True, + ) + + @staticmethod + def test_vplot_color(mapdl, make_block): + mapdl.vplot(color="gray") + + @staticmethod + def test_vplot_cpos(mapdl, make_block): + mapdl.vplot(cpos="xy") + + @staticmethod + def test_vplot_multiargs(mapdl, make_block): + mapdl.vplot( + color="gray", + cpos="xy", + show_volume_numbering=True, + show_line_numbering=False, + show_area_numbering=True, + ) + + @staticmethod + @pytest.mark.parametrize("quality", [101, -2, 0, "as"]) + def test_aplot_quality_fail(mapdl, make_block, quality): + with pytest.raises( + ValueError, + match="The argument 'quality' can only be an integer between 1 and 10", + ): + mapdl.aplot(quality=quality) + + @staticmethod + def test_deprecated_params(mapdl, make_block): + with pytest.warns( + DeprecationWarning, match="'vtk' and 'use_vtk' are deprecated" + ): + mapdl.eplot(vtk=True) + with pytest.warns( + DeprecationWarning, match="'vtk' and 'use_vtk' are deprecated" + ): + mapdl.eplot(vtk=False) + + def test_plot_empty_mesh(mapdl, cleared): with pytest.warns(UserWarning): mapdl.nplot() @@ -290,32 +1141,6 @@ def test_nplot(cleared, mapdl, nnum, backend): mapdl.nplot(graphics_backend=backend, nnum=nnum, background="w", color="k") -@pytest.mark.parametrize( - "backend", [GraphicsBackend.PYVISTA, GraphicsBackend.MAPDL, None] -) -def test_eplot(mapdl, make_block, backend): - init_elem = mapdl.mesh.n_elem - mapdl.aplot() # check aplot and verify it doesn't mess up the element plotting - mapdl.eplot(show_node_numbering=True, background="w", color="b") - mapdl.eplot( - graphics_backend=backend, show_node_numbering=True, background="w", color="b" - ) - mapdl.aplot() # check aplot and verify it doesn't mess up the element plotting - assert mapdl.mesh.n_elem == init_elem - - -def test_eplot_savefig(mapdl, make_block, tmpdir): - filename = str(tmpdir.mkdir("tmpdir").join("tmp.png")) - mapdl.eplot( - background="w", - show_edges=True, - smooth_shading=True, - window_size=[1920, 1080], - savefig=filename, - ) - assert os.path.isfile(filename) - - @pytest.mark.parametrize( "field", ["UX", "UY", "UZ", "FX", "FY", "FZ", "TEMP", "HEAT", "VOLT", "CHRG"] ) @@ -352,147 +1177,6 @@ def test_single_glyph(mapdl, cleared, field, magnitude, verify_image_cache): ) -@pytest.mark.parametrize("return_plotter", [True, False]) -@pytest.mark.parametrize("plot_bc_legend", [True, False]) -@pytest.mark.parametrize("plot_bc_labels", [True, False]) -def test_bc_plot_options( - mapdl, - boundary_conditions_example, - verify_image_cache, - return_plotter, - plot_bc_legend, - plot_bc_labels, - bc_labels_font_size=50, -): - - if plot_bc_legend or plot_bc_labels: - # The legend and labels generate highly variance than other tests - verify_image_cache.high_variance_test = True - - p = mapdl.nplot( - return_plotter=return_plotter, - plot_bc=True, - plot_bc_legend=plot_bc_legend, - plot_bc_labels=plot_bc_labels, - title="", - ) - - if return_plotter: - assert isinstance(p, MapdlPlotter) - p.show() - else: - assert p is None - - -@pytest.mark.parametrize("field", ALL_LABELS) -@pytest.mark.parametrize( - "loads", [[0, 0, 0], [10, 10, 10], [10, 20, 30], [10, 100, 1000]] -) -def test_bc_plot_options_fields( - mapdl, block_example_coupled, verify_image_cache, field, loads -): - mapdl.prep7() - for i in range(len(field)): - mapdl.nsel("s", "node", "", i + 1) - if field[i] in [x for group in FORCE_LABELS for x in group]: - mapdl.f("all", field[i], loads[i]) - else: - mapdl.d("all", field[i], loads[i]) - - mapdl.nsel("s", "node", "", 1, 3) - - p = mapdl.nplot( - plot_bc=True, - plot_bc_legend=True, - plot_bc_labels=True, - title="", - ) - - assert p is None - - -@pytest.mark.parametrize( - "bc_labels", # Added second part of the argument to avoid image cache name clashing. - # See https://github.com/pyvista/pytest-pyvista/issues/93 - [ - ["Mechanical", "Title case"], - ["mechanical", "lower case"], - ["meCHANICAL", "Mixed case"], - ["ux", "Lower case"], - ["UX", "Upper case"], - [["UX", "UY"], "List of displacements"], - ["CSGZ", "Magnetic forces"], - ], -) -def test_bc_plot_bc_labels(mapdl, boundary_conditions_example, bc_labels): - p = mapdl.nplot( - return_plotter=True, - plot_bc=True, - plot_bc_labels=True, - bc_labels=bc_labels[0], - title="", - ) - assert isinstance(p, MapdlPlotter), bc_labels[1] - p.show() # plotting for catching - - -@pytest.mark.parametrize( - "bc_labels", - [ - "error", - ["UX", "error"], - ], -) -def test_bc_plot_bc_labels_error(mapdl, boundary_conditions_example, bc_labels): - with pytest.raises(ValueError): - mapdl.nplot( - return_plotter=True, - plot_bc=True, - plot_bc_labels=True, - bc_labels=bc_labels, - title="", - ) - - -@pytest.mark.parametrize( - "bc_target", - [ - ["Nodes", "Title case"], - ["NOdes", "Mixed case"], - ], -) -def test_bc_plot_bc_target(mapdl, boundary_conditions_example, bc_target): - p = mapdl.nplot( - return_plotter=True, - plot_bc=True, - plot_bc_labels=True, - bc_target=bc_target[0], - title="", - ) - assert isinstance(p, MapdlPlotter), bc_target[1] - p.show() # plotting for catching - - -@pytest.mark.parametrize( - "bc_target", - [ - ["NOsdes"], - "error", - ["error"], - {"error": "Not accepting dicts"}, - ], -) -def test_bc_plot_bc_target_error(mapdl, boundary_conditions_example, bc_target): - with pytest.raises(ValueError): - mapdl.nplot( - return_plotter=True, - plot_bc=True, - plot_bc_labels=True, - bc_target=bc_target, - title="", - ) - - def test_bc_no_mapdl(mapdl, cleared): with pytest.raises(ValueError): pl = MapdlPlotter() @@ -501,538 +1185,6 @@ def test_bc_no_mapdl(mapdl, cleared): # mapdl should be an argument if plotting BC -def test_bc_only_one_node(mapdl, boundary_conditions_example): - mapdl.nsel("s", "node", "", 1) - mapdl.nplot( - plot_bc=True, - title="", - ) - - -def test_bc_glyph(mapdl, boundary_conditions_example): - mapdl.nplot(plot_bc=True, bc_glyph_size=19) - with pytest.raises(ValueError): - mapdl.nplot( - plot_bc=True, - bc_glyph_size="big", - title="", - ) - - -def test_bc_bc_labels(mapdl, boundary_conditions_example, verify_image_cache): - """Test values for 'bc_labels' keyword argument.""" - verify_image_cache.skip = True # skipping image verification - - mapdl.nplot(plot_bc=True, bc_labels="UX") - mapdl.nplot(plot_bc=True, bc_labels=["Ux", "uy", "VOLT"]) - - with pytest.raises(ValueError): - mapdl.nplot(plot_bc=True, bc_labels=["big"]) - - with pytest.raises(ValueError): - mapdl.nplot(plot_bc=True, bc_labels={"not": "valid"}) - - with pytest.raises(ValueError): - mapdl.nplot(plot_bc=True, bc_labels=["UX", {"not": "valid"}]) - - -def test_all_same_values(mapdl, boundary_conditions_example): - """Test the BC glyph size when all the BC have same magnitude.""" - mapdl.nsel("all") - mapdl.f("all", "FX", 0) - mapdl.nplot( - plot_bc=True, - bc_labels="FX", - title="", - ) - - -@pytest.mark.parametrize( - "selection", - ["S", "R", "A", "U"], -) -def test_pick_nodes(mapdl, make_block, selection, verify_image_cache): - # Cleaning the model a bit - mapdl.modmsh("detach") # detaching geom and fem - mapdl.edele("all") - mapdl.nsel("s", "node", "", 1) - mapdl.nsel("a", "node", "", 2) - mapdl.nsel("inver") - mapdl.ndele("all") - - def debug_orders(pl, point): - pl = pl.scene - pl.show(auto_close=False) - pl.window_size = (100, 100) - width, height = pl.window_size - if pl._picking_right_clicking_observer is None: - pl.iren._mouse_left_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_left_button_release(width, height) - else: - pl.iren._mouse_right_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_right_button_release(width, height) - pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) - - mapdl.nsel("S", "node", "", 1) - if selection == "R" or selection == "U": - point = (285 / 1024, 280 / 800) - mapdl.nsel("a", "node", "", 2) - elif selection == "A": - point = (285 / 1024, 280 / 800) - else: - point = (0.5, 0.5) - - selected = mapdl.nsel( - selection, - "P", - _debug=lambda x: debug_orders(x, point=point), - tolerance=1, - ) # Selects node 2 - - assert isinstance(selected, (list, np.ndarray)) - if isinstance(selected, np.ndarray): - assert selected.all(), "Array is empty" - else: - assert selected, "List is empty" - assert len(selected) > 0, "The result has length zero" - - if selection != "U": - assert sorted(selected) == sorted( - mapdl._get_selected_("node") - ), "Order does not match" - - if selection in ["S", "R"]: - assert selected == [2], "Second node is not selected, nor the only one" - elif selection == "A": - assert 1 in selected, "Node 1 is not selected" - assert len(selected) > 1, "There should be at least two nodes" - elif selection == "U": - assert 2 not in selected, "Node 2 should not be selected." - assert 1 in selected, "Node 1 should be selected." - - mapdl.nplot() - - -@pytest.mark.skip(reason="Issues on CI/CD - will be addressed in another PR") -@pytest.mark.parametrize( - "selection", - ["S", "R", "A", "U"], -) -def test_pick_kp(mapdl, make_block, selection): - # Cleaning the model a bit - mapdl.modmsh("detach") # detaching geom and fem - mapdl.vdele("all") - mapdl.adele("all") - mapdl.ldele("all") - mapdl.ksel("u", "kp", "", 1) - mapdl.ksel("u", "kp", "", 2) - mapdl.kdele("all") - mapdl.ksel("all") - - def debug_orders(pl, point): - pl = pl.scene - pl.show(auto_close=False) - pl.window_size = (100, 100) - width, height = pl.window_size - if pl._picking_right_clicking_observer is None: - pl.iren._mouse_left_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_left_button_release(width, height) - else: - pl.iren._mouse_right_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_right_button_release(width, height) - - pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) - - mapdl.ksel("S", "KP", "", 1) - if selection == "R" or selection == "U": - point = (285 / 1024, 280 / 800) - mapdl.ksel("a", "kp", "", 2) # Selects node 2 - elif selection == "A": - point = (285 / 1024, 280 / 800) - else: - point = (0.5, 0.5) - - selected = mapdl.ksel( - selection, - "P", - _debug=lambda x: debug_orders(x, point=point), - tolerance=1.0, - ) - - assert isinstance(selected, (list, np.ndarray)) - if isinstance(selected, np.ndarray): - assert selected.all() - else: - assert selected - assert len(selected) > 0 - if selection != "U": - assert sorted(selected) == sorted(mapdl._get_selected_("kp")) - - if selection == "S": - assert selected == [1] - elif selection == "R": - assert selected == [2] - elif selection == "A": - assert 1 in selected - assert 2 in selected - elif selection == "U": - assert 2 not in selected - assert 1 in selected - - -def test_pick_node_failure(mapdl, make_block): - # it should work for the KP too. - # Cleaning the model a bit - mapdl.modmsh("detach") # detaching geom and fem - mapdl.edele("all") - mapdl.nsel("s", "node", "", 1) - mapdl.nsel("a", "node", "", 2) - mapdl.nsel("inver") - mapdl.ndele("all") - - with pytest.raises(ValueError): - mapdl.nsel("X", "P") - - with pytest.raises(ValueError): - mapdl.nsel("S", "node", "", [1, 2, 3], 1) - - with pytest.raises(ValueError): - mapdl.nsel("S", "node", "", [1, 2, 3], "", 1) - - -def test_nsel_ksel_iterable_input(mapdl, make_block): - # Testing using iterable (list/tuple/array) as vmin - assert np.allclose( - mapdl.nsel("S", "node", "", [1, 2, 3], "", ""), np.array([1, 2, 3]) - ) - - # Special cases where the iterable is empty - # empty list - output = mapdl.nsel("S", "node", "", []) - assert output is not None # it should select nothing - if isinstance(output, np.ndarray): - assert output.size == 0 - elif isinstance(output, list): - assert len(output) == 0 - assert len(mapdl._get_selected_("node")) == 0 - - # empty tuple - output = mapdl.nsel("S", "node", "", ()) - assert output is not None # it should select nothing - if isinstance(output, np.ndarray): - assert output.size == 0 - elif isinstance(output, list): - assert len(output) == 0 - - assert len(mapdl._get_selected_("node")) == 0 - - # empty array - output = mapdl.nsel("S", "node", "", np.empty((0))) - assert output is not None # it should select nothing - if isinstance(output, np.ndarray): - assert output.size == 0 - elif isinstance(output, list): - assert len(output) == 0 - assert len(mapdl._get_selected_("node")) == 0 - - -def test_pick_node_special_cases(mapdl, make_block): - # Cleaning the model a bit - mapdl.modmsh("detach") # detaching geom and fem - mapdl.edele("all") - mapdl.nsel("s", "node", "", 1) - mapdl.nsel("a", "node", "", 2) - mapdl.nsel("inver") - mapdl.ndele("all") - - # we pick nothing - def debug_orders_0(pl, point): - pl = pl.scene - pl.show(auto_close=False) - pl.window_size = (100, 100) - width, height = pl.window_size - pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) - - mapdl.nsel("S", "node", "", 1) - point = (285 / 1024, 280 / 800) - mapdl.nsel("a", "node", "", 2) - selected = mapdl.nsel( - "S", "P", _debug=lambda x: debug_orders_0(x, point=point), tolerance=0.2 - ) # Selects node 2 - assert selected == [] - assert np.allclose(mapdl._get_selected_("node"), [1, 2]) - - # we pick something already picked - # we just make sure the number is not repeated and there is no error. - def debug_orders_1(pl, point): - pl = pl.scene - pl.show(auto_close=False) - pl.window_size = (100, 100) - width, height = pl.window_size - # First click - pl.iren._mouse_left_button_press(int(width * point[0]), int(height * point[1])) - pl.iren._mouse_left_button_release(width, height) - # Second click - pl.iren._mouse_left_button_press(int(width * point[0]), int(height * point[1])) - pl.iren._mouse_left_button_release(width, height) - pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) - - mapdl.nsel("S", "node", "", 1) - point = (285 / 1024, 280 / 800) - mapdl.nsel("a", "node", "", 2) - selected = mapdl.nsel( - "S", "P", _debug=lambda x: debug_orders_1(x, point=point), tolerance=0.1 - ) # Selects node 2 - assert selected is not None - - -def test_pick_node_select_unselect_with_mouse(mapdl, make_block): - # Cleaning the model a bit - mapdl.modmsh("detach") # detaching geom and fem - mapdl.edele("all") - mapdl.nsel("s", "node", "", 1) - mapdl.nsel("a", "node", "", 2) - mapdl.nsel("inver") - mapdl.ndele("all") - - # we pick something already picked - # we just make sure the number is not repeated and there is no error. - def debug_orders_1(pl, point): - pl = pl.scene - pl.show(auto_close=False) - pl.window_size = (100, 100) - width, height = pl.window_size - # First click- selecting - pl.iren._mouse_left_button_press(int(width * point[0]), int(height * point[1])) - pl.iren._mouse_left_button_release(width, height) - - pl.iren._simulate_keypress("u") # changing to unselecting - pl._inver_mouse_click_selection = True # making sure - - # Second click- unselecting - pl.iren._mouse_left_button_press(int(width * point[0]), int(height * point[1])) - pl.iren._mouse_left_button_release(width, height) - pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) - # so we have selected nothing - - mapdl.nsel("S", "node", "", 1) - point = (285 / 1024, 280 / 800) - mapdl.nsel("a", "node", "", 2) - selected = mapdl.nsel( - "S", "P", _debug=lambda x: debug_orders_1(x, point=point), tolerance=0.1 - ) # Selects node 2 - assert selected == [] - - -@pytest.mark.parametrize( - "selection", - ["S", "R", "A", "U"], -) -def test_pick_areas(mapdl, make_block, selection): - # Cleaning the model a bit - mapdl.modmsh("detach") # detaching geom and fem - mapdl.edele("all") - mapdl.asel("s", "area", "", 1) - mapdl.asel("a", "area", "", 2) - - def debug_orders(pl, point): - pl = pl.scene - pl.show(auto_close=False) - pl.window_size = (100, 100) - width, height = pl.window_size - if pl._picking_right_clicking_observer is None: - pl.iren._mouse_left_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_left_button_release(width, height) - else: - pl.iren._mouse_right_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_right_button_release(width, height) - pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) - - mapdl.asel("S", "area", "", 1) - if selection == "R" or selection == "U": - point_to_pick = (285 / 1024, 280 / 800) - mapdl.asel("a", "area", "", 2) - elif selection == "A": - point_to_pick = (285 / 1024, 280 / 800) - else: - point_to_pick = (0.5, 0.5) - - selected = mapdl.asel( - selection, - "P", - "area", - _debug=lambda x: debug_orders(x, point=point_to_pick), - tolerance=0.2, - ) # Selects node 2 - - assert isinstance(selected, (list, np.ndarray)) - if isinstance(selected, np.ndarray): - assert selected.all() - else: - assert selected - assert len(selected) > 0 - - if selection != "U": - assert sorted(selected) == sorted(mapdl._get_selected_("area")) - - if selection == "S": - assert selected == [2] # area where the point clicks is area 2. - elif selection == "R": - assert selected == [1] # area where the point clicks is area 282. - elif selection == "A": - assert 6 in selected - assert len(selected) > 1 - elif selection == "U": - assert 282 not in selected - assert 2 in selected - - -@requires("pyvista") -def test_plotter_input(mapdl, make_block): - import pyvista as pv - - pl = MapdlPlotter() - pl2 = mapdl.eplot(return_plotter=True, plotter=pl) - assert pl is pl2 - pl2.show() # plotting for catching - - # invalid plotter type - with pytest.raises(TypeError): - pl2 = mapdl.eplot(return_plotter=True, plotter=[]) - - pl_pv = pv.Plotter() - pl3 = mapdl.eplot(return_plotter=True, plotter=pl_pv) - assert pl3.scene is pl_pv - pl3.show() - - -def test_cpos_input(mapdl, make_block): - cpos = [ - (0.3914, 0.4542, 0.7670), - (0.0243, 0.0336, -0.0222), - (-0.2148, 0.8998, -0.3796), - ] - - cpos1 = mapdl.eplot(cpos=cpos, return_cpos=True) - assert np.allclose(np.array(cpos), np.array([each for each in cpos1]), rtol=1e-4) - - -def test_show_bounds(mapdl, make_block): - default_bounds = [-1.0, 1.0, -1.0, 1.0, -1.0, 1.0] - pl = mapdl.eplot(show_bounds=True, return_plotter=True) - - assert pl.scene.bounds - assert len(pl.scene.bounds) == 6 - assert pl.scene.bounds != default_bounds - pl.show() # plotting for catching - - -def test_background(mapdl, make_block): - default_color = "#4c4c4cff" - pl = mapdl.eplot(background="red", return_plotter=True) - assert pl.scene.background_color != default_color - assert pl.scene.background_color == "red" - pl.show() # plotting for catching - - -def test_plot_nodal_values(mapdl, make_block): - assert mapdl.post_processing.plot_nodal_values("U", "X") is None - assert mapdl.post_processing.plot_nodal_values("U", "Y") is None - assert mapdl.post_processing.plot_nodal_values("U", "Z") is None - - -def test_lsel_iterable(mapdl, make_block): - assert np.allclose( - mapdl.lsel("S", "line", "", [1, 2, 3], "", ""), np.array([1, 2, 3]) - ) - - -def test_asel_iterable(mapdl, make_block): - assert np.allclose( - mapdl.asel("S", "area", "", [1, 2, 3], "", ""), np.array([1, 2, 3]) - ) - - -def test_vsel_iterable_and_kswp(mapdl, make_block): - mapdl.run("VGEN, 5, 1, , , 100, , , , , ") - assert np.allclose( - mapdl.vsel("S", "volu", "", [1, 2, 4], "", ""), np.array([1, 2, 4]) - ) - mapdl.vsel("S", "volu", "", [1], "", "", kswp=1) - assert np.allclose(mapdl.geometry.vnum, [1]) and np.allclose( - mapdl.geometry.anum, [1, 2, 3, 4, 5, 6] - ) - - -def test_color_areas(mapdl, make_block): - mapdl.aplot(color_areas=True) - - -@pytest.mark.parametrize( - "color_areas", - [ - ["red", "green", "blue", "yellow", "white", "purple"], - [ - [255, 255, 255], - [255, 255, 0], - [255, 0, 0], - [0, 255, 0], - [0, 255, 255], - [0, 0, 0], - ], - 255 - * np.array([[1, 1, 1], [1, 1, 0], [1, 0, 0], [0, 1, 0], [0, 1, 1], [0, 0, 0]]), - ], -) -def test_color_areas_individual(mapdl, make_block, color_areas): - # we do rely on the `pytest-pyvista` extension to deal with the differences - mapdl.aplot(color_areas=color_areas) - - -def test_color_areas_error(mapdl, make_block): - color_areas = ["red", "green", "blue"] - with pytest.raises(ValueError): - mapdl.aplot(color_areas=color_areas) - - -def test_WithInterativePlotting(mapdl, make_block): - mapdl.eplot(graphics_backend=GraphicsBackend.MAPDL) - jobname = mapdl.jobname.upper() - - def filtering(file_name): - file_name = file_name.upper() - if file_name.startswith(jobname) and file_name.endswith(".PNG"): - return True - else: - return False - - list_files = sorted([each for each in mapdl.list_files() if filtering(each)]) - last_png = list_files[0] - - if mapdl.is_local: - last_png = mapdl.directory / last_png - else: - mapdl.download(last_png) - - # the file size will be 3kb if the image is empty. - assert os.path.getsize(last_png) // 1024 > 4 # kbs - - # cleaning - os.remove(last_png) - - def test_file_type_for_plots(mapdl, cleared): assert mapdl.file_type_for_plots in ["PNG", "TIFF", "PNG", "VRML", "TERM", "CLOSE"] @@ -1055,59 +1207,6 @@ def test_file_type_for_plots(mapdl, cleared): assert n_files_ending_png_before + 2 == n_files_ending_png_after -@pytest.mark.parametrize("entity", ["KP", "LINE", "AREA", "VOLU", "NODE", "ELEM"]) -def test_cmplot_individual(mapdl, make_block, entity): - mapdl.allsel() - mapdl.cm("tmp_cm", entity=entity) - mapdl.cmplot("tmp_cm") - - -@pytest.mark.parametrize("label", ["N", "P"]) -def test_cmplot_label_error(mapdl, make_block, label): - with pytest.raises(ValueError): - mapdl.cmplot(label) - - -def test_cmplot_entity_error(mapdl, make_block): - with pytest.raises(ValueError): - mapdl.cmplot("all", "non_valid_entity") - - -def test_cmplot_incorrect_entity(mapdl, make_block): - mapdl.allsel() - mapdl.cm("tmp_cm", entity="NODE") - with pytest.raises(ValueError): - mapdl.cmplot("tmp_cm", "KP") - - -def test_cmplot_component_not_exist(mapdl, make_block): - with pytest.raises(ComponentDoesNotExits): - mapdl.cmplot("not_exist") - - -@pytest.mark.parametrize("entity", ["KP", "NODE"]) -def test_cmplot_all(mapdl, make_block, entity): - mapdl.allsel() - ids = np.array([1, 2, 3, 4]) - if entity == "KP": - ent = mapdl.geometry.get_keypoints(return_as_array=True) - func_sel = mapdl.ksel - else: - ent = mapdl.mesh.nodes - func_sel = mapdl.nsel - - func_sel("S", vmin=ids[:2]) - mapdl.cm("tmp_cm1", entity=entity) - - func_sel("S", vmin=ids[2:]) - mapdl.cm("tmp_cm2", entity=entity) - - pl = mapdl.cmplot("all", entity, return_plotter=True) - - assert np.allclose(pl.meshes[0].points, ent[ids - 1]) - pl.show() - - def test_cuadratic_beam(mapdl, cuadratic_beam_problem): mapdl.post1() mapdl.set(1) @@ -1119,48 +1218,6 @@ def test_cuadratic_beam(mapdl, cuadratic_beam_problem): ) -@pytest.mark.parametrize("background", ["white", "black", "green", "red"]) -def test_labels_colors_background(mapdl, make_block, background): - # Test if the labels change color according background - mapdl.nplot(background=background, nnum=True) - - -def test_vplot_show_volume_numbering(mapdl, make_block): - mapdl.vplot(show_volume_numbering=True) - - -def test_vplot_area_numbering(mapdl, make_block): - mapdl.vplot(show_area_numbering=True) - - -def test_vplot_line_numbering(mapdl, make_block): - mapdl.vplot(show_line_numbering=True) - - -def test_vplot_multi_numbering(mapdl, make_block): - mapdl.vplot( - show_area_numbering=True, show_line_numbering=True, show_volume_numbering=True - ) - - -def test_vplot_color(mapdl, make_block): - mapdl.vplot(color="gray") - - -def test_vplot_cpos(mapdl, make_block): - mapdl.vplot(cpos="xy") - - -def test_vplot_multiargs(mapdl, make_block): - mapdl.vplot( - color="gray", - cpos="xy", - show_volume_numbering=True, - show_line_numbering=False, - show_area_numbering=True, - ) - - def test_node_numbering_order(mapdl, cleared): # create nodes for node in range(1, 6): @@ -1269,15 +1326,6 @@ def test_xplot_not_changing_geo_selection_components( check_geometry(mapdl, plot_func) -@pytest.mark.parametrize("quality", [101, -2, 0, "as"]) -def test_aplot_quality_fail(mapdl, make_block, quality): - with pytest.raises( - ValueError, - match="The argument 'quality' can only be an integer between 1 and 10", - ): - mapdl.aplot(quality=quality) - - @patch("ansys.mapdl.core.Mapdl.is_png_found", lambda *args, **kwargs: False) def test_plot_path(mapdl, tmpdir): mapdl.graphics("POWER") @@ -1334,13 +1382,6 @@ def test_plot_path_screenshoot(mapdl, cleared, tmpdir): mapdl.graphics("FULL") -def test_deprecated_params(mapdl, make_block): - with pytest.warns(DeprecationWarning, match="'vtk' and 'use_vtk' are deprecated"): - mapdl.eplot(vtk=True) - with pytest.warns(DeprecationWarning, match="'vtk' and 'use_vtk' are deprecated"): - mapdl.eplot(vtk=False) - - def test_plvar(mapdl, coupled_example): mapdl.post26() with patch("ansys.mapdl.core.Mapdl.screenshot") as mock_screenshot: From e8808692f6ec80dc9d5b7518865aa009b64d7ac4 Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Thu, 27 Nov 2025 11:51:44 +0000 Subject: [PATCH 2/6] chore: adding changelog file 4326.miscellaneous.md [dependabot-skip] --- doc/changelog.d/4326.miscellaneous.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/4326.miscellaneous.md diff --git a/doc/changelog.d/4326.miscellaneous.md b/doc/changelog.d/4326.miscellaneous.md new file mode 100644 index 00000000000..070d26567bd --- /dev/null +++ b/doc/changelog.d/4326.miscellaneous.md @@ -0,0 +1 @@ +Moving tests to classes to speed up execution From 35e97ab785d2b2ef2c3400a98d48f6cf06d47094 Mon Sep 17 00:00:00 2001 From: germa89 <28149841+germa89@users.noreply.github.com> Date: Thu, 27 Nov 2025 16:28:01 +0100 Subject: [PATCH 3/6] refactor: implement make_block fixture for TestPlottingMeshing --- tests/test_plotting.py | 85 +++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/tests/test_plotting.py b/tests/test_plotting.py index fb5c6b5ccd1..892634e3374 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -351,14 +351,21 @@ def test_bc_plot_options_fields( class TestPlottingMeshing: @pytest.fixture(scope="class") - def make_block(self): - pass + def make_class_block(self, mapdl): + from conftest import clear + + clear(mapdl) + + mapdl.block(0, 1, 0, 1, 0, 1) + mapdl.et(1, 186) + mapdl.esize(0.25) + mapdl.vmesh("ALL") @staticmethod @pytest.mark.parametrize( "backend", [GraphicsBackend.PYVISTA, GraphicsBackend.MAPDL, None] ) - def test_eplot(mapdl, make_block, backend): + def test_eplot(mapdl, make_class_block, backend): init_elem = mapdl.mesh.n_elem mapdl.aplot() # check aplot and verify it doesn't mess up the element plotting mapdl.eplot(show_node_numbering=True, background="w", color="b") @@ -372,7 +379,7 @@ def test_eplot(mapdl, make_block, backend): assert mapdl.mesh.n_elem == init_elem @staticmethod - def test_eplot_savefig(mapdl, make_block, tmpdir): + def test_eplot_savefig(mapdl, make_class_block, tmpdir): filename = str(tmpdir.mkdir("tmpdir").join("tmp.png")) mapdl.eplot( background="w", @@ -388,7 +395,7 @@ def test_eplot_savefig(mapdl, make_block, tmpdir): "selection", ["S", "R", "A", "U"], ) - def test_pick_nodes(mapdl, make_block, selection, verify_image_cache): + def test_pick_nodes(mapdl, make_class_block, selection, verify_image_cache): # Cleaning the model a bit mapdl.modmsh("detach") # detaching geom and fem mapdl.edele("all") @@ -459,7 +466,7 @@ def debug_orders(pl, point): "selection", ["S", "R", "A", "U"], ) - def test_pick_kp(mapdl, make_block, selection): + def test_pick_kp(mapdl, make_class_block, selection): # Cleaning the model a bit mapdl.modmsh("detach") # detaching geom and fem mapdl.vdele("all") @@ -525,7 +532,7 @@ def debug_orders(pl, point): assert 1 in selected @staticmethod - def test_pick_node_failure(mapdl, make_block): + def test_pick_node_failure(mapdl, make_class_block): # it should work for the KP too. # Cleaning the model a bit mapdl.modmsh("detach") # detaching geom and fem @@ -545,7 +552,7 @@ def test_pick_node_failure(mapdl, make_block): mapdl.nsel("S", "node", "", [1, 2, 3], "", 1) @staticmethod - def test_nsel_ksel_iterable_input(mapdl, make_block): + def test_nsel_ksel_iterable_input(mapdl, make_class_block): # Testing using iterable (list/tuple/array) as vmin assert np.allclose( mapdl.nsel("S", "node", "", [1, 2, 3], "", ""), np.array([1, 2, 3]) @@ -581,7 +588,7 @@ def test_nsel_ksel_iterable_input(mapdl, make_block): assert len(mapdl._get_selected_("node")) == 0 @staticmethod - def test_pick_node_special_cases(mapdl, make_block): + def test_pick_node_special_cases(mapdl, make_class_block): # Cleaning the model a bit mapdl.modmsh("detach") # detaching geom and fem mapdl.edele("all") @@ -635,7 +642,7 @@ def debug_orders_1(pl, point): assert selected is not None @staticmethod - def test_pick_node_select_unselect_with_mouse(mapdl, make_block): + def test_pick_node_select_unselect_with_mouse(mapdl, make_class_block): # Cleaning the model a bit mapdl.modmsh("detach") # detaching geom and fem mapdl.edele("all") @@ -681,7 +688,7 @@ def debug_orders_1(pl, point): "selection", ["S", "R", "A", "U"], ) - def test_pick_areas(mapdl, make_block, selection): + def test_pick_areas(mapdl, make_class_block, selection): # Cleaning the model a bit mapdl.modmsh("detach") # detaching geom and fem mapdl.edele("all") @@ -745,7 +752,7 @@ def debug_orders(pl, point): @staticmethod @requires("pyvista") - def test_plotter_input(mapdl, make_block): + def test_plotter_input(mapdl, make_class_block): import pyvista as pv pl = MapdlPlotter() @@ -763,7 +770,7 @@ def test_plotter_input(mapdl, make_block): pl3.show() @staticmethod - def test_cpos_input(mapdl, make_block): + def test_cpos_input(mapdl, make_class_block): cpos = [ (0.3914, 0.4542, 0.7670), (0.0243, 0.0336, -0.0222), @@ -776,7 +783,7 @@ def test_cpos_input(mapdl, make_block): ) @staticmethod - def test_show_bounds(mapdl, make_block): + def test_show_bounds(mapdl, make_class_block): default_bounds = [-1.0, 1.0, -1.0, 1.0, -1.0, 1.0] pl = mapdl.eplot(show_bounds=True, return_plotter=True) @@ -786,7 +793,7 @@ def test_show_bounds(mapdl, make_block): pl.show() # plotting for catching @staticmethod - def test_background(mapdl, make_block): + def test_background(mapdl, make_class_block): default_color = "#4c4c4cff" pl = mapdl.eplot(background="red", return_plotter=True) assert pl.scene.background_color != default_color @@ -794,25 +801,25 @@ def test_background(mapdl, make_block): pl.show() # plotting for catching @staticmethod - def test_plot_nodal_values(mapdl, make_block): + def test_plot_nodal_values(mapdl, make_class_block): assert mapdl.post_processing.plot_nodal_values("U", "X") is None assert mapdl.post_processing.plot_nodal_values("U", "Y") is None assert mapdl.post_processing.plot_nodal_values("U", "Z") is None @staticmethod - def test_lsel_iterable(mapdl, make_block): + def test_lsel_iterable(mapdl, make_class_block): assert np.allclose( mapdl.lsel("S", "line", "", [1, 2, 3], "", ""), np.array([1, 2, 3]) ) @staticmethod - def test_asel_iterable(mapdl, make_block): + def test_asel_iterable(mapdl, make_class_block): assert np.allclose( mapdl.asel("S", "area", "", [1, 2, 3], "", ""), np.array([1, 2, 3]) ) @staticmethod - def test_vsel_iterable_and_kswp(mapdl, make_block): + def test_vsel_iterable_and_kswp(mapdl, make_class_block): mapdl.run("VGEN, 5, 1, , , 100, , , , , ") assert np.allclose( mapdl.vsel("S", "volu", "", [1, 2, 4], "", ""), np.array([1, 2, 4]) @@ -823,7 +830,7 @@ def test_vsel_iterable_and_kswp(mapdl, make_block): ) @staticmethod - def test_color_areas(mapdl, make_block): + def test_color_areas(mapdl, make_class_block): mapdl.aplot(color_areas=True) @staticmethod @@ -845,18 +852,18 @@ def test_color_areas(mapdl, make_block): ), ], ) - def test_color_areas_individual(mapdl, make_block, color_areas): + def test_color_areas_individual(mapdl, make_class_block, color_areas): # we do rely on the `pytest-pyvista` extension to deal with the differences mapdl.aplot(color_areas=color_areas) @staticmethod - def test_color_areas_error(mapdl, make_block): + def test_color_areas_error(mapdl, make_class_block): color_areas = ["red", "green", "blue"] with pytest.raises(ValueError): mapdl.aplot(color_areas=color_areas) @staticmethod - def test_WithInterativePlotting(mapdl, make_block): + def test_WithInterativePlotting(mapdl, make_class_block): mapdl.eplot(graphics_backend=GraphicsBackend.MAPDL) jobname = mapdl.jobname.upper() @@ -883,37 +890,37 @@ def filtering(file_name): @staticmethod @pytest.mark.parametrize("entity", ["KP", "LINE", "AREA", "VOLU", "NODE", "ELEM"]) - def test_cmplot_individual(mapdl, make_block, entity): + def test_cmplot_individual(mapdl, make_class_block, entity): mapdl.allsel() mapdl.cm("tmp_cm", entity=entity) mapdl.cmplot("tmp_cm") @staticmethod @pytest.mark.parametrize("label", ["N", "P"]) - def test_cmplot_label_error(mapdl, make_block, label): + def test_cmplot_label_error(mapdl, make_class_block, label): with pytest.raises(ValueError): mapdl.cmplot(label) @staticmethod - def test_cmplot_entity_error(mapdl, make_block): + def test_cmplot_entity_error(mapdl, make_class_block): with pytest.raises(ValueError): mapdl.cmplot("all", "non_valid_entity") @staticmethod - def test_cmplot_incorrect_entity(mapdl, make_block): + def test_cmplot_incorrect_entity(mapdl, make_class_block): mapdl.allsel() mapdl.cm("tmp_cm", entity="NODE") with pytest.raises(ValueError): mapdl.cmplot("tmp_cm", "KP") @staticmethod - def test_cmplot_component_not_exist(mapdl, make_block): + def test_cmplot_component_not_exist(mapdl, make_class_block): with pytest.raises(ComponentDoesNotExits): mapdl.cmplot("not_exist") @staticmethod @pytest.mark.parametrize("entity", ["KP", "NODE"]) - def test_cmplot_all(mapdl, make_block, entity): + def test_cmplot_all(mapdl, make_class_block, entity): mapdl.allsel() ids = np.array([1, 2, 3, 4]) if entity == "KP": @@ -936,24 +943,24 @@ def test_cmplot_all(mapdl, make_block, entity): @staticmethod @pytest.mark.parametrize("background", ["white", "black", "green", "red"]) - def test_labels_colors_background(mapdl, make_block, background): + def test_labels_colors_background(mapdl, make_class_block, background): # Test if the labels change color according background mapdl.nplot(background=background, nnum=True) @staticmethod - def test_vplot_show_volume_numbering(mapdl, make_block): + def test_vplot_show_volume_numbering(mapdl, make_class_block): mapdl.vplot(show_volume_numbering=True) @staticmethod - def test_vplot_area_numbering(mapdl, make_block): + def test_vplot_area_numbering(mapdl, make_class_block): mapdl.vplot(show_area_numbering=True) @staticmethod - def test_vplot_line_numbering(mapdl, make_block): + def test_vplot_line_numbering(mapdl, make_class_block): mapdl.vplot(show_line_numbering=True) @staticmethod - def test_vplot_multi_numbering(mapdl, make_block): + def test_vplot_multi_numbering(mapdl, make_class_block): mapdl.vplot( show_area_numbering=True, show_line_numbering=True, @@ -961,15 +968,15 @@ def test_vplot_multi_numbering(mapdl, make_block): ) @staticmethod - def test_vplot_color(mapdl, make_block): + def test_vplot_color(mapdl, make_class_block): mapdl.vplot(color="gray") @staticmethod - def test_vplot_cpos(mapdl, make_block): + def test_vplot_cpos(mapdl, make_class_block): mapdl.vplot(cpos="xy") @staticmethod - def test_vplot_multiargs(mapdl, make_block): + def test_vplot_multiargs(mapdl, make_class_block): mapdl.vplot( color="gray", cpos="xy", @@ -980,7 +987,7 @@ def test_vplot_multiargs(mapdl, make_block): @staticmethod @pytest.mark.parametrize("quality", [101, -2, 0, "as"]) - def test_aplot_quality_fail(mapdl, make_block, quality): + def test_aplot_quality_fail(mapdl, make_class_block, quality): with pytest.raises( ValueError, match="The argument 'quality' can only be an integer between 1 and 10", @@ -988,7 +995,7 @@ def test_aplot_quality_fail(mapdl, make_block, quality): mapdl.aplot(quality=quality) @staticmethod - def test_deprecated_params(mapdl, make_block): + def test_deprecated_params(mapdl, make_class_block): with pytest.warns( DeprecationWarning, match="'vtk' and 'use_vtk' are deprecated" ): From 81919f970deb8dd320ae0648f9a68c2ebe73ef84 Mon Sep 17 00:00:00 2001 From: germa89 <28149841+germa89@users.noreply.github.com> Date: Thu, 27 Nov 2025 17:06:53 +0100 Subject: [PATCH 4/6] refactor: not reusing MAPDL in some tests --- tests/test_plotting.py | 645 ++++++++++++++++++++--------------------- 1 file changed, 320 insertions(+), 325 deletions(-) diff --git a/tests/test_plotting.py b/tests/test_plotting.py index 892634e3374..ddc3a86cc23 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -390,167 +390,6 @@ def test_eplot_savefig(mapdl, make_class_block, tmpdir): ) assert os.path.isfile(filename) - @staticmethod - @pytest.mark.parametrize( - "selection", - ["S", "R", "A", "U"], - ) - def test_pick_nodes(mapdl, make_class_block, selection, verify_image_cache): - # Cleaning the model a bit - mapdl.modmsh("detach") # detaching geom and fem - mapdl.edele("all") - mapdl.nsel("s", "node", "", 1) - mapdl.nsel("a", "node", "", 2) - mapdl.nsel("inver") - mapdl.ndele("all") - - def debug_orders(pl, point): - pl = pl.scene - pl.show(auto_close=False) - pl.window_size = (100, 100) - width, height = pl.window_size - if pl._picking_right_clicking_observer is None: - pl.iren._mouse_left_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_left_button_release(width, height) - else: - pl.iren._mouse_right_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_right_button_release(width, height) - pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) - - mapdl.nsel("S", "node", "", 1) - if selection == "R" or selection == "U": - point = (285 / 1024, 280 / 800) - mapdl.nsel("a", "node", "", 2) - elif selection == "A": - point = (285 / 1024, 280 / 800) - else: - point = (0.5, 0.5) - - selected = mapdl.nsel( - selection, - "P", - _debug=lambda x: debug_orders(x, point=point), - tolerance=1, - ) # Selects node 2 - - assert isinstance(selected, (list, np.ndarray)) - if isinstance(selected, np.ndarray): - assert selected.all(), "Array is empty" - else: - assert selected, "List is empty" - assert len(selected) > 0, "The result has length zero" - - if selection != "U": - assert sorted(selected) == sorted( - mapdl._get_selected_("node") - ), "Order does not match" - - if selection in ["S", "R"]: - assert selected == [2], "Second node is not selected, nor the only one" - elif selection == "A": - assert 1 in selected, "Node 1 is not selected" - assert len(selected) > 1, "There should be at least two nodes" - elif selection == "U": - assert 2 not in selected, "Node 2 should not be selected." - assert 1 in selected, "Node 1 should be selected." - - mapdl.nplot() - - @staticmethod - @pytest.mark.skip(reason="Issues on CI/CD - will be addressed in another PR") - @pytest.mark.parametrize( - "selection", - ["S", "R", "A", "U"], - ) - def test_pick_kp(mapdl, make_class_block, selection): - # Cleaning the model a bit - mapdl.modmsh("detach") # detaching geom and fem - mapdl.vdele("all") - mapdl.adele("all") - mapdl.ldele("all") - mapdl.ksel("u", "kp", "", 1) - mapdl.ksel("u", "kp", "", 2) - mapdl.kdele("all") - mapdl.ksel("all") - - def debug_orders(pl, point): - pl = pl.scene - pl.show(auto_close=False) - pl.window_size = (100, 100) - width, height = pl.window_size - if pl._picking_right_clicking_observer is None: - pl.iren._mouse_left_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_left_button_release(width, height) - else: - pl.iren._mouse_right_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_right_button_release(width, height) - - pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) - - mapdl.ksel("S", "KP", "", 1) - if selection == "R" or selection == "U": - point = (285 / 1024, 280 / 800) - mapdl.ksel("a", "kp", "", 2) # Selects node 2 - elif selection == "A": - point = (285 / 1024, 280 / 800) - else: - point = (0.5, 0.5) - - selected = mapdl.ksel( - selection, - "P", - _debug=lambda x: debug_orders(x, point=point), - tolerance=1.0, - ) - - assert isinstance(selected, (list, np.ndarray)) - if isinstance(selected, np.ndarray): - assert selected.all() - else: - assert selected - assert len(selected) > 0 - if selection != "U": - assert sorted(selected) == sorted(mapdl._get_selected_("kp")) - - if selection == "S": - assert selected == [1] - elif selection == "R": - assert selected == [2] - elif selection == "A": - assert 1 in selected - assert 2 in selected - elif selection == "U": - assert 2 not in selected - assert 1 in selected - - @staticmethod - def test_pick_node_failure(mapdl, make_class_block): - # it should work for the KP too. - # Cleaning the model a bit - mapdl.modmsh("detach") # detaching geom and fem - mapdl.edele("all") - mapdl.nsel("s", "node", "", 1) - mapdl.nsel("a", "node", "", 2) - mapdl.nsel("inver") - mapdl.ndele("all") - - with pytest.raises(ValueError): - mapdl.nsel("X", "P") - - with pytest.raises(ValueError): - mapdl.nsel("S", "node", "", [1, 2, 3], 1) - - with pytest.raises(ValueError): - mapdl.nsel("S", "node", "", [1, 2, 3], "", 1) - @staticmethod def test_nsel_ksel_iterable_input(mapdl, make_class_block): # Testing using iterable (list/tuple/array) as vmin @@ -587,169 +426,6 @@ def test_nsel_ksel_iterable_input(mapdl, make_class_block): assert len(output) == 0 assert len(mapdl._get_selected_("node")) == 0 - @staticmethod - def test_pick_node_special_cases(mapdl, make_class_block): - # Cleaning the model a bit - mapdl.modmsh("detach") # detaching geom and fem - mapdl.edele("all") - mapdl.nsel("s", "node", "", 1) - mapdl.nsel("a", "node", "", 2) - mapdl.nsel("inver") - mapdl.ndele("all") - - # we pick nothing - def debug_orders_0(pl, point): - pl = pl.scene - pl.show(auto_close=False) - pl.window_size = (100, 100) - width, height = pl.window_size - pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) - - mapdl.nsel("S", "node", "", 1) - point = (285 / 1024, 280 / 800) - mapdl.nsel("a", "node", "", 2) - selected = mapdl.nsel( - "S", "P", _debug=lambda x: debug_orders_0(x, point=point), tolerance=0.2 - ) # Selects node 2 - assert selected == [] - assert np.allclose(mapdl._get_selected_("node"), [1, 2]) - - # we pick something already picked - # we just make sure the number is not repeated and there is no error. - def debug_orders_1(pl, point): - pl = pl.scene - pl.show(auto_close=False) - pl.window_size = (100, 100) - width, height = pl.window_size - # First click - pl.iren._mouse_left_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_left_button_release(width, height) - # Second click - pl.iren._mouse_left_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_left_button_release(width, height) - pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) - - mapdl.nsel("S", "node", "", 1) - point = (285 / 1024, 280 / 800) - mapdl.nsel("a", "node", "", 2) - selected = mapdl.nsel( - "S", "P", _debug=lambda x: debug_orders_1(x, point=point), tolerance=0.1 - ) # Selects node 2 - assert selected is not None - - @staticmethod - def test_pick_node_select_unselect_with_mouse(mapdl, make_class_block): - # Cleaning the model a bit - mapdl.modmsh("detach") # detaching geom and fem - mapdl.edele("all") - mapdl.nsel("s", "node", "", 1) - mapdl.nsel("a", "node", "", 2) - mapdl.nsel("inver") - mapdl.ndele("all") - - # we pick something already picked - # we just make sure the number is not repeated and there is no error. - def debug_orders_1(pl, point): - pl = pl.scene - pl.show(auto_close=False) - pl.window_size = (100, 100) - width, height = pl.window_size - # First click- selecting - pl.iren._mouse_left_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_left_button_release(width, height) - - pl.iren._simulate_keypress("u") # changing to unselecting - pl._inver_mouse_click_selection = True # making sure - - # Second click- unselecting - pl.iren._mouse_left_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_left_button_release(width, height) - pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) - # so we have selected nothing - - mapdl.nsel("S", "node", "", 1) - point = (285 / 1024, 280 / 800) - mapdl.nsel("a", "node", "", 2) - selected = mapdl.nsel( - "S", "P", _debug=lambda x: debug_orders_1(x, point=point), tolerance=0.1 - ) # Selects node 2 - assert selected == [] - - @staticmethod - @pytest.mark.parametrize( - "selection", - ["S", "R", "A", "U"], - ) - def test_pick_areas(mapdl, make_class_block, selection): - # Cleaning the model a bit - mapdl.modmsh("detach") # detaching geom and fem - mapdl.edele("all") - mapdl.asel("s", "area", "", 1) - mapdl.asel("a", "area", "", 2) - - def debug_orders(pl, point): - pl = pl.scene - pl.show(auto_close=False) - pl.window_size = (100, 100) - width, height = pl.window_size - if pl._picking_right_clicking_observer is None: - pl.iren._mouse_left_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_left_button_release(width, height) - else: - pl.iren._mouse_right_button_press( - int(width * point[0]), int(height * point[1]) - ) - pl.iren._mouse_right_button_release(width, height) - pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) - - mapdl.asel("S", "area", "", 1) - if selection == "R" or selection == "U": - point_to_pick = (285 / 1024, 280 / 800) - mapdl.asel("a", "area", "", 2) - elif selection == "A": - point_to_pick = (285 / 1024, 280 / 800) - else: - point_to_pick = (0.5, 0.5) - - selected = mapdl.asel( - selection, - "P", - "area", - _debug=lambda x: debug_orders(x, point=point_to_pick), - tolerance=0.2, - ) # Selects node 2 - - assert isinstance(selected, (list, np.ndarray)) - if isinstance(selected, np.ndarray): - assert selected.all() - else: - assert selected - assert len(selected) > 0 - - if selection != "U": - assert sorted(selected) == sorted(mapdl._get_selected_("area")) - - if selection == "S": - assert selected == [2] # area where the point clicks is area 2. - elif selection == "R": - assert selected == [1] # area where the point clicks is area 282. - elif selection == "A": - assert 6 in selected - assert len(selected) > 1 - elif selection == "U": - assert 282 not in selected - assert 2 in selected - @staticmethod @requires("pyvista") def test_plotter_input(mapdl, make_class_block): @@ -796,7 +472,6 @@ def test_show_bounds(mapdl, make_class_block): def test_background(mapdl, make_class_block): default_color = "#4c4c4cff" pl = mapdl.eplot(background="red", return_plotter=True) - assert pl.scene.background_color != default_color assert pl.scene.background_color == "red" pl.show() # plotting for catching @@ -894,6 +569,7 @@ def test_cmplot_individual(mapdl, make_class_block, entity): mapdl.allsel() mapdl.cm("tmp_cm", entity=entity) mapdl.cmplot("tmp_cm") + mapdl.cmdele("tmp_cm") @staticmethod @pytest.mark.parametrize("label", ["N", "P"]) @@ -912,6 +588,7 @@ def test_cmplot_incorrect_entity(mapdl, make_class_block): mapdl.cm("tmp_cm", entity="NODE") with pytest.raises(ValueError): mapdl.cmplot("tmp_cm", "KP") + mapdl.cmdele("tmp_cm") @staticmethod def test_cmplot_component_not_exist(mapdl, make_class_block): @@ -940,6 +617,8 @@ def test_cmplot_all(mapdl, make_class_block, entity): assert np.allclose(pl.meshes[0].points, ent[ids - 1]) pl.show() + mapdl.cmdele("tmp_cm1") + mapdl.cmdele("tmp_cm2") @staticmethod @pytest.mark.parametrize("background", ["white", "black", "green", "red"]) @@ -1394,3 +1073,319 @@ def test_plvar(mapdl, coupled_example): with patch("ansys.mapdl.core.Mapdl.screenshot") as mock_screenshot: mapdl.plvar(4, 5) mock_screenshot.assert_called_once() + + +@pytest.mark.parametrize( + "selection", + ["S", "R", "A", "U"], +) +def test_pick_areas(mapdl, make_block, selection): + # Cleaning the model a bit + mapdl.modmsh("detach") # detaching geom and fem + mapdl.edele("all") + mapdl.asel("s", "area", "", 1) + mapdl.asel("a", "area", "", 2) + + def debug_orders(pl, point): + pl = pl.scene + pl.show(auto_close=False) + pl.window_size = (100, 100) + width, height = pl.window_size + if pl._picking_right_clicking_observer is None: + pl.iren._mouse_left_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_left_button_release(width, height) + else: + pl.iren._mouse_right_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_right_button_release(width, height) + pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) + + mapdl.asel("S", "area", "", 1) + if selection == "R" or selection == "U": + point_to_pick = (285 / 1024, 280 / 800) + mapdl.asel("a", "area", "", 2) + elif selection == "A": + point_to_pick = (285 / 1024, 280 / 800) + else: + point_to_pick = (0.5, 0.5) + + selected = mapdl.asel( + selection, + "P", + "area", + _debug=lambda x: debug_orders(x, point=point_to_pick), + tolerance=0.2, + ) # Selects node 2 + + assert isinstance(selected, (list, np.ndarray)) + if isinstance(selected, np.ndarray): + assert selected.all() + else: + assert selected + assert len(selected) > 0 + + if selection != "U": + assert sorted(selected) == sorted(mapdl._get_selected_("area")) + + if selection == "S": + assert selected == [2] # area where the point clicks is area 2. + elif selection == "R": + assert selected == [1] # area where the point clicks is area 282. + elif selection == "A": + assert 6 in selected + assert len(selected) > 1 + elif selection == "U": + assert 282 not in selected + assert 2 in selected + + +@pytest.mark.parametrize( + "selection", + ["S", "R", "A", "U"], +) +def test_pick_nodes(mapdl, make_block, selection, verify_image_cache): + # Cleaning the model a bit + mapdl.modmsh("detach") # detaching geom and fem + mapdl.edele("all") + mapdl.nsel("s", "node", "", 1) + mapdl.nsel("a", "node", "", 2) + mapdl.nsel("inver") + mapdl.ndele("all") + + def debug_orders(pl, point): + pl = pl.scene + pl.show(auto_close=False) + pl.window_size = (100, 100) + width, height = pl.window_size + if pl._picking_right_clicking_observer is None: + pl.iren._mouse_left_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_left_button_release(width, height) + else: + pl.iren._mouse_right_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_right_button_release(width, height) + pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) + + mapdl.nsel("S", "node", "", 1) + if selection == "R" or selection == "U": + point = (285 / 1024, 280 / 800) + mapdl.nsel("a", "node", "", 2) + elif selection == "A": + point = (285 / 1024, 280 / 800) + else: + point = (0.5, 0.5) + + selected = mapdl.nsel( + selection, + "P", + _debug=lambda x: debug_orders(x, point=point), + tolerance=1, + ) # Selects node 2 + + assert isinstance(selected, (list, np.ndarray)) + if isinstance(selected, np.ndarray): + assert selected.all(), "Array is empty" + else: + assert selected, "List is empty" + assert len(selected) > 0, "The result has length zero" + + if selection != "U": + assert sorted(selected) == sorted( + mapdl._get_selected_("node") + ), "Order does not match" + + if selection in ["S", "R"]: + assert selected == [2], "Second node is not selected, nor the only one" + elif selection == "A": + assert 1 in selected, "Node 1 is not selected" + assert len(selected) > 1, "There should be at least two nodes" + elif selection == "U": + assert 2 not in selected, "Node 2 should not be selected." + assert 1 in selected, "Node 1 should be selected." + + mapdl.nplot() + + +@pytest.mark.skip(reason="Issues on CI/CD - will be addressed in another PR") +@pytest.mark.parametrize( + "selection", + ["S", "R", "A", "U"], +) +def test_pick_kp(mapdl, make_block, selection): + # Cleaning the model a bit + mapdl.modmsh("detach") # detaching geom and fem + mapdl.vdele("all") + mapdl.adele("all") + mapdl.ldele("all") + mapdl.ksel("u", "kp", "", 1) + mapdl.ksel("u", "kp", "", 2) + mapdl.kdele("all") + mapdl.ksel("all") + + def debug_orders(pl, point): + pl = pl.scene + pl.show(auto_close=False) + pl.window_size = (100, 100) + width, height = pl.window_size + if pl._picking_right_clicking_observer is None: + pl.iren._mouse_left_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_left_button_release(width, height) + else: + pl.iren._mouse_right_button_press( + int(width * point[0]), int(height * point[1]) + ) + pl.iren._mouse_right_button_release(width, height) + + pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) + + mapdl.ksel("S", "KP", "", 1) + if selection == "R" or selection == "U": + point = (285 / 1024, 280 / 800) + mapdl.ksel("a", "kp", "", 2) # Selects node 2 + elif selection == "A": + point = (285 / 1024, 280 / 800) + else: + point = (0.5, 0.5) + + selected = mapdl.ksel( + selection, + "P", + _debug=lambda x: debug_orders(x, point=point), + tolerance=1.0, + ) + + assert isinstance(selected, (list, np.ndarray)) + if isinstance(selected, np.ndarray): + assert selected.all() + else: + assert selected + assert len(selected) > 0 + if selection != "U": + assert sorted(selected) == sorted(mapdl._get_selected_("kp")) + + if selection == "S": + assert selected == [1] + elif selection == "R": + assert selected == [2] + elif selection == "A": + assert 1 in selected + assert 2 in selected + elif selection == "U": + assert 2 not in selected + assert 1 in selected + + +def test_pick_node_failure(mapdl, make_block): + # it should work for the KP too. + # Cleaning the model a bit + mapdl.modmsh("detach") # detaching geom and fem + mapdl.edele("all") + mapdl.nsel("s", "node", "", 1) + mapdl.nsel("a", "node", "", 2) + mapdl.nsel("inver") + mapdl.ndele("all") + + with pytest.raises(ValueError): + mapdl.nsel("X", "P") + + with pytest.raises(ValueError): + mapdl.nsel("S", "node", "", [1, 2, 3], 1) + + with pytest.raises(ValueError): + mapdl.nsel("S", "node", "", [1, 2, 3], "", 1) + + +def test_pick_node_special_cases(mapdl, make_block): + # Cleaning the model a bit + mapdl.modmsh("detach") # detaching geom and fem + mapdl.edele("all") + mapdl.nsel("s", "node", "", 1) + mapdl.nsel("a", "node", "", 2) + mapdl.nsel("inver") + mapdl.ndele("all") + + # we pick nothing + def debug_orders_0(pl, point): + pl = pl.scene + pl.show(auto_close=False) + pl.window_size = (100, 100) + width, height = pl.window_size + pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) + + mapdl.nsel("S", "node", "", 1) + point = (285 / 1024, 280 / 800) + mapdl.nsel("a", "node", "", 2) + selected = mapdl.nsel( + "S", "P", _debug=lambda x: debug_orders_0(x, point=point), tolerance=0.2 + ) # Selects node 2 + assert selected == [] + assert np.allclose(mapdl._get_selected_("node"), [1, 2]) + + # we pick something already picked + # we just make sure the number is not repeated and there is no error. + def debug_orders_1(pl, point): + pl = pl.scene + pl.show(auto_close=False) + pl.window_size = (100, 100) + width, height = pl.window_size + # First click + pl.iren._mouse_left_button_press(int(width * point[0]), int(height * point[1])) + pl.iren._mouse_left_button_release(width, height) + # Second click + pl.iren._mouse_left_button_press(int(width * point[0]), int(height * point[1])) + pl.iren._mouse_left_button_release(width, height) + pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) + + mapdl.nsel("S", "node", "", 1) + point = (285 / 1024, 280 / 800) + mapdl.nsel("a", "node", "", 2) + selected = mapdl.nsel( + "S", "P", _debug=lambda x: debug_orders_1(x, point=point), tolerance=0.1 + ) # Selects node 2 + assert selected is not None + + +def test_pick_node_select_unselect_with_mouse(mapdl, make_block): + # Cleaning the model a bit + mapdl.modmsh("detach") # detaching geom and fem + mapdl.edele("all") + mapdl.nsel("s", "node", "", 1) + mapdl.nsel("a", "node", "", 2) + mapdl.nsel("inver") + mapdl.ndele("all") + + # we pick something already picked + # we just make sure the number is not repeated and there is no error. + def debug_orders_1(pl, point): + pl = pl.scene + pl.show(auto_close=False) + pl.window_size = (100, 100) + width, height = pl.window_size + # First click- selecting + pl.iren._mouse_left_button_press(int(width * point[0]), int(height * point[1])) + pl.iren._mouse_left_button_release(width, height) + + pl.iren._simulate_keypress("u") # changing to unselecting + pl._inver_mouse_click_selection = True # making sure + + # Second click- unselecting + pl.iren._mouse_left_button_press(int(width * point[0]), int(height * point[1])) + pl.iren._mouse_left_button_release(width, height) + pl.iren._mouse_move(int(width * point[0]), int(height * point[1])) + # so we have selected nothing + + mapdl.nsel("S", "node", "", 1) + point = (285 / 1024, 280 / 800) + mapdl.nsel("a", "node", "", 2) + selected = mapdl.nsel( + "S", "P", _debug=lambda x: debug_orders_1(x, point=point), tolerance=0.1 + ) # Selects node 2 + assert selected == [] From ac537b666bde8ce843739f11422587dc2359c141 Mon Sep 17 00:00:00 2001 From: germa89 <28149841+germa89@users.noreply.github.com> Date: Thu, 27 Nov 2025 17:24:48 +0100 Subject: [PATCH 5/6] refactor: update plotting tests to allow empty meshes and reorganize test_vsel_iterable_and_kswp --- tests/test_plotting.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tests/test_plotting.py b/tests/test_plotting.py index ddc3a86cc23..eae802cc3e3 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -33,7 +33,10 @@ pytest.skip( allow_module_level=True, reason="Skipping because 'pyvista' is not installed" ) +else: + import pyvista as pv + pv.global_theme.allow_empty_mesh = True from ansys.mapdl.core.errors import ComponentDoesNotExits, MapdlRuntimeError from ansys.mapdl.core.plotting import GraphicsBackend @@ -470,7 +473,6 @@ def test_show_bounds(mapdl, make_class_block): @staticmethod def test_background(mapdl, make_class_block): - default_color = "#4c4c4cff" pl = mapdl.eplot(background="red", return_plotter=True) assert pl.scene.background_color == "red" pl.show() # plotting for catching @@ -493,17 +495,6 @@ def test_asel_iterable(mapdl, make_class_block): mapdl.asel("S", "area", "", [1, 2, 3], "", ""), np.array([1, 2, 3]) ) - @staticmethod - def test_vsel_iterable_and_kswp(mapdl, make_class_block): - mapdl.run("VGEN, 5, 1, , , 100, , , , , ") - assert np.allclose( - mapdl.vsel("S", "volu", "", [1, 2, 4], "", ""), np.array([1, 2, 4]) - ) - mapdl.vsel("S", "volu", "", [1], "", "", kswp=1) - assert np.allclose(mapdl.geometry.vnum, [1]) and np.allclose( - mapdl.geometry.anum, [1, 2, 3, 4, 5, 6] - ) - @staticmethod def test_color_areas(mapdl, make_class_block): mapdl.aplot(color_areas=True) @@ -1389,3 +1380,15 @@ def debug_orders_1(pl, point): "S", "P", _debug=lambda x: debug_orders_1(x, point=point), tolerance=0.1 ) # Selects node 2 assert selected == [] + + +def test_vsel_iterable_and_kswp(mapdl, make_block): + mapdl.run("VGEN, 5, 1, , , 100, , , , , ") + assert np.allclose( + mapdl.vsel("S", "volu", "", [1, 2, 4], "", ""), np.array([1, 2, 4]) + ) + mapdl.vsel("S", "volu", "", [1], "", "", kswp=1) + assert np.allclose(mapdl.geometry.vnum, [1]) and np.allclose( + mapdl.geometry.anum, [1, 2, 3, 4, 5, 6] + ) + mapdl.allsel() From b3195d5c080a7002139e454489b5ed683b005a9f Mon Sep 17 00:00:00 2001 From: germa89 <28149841+germa89@users.noreply.github.com> Date: Thu, 27 Nov 2025 18:35:29 +0100 Subject: [PATCH 6/6] refactor: ensure all selections are cleared in TestPlottingMeshing --- tests/test_plotting.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_plotting.py b/tests/test_plotting.py index eae802cc3e3..32cc18cdb4c 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -428,6 +428,7 @@ def test_nsel_ksel_iterable_input(mapdl, make_class_block): elif isinstance(output, list): assert len(output) == 0 assert len(mapdl._get_selected_("node")) == 0 + mapdl.allsel() @staticmethod @requires("pyvista")