Skip to content
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

determine continuous states of an FMU? #76

Open
GarronFish opened this issue Dec 10, 2024 · 4 comments
Open

determine continuous states of an FMU? #76

GarronFish opened this issue Dec 10, 2024 · 4 comments

Comments

@GarronFish
Copy link

It can be useful to know the names of the continuous states in a ME FMU. One way to do this using FMPy is described here: https://stackoverflow.com/questions/75730464/how-to-using-fmpy-extract-the-list-of-continuous-time-states. Is there an equivalent method for fmi4c?

What methods would you suggest for parsing the model_description file to extract that information?

@robbr48
Copy link
Owner

robbr48 commented Dec 10, 2024

That is a good suggestion! It is not possible now but should be fairly easy to add. Would it be sufficient with a function for fetching the value references? Something like this:

fmiX_getNumberOfContinuousStates(fmu, nStates);
fmiX_getContinuousStateValueReferences(fmu, valueReferences, nStates);
for(int i=0; i<nStates; ++i) {
    var = fmiX_getVariableByValueReference(valueReferences[i]);
    names[i] = fmiX_getVariableName(var)
}

@GarronFish
Copy link
Author

That would be good, thanks

@robbr48
Copy link
Owner

robbr48 commented Dec 13, 2024

After a second thought I came up with a different solution. The information is already available in the ModelStructure element of the model description XML. By fetching all derivative variables, and checking which variables they are the derivative of, a list of continuous states (and of course state derivataives) can easily be obtained:

numDerivatives = fmi2_getNumberOfModelStructureDerivatives();
for(int i=0; i<numDerivatives; ++i) {
    fmi2ModelStructureHandle unknown = fmi2_getModelStructureDerivative(i);
    int derIndex = fmi2_getModelStructureIndex(unknown);
    fmi2VariableHandle der = fmi2_getVariableByIndex(derIndex);
    int varIndex = fmi2_getVariableDerivativeIndex(der);
    fmi2VariableHandle var = fmi2_getVariableByIndex(varIndex);
    printf("Continuous State: %s\n", fmi2_getVariableName(var));
}

Would this work for you?

Notice that according to the FMI 2.0.4 specification,

The “continuous states” of a model are all variables that appear differentiated in the model and are independent from each other.

If you want to be strict you also need to check the second requirement. This information is also available in the XML and exposed in the API.

@GarronFish
Copy link
Author

That would work, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants