@@ -383,7 +383,6 @@ def __init__(
383
383
if not isinstance (lmodel , list ):
384
384
raise ModelicaSystemError (f"Invalid input type for lmodel: { type (lmodel )} - list expected!" )
385
385
386
- self ._xml_file = None
387
386
self ._lmodel = lmodel # may be needed if model is derived from other model
388
387
self ._model_name = modelName # Model class name
389
388
self ._file_name = pathlib .Path (fileName ).resolve () if fileName is not None else None # Model file/package name
@@ -480,8 +479,8 @@ def buildModel(self, variableFilter: Optional[str] = None):
480
479
buildModelResult = self ._requestApi ("buildModel" , self ._model_name , properties = varFilter )
481
480
logger .debug ("OM model build result: %s" , buildModelResult )
482
481
483
- self . _xml_file = pathlib .Path (buildModelResult [0 ]).parent / buildModelResult [1 ]
484
- self ._xmlparse ()
482
+ xml_file = pathlib .Path (buildModelResult [0 ]).parent / buildModelResult [1 ]
483
+ self ._xmlparse (xml_file = xml_file )
485
484
486
485
def sendExpression (self , expr : str , parsed : bool = True ):
487
486
try :
@@ -507,30 +506,42 @@ def _requestApi(self, apiName, entity=None, properties=None): # 2
507
506
508
507
return self .sendExpression (exp )
509
508
510
- def _xmlparse (self ):
511
- if not self . _xml_file .is_file ():
512
- raise ModelicaSystemError (f"XML file not generated: { self . _xml_file } " )
509
+ def _xmlparse (self , xml_file : pathlib . Path ):
510
+ if not xml_file .is_file ():
511
+ raise ModelicaSystemError (f"XML file not generated: { xml_file } " )
513
512
514
- tree = ET .parse (self ._xml_file )
513
+ xml_content = xml_file .read_text ()
514
+ tree = ET .ElementTree (ET .fromstring (xml_content ))
515
515
rootCQ = tree .getroot ()
516
516
for attr in rootCQ .iter ('DefaultExperiment' ):
517
517
for key in ("startTime" , "stopTime" , "stepSize" , "tolerance" ,
518
518
"solver" , "outputFormat" ):
519
- self ._simulate_options [key ] = attr .get (key )
519
+ self ._simulate_options [key ] = str ( attr .get (key ) )
520
520
521
521
for sv in rootCQ .iter ('ScalarVariable' ):
522
- scalar = {}
523
- for key in ("name" , "description" , "variability" , "causality" , "alias" ):
524
- scalar [key ] = sv .get (key )
525
- scalar ["changeable" ] = sv .get ('isValueChangeable' )
526
- scalar ["aliasvariable" ] = sv .get ('aliasVariable' )
522
+ translations = {
523
+ "alias" : "alias" ,
524
+ "aliasvariable" : "aliasVariable" ,
525
+ "causality" : "causality" ,
526
+ "changeable" : "isValueChangeable" ,
527
+ "description" : "description" ,
528
+ "name" : "name" ,
529
+ "variability" : "variability" ,
530
+ }
531
+
532
+ scalar : dict [str , Any ] = {}
533
+ for key_dst , key_src in translations .items ():
534
+ val = sv .get (key_src )
535
+ scalar [key_dst ] = None if val is None else str (val )
536
+
527
537
ch = list (sv )
528
538
for att in ch :
529
539
scalar ["start" ] = att .get ('start' )
530
540
scalar ["min" ] = att .get ('min' )
531
541
scalar ["max" ] = att .get ('max' )
532
542
scalar ["unit" ] = att .get ('unit' )
533
543
544
+ # save parameters in the corresponding class variables
534
545
if scalar ["variability" ] == "parameter" :
535
546
if scalar ["name" ] in self ._override_variables :
536
547
self ._params [scalar ["name" ]] = self ._override_variables [scalar ["name" ]]
@@ -1535,7 +1546,8 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
1535
1546
compatibility, because linearize() used to return `[A, B, C, D]`.
1536
1547
"""
1537
1548
1538
- if self ._xml_file is None :
1549
+ if len (self ._quantities ) == 0 :
1550
+ # if self._quantities has no content, the xml file was not parsed; see self._xmlparse()
1539
1551
raise ModelicaSystemError (
1540
1552
"Linearization cannot be performed as the model is not build, "
1541
1553
"use ModelicaSystem() to build the model first"
@@ -1546,10 +1558,10 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
1546
1558
overrideLinearFile = self ._tempdir / f'{ self ._model_name } _override_linear.txt'
1547
1559
1548
1560
with open (file = overrideLinearFile , mode = "w" , encoding = "utf-8" ) as fh :
1549
- for key , value in self ._override_variables .items ():
1550
- fh .write (f"{ key } ={ value } \n " )
1551
- for key , value in self ._linearization_options .items ():
1552
- fh .write (f"{ key } ={ value } \n " )
1561
+ for key1 , value1 in self ._override_variables .items ():
1562
+ fh .write (f"{ key1 } ={ value1 } \n " )
1563
+ for key2 , value2 in self ._linearization_options .items ():
1564
+ fh .write (f"{ key2 } ={ value2 } \n " )
1553
1565
1554
1566
om_cmd .arg_set (key = "overrideFile" , val = overrideLinearFile .as_posix ())
1555
1567
0 commit comments