DAObjFunc.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v3
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAObjFunc.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
16 
17 defineTypeNameAndDebug(DAObjFunc, 0);
18 defineRunTimeSelectionTable(DAObjFunc, dictionary);
19 
20 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
21 
22 DAObjFunc::DAObjFunc(
23  const fvMesh& mesh,
24  const DAOption& daOption,
25  const DAModel& daModel,
26  const DAIndex& daIndex,
27  const DAResidual& daResidual,
28  const word objFuncName,
29  const word objFuncPart,
30  const dictionary& objFuncDict)
31  : mesh_(mesh),
32  daOption_(daOption),
33  daModel_(daModel),
34  daIndex_(daIndex),
35  daResidual_(daResidual),
36  objFuncName_(objFuncName),
37  objFuncPart_(objFuncPart),
38  objFuncDict_(objFuncDict),
39  daField_(mesh, daOption, daModel, daIndex)
40 {
41 
42  // calcualte the face and cell indices that are associated with this objective
44 
45  // initialize objFuncFaceValues_ and objFuncCellValues_ and assign zeros
46  // they will be computed later by calling DAObjFunc::calcObjFunc
49  {
50  objFuncFaceValues_[idxI] = 0.0;
51  }
52 
55  {
56  objFuncCellValues_[idxI] = 0.0;
57  }
58 }
59 
60 // * * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * //
61 
62 autoPtr<DAObjFunc> DAObjFunc::New(
63  const fvMesh& mesh,
64  const DAOption& daOption,
65  const DAModel& daModel,
66  const DAIndex& daIndex,
67  const DAResidual& daResidual,
68  const word objFuncName,
69  const word objFuncPart,
70  const dictionary& objFuncDict)
71 {
72  // standard setup for runtime selectable classes
73 
74  // look up the solver name
75  word modelType;
76  objFuncDict.readEntry<word>("type", modelType);
77 
78  if (daOption.getAllOptions().lookupOrDefault<label>("debug", 0))
79  {
80  Info << "Selecting type: " << modelType << " for DAObjFunc. Name: " << objFuncName
81  << " part: " << objFuncPart << endl;
82  }
83 
84  dictionaryConstructorTable::iterator cstrIter =
85  dictionaryConstructorTablePtr_->find(modelType);
86 
87  // if the solver name is not found in any child class, print an error
88  if (cstrIter == dictionaryConstructorTablePtr_->end())
89  {
90  FatalErrorIn(
91  "DAObjFunc::New"
92  "("
93  " const fvMesh&,"
94  " const DAOption&,"
95  " const DAModel&,"
96  " const DAIndex&,"
97  " const DAResidual&,"
98  " const word,"
99  " const word,"
100  " const dictionary&"
101  ")")
102  << "Unknown DAObjFunc type "
103  << modelType << nl << nl
104  << "Valid DAObjFunc types:" << endl
105  << dictionaryConstructorTablePtr_->sortedToc()
106  << exit(FatalError);
107  }
108 
109  // child class found
110  return autoPtr<DAObjFunc>(
111  cstrIter()(mesh,
112  daOption,
113  daModel,
114  daIndex,
115  daResidual,
116  objFuncName,
117  objFuncPart,
118  objFuncDict));
119 }
120 
121 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
122 
124  labelList& faceSources,
125  labelList& cellSources)
126 {
127  /*
128  Description:
129  Compute the face and cell sources for the objective function.
130 
131  Output:
132  faceSources, cellSources: The face and cell indices that
133  are associated with this objective function
134 
135  Example:
136  A typical objFunc dictionary reads:
137 
138  {
139  "type": "force",
140  "source": "patchToFace",
141  "patches": ["walls", "wallsbump"],
142  "scale": 0.5,
143  "addToAdjoint": False
144  }
145 
146  This information is obtained from DAObjFunc::objFuncDict_
147 
148  */
149 
150  // all avaiable source type are in src/meshTools/sets/cellSources
151  // Example of IO parameters os in applications/utilities/mesh/manipulation/topoSet
152 
153  word objSource;
154  objFuncDict_.readEntry("source", objSource);
155  if (objSource == "patchToFace")
156  {
157  // create a topoSet
158  autoPtr<topoSet> currentSet(
159  topoSet::New(
160  "faceSet",
161  mesh_,
162  "set0",
163  IOobject::NO_READ));
164  // create the source
165  autoPtr<topoSetSource> sourceSet(
166  topoSetSource::New(objSource, mesh_, objFuncDict_));
167 
168  // add the sourceSet to topoSet
169  sourceSet().applyToSet(topoSetSource::NEW, currentSet());
170  // get the face index from currentSet, we need to use
171  // this special for loop
172  for (const label i : currentSet())
173  {
174  faceSources.append(i);
175  }
176  }
177  else if (objSource == "boxToCell")
178  {
179  // create a topoSet
180  autoPtr<topoSet> currentSet(
181  topoSet::New(
182  "cellSet",
183  mesh_,
184  "set0",
185  IOobject::NO_READ));
186  // we need to change the min and max because they need to
187  // be of type point; however, we can't parse point type
188  // in pyDict, we need to change them here.
189  dictionary objFuncTmp = objFuncDict_;
190  scalarList boxMin;
191  scalarList boxMax;
192  objFuncDict_.readEntry("min", boxMin);
193  objFuncDict_.readEntry("max", boxMax);
194 
195  point boxMin1;
196  point boxMax1;
197  boxMin1[0] = boxMin[0];
198  boxMin1[1] = boxMin[1];
199  boxMin1[2] = boxMin[2];
200  boxMax1[0] = boxMax[0];
201  boxMax1[1] = boxMax[1];
202  boxMax1[2] = boxMax[2];
203 
204  objFuncTmp.set("min", boxMin1);
205  objFuncTmp.set("max", boxMax1);
206 
207  // create the source
208  autoPtr<topoSetSource> sourceSet(
209  topoSetSource::New(objSource, mesh_, objFuncTmp));
210 
211  // add the sourceSet to topoSet
212  sourceSet().applyToSet(topoSetSource::NEW, currentSet());
213  // get the face index from currentSet, we need to use
214  // this special for loop
215  for (const label i : currentSet())
216  {
217  cellSources.append(i);
218  }
219  }
220  else
221  {
222  FatalErrorIn("calcObjFuncSources") << "source: " << objSource << " not supported!"
223  << "Options are: patchToFace, boxToCell!"
224  << abort(FatalError);
225  }
226 }
227 
229  const dictionary& options,
230  const Vec xvVec,
231  const Vec wVec)
232 {
233  /*
234  Description:
235  A master function that takes the volume mesh points and state variable vecs
236  as input, and compute the value of the objective and their discrete values
237  on each face/cell source
238 
239  Input:
240  options.updateState: whether to assign the values in wVec to the state
241  variables of the OpenFOAM fields (e.g., U, p). This will also update boundary conditions
242  and update all intermediate variables that are dependent on the state
243  variables.
244 
245  options.updateMesh: whether to assign the values in xvVec to the OpenFOAM mesh
246  coordinates in Foam::fvMesh. This will also call mesh.movePoints() to update
247  all the mesh metrics such as mesh volume, cell centers, mesh surface area, etc.
248 
249  xvVec: the volume coordinates vector (flatten)
250 
251  wVec: the state variable vector
252 
253  Output:
254  objFuncValue: the reduced objective value
255 
256  */
257 
258  DAModel& daModel = const_cast<DAModel&>(daModel_);
259  DAResidual& daResidual = const_cast<DAResidual&>(daResidual_);
260 
261  label updateState = 0;
262  options.readEntry<label>("updateState", updateState);
263 
264  label updateMesh = 0;
265  options.readEntry<label>("updateMesh", updateMesh);
266 
267  if (updateMesh)
268  {
269  daField_.pointVec2OFMesh(xvVec);
270  }
271 
272  if (updateState)
273  {
275 
276  // now update intermediate states and boundry conditions
277  daResidual.correctBoundaryConditions();
278  daResidual.updateIntermediateVariables();
279  daModel.correctBoundaryConditions();
280  daModel.updateIntermediateVariables();
281  // if there are special boundary conditions, apply special treatment
283  }
284 
285  scalar objFuncValue = this->getObjFuncValue();
286 
287  return objFuncValue;
288 }
289 
291 {
292  /*
293  Description:
294  Call the calcObjFunc in the child class and return
295  objFuncValue_
296 
297  NOTE: This is a interface for external calls where users
298  only want compute the objective value based on the existing
299  variable fields in OpenFOAM; no need to update state variables
300  or point coordinates. This can be used in the primal solver to print
301  the objective for each time step.
302  */
303  // calculate
304  this->calcObjFunc(
309  objFuncValue_);
310 
311  // return
312  return objFuncValue_;
313 }
314 
315 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
316 
317 } // End namespace Foam
318 
319 // ************************************************************************* //
Foam::DAResidual::correctBoundaryConditions
virtual void correctBoundaryConditions()=0
update the boundary condition for all the states in the selected solver
Foam::DAObjFunc::daResidual_
const DAResidual & daResidual_
DAResidual object.
Definition: DAObjFunc.H:57
Foam::DAObjFunc::objFuncCellSources_
labelList objFuncCellSources_
a sorted list of all cell sources for the objective function
Definition: DAObjFunc.H:78
forAll
forAll(pseudoP.boundaryField(), patchI)
Definition: solvePseudoPEqn.H:10
Foam::DAObjFunc::daModel_
const DAModel & daModel_
DAModel object.
Definition: DAObjFunc.H:51
Foam::DAObjFunc::daField_
DAField daField_
DAField object.
Definition: DAObjFunc.H:72
Foam::DAOption
Definition: DAOption.H:29
Foam::DAResidual::updateIntermediateVariables
virtual void updateIntermediateVariables()=0
update any intermdiate variables that are dependent on state variables and are used in calcResiduals
daOption
DAOption daOption(mesh, pyOptions_)
Foam::DAObjFunc::objFuncCellValues_
scalarList objFuncCellValues_
value of the obj function in the discrete cells
Definition: DAObjFunc.H:84
Foam::DAObjFunc::calcObjFuncSources
void calcObjFuncSources(labelList &faceSources, labelList &cellSources)
calculate DAObjFunc::objFuncFaceSources_ and DAObjFunc::objFuncCellSources_
Definition: DAObjFunc.C:123
Foam::DAObjFunc::calcObjFunc
virtual void calcObjFunc(const labelList &objFuncFaceSources, const labelList &objFuncCellSources, scalarList &objFuncFaceValues, scalarList &objFuncCellValues, scalar &objFuncValue)=0
calculate the value of objective function
Foam::DAObjFunc::getObjFuncValue
scalar getObjFuncValue()
calculate the value of objective function
Definition: DAObjFunc.C:290
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::defineRunTimeSelectionTable
defineRunTimeSelectionTable(DAFvSource, dictionary)
Foam::DAObjFunc::masterFunction
scalar masterFunction(const dictionary &options, const Vec xvVec, const Vec wVec)
the master function to compute objective function given the state and point vectors
Definition: DAObjFunc.C:228
Foam::DAObjFunc::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAObjFunc.H:45
Foam::DAIndex
Definition: DAIndex.H:32
Foam::DAModel
Definition: DAModel.H:59
Foam
Definition: multiFreqScalarFvPatchField.C:144
Foam::DAObjFunc::New
static autoPtr< DAObjFunc > New(const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const DAResidual &daResidual, const word objFuncName, const word objFuncPart, const dictionary &objFuncDict)
Definition: DAObjFunc.C:62
Foam::DAField::stateVec2OFField
void stateVec2OFField(const Vec stateVec) const
assign the fields in OpenFOAM based on the state vector
Definition: DAField.C:375
Foam::DAResidual
Definition: DAResidual.H:35
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFvSource, 0)
daModel
DAModel daModel(mesh, daOption)
Foam::DAField::pointVec2OFMesh
void pointVec2OFMesh(const Vec xvVec) const
assign the points in fvMesh of OpenFOAM based on the point vector
Definition: DAField.C:506
Foam::DAObjFunc::objFuncFaceValues_
scalarList objFuncFaceValues_
value of the obj function on the discrete faces
Definition: DAObjFunc.H:81
daIndex
DAIndex daIndex(mesh, daOption, daModel)
Foam::DAObjFunc::objFuncDict_
const dictionary & objFuncDict_
dictionary containing the information for the objective function
Definition: DAObjFunc.H:69
DAObjFunc.H
Foam::DAField::specialBCTreatment
void specialBCTreatment()
apply special treatment for boundary conditions
Definition: DAField.C:812
Foam::DAObjFunc::objFuncValue_
scalar objFuncValue_
the value of the objective function
Definition: DAObjFunc.H:90
Foam::DAObjFunc::objFuncFaceSources_
labelList objFuncFaceSources_
a sorted list of all face sources for the objective function
Definition: DAObjFunc.H:75