Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check kwarg in mapdl.run #2276

Merged
merged 12 commits into from
Sep 1, 2023
26 changes: 22 additions & 4 deletions src/ansys/mapdl/core/mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,26 +385,26 @@
"""Default file type for plots.

Use when device is not properly set, for instance when the device is closed."""
return self._default_file_type_for_plots

Check warning on line 388 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L388

Added line #L388 was not covered by tests

@default_file_type_for_plots.setter
def default_file_type_for_plots(self, value: VALID_FILE_TYPE_FOR_PLOT_LITERAL):
"""Set default file type for plots.

Used when device is not properly set, for instance when the device is closed."""
if not isinstance(value, str) and value.upper() not in VALID_FILE_TYPE_FOR_PLOT:
raise ValueError(f"'{value}' is not allowed as file output for plots.")
return self._default_file_type_for_plots

Check warning on line 397 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L395-L397

Added lines #L395 - L397 were not covered by tests

@property
def use_vtk(self):
"""Returns if using VTK by default or not."""
return self._use_vtk

Check warning on line 402 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L402

Added line #L402 was not covered by tests

@use_vtk.setter
def use_vtk(self, value: bool):
"""Set VTK to be used by default or not."""
self._use_vtk = value

Check warning on line 407 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L407

Added line #L407 was not covered by tests

def _wrap_listing_functions(self):
# Wrapping LISTING FUNCTIONS.
Expand Down Expand Up @@ -980,7 +980,7 @@
from ansys.mapdl.core.mapdl_geometry import Geometry, LegacyGeometry

if self.legacy_geometry:
return LegacyGeometry

Check warning on line 983 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L983

Added line #L983 was not covered by tests
else:
return Geometry(self)

Expand Down Expand Up @@ -1912,7 +1912,7 @@

elif isinstance(color_areas, str):
# A color is provided as a string
colors = np.atleast_2d(to_rgba(color_areas))

Check warning on line 1915 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L1915

Added line #L1915 was not covered by tests

else:
if len(anums) != len(color_areas):
Expand All @@ -1923,10 +1923,10 @@
f"\ncolor_areas: {color_areas}"
)

if isinstance(color_areas[0], str):
colors = [to_rgba(each) for each in color_areas]

Check warning on line 1927 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L1926-L1927

Added lines #L1926 - L1927 were not covered by tests
else:
colors = color_areas

Check warning on line 1929 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L1929

Added line #L1929 was not covered by tests

# Creating a mapping of colors
ent_num = []
Expand All @@ -1948,9 +1948,9 @@
if show_area_numbering:
centers = []

for surf in surfs:
anum = np.unique(surf["entity_num"])
assert (

Check warning on line 1953 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L1951-L1953

Added lines #L1951 - L1953 were not covered by tests
len(anum) == 1
), f"The pv.Unstructured from the entity {anum[0]} contains entities from other entities {anum}" # Sanity check

Expand All @@ -1965,7 +1965,7 @@
self.cm("__area__", "AREA", mute=True)
self.lsla("S", mute=True)

lines = self.geometry.get_lines()

Check warning on line 1968 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L1968

Added line #L1968 was not covered by tests
self.cmsel("S", "__area__", "AREA", mute=True)

if show_lines:
Expand Down Expand Up @@ -2025,16 +2025,16 @@
self.previous_device = self._parent().file_type_for_plots

if self._parent().file_type_for_plots not in ["PNG", "TIFF", "PNG", "VRML"]:
self._parent().show(self._parent().default_file_type_for_plots)

Check warning on line 2028 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L2028

Added line #L2028 was not covered by tests

def __exit__(self, *args) -> None:
self._parent()._log.debug("Exiting in 'WithInterativePlotting' mode")
self._parent().show("close", mute=True)
if not self._parent()._png_mode:
self._parent().show("PNG", mute=True)
self._parent().gfile(self._pixel_res, mute=True)

Check warning on line 2035 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L2031-L2035

Added lines #L2031 - L2035 were not covered by tests

self._parent().file_type_for_plots = self.previous_device

Check warning on line 2037 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L2037

Added line #L2037 was not covered by tests

