Skip to content

Commit 645ccce

Browse files
Immudzenschmoelder
authored andcommitted
handle more cases for split components and ports
1 parent 6c5106a commit 645ccce

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

cadet/cadet_dll.py

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
from cmath import sin
23
import ctypes
34
import numpy
45
import addict
@@ -140,18 +141,20 @@ def load_data(self, unit, get_solution, get_solution_str, idx=None, parType=None
140141
result = get_solution(*tuple(wrappers[var_key](var_value) for var_key, var_value in vars.items()))
141142

142143
shape = []
144+
dims = []
143145
dimensions = ['nTime', 'nPort', 'nParShells', 'nAxialCells', 'nRadialCells', 'nComp', 'nBound']
144146
for dim in dimensions:
145147
if dim in vars and vars[dim].value:
146148
shape.append(vars[dim].value)
149+
dims.append(dim)
147150

148151
data = numpy.ctypeslib.as_array(vars['data'], shape=shape)
149152
time = numpy.ctypeslib.as_array(vars['time'], shape=(vars['nTime'].value, ))
150153

151154
if own_data:
152-
return (time.copy(), data.copy())
155+
return time.copy(), data.copy(), dims
153156
else:
154-
return (time, data)
157+
return time, data, dims
155158

156159
def inlet(self, unit, own_data=True):
157160
return self.load_data(unit, self._api.getSolutionInlet, 'getSolutionInlet', own_data=own_data)
@@ -312,7 +315,6 @@ def run(self, filename = None, simulation=None, timeout = None, check=None):
312315
return self.res
313316

314317
def load_solution(self, sim, solution_fun, solution_str):
315-
# - [ ] Split Components (Should be unified)
316318
# - [ ] Split Ports (incl `SINGLE_AS_MULTI_PORT`)
317319
# - [ ] Split Partype (Particle + Solid)
318320
# - [ ] Coordinates?
@@ -325,7 +327,7 @@ def load_solution(self, sim, solution_fun, solution_str):
325327
if key.startswith('unit'):
326328
if value[f'write_{solution_str}']:
327329
unit = int(key[-3:])
328-
t, out = solution_fun(unit)
330+
t, out, dims = solution_fun(unit)
329331

330332
if not len(solution.solution_times):
331333
solution.solution_times = t
@@ -340,14 +342,54 @@ def load_solution_io(self, sim, solution_fun, solution_str):
340342
if key.startswith('unit'):
341343
if value[f'write_{solution_str}']:
342344
unit = int(key[-3:])
343-
t, out = solution_fun(unit)
345+
t, out, dims = solution_fun(unit)
344346

345347
if not len(solution.solution_times):
346348
solution.solution_times = t
347-
348-
for comp in range(out.shape[2]):
349-
comp_out = numpy.squeeze(out[:,:,comp])
350-
solution[key][f'{solution_str}_comp_{comp:03d}'] = comp_out
349+
350+
split_components_data = value.get('split_components_data', 1)
351+
split_ports_data = value.get('split_ports_data', 1)
352+
single_as_multi_port = value.get('single_as_multi_port', 0)
353+
354+
nComp = dims.index('nComp')
355+
try:
356+
nPorts = dims.index('nPorts')
357+
except ValueError:
358+
nPorts = None
359+
360+
if split_components_data:
361+
if split_ports_data:
362+
if nPorts is None:
363+
if single_as_multi_port:
364+
for comp in range(out.shape[nComp]):
365+
comp_out = numpy.squeeze(out[..., comp])
366+
solution[key][f'{solution_str}_port_000_comp_{comp:03d}'] = comp_out
367+
else:
368+
for comp in range(out.shape[nComp]):
369+
comp_out = numpy.squeeze(out[..., comp])
370+
solution[key][f'{solution_str}_comp_{comp:03d}'] = comp_out
371+
else:
372+
for port in range(out.shape[nPorts]):
373+
for comp in range(out.shape[nComp]):
374+
comp_out = numpy.squeeze(out[..., port, comp])
375+
solution[key][f'{solution_str}_port_{port:03d}_comp_{comp:03d}'] = comp_out
376+
else:
377+
for comp in range(out.shape[nComp]):
378+
comp_out = numpy.squeeze(out[...,comp])
379+
solution[key][f'{solution_str}_comp_{comp:03d}'] = comp_out
380+
else:
381+
if split_ports_data:
382+
if nPorts is None:
383+
if single_as_multi_port:
384+
solution[key][f'{solution_str}_port_000'] = out
385+
else:
386+
solution[key][solution_str] = out
387+
else:
388+
for port in range(out.shape[nPorts]):
389+
port_out = numpy.squeeze(out[..., port, :])
390+
solution[key][f'{solution_str}_port_{port:03d}'] = port_out
391+
else:
392+
solution[key][solution_str] = out
351393
return solution
352394

353395
def load_inlet(self, sim):

examples/dll_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
sim.root.input['return'].unit_000.write_solution_inlet = 1
1515
sim.root.input['return'].unit_000.write_solution_bulk = 1
16+
sim.root.input['return'].unit_000.single_as_multi_port = 0
1617
sim.root.input['return'].unit_001.write_solution_bulk = 0
1718

1819
print('before run')

0 commit comments

Comments
 (0)