DAFunctionVariableVolSum.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v4
5 
6 \*---------------------------------------------------------------------------*/
7 
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 defineTypeNameAndDebug(DAFunctionVariableVolSum, 0);
16 addToRunTimeSelectionTable(DAFunction, DAFunctionVariableVolSum, dictionary);
17 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
18 
20  const fvMesh& mesh,
21  const DAOption& daOption,
22  const DAModel& daModel,
23  const DAIndex& daIndex,
24  const word functionName)
25  : DAFunction(
26  mesh,
27  daOption,
28  daModel,
29  daIndex,
30  functionName)
31 {
32 
33  functionDict_.readEntry<word>("varName", varName_);
34 
35  functionDict_.readEntry<word>("varType", varType_);
36 
37  functionDict_.readEntry<label>("index", index_);
38 
39  isSquare_ = functionDict_.lookupOrDefault<label>("isSquare", 0);
40 
41  multiplyVol_ = functionDict_.lookupOrDefault<label>("multiplyVol", 1);
42 
43  divByTotalVol_ = functionDict_.lookupOrDefault<label>("divByTotalVol", 0);
44 }
45 
48 {
49  /*
50  Description:
51  Calculate the obj = mesh-volume * variable (whether to take a square of the variable
52  depends on isSquare)
53  */
54 
55  // initialize objFunValue
56  scalar functionValue = 0.0;
57 
58  const objectRegistry& db = mesh_.thisDb();
59 
60  scalar totalVol = 1.0;
61 
62  if (divByTotalVol_)
63  {
64  forAll(mesh_.cells(), cellI)
65  {
66  totalVol += mesh_.V()[cellI];
67  }
68  reduce(totalVol, sumOp<scalar>());
69  }
70 
71  if (varType_ == "scalar")
72  {
73  const volScalarField& var = db.lookupObject<volScalarField>(varName_);
74  // calculate mass
75  forAll(cellSources_, idxI)
76  {
77  const label& cellI = cellSources_[idxI];
78  scalar volume = 1.0;
79  if (multiplyVol_)
80  {
81  volume = mesh_.V()[cellI];
82  }
83  if (isSquare_)
84  {
85  functionValue += scale_ * volume * var[cellI] * var[cellI];
86  }
87  else
88  {
89  functionValue += scale_ * volume * var[cellI];
90  }
91  }
92  }
93  else if (varType_ == "vector")
94  {
95  const volVectorField& var = db.lookupObject<volVectorField>(varName_);
96  // calculate mass
97  forAll(cellSources_, idxI)
98  {
99  const label& cellI = cellSources_[idxI];
100  scalar volume = 1.0;
101  if (multiplyVol_)
102  {
103  volume = mesh_.V()[cellI];
104  }
105  if (isSquare_)
106  {
107  functionValue += scale_ * volume * var[cellI][index_] * var[cellI][index_];
108  }
109  else
110  {
111  functionValue += scale_ * volume * var[cellI][index_];
112  }
113  }
114  }
115  else
116  {
117  FatalErrorIn("") << "varType " << varType_ << " not supported!"
118  << "Options are: scalar or vector"
119  << abort(FatalError);
120  }
121 
122  // need to reduce the sum of force across all processors
123  reduce(functionValue, sumOp<scalar>());
124 
125  functionValue /= totalVol;
126 
127  // check if we need to calculate refDiff.
128  this->calcRefVar(functionValue);
129 
130  return functionValue;
131 }
132 
133 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
134 
135 } // End namespace Foam
136 
137 // ************************************************************************* //
Foam::DAFunctionVariableVolSum::varType_
word varType_
type of the variable either vector or scalar
Definition: DAFunctionVariableVolSum.H:35
Foam::DAFunctionVariableVolSum::varName_
word varName_
name of the variable
Definition: DAFunctionVariableVolSum.H:32
Foam::DAFunctionVariableVolSum::multiplyVol_
label multiplyVol_
whether to multiply the variable by the volume
Definition: DAFunctionVariableVolSum.H:47
Foam::DAFunctionVariableVolSum::isSquare_
label isSquare_
whether to take a square of the variable
Definition: DAFunctionVariableVolSum.H:41
Foam::DAFunctionVariableVolSum::calcFunction
virtual scalar calcFunction()
calculate the value of objective function
Definition: DAFunctionVariableVolSum.C:47
Foam::DAOption
Definition: DAOption.H:29
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFunction, DAFunctionForce, dictionary)
Foam::DAFunctionVariableVolSum::DAFunctionVariableVolSum
DAFunctionVariableVolSum(const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const word functionName)
Definition: DAFunctionVariableVolSum.C:19
Foam::DAFunction::calcRefVar
void calcRefVar(scalar &functionValue)
calculate (var-ref)^2
Definition: DAFunction.C:216
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DAFunctionVariableVolSum::divByTotalVol_
label divByTotalVol_
whether to normalize the volume sum with the total volume
Definition: DAFunctionVariableVolSum.H:44
Foam::DAIndex
Definition: DAIndex.H:32
Foam::DAModel
Definition: DAModel.H:57
Foam::DAFunction
Definition: DAFunction.H:31
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFunction, 0)
Foam
Definition: checkGeometry.C:32
DAFunctionVariableVolSum.H
forAll
forAll(nuTilda1, cellI)
Definition: nuTilda1EqnIrkPimple.H:19
Foam::DAFunctionVariableVolSum::index_
label index_
if vector which element/index?
Definition: DAFunctionVariableVolSum.H:38
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::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAFunction.H:43