def __exit__(self, *args) -> None:
self._parent()._log.debug("Exiting in 'WithInterativePlotting' mode")
Expand Down Expand Up @@ -2167,7 +2167,7 @@
meshes = []

if color_lines:
size_ = len(lines)

Check warning on line 2170 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L2170

Added line #L2170 was not covered by tests
# Because this is only going to be used for plotting
# purposes, we don't need to allocate
# a huge vector with random numbers (colours).
Expand All @@ -2177,24 +2177,24 @@
# to 256.
#
# Link: https://docs.pyvista.org/api/plotting/_autosummary/pyvista.DataSetMapper.set_scalars.html#pyvista.DataSetMapper.set_scalars
size_ = min([256, size_])

Check warning on line 2180 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L2180

Added line #L2180 was not covered by tests
# Generating a colour array,
# Size = number of areas.
# Values are random between 0 and min(256, number_areas)
colors = get_ansys_colors(size_)

Check warning on line 2184 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L2184

Added line #L2184 was not covered by tests

# Creating a mapping of colors
ent_num = []
for line in lines:
ent_num.append(int(np.unique(line["entity_num"])[0]))
ent_num.sort()

Check warning on line 2190 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L2187-L2190

Added lines #L2187 - L2190 were not covered by tests

# expand color array until matching the number of areas.
# In this case we start to repeat colors in the same order.
colors = np.resize(colors, (len(ent_num), 4))

Check warning on line 2194 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L2194

Added line #L2194 was not covered by tests

for line, color in zip(lines, colors):
meshes.append({"mesh": line, "color": color})

Check warning on line 2197 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L2196-L2197

Added lines #L2196 - L2197 were not covered by tests

else:
for line in lines:
Expand Down Expand Up @@ -3204,11 +3204,31 @@
if "\n" in command or "\r" in command:
raise ValueError("Use ``input_strings`` for multi-line commands")

# check if we want to avoid the current non-interactive context.
# Check kwargs
verbose = kwargs.pop("verbose", False)
save_fig = kwargs.pop("savefig", False)

# Check if you want to avoid the current non-interactive context.
avoid_non_interactive = kwargs.pop("avoid_non_interactive", False)

# Check if there is an unused keyword argument. If there is, it
# might be because you wrote a wrong argument name.
#
# Remove empty string kwargs
for key, value in list(kwargs.items()):
germa89 marked this conversation as resolved.
Show resolved Hide resolved
if value == "":
kwargs.pop(key)

if kwargs:
warn(
"The following keyword arguments are not used:\n"
f"{', '.join(kwargs.keys())}\n"
"Make sure you are using the intended keyword arguments.",
UserWarning,
)

if self._store_commands and not avoid_non_interactive:
# If we are using NBLOCK on input, we should not strip the string
# If you are using NBLOCK on input, you should not strip the string
self._stored_commands.append(command)
return

Expand Down Expand Up @@ -3267,7 +3287,6 @@
# Edge case. `\title, 'par=1234' `
self._check_parameter_name(param_name)

verbose = kwargs.get("verbose", False)
text = self._run(command, verbose=verbose, mute=mute)

if command[:4].upper() == "/CLE" and self.is_grpc:
Expand All @@ -3294,7 +3313,6 @@
if short_cmd in PLOT_COMMANDS:
self._log.debug("It is a plot command.")
plot_path = self._get_plot_name(text)
save_fig = kwargs.get("savefig", False)
if save_fig:
return self._download_plot(plot_path, save_fig)
else:
Expand Down Expand Up @@ -3793,7 +3811,7 @@
else: # pragma: no cover
self._log.error("Unable to find screenshot at %s", filename)
else:
self._log.error("Unable to find file in MAPDL command output.")

Check warning on line 3814 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L3814

Added line #L3814 was not covered by tests

def _display_plot(self, filename: str) -> None:
"""Display the last generated plot (*.png) from MAPDL"""
Expand All @@ -3805,7 +3823,7 @@
# to avoid dependency here.
try:
__IPYTHON__
return True

Check warning on line 3826 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L3826

