@@ -472,12 +472,12 @@ def setTempDirectory(self, customBuildDirectory: Optional[str | os.PathLike | pa
472
472
# create a unique temp directory for each session and build the model in that directory
473
473
if customBuildDirectory is not None :
474
474
if not os .path .exists (customBuildDirectory ):
475
- raise IOError (customBuildDirectory , " does not exist" )
475
+ raise IOError (f" { customBuildDirectory } does not exist" )
476
476
tempdir = pathlib .Path (customBuildDirectory )
477
477
else :
478
478
tempdir = pathlib .Path (tempfile .mkdtemp ())
479
479
if not tempdir .is_dir ():
480
- raise IOError (tempdir , " cannot be created" )
480
+ raise IOError (f" { tempdir } could not be created" )
481
481
482
482
logger .info ("Define tempdir as %s" , tempdir )
483
483
exp = f'cd("{ tempdir .absolute ().as_posix ()} ")'
@@ -565,19 +565,57 @@ def xmlparse(self):
565
565
566
566
self .quantitiesList .append (scalar )
567
567
568
- def getQuantities (self , names = None ): # 3
568
+ def getQuantities (self , names : Optional [ str | list [ str ]] = None ) -> list [ dict ]:
569
569
"""
570
- This method returns list of dictionaries. It displays details of quantities such as name, value, changeable, and description, where changeable means if value for corresponding quantity name is changeable or not. It can be called :
571
- usage:
572
- >>> getQuantities()
573
- >>> getQuantities("Name1")
574
- >>> getQuantities(["Name1","Name2"])
570
+ This method returns list of dictionaries. It displays details of
571
+ quantities such as name, value, changeable, and description.
572
+
573
+ Examples:
574
+ >>> mod.getQuantities()
575
+ [
576
+ {
577
+ 'alias': 'noAlias',
578
+ 'aliasvariable': None,
579
+ 'causality': 'local',
580
+ 'changeable': 'true',
581
+ 'description': None,
582
+ 'max': None,
583
+ 'min': None,
584
+ 'name': 'x',
585
+ 'start': '1.0',
586
+ 'unit': None,
587
+ 'variability': 'continuous',
588
+ },
589
+ {
590
+ 'name': 'der(x)',
591
+ # ...
592
+ },
593
+ # ...
594
+ ]
595
+
596
+ >>> getQuantities("y")
597
+ [{
598
+ 'name': 'y', # ...
599
+ }]
600
+
601
+ >>> getQuantities(["y","x"])
602
+ [
603
+ {
604
+ 'name': 'y', # ...
605
+ },
606
+ {
607
+ 'name': 'x', # ...
608
+ }
609
+ ]
575
610
"""
576
611
if names is None :
577
612
return self .quantitiesList
578
613
579
614
if isinstance (names , str ):
580
- return [x for x in self .quantitiesList if x ["name" ] == names ]
615
+ r = [x for x in self .quantitiesList if x ["name" ] == names ]
616
+ if r == []:
617
+ raise KeyError (names )
618
+ return r
581
619
582
620
if isinstance (names , list ):
583
621
return [x for y in names for x in self .quantitiesList if x ["name" ] == y ]
@@ -597,10 +635,10 @@ def getContinuous(self, names=None): # 4
597
635
return self .continuouslist
598
636
599
637
if isinstance (names , str ):
600
- return [self .continuouslist . get ( names , "NotExist" ) ]
638
+ return [self .continuouslist [ names ] ]
601
639
602
640
if isinstance (names , list ):
603
- return [self .continuouslist . get ( x , "NotExist" ) for x in names ]
641
+ return [self .continuouslist [ x ] for x in names ]
604
642
else :
605
643
if names is None :
606
644
for i in self .continuouslist :
@@ -615,7 +653,7 @@ def getContinuous(self, names=None): # 4
615
653
if names in self .continuouslist :
616
654
value = self .getSolutions (names )
617
655
self .continuouslist [names ] = value [0 ][- 1 ]
618
- return [self .continuouslist . get ( names ) ]
656
+ return [self .continuouslist [ names ] ]
619
657
else :
620
658
raise ModelicaSystemError (f"{ names } is not continuous" )
621
659
@@ -657,9 +695,9 @@ def getParameters(self, names: Optional[str | list[str]] = None) -> dict[str, st
657
695
if names is None :
658
696
return self .paramlist
659
697
elif isinstance (names , str ):
660
- return [self .paramlist . get ( names , "NotExist" ) ]
698
+ return [self .paramlist [ names ] ]
661
699
elif isinstance (names , list ):
662
- return [self .paramlist . get ( x , "NotExist" ) for x in names ]
700
+ return [self .paramlist [ x ] for x in names ]
663
701
664
702
raise ModelicaSystemError ("Unhandled input for getParameters()" )
665
703
@@ -687,15 +725,13 @@ def getInputs(self, names: Optional[str | list[str]] = None) -> dict | list: #
687
725
[[(0.0, 0.0), (1.0, 1.0)]]
688
726
>>> mod.getInputs(["Name1","Name2"])
689
727
[[(0.0, 0.0), (1.0, 1.0)], None]
690
- >>> mod.getInputs("ThisInputDoesNotExist")
691
- ['NotExist']
692
728
"""
693
729
if names is None :
694
730
return self .inputlist
695
731
elif isinstance (names , str ):
696
- return [self .inputlist . get ( names , "NotExist" ) ]
732
+ return [self .inputlist [ names ] ]
697
733
elif isinstance (names , list ):
698
- return [self .inputlist . get ( x , "NotExist" ) for x in names ]
734
+ return [self .inputlist [ x ] for x in names ]
699
735
700
736
raise ModelicaSystemError ("Unhandled input for getInputs()" )
701
737
@@ -725,8 +761,6 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
725
761
['-0.4']
726
762
>>> mod.getOutputs(["out1","out2"])
727
763
['-0.4', '1.2']
728
- >>> mod.getOutputs("ThisOutputDoesNotExist")
729
- ['NotExist']
730
764
731
765
After simulate():
732
766
>>> mod.getOutputs()
@@ -740,9 +774,9 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
740
774
if names is None :
741
775
return self .outputlist
742
776
elif isinstance (names , str ):
743
- return [self .outputlist . get ( names , "NotExist" ) ]
777
+ return [self .outputlist [ names ] ]
744
778
else :
745
- return [self .outputlist . get ( x , "NotExist" ) for x in names ]
779
+ return [self .outputlist [ x ] for x in names ]
746
780
else :
747
781
if names is None :
748
782
for i in self .outputlist :
@@ -753,9 +787,9 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
753
787
if names in self .outputlist :
754
788
value = self .getSolutions (names )
755
789
self .outputlist [names ] = value [0 ][- 1 ]
756
- return [self .outputlist . get ( names ) ]
790
+ return [self .outputlist [ names ] ]
757
791
else :
758
- return names , " is not Output"
792
+ raise KeyError ( names )
759
793
elif isinstance (names , list ):
760
794
valuelist = []
761
795
for i in names :
@@ -764,7 +798,7 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
764
798
self .outputlist [i ] = value [0 ][- 1 ]
765
799
valuelist .append (value [0 ][- 1 ])
766
800
else :
767
- return i , "is not Output"
801
+ raise KeyError ( i )
768
802
return valuelist
769
803
770
804
raise ModelicaSystemError ("Unhandled input for getOutputs()" )
@@ -781,9 +815,9 @@ def getSimulationOptions(self, names=None): # 8
781
815
if names is None :
782
816
return self .simulateOptions
783
817
elif isinstance (names , str ):
784
- return [self .simulateOptions . get ( names , "NotExist" ) ]
818
+ return [self .simulateOptions [ names ] ]
785
819
elif isinstance (names , list ):
786
- return [self .simulateOptions . get ( x , "NotExist" ) for x in names ]
820
+ return [self .simulateOptions [ x ] for x in names ]
787
821
788
822
raise ModelicaSystemError ("Unhandled input for getSimulationOptions()" )
789
823
@@ -799,9 +833,9 @@ def getLinearizationOptions(self, names=None): # 9
799
833
if names is None :
800
834
return self .linearOptions
801
835
elif isinstance (names , str ):
802
- return [self .linearOptions . get ( names , "NotExist" ) ]
836
+ return [self .linearOptions [ names ] ]
803
837
elif isinstance (names , list ):
804
- return [self .linearOptions . get ( x , "NotExist" ) for x in names ]
838
+ return [self .linearOptions [ x ] for x in names ]
805
839
806
840
raise ModelicaSystemError ("Unhandled input for getLinearizationOptions()" )
807
841
@@ -815,9 +849,9 @@ def getOptimizationOptions(self, names=None): # 10
815
849
if names is None :
816
850
return self .optimizeOptions
817
851
elif isinstance (names , str ):
818
- return [self .optimizeOptions . get ( names , "NotExist" ) ]
852
+ return [self .optimizeOptions [ names ] ]
819
853
elif isinstance (names , list ):
820
- return [self .optimizeOptions . get ( x , "NotExist" ) for x in names ]
854
+ return [self .optimizeOptions [ x ] for x in names ]
821
855
822
856
raise ModelicaSystemError ("Unhandled input for getOptimizationOptions()" )
823
857
@@ -1236,8 +1270,10 @@ def load_module_from_path(module_name, file_path):
1236
1270
return module_def
1237
1271
1238
1272
if self .xmlFile is None :
1239
- raise IOError ("Linearization cannot be performed as the model is not build, "
1240
- "use ModelicaSystem() to build the model first" )
1273
+ raise ModelicaSystemError (
1274
+ "Linearization cannot be performed as the model is not build, "
1275
+ "use ModelicaSystem() to build the model first"
1276
+ )
1241
1277
1242
1278
om_cmd = ModelicaSystemCmd (runpath = self .tempdir , modelname = self .modelName , timeout = timeout )
1243
1279
0 commit comments