diff --git a/src/ansys/dpf/core/field_base.py b/src/ansys/dpf/core/field_base.py index 05d9612314..f9303ad60a 100644 --- a/src/ansys/dpf/core/field_base.py +++ b/src/ansys/dpf/core/field_base.py @@ -2,6 +2,8 @@ import warnings from abc import abstractmethod +from typing import Union, List + from ansys.dpf.gate.generated import field_abstract_api from ansys.dpf.core import scoping @@ -358,15 +360,19 @@ def get_entity_data_by_id(self, id): pass @abstractmethod - def append(self, data, scopingid): - """Add an entity data to the existing data. + def append( + self, + data: Union[List[int], List[float], List[List[float]], List, np.ndarray], + scopingid: Union[int, List[int]] + ): + """Add data to the field for an entity or a list of entities. Parameters ---------- - data : list of int, double, or array - Data in the entity. - scopingid : int - ID of the scoping. + data: + Data for the entity or list of entities. + scopingid: + ID or list of IDs of the entities to add data for. Examples -------- diff --git a/tests/test_math.py b/tests/test_math.py new file mode 100644 index 0000000000..a0ba89d662 --- /dev/null +++ b/tests/test_math.py @@ -0,0 +1,89 @@ +import numpy as np + +import ansys.dpf.core as dpf + + +def test_math_matrix_inverse_3d(): + dim = 3 + real_field = dpf.fields_factory.create_matrix_field( + num_entities=1, + num_lines=dim, + num_col=dim, + ) + real_field.unit = "m" + values = [1.0, 1.0, 1.0, + 4.0, 0.0, 0.0, + 1.0, 1.0, 1.0] + real_field.append(values, 1) + + img_field = dpf.fields_factory.create_matrix_field( + num_entities=1, + num_lines=dim, + num_col=dim, + ) + img_field.unit = "m" + values = [0.3, 0.0, 0.0, + 1.0, 1.0, 2.0, + 0.0, 0.0, 0.1] + img_field.append(values, 1) + + fc_in = dpf.fields_container_factory.over_time_freq_complex_fields_container( + real_fields={1.0: real_field}, imaginary_fields={1.0: img_field}, time_freq_unit="s" + ) + + fc_out = dpf.operators.math.matrix_inverse(fields_container=fc_in).eval() + + print(fc_out) + print(fc_out[0]) + assert fc_out[0].unit == "m^-1" + real_inverse_values = [1.4551, 0.1564, -1.5642, + -5.1946, -0.6584, 6.5842, + 4.3652, 0.4693, -4.6926] + assert np.allclose(fc_out[0].data, real_inverse_values, atol=0.0001) + + print(fc_out[1]) + assert fc_out[1].unit == "m^-1" + img_inverse_values = [-1.2477, -0.1091, 1.0913, + -5.4456, 0.3896, 6.1040, + 6.2568, -0.3274, -6.7261] + assert np.allclose(fc_out[1].data, img_inverse_values, atol=0.0001) + + +def test_math_matrix_inverse_2d(): + dim = 2 + real_field = dpf.fields_factory.create_matrix_field( + num_entities=1, + num_lines=dim, + num_col=dim, + ) + real_field.unit = "m" + values = [0.0, 1.0, + 2.0, 3.0] + real_field.append(values, 1) + + img_field = dpf.fields_factory.create_matrix_field( + num_entities=1, + num_lines=dim, + num_col=dim, + ) + img_field.unit = "m" + values = [1.0, 1.0, + 1.0, 1.0] + img_field.append(values, 1) + + fc_in = dpf.fields_container_factory.over_time_freq_complex_fields_container( + real_fields={1.0: real_field}, imaginary_fields={1.0: img_field}, time_freq_unit="s" + ) + + fc_out = dpf.operators.math.matrix_inverse(fields_container=fc_in).eval() + + print(fc_out) + print(fc_out[0]) + assert fc_out[0].unit == "m^-1" + real_inverse_values = [-1.5, 0.5, 1.0, 0.0] + assert np.allclose(fc_out[0].data, real_inverse_values, atol=0.0001) + + print(fc_out[1]) + assert fc_out[1].unit == "m^-1" + img_inverse_values = [-0.5, 0.5, 0.5, -0.5] + assert np.allclose(fc_out[1].data, img_inverse_values, atol=0.0001)