diff --git a/cadet/__init__.py b/cadet/__init__.py index 8978e6f..13477c2 100644 --- a/cadet/__init__.py +++ b/cadet/__init__.py @@ -2,5 +2,5 @@ __version__ = "0.14.1" -from .cadet import H5 from .cadet import Cadet +from .cadet import H5 diff --git a/cadet/cadet.py b/cadet/cadet.py index 1a3530f..f17e919 100644 --- a/cadet/cadet.py +++ b/cadet/cadet.py @@ -1,8 +1,9 @@ +import warnings + from addict import Dict -import warnings with warnings.catch_warnings(): - warnings.filterwarnings("ignore",category=FutureWarning) + warnings.filterwarnings("ignore", category=FutureWarning) import h5py import numpy import subprocess @@ -17,6 +18,7 @@ from cadet.cadet_dll import CadetDLL + class H5(): pp = pprint.PrettyPrinter(indent=4) @@ -117,10 +119,12 @@ def __setitem__(self, key, value): obj = obj[i] obj[parts[-1]] = value + def is_dll(value): suffix = Path(value).suffix return suffix in {'.so', '.dll'} + class CadetMeta(type): _cadet_runner_class = None _is_file_class = None @@ -144,7 +148,7 @@ def __init__(cls): del cls._cadet_runner_class if is_dll(value): - cls._cadet_runner_class = CadetDLL(value) + cls._cadet_runner_class = CadetDLL(value) cls._is_file_class = False else: cls._cadet_runner_class = CadetFile(value) @@ -154,8 +158,9 @@ def __init__(cls): def cadet_path(cls): del cls._cadet_runner_class + class Cadet(H5, metaclass=CadetMeta): - #cadet_path must be set in order for simulations to run + # cadet_path must be set in order for simulations to run def __init__(self, *data): super().__init__(*data) self._cadet_runner = None @@ -188,7 +193,7 @@ def cadet_path(self, value): del self._cadet_runner if is_dll(value): - self._cadet_runner = CadetDLL(value) + self._cadet_runner = CadetDLL(value) self._is_file = False else: self._cadet_runner = CadetFile(value) @@ -209,14 +214,14 @@ def load_results(self): if runner is not None: runner.load_results(self) - def run(self, timeout = None, check=None): + def run(self, timeout=None, check=None): data = self.cadet_runner.run(simulation=self.root.input, filename=self.filename, timeout=timeout, check=check) - #self.return_information = data + # self.return_information = data return data - def run_load(self, timeout = None, check=None, clear=True): + def run_load(self, timeout=None, check=None, clear=True): data = self.cadet_runner.run(simulation=self.root.input, filename=self.filename, timeout=timeout, check=check) - #self.return_information = data + # self.return_information = data self.load_results() if clear: self.clear() @@ -227,14 +232,15 @@ def clear(self): if runner is not None: runner.clear() + class CadetFile: def __init__(self, cadet_path): self.cadet_path = cadet_path - def run(self, filename = None, simulation=None, timeout = None, check=None): + def run(self, filename=None, simulation=None, timeout=None, check=None): if filename is not None: - data = subprocess.run([self.cadet_path, filename], timeout = timeout, check=check, capture_output=True) + data = subprocess.run([self.cadet_path, filename], timeout=timeout, check=check, capture_output=True) return data else: print("Filename must be set before run can be used") @@ -245,9 +251,10 @@ def clear(self): def load_results(self, sim): sim.load(paths=["/meta", "/output"], update=True) + def convert_from_numpy(data, func): ans = Dict() - for key_original,item in data.items(): + for key_original, item in data.items(): key = func(key_original) if isinstance(item, numpy.ndarray): item = item.tolist() @@ -264,9 +271,10 @@ def convert_from_numpy(data, func): ans[key] = item return ans -def recursively_load_dict( data, func): + +def recursively_load_dict(data, func): ans = Dict() - for key_original,item in data.items(): + for key_original, item in data.items(): key = func(key_original) if isinstance(item, dict): ans[key] = recursively_load_dict(item, func) @@ -274,8 +282,9 @@ def recursively_load_dict( data, func): ans[key] = item return ans + def set_path(obj, path, value): - "paths need to be broken up so that subobjects are correctly made" + """paths need to be broken up so that subobjects are correctly made""" path = path.split('/') path = [i for i in path if i] @@ -285,7 +294,8 @@ def set_path(obj, path, value): temp[path[-1]] = value -def recursively_load( h5file, path, func, paths): + +def recursively_load(h5file, path, func, paths): ans = Dict() if paths is not None: for path in paths: @@ -306,8 +316,8 @@ def recursively_load( h5file, path, func, paths): ans[key] = recursively_load(h5file, local_path + '/', func, None) return ans -def recursively_save(h5file, path, dic, func): +def recursively_save(h5file, path, dic, func): if not isinstance(path, str): raise ValueError("path must be a string") if not isinstance(h5file, h5py._hl.files.File): diff --git a/cadet/cadet_dll.py b/cadet/cadet_dll.py index 88920c5..59cc250 100644 --- a/cadet/cadet_dll.py +++ b/cadet/cadet_dll.py @@ -1,23 +1,31 @@ - -from cmath import sin import ctypes -import numpy + import addict +import numpy + import cadet.cadet_dll_parameterprovider as cadet_dll_parameterprovider + def log_handler(file, func, line, level, level_name, message): - log_print('{} ({}:{:d}) {}'.format(level_name.decode('utf-8') , func.decode('utf-8') , line, message.decode('utf-8') )) + log_print('{} ({}:{:d}) {}'.format( + level_name.decode('utf-8'), + func.decode('utf-8'), + line, + message.decode('utf-8')) + ) + c_cadet_result = ctypes.c_int array_double = ctypes.POINTER(ctypes.POINTER(ctypes.c_double)) - point_int = ctypes.POINTER(ctypes.c_int) + def null(*args): pass + if 0: log_print = print else: @@ -32,31 +40,74 @@ class CADETAPIV010000_DATA(): _data_['getNParTypes'] = ('return', 'drv', 'unitOpId', 'nParTypes') _data_['getSolutionInlet'] = ('return', 'drv', 'unitOpId', 'time', 'data', 'nTime', 'nPort', 'nComp') _data_['getSolutionOutlet'] = ('return', 'drv', 'unitOpId', 'time', 'data', 'nTime', 'nPort', 'nComp') - _data_['getSolutionBulk'] = ('return', 'drv', 'unitOpId', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp') - _data_['getSolutionParticle'] = ('return', 'drv', 'unitOpId', 'parType', 'time', 'data', 'nTime', 'nParShells', 'nAxialCells', 'nRadialCells', 'nComp') - _data_['getSolutionSolid'] = ('return', 'drv', 'unitOpId', 'parType', 'time', 'data', 'nTime', 'nParShells', 'nAxialCells', 'nRadialCells', 'nBound') - _data_['getSolutionFlux'] = ('return', 'drv', 'unitOpId', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp') + _data_['getSolutionBulk'] = ( + 'return', 'drv', 'unitOpId', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp' + ) + _data_['getSolutionParticle'] = ( + 'return', 'drv', 'unitOpId', 'parType', 'time', 'data', 'nTime', + 'nParShells', 'nAxialCells', 'nRadialCells', 'nComp' + ) + _data_['getSolutionSolid'] = ( + 'return', 'drv', 'unitOpId', 'parType', 'time', 'data', 'nTime', + 'nParShells', 'nAxialCells', 'nRadialCells', 'nBound' + ) + _data_['getSolutionFlux'] = ( + 'return', 'drv', 'unitOpId', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp') _data_['getSolutionVolume'] = ('return', 'drv', 'unitOpId', 'time', 'data', 'nTime') _data_['getSolutionDerivativeInlet'] = ('return', 'drv', 'unitOpId', 'time', 'data', 'nTime', 'nPort', 'nComp') _data_['getSolutionDerivativeOutlet'] = ('return', 'drv', 'unitOpId', 'time', 'data', 'nTime', 'nPort', 'nComp') - _data_['getSolutionDerivativeBulk'] = ('return', 'drv', 'unitOpId', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp') - _data_['getSolutionDerivativeParticle'] = ('return', 'drv', 'unitOpId', 'parType', 'time', 'data', 'nTime', 'nParShells', 'nAxialCells', 'nRadialCells', 'nComp') - _data_['getSolutionDerivativeSolid'] = ('return', 'drv', 'unitOpId', 'parType', 'time', 'data', 'nTime', 'nParShells', 'nAxialCells', 'nRadialCells', 'nBound') - _data_['getSolutionDerivativeFlux'] = ('return', 'drv', 'unitOpId', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp') + _data_['getSolutionDerivativeBulk'] = ( + 'return', 'drv', 'unitOpId', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp' + ) + _data_['getSolutionDerivativeParticle'] = ( + 'return', 'drv', 'unitOpId', 'parType', 'time', 'data', 'nTime', + 'nParShells', 'nAxialCells', 'nRadialCells', 'nComp' + ) + _data_['getSolutionDerivativeSolid'] = ( + 'return', 'drv', 'unitOpId', 'parType', 'time', 'data', 'nTime', + 'nParShells', 'nAxialCells', 'nRadialCells', 'nBound' + ) + _data_['getSolutionDerivativeFlux'] = ( + 'return', 'drv', 'unitOpId', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp' + ) _data_['getSolutionDerivativeVolume'] = ('return', 'drv', 'unitOpId', 'time', 'data', 'nTime') _data_['getSensitivityInlet'] = ('return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nPort', 'nComp') _data_['getSensitivityOutlet'] = ('return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nPort', 'nComp') - _data_['getSensitivityBulk'] = ('return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp') - _data_['getSensitivityParticle'] = ('return', 'drv', 'unitOpId', 'idx', 'parType', 'time', 'data', 'nTime', 'nParShells', 'nAxialCells', 'nRadialCells', 'nComp') - _data_['getSensitivitySolid'] = ('return', 'drv', 'unitOpId', 'idx', 'parType', 'time', 'data', 'nTime', 'nParShells', 'nAxialCells', 'nRadialCells', 'nBound') - _data_['getSensitivityFlux'] = ('return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp') + _data_['getSensitivityBulk'] = ( + 'return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp' + ) + _data_['getSensitivityParticle'] = ( + 'return', 'drv', 'unitOpId', 'idx', 'parType', 'time', 'data', 'nTime', + 'nParShells', 'nAxialCells', 'nRadialCells', 'nComp' + ) + _data_['getSensitivitySolid'] = ( + 'return', 'drv', 'unitOpId', 'idx', 'parType', 'time', 'data', + 'nTime', 'nParShells', 'nAxialCells', 'nRadialCells', 'nBound' + ) + _data_['getSensitivityFlux'] = ( + 'return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp' + ) _data_['getSensitivityVolume'] = ('return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime') - _data_['getSensitivityDerivativeInlet'] = ('return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nPort', 'nComp') - _data_['getSensitivityDerivativeOutlet'] = ('return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nPort', 'nComp') - _data_['getSensitivityDerivativeBulk'] = ('return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp') - _data_['getSensitivityDerivativeParticle'] = ('return', 'drv', 'unitOpId', 'idx', 'parType', 'time', 'data', 'nTime', 'nParShells', 'nAxialCells', 'nRadialCells', 'nComp') - _data_['getSensitivityDerivativeSolid'] = ('return', 'drv', 'unitOpId', 'idx', 'parType', 'time', 'data', 'nTime', 'nParShells', 'nAxialCells', 'nRadialCells', 'nBound') - _data_['getSensitivityDerivativeFlux'] = ('return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp') + _data_['getSensitivityDerivativeInlet'] = ( + 'return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nPort', 'nComp' + ) + _data_['getSensitivityDerivativeOutlet'] = ( + 'return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nPort', 'nComp' + ) + _data_['getSensitivityDerivativeBulk'] = ( + 'return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp' + ) + _data_['getSensitivityDerivativeParticle'] = ( + 'return', 'drv', 'unitOpId', 'idx', 'parType', 'time', 'data', + 'nTime', 'nParShells', 'nAxialCells', 'nRadialCells', 'nComp' + ) + _data_['getSensitivityDerivativeSolid'] = ( + 'return', 'drv', 'unitOpId', 'idx', 'parType', 'time', + 'data', 'nTime', 'nParShells', 'nAxialCells', 'nRadialCells', 'nBound' + ) + _data_['getSensitivityDerivativeFlux'] = ( + 'return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime', 'nAxialCells', 'nRadialCells', 'nComp' + ) _data_['getSensitivityDerivativeVolume'] = ('return', 'drv', 'unitOpId', 'idx', 'time', 'data', 'nTime') lookup_prototype = { @@ -98,7 +149,7 @@ def setup_api(): for key, value in CADETAPIV010000_DATA._data_.items(): args = tuple(CADETAPIV010000_DATA.lookup_prototype[key] for key in value) - _fields_.append( (key, ctypes.CFUNCTYPE(*args)) ) + _fields_.append((key, ctypes.CFUNCTYPE(*args))) return _fields_ @@ -108,8 +159,10 @@ class CADETAPIV010000(ctypes.Structure): def null(obj): - "do nothing" + """do nothing""" return obj + + class SimulationResult: def __init__(self, api, driver): @@ -149,7 +202,7 @@ def load_data(self, unit, get_solution, get_solution_str, idx=None, parType=None dims.append(dim) data = numpy.ctypeslib.as_array(vars['data'], shape=shape) - time = numpy.ctypeslib.as_array(vars['time'], shape=(vars['nTime'].value, )) + time = numpy.ctypeslib.as_array(vars['time'], shape=(vars['nTime'].value,)) if own_data: return time.copy(), data.copy(), dims @@ -178,25 +231,30 @@ def volume(self, unit, own_data=True): return self.load_data(unit, self._api.getSolutionVolume, 'getSolutionVolume', own_data=own_data) def derivativeInlet(self, unit, own_data=True): - return self.load_data(unit, self._api.getSolutionDerivativeInlet, 'getSolutionDerivativeInlet', own_data=own_data) + return self.load_data(unit, self._api.getSolutionDerivativeInlet, 'getSolutionDerivativeInlet', + own_data=own_data) def derivativeOutlet(self, unit, own_data=True): - return self.load_data(unit, self._api.getSolutionDerivativeOutlet, 'getSolutionDerivativeOutlet', own_data=own_data) + return self.load_data(unit, self._api.getSolutionDerivativeOutlet, 'getSolutionDerivativeOutlet', + own_data=own_data) def derivativeBulk(self, unit, own_data=True): return self.load_data(unit, self._api.getSolutionDerivativeBulk, 'getSolutionDerivativeBulk', own_data=own_data) def derivativeParticle(self, unit, parType, own_data=True): - return self.load_data(unit, self._api.getSolutionDerivativeParticle, 'getSolutionDerivativeParticle', own_data=own_data) + return self.load_data(unit, self._api.getSolutionDerivativeParticle, 'getSolutionDerivativeParticle', + own_data=own_data) def derivativeSolid(self, unit, parType, own_data=True): - return self.load_data(unit, self._api.getSolutionDerivativeSolid, 'getSolutionDerivativeSolid', own_data=own_data) + return self.load_data(unit, self._api.getSolutionDerivativeSolid, 'getSolutionDerivativeSolid', + own_data=own_data) def derivativeFlux(self, unit, own_data=True): return self.load_data(unit, self._api.getSolutionDerivativeFlux, 'getSolutionDerivativeFlux', own_data=own_data) def derivativeVolume(self, unit, own_data=True): - return self.load_data(unit, self._api.getSolutionDerivativeVolume, 'getSolutionDerivativeVolume', own_data=own_data) + return self.load_data(unit, self._api.getSolutionDerivativeVolume, 'getSolutionDerivativeVolume', + own_data=own_data) def sensitivityInlet(self, unit, idx, own_data=True): return self.load_data(unit, self._api.getSensitivityInlet, 'getSensitivityInlet', idx=idx, own_data=own_data) @@ -208,10 +266,12 @@ def sensitivityBulk(self, unit, idx, own_data=True): return self.load_data(unit, self._api.getSensitivityBulk, 'getSensitivityBulk', idx=idx, own_data=own_data) def sensitivityParticle(self, unit, idx, parType, own_data=True): - return self.load_data(unit, self._api.getSensitivityParticle, 'getSensitivityParticle', idx=idx, parType=parType, own_data=own_data) + return self.load_data(unit, self._api.getSensitivityParticle, 'getSensitivityParticle', idx=idx, + parType=parType, own_data=own_data) def sensitivitySolid(self, unit, idx, parType, own_data=True): - return self.load_data(unit, self._api.getSensitivitySolid, 'getSensitivitySolid', idx=idx, parType=parType, own_data=own_data) + return self.load_data(unit, self._api.getSensitivitySolid, 'getSensitivitySolid', idx=idx, parType=parType, + own_data=own_data) def sensitivityFlux(self, unit, idx, own_data=True): return self.load_data(unit, self._api.getSensitivityFlux, 'getSensitivityFlux', idx=idx, own_data=own_data) @@ -220,25 +280,32 @@ def sensitivityVolume(self, unit, idx, own_data=True): return self.load_data(unit, self._api.getSensitivityVolume, 'getSensitivityVolume', idx=idx, own_data=own_data) def sensitivityDerivativeInlet(self, unit, idx, own_data=True): - return self.load_data(unit, self._api.getSensitivityDerivativeInlet, 'getSensitivityDerivativeInlet', idx=idx, own_data=own_data) + return self.load_data(unit, self._api.getSensitivityDerivativeInlet, 'getSensitivityDerivativeInlet', idx=idx, + own_data=own_data) def sensitivityDerivativeOutlet(self, unit, idx, own_data=True): - return self.load_data(unit, self._api.getSensitivityDerivativeOutlet, 'getSensitivityDerivativeOutlet', idx=idx, own_data=own_data) + return self.load_data(unit, self._api.getSensitivityDerivativeOutlet, 'getSensitivityDerivativeOutlet', idx=idx, + own_data=own_data) def sensitivityDerivativeBulk(self, unit, idx, own_data=True): - return self.load_data(unit, self._api.getSensitivityDerivativeBulk, 'getSensitivityDerivativeBulk', idx=idx, own_data=own_data) + return self.load_data(unit, self._api.getSensitivityDerivativeBulk, 'getSensitivityDerivativeBulk', idx=idx, + own_data=own_data) def sensitivityDerivativeParticle(self, unit, idx, parType, own_data=True): - return self.load_data(unit, self._api.getSensitivityDerivativeParticle, 'getSensitivityDerivativeParticle', idx=idx, parType=parType, own_data=own_data) + return self.load_data(unit, self._api.getSensitivityDerivativeParticle, 'getSensitivityDerivativeParticle', + idx=idx, parType=parType, own_data=own_data) def sensitivityDerivativeSolid(self, unit, idx, parType, own_data=True): - return self.load_data(unit, self._api.getSensitivityDerivativeSolid, 'getSensitivityDerivativeSolid', idx=idx, parType=parType, own_data=own_data) + return self.load_data(unit, self._api.getSensitivityDerivativeSolid, 'getSensitivityDerivativeSolid', idx=idx, + parType=parType, own_data=own_data) def sensitivityDerivativeFlux(self, unit, idx, own_data=True): - return self.load_data(unit, self._api.getSensitivityDerivativeFlux, 'getSensitivityDerivativeFlux', idx=idx, own_data=own_data) + return self.load_data(unit, self._api.getSensitivityDerivativeFlux, 'getSensitivityDerivativeFlux', idx=idx, + own_data=own_data) def sensitivityDerivativeVolume(self, unit, idx, own_data=True): - return self.load_data(unit, self._api.getSensitivityDerivativeVolume, 'getSensitivityDerivativeVolume', idx=idx, own_data=own_data) + return self.load_data(unit, self._api.getSensitivityDerivativeVolume, 'getSensitivityDerivativeVolume', idx=idx, + own_data=own_data) class CadetDLL: @@ -306,8 +373,7 @@ def __del__(self): log_print('deleteDriver()') self._api.deleteDriver(self._driver) - - def run(self, filename = None, simulation=None, timeout = None, check=None): + def run(self, filename=None, simulation=None, timeout=None, check=None): pp = cadet_dll_parameterprovider.PARAMETERPROVIDER(simulation) self._api.runSimulation(self._driver, ctypes.byref(pp)) @@ -338,7 +404,7 @@ def load_solution(self, sim, solution_fun, solution_str): def load_solution_io(self, sim, solution_fun, solution_str): solution = addict.Dict() if self.res is not None: - for key,value in sim.root.input['return'].items(): + for key, value in sim.root.input['return'].items(): if key.startswith('unit'): if value[f'write_{solution_str}']: unit = int(key[-3:]) @@ -375,7 +441,7 @@ def load_solution_io(self, sim, solution_fun, solution_str): solution[key][f'{solution_str}_port_{port:03d}_comp_{comp:03d}'] = comp_out else: for comp in range(out.shape[nComp]): - comp_out = numpy.squeeze(out[...,comp]) + comp_out = numpy.squeeze(out[..., comp]) solution[key][f'{solution_str}_comp_{comp:03d}'] = comp_out else: if split_ports_data: @@ -409,19 +475,19 @@ def load_solid(self, sim): def load_flux(self, sim): return self.load_solution(sim, self.res.flux, 'solution_flux') - + def load_flux(self, sim): return self.load_solution(sim, self.res.flux, 'solution_flux') - + def load_volume(self, sim): return self.load_solution(sim, self.res.volume, 'solution_volume') - + def load_derivative_inlet(self, sim): return self.load_solution_io(sim, self.res.derivativeInlet, 'soldot_inlet') - + def load_derivative_outlet(self, sim): return self.load_solution_io(sim, self.res.derivativeOutlet, 'soldot_outlet') - + def load_derivative_bulk(self, sim): return self.load_solution(sim, self.res.derivativeBulk, 'soldot_bulk') @@ -494,17 +560,17 @@ def load_results(self, sim): sim.root.output.solution.update(self.load_derivative_solid(sim)) sim.root.output.solution.update(self.load_derivative_flux(sim)) sim.root.output.solution.update(self.load_derivative_volume(sim)) - #sim.root.output.solution.update(self.load_sensitivity_inlet(sim)) - #sim.root.output.solution.update(self.load_sensitivity_outlet(sim)) - #sim.root.output.solution.update(self.load_sensitivity_bulk(sim)) - #sim.root.output.solution.update(self.load_sensitivity_particle(sim)) - #sim.root.output.solution.update(self.load_sensitivity_solid(sim)) - #sim.root.output.solution.update(self.load_sensitivity_flux(sim)) - #sim.root.output.solution.update(self.load_sensitivity_volume(sim)) - #sim.root.output.solution.update(self.load_sensitivity_derivative_inlet(sim)) - #sim.root.output.solution.update(self.load_sensitivity_derivative_outlet(sim)) - #sim.root.output.solution.update(self.load_sensitivity_derivative_bulk(sim)) - #sim.root.output.solution.update(self.load_sensitivity_derivative_particle(sim)) - #sim.root.output.solution.update(self.load_sensitivity_derivative_solid(sim)) - #sim.root.output.solution.update(self.load_sensitivity_derivative_flux(sim)) - #sim.root.output.solution.update(self.load_sensitivity_derivative_volume(sim)) + # sim.root.output.solution.update(self.load_sensitivity_inlet(sim)) + # sim.root.output.solution.update(self.load_sensitivity_outlet(sim)) + # sim.root.output.solution.update(self.load_sensitivity_bulk(sim)) + # sim.root.output.solution.update(self.load_sensitivity_particle(sim)) + # sim.root.output.solution.update(self.load_sensitivity_solid(sim)) + # sim.root.output.solution.update(self.load_sensitivity_flux(sim)) + # sim.root.output.solution.update(self.load_sensitivity_volume(sim)) + # sim.root.output.solution.update(self.load_sensitivity_derivative_inlet(sim)) + # sim.root.output.solution.update(self.load_sensitivity_derivative_outlet(sim)) + # sim.root.output.solution.update(self.load_sensitivity_derivative_bulk(sim)) + # sim.root.output.solution.update(self.load_sensitivity_derivative_particle(sim)) + # sim.root.output.solution.update(self.load_sensitivity_derivative_solid(sim)) + # sim.root.output.solution.update(self.load_sensitivity_derivative_flux(sim)) + # sim.root.output.solution.update(self.load_sensitivity_derivative_volume(sim)) diff --git a/cadet/cadet_dll_parameterprovider.py b/cadet/cadet_dll_parameterprovider.py index 73e03d4..41780cb 100644 --- a/cadet/cadet_dll_parameterprovider.py +++ b/cadet/cadet_dll_parameterprovider.py @@ -1,23 +1,26 @@ - import ctypes -import cadet.cadet_dll_utils as utils + import addict +import cadet.cadet_dll_utils as utils + c_cadet_result = ctypes.c_int array_double = ctypes.POINTER(ctypes.POINTER(ctypes.c_double)) - point_int = ctypes.POINTER(ctypes.c_int) + def null(*args): pass + if 0: log_print = print else: log_print = null + class NestedDictReader: def __init__(self, data): @@ -41,6 +44,7 @@ def pop_scope(self): def current(self): return self._cursor[-1] + def recursively_convert_dict(data): ans = addict.Dict() for key_original, item in data.items(): @@ -66,7 +70,7 @@ def __init__(self, simulation): self.userData = NestedDictReader(sim_input) - #figure out how to add this to the class + # figure out how to add this to the class self.getDouble = self._fields_[1][1](utils.param_provider_get_double) self.getInt = self._fields_[2][1](utils.param_provider_get_int) self.getBool = self._fields_[3][1](utils.param_provider_get_bool) @@ -89,48 +93,103 @@ def __init__(self, simulation): self.popScope = self._fields_[17][1](utils.param_provider_pop_scope) _fields_ = [ - ('userData', ctypes.py_object), - - ('getDouble', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.POINTER(ctypes.c_double))), - ('getInt', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int)), - ('getBool', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.POINTER(ctypes.c_uint8))), - ('getString', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p))), - - ('getDoubleArray', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int, array_double)), - ('getIntArray', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int, ctypes.POINTER(point_int))), - ('getBoolArray', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int, ctypes.POINTER(ctypes.POINTER(ctypes.c_uint8)))), - ('getStringArray', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int, ctypes.POINTER(ctypes.POINTER(ctypes.c_char_p)))), - - ('getDoubleArrayItem', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(ctypes.c_double))), - ('getIntArrayItem', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.c_int, point_int)), - ('getBoolArrayItem', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(ctypes.c_uint8))), - ('getStringArrayItem', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(ctypes.c_char_p))), - - ('exists', ctypes.CFUNCTYPE(ctypes.c_int, ctypes.py_object, ctypes.c_char_p)), - ('isArray', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.POINTER(ctypes.c_uint8))), - ('numElements', ctypes.CFUNCTYPE(ctypes.c_int, ctypes.py_object, ctypes.c_char_p)), - ('pushScope', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p)), - ('popScope', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object)), + ( + 'userData', + ctypes.py_object + ), + ( + 'getDouble', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.POINTER(ctypes.c_double)) + ), + ( + 'getInt', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int) + ), + ( + 'getBool', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.POINTER(ctypes.c_uint8)) + ), + ( + 'getString', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p)) + ), + ( + 'getDoubleArray', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int, array_double) + ), + ( + 'getIntArray', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int, ctypes.POINTER(point_int)) + ), + ( + 'getBoolArray', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int, + ctypes.POINTER(ctypes.POINTER(ctypes.c_uint8))) + ), + ( + 'getStringArray', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int, + ctypes.POINTER(ctypes.POINTER(ctypes.c_char_p))) + ), + ( + 'getDoubleArrayItem', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.c_int, + ctypes.POINTER(ctypes.c_double)) + ), + ( + 'getIntArrayItem', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.c_int, point_int) + ), + ( + 'getBoolArrayItem', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.c_int, + ctypes.POINTER(ctypes.c_uint8)) + ), + ( + 'getStringArrayItem', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.c_int, + ctypes.POINTER(ctypes.c_char_p)) + ), + ( + 'exists', + ctypes.CFUNCTYPE(ctypes.c_int, ctypes.py_object, ctypes.c_char_p) + ), + ( + 'isArray', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.POINTER(ctypes.c_uint8)) + ), + ( + 'numElements', + ctypes.CFUNCTYPE(ctypes.c_int, ctypes.py_object, ctypes.c_char_p) + ), + ( + 'pushScope', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p) + ), + ( + 'popScope', + ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object) + ), ] - #these don't work right now but work when places on an instance - #getDouble = _fields_[1][1](utils.param_provider_get_double) - #getInt = _fields_[2][1](utils.param_provider_get_int) - #getBool = _fields_[3][1](utils.param_provider_get_bool) - #getString = _fields_[4][1](utils.param_provider_get_string) - - #getDoubleArray = _fields_[5][1](utils.param_provider_get_double_array) - #getIntArray = _fields_[6][1](utils.param_provider_get_int_array) - #getBoolArray = ctypes.cast(None, _fields_[7][1]) - #getStringArray = ctypes.cast(None, _fields_[8][1]) - - #getDoubleArrayItem = _fields_[9][1](utils.param_provider_get_double_array_item) - #getIntArrayItem = _fields_[10][1](utils.param_provider_get_int_array_item) - #getBoolArrayItem = _fields_[11][1](utils.param_provider_get_bool_array_item) - #getStringArrayItem = _fields_[12][1](utils.param_provider_get_string_array_item) - - #exists = _fields_[13][1](utils.param_provider_exists) - #isArray = _fields_[14][1](utils.param_provider_is_array) - #numElements = _fields_[15][1](utils.param_provider_num_elements) - #pushScope = _fields_[16][1](utils.param_provider_push_scope) - #popScope = _fields_[17][1](utils.param_provider_pop_scope) \ No newline at end of file + # these don't work right now but work when places on an instance + # getDouble = _fields_[1][1](utils.param_provider_get_double) + # getInt = _fields_[2][1](utils.param_provider_get_int) + # getBool = _fields_[3][1](utils.param_provider_get_bool) + # getString = _fields_[4][1](utils.param_provider_get_string) + + # getDoubleArray = _fields_[5][1](utils.param_provider_get_double_array) + # getIntArray = _fields_[6][1](utils.param_provider_get_int_array) + # getBoolArray = ctypes.cast(None, _fields_[7][1]) + # getStringArray = ctypes.cast(None, _fields_[8][1]) + + # getDoubleArrayItem = _fields_[9][1](utils.param_provider_get_double_array_item) + # getIntArrayItem = _fields_[10][1](utils.param_provider_get_int_array_item) + # getBoolArrayItem = _fields_[11][1](utils.param_provider_get_bool_array_item) + # getStringArrayItem = _fields_[12][1](utils.param_provider_get_string_array_item) + + # exists = _fields_[13][1](utils.param_provider_exists) + # isArray = _fields_[14][1](utils.param_provider_is_array) + # numElements = _fields_[15][1](utils.param_provider_num_elements) + # pushScope = _fields_[16][1](utils.param_provider_push_scope) + # popScope = _fields_[17][1](utils.param_provider_pop_scope) diff --git a/cadet/cadet_dll_utils.py b/cadet/cadet_dll_utils.py index d4b4045..3b11be7 100644 --- a/cadet/cadet_dll_utils.py +++ b/cadet/cadet_dll_utils.py @@ -1,9 +1,12 @@ import ctypes + import numpy + def null(*args): pass + if 0: log_print = print else: @@ -75,7 +78,8 @@ def param_provider_get_string(reader, name, val): if n in c: o = c[n] - #we have one of array of strings, array of bytestrings, bytestring or string or something convertable to one of these + # we have one of array of strings, array of bytestrings, + # bytestring or string or something convertable to one of these if hasattr(o, 'encode'): bytes_val = o.encode('utf-8') elif hasattr(o, 'decode'): @@ -262,15 +266,16 @@ def param_provider_num_elements(reader, name): log_print('NUMELEMENTS {}: {}'.format(n, 1)) return 1 + def param_provider_push_scope(reader, name): - n = name.decode('utf-8') - - if reader.push_scope(n): - return 0 - else: - return -1 + n = name.decode('utf-8') + + if reader.push_scope(n): + return 0 + else: + return -1 def param_provider_pop_scope(reader): - reader.pop_scope() - return 0 \ No newline at end of file + reader.pop_scope() + return 0