From 9b0c767dfe5b3f972768e9af58e19cbeb7793bb7 Mon Sep 17 00:00:00 2001 From: Leon Teichroeb <leon.teichroeb@cern.ch> Date: Tue, 2 Jul 2024 11:16:52 +0200 Subject: [PATCH] Python Bindings: Removed unused file. Cleaned import. --- bindings/python/STEAM_mat.py | 97 ------------------- .../python/src/steammaterials/__init__.py | 3 + 2 files changed, 3 insertions(+), 97 deletions(-) delete mode 100644 bindings/python/STEAM_mat.py diff --git a/bindings/python/STEAM_mat.py b/bindings/python/STEAM_mat.py deleted file mode 100644 index 0d4494c..0000000 --- a/bindings/python/STEAM_mat.py +++ /dev/null @@ -1,97 +0,0 @@ -import ctypes as ct -import os -import platform -import numpy as np -from numpy.ctypeslib import ndpointer -#import matlab.engine - - - -class STEAM_materials: - - def __init__(self, func_name, n_arg, n_points, C_FUN_folder=None): - """ - - :param func_name: string with function name corresponding to dll file name (without the .dll in the string) - :param n_arg: number of arguments of the func_name function. This corresponds to number of columns in 2D numpy array, numpy2d, to be used in the method. Use numpy2d.shape[1] to get the number. - :param n_points: number of points to evaluate. This corresponds to number of rows in 2D numpy array, numpy2d, to be used in the eval method. Use numpy2d.shape[0] to get the number. - :param C_FUN_folder: If not specified, the code assumes the .dll files are in a folder called CFUN in the same directory as this script. Otherwise a full path to a folder needs to be given. - """ - if C_FUN_folder: - self.C_FUN_folder = C_FUN_folder # allows user to specify full path to folder with .dlls - else: - self.C_FUN_folder = os.path.join(os.getcwd(), "CFUN") # Assumes .dlls are in a folder called CFUN in the same directory as this script - - if platform.system() == 'Windows': - self._dll_name = f'{func_name}.dll' - elif platform.system() == 'Linux': - self._dll_name = f'lib{func_name}.so' - else: - raise NotImplementedError(f'Platform "{platform.system()}" is not supported!') - - _dll = ct.CDLL(os.path.join(self.C_FUN_folder, self._dll_name)) - self.func_name = func_name.encode('ascii') - self.n_points = n_points - self.n_arg = n_arg - array_type = ct.c_double * self.n_points - self.RealPtr = array_type() - self.Int_Ptr = array_type() - _doublepp = ndpointer(dtype=np.uintp, ndim=1, flags='C') - f_name = ct.c_char_p - n_arg = ct.c_int - b_size = ct.c_int - ifail = ct.c_long - _dll.init.argtypes = [] - _dll.init.restype = ct.c_long - self.eval = _dll.eval - self.eval.argtypes = [f_name, n_arg, _doublepp, _doublepp, b_size, array_type, array_type] - self.eval.restype = ifail - - def evaluate(self, numpy2d): - """ - DLL funcion call. It can take a tuple with arguments or numpy array where each row is a set of arguments - :param numpy2d: Numpy array with number of columns corresponding to number of function arguments and points to evaluate in rows - :return: Numpy array with values calculated by .dll function - """ - inReal = (numpy2d.__array_interface__['data'][0] + np.arange(numpy2d.shape[0]) * numpy2d.strides[0]).astype(np.uintp) - error_out = self.eval(self.func_name, self.n_arg, inReal, inReal, self.n_points, self.RealPtr, self.Int_Ptr) - if error_out == 1: - pass - else: - raise ValueError(f"There was a problem with calling {self._dll_name} with arguments {numpy2d}. Check if library file exists or if number of arguments is correct.") - return np.array(self.RealPtr) - -class STEAM_materials_Matlab: - def __init__(self,func_name:str,arg_list:str): - self.arguments=arg_list - self.func=func_name - - - def evaluate(self): - eng = matlab.engine.start_matlab() - result=eng.eval(self.func+self.arguments) - print(result) - -if __name__ == "__main__": - # STEAM_materials_Matlab("rhoCu_nist","(1.8,1,120.0,5)").evaluate() - - - - func_4d = 'CFUN_rhoCuNIST' # function name, this one takes 4 arguments as input and returns a single float for resistivity - T_min = 1.8 - T_max = 300 - B = 1. - RRR = 120.0 - T_ref_RRR = 273. - num_elem = 1000 - T = np.linspace(T_min, T_max, num_elem) - - # make numpy array - numpy2d = np.vstack((T, np.ones(num_elem) * B, np.ones(num_elem) * RRR, np.ones(num_elem) * T_ref_RRR)) - - # make dll func object - sm = STEAM_materials(func_4d, numpy2d.shape[0], numpy2d.shape[1], r'G:\Projects\lhccm\STEAM\MaterialsLibrary\V0.1') - - # call with 2D numpy array and get results back - result_numpy = sm.evaluate(numpy2d) - print(result_numpy) diff --git a/bindings/python/src/steammaterials/__init__.py b/bindings/python/src/steammaterials/__init__.py index e69de29..e63834a 100755 --- a/bindings/python/src/steammaterials/__init__.py +++ b/bindings/python/src/steammaterials/__init__.py @@ -0,0 +1,3 @@ +# Re-export with a more pythonic name. +from .STEAM_materials import STEAM_materials as SteamMaterials + -- GitLab