DAFunction.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v4
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAFunction.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
16 
17 defineTypeNameAndDebug(DAFunction, 0);
18 defineRunTimeSelectionTable(DAFunction, dictionary);
19 
20 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
21 
22 DAFunction::DAFunction(
23  const fvMesh& mesh,
24  const DAOption& daOption,
25  const DAModel& daModel,
26  const DAIndex& daIndex,
27  const word functionName)
28  : mesh_(mesh),
29  daOption_(daOption),
30  daModel_(daModel),
31  daIndex_(daIndex),
32  functionName_(functionName)
33 {
34  functionDict_ = daOption.getAllOptions().subDict("function").subDict(functionName);
35 
36  // Assign type and scale, this is common for all objectives
37  functionDict_.readEntry<word>("type", functionType_);
38  functionDict_.readEntry<scalar>("scale", scale_);
39  timeOp_ = functionDict_.lookupOrDefault<word>("timeOp", "final");
40 
41  // calcualte the face and cell indices that are associated with this objective
42  this->calcFunctionSources();
43 
44  // initialize calcRefVar related stuff
45  calcRefVar_ = functionDict_.lookupOrDefault<label>("calcRefVar", 0);
46  if (calcRefVar_)
47  {
48  functionDict_.readEntry<scalarList>("ref", ref_);
49  }
50 }
51 
52 // * * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * //
53 
54 autoPtr<DAFunction> DAFunction::New(
55  const fvMesh& mesh,
56  const DAOption& daOption,
57  const DAModel& daModel,
58  const DAIndex& daIndex,
59  const word functionName)
60 {
61  // standard setup for runtime selectable classes
62 
63  dictionary functionDict = daOption.getAllOptions().subDict("function").subDict(functionName);
64 
65  // look up the solver name
66  word modelType;
67  functionDict.readEntry<word>("type", modelType);
68 
69  if (daOption.getAllOptions().lookupOrDefault<label>("debug", 0))
70  {
71  Info << "Selecting type: " << modelType << " for DAFunction. Name: " << functionName << endl;
72  }
73 
74  dictionaryConstructorTable::iterator cstrIter =
75  dictionaryConstructorTablePtr_->find(modelType);
76 
77  // if the solver name is not found in any child class, print an error
78  if (cstrIter == dictionaryConstructorTablePtr_->end())
79  {
80  FatalErrorIn(
81  "DAFunction::New"
82  "("
83  " const fvMesh&,"
84  " const DAOption&,"
85  " const DAModel&,"
86  " const DAIndex&,"
87  " const word,"
88  ")")
89  << "Unknown DAFunction type "
90  << modelType << nl << nl
91  << "Valid DAFunction types:" << endl
92  << dictionaryConstructorTablePtr_->sortedToc()
93  << exit(FatalError);
94  }
95 
96  // child class found
97  return autoPtr<DAFunction>(
98  cstrIter()(mesh,
99  daOption,
100  daModel,
101  daIndex,
102  functionName));
103 }
104 
105 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
106 
108 {
109  /*
110  Description:
111  Compute the face and cell sources for the objective function.
112 
113  Output:
114  faceSources, cellSources: The face and cell indices that
115  are associated with this objective function
116 
117  Example:
118  A typical function dictionary reads:
119 
120  {
121  "type": "force",
122  "source": "patchToFace",
123  "patches": ["walls", "wallsbump"],
124  "scale": 0.5,
125  }
126 
127  This information is obtained from DAFunction::functionDict_
128 
129  */
130 
131  // all avaiable source type are in src/meshTools/sets/cellSources
132  // Example of IO parameters os in applications/utilities/mesh/manipulation/topoSet
133 
134  word functionSource;
135  functionDict_.readEntry("source", functionSource);
136  if (functionSource == "patchToFace")
137  {
138  // create a topoSet
139  autoPtr<topoSet> currentSet(
140  topoSet::New(
141  "faceSet",
142  mesh_,
143  "set0",
144  IOobject::NO_READ));
145  // create the source
146  autoPtr<topoSetSource> sourceSet(
147  topoSetSource::New(functionSource, mesh_, functionDict_));
148 
149  // add the sourceSet to topoSet
150  sourceSet().applyToSet(topoSetSource::NEW, currentSet());
151  // get the face index from currentSet, we need to use
152  // this special for loop
153  for (const label i : currentSet())
154  {
155  faceSources_.append(i);
156  }
157  }
158  else if (functionSource == "boxToCell")
159  {
160  // create a topoSet
161  autoPtr<topoSet> currentSet(
162  topoSet::New(
163  "cellSet",
164  mesh_,
165  "set0",
166  IOobject::NO_READ));
167  // we need to change the min and max because they need to
168  // be of type point; however, we can't parse point type
169  // in pyDict, we need to change them here.
170  dictionary functionTmp = functionDict_;
171  scalarList boxMin;
172  scalarList boxMax;
173  functionDict_.readEntry("min", boxMin);
174  functionDict_.readEntry("max", boxMax);
175 
176  point boxMin1;
177  point boxMax1;
178  boxMin1[0] = boxMin[0];
179  boxMin1[1] = boxMin[1];
180  boxMin1[2] = boxMin[2];
181  boxMax1[0] = boxMax[0];
182  boxMax1[1] = boxMax[1];
183  boxMax1[2] = boxMax[2];
184 
185  functionTmp.set("min", boxMin1);
186  functionTmp.set("max", boxMax1);
187 
188  // create the source
189  autoPtr<topoSetSource> sourceSet(
190  topoSetSource::New(functionSource, mesh_, functionTmp));
191 
192  // add the sourceSet to topoSet
193  sourceSet().applyToSet(topoSetSource::NEW, currentSet());
194  // get the face index from currentSet, we need to use
195  // this special for loop
196  for (const label i : currentSet())
197  {
198  cellSources_.append(i);
199  }
200  }
201  else if (functionSource == "allCells")
202  {
203  forAll(mesh_.cells(), cellI)
204  {
205  cellSources_.append(cellI);
206  }
207  }
208  else
209  {
210  FatalErrorIn("calcFunctionSources") << "source: " << functionSource << " not supported!"
211  << "Options are: allCells, patchToFace, or boxToCell!"
212  << abort(FatalError);
213  }
214 }
215 
216 void DAFunction::calcRefVar(scalar& functionValue)
217 {
218  /*
219  Description:
220  Call the variable difference with respect to a given reference and take a square of it.
221  This can be used in FIML. This function is for calcRefVar == 1
222  */
223 
224  if (calcRefVar_)
225  {
226  if (ref_.size() == 1)
227  {
228  functionValue = (functionValue - ref_[0]) * (functionValue - ref_[0]);
229  }
230  else
231  {
232  label idxI = mesh_.time().timeIndex() - 1;
233  functionValue = (functionValue - ref_[idxI]) * (functionValue - ref_[idxI]);
234  }
235  }
236 }
237 
238 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
239 
240 } // End namespace Foam
241 
242 // ************************************************************************* //
Foam::DAFunction::faceSources_
labelList faceSources_
a sorted list of all face sources for the objective function
Definition: DAFunction.H:67
Foam::DAFunction::calcFunctionSources
void calcFunctionSources()
calculate DAFunction::faceSources_ and DAFunction::cellSources_
Definition: DAFunction.C:107
Foam::DAOption
Definition: DAOption.H:29
DAFunction.H
Foam::DAFunction::calcRefVar_
label calcRefVar_
whether to calculate (obj-ref)^2
Definition: DAFunction.H:76
Foam::DAFunction::calcRefVar
void calcRefVar(scalar &functionValue)
calculate (var-ref)^2
Definition: DAFunction.C:216
Foam::DAOption::getAllOptions
const dictionary & getAllOptions() const
return a reference of allOptions_ dictionary
Definition: DAOption.H:56
Foam::DAFunction::timeOp_
word timeOp_
time operator such as final, sum, or average
Definition: DAFunction.H:61
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DAIndex
Definition: DAIndex.H:32
Foam::defineRunTimeSelectionTable
defineRunTimeSelectionTable(DAFunction, dictionary)
Foam::DAModel
Definition: DAModel.H:57
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFunction, 0)
Foam
Definition: checkGeometry.C:32
forAll
forAll(nuTilda1, cellI)
Definition: nuTilda1EqnIrkPimple.H:19
Foam::DAFunction::New
static autoPtr< DAFunction > New(const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const word functionName)
Definition: DAFunction.C:54
Foam::DAFunction::functionType_
word functionType_
the type of the objective function
Definition: DAFunction.H:58
Foam::DAFunction::functionDict_
dictionary functionDict_
dictionary containing the information for the objective function
Definition: DAFunction.H:64
Foam::DAFunction::cellSources_
labelList cellSources_
a sorted list of all cell sources for the objective function
Definition: DAFunction.H:70
Foam::DAFunction::scale_
scalar scale_
scale of the objective function
Definition: DAFunction.H:73
Foam::DAFunction::ref_
scalarList ref_
if calcRefVar_ is True, set the reference value list
Definition: DAFunction.H:79
Foam::DAFunction::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAFunction.H:43