Skip to content

Commit ea8a762

Browse files
Immudzenschmoelder
authored andcommitted
fix calling convention
1 parent 1e61c4d commit ea8a762

File tree

1 file changed

+86
-87
lines changed

1 file changed

+86
-87
lines changed

cadet/cadet_dll.py

Lines changed: 86 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,25 @@ class CADETAPIV010000(ctypes.Structure):
139139
class NestedDictReader:
140140

141141
def __init__(self, data):
142-
self.__root = data
143-
self.__cursor = []
144-
self.__cursor.append(data)
142+
self._root = data
143+
self._cursor = []
144+
self._cursor.append(data)
145145
self.buffer = None
146146

147147
def push_scope(self, scope):
148-
if scope in self.__cursor[-1]:
148+
if scope in self._cursor[-1]:
149149
log_print('Entering scope {}'.format(scope))
150-
self.__cursor.append(self.__cursor[-1][scope])
150+
self._cursor.append(self._cursor[-1][scope])
151151
return True
152152

153153
return False
154154

155155
def pop_scope(self):
156-
self.__cursor.pop()
156+
self._cursor.pop()
157157
log_print('Exiting scope')
158158

159159
def current(self):
160-
return self.__cursor[-1]
160+
return self._cursor[-1]
161161

162162

163163
def param_provider_get_double(reader, name, val):
@@ -426,159 +426,158 @@ def param_provider_pop_scope(reader):
426426
reader.pop_scope()
427427
return 0
428428

429-
429+
def null(obj):
430+
"do nothing"
431+
return obj
430432
class SimulationResult:
431433

432434
def __init__(self, api, driver):
433-
self.__api = api
434-
self.__driver = driver
435+
self._api = api
436+
self._driver = driver
435437

436438
def load_data(self, unit, get_solution, get_solution_str, idx=None, parType=None, own_data=True):
437-
args = {}
439+
vars = {}
440+
wrappers = {}
438441
for key in CADETAPIV010000_DATA._data_[get_solution_str]:
439442
if key == 'return':
440443
continue
441444
elif key == 'drv':
442-
args['drv'] = self.__driver
445+
vars['drv'] = self._driver
446+
wrappers[key] = null
443447
elif key == 'unitOpId':
444-
args['unitOpId'] = unit
448+
vars['unitOpId'] = unit
449+
wrappers[key] = null
445450
elif key == 'idx':
446-
args['idx'] = idx
451+
vars['idx'] = idx
452+
wrappers[key] = null
447453
elif key == 'parType':
448-
args['parType'] = parType
454+
vars['parType'] = parType
455+
wrappers[key] = null
449456
else:
450-
args[key] = ctypes.byref(CADETAPIV010000_DATA.lookup_call[key]())
451-
452-
result = get_solution(self.__driver, unit, *tuple(args.values()))
453-
454-
shape = (args['nTime'].value,)
455-
for arg in args:
456-
if arg == 'nPort':
457-
shape += args['nPort'].value
458-
elif arg == 'nParShells':
459-
shape += args['nParShells'].value
460-
elif 'nAxialCells' in args:
461-
shape += args['nAxialCells'].value
462-
elif 'nRadialCells' in args:
463-
shape += args['nRadialCells'].value
464-
elif 'nComp' in args:
465-
shape += args['nComp'].value
466-
elif 'nBound' in args:
467-
shape += args['nBound'].value
468-
469-
data = numpy.ctypeslib.as_array(args['data'], shape=shape)
470-
time = numpy.ctypeslib.as_array(args['time'], shape=(args['nTime'].value, ))
457+
vars[key] = CADETAPIV010000_DATA.lookup_call[key]()
458+
wrappers[key] = ctypes.byref
459+
460+
result = get_solution(*tuple(wrappers[var_key](var_value) for var_key, var_value in vars.items()))
461+
462+
shape = []
463+
dimensions = ['nTime', 'nPort', 'nParShells', 'nAxialCells', 'nRadialCells', 'nComp', 'nBound']
464+
for dim in dimensions:
465+
if dim in vars and vars[dim].value:
466+
shape.append(vars[dim].value)
467+
468+
data = numpy.ctypeslib.as_array(vars['data'], shape=shape)
469+
time = numpy.ctypeslib.as_array(vars['time'], shape=(vars['nTime'].value, ))
471470