Added line #L3826 was not covered by tests
except NameError: # pragma: no cover
return False

Expand All @@ -3819,10 +3837,10 @@
plt.show() # consider in-line plotting

if in_ipython():
self._log.debug("Using ipython")
from IPython.display import display

Check warning on line 3841 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L3840-L3841

Added lines #L3840 - L3841 were not covered by tests

display(plt.gcf())

Check warning on line 3843 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L3843

Added line #L3843 was not covered by tests

def _download_plot(self, filename: str, plot_name: str) -> None:
"""Copy the temporary download plot to the working directory."""
Expand Down Expand Up @@ -4142,9 +4160,9 @@
# to 40 characters
picked_entities_str = str(picked_entities[::-1])[1:-1]
if len(picked_entities_str) > 40:
picked_entities_str = picked_entities_str[:40]
idx = picked_entities_str.rfind(",") + 2
picked_entities_str = picked_entities_str[:idx] + "..."

Check warning on line 4165 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L4163-L4165

Added lines #L4163 - L4165 were not covered by tests

return text + f"Current {entity} selection: {picked_entities_str}"

Expand All @@ -4163,8 +4181,8 @@
picked_ids.append(id_)
else:
# Updating MAPDL entity mapping
if node_id in picked_entities:
picked_entities.remove(node_id)

Check warning on line 4185 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L4184-L4185

Added lines #L4184 - L4185 were not covered by tests
# Updating pyvista entity mapping
if id_ in picked_ids:
picked_ids.remove(id_)
Expand All @@ -4186,7 +4204,7 @@
reset_camera=False,
)
else:
pl.remove_actor("_picked_entities")

Check warning on line 4207 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L4207

Added line #L4207 was not covered by tests

def callback_mesh(mesh):
def get_entnum(mesh):
Expand Down Expand Up @@ -4215,18 +4233,18 @@

else:
# Updating MAPDL entity mapping
if mesh_id in picked_entities:
picked_entities.remove(mesh_id)

Check warning on line 4237 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L4236-L4237

Added lines #L4236 - L4237 were not covered by tests

for i, each in enumerate(meshes):
pl.remove_actor(f"_picked_entity_{mesh_id}_{i}")

Check warning on line 4240 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L4239-L4240

Added lines #L4239 - L4240 were not covered by tests

# Removing only-first time actors
pl.remove_actor("title")
pl.remove_actor("_point_picking_message")

if "_entity_picking_message" in pl.actors:
pl.remove_actor("_entity_picking_message")

Check warning on line 4247 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L4247

Added line #L4247 was not covered by tests

