From 62a3bb091aa491ca97dec7284a1739484f7ad121 Mon Sep 17 00:00:00 2001 From: Prithwish Mukherjee Date: Tue, 27 Aug 2024 11:31:06 +0530 Subject: [PATCH 1/3] fix: Convert protobuf Point to python Vector. --- .../fluent/core/services/datamodel_se.py | 28 +++++++++++++++++++ src/ansys/fluent/core/services/reduction.py | 12 ++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/ansys/fluent/core/services/datamodel_se.py b/src/ansys/fluent/core/services/datamodel_se.py index c8d064cc6d3..22506acc51e 100644 --- a/src/ansys/fluent/core/services/datamodel_se.py +++ b/src/ansys/fluent/core/services/datamodel_se.py @@ -13,6 +13,7 @@ from ansys.api.fluent.v0 import datamodel_se_pb2 as DataModelProtoModule from ansys.api.fluent.v0 import datamodel_se_pb2_grpc as DataModelGrpcModule +from ansys.api.fluent.v0.common_api_pb2 import Point from ansys.api.fluent.v0.variant_pb2 import Variant import ansys.fluent.core as pyfluent from ansys.fluent.core.data_model_cache import DataModelCache, NameKey @@ -329,6 +330,33 @@ def _convert_variant_to_value(var: Variant) -> _TValue: return val +class Vector: + """Wrap Fluent's Point type into a Python class.""" + + def __init__(self, point: Point): + """__init__ of Vector class.""" + self._val = point + + @property + def x(self) -> float: + """Returns vector point x.""" + return self._val.x + + @property + def y(self) -> float: + """Returns vector point y.""" + return self._val.y + + @property + def z(self) -> float: + """Returns vector point z.""" + return self._val.z + + def __call__(self): + """Returns vector as [x, y, z].""" + return [self.x, self.y, self.z] + + class EventSubscription: """EventSubscription class for any datamodel event.""" diff --git a/src/ansys/fluent/core/services/reduction.py b/src/ansys/fluent/core/services/reduction.py index a725d0f30fe..7c12105ed53 100644 --- a/src/ansys/fluent/core/services/reduction.py +++ b/src/ansys/fluent/core/services/reduction.py @@ -7,7 +7,7 @@ from ansys.api.fluent.v0 import reduction_pb2 as ReductionProtoModule from ansys.api.fluent.v0 import reduction_pb2_grpc as ReductionGrpcModule -from ansys.fluent.core.services.datamodel_se import _convert_variant_to_value +from ansys.fluent.core.services.datamodel_se import Vector, _convert_variant_to_value from ansys.fluent.core.services.interceptors import ( BatchInterceptor, ErrorStateInterceptor, @@ -300,7 +300,7 @@ def centroid(self, locations, ctxt=None) -> Any: request = ReductionProtoModule.CentroidRequest() request.locations.extend(self._get_location_string(locations, ctxt)) response = self.service.centroid(request) - return response.value + return Vector(response.value) def count(self, locations, ctxt=None) -> Any: """Get count.""" @@ -322,7 +322,7 @@ def force(self, locations, ctxt=None) -> Any: request = ReductionProtoModule.ForceRequest() request.locations.extend(self._get_location_string(locations, ctxt)) response = self.service.force(request) - return response.value + return Vector(response.value) def mass_average(self, expression, locations, ctxt=None) -> Any: """Get mass average.""" @@ -385,14 +385,14 @@ def pressure_force(self, locations, ctxt=None) -> Any: request = ReductionProtoModule.PressureForceRequest() request.locations.extend(self._get_location_string(locations, ctxt)) response = self.service.pressure_force(request) - return response.value + return Vector(response.value) def viscous_force(self, locations, ctxt=None) -> Any: """Get viscous force.""" request = ReductionProtoModule.ViscousForceRequest() request.locations.extend(self._get_location_string(locations, ctxt)) response = self.service.viscous_force(request) - return response.value + return Vector(response.value) def volume(self, locations, ctxt=None) -> Any: """Get volume.""" @@ -423,7 +423,7 @@ def moment(self, expression, locations, ctxt=None) -> Any: request.expression = expression request.locations.extend(self._get_location_string(locations, ctxt)) response = self.service.moment(request) - return response.value + return Vector(response.value) def sum(self, expression, locations, weight, ctxt=None) -> Any: """Get sum.""" From 222dfdb62bf90b65775172350ecdb8c9f28affa8 Mon Sep 17 00:00:00 2001 From: Prithwish Mukherjee Date: Wed, 28 Aug 2024 11:24:17 +0530 Subject: [PATCH 2/3] Return tuple from Point. --- .../fluent/core/services/datamodel_se.py | 28 ------------------- src/ansys/fluent/core/services/reduction.py | 12 ++++---- tests/test_reduction.py | 24 ++++++++-------- 3 files changed, 18 insertions(+), 46 deletions(-) diff --git a/src/ansys/fluent/core/services/datamodel_se.py b/src/ansys/fluent/core/services/datamodel_se.py index 22506acc51e..c8d064cc6d3 100644 --- a/src/ansys/fluent/core/services/datamodel_se.py +++ b/src/ansys/fluent/core/services/datamodel_se.py @@ -13,7 +13,6 @@ from ansys.api.fluent.v0 import datamodel_se_pb2 as DataModelProtoModule from ansys.api.fluent.v0 import datamodel_se_pb2_grpc as DataModelGrpcModule -from ansys.api.fluent.v0.common_api_pb2 import Point from ansys.api.fluent.v0.variant_pb2 import Variant import ansys.fluent.core as pyfluent from ansys.fluent.core.data_model_cache import DataModelCache, NameKey @@ -330,33 +329,6 @@ def _convert_variant_to_value(var: Variant) -> _TValue: return val -class Vector: - """Wrap Fluent's Point type into a Python class.""" - - def __init__(self, point: Point): - """__init__ of Vector class.""" - self._val = point - - @property - def x(self) -> float: - """Returns vector point x.""" - return self._val.x - - @property - def y(self) -> float: - """Returns vector point y.""" - return self._val.y - - @property - def z(self) -> float: - """Returns vector point z.""" - return self._val.z - - def __call__(self): - """Returns vector as [x, y, z].""" - return [self.x, self.y, self.z] - - class EventSubscription: """EventSubscription class for any datamodel event.""" diff --git a/src/ansys/fluent/core/services/reduction.py b/src/ansys/fluent/core/services/reduction.py index 7c12105ed53..5b9a88b6fa0 100644 --- a/src/ansys/fluent/core/services/reduction.py +++ b/src/ansys/fluent/core/services/reduction.py @@ -7,7 +7,7 @@ from ansys.api.fluent.v0 import reduction_pb2 as ReductionProtoModule from ansys.api.fluent.v0 import reduction_pb2_grpc as ReductionGrpcModule -from ansys.fluent.core.services.datamodel_se import Vector, _convert_variant_to_value +from ansys.fluent.core.services.datamodel_se import _convert_variant_to_value from ansys.fluent.core.services.interceptors import ( BatchInterceptor, ErrorStateInterceptor, @@ -300,7 +300,7 @@ def centroid(self, locations, ctxt=None) -> Any: request = ReductionProtoModule.CentroidRequest() request.locations.extend(self._get_location_string(locations, ctxt)) response = self.service.centroid(request) - return Vector(response.value) + return (response.value.x, response.value.y, response.value.z) def count(self, locations, ctxt=None) -> Any: """Get count.""" @@ -322,7 +322,7 @@ def force(self, locations, ctxt=None) -> Any: request = ReductionProtoModule.ForceRequest() request.locations.extend(self._get_location_string(locations, ctxt)) response = self.service.force(request) - return Vector(response.value) + return (response.value.x, response.value.y, response.value.z) def mass_average(self, expression, locations, ctxt=None) -> Any: """Get mass average.""" @@ -385,14 +385,14 @@ def pressure_force(self, locations, ctxt=None) -> Any: request = ReductionProtoModule.PressureForceRequest() request.locations.extend(self._get_location_string(locations, ctxt)) response = self.service.pressure_force(request) - return Vector(response.value) + return (response.value.x, response.value.y, response.value.z) def viscous_force(self, locations, ctxt=None) -> Any: """Get viscous force.""" request = ReductionProtoModule.ViscousForceRequest() request.locations.extend(self._get_location_string(locations, ctxt)) response = self.service.viscous_force(request) - return Vector(response.value) + return (response.value.x, response.value.y, response.value.z) def volume(self, locations, ctxt=None) -> Any: """Get volume.""" @@ -423,7 +423,7 @@ def moment(self, expression, locations, ctxt=None) -> Any: request.expression = expression request.locations.extend(self._get_location_string(locations, ctxt)) response = self.service.moment(request) - return Vector(response.value) + return (response.value.x, response.value.y, response.value.z) def sum(self, expression, locations, weight, ctxt=None) -> Any: """Get sum.""" diff --git a/tests/test_reduction.py b/tests/test_reduction.py index 17980815ea2..8c02ec869cf 100644 --- a/tests/test_reduction.py +++ b/tests/test_reduction.py @@ -163,9 +163,9 @@ def _test_centroid(solver): red_val_1 = solver.fields.reduction.centroid(locations=[velocity_inlet["inlet1"]]) red_val_2 = solver.fields.reduction.centroid(locations=[velocity_inlet["inlet2"]]) red_val_3 = solver.fields.reduction.centroid(locations=[velocity_inlet]) - assert [red_val_1.x, red_val_1.y, red_val_1.z] == expr_val_1 - assert [red_val_2.x, red_val_2.y, red_val_2.z] == expr_val_2 - assert [red_val_3.x, red_val_3.y, red_val_3.z] == expr_val_3 + assert [red_val_1[0], red_val_1[1], red_val_1[2]] == expr_val_1 + assert [red_val_2[0], red_val_2[1], red_val_2[2]] == expr_val_2 + assert [red_val_3[0], red_val_3[1], red_val_3[2]] == expr_val_3 solver_named_expressions.pop(key="test_expr_1") @@ -284,13 +284,13 @@ def _test_force(solver): locations=[solver.setup.boundary_conditions.wall] ) - assert [red_total_force.x, red_total_force.y, red_total_force.z] == expr_val_1 + assert [red_total_force[0], red_total_force[1], red_total_force[2]] == expr_val_1 - assert red_pressure_force.x + red_viscous_force.x == red_total_force.x + assert red_pressure_force[0] + red_viscous_force[0] == red_total_force[0] - assert red_pressure_force.y + red_viscous_force.y == red_total_force.y + assert red_pressure_force[1] + red_viscous_force[1] == red_total_force[1] - assert red_pressure_force.z + red_viscous_force.z == red_total_force.z + assert red_pressure_force[2] + red_viscous_force[2] == red_total_force[2] solver_named_expressions.pop(key="test_expr_1") @@ -316,11 +316,11 @@ def _test_moment(solver): expression="['inlet1']", locations=[location] ) - assert [red_moment_force.x, red_moment_force.y, red_moment_force.z] == expr_val_1 + assert [red_moment_force[0], red_moment_force[1], red_moment_force[2]] == expr_val_1 assert [ - red_moment_location.x, - red_moment_location.y, - red_moment_location.z, + red_moment_location[0], + red_moment_location[1], + red_moment_location[2], ] == expr_val_2 solver_named_expressions.pop(key="test_expr_1") @@ -368,7 +368,7 @@ def _test_sum_if(solver): static_mixer_case_session2 = static_mixer_case_session -@pytest.mark.nightly +# @pytest.mark.nightly @pytest.mark.fluent_version(">=23.1") def test_reductions(static_mixer_case_session, static_mixer_case_session2) -> None: solver1 = static_mixer_case_session From 55955e531e9b52dcc7a39817abe35197d2fd8abd Mon Sep 17 00:00:00 2001 From: Prithwish Mukherjee Date: Wed, 28 Aug 2024 11:29:09 +0530 Subject: [PATCH 3/3] Mark test as nightly. --- tests/test_reduction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_reduction.py b/tests/test_reduction.py index 8c02ec869cf..cf0ed8d29cf 100644 --- a/tests/test_reduction.py +++ b/tests/test_reduction.py @@ -368,7 +368,7 @@ def _test_sum_if(solver): static_mixer_case_session2 = static_mixer_case_session -# @pytest.mark.nightly +@pytest.mark.nightly @pytest.mark.fluent_version(">=23.1") def test_reductions(static_mixer_case_session, static_mixer_case_session2) -> None: solver1 = static_mixer_case_session