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

New listThermalMaps() API #200

Merged
merged 13 commits into from
Jan 25, 2024
17 changes: 17 additions & 0 deletions src/ansys/sherlock/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,3 +909,20 @@

assert self.error_array is None
return [f"Update part from AVL error: {self.message}"]


class SherlockListThermalMapsError(Exception):
"""Contains the errors raised when thermal map files for a project cannot be listed."""

def __init__(self, message=None, error_array=None):
"""Initialize error message."""
self.message = message
self.error_array = error_array

def str_itr(self):
"""Format error message."""
if self.message is None:
return [f"List thermal maps error: {error}" for error in self.error_array]

Check warning on line 925 in src/ansys/sherlock/core/errors.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/sherlock/core/errors.py#L925

Added line #L925 was not covered by tests

assert self.error_array is None
return [f"List thermal maps error: {self.message}"]
59 changes: 59 additions & 0 deletions src/ansys/sherlock/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
SherlockImportODBError,
SherlockListCCAsError,
SherlockListStrainMapsError,
SherlockListThermalMapsError,
)
from ansys.sherlock.core.grpc_stub import GrpcStub

Expand Down Expand Up @@ -763,3 +764,61 @@
raise SherlockAddProjectError(return_code.message)

return return_code.value

def list_thermal_maps(self, project, cca_names=None):
"""List the thermal map files and their type assigned to each CCA of given CCAs.

Parameters
----------
project: str
Name of the Sherlock project.
cca_names : List of str, optional
List of CCA names to provide thermal maps for. The default is ``None``,
in which case all CCAs in the project are returned.

Returns
-------
list
All thermal map files or thermal map files and their type for the specified CCAs.

Examples
--------
>>> from ansys.sherlock.core.launcher import launch_sherlock
>>> sherlock = launch_sherlock()
>>> thermal_maps = sherlock.project.list_thermal_maps(
"AssemblyTutorial",
["Main Board","Power Module"]
)
"""
try:
if project == "":
raise SherlockListThermalMapsError(message="Project name is invalid.")

if cca_names is not None and type(cca_names) is not list:
raise SherlockListThermalMapsError(message="cca_names is not a list.")

if not self._is_connection_up():
LOG.error("There is no connection to a gRPC service.")
return

Check warning on line 802 in src/ansys/sherlock/core/project.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/sherlock/core/project.py#L800-L802

Added lines #L800 - L802 were not covered by tests

request = SherlockProjectService_pb2.ListThermalMapsRequest(project=project)

Check warning on line 804 in src/ansys/sherlock/core/project.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/sherlock/core/project.py#L804

Added line #L804 was not covered by tests

if cca_names is not None:
for cca_name in cca_names:
request.cca.append(cca_name)

Check warning on line 808 in src/ansys/sherlock/core/project.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/sherlock/core/project.py#L806-L808

Added lines #L806 - L808 were not covered by tests

response = self.stub.listThermalMaps(request)

Check warning on line 810 in src/ansys/sherlock/core/project.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/sherlock/core/project.py#L810

Added line #L810 was not covered by tests

return_code = response.returnCode

Check warning on line 812 in src/ansys/sherlock/core/project.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/sherlock/core/project.py#L812

Added line #L812 was not covered by tests

if return_code.value == -1:
if return_code.message == "":
raise SherlockListThermalMapsError(error_array=response.errors)

Check warning on line 816 in src/ansys/sherlock/core/project.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/sherlock/core/project.py#L814-L816

Added lines #L814 - L816 were not covered by tests

raise SherlockListThermalMapsError(message=return_code.message)

Check warning on line 818 in src/ansys/sherlock/core/project.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/sherlock/core/project.py#L818

Added line #L818 was not covered by tests

except SherlockListThermalMapsError as e:
LOG.error(str(e))
raise e

return response.ccaThermalMaps

Check warning on line 824 in src/ansys/sherlock/core/project.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/sherlock/core/project.py#L824

Added line #L824 was not covered by tests
71 changes: 71 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
SherlockImportODBError,
SherlockListCCAsError,
SherlockListStrainMapsError,
SherlockListThermalMapsError,
)
from ansys.sherlock.core.project import Project

Expand All @@ -38,6 +39,7 @@ def test_all():
helper_test_list_ccas(project)
helper_test_add_cca(project)
helper_test_list_strain_maps(project)
helper_test_list_thermal_maps(project)
project_name = None
try:
project_name = helper_test_add_project(project)
Expand Down Expand Up @@ -598,6 +600,75 @@ def helper_test_add_project(project):
pytest.fail(str(e))


def helper_test_list_thermal_maps(project):
"""Test list_thermal_maps API"""

expected_cca_name = "Main Board"
expected_file_names = [
"Thermal Map.xlsx",
"Thermal Map.tmap",
"Thermal Image.jpg",
"Thermal Map.csv",
]
expected_file_types = [
"Thermal Map (Excel)",
"Icepak Thermal Map (TMAP)",
"Thermal Map (Image)",
"Thermal Map (CSV)",
]

try:
project.list_thermal_maps("")
pytest.fail("No exception raised when using an invalid parameter")
except SherlockListThermalMapsError as e:
assert str(e.str_itr()) == "['List thermal maps error: Project name is invalid.']"

try:
project.list_thermal_maps("Tutorial Project", "Not a list")
pytest.fail("No exception raised when using an invalid parameter")
except SherlockListThermalMapsError as e:
assert str(e.str_itr()) == "['List thermal maps error: cca_names is not a list.']"

if project._is_connection_up():
try:
project.list_thermal_maps("AssemblyTutorial", ["CCA name that doesn't exist"])
pytest.fail("No exception raised when using an invalid parameter")
except Exception as e:
assert type(e) == SherlockListThermalMapsError

try:
thermal_maps = project.list_thermal_maps("Tutorial Project", [expected_cca_name])
assert len(thermal_maps) == 1
thermal_map = thermal_maps[0]
assert thermal_map.ccaName == expected_cca_name
assert len(thermal_map.thermalMaps) == len(expected_file_names)

for i, thermal_map_info in enumerate(thermal_map.thermalMaps):
assert expected_file_names[i] == thermal_map_info.fileName
assert expected_file_types[i] == thermal_map_info.fileType
except SherlockListThermalMapsError as e:
pytest.fail(str(e.str_itr()))

try:
thermal_maps = project.list_thermal_maps("Tutorial Project")

for thermal_map in thermal_maps:
assert hasattr(thermal_map, "ccaName") and hasattr(thermal_map, "thermalMaps")

if thermal_map.thermalMaps:
for i, thermal_map_info in enumerate(thermal_map.thermalMaps):
assert hasattr(thermal_map_info, "fileName") and hasattr(
thermal_map_info, "fileType"
)

if thermal_map.ccaName == expected_cca_name:
assert expected_file_names[i] == thermal_map_info.fileName
assert expected_file_types[i] == thermal_map_info.fileType

except SherlockListThermalMapsError as e:
pytest.fail(str(e.str_itr()))


def clean_up_after_add(project, project_name):
if project_name is not None:
project.delete_project(project_name)
Expand Down
Loading