Skip to content

Commit

Permalink
Add tests for matrix_inverse
Browse files Browse the repository at this point in the history
Signed-off-by: paul.profizi <[email protected]>
  • Loading branch information
PProfizi committed Aug 2, 2024
1 parent 631ac64 commit 42941f3
Showing 1 changed file with 89 additions and 0 deletions.
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)

0 comments on commit 42941f3

Please sign in to comment.