Skip to content

Commit b06722d

Browse files
authored
Merge branch 'master' into improve-docstrings
2 parents 2cce81a + 98174c3 commit b06722d

1 file changed

Lines changed: 44 additions & 104 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 44 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -940,112 +940,52 @@ def checkValidInputs(self, name):
940940
else:
941941
ModelicaSystemError('Error!!! Value must be in tuple format')
942942

943-
# To create csv file for inputs
944-
def createCSVData(self):
945-
sl = [] # Actual timestamps
946-
skip = False
947-
948-
# check for NONE in input list and replace with proper data (e.g) [(startTime, 0.0), (stopTime, 0.0)]
949-
tmpinputlist = {}
950-
for key, value in self.inputlist.items():
951-
if value is None:
952-
tmpinputlist[key] = [(float(self.simulateOptions["startTime"]), 0.0),
953-
(float(self.simulateOptions["stopTime"]), 0.0)]
943+
def createCSVData(self) -> None:
944+
start_time: float = float(self.simulateOptions["startTime"])
945+
stop_time: float = float(self.simulateOptions["stopTime"])
946+
947+
# Replace None inputs with a default constant zero signal
948+
inputs: dict[str, list[tuple[float, float]]] = {}
949+
for input_name, input_signal in self.inputlist.items():
950+
if input_signal is None:
951+
inputs[input_name] = [(start_time, 0.0), (stop_time, 0.0)]
954952
else:
955-
tmpinputlist[key] = value
956-
957-
inp = list(tmpinputlist.values())
958-
959-
for i in inp:
960-
cl = list()
961-
el = list()
962-
for t, x in i:
963-
cl.append(t)
964-
for i in cl:
965-
if skip is True:
966-
skip = False
967-
continue
968-
if i not in sl:
969-
el.append(i)
970-
else:
971-
elem_no = cl.count(i)
972-
sl_no = sl.count(i)
973-
if elem_no == 2 and sl_no == 1:
974-
el.append(i)
975-
skip = True
976-
sl = sl + el
977-
978-
sl.sort()
979-
for t in sl:
980-
for i in inp:
981-
for ttt in [tt[0] for tt in i]:
982-
if t not in [tt[0] for tt in i]:
983-
i.append((t, '?'))
984-
inpSortedList = list()
985-
sortedList = list()
986-
for i in inp:
987-
sortedList = sorted(i, key=lambda x: x[0])
988-
inpSortedList.append(sortedList)
989-
for i in inpSortedList:
990-
ind = 0
991-
for t, x in i:
992-
if x == '?':
993-
t1 = i[ind - 1][0]
994-
u1 = i[ind - 1][1]
995-
t2 = i[ind + 1][0]
996-
u2 = i[ind + 1][1]
997-
nex = 2
998-
while (u2 == '?'):
999-
u2 = i[ind + nex][1]
1000-
t2 = i[ind + nex][0]
1001-
nex += 1
1002-
x = float(u1 + (u2 - u1) * (t - t1) / (t2 - t1))
1003-
i[ind] = (t, x)
1004-
ind += 1
1005-
slSet = list()
1006-
slSet = set(sl)
1007-
for i in inpSortedList:
1008-
tempTime = list()
1009-
for (t, x) in i:
1010-
tempTime.append(t)
1011-
inSl = None
1012-
inI = None
1013-
for s in slSet:
1014-
inSl = sl.count(s)
1015-
inI = tempTime.count(s)
1016-
if inSl != inI:
1017-
test = list()
1018-
test = [(x, y) for x, y in i if x == s]
1019-
i.append(test[0])
1020-
newInpList = list()
1021-
tempSorting = list()
1022-
for i in inpSortedList:
1023-
# i.sort() => just sorting might not work so need to sort according to 1st element of a tuple
1024-
tempSorting = sorted(i, key=lambda x: x[0])
1025-
newInpList.append(tempSorting)
1026-
1027-
interpolated_inputs_all = list()
1028-
for i in newInpList:
1029-
templist = list()
1030-
for (t, x) in i:
1031-
templist.append(x)
1032-
interpolated_inputs_all.append(templist)
1033-
1034-
name = ','.join(list(self.inputlist.keys()))
1035-
name = f'time,{name},end'
1036-
1037-
a = ''
1038-
l = []
1039-
l.append(name)
1040-
for i in range(0, len(sl)):
1041-
a = f'{float(sl[i])},{",".join(str(float(inppp[i])) for inppp in interpolated_inputs_all)},0'
1042-
l.append(a)
1043-
1044-
self.csvFile = (pathlib.Path(self.tempdir) / f'{self.modelName}.csv').as_posix()
953+
inputs[input_name] = input_signal
954+
955+
# Collect all unique timestamps across all input signals
956+
all_times = np.array(
957+
sorted({t for signal in inputs.values() for t, _ in signal}),
958+
dtype=float
959+
)
960+
961+
# Interpolate missing values
962+
interpolated_inputs: dict[str, np.ndarray] = {}
963+
for signal_name, signal_values in inputs.items():
964+
signal = np.array(signal_values)
965+
interpolated_inputs[signal_name] = np.interp(
966+
all_times,
967+
signal[:, 0], # times
968+
signal[:, 1] # values
969+
)
970+
971+
# Write CSV file
972+
input_names = list(interpolated_inputs.keys())
973+
header = ['time'] + input_names + ['end']
974+
975+
csv_rows = [header]
976+
for i, t in enumerate(all_times):
977+
row = [
978+
t, # time
979+
*(interpolated_inputs[name][i] for name in input_names), # input values
980+
0 # trailing 'end' column
981+
]
982+
csv_rows.append(row)
983+
984+
self.csvFile: str = (pathlib.Path(self.tempdir) / f'{self.modelName}.csv').as_posix()
985+
1045986
with open(self.csvFile, "w", newline="") as f:
1046-
writer = csv.writer(f, delimiter='\n')
1047-
writer.writerow(l)
1048-
f.close()
987+
writer = csv.writer(f)
988+
writer.writerows(csv_rows)
1049989

1050990
# to convert Modelica model to FMU
1051991
def convertMo2Fmu(self, version="2.0", fmuType="me_cs", fileNamePrefix="<default>", includeResources=True): # 19

0 commit comments

Comments
 (0)