pl._picking_text = pl.add_text(
gen_text(picked_entities),
Expand Down Expand Up @@ -4259,7 +4277,7 @@
def callback_u():
# inverting bool
pl._inver_mouse_click_selection = not pl._inver_mouse_click_selection
pl.remove_actor("_entity_picking_message")

Check warning on line 4280 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L4280

Added line #L4280 was not covered by tests

pl._picking_text = pl.add_text(
gen_text(picked_entities),
Expand Down Expand Up @@ -4837,7 +4855,7 @@
raise ValueError(f"The label '{label}' is not supported.")

if (not label or label == "ALL") and not entity:
raise ValueError(

Check warning on line 4858 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L4858

Added line #L4858 was not covered by tests
f"If not using label or label =='ALL', then you "
"need to provide a valid entity."
)
Expand All @@ -4860,7 +4878,7 @@

if label and not entity:
# supposing entity
entity = self.components[label].type

Check warning on line 4881 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L4881

Added line #L4881 was not covered by tests

if entity[:4] not in ["NODE", "ELEM", "KP", "LINE", "AREA", "VOLU"]:
raise ValueError(f"The entity '{entity}' is not allowed.")
Expand Down
8 changes: 4 additions & 4 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2211,7 +2211,7 @@

else: # remote session
if recursive:
warn(

Check warning on line 2214 in src/ansys/mapdl/core/mapdl_grpc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_grpc.py#L2214

Added line #L2214 was not covered by tests
"The 'recursive' parameter is ignored if the session is non-local."
)
return self._download_from_remote(
Expand Down Expand Up @@ -2303,7 +2303,7 @@

elif isinstance(files, list):
if not all([isinstance(each, str) for each in files]):
raise ValueError(

Check warning on line 2306 in src/ansys/mapdl/core/mapdl_grpc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_grpc.py#L2306

Added line #L2306 was not covered by tests
"The parameter `'files'` can be a list or tuple, but it should only contain strings."
)
list_files = []
Expand All @@ -2311,7 +2311,7 @@
list_files.extend(self._validate_files(each, extension=extension))

else:
raise ValueError(

Check warning on line 2314 in src/ansys/mapdl/core/mapdl_grpc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_grpc.py#L2314

Added line #L2314 was not covered by tests
f"The `file` parameter type ({type(files)}) is not supported."
"Only strings, tuple of strings or list of strings are allowed."
)
Expand All @@ -2331,7 +2331,7 @@
) -> List[str]:
if extension is not None:
if not isinstance(extension, str):
raise TypeError(f"The extension {extension} must be a string.")

Check warning on line 2334 in src/ansys/mapdl/core/mapdl_grpc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_grpc.py#L2334

Added line #L2334 was not covered by tests

if not extension.startswith("."):
extension = "." + extension
Expand Down Expand Up @@ -2832,7 +2832,7 @@
if not error_file:
return None

if self._local:

Check warning on line 2835 in src/ansys/mapdl/core/mapdl_grpc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_grpc.py#L2835

Added line #L2835 was not covered by tests
return open(os.path.join(self.directory, error_file)).read()
elif self._exited:
raise MapdlExitedError(
Expand Down Expand Up @@ -3038,7 +3038,7 @@
file_, ext_, _ = self._decompose_fname(fname)
fname = fname[: -len(ext_) - 1] # Removing extension. -1 for the dot.
if self._local:
return self._file(filename=fname, extension=ext_, **kwargs)

Check warning on line 3041 in src/ansys/mapdl/core/mapdl_grpc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_grpc.py#L3041

Added line #L3041 was not covered by tests
else:
return self._file(filename=file_, extension=ext_)

Expand Down Expand Up @@ -3110,7 +3110,7 @@
comp=comp,
name=name,
sector=sector,
kwargs=kwargs,
**kwargs,
)
return self.vget("_temp", nvar)

Expand All @@ -3133,7 +3133,7 @@
item=item,
comp=comp,
name=name,
kwargs=kwargs,
**kwargs,
)
return self.vget("_temp", nvar)

Expand Down Expand Up @@ -3203,7 +3203,7 @@
comp=comp,
name=name,
sector=sector,
kwargs=kwargs,
**kwargs,
)

def get_esol(
Expand Down Expand Up @@ -3317,7 +3317,7 @@
comp=comp,
name=name,
sector=sector,
kwargs=kwargs,
**kwargs,
)
# Using get_variable because it deletes the intermediate parameter after using it.
return self.get_variable(VAR_IR, tstrt=tstrt, kcplx=kcplx)
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ def coupled_example(mapdl, cleared):
mapdl_code = mapdl_code.replace(
"SOLVE", "SOLVE\n/COM Ending script after first simulation\n/EOF"
)
mapdl.finish()
mapdl.input_strings(mapdl_code)


Expand Down
20 changes: 20 additions & 0 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2129,3 +2129,23 @@ def test_distributed(mapdl):
assert mapdl._distributed
else:
assert not mapdl._distributed # assuming remote is using -smp


def test_non_used_kwargs(mapdl):
with pytest.warns(UserWarning):
mapdl.prep7(non_valid_argument=2)

with pytest.warns(UserWarning):
mapdl.run("/prep7", True, False, unvalid_argument=2)

kwarg = {"unvalid_argument": 2}
with pytest.warns(UserWarning):
mapdl.run("/prep7", True, None, **kwarg)


def test_non_valid_kwarg(mapdl):
mapdl.prep7()
mapdl.blc4(0, 0, 1, 1, 1)

with pytest.warns(UserWarning):
mapdl.cdwrite(options="DB", fname="test1", ext="cdb")
Loading