New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The function linearize() produces a Python file not written in a valid Python format. #8687
Comments
@arun3688, you are familiar with Python. Could you please ask @kabdelhak where the code for this feature is, and try to fix it according to @naibafomsare's suggestions? |
The orientation of a rigid body in space is an SU(3) group. It cannot be fully represented by a fixed set of three variables (e.g. Euler's angles), because no matte how you choose them, there will always be a singular orientation. What OMC does is then to use dynamic state selection: it picks three potential states as actual states, but when they get close to singularity, it switches to some other choice. What you see are the names of those three generic dynamically selected states. |
related issue: https://trac.openmodelica.org/OpenModelica/ticket/6424 |
I remember we fixed the syntax issue for linearization but maybe we didn't or is an PR that was not merged?! Maybe Python 3 vs Python 2 issue? I will check if I can reproduce this. |
All of the syntax problems reported here or in the trac issue would also be present in Python 2. |
The Matlab output, which was mentioned as broken in #6424 is fine, I used it in one of my courses and it worked fine. In fact, I also suggested the students that they could use Python, but nobody complained, so I guess all went with Matlab. We start feeding them Matlab when they are still toddlers, and that's the result :) @arun3688, would you mind having a look? |
@casella yes i will have a look into this |
OK, do you think you can fix the syntax? That would be good already. |
@casella I am currently working on the exporting |
@naibafomsare @casella The issue is fixed with this commit 809ed28 @naibafomsare you can test the changes from nighlty builds available from tomorrow (23-3-2022) |
If such code is expected to be integrated into other Python code bases: One thing I noticed is that it's very customary to have scope-defining indentation levels be a multiple of 4 spaces (not 1), i.e. this way:
There are some other linter-level inconsistencies that are probably too much effort to fix consistenly, but that one should be easy and will reduce the amount of linter noise significantly. :-) |
@bilderbuchi that is true the python codes have default 4 spaces, but i see the current generated code has 2 spaces and not 1 |
Ah, I misread the diff, then, thought it was 1. |
Thank you very much! With the changes to the syntax in the These changes are already useful. However, for larger systems, with several inputs, outputs and state variables, it would be good to provide the names of the variables in dictionaries, together with their indices within their vectors. Currently, what I do is to process the file |
@naibafomsare The current generated python code structure looks like this
here
the way to interpret, the comments
So in the current python generated code , we can generate new variables which contains the stateNames, InputNames and output names something like below
So the final generated code looks something like below
and you can easily map the names with corresponding list, What do you think ? |
@arun3688 The solution that you propose seems fine, and it's simpler: the indices, corresponding to each variable, are indicated by the place of the variable name within the list. However, your example seems confusing, maybe because there's only one input and one output in the example:
Shouldn't the
instead? |
@naibafomsare , you are correct, That was an typing mistake |
@naibafomsare, the issue of this ticket is fixed, so I'm closing it now. See #8770 for the new feature. |
@naibafomsare, the feature of generating different vars is implemented with this commit, 62abb40 |
Description
The
linearize()
function produces a file with information about the linearized model, that can be written in either Modelica, Python, Julia or Matlab format. In the case of Python, the file generated has errors.The file is called
linearized_model.py
and contains a function. The fist error is that a semicolon is missing at the end of the first line. It should bedef linearized_model():
rather than justdef linearized_model()
.The second error is that the format in which the A, B, C and D matrices are written, is not recognized by Python. It simply produces a syntax error. Although listening to the opinion of a Python expert is in order, it sounds natural to provide those matrices in a format that can be used directly in Python to define a space state model, for example, with the function
control.ss
. According to the documentation, such a function requires the matrix to be in an array_like or string format.Steps to Reproduce
Linearize the model SpringPendulum.mo.txt by issuing the following commands on OMShell:
Then, open ipython and try to load the file with
import linearized_model
. You will get the syntax error message.Expected Behavior
The function referred to above should be written in a valid Python format, and the state space matrices should be written in a form compatible with Python's control package.
A third observation is about how the information about the variable names and vector indices are provided. Those are not returned by the function, but they are rather written as comments with the hash symbol
#
in front of them. It would be far more useful to make this information available in the form of dictionaries, one for each of the state, input and output variables. The keys of the dictionaries could be the names of the variables, and the values, the indices of the variables within their corresponding vectors. Again, it would be good to ask a Python expert about this.By the way, what are the variables
What are their meaning in terms of the physical elements of the model? I want to calculate the eigenvectors of the system and knowing what they are would be useful.
Version and OS
The text was updated successfully, but these errors were encountered: