DAFunctionTotalPressureRatio.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(DAFunctionTotalPressureRatio, 0);
16 addToRunTimeSelectionTable(DAFunction, DAFunctionTotalPressureRatio, 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  functionDict_.readEntry<wordList>("inletPatches", inletPatches_);
33  functionDict_.readEntry<wordList>("outletPatches", outletPatches_);
34 
35  // get Cp from thermophysicalProperties
36  const IOdictionary& thermoDict = mesh_.thisDb().lookupObject<IOdictionary>("thermophysicalProperties");
37  dictionary mixSubDict = thermoDict.subDict("mixture");
38  dictionary thermodynamicsSubDict = mixSubDict.subDict("thermodynamics");
39  Cp_ = thermodynamicsSubDict.getScalar("Cp");
40  gamma_ = thermodynamicsSubDict.getScalar("gamma");
41 
42  if (daOption_.getOption<label>("debug"))
43  {
44  Info << "Cp " << Cp_ << endl;
45  Info << "gamma " << gamma_ << endl;
46  }
47 }
48 
51 {
52  /*
53  Description:
54  Calculate total pressure ratio, TP_outlet/TP_inlet
55  */
56 
57  // always calculate the area of all the inlet/outletPatches
58  areaSumInlet_ = 0.0;
59  areaSumOutlet_ = 0.0;
60  forAll(faceSources_, idxI)
61  {
62  const label& functionFaceI = faceSources_[idxI];
63  label bFaceI = functionFaceI - daIndex_.nLocalInternalFaces;
64  const label patchI = daIndex_.bFacePatchI[bFaceI];
65  const label faceI = daIndex_.bFaceFaceI[bFaceI];
66  word patchName = mesh_.boundaryMesh()[patchI].name();
67  if (inletPatches_.found(patchName))
68  {
69  areaSumInlet_ += mesh_.magSf().boundaryField()[patchI][faceI];
70  }
71  else if (outletPatches_.found(patchName))
72  {
73  areaSumOutlet_ += mesh_.magSf().boundaryField()[patchI][faceI];
74  }
75  else
76  {
77  FatalErrorIn(" ") << "inlet/outletPatches names are not in patches" << abort(FatalError);
78  }
79  }
80  reduce(areaSumInlet_, sumOp<scalar>());
81  reduce(areaSumOutlet_, sumOp<scalar>());
82 
83  // initialize objFunValue
84  scalar functionValue = 0.0;
85 
86  const objectRegistry& db = mesh_.thisDb();
87  const volScalarField& p = db.lookupObject<volScalarField>("p");
88  const volScalarField& T = db.lookupObject<volScalarField>("T");
89  const volVectorField& U = db.lookupObject<volVectorField>("U");
90 
91  const scalar R = Cp_ - Cp_ / gamma_;
92  const scalar expCoeff = gamma_ / (gamma_ - 1.0);
93 
94  const volScalarField::Boundary& pBf = p.boundaryField();
95  const volScalarField::Boundary& TBf = T.boundaryField();
96  const volVectorField::Boundary& UBf = U.boundaryField();
97 
98  // we first compute the averaged inlet and outlet total pressure they will
99  // be used later for normalization
100  scalar TPIn = 0.0;
101  scalar TPOut = 0.0;
102  forAll(faceSources_, idxI)
103  {
104  const label& functionFaceI = faceSources_[idxI];
105  label bFaceI = functionFaceI - daIndex_.nLocalInternalFaces;
106  const label patchI = daIndex_.bFacePatchI[bFaceI];
107  const label faceI = daIndex_.bFaceFaceI[bFaceI];
108 
109  scalar pS = pBf[patchI][faceI];
110  scalar TS = TBf[patchI][faceI];
111  scalar UMag = mag(UBf[patchI][faceI]);
112  scalar SfX = mesh_.magSf().boundaryField()[patchI][faceI];
113  scalar Ma2 = sqr(UMag / sqrt(gamma_ * R * TS));
114  scalar pT = pS * pow(1.0 + 0.5 * (gamma_ - 1.0) * Ma2, expCoeff);
115 
116  word patchName = mesh_.boundaryMesh()[patchI].name();
117  if (inletPatches_.found(patchName))
118  {
119  TPIn += pT * SfX / areaSumInlet_;
120  }
121  else if (outletPatches_.found(patchName))
122  {
123  TPOut += pT * SfX / areaSumOutlet_;
124  }
125  else
126  {
127  FatalErrorIn(" ") << "inlet/outletPatches names are not in patches" << abort(FatalError);
128  }
129  }
130  reduce(TPIn, sumOp<scalar>());
131  reduce(TPOut, sumOp<scalar>());
132 
133  functionValue = TPOut / TPIn;
134 
135  // check if we need to calculate refDiff.
136  this->calcRefVar(functionValue);
137 
138  return functionValue;
139 }
140 
141 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
142 
143 } // End namespace Foam
144 
145 // ************************************************************************* //
U
volVectorField & U
Definition: createRefsPimpleDyM.H:7
Foam::DAFunction::faceSources_
labelList faceSources_
a sorted list of all face sources for the objective function
Definition: DAFunction.H:67
Foam::DAFunctionTotalPressureRatio::Cp_
scalar Cp_
Definition: DAFunctionTotalPressureRatio.H:43
Foam::DAIndex::bFacePatchI
labelList bFacePatchI
Definition: DAIndex.H:110
Foam::DAOption
Definition: DAOption.H:29
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFunction, DAFunctionForce, dictionary)
Foam::DAFunctionTotalPressureRatio::DAFunctionTotalPressureRatio
DAFunctionTotalPressureRatio(const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const word functionName)
Definition: DAFunctionTotalPressureRatio.C:19
Foam::DAFunctionTotalPressureRatio::outletPatches_
wordList outletPatches_
names of outlet patches
Definition: DAFunctionTotalPressureRatio.H:38
Foam::DAOption::getOption
classType getOption(const word key) const
get an option from subDict and key
Definition: DAOption.H:92
Foam::DAFunctionTotalPressureRatio::calcFunction
virtual scalar calcFunction()
calculate the value of objective function
Definition: DAFunctionTotalPressureRatio.C:50
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
Foam::DAFunctionTotalPressureRatio::areaSumOutlet_
scalar areaSumOutlet_
the area of all outlet patches
Definition: DAFunctionTotalPressureRatio.H:35
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DAIndex
Definition: DAIndex.H:32
p
volScalarField & p
Definition: createRefsPimpleDyM.H:6
Foam::DAFunctionTotalPressureRatio::inletPatches_
wordList inletPatches_
names of inlet patches
Definition: DAFunctionTotalPressureRatio.H:41
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
T
volScalarField & T
Definition: createRefsHeatTransfer.H:5
forAll
forAll(nuTilda1, cellI)
Definition: nuTilda1EqnIrkPimple.H:19
Foam::DAIndex::nLocalInternalFaces
label nLocalInternalFaces
local internal face size
Definition: DAIndex.H:98
Foam::DAFunctionTotalPressureRatio::areaSumInlet_
scalar areaSumInlet_
the area of all inlet patches
Definition: DAFunctionTotalPressureRatio.H:32
Foam::DAFunctionTotalPressureRatio::gamma_
scalar gamma_
Definition: DAFunctionTotalPressureRatio.H:45
Foam::DAFunction::functionDict_
dictionary functionDict_
dictionary containing the information for the objective function
Definition: DAFunction.H:64
DAFunctionTotalPressureRatio.H
Foam::DAFunction::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAFunction.H:43
Foam::DAIndex::bFaceFaceI
labelList bFaceFaceI
Definition: DAIndex.H:116