TRACE User Guide  TRACE Version 9.8.1
Machine Learning framework

Overview

The TRACE Machine Learning framework is intended to enable and facilitate the optimization and augmentation of physical models used in TRACE with data-driven techniques. It is particularly designed for methods which are based on Bayesian inference, or in other words, which aim at solving the inverse problem. These are for example the different variants of field inversion proposed by the group of Duraisamy, as well as the data-driven Gene Expression approach initially demonstrated by Sandberg.

The Bayesian inference requires the solver to be repeated evaluations of the CFD solver in each iteration of the model optimization. Gradient based optimization algorithms as used for field inversion additionally require the computation of gradients for each solver invocation. Therefore, the machine learning framework in TRACE is fully based on the AD adjoint functionality and shares most of its settings. Before further reading you are advised to get familiar with basic concepts of the AD adjoint framework described in adAdjointTRACE.

This framework is developed to compute gradients for a multitude of flexibly definable different machine learning functions, which are embedded into the CFD code such as radial basis functions, smaller neural networks, expressions with optimizable constants or constant fields. Furthermore, it is designed to enable a developer with access to the source code to easily extend the functionality to be applied to almost every arbitrary location within Trace. It does not provide any optimization algorithms, since the framework is intended to be used in conjunction with powerful optimization tools provided by thirdparty libraries such as scipy.

Structure

Model configuration and reference case perspective of the Machine Learning framework

When designing and performing machine learning related tasks, there are generally two perspective from which the problem can be viewed. The first one is the question which machine learning model shall be chosen and how it shall be configured. E.g. whether to use a radial basis function or a neural network and how many layers etc. The second perspective is the data perspective. I.e. which data shall be used for training the selected model and how it shall be processed.

The distinction between these two subjects is reflected within the code design of the machine learning framework as well as in the user interface. In order to perform a bayesian inference on a given reference case for a given machine learning model, the user must provide two input files. One input file contains the definition of the requested machine learning model and is used for all reference cases that are used for a given model optimization. The second input file contains information about the reference data, hence is different for each given reference case. For example, it contains information about a reference file containing the pressure distribution for a given blade profile. Therefore, this file usually resides within the folder of the reference case and will stay the same when the machine learning model is changed.

Workflow

The typical motivation is to augment a given turbulence model with a certain machine learning model, e.g. a neural network. The task is then to fit the neural networks weights - as part of the given augmented turbulence model - in such a way, that the solutions of the augmented turbulence model better match the given reference data for all given reference cases. Reference data might originate from experimental studies and may consist only of e.g. surface pressure distributions, or it may originate from scale resolving simulations and consist of whole time averaged flow fields.

For example, the weights and bias values of a neural network that was chosen as machine learning model are the optimization parameters used to minimize the optimization functional. The optimization functional is a measure of how far the current augmented solution deviates from the given reference data. If multiple reference cases are used, the individual contributions of each case needs to be reduced into a total deviation of all cases, which is used ultimately as the global optimization functional. On a practical level, this can be achieved by simply adding up the gradients determined for each individual case.

Performing a single case evaluation

In order to compute the gradients of the machine learning models parameters with respect to the deviation to the reference data of a single case, the case configuration file as well as the machine learning model configuration file are required. Firstly, the primal run has to be conducted with the current values of the optimization parameters as input and a checkpoint has to be written. The command line to compute and write the checkpoint may read as follows:

