Training Data Generation

To generate training data for the slowest non-linear equations found during Profiling Modelica Models we now simulate the equations multiple time and save in- and outputs.

We will use the Functional Mock-up Interface (FMI) standard to generate FMU that we extend with some function to evaluate single equations without the need to simulate the rest of the model.

Functions

NonLinearSystemNeuralNetworkFMU.generateTrainingDataFunction
generateTrainingData(fmuPath, workDir, fname, eqId, inputVars, inputBounds, outputVars;
                     options=DataGenOptions())

Generate training data for given equation of FMU.

Generate random inputs between min and max, evalaute equation and compute output. All input-output pairs are saved in CSV file fname.

Arguments

  • fmuPath::String: Path to FMU.
  • workDir::String: Working directory for generateTrainingData.
  • fname::String: File name to save training data to.
  • eqId::Int64: Index of equation to generate training data for.
  • inputVars::Array{String}: Array with names of input variables.
  • inputBounds::MinMaxBoundaryValues{T}: Boundary values for input space.
  • outputVars::Array{String}: Array with names of output variables.

Keywords

  • options::DataGenOptions: Settings for data generation.

See also generateFMU, DataGenOptions..

source
NonLinearSystemNeuralNetworkFMU.addEqInterface2FMUFunction
addEqInterface2FMU(modelName, pathToFmu, eqIndices; workingDir=pwd())

Create extendedFMU with special_interface to evalaute single equations.

Arguments

  • modelName::String: Name of Modelica model to export as FMU.
  • pathToFmu::String: Path to FMU to extend.
  • eqIndices::Array{Int64}: Array with equation indices to add equiation interface for.

Keywords

  • workingDir::String=pwd(): Working directory. Defaults to current working directory.

Returns

  • Path to generated FMU workingDir/<modelName>.interface.fmu.

See also profiling, generateFMU, generateTrainingData.

source

Structures

NonLinearSystemNeuralNetworkFMU.DataGenOptionsType
DataGenOptions <: Any

Settings for data generation.

  • method::NonLinearSystemNeuralNetworkFMU.DataGenerationMethod: Method to generate data points. Allowed values: RandomMethod, RandomWalkMethod

  • n::Integer: Number of data points to generate.

  • nBatches::Integer: Number of batches to divide N into.

  • nThreads::Integer: Number of threads to use in parallel

  • append::Bool: Append to already existing data

  • clean::Bool: Clean up temp CSV files

See als RandomMethod, RandomWalkMethod.

source
NonLinearSystemNeuralNetworkFMU.RandomWalkMethodType
RandomWalkMethod <: DataGenerationMethod

Randomized brownian-like motion data generation. Tries to stay within one solution in case the non-linear system is not unique solveable. Uses previous data point to generate close data point with previous solution as input to NLS solver.

  • delta::Float64: Step size of random walk.
source

Examples

First we need to create a Model-Exchange 2.0 FMU with OpenModelica.

This can be done directly from OpenModelica or with generateFMU:

moFiles = ["test/simpleLoop.mo"]
options = OMOptions(workingDir = "tempDir")

fmu = generateFMU("simpleLoop",
                  moFiles;
                  options = options)
"tempDir/simpleLoop.fmu"

Next we need to add non-standard FMI function

fmi2Status myfmi2EvaluateEq(fmi2Component c, const size_t eqNumber)

that will call <modelname>_eqFunction_<eqIndex>(DATA* data, threadData_t *threadData) for all non-linear equations we want to generate data for.

Using addEqInterface2FMU this C code will be generated and added to the FMU.

interfaceFmu = addEqInterface2FMU("simpleLoop",
                                  fmu,
                                  [14],
                                  workingDir = "tempDir")
"tempDir/simpleLoop.interface.fmu"

Now we can create evaluate equation 14 for random values and save the outputs to generate training data.

using CSV
using DataFrames
options = DataGenOptions(n=10, nThreads=1)
boundary = MinMaxBoundaryValues([0.0, 0.95], [1.5, 3.15])
generateTrainingData(interfaceFmu,
                     "tempDir",
                     "simpleLoop_data.csv",
                     14,
                     ["s", "r"],
                     boundary,
                     ["y"];
                     options = options)
df =  DataFrame(CSV.File("simpleLoop_data.csv"))
10×4 DataFrame
RowsryTrace
Float64Float64Float64Int64
11.064692.594060.1736331
21.064542.593720.1731971
31.065592.593680.1760941
41.066442.592020.1783461
51.065462.590730.1755321
61.065222.59080.1748861
71.065532.589170.1756321
81.06522.58880.1746831
91.063932.5890.1711741
101.063842.590080.1710061