Skip to content

Showcase matrix inverse operator #1680

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/ansys/dpf/core/field_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
--------
Expand Down
89 changes: 89 additions & 0 deletions tests/test_math.py
Original file line number Diff line number Diff line change
@@ -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)
Loading