DAFunctionForce.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v4
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAFunctionForce.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 defineTypeNameAndDebug(DAFunctionForce, 0);
16 addToRunTimeSelectionTable(DAFunction, DAFunctionForce, 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  daTurb_(daModel.getDATurbulenceModel())
32 {
33 
34  // for computing force, first read in some parameters from functionDict_
35  // these parameters are only for force objective
36 
37  // we support three direction modes
38  dirMode_ = functionDict_.getWord("directionMode");
39  if (dirMode_ == "fixedDirection")
40  {
41  scalarList dir;
42  functionDict_.readEntry<scalarList>("direction", dir);
43  forceDir_[0] = dir[0];
44  forceDir_[1] = dir[1];
45  forceDir_[2] = dir[2];
46  }
47  else if (dirMode_ == "parallelToFlow" || dirMode_ == "normalToFlow")
48  {
49  // initial value for forceDir_. it will be dynamically adjusted later
50  forceDir_ = {1.0, 0.0, 0.0};
51  word patchVelocityInputName = functionDict_.getWord("patchVelocityInputName");
52  dictionary patchVSubDict = daOption_.getAllOptions().subDict("inputInfo").subDict(patchVelocityInputName);
53  HashTable<label> axisIndices;
54  axisIndices.set("x", 0);
55  axisIndices.set("y", 1);
56  axisIndices.set("z", 2);
57  word flowAxis = patchVSubDict.getWord("flowAxis");
58  word normalAxis = patchVSubDict.getWord("normalAxis");
59  flowAxisIndex_ = axisIndices[flowAxis];
60  normalAxisIndex_ = axisIndices[normalAxis];
61  }
62  else
63  {
64  FatalErrorIn(" ") << "directionMode for "
65  << functionName << " not valid!"
66  << "Options: fixedDirection, parallelToFlow, normalToFlow."
67  << abort(FatalError);
68  }
69 
70  if (fabs(mag(forceDir_) - 1.0) > 1.0e-8)
71  {
72  FatalErrorIn(" ") << "the magnitude of the direction parameter in "
73  << functionName << " is not 1.0!"
74  << abort(FatalError);
75  }
76 }
77 
80 {
81  /*
82  Description:
83  Calculate the force which consist of pressure and viscous components.
84  This code is modified based on:
85  src/functionObjects/forcces/forces.C
86 
87  Output:
88  functionValue: the sum of objective, reduced across all processsors and scaled by "scale"
89  */
90 
91  // dynamically update the force direction, if either parallelToFlow or normalToFlow is active.
92  if (dirMode_ != "fixedDirection")
93  {
94  // we need to read the velocity magnitude and aoa
95  // from DAGlobalVar::patchVelocity. NOTE: DAGlobalVar::patchVelocity is already set
96  // by DAInputPatchVelocity
97  DAGlobalVar& globalVar =
98  const_cast<DAGlobalVar&>(mesh_.thisDb().lookupObject<DAGlobalVar>("DAGlobalVar"));
99  scalar aoaDeg = globalVar.patchVelocity[1];
100  scalar aoaRad = aoaDeg * constant::mathematical::pi / 180.0;
101 
102  scalar compA = cos(aoaRad);
103  scalar compB = sin(aoaRad);
104  if (dirMode_ == "parallelToFlow")
105  {
106  forceDir_[flowAxisIndex_] = compA;
107  forceDir_[normalAxisIndex_] = compB;
108  }
109  else if (dirMode_ == "normalToFlow")
110  {
111  forceDir_[flowAxisIndex_] = -compB;
112  forceDir_[normalAxisIndex_] = compA;
113  }
114  }
115 
116  // initialize objFunValue
117  scalar functionValue = 0.0;
118 
119  const objectRegistry& db = mesh_.thisDb();
120  const volScalarField& p = db.lookupObject<volScalarField>("p");
121 
122  const surfaceVectorField::Boundary& Sfb = mesh_.Sf().boundaryField();
123 
124  tmp<volSymmTensorField> tdevRhoReff = daTurb_.devRhoReff();
125  const volSymmTensorField::Boundary& devRhoReffb = tdevRhoReff().boundaryField();
126 
127  // calculate discrete force for each functionFace
128  forAll(faceSources_, idxI)
129  {
130  const label& functionFaceI = faceSources_[idxI];
131  label bFaceI = functionFaceI - daIndex_.nLocalInternalFaces;
132  const label patchI = daIndex_.bFacePatchI[bFaceI];
133  const label faceI = daIndex_.bFaceFaceI[bFaceI];
134 
135  // normal force
136  vector fN(Sfb[patchI][faceI] * p.boundaryField()[patchI][faceI]);
137  // tangential force
138  vector fT(Sfb[patchI][faceI] & devRhoReffb[patchI][faceI]);
139  // project the force to forceDir
140  scalar faceValue = scale_ * ((fN + fT) & forceDir_);
141 
142  functionValue += faceValue;
143  }
144 
145  // need to reduce the sum of force across all processors
146  reduce(functionValue, sumOp<scalar>());
147 
148  // check if we need to calculate refDiff.
149  this->calcRefVar(functionValue);
150 
151  return functionValue;
152 }
153 
154 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
155 
156 } // End namespace Foam
157 
158 // ************************************************************************* //
Foam::DAFunctionForce::flowAxisIndex_
label flowAxisIndex_
flowAxisIndex_ for the alpha design variable with tan(U_normal/U_flow)
Definition: DAFunctionForce.H:40
Foam::DAFunction::faceSources_
labelList faceSources_
a sorted list of all face sources for the objective function
Definition: DAFunction.H:67
Foam::DAFunctionForce::dirMode_
word dirMode_
if dynamically adjusting the angle what mode to use
Definition: DAFunctionForce.H:37
Foam::DAIndex::bFacePatchI
labelList bFacePatchI
Definition: DAIndex.H:110
Foam::DAOption
Definition: DAOption.H:29
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFunction, DAFunctionForce, dictionary)
Foam::DAFunctionForce::DAFunctionForce
DAFunctionForce(const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const word functionName)
Definition: DAFunctionForce.C:19
Foam::DAGlobalVar::patchVelocity
scalarList patchVelocity
patch velocity list [UMag, AOA] AoA is in degrees
Definition: DAGlobalVar.H:59
Foam::DAFunction::calcRefVar
void calcRefVar(scalar &functionValue)
calculate (var-ref)^2
Definition: DAFunction.C:216
Foam::DAFunctionForce::calcFunction
virtual scalar calcFunction()
calculate the value of objective function
Definition: DAFunctionForce.C:79
Foam::DAFunction::daIndex_
const DAIndex & daIndex_
DAIndex object.
Definition: DAFunction.H:52
Foam::DAOption::getAllOptions
const dictionary & getAllOptions() const
return a reference of allOptions_ dictionary
Definition: DAOption.H:56
Foam::DAFunctionForce::normalAxisIndex_
label normalAxisIndex_
normalAxisIndex_ for the alpha design variable with tan(U_normal/U_flow)
Definition: DAFunctionForce.H:43
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DAIndex
Definition: DAIndex.H:32
p
volScalarField & p
Definition: createRefsPimpleDyM.H:6
DAFunctionForce.H
Foam::DAModel
Definition: DAModel.H:57
Foam::DAFunction
Definition: DAFunction.H:31
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFunction, 0)
Foam::DAFunction::daOption_
const DAOption & daOption_
DAOption object.
Definition: DAFunction.H:46
Foam
Definition: checkGeometry.C:32
Foam::DATurbulenceModel::devRhoReff
tmp< volSymmTensorField > devRhoReff() const
dev terms
Definition: DATurbulenceModel.C:371
forAll
forAll(nuTilda1, cellI)
Definition: nuTilda1EqnIrkPimple.H:19
Foam::DAIndex::nLocalInternalFaces
label nLocalInternalFaces
local internal face size
Definition: DAIndex.H:98
Foam::DAFunction::functionDict_
dictionary functionDict_
dictionary containing the information for the objective function
Definition: DAFunction.H:64
Foam::DAFunction::scale_
scalar scale_
scale of the objective function
Definition: DAFunction.H:73
Foam::DAGlobalVar
Definition: DAGlobalVar.H:26
Foam::DAFunctionForce::daTurb_
const DATurbulenceModel & daTurb_
DATurbulenceModel object.
Definition: DAFunctionForce.H:46
Foam::DAFunction::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAFunction.H:43
Foam::DAIndex::bFaceFaceI
labelList bFaceFaceI
Definition: DAIndex.H:116
Foam::DAFunctionForce::forceDir_
vector forceDir_
the direction of the force
Definition: DAFunctionForce.H:34