From f4cdb3e95ef2255bf79ad1bac09c58696e0b7931 Mon Sep 17 00:00:00 2001 From: Ishaan Desai <44898158+IshaanDesai@users.noreply.github.com> Date: Wed, 18 Nov 2020 17:21:11 +0100 Subject: [PATCH] Handle writing of empty arrays to preCICE (#69) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Correcting spelling mistake Co-authored-by: Benjamin RĂ¼th Co-authored-by: BenjaminRueth --- CHANGELOG.md | 4 ++++ precice.pyx | 18 ++++++++++++++---- test/test_bindings_module.pyx | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e49c281..98da971e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. +## latest + +* Bugfix: Bindings also support empty read/write data for block read/write operations (like C++ preCICE API). https://github.com/precice/python-bindings/pull/69 + ## 2.1.1.1 * Bindings can now handle mesh initialization with no vertices. This behavior is consistent with the C++ preCICE API. diff --git a/precice.pyx b/precice.pyx index c5523451..05b77fe0 100644 --- a/precice.pyx +++ b/precice.pyx @@ -868,8 +868,12 @@ cdef class Interface: """ if not isinstance(values, np.ndarray): values = np.asarray(values) - size, dimensions = values.shape - assert(dimensions == self.get_dimensions()) + if len(values) > 0: + size, dimensions = values.shape + assert(dimensions == self.get_dimensions()) + if len(values) == 0: + size = 0 + cdef np.ndarray[int, ndim=1] _vertex_ids = np.ascontiguousarray(vertex_ids, dtype=np.int32) cdef np.ndarray[double, ndim=1] _values = np.ascontiguousarray(values.flatten(), dtype=np.double) assert(size == _vertex_ids.size) @@ -913,6 +917,7 @@ cdef class Interface: """ if not isinstance(value, np.ndarray): value = np.asarray(value) + assert(len(value) > 0) dimensions = value.size assert(dimensions == self.get_dimensions()) cdef np.ndarray[np.double_t, ndim=1] _value = np.ascontiguousarray(value, dtype=np.double) @@ -948,8 +953,13 @@ cdef class Interface: """ cdef np.ndarray[int, ndim=1] _vertex_ids = np.ascontiguousarray(vertex_ids, dtype=np.int32) cdef np.ndarray[double, ndim=1] _values = np.ascontiguousarray(values, dtype=np.double) - assert(_values.size == _vertex_ids.size) - size = vertex_ids.size + + if len(values) > 0: + assert(_values.size == _vertex_ids.size) + size = vertex_ids.size + if len(values) == 0: + size = 0 + self.thisptr.writeBlockScalarData (data_id, size, _vertex_ids.data, _values.data) def write_scalar_data (self, data_id, vertex_id, double value): diff --git a/test/test_bindings_module.pyx b/test/test_bindings_module.pyx index 7f5667d7..62a527eb 100644 --- a/test/test_bindings_module.pyx +++ b/test/test_bindings_module.pyx @@ -195,6 +195,13 @@ class TestBindings(TestCase): read_data = solver_interface.read_block_scalar_data(1, np.array([1, 2, 3])) self.assertTrue(np.array_equal(write_data, read_data)) + def test_read_write_block_scalar_data_empty(self): + solver_interface = precice.Interface("test", "dummy.xml", 0, 1) + write_data = np.array([]) + solver_interface.write_block_scalar_data(1, [], write_data) + read_data = solver_interface.read_block_scalar_data(1, []) + self.assertTrue(len(read_data) == 0) + def test_read_write_block_scalar_data_non_contiguous(self): """ Tests behaviour of solver interface, if a non contiguous array is passed to the interface. @@ -224,6 +231,13 @@ class TestBindings(TestCase): read_data = solver_interface.read_block_vector_data(1, np.array([1, 2])) self.assertTrue(np.array_equal(write_data, read_data)) + def test_read_write_block_vector_data_empty(self): + solver_interface = precice.Interface("test", "dummy.xml", 0, 1) + write_data = np.array([]) + solver_interface.write_block_vector_data(1, [], write_data) + read_data = solver_interface.read_block_vector_data(1, []) + self.assertTrue(len(read_data) == 0) + def test_read_write_block_vector_data_list(self): solver_interface = precice.Interface("test", "dummy.xml", 0, 1) write_data = [[3, 7, 8], [7 ,6, 5]]