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

Update ResultInfo and AvailableResult print #978

Merged
merged 8 commits into from
Jun 12, 2023
19 changes: 18 additions & 1 deletion src/ansys/dpf/core/available_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ def __init__(self, availableresult):
}
self._sub_res = availableresult.sub_res
self._qualifiers = availableresult.qualifiers
self._qualifier_labels = availableresult.qualifier_labels

def __str__(self):
txt = (
self.name
"DPF Result\n----------\n"
+ self.name
+ "\n"
+ 'Operator name: "%s"\n' % self.operator_name
+ "Number of components: %d\n" % self.n_components
Expand All @@ -133,6 +135,15 @@ def __str__(self):
)
if self.unit:
txt += "Units: %s\n" % self.unit
if self.native_location:
txt += "Location: %s\n" % self.native_location
if self.qualifiers:
txt += "Available qualifier labels:\n"
for label in self.qualifier_labels:
txt += f" - {label}: {', '.join(map(str, self.qualifier_labels[label]))}\n"
txt += "Available qualifier combinations:\n"
for qualifier in self.qualifiers:
txt += f" {qualifier.__dict__()}\n"
return txt

@property
Expand Down Expand Up @@ -231,6 +242,11 @@ def qualifiers(self) -> list:
"""
return self._qualifiers

@property
def qualifier_labels(self) -> dict:
"""Returns a dictionary of available labels for each available qualifier."""
return self._qualifier_labels


_result_properties = {
"S": {"location": "ElementalNodal", "scripting_name": "stress"},
Expand Down Expand Up @@ -281,6 +297,7 @@ def available_result_from_name(name) -> AvailableResult:
sub_res={},
properties={"loc_name": item["location"], "scripting_name": name},
qualifiers=[],
qualifier_labels={},
)

return AvailableResult(availableresult)
26 changes: 20 additions & 6 deletions src/ansys/dpf/core/result_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ def _get_result(self, numres):
sub_res[sub_res_name] = [ssub_res_rec_name, descr]

qualifiers = []
qualifier_labels = {}
if self._server.meet_version("5.0"):
qual_obj = object_handler.ObjHandler(
data_processing_api=self._data_processing_core_api,
Expand All @@ -380,13 +381,25 @@ def _get_result(self, numres):
)
num_qual_obj = label_space_api.list_label_spaces_size(qual_obj)
for ires in range(num_qual_obj):
qualifiers.append(
LabelSpace(
label_space=label_space_api.list_label_spaces_at(qual_obj, ires),
obj=self,
server=self._server,
)
label_space = LabelSpace(
label_space=label_space_api.list_label_spaces_at(qual_obj, ires),
obj=self,
server=self._server,
)
qualifiers.append(label_space)
label_space_dict = label_space.__dict__()
for key in label_space_dict:
value = label_space_dict[key]
label_support = self.qualifier_label_support(key)
names_field = label_support.string_field_support_by_property("names")
label_value = names_field.data_as_list[
names_field.scoping.ids.tolist().index(value)
]
label_value = label_value + f" ({value})"
if key in qualifier_labels.keys() and label_value not in qualifier_labels[key]:
qualifier_labels[key].append(label_value)
else:
qualifier_labels[key] = [label_value]

availableresult = SimpleNamespace(
name=name,
Expand All @@ -398,6 +411,7 @@ def _get_result(self, numres):
sub_res=sub_res,
properties={"loc_name": loc_name, "scripting_name": scripting_name},
qualifiers=qualifiers,
qualifier_labels=qualifier_labels,
)
return available_result.AvailableResult(availableresult)

Expand Down
59 changes: 56 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ def model_with_ns():


@pytest.fixture()
def cff_data_sources():
"""Create a data sources with a cas and a dat file of fluent"""
def fluent_multi_species():
"""Create a data sources with a cas and a dat file of fluent multi-species case."""
ds = core.DataSources()
files = examples.download_fluent_multi_species_files()
files = examples.download_fluent_multi_species()
ds.set_result_file_path(files["cas"], "cas")
ds.add_file_path(files["dat"], "dat")
return ds
Expand Down Expand Up @@ -210,6 +210,59 @@ def cyclic_multistage():
return examples.download_multi_stage_cyclic_result()


@pytest.fixture()
def fluent_axial_comp():
"""Create a data sources with a cas and a dat file of fluent axial compressor case."""
ds = core.DataSources()
files = examples.download_fluent_axial_comp()
ds.set_result_file_path(files["cas"][0], "cas")
ds.add_file_path(files["dat"][0], "dat")
return ds


@pytest.fixture()
def fluent_mixing_elbow_steady_state():
"""Create a data sources with a cas and a dat file of fluent mixing elbow steady-state case."""
ds = core.DataSources()
files = examples.download_fluent_mixing_elbow_steady_state()
ds.set_result_file_path(files["cas"][0], "cas")
ds.add_file_path(files["dat"][0], "dat")
return ds


@pytest.fixture()
def fluent_mixing_elbow_transient():
"""Create a data sources with a cas and a dat file of fluent mixing elbow transient case."""
ds = core.DataSources()
files = examples.download_fluent_mixing_elbow_transient()
ds.set_result_file_path(files["cas"][0], "cas")
ds.add_file_path(files["dat"][0], "dat")
return ds


@pytest.fixture()
def cfx_heating_coil():
"""Create a data sources with a cas and a dat file of CFX heating coil case."""
ds = core.DataSources()
files = examples.download_cfx_heating_coil()
ds.set_result_file_path(files["cas"], "cas")
ds.add_file_path(files["dat"], "dat")
return ds


@pytest.fixture()
def cfx_mixing_elbow():
"""Create a data sources with a cas and a dat file of CFX mixing elbow case."""
ds = core.DataSources()
files = examples.download_cfx_mixing_elbow()
ds.set_result_file_path(files["cas"], "cas")
ds.add_file_path(files["dat"], "dat")
return ds


SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0 = meets_version(
get_server_version(core._global_server()), "7.0"
)
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_6_2 = meets_version(
get_server_version(core._global_server()), "6.2"
)
Expand Down
48 changes: 47 additions & 1 deletion tests/test_resultinfo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import platform
import pytest

from ansys import dpf
from ansys.dpf.core import Model
from conftest import SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0
from conftest import (
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0,
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0,
)

if SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0:
mechanical = "mechanical"
Expand Down Expand Up @@ -71,6 +75,48 @@ def test_print_result_info(model):
print(model.metadata.result_info)


@pytest.mark.skipif(platform.system() == "Linux", reason="CFF not available for Linux InProcess.")
@pytest.mark.skipif(
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available with CFF starting 7.0"
)
def test_print_available_result_with_qualifiers(cfx_heating_coil):
model = Model(cfx_heating_coil)
ref = """DPF Result
----------
specific_heat
Operator name: "specific_heat"
Number of components: 1
Dimensionality: scalar
Homogeneity: specific_heat
Units: j/kg*k^-1
Location: Nodal
Available qualifier labels:
- phase: Copper (3)
- zone: Default 1 (5), ZN1/FS1 (9), ZN1/FS2 (10), ZN1/FS3 (11), ZN1/FS4 (12), ZN1/FS5 (13), ZN1/FS6 (14), ZN1/FS7 (15), ZN1/FS8 (16), ZN1/FS9 (17), ZN1/FS10 (18), heater (8), ZN2/FS1 (19), ZN2/FS2 (20), ZN2/FS3 (21), ZN2/FS4 (22), ZN2/FS5 (23), ZN2/FS6 (24), ZN2/FS7 (25), ZN2/FS8 (26)
Available qualifier combinations:
{'phase': 2, 'zone': 5}
{'phase': 2, 'zone': 9}
{'phase': 2, 'zone': 10}
{'phase': 2, 'zone': 11}
{'phase': 2, 'zone': 12}
{'phase': 2, 'zone': 13}
{'phase': 2, 'zone': 14}
{'phase': 2, 'zone': 15}
{'phase': 2, 'zone': 16}
{'phase': 2, 'zone': 17}
{'phase': 2, 'zone': 18}
{'phase': 3, 'zone': 8}
{'phase': 3, 'zone': 19}
{'phase': 3, 'zone': 20}
{'phase': 3, 'zone': 21}
{'phase': 3, 'zone': 22}
{'phase': 3, 'zone': 23}
{'phase': 3, 'zone': 24}
{'phase': 3, 'zone': 25}
{'phase': 3, 'zone': 26}""" # noqa: E501
assert ref in str(model.metadata.result_info.available_results[0])


@pytest.mark.skipif(True, reason="Used to test memory leaks")
def test_result_info_memory_leaks(model):
import gc
Expand Down