DAFunctionMoment.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v4
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAFunctionMoment.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 defineTypeNameAndDebug(DAFunctionMoment, 0);
16 addToRunTimeSelectionTable(DAFunction, DAFunctionMoment, 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 moment, first read in some parameters from functionDict_
35  // these parameters are only for moment objective
36 
37  scalarList dir;
38  functionDict_.readEntry<scalarList>("axis", dir);
39  momentDir_[0] = dir[0];
40  momentDir_[1] = dir[1];
41  momentDir_[2] = dir[2];
42 
43  if (fabs(mag(momentDir_) - 1.0) > 1.0e-4)
44  {
45  FatalErrorIn(" ") << "the magnitude of the axis parameter in "
46  << functionName << " is not 1.0!"
47  << abort(FatalError);
48  }
49 
50  scalarList center;
51  functionDict_.readEntry<scalarList>("center", center);
52  momentCenter_[0] = center[0];
53  momentCenter_[1] = center[1];
54  momentCenter_[2] = center[2];
55 }
56 
59 {
60  /*
61  Description:
62  Calculate the moment which consist of pressure and viscous components
63  of force cross-producting the r vector wrt to a ref point
64  This code for computiong force is modified based on:
65  src/functionObjects/forcces/forces.C
66 
67  Output:
68 
69  functionValue: the sum of objective, reduced across all processsors and scaled by "scale"
70  */
71 
72  // initialize objFunValue
73  scalar functionValue = 0.0;
74 
75  const objectRegistry& db = mesh_.thisDb();
76  const volScalarField& p = db.lookupObject<volScalarField>("p");
77 
78  const surfaceVectorField::Boundary& Sfb = mesh_.Sf().boundaryField();
79 
80  tmp<volSymmTensorField> tdevRhoReff = daTurb_.devRhoReff();
81  const volSymmTensorField::Boundary& devRhoReffb = tdevRhoReff().boundaryField();
82 
83  // calculate discrete force for each functionFace
84  forAll(faceSources_, idxI)
85  {
86  const label& functionFaceI = faceSources_[idxI];
87  label bFaceI = functionFaceI - daIndex_.nLocalInternalFaces;
88  const label patchI = daIndex_.bFacePatchI[bFaceI];
89  const label faceI = daIndex_.bFaceFaceI[bFaceI];
90 
91  // normal force
92  vector fN(Sfb[patchI][faceI] * p.boundaryField()[patchI][faceI]);
93  // tangential force
94  vector fT(Sfb[patchI][faceI] & devRhoReffb[patchI][faceI]);
95  // r vector
96  vector rVec = mesh_.Cf().boundaryField()[patchI][faceI] - momentCenter_;
97  // compute moment
98  scalar val = scale_ * (rVec ^ (fN + fT)) & momentDir_;
99 
100  functionValue += val;
101  }
102 
103  // need to reduce the sum of force across all processors
104  reduce(functionValue, sumOp<scalar>());
105 
106  // check if we need to calculate refDiff.
107  this->calcRefVar(functionValue);
108 
109  return functionValue;
110 }
111 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
112 
113 } // End namespace Foam
114 
115 // ************************************************************************* //
Foam::DAFunctionMoment::calcFunction
virtual scalar calcFunction()
calculate the value of objective function
Definition: DAFunctionMoment.C:58
Foam::DAFunction::faceSources_
labelList faceSources_
a sorted list of all face sources for the objective function
Definition: DAFunction.H:67
Foam::DAFunctionMoment::momentCenter_
vector momentCenter_
the center of rotation for moment
Definition: DAFunctionMoment.H:35
Foam::DAIndex::bFacePatchI
labelList bFacePatchI
Definition: DAIndex.H:110
Foam::DAOption
Definition: DAOption.H:29
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFunction, DAFunctionForce, dictionary)
Foam::DAFunction::calcRefVar
void calcRefVar(scalar &functionValue)
calculate (var-ref)^2
Definition: DAFunction.C:216
Foam::DAFunction::daIndex_
const DAIndex & daIndex_
DAIndex object.
Definition: DAFunction.H:52
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DAIndex
Definition: DAIndex.H:32
p
volScalarField & p
Definition: createRefsPimpleDyM.H:6
Foam::DAModel
Definition: DAModel.H:57
Foam::DAFunction
Definition: DAFunction.H:31
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFunction, 0)
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::DAFunctionMoment::momentDir_
vector momentDir_
the direction of the moment
Definition: DAFunctionMoment.H:32
DAFunctionMoment.H
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::DAFunction::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAFunction.H:43
Foam::DAIndex::bFaceFaceI
labelList bFaceFaceI
Definition: DAIndex.H:116
Foam::DAFunctionMoment::daTurb_
const DATurbulenceModel & daTurb_
DATurbulenceModel object.
Definition: DAFunctionMoment.H:38
Foam::DAFunctionMoment::DAFunctionMoment
DAFunctionMoment(const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const word functionName)
Definition: DAFunctionMoment.C:19