DAObjFuncWallHeatFlux.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(DAObjFuncWallHeatFlux, 0);
16 addToRunTimeSelectionTable(DAObjFunc, DAObjFuncWallHeatFlux, 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 #ifdef CompressibleFlow
38  thermo_(const_cast<fluidThermo&>(daModel.getThermo())),
39  daTurb_(const_cast<DATurbulenceModel&>(daModel.getDATurbulenceModel())),
40  wallHeatFlux_(
41  IOobject(
42  "wallHeatFlux",
43  mesh.time().timeName(),
44  mesh,
45  IOobject::NO_READ,
46  IOobject::AUTO_WRITE),
47  mesh,
48  dimensionedScalar("wallHeatFlux", dimensionSet(1, 0, -3, 0, 0, 0, 0), 0.0),
49  "calculated")
50 #endif
51 #ifdef IncompressibleFlow
52  daTurb_(const_cast<DATurbulenceModel&>(daModel.getDATurbulenceModel())),
53  wallHeatFlux_(
54  IOobject(
55  "wallHeatFlux",
56  mesh.time().timeName(),
57  mesh,
58  IOobject::NO_READ,
59  IOobject::AUTO_WRITE),
60  mesh,
61  dimensionedScalar("wallHeatFlux", dimensionSet(1, -2, 1, 1, 0, 0, 0), 0.0),
62  "calculated")
63 #endif
64 #ifdef SolidDASolver
65  wallHeatFlux_(
66  IOobject(
67  "wallHeatFlux",
68  mesh.time().timeName(),
69  mesh,
70  IOobject::NO_READ,
71  IOobject::AUTO_WRITE),
72  mesh,
73  dimensionedScalar("wallHeatFlux", dimensionSet(1, -2, 1, 1, 0, 0, 0), 0.0),
74  "calculated")
75 #endif
76 {
77  // Assign type, this is common for all objectives
78  objFuncDict_.readEntry<word>("type", objFuncType_);
79 
80  objFuncDict_.readEntry<scalar>("scale", scale_);
81 
82 #ifdef CompressibleFlow
83 
84  // setup the connectivity for heat flux, this is needed in Foam::DAJacCondFdW
85  objFuncConInfo_ = {
86  {"nut", "T"}, // level 0
87  {"T"}}; // level 1
88 
89  // now replace nut with the corrected name for the selected turbulence model
90  daModel.correctModelStates(objFuncConInfo_[0]);
91 
92 #endif
93 
94 #ifdef IncompressibleFlow
95 
96  // setup the connectivity for heat flux, this is needed in Foam::DAJacCondFdW
97  objFuncConInfo_ = {
98  {"nut", "T"}, // level 0
99  {"T"}}; // level 1
100 
101  // now replace nut with the corrected name for the selected turbulence model
102  daModel.correctModelStates(objFuncConInfo_[0]);
103 
104  // initialize the Prandtl number from transportProperties
105  IOdictionary transportProperties(
106  IOobject(
107  "transportProperties",
108  mesh.time().constant(),
109  mesh,
110  IOobject::MUST_READ,
111  IOobject::NO_WRITE,
112  false));
113  // for incompressible flow, we need to read Cp from transportProperties
114  if (Cp_ < 0)
115  {
116  Cp_ = readScalar(transportProperties.lookup("Cp"));
117  }
118 #endif
119 
120 #ifdef SolidDASolver
121  IOdictionary transportProperties(
122  IOobject(
123  "transportProperties",
124  mesh.time().constant(),
125  mesh,
126  IOobject::MUST_READ,
127  IOobject::NO_WRITE,
128  false));
129  // for solid, we need to read k from transportProperties
130  if (k_ < 0)
131  {
132  k_ = readScalar(transportProperties.lookup("k"));
133  }
134 #endif
135 }
136 
139  const labelList& objFuncFaceSources,
140  const labelList& objFuncCellSources,
141  scalarList& objFuncFaceValues,
142  scalarList& objFuncCellValues,
143  scalar& objFuncValue)
144 {
145  /*
146  Description:
147  Calculate the heat flux F=k*dT/dz from the first cell. Modified based on
148  OpenFOAM/OpenFOAM-v1812/src/functionObjects/field/wallWallHeatFlux/wallWallHeatFlux.C
149 
150  Input:
151  objFuncFaceSources: List of face source (index) for this objective
152 
153  objFuncCellSources: List of cell source (index) for this objective
154 
155  Output:
156  objFuncFaceValues: the discrete value of objective for each face source (index).
157  This will be used for computing df/dw in the adjoint.
158 
159  objFuncCellValues: the discrete value of objective on each cell source (index).
160  This will be used for computing df/dw in the adjoint.
161 
162  objFuncValue: the sum of objective, reduced across all processors and scaled by "scale"
163  */
164 
165  // calculate the area of all the heat flux patches
166  if (areaSum_ < 0.0)
167  {
168  areaSum_ = 0.0;
169  forAll(objFuncFaceSources, idxI)
170  {
171  const label& objFuncFaceI = objFuncFaceSources[idxI];
172  label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces;
173  const label patchI = daIndex_.bFacePatchI[bFaceI];
174  const label faceI = daIndex_.bFaceFaceI[bFaceI];
175  areaSum_ += mesh_.magSf().boundaryField()[patchI][faceI];
176  }
177  reduce(areaSum_, sumOp<scalar>());
178  }
179 
180  // initialize faceValues to zero
181  forAll(objFuncFaceValues, idxI)
182  {
183  objFuncFaceValues[idxI] = 0.0;
184  }
185  // initialize objFunValue
186  objFuncValue = 0.0;
187 
188  volScalarField::Boundary& wallHeatFluxBf = wallHeatFlux_.boundaryFieldRef();
189 
190 #ifdef IncompressibleFlow
191  // incompressible flow does not have he, so we do H = Cp * alphaEff * dT/dz
192  const objectRegistry& db = mesh_.thisDb();
193  const volScalarField& T = db.lookupObject<volScalarField>("T");
194  volScalarField alphaEff = daTurb_.alphaEff();
195  const volScalarField::Boundary& TBf = T.boundaryField();
196  const volScalarField::Boundary& alphaEffBf = alphaEff.boundaryField();
197  forAll(wallHeatFluxBf, patchI)
198  {
199  if (!wallHeatFluxBf[patchI].coupled())
200  {
201  wallHeatFluxBf[patchI] = Cp_ * alphaEffBf[patchI] * TBf[patchI].snGrad();
202  }
203  }
204 #endif
205 
206 #ifdef CompressibleFlow
207  // compressible flow, H = alphaEff * dHE/dz
208  volScalarField& he = thermo_.he();
209  const volScalarField::Boundary& heBf = he.boundaryField();
210  volScalarField alphaEff = daTurb_.alphaEff();
211  const volScalarField::Boundary& alphaEffBf = alphaEff.boundaryField();
212  forAll(wallHeatFluxBf, patchI)
213  {
214  if (!wallHeatFluxBf[patchI].coupled())
215  {
216  wallHeatFluxBf[patchI] = alphaEffBf[patchI] * heBf[patchI].snGrad();
217  }
218  }
219 #endif
220 
221 #ifdef SolidDASolver
222  // solid. H = k * dT/dz, where k = DT / rho / Cp
223  const objectRegistry& db = mesh_.thisDb();
224  const volScalarField& T = db.lookupObject<volScalarField>("T");
225  const volScalarField::Boundary& TBf = T.boundaryField();
226  forAll(wallHeatFluxBf, patchI)
227  {
228  if (!wallHeatFluxBf[patchI].coupled())
229  {
230  wallHeatFluxBf[patchI] = k_ * TBf[patchI].snGrad();
231  }
232  }
233 #endif
234 
235  // calculate area weighted heat flux
236  forAll(objFuncFaceSources, idxI)
237  {
238  const label& objFuncFaceI = objFuncFaceSources[idxI];
239  label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces;
240  const label patchI = daIndex_.bFacePatchI[bFaceI];
241  const label faceI = daIndex_.bFaceFaceI[bFaceI];
242 
243  scalar area = mesh_.magSf().boundaryField()[patchI][faceI];
244  objFuncFaceValues[idxI] = scale_ * wallHeatFluxBf[patchI][faceI] * area / areaSum_;
245 
246  objFuncValue += objFuncFaceValues[idxI];
247  }
248 
249  // need to reduce the sum of force across all processors
250  reduce(objFuncValue, sumOp<scalar>());
251 
252  return;
253 }
254 
255 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
256 
257 } // End namespace Foam
258 
259 // ************************************************************************* //
Foam::DAObjFuncWallHeatFlux::Cp_
scalar Cp_
Cp used in incompressible heatFlux calculation.
Definition: DAObjFuncWallHeatFlux.H:37
Foam::DAObjFunc::objFuncType_
word objFuncType_
the type of the objective function
Definition: DAObjFunc.H:66
Foam::DAObjFuncWallHeatFlux::DAObjFuncWallHeatFlux
DAObjFuncWallHeatFlux(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: DAObjFuncWallHeatFlux.C:19
Foam::DAObjFuncWallHeatFlux::calcObjFunc
virtual void calcObjFunc(const labelList &objFuncFaceSources, const labelList &objFuncCellSources, scalarList &objFuncFaceValues, scalarList &objFuncCellValues, scalar &objFuncValue)
calculate the value of objective function
Definition: DAObjFuncWallHeatFlux.C:138
forAll
forAll(pseudoP.boundaryField(), patchI)
Definition: solvePseudoPEqn.H:10
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_)
DAObjFuncWallHeatFlux.H
Foam::DAObjFuncWallHeatFlux::wallHeatFlux_
volScalarField wallHeatFlux_
wall heat flux field
Definition: DAObjFuncWallHeatFlux.H:48
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DAObjFunc::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAObjFunc.H:45
Foam::DAIndex
Definition: DAIndex.H:32
Foam::DAModel
Definition: DAModel.H:59
Foam
Definition: multiFreqScalarFvPatchField.C:144
T
volScalarField & T
Definition: createRefsHeatTransfer.H:5
alphaEff
volScalarField alphaEff("alphaEff", turbulencePtr_->nu()/Pr+alphat)
Foam::DAIndex::nLocalInternalFaces
label nLocalInternalFaces
local internal face size
Definition: DAIndex.H:98
Foam::DAResidual
Definition: DAResidual.H:35
Foam::DATurbulenceModel
Definition: DATurbulenceModel.H:61
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFvSource, 0)
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFvSource, DAFvSourceActuatorDisk, dictionary)
he
volScalarField & he
Definition: EEqnRhoSimple.H:5
Foam::DAObjFunc::scale_
scalar scale_
scale of the objective function
Definition: DAObjFunc.H:87
Foam::DATurbulenceModel::alphaEff
tmp< volScalarField > alphaEff()
return effective thermal diffusivity
Definition: DATurbulenceModel.C:236
daModel
DAModel daModel(mesh, daOption)
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::DAObjFuncWallHeatFlux::daTurb_
DATurbulenceModel & daTurb_
DATurbulenceModel object.
Definition: DAObjFuncWallHeatFlux.H:44
Foam::DAObjFunc::objFuncDict_
const dictionary & objFuncDict_
dictionary containing the information for the objective function
Definition: DAObjFunc.H:69
Foam::DAObjFuncWallHeatFlux::areaSum_
scalar areaSum_
the area of all heat flux patches
Definition: DAObjFuncWallHeatFlux.H:51
Foam::DAObjFuncWallHeatFlux::k_
scalar k_
thermal conductivity for solid heatFlux calculation
Definition: DAObjFuncWallHeatFlux.H:40
Foam::DAIndex::bFaceFaceI
labelList bFaceFaceI
Definition: DAIndex.H:116
Foam::DAObjFunc
Definition: DAObjFunc.H:33