From 43a82f4a4c1d101331be2b17093653b0079a01d4 Mon Sep 17 00:00:00 2001 From: Robin Walloner Date: Tue, 13 May 2025 09:01:04 +0200 Subject: [PATCH 1/4] Flatten coordinate array in `map_and_read_data` (#237) * Flatten coordinate array in map_and_read_data * Apply suggestions from code review * Add Changelog entry --------- Co-authored-by: Benjamin Rodenberg --- CHANGELOG.md | 4 ++++ cyprecice/cyprecice.pyx | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f495bd8..171668ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. +## latest + +* Fix bug in `map_and_read_data` https://github.com/precice/python-bindings/pull/237 + ## 3.2.0 * Update `requirements.txt` of the solver dummy https://github.com/precice/python-bindings/pull/233 diff --git a/cyprecice/cyprecice.pyx b/cyprecice/cyprecice.pyx index c2519478..29120a2c 100644 --- a/cyprecice/cyprecice.pyx +++ b/cyprecice/cyprecice.pyx @@ -997,7 +997,7 @@ cdef class Participant: size = coordinates.shape[0] dimensions = self.get_data_dimensions(mesh_name, data_name) - cdef vector[double] cpp_coordinates = coordinates + cdef vector[double] cpp_coordinates = coordinates.flatten() cdef vector[double] cpp_values = [-1 for _ in range(size * dimensions)] self.thisptr.mapAndReadData (convert(mesh_name), convert(data_name), cpp_coordinates, relative_read_time, cpp_values) From 3d2e97f4340ca59783e95175d675570e150f547e Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Mon, 16 Jun 2025 12:40:03 +0200 Subject: [PATCH 2/4] Modify docstrings to clearly state the flexibility of the API in accepting multidimensional data structures (#239) * Modify docstrings to clearly state the flexibility of the API in accepting multidimensional data structures * Modify set_mesh_vertices docstring and add changelog entry * Add list of tuples input example to read_and_map_data --- CHANGELOG.md | 1 + cyprecice/cyprecice.pyx | 55 +++++++++++++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 171668ad..76c96395 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. ## latest +* Modify docstrings to higlight flexbility of the API in terms of its ability to handle multidimensional input data structures https://github.com/precice/python-bindings/pull/239 * Fix bug in `map_and_read_data` https://github.com/precice/python-bindings/pull/237 ## 3.2.0 diff --git a/cyprecice/cyprecice.pyx b/cyprecice/cyprecice.pyx index 29120a2c..06095e46 100644 --- a/cyprecice/cyprecice.pyx +++ b/cyprecice/cyprecice.pyx @@ -401,6 +401,7 @@ cdef class Participant: positions : array_like The coordinates of the vertices in a numpy array [N x D] where N = number of vertices and D = dimensions of geometry. + A list of the same shape is also accepted. Returns ------- @@ -435,6 +436,16 @@ cdef class Participant: >>> vertex_ids = participant.set_mesh_vertices(mesh_name, positions) >>> vertex_ids.shape (5,) + + Set mesh vertices for a 3D problem with 5 mesh vertices, where the positions are a list of tuples. + + >>> positions = [(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5)] + >>> positions.shape + (5, 3) + >>> mesh_name = "MeshOne" + >>> vertex_ids = participant.set_mesh_vertices(mesh_name, positions) + >>> vertex_ids.shape + (5,) """ check_array_like(positions, "positions", "set_mesh_vertices") @@ -496,6 +507,7 @@ cdef class Participant: vertices : array_like The IDs of the vertices in a numpy array [N x 2] where N = number of edges and D = dimensions of geometry. + A list of the same shape is also accepted. Examples -------- @@ -557,6 +569,7 @@ cdef class Participant: vertices : array_like The IDs of the vertices in a numpy array [N x 3] where N = number of triangles and D = dimensions of geometry. + A list of the same shape is also accepted. Examples -------- @@ -621,6 +634,7 @@ cdef class Participant: vertices : array_like The IDs of the vertices in a numpy array [N x 4] where N = number of quads and D = dimensions of geometry. + A list of the same shape is also accepted. Examples -------- @@ -685,6 +699,7 @@ cdef class Participant: vertices : array_like The IDs of the vertices in a numpy array [N x 4] where N = number of quads and D = dimensions of geometry. + A list of the same shape is also accepted. Examples -------- @@ -794,6 +809,13 @@ cdef class Participant: >>> vertex_ids = [1, 2, 3, 4, 5] >>> values = np.array([[v1_x, v1_y, v1_z], [v2_x, v2_y, v2_z], [v3_x, v3_y, v3_z], [v4_x, v4_y, v4_z], [v5_x, v5_y, v5_z]]) >>> participant.write_data(mesh_name, data_name, vertex_ids, values) + + Write vector data for a 3D (D=3) problem with 5 (N=5) vertices, where the values are provided as a list of tuples: + >>> mesh_name = "MeshOne" + >>> data_name = "DataOne" + >>> vertex_ids = [1, 2, 3, 4, 5] + >>> values = [(v1_x, v1_y, v1_z), (v2_x, v2_y, v2_z), (v3_x, v3_y, v3_z), (v4_x, v4_y, v4_z), (v5_x, v5_y, v5_z)] + >>> participant.write_data(mesh_name, data_name, vertex_ids, values) """ check_array_like(vertex_ids, "vertex_ids", "write_data") check_array_like(values, "values", "write_data") @@ -934,8 +956,14 @@ cdef class Participant: >>> coordinates = np.array([[c1_x, c1_y], [c2_x, c2_y], [c3_x, c3_y], [c4_x, c4_y], [c5_x, c5_y]]) >>> values = np.array([v1, v2, v3, v4, v5]) >>> participant.write_and_map_data(mesh_name, data_name, coordinates, values) - """ + Write scalar data for a 2D problem with 5 vertices, where the coordinates are provided as a list of tuples, and the values are provided as a list of scalars: + >>> mesh_name = "MeshOne" + >>> data_name = "DataOne" + >>> coordinates = [(c1_x, c1_y), (c2_x, c2_y), (c3_x, c3_y), (c4_x, c4_y), (c5_x, c5_y)] + >>> values = [v1, v2, v3, v4, v5] + >>> participant.write_and_map_data(mesh_name, data_name, coordinates, values) + """ check_array_like(coordinates, "coordinates", "write_and_map_data") check_array_like(values, "values", "write_and_map_data") @@ -982,13 +1010,21 @@ cdef class Participant: Read scalar data for a 2D problem with 2 vertices: >>> mesh_name = "MeshOne" >>> data_name = "DataOne" + >>> coordinates = np.array([[1.0, 1.0], [2.0, 2.0]]) + >>> dt = 1.0 + >>> values = map_and_read_data(mesh_name, data_name, coordinates, dt) + >>> values.shape + >>> (2, ) + + Read scalar data for a 2D problem with 2 vertices, where the coordinates are provided as a list of tuples: + >>> mesh_name = "MeshOne" + >>> data_name = "DataOne" >>> coordinates = [(1.0, 1.0), (2.0, 2.0)] >>> dt = 1.0 >>> values = map_and_read_data(mesh_name, data_name, coordinates, dt) >>> values.shape >>> (2, ) """ - check_array_like(coordinates, "coordinates", "map_and_read_data") if not isinstance(coordinates, np.ndarray): @@ -1045,6 +1081,13 @@ cdef class Participant: >>> gradients = np.array([[v1x_dx, v1y_dx, v1x_dy, v1y_dy], [v2x_dx, v2y_dx, v2x_dy, v2y_dy]]) >>> participant.write_gradient_data(mesh_name, data_name, vertex_ids, gradients) + Write gradient vector data for a 2D problem with 2 vertices, where the gradients are provided as a list of tuples: + >>> mesh_name = "MeshOne" + >>> data_name = "DataOne" + >>> vertex_ids = [1, 2] + >>> gradients = [(v1x_dx, v1y_dx, v1x_dy, v1y_dy), (v2x_dx, v2y_dx, v2x_dy, v2y_dy)] + >>> participant.write_gradient_data(mesh_name, data_name, vertex_ids, gradients) + Write vector data for a 3D problem with 2 vertices: >>> mesh_name = "MeshOne" >>> data_name = "DataOne" @@ -1072,7 +1115,6 @@ cdef class Participant: self.thisptr.writeGradientData (convert(mesh_name), convert(data_name), cpp_vertex_ids, cpp_gradients) - def requires_gradient_data_for(self, mesh_name, data_name): """ Checks if the given data set requires gradient data. We check if the data object has been intialized with the gradient flag. @@ -1096,11 +1138,9 @@ cdef class Participant: >>> data_name = "DataOne" >>> participant.is_gradient_data_required(mesh_name, data_name) """ - return self.thisptr.requiresGradientDataFor(convert(mesh_name), convert(data_name)) - - def set_mesh_access_region (self, mesh_name, bounding_box): + def set_mesh_access_region(self, mesh_name, bounding_box): """ This function is required if you don't want to use the mapping schemes in preCICE, but rather want to use your own solver for data mapping. As opposed to the usual preCICE mapping, only a @@ -1156,8 +1196,7 @@ cdef class Participant: self.thisptr.setMeshAccessRegion(convert(mesh_name), cpp_bounding_box) - - def get_mesh_vertex_ids_and_coordinates (self, mesh_name): + def get_mesh_vertex_ids_and_coordinates(self, mesh_name): """ Iterating over the region of interest defined by bounding boxes and reading the corresponding coordinates omitting the mapping. This function is still experimental. From 346de1684cad808340ae6ba3fb5df9f653a2239b Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Wed, 18 Jun 2025 14:02:33 +0200 Subject: [PATCH 3/4] Bump version --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76c96395..5dbd73cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file. -## latest +## 3.2.1 * Modify docstrings to higlight flexbility of the API in terms of its ability to handle multidimensional input data structures https://github.com/precice/python-bindings/pull/239 * Fix bug in `map_and_read_data` https://github.com/precice/python-bindings/pull/237 From 8665ba9a6f0ae70fb87b2c68c666a56fa8e85182 Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Wed, 18 Jun 2025 14:16:01 +0200 Subject: [PATCH 4/4] Fix spellings in the CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5dbd73cf..785df480 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file. ## 3.2.1 -* Modify docstrings to higlight flexbility of the API in terms of its ability to handle multidimensional input data structures https://github.com/precice/python-bindings/pull/239 +* Modify docstrings to highlight flexibility of the API in terms of its ability to handle multidimensional input data structures https://github.com/precice/python-bindings/pull/239 * Fix bug in `map_and_read_data` https://github.com/precice/python-bindings/pull/237 ## 3.2.0