Skip to content

Commit 08e95f5

Browse files
committed
Merge branch 'ModelicaSystem_xml' into OMCPath_merge
2 parents c45b4f7 + c80d31d commit 08e95f5

1 file changed

Lines changed: 45 additions & 21 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@ def __init__(
383383
if not isinstance(lmodel, list):
384384
raise ModelicaSystemError(f"Invalid input type for lmodel: {type(lmodel)} - list expected!")
385385

386-
self._xml_file = None
387386
self._lmodel = lmodel # may be needed if model is derived from other model
388387
self._model_name = modelName # Model class name
389388
self._file_name = pathlib.Path(fileName).resolve() if fileName is not None else None # Model file/package name
@@ -481,8 +480,8 @@ def buildModel(self, variableFilter: Optional[str] = None):
481480
buildModelResult = self._requestApi("buildModel", self._model_name, properties=varFilter)
482481
logger.debug("OM model build result: %s", buildModelResult)
483482

484-
self._xml_file = pathlib.Path(buildModelResult[0]).parent / buildModelResult[1]
485-
self._xmlparse()
483+
xml_file = pathlib.Path(buildModelResult[0]).parent / buildModelResult[1]
484+
self._xmlparse(xml_file=xml_file)
486485

487486
def sendExpression(self, expr: str, parsed: bool = True):
488487
try:
@@ -508,30 +507,42 @@ def _requestApi(self, apiName, entity=None, properties=None): # 2
508507

509508
return self.sendExpression(exp)
510509

511-
def _xmlparse(self):
512-
if not self._xml_file.is_file():
513-
raise ModelicaSystemError(f"XML file not generated: {self._xml_file}")
510+
def _xmlparse(self, xml_file: pathlib.Path):
511+
if not xml_file.is_file():
512+
raise ModelicaSystemError(f"XML file not generated: {xml_file}")
514513

515-
tree = ET.parse(self._xml_file)
514+
xml_content = xml_file.read_text()
515+
tree = ET.ElementTree(ET.fromstring(xml_content))
516516
rootCQ = tree.getroot()
517517
for attr in rootCQ.iter('DefaultExperiment'):
518518
for key in ("startTime", "stopTime", "stepSize", "tolerance",
519519
"solver", "outputFormat"):
520-
self._simulate_options[key] = attr.get(key)
520+
self._simulate_options[key] = str(attr.get(key))
521521

522522
for sv in rootCQ.iter('ScalarVariable'):
523-
scalar = {}
524-
for key in ("name", "description", "variability", "causality", "alias"):
525-
scalar[key] = sv.get(key)
526-
scalar["changeable"] = sv.get('isValueChangeable')
527-
scalar["aliasvariable"] = sv.get('aliasVariable')
523+
translations = {
524+
"alias": "alias",
525+
"aliasvariable": "aliasVariable",
526+
"causality": "causality",
527+
"changeable": "isValueChangeable",
528+
"description": "description",
529+
"name": "name",
530+
"variability": "variability",
531+
}
532+
533+
scalar: dict[str, Any] = {}
534+
for key_dst, key_src in translations.items():
535+
val = sv.get(key_src)
536+
scalar[key_dst] = None if val is None else str(val)
537+
528538
ch = list(sv)
529539
for att in ch:
530540
scalar["start"] = att.get('start')
531541
scalar["min"] = att.get('min')
532542
scalar["max"] = att.get('max')
533543
scalar["unit"] = att.get('unit')
534544

545+
# save parameters in the corresponding class variables
535546
if scalar["variability"] == "parameter":
536547
if scalar["name"] in self._override_variables:
537548
self._params[scalar["name"]] = self._override_variables[scalar["name"]]
@@ -1446,7 +1457,8 @@ def load_module_from_path(module_name, file_path):
14461457

14471458
return module_def
14481459

1449-
if self._xml_file is None:
1460+
if len(self._quantities) == 0:
1461+
# if self._quantities has no content, the xml file was not parsed; see self._xmlparse()
14501462
raise ModelicaSystemError(
14511463
"Linearization cannot be performed as the model is not build, "
14521464
"use ModelicaSystem() to build the model first"
@@ -1457,15 +1469,15 @@ def load_module_from_path(module_name, file_path):
14571469
overrideLinearFile = self._tempdir / f'{self._model_name}_override_linear.txt'
14581470

14591471
with open(file=overrideLinearFile, mode="w", encoding="utf-8") as fh:
1460-
for key, value in self._override_variables.items():
1461-
fh.write(f"{key}={value}\n")
1462-
for key, value in self._linearization_options.items():
1463-
fh.write(f"{key}={value}\n")
1472+
for key1, value1 in self._override_variables.items():
1473+
fh.write(f"{key1}={value1}\n")
1474+
for key2, value2 in self._linearization_options.items():
1475+
fh.write(f"{key2}={value2}\n")
14641476

14651477
om_cmd.arg_set(key="overrideFile", val=overrideLinearFile.as_posix())
14661478

14671479
if self._has_inputs:
1468-
nameVal = self.getInputs()
1480+
nameVal = self._inputs
14691481
for n in nameVal:
14701482
tupleList = nameVal.get(n)
14711483
if tupleList is not None:
@@ -1512,8 +1524,20 @@ def load_module_from_path(module_name, file_path):
15121524
self._linearized_inputs = inputVars
15131525
self._linearized_outputs = outputVars
15141526
self._linearized_states = stateVars
1515-
return LinearizationResult(n, m, p, A, B, C, D, x0, u0, stateVars,
1516-
inputVars, outputVars)
1527+
# TODO: why is here a mypy warning?
1528+
return LinearizationResult(
1529+
int(n),
1530+
int(m),
1531+
int(p),
1532+
A,
1533+
B,
1534+
C,
1535+
D,
1536+
x0,
1537+
u0,
1538+
stateVars,
1539+
inputVars,
1540+
outputVars)
15171541
except ModuleNotFoundError as ex:
15181542
raise ModelicaSystemError("No module named 'linearized_model'") from ex
15191543

0 commit comments

Comments
 (0)