DAOutputThermalCoupling.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(DAOutputThermalCoupling, 0);
16 addToRunTimeSelectionTable(DAOutput, DAOutputThermalCoupling, dictionary);
17 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
18 
20  const word outputName,
21  const word outputType,
22  fvMesh& mesh,
23  const DAOption& daOption,
24  DAModel& daModel,
25  const DAIndex& daIndex,
26  DAResidual& daResidual,
27  UPtrList<DAFunction>& daFunctionList)
28  : DAOutput(
29  outputName,
30  outputType,
31  mesh,
32  daOption,
33  daModel,
34  daIndex,
35  daResidual,
36  daFunctionList)
37 {
38  daOption_.getAllOptions().subDict("outputInfo").subDict(outputName_).readEntry("patches", patches_);
39  // NOTE: always sort the patch because the order of the patch element matters in CHT coupling
40  sort(patches_);
41 
42  // check discipline
43  discipline_ = daOption_.getAllOptions().getWord("discipline");
44 
45  // check coupling mode and validate
46  distanceMode_ = daOption_.getAllOptions().getWord("wallDistanceMethod");
47  if (distanceMode_ != "daCustom" && distanceMode_ != "default")
48  {
49  FatalErrorIn(" ") << "wallDistanceMethod: "
50  << distanceMode_ << " not supported!"
51  << " Options are: default and daCustom."
52  << abort(FatalError);
53  }
54 
55  size_ = 0;
56  forAll(patches_, idxI)
57  {
58  word patchName = patches_[idxI];
59  label patchI = mesh_.boundaryMesh().findPatchID(patchName);
60  forAll(mesh_.boundaryMesh()[patchI], faceI)
61  {
62  size_++;
63  }
64  }
65  // we have two sets of variable to transfer
66  size_ *= 2;
67 }
68 
69 void DAOutputThermalCoupling::run(scalarList& output)
70 {
71  /*
72  Description:
73  Assign output based on OF fields
74  */
75 
76  const objectRegistry& db = mesh_.thisDb();
77  const volScalarField& T = db.lookupObject<volScalarField>("T");
78 
79  // ************ first loop, get the near wall cell temperature
80  label counterI = 0;
81  forAll(patches_, idxI)
82  {
83  // get the patch id label
84  word patchName = patches_[idxI];
85  label patchI = mesh_.boundaryMesh().findPatchID(patchName);
86  forAll(mesh_.boundaryMesh()[patchI], faceI)
87  {
88  label faceCellI = mesh_.boundaryMesh()[patchI].faceCells()[faceI];
89  output[counterI] = T[faceCellI];
90  counterI++;
91  }
92  }
93 
94  // ********* second loop, get the (kappa / d) coefficient
95  scalar deltaCoeffs = 0;
96 
97  if (discipline_ == "aero")
98  {
99  // for incompressible flow Q = Cp * alphaEff * dT/dz, so kappa = Cp * alphaEff
100 
101  // for incompressible flow Q = Cp * alphaEff * dT/dz, so kappa = Cp * alphaEff
103  word turbModelType = daTurb.getTurbModelType();
104 
105  if (turbModelType == "incompressible")
106  {
107  volScalarField alphaEff = daTurb.alphaEff();
108 
109  IOdictionary transportProperties(
110  IOobject(
111  "transportProperties",
112  mesh_.time().constant(),
113  mesh_,
114  IOobject::MUST_READ,
115  IOobject::NO_WRITE,
116  false));
117  scalar Cp = readScalar(transportProperties.lookup("Cp"));
118 
119  forAll(patches_, idxI)
120  {
121  // get the patch id label
122  word patchName = patches_[idxI];
123  label patchI = mesh_.boundaryMesh().findPatchID(patchName);
124  forAll(mesh_.boundaryMesh()[patchI], faceI)
125  {
126  if (distanceMode_ == "default")
127  {
128  // deltaCoeffs = 1 / d
129  deltaCoeffs = T.boundaryField()[patchI].patch().deltaCoeffs()[faceI];
130  }
131  else if (distanceMode_ == "daCustom")
132  {
133  label nearWallCellIndex = mesh_.boundaryMesh()[patchI].faceCells()[faceI];
134  vector c1 = mesh_.Cf().boundaryField()[patchI][faceI];
135  vector c2 = mesh_.C()[nearWallCellIndex];
136  scalar d = mag(c1 - c2);
137  deltaCoeffs = 1 / d;
138  }
139  scalar alphaEffBf = alphaEff.boundaryField()[patchI][faceI];
140  // NOTE: we continue to use the counterI from the first loop
141  output[counterI] = Cp * alphaEffBf * deltaCoeffs;
142  counterI++;
143  }
144  }
145  }
146 
147  if (turbModelType == "compressible")
148  {
149  // for compressible flow Q = alphaEff * dHE/dz, so if enthalpy is used, kappa = Cp * alphaEff
150  // if the internalEnergy is used, kappa = (Cp - R) * alphaEff
151 
152  volScalarField alphaEff = daTurb.alphaEff();
153  // compressible flow, H = alphaEff * dHE/dz
154  const fluidThermo& thermo = mesh_.thisDb().lookupObject<fluidThermo>("thermophysicalProperties");
155  const volScalarField& he = thermo.he();
156 
157  const IOdictionary& thermoDict = mesh_.thisDb().lookupObject<IOdictionary>("thermophysicalProperties");
158  dictionary mixSubDict = thermoDict.subDict("mixture");
159  dictionary specieSubDict = mixSubDict.subDict("specie");
160  scalar molWeight = specieSubDict.getScalar("molWeight");
161  dictionary thermodynamicsSubDict = mixSubDict.subDict("thermodynamics");
162  scalar Cp = thermodynamicsSubDict.getScalar("Cp");
163 
164  // 8314.4700665 gas constant in OpenFOAM
165  // src/OpenFOAM/global/constants/thermodynamic/thermodynamicConstants.H
166  scalar RR = Foam::constant::thermodynamic::RR;
167 
168  // R = RR/molWeight
169  // Foam::specie::R() function in src/thermophysicalModels/specie/specie/specieI.H
170  scalar R = RR / molWeight;
171 
172  scalar tmpVal = 0;
173  // e = (Cp - R) * T, so Q = alphaEff * (Cp-R) * dT/dz
174  if (he.name() == "e")
175  {
176  tmpVal = Cp - R;
177  }
178  // h = Cp * T, so Q = alphaEff * Cp * dT/dz
179  else
180  {
181  tmpVal = Cp;
182  }
183 
184  forAll(patches_, idxI)
185  {
186  // get the patch id label
187  word patchName = patches_[idxI];
188  label patchI = mesh_.boundaryMesh().findPatchID(patchName);
189  forAll(mesh_.boundaryMesh()[patchI], faceI)
190  {
191  if (distanceMode_ == "default")
192  {
193  // deltaCoeffs = 1 / d
194  deltaCoeffs = T.boundaryField()[patchI].patch().deltaCoeffs()[faceI];
195  }
196  else if (distanceMode_ == "daCustom")
197  {
198  label nearWallCellIndex = mesh_.boundaryMesh()[patchI].faceCells()[faceI];
199  vector c1 = mesh_.Cf().boundaryField()[patchI][faceI];
200  vector c2 = mesh_.C()[nearWallCellIndex];
201  scalar d = mag(c1 - c2);
202  deltaCoeffs = 1 / d;
203  }
204  scalar alphaEffBf = alphaEff.boundaryField()[patchI][faceI];
205  // NOTE: we continue to use the counterI from the first loop
206  output[counterI] = tmpVal * alphaEffBf * deltaCoeffs;
207  counterI++;
208  }
209  }
210  }
211  }
212  else if (discipline_ == "thermal")
213  {
214  // for solid solvers Q = k * dT/dz, so kappa = k
215  IOdictionary solidProperties(
216  IOobject(
217  "solidProperties",
218  mesh_.time().constant(),
219  mesh_,
220  IOobject::MUST_READ,
221  IOobject::NO_WRITE,
222  false));
223  scalar k = readScalar(solidProperties.lookup("k"));
224 
225  forAll(patches_, idxI)
226  {
227  // get the patch id label
228  word patchName = patches_[idxI];
229  label patchI = mesh_.boundaryMesh().findPatchID(patchName);
230  forAll(mesh_.boundaryMesh()[patchI], faceI)
231  {
232  if (distanceMode_ == "default")
233  {
234  // deltaCoeffs = 1 / d
235  deltaCoeffs = T.boundaryField()[patchI].patch().deltaCoeffs()[faceI];
236  }
237  else if (distanceMode_ == "daCustom")
238  {
239  label nearWallCellIndex = mesh_.boundaryMesh()[patchI].faceCells()[faceI];
240  vector c1 = mesh_.Cf().boundaryField()[patchI][faceI];
241  vector c2 = mesh_.C()[nearWallCellIndex];
242  scalar d = mag(c1 - c2);
243  deltaCoeffs = 1 / d;
244  }
245  // NOTE: we continue to use the counterI from the first loop
246  output[counterI] = k * deltaCoeffs;
247  counterI++;
248  }
249  }
250  }
251  else
252  {
253  FatalErrorIn("DAOutputThermalCoupling::run") << " discipline not valid! "
254  << abort(FatalError);
255  }
256 }
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
260 } // End namespace Foam
261 
262 // ************************************************************************* //
Foam::DAOption
Definition: DAOption.H:29
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFunction, DAFunctionForce, dictionary)
he
volScalarField & he
Definition: EEqnRhoPimple.H:5
thermo
fluidThermo & thermo
Definition: createRefsRhoPimple.H:6
Foam::DAOutputThermalCoupling::discipline_
word discipline_
whether this is flow or solid
Definition: DAOutputThermalCoupling.H:52
Foam::DAOption::getAllOptions
const dictionary & getAllOptions() const
return a reference of allOptions_ dictionary
Definition: DAOption.H:56
Foam::DAOutputThermalCoupling::size_
label size_
the total face number for all the patches_ owned by this processor
Definition: DAOutputThermalCoupling.H:49
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DAOutputThermalCoupling::patches_
wordList patches_
list of patch names for the thermal var
Definition: DAOutputThermalCoupling.H:46
Foam::DAOutput::outputName_
const word outputName_
name of the output
Definition: DAOutput.H:44
Foam::DAOutput::daModel_
DAModel & daModel_
DAIndex object.
Definition: DAOutput.H:56
Foam::DAIndex
Definition: DAIndex.H:32
Foam::DAOutput
Definition: DAOutput.H:32
Foam::DAModel
Definition: DAModel.H:57
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFunction, 0)
k
dimensionedScalar & k
Definition: createRefsHeatTransfer.H:6
Foam::DAOutput::daOption_
const DAOption & daOption_
DAOption object.
Definition: DAOutput.H:53
Foam
Definition: checkGeometry.C:32
T
volScalarField & T
Definition: createRefsHeatTransfer.H:5
alphaEff
volScalarField alphaEff("alphaEff", turbulencePtr_->nu()/Pr+alphat)
forAll
forAll(nuTilda1, cellI)
Definition: nuTilda1EqnIrkPimple.H:19
Foam::DATurbulenceModel::getTurbModelType
word getTurbModelType() const
Definition: DATurbulenceModel.H:260
Foam::DAResidual
Definition: DAResidual.H:36
Foam::DATurbulenceModel
Definition: DATurbulenceModel.H:52
DAOutputThermalCoupling.H
Foam::DATurbulenceModel::alphaEff
tmp< volScalarField > alphaEff()
return effective thermal diffusivity
Definition: DATurbulenceModel.C:233
Foam::DAModel::getDATurbulenceModel
const DATurbulenceModel & getDATurbulenceModel() const
get a reference to DATurbulenceModel
Definition: DAModel.C:176
Foam::DAOutputThermalCoupling::distanceMode_
word distanceMode_
if calculating wallHeatFlux by OpenFOAMs snGrad() or DAFOAM's custom (daCustom) formulation
Definition: DAOutputThermalCoupling.H:55
Foam::DAOutput::mesh_
fvMesh & mesh_
fvMesh
Definition: DAOutput.H:50
Foam::DAOutputThermalCoupling::DAOutputThermalCoupling
DAOutputThermalCoupling(const word outputName, const word outputType, fvMesh &mesh, const DAOption &daOption, DAModel &daModel, const DAIndex &daIndex, DAResidual &daResidual, UPtrList< DAFunction > &daFunctionList)
Definition: DAOutputThermalCoupling.C:19
Foam::DAOutputThermalCoupling::run
virtual void run(scalarList &output)
Definition: DAOutputThermalCoupling.C:69