472471
if own_data:
473472
return (time.copy(), data.copy())
474473
else:
475474
return (time, data)
476475

477476
def inlet(self, unit, own_data=True):
478-
return self.load_data(unit, self.__api.getSolutionInlet, 'getSolutionInlet', own_data=own_data)
477+
return self.load_data(unit, self._api.getSolutionInlet, 'getSolutionInlet', own_data=own_data)
479478

480479
def outlet(self, unit, own_data=True):
481-
return self.load_data(unit, self.__api.getSolutionOutlet, 'getSolutionOutlet', own_data=own_data)
480+
return self.load_data(unit, self._api.getSolutionOutlet, 'getSolutionOutlet', own_data=own_data)
482481

483482
def bulk(self, unit, own_data=True):
484-
return self.load_data(unit, self.__api.getSolutionBulk, 'getSolutionBulk', own_data=own_data)
483+
return self.load_data(unit, self._api.getSolutionBulk, 'getSolutionBulk', own_data=own_data)
485484

486485
def particle(self, unit, parType, own_data=True):
487-
return self.load_data(unit, self.__api.getSolutionBulk, 'getSolutionBulk', own_data=own_data)
486+
return self.load_data(unit, self._api.getSolutionBulk, 'getSolutionBulk', own_data=own_data)
488487

489488
def solid(self, unit, parType, own_data=True):
490-
return self.load_data(unit, self.__api.getSolutionSolid, 'getSolutionSolid', own_data=own_data)
489+
return self.load_data(unit, self._api.getSolutionSolid, 'getSolutionSolid', own_data=own_data)
491490

492491
def flux(self, unit, own_data=True):
493-
return self.load_data(unit, self.__api.getSolutionFlux, 'getSolutionFlux', own_data=own_data)
492+
return self.load_data(unit, self._api.getSolutionFlux, 'getSolutionFlux', own_data=own_data)
494493

495494
def volume(self, unit, own_data=True):
496-
return self.load_data(unit, self.__api.getSolutionVolume, 'getSolutionVolume', own_data=own_data)
495+
return self.load_data(unit, self._api.getSolutionVolume, 'getSolutionVolume', own_data=own_data)
497496

498497
def derivativeInlet(self, unit, own_data=True):
499-
return self.load_data(unit, self.__api.getSolutionDerivativeInlet, 'getSolutionDerivativeInlet', own_data=own_data)
498+
return self.load_data(unit, self._api.getSolutionDerivativeInlet, 'getSolutionDerivativeInlet', own_data=own_data)
500499

501500
def derivativeOutlet(self, unit, own_data=True):
502-
return self.load_data(unit, self.__api.getSolutionDerivativeOutlet, 'getSolutionDerivativeOutlet', own_data=own_data)
501+
return self.load_data(unit, self._api.getSolutionDerivativeOutlet, 'getSolutionDerivativeOutlet', own_data=own_data)
503502

504503
def derivativeBulk(self, unit, own_data=True):
505-
return self.load_data(unit, self.__api.getSolutionDerivativeBulk, 'getSolutionDerivativeBulk', own_data=own_data)
504+
return self.load_data(unit, self._api.getSolutionDerivativeBulk, 'getSolutionDerivativeBulk', own_data=own_data)
506505

507506
def derivativeParticle(self, unit, parType, own_data=True):
508-
return self.load_data(unit, self.__api.getSolutionDerivativeParticle, 'getSolutionDerivativeParticle', own_data=own_data)
507+
return self.load_data(unit, self._api.getSolutionDerivativeParticle, 'getSolutionDerivativeParticle', own_data=own_data)
509508

