DARegression.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v4
5 
6  Description:
7  Regression model
8 
9 \*---------------------------------------------------------------------------*/
10 
11 #ifndef DARegression_H
12 #define DARegression_H
13 
14 #include "fvOptions.H"
15 #include "surfaceFields.H"
16 #include "DAOption.H"
17 #include "DAUtility.H"
18 #include "DAModel.H"
19 #include "globalIndex.H"
20 #include "DAMacroFunctions.H"
21 
22 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
23 
24 namespace Foam
25 {
26 
27 /*---------------------------------------------------------------------------*\
28  Class DARegression Declaration
29 \*---------------------------------------------------------------------------*/
30 
32  : public regIOobject
33 {
34 
35 private:
37  DARegression(const DARegression&);
38 
40  void operator=(const DARegression&);
41 
42 protected:
44  const fvMesh& mesh_;
45 
48 
50  const DAModel& daModel_;
51 
53  label active_;
54 
56  wordList modelNames_;
57 
59  HashTable<word> modelType_;
60 
62  HashTable<wordList> inputNames_;
63 
65  HashTable<word> outputName_;
66 
68  HashTable<labelList> hiddenLayerNeurons_;
69 
71  HashTable<scalarList> inputShift_;
72 
74  HashTable<scalarList> inputScale_;
75 
77  HashTable<scalar> outputShift_;
78 
80  HashTable<scalar> outputScale_;
81 
83  HashTable<scalarList> parameters_;
84 
86  HashTable<word> activationFunction_;
87 
89  HashTable<scalar> leakyCoeff_;
90 
92  HashTable<scalar> outputUpperBound_;
93 
95  HashTable<scalar> outputLowerBound_;
96 
98  HashTable<label> printInputInfo_;
99 
101  HashTable<scalar> defaultOutputValue_;
102 
104  HashTable<label> nRBFs_;
105 
107  HashTable<PtrList<volScalarField>> features_;
108 
109 #if defined(CODI_ADF)
110  double* featuresFlattenArrayDouble_;
112 
114  double* outputFieldArrayDouble_;
115 #else
116  scalar* featuresFlattenArray_;
118 
121 #endif
122  label useExternalModel_ = 0;
124 
127 
129  HashTable<label> writeFeatures_;
130 
131 public:
133  DARegression(
134  const fvMesh& mesh,
135  const DAOption& daOption,
136  const DAModel& daModel);
137 
139  virtual ~DARegression()
140  {
141  if (useExternalModel_)
142  {
143 #if defined(CODI_ADF)
144  delete[] featuresFlattenArrayDouble_;
145  delete[] outputFieldArrayDouble_;
146 #else
147  delete[] featuresFlattenArray_;
148  delete[] outputFieldArray_;
149 #endif
150  }
151  }
152 
153  // Members
154 
156  label compute();
157 
159  void calcInputFeatures(word modelName);
160 
162  label nParameters(word modelName);
163 
165  scalar getParameter(word modelName, label idxI)
166  {
167  return parameters_[modelName][idxI];
168  }
169 
171  void setParameter(word modelName, label idxI, scalar val)
172  {
173  parameters_[modelName][idxI] = val;
174  }
175 
177  label checkOutput(word modelName, volScalarField& outputField);
178 
180  void printInputInfo(const label printToScreen)
181  {
182  if (!active_ || !printToScreen)
183  {
184  return;
185  }
186  forAll(modelNames_, idxI)
187  {
188  word modelName = modelNames_[idxI];
189  if (printInputInfo_[modelName])
190  {
191  Info << "RegModel input info for " << modelName << endl;
192  forAll(inputNames_[modelName], idxI)
193  {
194  word name = inputNames_[modelName][idxI];
195  Info << name << " Min: " << gMin(features_[modelName][idxI]) << " Max: " << gMax(features_[modelName][idxI])
196  << " Avg: " << gAverage(features_[modelName][idxI]) << " Std: " << sqrt(gSumSqr(features_[modelName][idxI]) / features_[modelName][idxI].size()) << endl;
197  }
198  }
199  }
200  }
201 
204  {
205  if (!active_)
206  {
207  return;
208  }
209  forAll(modelNames_, idxI)
210  {
211  word modelName = modelNames_[idxI];
212  if (writeFeatures_[modelName])
213  {
214  forAll(features_[modelName], idxI)
215  {
216  features_[modelName][idxI].write();
217  }
218  }
219  }
220  }
221 
222 #ifdef CODI_ADR
223 /*
225  static void betaCompute(
226  const double* x,
227  size_t n,
228  double* y,
229  size_t m,
230  codi::ExternalFunctionUserData* d)
231  {
232  DAUtility::pyCalcBetaInterface(x, n, y, m, DAUtility::pyCalcBeta);
233  }
234 
235  static void betaJacVecProd(
236  const double* x,
237  double* x_b,
238  size_t n,
239  const double* y,
240  const double* y_b,
241  size_t m,
242  codi::ExternalFunctionUserData* d)
243  {
244  DAUtility::pyCalcBetaJacVecProdInterface(x, x_b, n, y, y_b, m, DAUtility::pyCalcBetaJacVecProd);
245  }
246 */
247 #endif
248 
250  bool writeData(Ostream& os) const
251  {
252  return true;
253  }
254 };
255 
256 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
257 
258 } // End namespace Foam
259 
260 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
261 
262 #endif
263 
264 // ************************************************************************* //
Foam::DARegression::mesh_
const fvMesh & mesh_
Foam::fvMesh object.
Definition: DARegression.H:44
DAOption.H
Foam::DARegression
Definition: DARegression.H:31
Foam::DARegression::inputNames_
HashTable< wordList > inputNames_
a list of words for the inputs
Definition: DARegression.H:62
Foam::DARegression::activationFunction_
HashTable< word > activationFunction_
neural network activation function
Definition: DARegression.H:86
Foam::DARegression::~DARegression
virtual ~DARegression()
Destructor.
Definition: DARegression.H:139
Foam::DARegression::outputName_
HashTable< word > outputName_
a list of words for the outputs
Definition: DARegression.H:65
Foam::DAOption
Definition: DAOption.H:29
DAUtility.H
Foam::DARegression::modelType_
HashTable< word > modelType_
the type of regression model
Definition: DARegression.H:59
Foam::DARegression::daOption_
const DAOption & daOption_
Foam::DAOption object.
Definition: DARegression.H:47
Foam::DARegression::featuresFlattenArray_
scalar * featuresFlattenArray_
a flatten feature array for externalTensorFlow model
Definition: DARegression.H:117
Foam::DARegression::leakyCoeff_
HashTable< scalar > leakyCoeff_
if the ReLU activation function is used we can prescribe a potentially leaky coefficient
Definition: DARegression.H:89
Foam::DARegression::nRBFs_
HashTable< label > nRBFs_
number of radial basis function
Definition: DARegression.H:104
Foam::DARegression::useExternalModel_
label useExternalModel_
whether to use external model
Definition: DARegression.H:123
Foam::DARegression::printInputInfo
void printInputInfo(const label printToScreen)
print the input
Definition: DARegression.H:180
DAMacroFunctions.H
Foam::DARegression::modelNames_
wordList modelNames_
name of the regression models
Definition: DARegression.H:56
Foam::DARegression::parameters_
HashTable< scalarList > parameters_
the parameters for the regression model
Definition: DARegression.H:83
DAModel.H
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DARegression::outputFieldArray_
scalar * outputFieldArray_
output field array for externalTensorFlow model
Definition: DARegression.H:120
Foam::DARegression::outputScale_
HashTable< scalar > outputScale_
we can scale the output. we always shift before scaling it.
Definition: DARegression.H:80
Foam::DARegression::featuresFlattenArraySize_
label featuresFlattenArraySize_
the array size is chosen based on the regModel that has the largest number of inputs (this is importa...
Definition: DARegression.H:126
Foam::DARegression::checkOutput
label checkOutput(word modelName, volScalarField &outputField)
check if the output values are valid otherwise bound or fix them
Definition: DARegression.C:698
Foam::DARegression::setParameter
void setParameter(word modelName, label idxI, scalar val)
set a value to a parameter give an index and a value
Definition: DARegression.H:171
Foam::DARegression::inputShift_
HashTable< scalarList > inputShift_
we can shift each input. we always shift before scaling it.
Definition: DARegression.H:71
Foam::DARegression::inputScale_
HashTable< scalarList > inputScale_
we can scale each input. we always shift before scaling it.
Definition: DARegression.H:74
Foam::DAModel
Definition: DAModel.H:57
Foam::DARegression::writeFeatures
void writeFeatures()
write the features to the disk
Definition: DARegression.H:203
Foam::DARegression::daModel_
const DAModel & daModel_
DAModel object.
Definition: DARegression.H:50
Foam
Definition: checkGeometry.C:32
Foam::DARegression::calcInputFeatures
void calcInputFeatures(word modelName)
calculate the input flow features
Definition: DARegression.C:161
Foam::DARegression::writeData
bool writeData(Ostream &os) const
this is a virtual function for regIOobject
Definition: DARegression.H:250
Foam::DARegression::active_
label active_
whether the regression model is active
Definition: DARegression.H:53
Foam::DARegression::outputLowerBound_
HashTable< scalar > outputLowerBound_
the lower bound for the output
Definition: DARegression.H:95
Foam::DARegression::printInputInfo_
HashTable< label > printInputInfo_
whether to print the input range info this is used to scale the input
Definition: DARegression.H:98
forAll
forAll(nuTilda1, cellI)
Definition: nuTilda1EqnIrkPimple.H:19
Foam::DARegression::outputShift_
HashTable< scalar > outputShift_
we can shift the output. we always shift before scaling it.
Definition: DARegression.H:77
Foam::DARegression::getParameter
scalar getParameter(word modelName, label idxI)
get a specific parameter value
Definition: DARegression.H:165
Foam::DARegression::writeFeatures_
HashTable< label > writeFeatures_
whether to write the feature fields to the disk
Definition: DARegression.H:129
Foam::DARegression::outputUpperBound_
HashTable< scalar > outputUpperBound_
the upper bound for the output
Definition: DARegression.H:92
Foam::DARegression::hiddenLayerNeurons_
HashTable< labelList > hiddenLayerNeurons_
number of neurons hidden layers of the neural network
Definition: DARegression.H:68
Foam::DARegression::features_
HashTable< PtrList< volScalarField > > features_
a list of scalarField to save the input features
Definition: DARegression.H:107
Foam::DARegression::nParameters
label nParameters(word modelName)
get the number of parameters for this regression model
Definition: DARegression.C:638
Foam::DARegression::defaultOutputValue_
HashTable< scalar > defaultOutputValue_
default output values
Definition: DARegression.H:101
Foam::DARegression::compute
label compute()
compute the output based on the latest parameters and inputs
Definition: DARegression.C:340