mpirun -np [#procs] TRACEcxxprim -cgns TRACE.cgns -adprop writeCheckpoints.prop -mlcase caseDefinition.json -mlconf mlModelConfig.json [+ any additional trace arguments]

Secondly, the reverse AD run computes the gradients for the given optimization values:

mpirun -np [#procs] TRACEcxxcodi -cgns TRACE.cgns -adprop reverseAccumulation.prop -mlcase caseDefinition.json -mlconf mlModelConfig.json [+ any additional trace arguments]

Note that in order to compute gradients with respect to the machine learning optimization functional (I.e. the deviation of the solution from the reference data) you must set the value "name" of subsection /settings/functional/trace to "FlowFieldDiff" in the writeCheckpoints.prop as well as the reverseAccumulation.prop. The according section might look as follows:

subsection settings
subsection functional
set use post = 0
subsection post
set com directory =
end
subsection trace
set value name = FlowFieldDiff
set point =
end
end
...
end

Controlling the optimization parameters and gradients

The file which contains the input values for the optimization parameters as well as the file where gradients are written to are specified in the machine learning model configuration file. Both are hdf5 files with an interpretable dictionary like structure. The gradients file will have the same structure as the values file, such that the assignment of the gradients to the values is easily possible.

When setting up a new optimization, an initial values file has to be set up with the correct structure containing the desired initial values. The machine learning framework provides functionality which sets up the values file for a given machine learning configuration file with default values. Inorder to generate an initial values file, set the parameter "phase" in the machine learning configurations file from "iteration" to "init". In this mode the checkpoint writing step does not require an optimization parameter values file, yet will write one filled with default values.

Generating JSON schemas for case and model config files

Setting up the case definition and model configuration input json files might be cumbersome particularly for new user. To facilitate the usage, Trace provides functionality to automatically generate JSON schema file for both, the case definition and the model configuration file. Subsequently, these schema files can be used for example for code completion in common json readers or even for generating a generic GUI based on json schema. Detailed documentation of both files is provided within the schemas. The following command will generate the two json schemas:

TRACEcxxprim -cgns TRACE.cgns -mlschema

The model configuration file

With the model configuration file the user can configure the machine learning model and provide additional settings from the model perspective as described in section Structure. This file provides settings which are not related to a specific test case. For a more detailed description, check the documentation provided by the json schemas Generating JSON schemas for case and model config files.

General settings regarding in and output files for optimization values and gradients can be made in the node "ioDefinition". The node "activeConstants" allows the activation of native turbulence model constant for gradient computation. Lastly, individual configuration of machine learning functions is provided by the node "learnables".

An example of a model configuration which features a neural network for the correction of the destruction term in the k equation of the k omega model as well as a symbolic expression for the production term looks as follows:

{
"ioDefinition": {
"fileType": "hdf5",
"phase": "iteration",
"valuesFile": "values_init_nn.h5",
"gradientsFile": "gradients_nn.h5",
"statisticsFile": "statistics.h5"
},
"learnables": {
"DestructionKFactor": {
"type": "expression",
"blendingFactor": 1,
"registerCalls": false,
"numberOfConstants": 2,
"implementation": "{y[0] = c[0] + c[1] * x[0];}",
"prefix": ""
},
"ProductionKFactor": {
"type": "eigenFCNN",
"blendingFactor": 1,
"registerCalls": false,
"valueNormalization": {
"input": {
"type": "stddev",
"mean": "normalization/meanInput.json",
"stddev": "normalization/stddevInput.json"
},
"output": {
"type": "stddev",
"mean": "normalization/meanOutput.json",
"stddev": "normalization/stddevOutput.json"
}
},
"innerLayers": [
{
"neurons": 3,
"activation": {
"type": "relu",
"alpha": 0.1
}
},
{
"neurons": 4,
"activation": {
"type": "elu",
"alpha": 1
}
}
],
"outputActivation": {
"type": "linear"
},
"init": {
"type": "default"
},
"regularization": {
"tikhonov": 0
}
}
},
"activeConstants": {
"Wilcox 1988 beta tke": true,
"Wilcox 1988 beta omega": true
}
}

The case definition file

The case definition file provides parameters which are specific to a given test case. I.e. the array node "losses" defines how the reference data is structured and how the deviation shall be computed. An arbitrary number of different sources files of reference data can be provided. The node "divergenceCriteria" allows the definition of divergence criteria specific to the testcase. If a divergence criteria is met, the computation will be terminated. This feature can be used to save resources e.g. for Gene Expression Programming, to prevent (hopelessly) diverging simulations from running until reaching maximum iterations. The node "functional" defines how different contributions to the global optimization functional are computed and weighted. It further contains setting regarding the output of each contributions value in the loss file. For a more detailed description, check the documentation provided by the json schemas Generating JSON schemas for case and model config files. An example case definition file which uses the pressure and tke field of an equally structured cgns file as reference looks as follows:

{
"functional": {
"reconstructionLossWeight": 1,
"regularizationLossWeight": 1,
"residualLossWeight": 0,
"residualLossBase": 1e-8,
"lastIterationLossWeight": 0,
"lossFile": "loss.dat"
},
"divergenceCriteria": {},
"losses": [
{
"type": "cgnsField",
"quantities": [],
"file": "reference.cgns",
"normalizationDegree": 2,
"useDimensionalizedData": 1,
"variables": [
{
"name": "pressure",
"weight": 7.038139e-06
},
{
"name": "turbulentEnergyKinetic",
"weight": 7.64263e-07
}
]
}
]
}