Skip to content

Commit

Permalink
Merge branch 'main' into enh/23-initialize-states-with-existing-fmu-v…
Browse files Browse the repository at this point in the history
…ariables
  • Loading branch information
KristofferSkare committed Mar 12, 2024
2 parents e521093 + bf0561c commit 7732709
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ The changelog format is based on [Keep a Changelog](https://keepachangelog.com/e
* Added feature to be able to initialize states using previously defined parameters or inputs
* This is done by setting "initializationVariable" = "{variable name}", instead of using the "name" and "start_value" attributes

### Changed
* The generated modelDescription.xml files now also contain <ModelStructure> with a list of the outputs of the FMU.

### Changed
* OnnxFmu cpp template class updated to be able to initialize state with FMU Variables
* Add variables to specify which FMU variable that should be used to initialize which state
Expand Down
8 changes: 7 additions & 1 deletion examples/wind_generator/WindGenerator/modelDescription.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<fmiModelDescription fmiVersion="2.0" modelName="WindGenerator" guid="@FMU_UUID@" version="0.0.1" generationDateAndTime="2024-03-11T09:48:21+00:00" variableNamingConvention="structured" generationTool="MLFMU 0.1.6" description="A Machine Learning based FMU that outputs a synthetic time series of wind.">
<fmiModelDescription fmiVersion="2.0" modelName="WindGenerator" guid="@FMU_UUID@" version="0.0.1" generationDateAndTime="2024-03-12T14:06:00+00:00" variableNamingConvention="structured" generationTool="MLFMU 0.1.6" description="A Machine Learning based FMU that outputs a synthetic time series of wind.">
<CoSimulation modelIdentifier="WindGenerator" canHandleVariableCommunicationStepSize="true" />
<ModelVariables>
<ScalarVariable name="speedNoise" valueReference="0" causality="input" description="Noise to be added to the change in wind speed" variability="continuous">
Expand All @@ -20,4 +20,10 @@
<Real />
</ScalarVariable>
</ModelVariables>
<ModelStructure>
<Outputs>
<Unknown index="2" />
<Unknown index="3" />
</Outputs>
</ModelStructure>
</fmiModelDescription>
7 changes: 6 additions & 1 deletion examples/wind_to_power/WindToPower/modelDescription.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<fmiModelDescription fmiVersion="2.0" modelName="WindToPower" guid="@FMU_UUID@" version="0.0.1" generationDateAndTime="2024-03-11T10:21:38+00:00" variableNamingConvention="structured" generationTool="MLFMU 0.1.6" description="A Machine Learning based FMU that outputs the estimated power output of a windmill given the wind speed and direction.">
<fmiModelDescription fmiVersion="2.0" modelName="WindToPower" guid="@FMU_UUID@" version="0.0.1" generationDateAndTime="2024-03-12T14:06:31+00:00" variableNamingConvention="structured" generationTool="MLFMU 0.1.6" description="A Machine Learning based FMU that outputs the estimated power output of a windmill given the wind speed and direction.">
<CoSimulation modelIdentifier="WindToPower" canHandleVariableCommunicationStepSize="true" />
<ModelVariables>
<ScalarVariable name="windSpeed" valueReference="0" causality="input" description="Noise to be added to the change in wind speed" variability="continuous">
Expand All @@ -11,4 +11,9 @@
<Real />
</ScalarVariable>
</ModelVariables>
<ModelStructure>
<Outputs>
<Unknown index="2" />
</Outputs>
</ModelStructure>
</fmiModelDescription>
9 changes: 9 additions & 0 deletions src/mlfmu/utils/fmi_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def generate_model_description(fmu_model: FmiModel) -> ElementTree:

# <ModelVariables> tag -> Append inputs/parameters/outputs
variables = SubElement(root, "ModelVariables")

# <ModelStructure> tag with <Outputs> tab inside --> Append all outputs
model_structure = SubElement(root, "ModelStructure")
outputs = SubElement(model_structure, "Outputs")

for var in fmu_model.get_fmi_model_variables():
# XML variable attributes
var_attrs = dict(
Expand All @@ -93,6 +98,10 @@ def generate_model_description(fmu_model: FmiModel) -> ElementTree:
# FMI variable type element
_ = SubElement(var_elem, var.type.value.capitalize(), var_type_attrs)

# Appending output to <Outputs> inside <ModelStructure>
if var.causality == FmiCausality.OUTPUT:
_ = SubElement(outputs, "Unknown", {"index": str(var.variable_reference)})

# Create XML tree containing root element and pretty format its contents
xml_tree = ElementTree(root)
indent(xml_tree, space="\t", level=0)
Expand Down

0 comments on commit 7732709

Please sign in to comment.