Skip to content

Commit e858549

Browse files
ondras12345adeas31
andauthored
Improve docstrings (#267)
* Improve ModelicaSystem.__init__ docstring * Remove ModelicaSystem.getlinearParameters It does not work, and its name is inconsistent with the rest of the API (note the lowercase l). I can't even tell what it was supposed to do. This closes #248 * Improve ModelicaSystem.getParameters docstring * Improve getInputs docstring * Improve getOutputs docstring --------- Co-authored-by: Adeel Asghar <[email protected]>
1 parent 98174c3 commit e858549

File tree

1 file changed

+130
-44
lines changed

1 file changed

+130
-44
lines changed

OMPython/ModelicaSystem.py

Lines changed: 130 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,54 @@ def __getitem__(self, index: int):
108108

109109

110110
class ModelicaSystem:
111-
def __init__(self, fileName=None, modelName=None, lmodel=None, commandLineOptions=None,
112-
variableFilter=None, customBuildDirectory=None, verbose=True, raiseerrors=False,
113-
omhome: str = None, session: OMCSessionBase = None): # 1
114-
"""
115-
"constructor"
116-
It initializes to load file and build a model, generating object, exe, xml, mat, and json files. etc. It can be called :
117-
•without any arguments: In this case it neither loads a file nor build a model. This is useful when a FMU needed to convert to Modelica model
118-
•with two arguments as file name with ".mo" extension and the model name respectively
119-
•with three arguments, the first and second are file name and model name respectively and the third arguments is Modelica standard library to load a model, which is common in such models where the model is based on the standard library. For example, here is a model named "dcmotor.mo" below table 4-2, which is located in the directory of OpenModelica at "C:\\OpenModelica1.9.4-dev.beta2\\share\\doc\\omc\\testmodels".
120-
Note: If the model file is not in the current working directory, then the path where file is located must be included together with file name. Besides, if the Modelica model contains several different models within the same package, then in order to build the specific model, in second argument, user must put the package name with dot(.) followed by specific model name.
121-
ex: myModel = ModelicaSystem("ModelicaModel.mo", "modelName")
111+
def __init__(
112+
self,
113+
fileName: Optional[str | os.PathLike] = None,
114+
modelName: Optional[str] = None,
115+
lmodel: Optional[list[str | tuple[str, str]]] = None,
116+
commandLineOptions: Optional[str] = None,
117+
variableFilter: Optional[str] = None,
118+
customBuildDirectory: Optional[str | os.PathLike] = None,
119+
verbose: bool = True,
120+
raiseerrors: bool = False,
121+
omhome: Optional[str] = None,
122+
session: Optional[OMCSessionBase] = None
123+
):
124+
"""Initialize, load and build a model.
125+
126+
The constructor loads the model file and builds it, generating exe and
127+
xml files, etc.
128+
129+
Args:
130+
fileName: Path to the model file. Either absolute or relative to
131+
the current working directory.
132+
modelName: The name of the model class. If it is contained within
133+
a package, "PackageName.ModelName" should be used.
134+
lmodel: List of libraries to be loaded before the model itself is
135+
loaded. Two formats are supported for the list elements:
136+
lmodel=["Modelica"] for just the library name
137+
and lmodel=[("Modelica","3.2.3")] for specifying both the name
138+
and the version.
139+
commandLineOptions: String with extra command line options to be
140+
provided to omc via setCommandLineOptions().
141+
variableFilter: A regular expression. Only variables fully
142+
matching the regexp will be stored in the result file.
143+
Leaving it unspecified is equivalent to ".*".
144+
customBuildDirectory: Path to a directory to be used for temporary
145+
files like the model executable. If left unspecified, a tmp
146+
directory will be created.
147+
verbose: If True, enable verbose logging.
148+
raiseerrors: If True, raise exceptions instead of just logging
149+
OpenModelica errors.
150+
omhome: OPENMODELICAHOME value to be used when creating the OMC
151+
session.
152+
session: OMC session to be used. If unspecified, a new session
153+
will be created.
154+
155+
Examples:
156+
mod = ModelicaSystem("ModelicaModel.mo", "modelName")
157+
mod = ModelicaSystem("ModelicaModel.mo", "modelName", ["Modelica"])
158+
mod = ModelicaSystem("ModelicaModel.mo", "modelName", [("Modelica","3.2.3"), "PowerSystems"])
122159
"""
123160
if fileName is None and modelName is None and not lmodel: # all None
124161
raise Exception("Cannot create ModelicaSystem object without any arguments")
@@ -445,14 +482,27 @@ def getContinuous(self, names=None): # 4
445482
raise ModelicaSystemError(f"OM error: {i} is not continuous")
446483
return valuelist
447484

448-
def getParameters(self, names=None): # 5
449-
"""
450-
This method returns dict. The key is parameter names and value is corresponding parameter value.
451-
If name is None then the function will return dict which contain all parameter names as key and value as corresponding values.
452-
usage:
453-
>>> getParameters()
454-
>>> getParameters("Name1")
455-
>>> getParameters(["Name1","Name2"])
485+
def getParameters(self, names: Optional[str | list[str]] = None) -> dict[str, str] | list[str]: # 5
486+
"""Get parameter values.
487+
488+
Args:
489+
names: Either None (default), a string with the parameter name,
490+
or a list of parameter name strings.
491+
Returns:
492+
If `names` is None, a dict in the format
493+
{parameter_name: parameter_value} is returned.
494+
If `names` is a string, a single element list is returned.
495+
If `names` is a list, a list with one value for each parameter name
496+
in names is returned.
497+
In all cases, parameter values are returned as strings.
498+
499+
Examples:
500+
>>> mod.getParameters()
501+
{'Name1': '1.23', 'Name2': '4.56'}
502+
>>> mod.getParameters("Name1")
503+
['1.23']
504+
>>> mod.getParameters(["Name1","Name2"])
505+
['1.23', '4.56']
456506
"""
457507
if names is None:
458508
return self.paramlist
@@ -461,24 +511,32 @@ def getParameters(self, names=None): # 5
461511
elif isinstance(names, list):
462512
return ([self.paramlist.get(x, "NotExist") for x in names])
463513

464-
def getlinearParameters(self, names=None): # 5
465-
"""
466-
This method returns dict. The key is parameter names and value is corresponding parameter value.
467-
If *name is None then the function will return dict which contain all parameter names as key and value as corresponding values. eg., getParameters()
468-
Otherwise variable number of arguments can be passed as parameter name in string format separated by commas. eg., getParameters('paraName1', 'paraName2')
469-
"""
470-
if names is None:
471-
return self.linearparameters
472-
elif isinstance(names, str):
473-
return [self.linearparameters.get(names, "NotExist")]
474-
else:
475-
return [self.linearparameters.get(x, "NotExist") for x in names]
514+
def getInputs(self, names: Optional[str | list[str]] = None) -> dict | list: # 6
515+
"""Get input values.
476516
477-
def getInputs(self, names=None): # 6
478-
"""
479-
This method returns dict. The key is input names and value is corresponding input value.
480-
If *name is None then the function will return dict which contain all input names as key and value as corresponding values. eg., getInputs()
481-
Otherwise variable number of arguments can be passed as input name in string format separated by commas. eg., getInputs('iName1', 'iName2')
517+
Args:
518+
names: Either None (default), a string with the input name,
519+
or a list of input name strings.
520+
Returns:
521+
If `names` is None, a dict in the format
522+
{input_name: input_value} is returned.
523+
If `names` is a string, a single element list [input_value] is
524+
returned.
525+
If `names` is a list, a list with one value for each input name
526+
in names is returned: [input1_values, input2_values, ...].
527+
In all cases, input values are returned as a list of tuples,
528+
where the first element in the tuple is the time and the second
529+
element is the input value.
530+
531+
Examples:
532+
>>> mod.getInputs()
533+
{'Name1': [(0.0, 0.0), (1.0, 1.0)], 'Name2': None}
534+
>>> mod.getInputs("Name1")
535+
[[(0.0, 0.0), (1.0, 1.0)]]
536+
>>> mod.getInputs(["Name1","Name2"])
537+
[[(0.0, 0.0), (1.0, 1.0)], None]
538+
>>> mod.getInputs("ThisInputDoesNotExist")
539+
['NotExist']
482540
"""
483541
if names is None:
484542
return self.inputlist
@@ -487,14 +545,42 @@ def getInputs(self, names=None): # 6
487545
elif isinstance(names, list):
488546
return ([self.inputlist.get(x, "NotExist") for x in names])
489547

490-
def getOutputs(self, names=None): # 7
491-
"""
492-
This method returns dict. The key is output names and value is corresponding output value.
493-
If name is None then the function will return dict which contain all output names as key and value as corresponding values. eg., getOutputs()
494-
usage:
495-
>>> getOutputs()
496-
>>> getOutputs("Name1")
497-
>>> getOutputs(["Name1","Name2"])
548+
def getOutputs(self, names: Optional[str | list[str]] = None): # 7
549+
"""Get output values.
550+
551+
If called before simulate(), the initial values are returned as
552+
strings. If called after simulate(), the final values (at stopTime)
553+
are returned as numpy.float64.
554+
555+
Args:
556+
names: Either None (default), a string with the output name,
557+
or a list of output name strings.
558+
Returns:
559+
If `names` is None, a dict in the format
560+
{output_name: output_value} is returned.
561+
If `names` is a string, a single element list [output_value] is
562+
returned.
563+
If `names` is a list, a list with one value for each output name
564+
in names is returned: [output1_value, output2_value, ...].
565+
566+
Examples:
567+
Before simulate():
568+
>>> mod.getOutputs()
569+
{'out1': '-0.4', 'out2': '1.2'}
570+
>>> mod.getOutputs("out1")
571+
['-0.4']
572+
>>> mod.getOutputs(["out1","out2"])
573+
['-0.4', '1.2']
574+
>>> mod.getOutputs("ThisOutputDoesNotExist")
575+
['NotExist']
576+
577+
After simulate():
578+
>>> mod.getOutputs()
579+
{'out1': np.float64(-0.1234), 'out2': np.float64(2.1)}
580+
>>> mod.getOutputs("out1")
581+
[np.float64(-0.1234)]
582+
>>> mod.getOutputs(["out1","out2"])
583+
[np.float64(-0.1234), np.float64(2.1)]
498584
"""
499585
if not self.simulationFlag:
500586
if names is None:

0 commit comments

Comments
 (0)