From 9b5c1c26aaffa695abd76a327420fe53830eb3cd Mon Sep 17 00:00:00 2001 From: abja-dhi Date: Wed, 6 Nov 2024 13:56:05 -0500 Subject: [PATCH 1/3] Update _FM_utils.py Updated the part that elements connected to each node are defined using csr_matrix function from scipy.sparse. This method works much faster than the previous one. --- mikeio/spatial/_FM_utils.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/mikeio/spatial/_FM_utils.py b/mikeio/spatial/_FM_utils.py index 4eefb8c72..cd7c45e9c 100644 --- a/mikeio/spatial/_FM_utils.py +++ b/mikeio/spatial/_FM_utils.py @@ -7,6 +7,7 @@ from matplotlib.figure import Figure from matplotlib.tri import Triangulation import numpy as np +from scipy.sparse import csr_matrix from collections import namedtuple from ._utils import _relative_cumulative_distance @@ -643,6 +644,16 @@ def _to_polygons(node_coordinates: np.ndarray, element_table: np.ndarray) -> lis return polygons +def _create_node_element_matrix(element_table, num_nodes): + row_ind = element_table.ravel() + col_ind = np.repeat(np.arange(element_table.shape[0]), element_table.shape[1]) + data = np.ones(len(row_ind), dtype=int) + connectivity_matrix = csr_matrix( + (data, (row_ind, col_ind)), shape=(num_nodes, element_table.shape[0]) + ) + return connectivity_matrix + + def _get_node_centered_data( node_coordinates: np.ndarray, element_table: np.ndarray, @@ -675,17 +686,11 @@ def _get_node_centered_data( elem_table, ec, data = __create_tri_only_element_table( nc, element_table, element_coordinates, data ) + connectivity_matrix = _create_node_element_matrix(elem_table, nc.shape[0]) - node_cellID = [ - list(np.argwhere(elem_table == i)[:, 0]) - for i in np.unique( - elem_table.reshape( - -1, - ) - ) - ] node_centered_data = np.zeros(shape=nc.shape[0]) - for n, item in enumerate(node_cellID): + for n in range(connectivity_matrix.shape[0]): + item = connectivity_matrix.getrow(n).indices I = ec[item][:, :2] - nc[n][:2] I2 = (I**2).sum(axis=0) Ixy = (I[:, 0] * I[:, 1]).sum(axis=0) From e92857c8a10f5804cecaedf1180683687763f88e Mon Sep 17 00:00:00 2001 From: abja-dhi Date: Wed, 6 Nov 2024 16:12:28 -0500 Subject: [PATCH 2/3] Update _FM_utils.py --- mikeio/spatial/_FM_utils.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/mikeio/spatial/_FM_utils.py b/mikeio/spatial/_FM_utils.py index cd7c45e9c..5796e5cbb 100644 --- a/mikeio/spatial/_FM_utils.py +++ b/mikeio/spatial/_FM_utils.py @@ -644,7 +644,23 @@ def _to_polygons(node_coordinates: np.ndarray, element_table: np.ndarray) -> lis return polygons -def _create_node_element_matrix(element_table, num_nodes): +def _create_node_element_matrix(element_table: np.ndarray, num_nodes: int) -> csr_matrix: + """Creates a sparse node-element connectivity matrix from a given element table. + + Parameters + ---------- + element_table : np.array + The element table (A 2D array where each row represents an element and each + column corresponds to a node index involved in the element.) + num_nodes : int + The total number of nodes in the mesh. + + Returns + ------- + scipy.sparse.csr_matrix + A sparse matrix of shape (num_nodes, number of elements), where the entry + (i, j) is 1 if node i is part of element j, and 0 otherwise. + """ row_ind = element_table.ravel() col_ind = np.repeat(np.arange(element_table.shape[0]), element_table.shape[1]) data = np.ones(len(row_ind), dtype=int) From 12348f4fb0a9895d0623dffad7b9453aa772cd5a Mon Sep 17 00:00:00 2001 From: abja-dhi Date: Thu, 7 Nov 2024 07:34:38 -0500 Subject: [PATCH 3/3] Updated _FM_utils.py ruff extension in VS Code is used to format the code --- mikeio/spatial/_FM_utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mikeio/spatial/_FM_utils.py b/mikeio/spatial/_FM_utils.py index 5796e5cbb..d6388f8f2 100644 --- a/mikeio/spatial/_FM_utils.py +++ b/mikeio/spatial/_FM_utils.py @@ -644,7 +644,9 @@ def _to_polygons(node_coordinates: np.ndarray, element_table: np.ndarray) -> lis return polygons -def _create_node_element_matrix(element_table: np.ndarray, num_nodes: int) -> csr_matrix: +def _create_node_element_matrix( + element_table: np.ndarray, num_nodes: int +) -> csr_matrix: """Creates a sparse node-element connectivity matrix from a given element table. Parameters @@ -654,12 +656,13 @@ def _create_node_element_matrix(element_table: np.ndarray, num_nodes: int) -> cs column corresponds to a node index involved in the element.) num_nodes : int The total number of nodes in the mesh. - + Returns ------- scipy.sparse.csr_matrix A sparse matrix of shape (num_nodes, number of elements), where the entry (i, j) is 1 if node i is part of element j, and 0 otherwise. + """ row_ind = element_table.ravel() col_ind = np.repeat(np.arange(element_table.shape[0]), element_table.shape[1])