DAObjFuncTotalPressureRatio.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(DAObjFuncTotalPressureRatio, 0);
16 addToRunTimeSelectionTable(DAObjFunc, DAObjFuncTotalPressureRatio, 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  // Assign type, this is common for all objectives
39  objFuncDict_.readEntry<word>("type", objFuncType_);
40 
41  // setup the connectivity for total pressure, this is needed in Foam::DAJacCondFdW
42  word pName = "p";
43  if (mesh_.thisDb().foundObject<volScalarField>("p_rgh"))
44  {
45  pName = "p_rgh";
46  }
47  // for pressureInlet velocity, U depends on phi
48  objFuncConInfo_ = {{"U", "T", pName, "phi"}};
49 
50  objFuncDict_.readEntry<scalar>("scale", scale_);
51 
52  objFuncDict_.readEntry<wordList>("inletPatches", inletPatches_);
53  objFuncDict_.readEntry<wordList>("outletPatches", outletPatches_);
54 
55  // get Cp from thermophysicalProperties
56  const IOdictionary& thermoDict = mesh_.thisDb().lookupObject<IOdictionary>("thermophysicalProperties");
57  dictionary mixSubDict = thermoDict.subDict("mixture");
58  dictionary thermodynamicsSubDict = mixSubDict.subDict("thermodynamics");
59  Cp_ = thermodynamicsSubDict.getScalar("Cp");
60  gamma_ = thermodynamicsSubDict.getScalar("gamma");
61 
62  if (daOption_.getOption<label>("debug"))
63  {
64  Info << "Cp " << Cp_ << endl;
65  Info << "gamma " << gamma_ << endl;
66  }
67 }
68 
71  const labelList& objFuncFaceSources,
72  const labelList& objFuncCellSources,
73  scalarList& objFuncFaceValues,
74  scalarList& objFuncCellValues,
75  scalar& objFuncValue)
76 {
77  /*
78  Description:
79  Calculate total pressure ratio, TP_outlet/TP_inlet
80  NOTE: to enable coloring, we need to separate TPIn and TPOut for each face, so we do:
81  d(TPInAvg/TPOutAvg)/dw = -TPOutAvg/TPInAvg^2 * dTPInAvg/dw + 1/TPInAvg * dTPOutAvg/dw
82  so here objDiscreteVal = -TPOutAvg/TPInAvg^2 * dTPIn_i*ds_i/dSIn for inlet patches
83  and objDiscreteVal = 1/TPInAvg * TPOut_i*ds_i/dSOut for outlet patches
84 
85  Input:
86  objFuncFaceSources: List of face source (index) for this objective
87 
88  objFuncCellSources: List of cell source (index) for this objective
89 
90  Output:
91  objFuncFaceValues: the discrete value of objective for each face source (index).
92  This will be used for computing df/dw in the adjoint.
93 
94  objFuncCellValues: the discrete value of objective on each cell source (index).
95  This will be used for computing df/dw in the adjoint.
96 
97  objFuncValue: the sum of objective, reduced across all processors and scaled by "scale"
98  */
99 
100  // calculate the area of all the inlet/outletPatches
101  if (areaSumInlet_ < 0.0 || areaSumOutlet_ < 0.0)
102  {
103  areaSumInlet_ = 0.0;
104  areaSumOutlet_ = 0.0;
105  forAll(objFuncFaceSources, idxI)
106  {
107  const label& objFuncFaceI = objFuncFaceSources[idxI];
108  label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces;
109  const label patchI = daIndex_.bFacePatchI[bFaceI];
110  const label faceI = daIndex_.bFaceFaceI[bFaceI];
111  word patchName = mesh_.boundaryMesh()[patchI].name();
112  if (DAUtility::isInList<word>(patchName, inletPatches_))
113  {
114  areaSumInlet_ += mesh_.magSf().boundaryField()[patchI][faceI];
115  }
116  else if (DAUtility::isInList<word>(patchName, outletPatches_))
117  {
118  areaSumOutlet_ += mesh_.magSf().boundaryField()[patchI][faceI];
119  }
120  else
121  {
122  FatalErrorIn(" ") << "inlet/outletPatches names are not in patches" << abort(FatalError);
123  }
124  }
125  reduce(areaSumInlet_, sumOp<scalar>());
126  reduce(areaSumOutlet_, sumOp<scalar>());
127  }
128 
129  // initialize faceValues to zero
130  forAll(objFuncFaceValues, idxI)
131  {
132  objFuncFaceValues[idxI] = 0.0;
133  }
134  // initialize objFunValue
135  objFuncValue = 0.0;
136 
137  const objectRegistry& db = mesh_.thisDb();
138  const volScalarField& p = db.lookupObject<volScalarField>("p");
139  const volScalarField& T = db.lookupObject<volScalarField>("T");
140  const volVectorField& U = db.lookupObject<volVectorField>("U");
141 
142  const scalar R = Cp_ - Cp_ / gamma_;
143  const scalar expCoeff = gamma_ / (gamma_ - 1.0);
144 
145  const volScalarField::Boundary& pBf = p.boundaryField();
146  const volScalarField::Boundary& TBf = T.boundaryField();
147  const volVectorField::Boundary& UBf = U.boundaryField();
148 
149  // we first compute the averaged inlet and outlet total pressure they will
150  // be used later for normalization
151  scalar TPIn = 0.0;
152  scalar TPOut = 0.0;
153  forAll(objFuncFaceSources, idxI)
154  {
155  const label& objFuncFaceI = objFuncFaceSources[idxI];
156  label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces;
157  const label patchI = daIndex_.bFacePatchI[bFaceI];
158  const label faceI = daIndex_.bFaceFaceI[bFaceI];
159 
160  scalar pS = pBf[patchI][faceI];
161  scalar TS = TBf[patchI][faceI];
162  scalar UMag = mag(UBf[patchI][faceI]);
163  scalar SfX = mesh_.magSf().boundaryField()[patchI][faceI];
164  scalar Ma2 = sqr(UMag / sqrt(gamma_ * R * TS));
165  scalar pT = pS * pow(1.0 + 0.5 * (gamma_ - 1.0) * Ma2, expCoeff);
166 
167  word patchName = mesh_.boundaryMesh()[patchI].name();
168  if (DAUtility::isInList<word>(patchName, inletPatches_))
169  {
170  TPIn += pT * SfX / areaSumInlet_;
171  }
172  else if (DAUtility::isInList<word>(patchName, outletPatches_))
173  {
174  TPOut += pT * SfX / areaSumOutlet_;
175  }
176  else
177  {
178  FatalErrorIn(" ") << "inlet/outletPatches names are not in patches" << abort(FatalError);
179  }
180  }
181  reduce(TPIn, sumOp<scalar>());
182  reduce(TPOut, sumOp<scalar>());
183 
184  // set Reference values
185  if (calcRefCoeffs)
186  {
187  TPInRef_ = TPIn;
188  TPOutRef_ = TPOut;
189  }
190 
191  objFuncValue = TPOut / TPIn;
192 
193  // pTotal ratio: Note we need special treatment to enable coloring
194  forAll(objFuncFaceSources, idxI)
195  {
196  const label& objFuncFaceI = objFuncFaceSources[idxI];
197  label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces;
198  const label patchI = daIndex_.bFacePatchI[bFaceI];
199  const label faceI = daIndex_.bFaceFaceI[bFaceI];
200 
201  scalar pS = pBf[patchI][faceI];
202  scalar TS = TBf[patchI][faceI];
203  scalar UMag = mag(UBf[patchI][faceI]);
204  scalar SfX = mesh_.magSf().boundaryField()[patchI][faceI];
205  scalar Ma2 = sqr(UMag / sqrt(gamma_ * R * TS));
206  scalar pT = pS * pow(1.0 + 0.5 * (gamma_ - 1.0) * Ma2, expCoeff);
207 
208  word patchName = mesh_.boundaryMesh()[patchI].name();
209  if (DAUtility::isInList<word>(patchName, inletPatches_))
210  {
211  objFuncFaceValues[idxI] = -pT * SfX / areaSumInlet_ * TPOutRef_ / TPInRef_ / TPInRef_;
212  }
213  else if (DAUtility::isInList<word>(patchName, outletPatches_))
214  {
215  objFuncFaceValues[idxI] = pT * SfX / areaSumOutlet_ / TPInRef_;
216  }
217  else
218  {
219  FatalErrorIn(" ") << "inlet/outletPatches names are not in patches" << abort(FatalError);
220  }
221  }
222 
223  return;
224 }
225 
226 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
227 
228 } // End namespace Foam
229 
230 // ************************************************************************* //
Foam::DAObjFunc::objFuncType_
word objFuncType_
the type of the objective function
Definition: DAObjFunc.H:66
U
U
Definition: pEqnPimpleDyM.H:60
forAll
forAll(pseudoP.boundaryField(), patchI)
Definition: solvePseudoPEqn.H:10
Foam::DAObjFuncTotalPressureRatio::calcObjFunc
virtual void calcObjFunc(const labelList &objFuncFaceSources, const labelList &objFuncCellSources, scalarList &objFuncFaceValues, scalarList &objFuncCellValues, scalar &objFuncValue)
calculate the value of objective function
Definition: DAObjFuncTotalPressureRatio.C:70
Foam::DAObjFuncTotalPressureRatio::TPOutRef_
scalar TPOutRef_
Definition: DAObjFuncTotalPressureRatio.H:46
Foam::DAObjFuncTotalPressureRatio::areaSumOutlet_
scalar areaSumOutlet_
the area of all outlet patches
Definition: DAObjFuncTotalPressureRatio.H:36
Foam::DAIndex::bFacePatchI
labelList bFacePatchI
Definition: DAIndex.H:110
Foam::DAOption
Definition: DAOption.H:29
Foam::DAObjFunc::daIndex_
const DAIndex & daIndex_
DAIndex object.
Definition: DAObjFunc.H:54
daOption
DAOption daOption(mesh, pyOptions_)
Foam::DAObjFuncTotalPressureRatio::TPInRef_
scalar TPInRef_
Definition: DAObjFuncTotalPressureRatio.H:44
Foam::DAOption::getOption
classType getOption(const word key) const
get an option from subDict and key
Definition: DAOption.H:92
p
volScalarField & p
Definition: createRefsPimple.H:6
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DAObjFuncTotalPressureRatio::areaSumInlet_
scalar areaSumInlet_
the area of all inlet patches
Definition: DAObjFuncTotalPressureRatio.H:33
Foam::DAObjFunc::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAObjFunc.H:45
Foam::DAIndex
Definition: DAIndex.H:32
Foam::DAObjFunc::calcRefCoeffs
label calcRefCoeffs
whether to compute reference coefficients for special objFunc treatment such as totalPressureRatio
Definition: DAObjFunc.H:238
Foam::DAModel
Definition: DAModel.H:59
Foam
Definition: multiFreqScalarFvPatchField.C:144
T
volScalarField & T
Definition: createRefsHeatTransfer.H:5
Foam::DAObjFuncTotalPressureRatio::DAObjFuncTotalPressureRatio
DAObjFuncTotalPressureRatio(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: DAObjFuncTotalPressureRatio.C:19
Foam::DAObjFunc::daOption_
const DAOption & daOption_
DAOption object.
Definition: DAObjFunc.H:48
Foam::DAIndex::nLocalInternalFaces
label nLocalInternalFaces
local internal face size
Definition: DAIndex.H:98
Foam::DAResidual
Definition: DAResidual.H:35
Foam::DAObjFuncTotalPressureRatio::Cp_
scalar Cp_
Definition: DAObjFuncTotalPressureRatio.H:48
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::DAObjFuncTotalPressureRatio::outletPatches_
wordList outletPatches_
names of outlet patches
Definition: DAObjFuncTotalPressureRatio.H:39
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::DAObjFuncTotalPressureRatio::inletPatches_
wordList inletPatches_
names of inlet patches
Definition: DAObjFuncTotalPressureRatio.H:42
Foam::DAObjFunc::objFuncDict_
const dictionary & objFuncDict_
dictionary containing the information for the objective function
Definition: DAObjFunc.H:69
DAObjFuncTotalPressureRatio.H
Foam::DAIndex::bFaceFaceI
labelList bFaceFaceI
Definition: DAIndex.H:116
Foam::DAObjFuncTotalPressureRatio::gamma_
scalar gamma_
Definition: DAObjFuncTotalPressureRatio.H:50
Foam::DAObjFunc
Definition: DAObjFunc.H:33