Skip to content

Commit 2944129

Browse files
authored
Merge branch 'master' into ModelicaSystem_prepare_OMCPath
2 parents a676435 + 2e69f3c commit 2944129

1 file changed

Lines changed: 30 additions & 18 deletions

File tree

OMPython/ModelicaSystem.py

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

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

482-
self._xml_file = pathlib.Path(buildModelResult[0]).parent / buildModelResult[1]
483-
self._xmlparse()
481+
xml_file = pathlib.Path(buildModelResult[0]).parent / buildModelResult[1]
482+
self._xmlparse(xml_file=xml_file)
484483

485484
def sendExpression(self, expr: str, parsed: bool = True):
486485
try:
@@ -506,30 +505,42 @@ def _requestApi(self, apiName, entity=None, properties=None): # 2
506505

507506
return self.sendExpression(exp)
508507

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

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

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

543+
# save parameters in the corresponding class variables
533544
if scalar["variability"] == "parameter":
534545
if scalar["name"] in self._override_variables:
535546
self._params[scalar["name"]] = self._override_variables[scalar["name"]]
@@ -1536,7 +1547,8 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
15361547
compatibility, because linearize() used to return `[A, B, C, D]`.
15371548
"""
15381549

1539-
if self._xml_file is None:
1550+
if len(self._quantities) == 0:
1551+
# if self._quantities has no content, the xml file was not parsed; see self._xmlparse()
15401552
raise ModelicaSystemError(
15411553
"Linearization cannot be performed as the model is not build, "
15421554
"use ModelicaSystem() to build the model first"
@@ -1547,10 +1559,10 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
15471559
overrideLinearFile = self._tempdir / f'{self._model_name}_override_linear.txt'
15481560

15491561
with open(file=overrideLinearFile, mode="w", encoding="utf-8") as fh:
1550-
for key, value in self._override_variables.items():
1551-
fh.write(f"{key}={value}\n")
1552-
for key, value in self._linearization_options.items():
1553-
fh.write(f"{key}={value}\n")
1562+
for key1, value1 in self._override_variables.items():
1563+
fh.write(f"{key1}={value1}\n")
1564+
for key2, value2 in self._linearization_options.items():
1565+
fh.write(f"{key2}={value2}\n")
15541566

15551567
om_cmd.arg_set(key="overrideFile", val=overrideLinearFile.as_posix())
15561568

0 commit comments

Comments
 (0)