Skip to content

Commit

Permalink
Merge pull request #15 from dnv-innersource/feature/7-move-example-fmus
Browse files Browse the repository at this point in the history
Move ML FMU examples into examples folder
  • Loading branch information
KristofferSkare authored Mar 7, 2024
2 parents 93b7fe0 + 7cfc2f6 commit 33375c5
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 3 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ The changelog format is based on [Keep a Changelog](https://keepachangelog.com/e

## [Unreleased]

### Changed
* Default build target in builder to wind_to_power example

### Added
* Wind to power model example in examples
* Onnx file containing ml model
* Interface json file containing information needed not contained in onnx file
* The generated fmu files resulting from running running builder on the new wind_to_power example

### Changed
* CMakeList.txt (from old repo) to be able to take an arbitrary path where the fmu files to be compiled are located
* Builder to run commands to compile generated files into an fmu
Expand Down
14 changes: 14 additions & 0 deletions examples/wind_to_power/WindToPower/modelDescription.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<fmiModelDescription fmiVersion="2.0" modelName="WindToPower" guid="@FMU_UUID@" version="0.0.1" generationDateAndTime="2024-03-06T15:52:41+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">
<Real start="0" />
</ScalarVariable>
<ScalarVariable name="windDirection" valueReference="1" causality="input" description="Noise to be added to the change in wind direction" variability="continuous">
<Real start="0" />
</ScalarVariable>
<ScalarVariable name="power" valueReference="2" causality="output" description="The estimated windmill power output" variability="continuous">
<Real />
</ScalarVariable>
</ModelVariables>
</fmiModelDescription>
Binary file not shown.
23 changes: 23 additions & 0 deletions examples/wind_to_power/WindToPower/sources/fmu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "model_definitions.h"
#include "fmu-uuid.h"
#include <cppfmu_cs.hpp>
#include <onnxFmu.hpp>


class WindToPower : public OnnxFmu{
public:
WindToPower(cppfmu::FMIString fmuResourceLocation) : OnnxFmu(fmuResourceLocation){}
private:
};


cppfmu::UniquePtr<cppfmu::SlaveInstance> CppfmuInstantiateSlave(
cppfmu::FMIString /*instanceName*/, cppfmu::FMIString fmuGUID, cppfmu::FMIString fmuResourceLocation,
cppfmu::FMIString /*mimeType*/, cppfmu::FMIReal /*timeout*/, cppfmu::FMIBoolean /*visible*/,
cppfmu::FMIBoolean /*interactive*/, cppfmu::Memory memory, cppfmu::Logger /*logger*/)
{
if (std::strcmp(fmuGUID, FMU_UUID) != 0) {
throw std::runtime_error("FMU GUID mismatch");
}
return cppfmu::AllocateUnique<WindToPower>(memory, fmuResourceLocation);
}
20 changes: 20 additions & 0 deletions examples/wind_to_power/WindToPower/sources/model_definitions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#define NUM_FMU_VARIABLES 3

#define NUM_ONNX_INPUTS 2
#define NUM_ONNX_FMU_INPUTS 2
#define NUM_ONNX_OUTPUTS 1
#define NUM_ONNX_FMU_OUTPUTS 1
#define NUM_ONNX_STATES 0
#define NUM_ONNX_STATES_OUTPUTS 0

#define ONNX_INPUT_VALUE_REFERENCES 0, 0, 1, 1
#define ONNX_OUTPUT_VALUE_REFERENCES 0, 2
#define ONNX_STATE_OUTPUT_INDEXES

#define ONNX_USE_TIME_INPUT false

#define ONNX_INPUT_NAME "args_0"
#define ONNX_OUTPUT_NAME "output_1"
#define ONNX_STATE_NAME ""
#define ONNX_TIME_INPUT_NAME ""
#define ONNX_FILENAME L"example.onnx"
Binary file added examples/wind_to_power/config/example.onnx
Binary file not shown.
33 changes: 33 additions & 0 deletions examples/wind_to_power/config/interface.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "WindToPower",
"description": "A Machine Learning based FMU that outputs the estimated power output of a windmill given the wind speed and direction.",
"inputs": [
{
"name": "windSpeed",
"description": "Noise to be added to the change in wind speed",
"agentInputIndexes": [
"0"
]
},
{
"name": "windDirection",
"description": "Noise to be added to the change in wind direction",
"agentInputIndexes": [
"1"
]
}
],
"parameters": [],
"outputs": [
{
"name": "power",
"description": "The estimated windmill power output",
"agentOutputIndexes": [
"0"
]
}
],
"states": {
"agentOutputIndexes": []
}
}
6 changes: 3 additions & 3 deletions src/mlfmu/utils/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
# TODO: I had some problems with this absolute_path.parent.parent, so I changed it to this to make it work.
# These are just temporary hard coded values that should be provided by the user. So it isn't that important.
template_parent_path = absolute_path / "templates" / "fmu"
json_interface = absolute_path / "examples" / "wind_generator" / "config" / "interface.json"
fmu_src_path = absolute_path / "examples" / "wind_generator"
onnx_path = absolute_path / "examples" / "wind_generator" / "config" / "example.onnx"
json_interface = absolute_path / "examples" / "wind_to_power" / "config" / "interface.json"
fmu_src_path = absolute_path / "examples" / "wind_to_power"
onnx_path = absolute_path / "examples" / "wind_to_power" / "config" / "example.onnx"
build_path = absolute_path / "build_fmu"
save_fmu_path = absolute_path / "fmus"

Expand Down

0 comments on commit 33375c5

Please sign in to comment.