DAObjFuncVariableVolSum.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v3
5 
6 \*---------------------------------------------------------------------------*/
7 
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 defineTypeNameAndDebug(DAObjFuncVariableVolSum, 0);
16 addToRunTimeSelectionTable(DAObjFunc, DAObjFuncVariableVolSum, dictionary);
17 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
18 
20  const fvMesh& mesh,
21  const DAOption& daOption,
22  const DAModel& daModel,
23  const DAIndex& daIndex,
24  const DAResidual& daResidual,
25  const word objFuncName,
26  const word objFuncPart,
27  const dictionary& objFuncDict)
28  : DAObjFunc(
29  mesh,
30  daOption,
31  daModel,
32  daIndex,
33  daResidual,
34  objFuncName,
35  objFuncPart,
36  objFuncDict)
37 {
38 
39  // Assign type, this is common for all objectives
40  objFuncDict_.readEntry<word>("type", objFuncType_);
41 
42  objFuncDict_.readEntry<scalar>("scale", scale_);
43 
44  objFuncDict_.readEntry<word>("varName", varName_);
45 
46  objFuncDict_.readEntry<word>("varType", varType_);
47 
48  objFuncDict_.readEntry<label>("component", component_);
49 
50  objFuncDict_.readEntry<label>("isSquare", isSquare_);
51 
52  objFuncDict_.readEntry<label>("divByTotalVol", divByTotalVol_);
53 
54  if (daIndex.adjStateNames.found(varName_))
55  {
57  }
58 }
59 
62  const labelList& objFuncFaceSources,
63  const labelList& objFuncCellSources,
64  scalarList& objFuncFaceValues,
65  scalarList& objFuncCellValues,
66  scalar& objFuncValue)
67 {
68  /*
69  Description:
70  Calculate the obj = mesh volume * variable (whether to take a square of the variable
71  depends on isSquare)
72 
73  Input:
74  objFuncFaceSources: List of face source (index) for this objective
75 
76  objFuncCellSources: List of cell source (index) for this objective
77 
78  Output:
79  objFuncFaceValues: the discrete value of objective for each face source (index).
80  This will be used for computing df/dw in the adjoint.
81 
82  objFuncCellValues: the discrete value of objective on each cell source (index).
83  This will be used for computing df/dw in the adjoint.
84 
85  objFuncValue: the sum of objective, reduced across all processors and scaled by "scale"
86  */
87 
88  // initialize objFunValue
89  objFuncValue = 0.0;
90 
91  // initialize faceValues to zero
92  forAll(objFuncCellValues, idxI)
93  {
94  objFuncCellValues[idxI] = 0.0;
95  }
96 
97  const objectRegistry& db = mesh_.thisDb();
98 
99  scalar totalVol = 1.0;
100 
101  if (divByTotalVol_)
102  {
103  forAll(mesh_.cells(), cellI)
104  {
105  totalVol += mesh_.V()[cellI];
106  }
107  reduce(totalVol, sumOp<scalar>());
108  }
109 
110  if (varType_ == "scalar")
111  {
112  const volScalarField& var = db.lookupObject<volScalarField>(varName_);
113  // calculate mass
114  forAll(objFuncCellSources, idxI)
115  {
116  const label& cellI = objFuncCellSources[idxI];
117  scalar volume = mesh_.V()[cellI];
118  if (isSquare_)
119  {
120  objFuncCellValues[idxI] = scale_ * volume * var[cellI] * var[cellI];
121  }
122  else
123  {
124  objFuncCellValues[idxI] = scale_ * volume * var[cellI];
125  }
126  objFuncValue += objFuncCellValues[idxI];
127  }
128  }
129  else if (varType_ == "vector")
130  {
131  const volVectorField& var = db.lookupObject<volVectorField>(varName_);
132  // calculate mass
133  forAll(objFuncCellSources, idxI)
134  {
135  const label& cellI = objFuncCellSources[idxI];
136  scalar volume = mesh_.V()[cellI];
137  if (isSquare_)
138  {
139  objFuncCellValues[idxI] = scale_ * volume * var[cellI][component_] * var[cellI][component_];
140  }
141  else
142  {
143  objFuncCellValues[idxI] = scale_ * volume * var[cellI][component_];
144  }
145  objFuncValue += objFuncCellValues[idxI];
146  }
147  }
148  else
149  {
150  FatalErrorIn("") << "varType " << varType_ << " not supported!"
151  << "Options are: scalar or vector"
152  << abort(FatalError);
153  }
154 
155  // need to reduce the sum of force across all processors
156  reduce(objFuncValue, sumOp<scalar>());
157 
158  objFuncValue /= totalVol;
159 
160  return;
161 }
162 
163 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
164 
165 } // End namespace Foam
166 
167 // ************************************************************************* //
Foam::DAObjFuncVariableVolSum::divByTotalVol_
label divByTotalVol_
whether to normalize the volume sum with the total volume
Definition: DAObjFuncVariableVolSum.H:45
Foam::DAObjFunc::objFuncType_
word objFuncType_
the type of the objective function
Definition: DAObjFunc.H:66
forAll
forAll(pseudoP.boundaryField(), patchI)
Definition: solvePseudoPEqn.H:10
Foam::DAObjFuncVariableVolSum::calcObjFunc
virtual void calcObjFunc(const labelList &objFuncFaceSources, const labelList &objFuncCellSources, scalarList &objFuncFaceValues, scalarList &objFuncCellValues, scalar &objFuncValue)
calculate the value of objective function
Definition: DAObjFuncVariableVolSum.C:61
Foam::DAObjFuncVariableVolSum::varName_
word varName_
name of the variable
Definition: DAObjFuncVariableVolSum.H:33
Foam::DAOption
Definition: DAOption.H:29
Foam::DAObjFuncVariableVolSum::DAObjFuncVariableVolSum
DAObjFuncVariableVolSum(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: DAObjFuncVariableVolSum.C:19
daOption
DAOption daOption(mesh, pyOptions_)
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
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::DAObjFuncVariableVolSum::varType_
word varType_
type of the variable either vector or scalar
Definition: DAObjFuncVariableVolSum.H:36
Foam::DAResidual
Definition: DAResidual.H:35
Foam::DAObjFuncVariableVolSum::isSquare_
label isSquare_
whether to take a square of the variable
Definition: DAObjFuncVariableVolSum.H:42
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFvSource, 0)
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFvSource, DAFvSourceActuatorDisk, dictionary)
Foam::DAObjFunc::scale_
scalar scale_
scale of the objective function
Definition: DAObjFunc.H:87
daModel
DAModel daModel(mesh, daOption)
Foam::DAObjFunc::objFuncConInfo_
List< List< word > > objFuncConInfo_
the connectivity information for the objective function
Definition: DAObjFunc.H:93
daIndex
DAIndex daIndex(mesh, daOption, daModel)
Foam::DAObjFunc::objFuncDict_
const dictionary & objFuncDict_
dictionary containing the information for the objective function
Definition: DAObjFunc.H:69
Foam::DAObjFuncVariableVolSum::component_
label component_
if vector which element?
Definition: DAObjFuncVariableVolSum.H:39
DAObjFuncVariableVolSum.H
Foam::DAObjFunc
Definition: DAObjFunc.H:33