510509
def derivativeSolid(self, unit, parType, own_data=True):
511-
return self.load_data(unit, self.__api.getSolutionDerivativeSolid, 'getSolutionDerivativeSolid', own_data=own_data)
510+
return self.load_data(unit, self._api.getSolutionDerivativeSolid, 'getSolutionDerivativeSolid', own_data=own_data)
512511

513512
def derivativeFlux(self, unit, own_data=True):
514-
return self.load_data(unit, self.__api.getSolutionDerivativeFlux, 'getSolutionDerivativeFlux', own_data=own_data)
513+
return self.load_data(unit, self._api.getSolutionDerivativeFlux, 'getSolutionDerivativeFlux', own_data=own_data)
515514

516515
def derivativeVolume(self, unit, own_data=True):
517-
return self.load_data(unit, self.__api.getSolutionDerivativeVolume, 'getSolutionDerivativeVolume', own_data=own_data)
516+
return self.load_data(unit, self._api.getSolutionDerivativeVolume, 'getSolutionDerivativeVolume', own_data=own_data)
518517

519518
def sensitivityInlet(self, unit, idx, own_data=True):
520-
return self.load_data(unit, self.__api.getSensitivityInlet, 'getSensitivityInlet', idx=idx, own_data=own_data)
519+
return self.load_data(unit, self._api.getSensitivityInlet, 'getSensitivityInlet', idx=idx, own_data=own_data)
521520

522521
def sensitivityOutlet(self, unit, idx, own_data=True):
523-
return self.load_data(unit, self.__api.getSensitivityOutlet, 'getSensitivityOutlet', idx=idx, own_data=own_data)
522+
return self.load_data(unit, self._api.getSensitivityOutlet, 'getSensitivityOutlet', idx=idx, own_data=own_data)
524523

525524
def sensitivityBulk(self, unit, idx, own_data=True):
526-
return self.load_data(unit, self.__api.getSensitivityBulk, 'getSensitivityBulk', idx=idx, own_data=own_data)
525+
return self.load_data(unit, self._api.getSensitivityBulk, 'getSensitivityBulk', idx=idx, own_data=own_data)
527526

528527
def sensitivityParticle(self, unit, idx, parType, own_data=True):
529-
return self.load_data(unit, self.__api.getSensitivityParticle, 'getSensitivityParticle', idx=idx, parType=parType, own_data=own_data)
528+
return self.load_data(unit, self._api.getSensitivityParticle, 'getSensitivityParticle', idx=idx, parType=parType, own_data=own_data)
530529

531530
def sensitivitySolid(self, unit, idx, parType, own_data=True):
532-
return self.load_data(unit, self.__api.getSensitivitySolid, 'getSensitivitySolid', idx=idx, parType=parType, own_data=own_data)
531+
return self.load_data(unit, self._api.getSensitivitySolid, 'getSensitivitySolid', idx=idx, parType=parType, own_data=own_data)
533532

534533
def sensitivityFlux(self, unit, idx, own_data=True):
535-
return self.load_data(unit, self.__api.getSensitivityFlux, 'getSensitivityFlux', idx=idx, own_data=own_data)
534+
return self.load_data(unit, self._api.getSensitivityFlux, 'getSensitivityFlux', idx=idx, own_data=own_data)
536535

537536
def sensitivityVolume(self, unit, idx, own_data=True):
538-
return self.load_data(unit, self.__api.getSensitivityVolume, 'getSensitivityVolume', idx=idx, own_data=own_data)
537+
return self.load_data(unit, self._api.getSensitivityVolume, 'getSensitivityVolume', idx=idx, own_data=own_data)
539538

540539
def sensitivityDerivativeInlet(self, unit, idx, own_data=True):
541-
return self.load_data(unit, self.__api.getSensitivityDerivativeInlet, 'getSensitivityDerivativeInlet', idx=idx, own_data=own_data)
540+
return self.load_data(unit, self._api.getSensitivityDerivativeInlet, 'getSensitivityDerivativeInlet', idx=idx, own_data=own_data)
542541

