From f57aa9762e5c28a9bd27f91d22eddd5a7dd3ec1d Mon Sep 17 00:00:00 2001 From: airwarriorg91 <gauravxpgupta@gmail.com> Date: Mon, 14 Oct 2024 11:24:56 +0530 Subject: [PATCH 1/2] unstructured-array-attempt --- src/pymech/dataset.py | 84 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/pymech/dataset.py b/src/pymech/dataset.py index 70cc958..08878fd 100644 --- a/src/pymech/dataset.py +++ b/src/pymech/dataset.py @@ -4,6 +4,7 @@ import numpy as np import xarray as xr +import uxarray as uxr from xarray.core.utils import Frozen from .neksuite import readnek @@ -98,6 +99,53 @@ def _open_nek_dataset(path, drop_variables=None): return ds +def extract_elem_data(elem_array): + # Use lists to accumulate data + x_list, y_list, z_list = [], [], [] + ux_list, uy_list, uz_list, p_list = [], [], [], [] + + # Loop through the elements in the array + for elem in elem_array: + # Append data to respective lists + x_list.append(np.ravel(elem.pos[0])) + y_list.append(np.ravel(elem.pos[1])) + z_list.append(np.ravel(elem.pos[2])) + + ux_list.append(np.ravel(elem.vel[0])) + uy_list.append(np.ravel(elem.vel[1])) + uz_list.append(np.ravel(elem.vel[2])) + + p_list.append(np.ravel(elem.pres)) + + # Convert lists to NumPy arrays after the loop + x = np.concatenate(x_list) + y = np.concatenate(y_list) + z = np.concatenate(z_list) + ux = np.concatenate(ux_list) + uy = np.concatenate(uy_list) + uz = np.concatenate(uz_list) + p = np.concatenate(p_list) + + # Create an xarray dataset + data = xr.Dataset( + { + "ux": (["points"], ux), + "uy": (["points"], uy), + "uz": (["points"], uz), # Correctly assign uz + "p": (["points"], p) # Correctly assign p + }, + coords={ + "x": (["points"], x), + "y": (["points"], y), + "z": (["points"], z), + } + ) + + # Wrap the xarray dataset in a uxarray grid (optional) + ux_ds = uxr.Grid.from_dataset(data) + + return ux_ds + class PymechXarrayBackend(xr.backends.BackendEntrypoint): def guess_can_open(self, filename_or_obj): @@ -184,3 +232,39 @@ def get_variables(self): ) return Frozen(data_vars) + + +def open_unstruc_dataset(path): + + # Proposed Methodology + # Step 1: Use readnek to import the data + # Step 2: Create an array of nodes, elements, and fields + # Step 3: Create a grid and xarray dataset from the data array + # Step 4: Create a uxarray dataset and return it + + field = readnek(path) + if isinstance(field, int): + raise OSError(f"Failed to load {path}") + + elements = field.elem + + # Method 1 : adapt the existing method used for xarray + + #elem_stores = [_NekDataStore(elem) for elem in elements] + # + # try: + # elem_dsets = [ + # uxr.UxDataset.load_store(store).set_coords(store.axes) for store in elem_stores + # ] + # except Exception as error: + # print("uxarray failure") + # print(error) + # print(elem_stores[0].axes) + + # Method 2 : manually create array of x, y, z and variables and use it to + # make a uxarray dataset + ds = extract_elem_data(elements) + + + + \ No newline at end of file From ef0360f9ea23f454afb79a23eac5d6eebb07c462 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Oct 2024 06:15:19 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pymech/dataset.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/pymech/dataset.py b/src/pymech/dataset.py index 08878fd..9e43d06 100644 --- a/src/pymech/dataset.py +++ b/src/pymech/dataset.py @@ -3,8 +3,8 @@ from pathlib import Path import numpy as np -import xarray as xr import uxarray as uxr +import xarray as xr from xarray.core.utils import Frozen from .neksuite import readnek @@ -99,6 +99,7 @@ def _open_nek_dataset(path, drop_variables=None): return ds + def extract_elem_data(elem_array): # Use lists to accumulate data x_list, y_list, z_list = [], [], [] @@ -110,11 +111,11 @@ def extract_elem_data(elem_array): x_list.append(np.ravel(elem.pos[0])) y_list.append(np.ravel(elem.pos[1])) z_list.append(np.ravel(elem.pos[2])) - + ux_list.append(np.ravel(elem.vel[0])) uy_list.append(np.ravel(elem.vel[1])) uz_list.append(np.ravel(elem.vel[2])) - + p_list.append(np.ravel(elem.pres)) # Convert lists to NumPy arrays after the loop @@ -132,13 +133,13 @@ def extract_elem_data(elem_array): "ux": (["points"], ux), "uy": (["points"], uy), "uz": (["points"], uz), # Correctly assign uz - "p": (["points"], p) # Correctly assign p + "p": (["points"], p), # Correctly assign p }, coords={ "x": (["points"], x), "y": (["points"], y), "z": (["points"], z), - } + }, ) # Wrap the xarray dataset in a uxarray grid (optional) @@ -235,7 +236,7 @@ def get_variables(self): def open_unstruc_dataset(path): - + # Proposed Methodology # Step 1: Use readnek to import the data # Step 2: Create an array of nodes, elements, and fields @@ -247,10 +248,10 @@ def open_unstruc_dataset(path): raise OSError(f"Failed to load {path}") elements = field.elem - + # Method 1 : adapt the existing method used for xarray - #elem_stores = [_NekDataStore(elem) for elem in elements] + # elem_stores = [_NekDataStore(elem) for elem in elements] # # try: # elem_dsets = [ @@ -261,10 +262,6 @@ def open_unstruc_dataset(path): # print(error) # print(elem_stores[0].axes) - # Method 2 : manually create array of x, y, z and variables and use it to + # Method 2 : manually create array of x, y, z and variables and use it to # make a uxarray dataset ds = extract_elem_data(elements) - - - - \ No newline at end of file