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

[Core] Fix Matrix from Sliced Array #11627

Merged
merged 4 commits into from
Sep 29, 2023
Merged
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
6 changes: 3 additions & 3 deletions kratos/python/add_matrix_to_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ namespace Kratos::Python
KRATOS_ERROR_IF( info.format != py::format_descriptor<double>::value ) << "Expected a double array\n";
KRATOS_ERROR_IF( info.ndim != 2 ) << "Buffer dimension of 2 is required, got: " << info.ndim << std::endl;
DenseMatrix<double> matrix = DenseMatrix<double>(info.shape[0], info.shape[1]);
std::array<std::size_t,2> strides {info.strides[0] / static_cast<std::size_t>(info.itemsize),
info.strides[1] / static_cast<std::size_t>(info.itemsize)};

std::size_t count = 0;
for( int i=0; i<info.shape[0]; ++i ) {
for( int j=0; j<info.shape[1]; ++j ) {
matrix(i,j) = static_cast<double *>(info.ptr)[count];
count++;
matrix(i,j) = static_cast<double *>(info.ptr)[i * strides[0] + j * strides[1]];
}
}

Expand Down
53 changes: 53 additions & 0 deletions kratos/tests/test_matrix_interface.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import KratosMultiphysics.KratosUnittest as KratosUnittest
import KratosMultiphysics as KM

import numpy

class TestMatrixInterface(KratosUnittest.TestCase):

def test_len(self):
Expand Down Expand Up @@ -148,6 +150,57 @@ def test_sqaure_list_of_list_construction(self):
for j in range(D.Size2()):
self.assertEqual(D[i,j], i + (j*0.1))

def test_buffer_protocol(self) -> None:
# + ------------ +
# | 0 1 2 3 |
# | 4 5 6 7 |
# | 8 9 10 11 |
# | 12 13 14 15 |
# | 16 17 18 19 |
# + ------------ +
numpy_matrix = numpy.arange(20.0, dtype=numpy.float64).reshape((5, 4))

# Test default casting
kratos_matrix = KM.Matrix(numpy_matrix)
reference_matrix = KM.Matrix([[ 0.0, 1.0, 2.0, 3.0],
[ 4.0, 5.0, 6.0, 7.0],
[ 8.0, 9.0, 10.0, 11.0],
[12.0, 13.0, 14.0, 15.0],
[16.0, 17.0, 18.0, 19.0]])
self.assertMatrixAlmostEqual(kratos_matrix, reference_matrix)

# Test slicing
# + ------ +
# | 5 6 |
# | 9 10 |
# | 13 14 |
# + ------ +
sliced_numpy_matrix = numpy_matrix[1:4,1:3]
kratos_matrix = KM.Matrix(sliced_numpy_matrix)
reference_matrix = KM.Matrix([[ 5.0, 6.0],
[ 9.0, 10.0],
[13.0, 14.0]])
self.assertMatrixAlmostEqual(kratos_matrix, reference_matrix)

# Test slicing with steps
# + ----- +
# | 1 3 |
# | 13 15 |
# + ----- +
sliced_numpy_matrix = numpy_matrix[0::3, 1::2]
kratos_matrix = KM.Matrix(sliced_numpy_matrix)
reference_matrix = KM.Matrix([[ 1.0, 3.0],
[13.0, 15.0]])
self.assertMatrixAlmostEqual(kratos_matrix, reference_matrix)

# Test empty slice
# ++
# ++
sliced_numpy_matrix = numpy_matrix[10:10,20:20]
kratos_matrix = KM.Matrix(sliced_numpy_matrix)
reference_matrix = KM.Matrix([])
self.assertMatrixAlmostEqual(kratos_matrix, reference_matrix)

def test_list_of_list_construction_error_hand(self):
with self.assertRaisesRegex(RuntimeError, r'Error: Wrong size of a row 1! Expected 2, got 3'):
KM.Matrix([[1, 2], [4, 5, 6]])
Expand Down