From a5c0a2ab45076fc1e87366f883672d2424fdb861 Mon Sep 17 00:00:00 2001 From: Kristoffer Skare Date: Thu, 19 Dec 2024 13:51:10 +0100 Subject: [PATCH] Change the order of the variables added to the modelDescription to be sorted by their variableReference --- CHANGELOG.md | 1 + src/mlfmu/utils/fmi_builder.py | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ff0424..c88fc55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The changelog format is based on [Keep a Changelog](https://keepachangelog.com/e ### Changed * Generation of modelDescription.xml file to correctly generate tags for each output with 1-indexed indexes and InitialUnknowns tags +* Generation of modelDescription.xml adds the variables (inputs, parameters and outputs) in the orderer of their valueReference. This is for the indexing to work correctly ## [1.0.2] diff --git a/src/mlfmu/utils/fmi_builder.py b/src/mlfmu/utils/fmi_builder.py index 0a68038..cdb3595 100644 --- a/src/mlfmu/utils/fmi_builder.py +++ b/src/mlfmu/utils/fmi_builder.py @@ -78,7 +78,14 @@ def generate_model_description(fmu_model: FmiModel) -> ElementTree: outputs = SubElement(model_structure, "Outputs") initial_unknowns = SubElement(model_structure, "InitialUnknowns") - for var in fmu_model.get_fmi_model_variables(): + # Get all variables to add them inside the tag + model_variables = fmu_model.get_fmi_model_variables() + + # The variables needs to be added in the order of their valueReference + sorted_model_variables = sorted(model_variables, key=lambda x: x.variable_reference) + + # Add each variable inside the tag + for var in sorted_model_variables: # XML variable attributes var_attrs = { "name": var.name,