543542
def sensitivityDerivativeOutlet(self, unit, idx, own_data=True):
544-
return self.load_data(unit, self.__api.getSensitivityDerivativeOutlet, 'getSensitivityDerivativeOutlet', idx=idx, own_data=own_data)
543+
return self.load_data(unit, self._api.getSensitivityDerivativeOutlet, 'getSensitivityDerivativeOutlet', idx=idx, own_data=own_data)
545544

546545
def sensitivityDerivativeBulk(self, unit, idx, own_data=True):
547-
return self.load_data(unit, self.__api.getSensitivityDerivativeBulk, 'getSensitivityDerivativeBulk', idx=idx, own_data=own_data)
546+
return self.load_data(unit, self._api.getSensitivityDerivativeBulk, 'getSensitivityDerivativeBulk', idx=idx, own_data=own_data)
548547

549548
def sensitivityDerivativeParticle(self, unit, idx, parType, own_data=True):
550-
return self.load_data(unit, self.__api.getSensitivityDerivativeParticle, 'getSensitivityDerivativeParticle', idx=idx, parType=parType, own_data=own_data)
549+
return self.load_data(unit, self._api.getSensitivityDerivativeParticle, 'getSensitivityDerivativeParticle', idx=idx, parType=parType, own_data=own_data)
551550

552551
def sensitivityDerivativeSolid(self, unit, idx, parType, own_data=True):
553-
return self.load_data(unit, self.__api.getSensitivityDerivativeSolid, 'getSensitivityDerivativeSolid', idx=idx, parType=parType, own_data=own_data)
552+
return self.load_data(unit, self._api.getSensitivityDerivativeSolid, 'getSensitivityDerivativeSolid', idx=idx, parType=parType, own_data=own_data)
554553

555554
def sensitivityDerivativeFlux(self, unit, idx, own_data=True):
556-
return self.load_data(unit, self.__api.getSensitivityDerivativeFlux, 'getSensitivityDerivativeFlux', idx=idx, own_data=own_data)
555+
return self.load_data(unit, self._api.getSensitivityDerivativeFlux, 'getSensitivityDerivativeFlux', idx=idx, own_data=own_data)
557556

558557
def sensitivityDerivativeVolume(self, unit, idx, own_data=True):
559-
return self.load_data(unit, self.__api.getSensitivityDerivativeVolume, 'getSensitivityDerivativeVolume', idx=idx, own_data=own_data)
558+
return self.load_data(unit, self._api.getSensitivityDerivativeVolume, 'getSensitivityDerivativeVolume', idx=idx, own_data=own_data)
560559

561560

562561
class CadetDLL:
563562

564563
def __init__(self, dll_path):
565564
self.cadet_path = dll_path
566-
self.__lib = ctypes.cdll.LoadLibrary(dll_path)
565+
self._lib = ctypes.cdll.LoadLibrary(dll_path)
567566

568567
# Query meta information
569-
cdtGetLibraryVersion = self.__lib.cdtGetLibraryVersion
568+
cdtGetLibraryVersion = self._lib.cdtGetLibraryVersion
570569
cdtGetLibraryVersion.restype = ctypes.c_char_p
571570
self.cadet_version = cdtGetLibraryVersion().decode('utf-8')
572571

573-
cdtGetLibraryCommitHash = self.__lib.cdtGetLibraryCommitHash
572+
cdtGetLibraryCommitHash = self._lib.cdtGetLibraryCommitHash
574573
cdtGetLibraryCommitHash.restype = ctypes.c_char_p
575574
self.cadet_commit_hash = cdtGetLibraryCommitHash().decode('utf-8')
576575

577-
cdtGetLibraryBranchRefspec = self.__lib.cdtGetLibraryBranchRefspec
576+
cdtGetLibraryBranchRefspec = self._lib.cdtGetLibraryBranchRefspec
578577
cdtGetLibraryBranchRefspec.restype = ctypes.c_char_p
579578
self.cadet_branch = cdtGetLibraryBranchRefspec().decode('utf-8')
580579

