|
32 | 32 | CONDITIONS OF OSMC-PL. |
33 | 33 | """ |
34 | 34 |
|
| 35 | +import ast |
35 | 36 | import csv |
36 | 37 | from dataclasses import dataclass |
37 | 38 | import importlib |
@@ -1337,62 +1338,38 @@ def setInputs( |
1337 | 1338 | >>> setInputs(["Name1=value1","Name2=value2"]) # depreciated |
1338 | 1339 | >>> setInputs(name={"Name1": "value1", "Name2": "value2"}) |
1339 | 1340 | """ |
1340 | | - # inputdata = self._prepare_input_data(raw_input=name) |
1341 | | - |
1342 | | - if isinstance(name, str): |
1343 | | - name1: str = name |
1344 | | - name1 = name1.replace(" ", "") |
1345 | | - value1 = name1.split("=") |
1346 | | - if value1[0] in self.inputlist: |
1347 | | - tmpvalue = eval(value1[1]) |
1348 | | - if isinstance(tmpvalue, (int, float)): |
1349 | | - self.inputlist[value1[0]] = [(float(self._simulateOptions["startTime"]), float(value1[1])), |
1350 | | - (float(self._simulateOptions["stopTime"]), float(value1[1]))] |
1351 | | - elif isinstance(tmpvalue, list): |
1352 | | - self._checkValidInputs(tmpvalue) |
1353 | | - self._inputs[value1[0]] = tmpvalue |
| 1341 | + inputdata = self._prepare_input_data(raw_input=name) |
| 1342 | + |
| 1343 | + for key, val in inputdata.items(): |
| 1344 | + if key in self._inputs: |
| 1345 | + val_evaluated = ast.literal_eval(val) |
| 1346 | + if isinstance(val_evaluated, (int, float)): |
| 1347 | + self._inputs[key] = [(float(self._simulate_options["startTime"]), float(val)), |
| 1348 | + (float(self._simulate_options["stopTime"]), float(val))] |
| 1349 | + elif isinstance(val_evaluated, list): |
| 1350 | + if not all([isinstance(item, tuple) for item in val_evaluated]): |
| 1351 | + raise ModelicaSystemError("Value for setInput() must be in tuple format; " |
| 1352 | + f"got {repr(val_evaluated)}") |
| 1353 | + if val_evaluated != sorted(val_evaluated, key=lambda x: x[0]): |
| 1354 | + raise ModelicaSystemError("Time value should be in increasing order; " |
| 1355 | + f"got {repr(val_evaluated)}") |
| 1356 | + |
| 1357 | + for item in val_evaluated: |
| 1358 | + if item[0] < float(self._simulate_options["startTime"]): |
| 1359 | + raise ModelicaSystemError(f"Time value in {repr(item)} of {repr(val_evaluated)} is less " |
| 1360 | + "than the simulation start time") |
| 1361 | + if len(item) != 2: |
| 1362 | + raise ModelicaSystemError(f"Value {repr(item)} of {repr(val_evaluated)} " |
| 1363 | + "is in incorrect format!") |
| 1364 | + |
| 1365 | + self._inputs[key] = val_evaluated |
1354 | 1366 | self._inputFlag = True |
1355 | 1367 | else: |
1356 | | - raise ModelicaSystemError(f"{value1[0]} is not an input") |
1357 | | - elif isinstance(name, list): |
1358 | | - name_list: list[str] = name |
1359 | | - for name2 in name_list: |
1360 | | - name2 = name2.replace(" ", "") |
1361 | | - value2 = name2.split("=") |
1362 | | - if value2[0] in self.inputlist: |
1363 | | - tmpvalue = eval(value2[1]) |
1364 | | - if isinstance(tmpvalue, (int, float)): |
1365 | | - self.inputlist[value2[0]] = [(float(self._simulateOptions["startTime"]), float(value2[1])), |
1366 | | - (float(self.:simulateOptions["stopTime"]), float(value2[1]))] |
1367 | | - elif isinstance(tmpvalue, list): |
1368 | | - self._checkValidInputs(tmpvalue) |
1369 | | - self._inputs[value2[0]] = tmpvalue |
1370 | | - self._inputFlag = True |
1371 | | - else: |
1372 | | - raise ModelicaSystemError(f"{value2[0]} is not an input!") |
1373 | | - elif isinstance(name, dict): |
1374 | | - raise NotImplementedError("Must be defined!") |
| 1368 | + raise ModelicaSystemError(f"{key} is not an input") |
1375 | 1369 |
|
1376 | 1370 | return True |
1377 | 1371 |
|
1378 | | - def _checkValidInputs(self, name): |
1379 | | - if name != sorted(name, key=lambda x: x[0]): |
1380 | | - raise ModelicaSystemError('Time value should be in increasing order') |
1381 | | - for l in name: |
1382 | | - if isinstance(l, tuple): |
1383 | | - # if l[0] < float(self.simValuesList[0]): |
1384 | | - if l[0] < float(self._simulate_options["startTime"]): |
1385 | | - raise ModelicaSystemError('Input time value is less than simulation startTime') |
1386 | | - if len(l) != 2: |
1387 | | - raise ModelicaSystemError(f'Value for {l} is in incorrect format!') |
1388 | | - else: |
1389 | | - raise ModelicaSystemError('Error!!! Value must be in tuple format') |
1390 | | - |
1391 | | - def _createCSVData(self, csvfile: Optional[pathlib.Path] = None) -> pathlib.Path: |
1392 | | - """ |
1393 | | - Create a csv file with inputs for the simulation/optimization of the model. If csvfile is provided as argument, |
1394 | | - this file is used; else a generic file name is created. |
1395 | | - """ |
| 1372 | + def _createCSVData(self) -> pathlib.Path: |
1396 | 1373 | start_time: float = float(self._simulate_options["startTime"]) |
1397 | 1374 | stop_time: float = float(self._simulate_options["stopTime"]) |
1398 | 1375 |
|
|
0 commit comments