581-
cdtGetLibraryBuildType = self.__lib.cdtGetLibraryBuildType
580+
cdtGetLibraryBuildType = self._lib.cdtGetLibraryBuildType
582581
cdtGetLibraryBuildType.restype = ctypes.c_char_p
583582
self.cadet_build_type = cdtGetLibraryBuildType().decode('utf-8')
584583

@@ -592,37 +591,37 @@ def __init__(self, dll_path):
592591
ctypes.c_char_p,
593592
ctypes.c_char_p
594593
)
595-
set_log_handler = self.__lib.cdtSetLogReceiver
594+
set_log_handler = self._lib.cdtSetLogReceiver
596595
set_log_handler.argtypes = [LOG_HANDLER_CLBK]
597596
set_log_handler.restype = None
598597
# Keep reference alive by assigning it to this Python object
599-
self.__log_handler = LOG_HANDLER_CLBK(log_handler)
600-
set_log_handler(self.__log_handler)
598+
self._log_handler = LOG_HANDLER_CLBK(log_handler)
599+
set_log_handler(self._log_handler)
601600

602-
self.__set_log_level = self.__lib.cdtSetLogLevel
603-
self.__set_log_level.argtypes = [ctypes.c_int]
604-
self.__set_log_level.restype = None
605-
self.__set_log_level(2)
601+
self._set_log_level = self._lib.cdtSetLogLevel
602+
self._set_log_level.argtypes = [ctypes.c_int]
603+
self._set_log_level.restype = None
604+
self._set_log_level(2)
606605

607606
# Query API
608-
cdtGetAPIv010000 = self.__lib.cdtGetAPIv010000
607+
cdtGetAPIv010000 = self._lib.cdtGetAPIv010000
609608
cdtGetAPIv010000.argtypes = [ctypes.POINTER(CADETAPIV010000)]
610609
cdtGetAPIv010000.restype = c_cadet_result
611610

612-
self.__api = CADETAPIV010000()
613-
cdtGetAPIv010000(ctypes.byref(self.__api))
611+
self._api = CADETAPIV010000()
612+
cdtGetAPIv010000(ctypes.byref(self._api))
614613

615-
self.__driver = self.__api.createDriver()
614+
self._driver = self._api.createDriver()
616615

617616
def clear(self):
618617
if hasattr(self, "res"):
619618
del self.res
620-
self.__api.deleteDriver(self.__driver)
621-
self.__driver = self.__api.createDriver()
619+
self._api.deleteDriver(self._driver)
620+
self._driver = self._api.createDriver()
622621

623622
def __del__(self):
624623
log_print('deleteDriver()')
625-
self.__api.deleteDriver(self.__driver)
624+
self._api.deleteDriver(self._driver)
626625

627626

628627
def run(self, filename = None, simulation=None, timeout = None, check=None):
@@ -653,11 +652,11 @@ def run(self, filename = None, simulation=None, timeout = None, check=None):
653652
pp.pushScope = PARAMETERPROVIDER._fields_[16][1](param_provider_push_scope)
654653
pp.popScope = PARAMETERPROVIDER._fields_[17][1](param_provider_pop_scope)
655654

656-
self.__api.runSimulation(self.__driver, ctypes.byref(pp))
657-
self.res = SimulationResult(self.__api, self.__driver)
655+
self._api.runSimulation(self._driver, ctypes.byref(pp))
656+
self.res = SimulationResult(self._api, self._driver)
658657
return self.res
659658

660-
def load_solution(self, sim, solution, solution_str):
659+
def load_solution(self, sim, solution_fun, solution_str):
661660
# - [ ] Split Components (Should be unified)
662661
# - [ ] Split Ports (incl `SINGLE_AS_MULTI_PORT`)
663662
# - [ ] Split Partype (Particle + Solid)
@@ -671,7 +670,7 @@ def load_solution(self, sim, solution, solution_str):
671670
if key.startswith('unit'):
672671
if value[f'write_{solution_str}']:
673672
unit = int(key[-3:])
674-
t, out = solution(unit)
673+
t, out = solution_fun(unit)
675674

676675
if not len(solution.solution_times):
677676
solution.solution_times = t

0 commit comments

Comments
 (0)