adjointSensitivityIncompressible.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2007-2019 PCOpt/NTUA
9  Copyright (C) 2013-2019 FOSS GP
10  Copyright (C) 2019 OpenCFD Ltd.
11 -------------------------------------------------------------------------------
12 License
13  This file is part of OpenFOAM.
14 
15  OpenFOAM is free software: you can redistribute it and/or modify it
16  under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 3 of the License, or
18  (at your option) any later version.
19 
20  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
27 
28 \*---------------------------------------------------------------------------*/
29 
30 #include "runTimeSelectionTables.H"
34 #include "wallFvPatch.H"
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 
41 namespace incompressible
42 {
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 defineTypeNameAndDebug(adjointSensitivity, 0);
47 defineRunTimeSelectionTable(adjointSensitivity, dictionary);
48 
49 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
50 
51 adjointSensitivity::adjointSensitivity
52 (
53  const fvMesh& mesh,
54  const dictionary& dict,
55  incompressibleVars& primalVars,
56  incompressibleAdjointVars& adjointVars,
59 )
60 :
62  derivatives_(0),
63  primalVars_(primalVars),
64  adjointVars_(adjointVars),
65  objectiveManager_(objectiveManager),
66  fvOptionsAdjoint_(fvOptionsAdjoint)
67 {}
68 
69 
70 // * * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * //
71 
73 (
74  const fvMesh& mesh,
75  const dictionary& dict,
76  incompressibleVars& primalVars,
77  incompressibleAdjointVars& adjointVars,
80 )
81 {
82  const word modelType(dict.get<word>("type"));
83 
84  Info<< "adjointSensitivity type : " << modelType << endl;
85 
86  auto cstrIter = dictionaryConstructorTablePtr_->cfind(modelType);
87 
88  if (!cstrIter.found())
89  {
91  (
92  dict,
93  "adjointSensitivity",
94  modelType,
95  *dictionaryConstructorTablePtr_
96  ) << exit(FatalIOError);
97  }
98 
100  (
101  cstrIter()
102  (
103  mesh,
104  dict,
105  primalVars,
106  adjointVars,
109  )
110  );
111 }
112 
113 
114 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * //
115 
117 {
119  write(type());
120  return derivatives_;
121 }
122 
123 
125 {
126  return derivatives_;
127 }
128 
129 
131 {
132  derivatives_ = scalar(0);
133  if (fieldSensPtr_.valid())
134  {
135  fieldSensPtr_().primitiveFieldRef() = scalar(0);
136  }
137 }
138 
139 
140 void adjointSensitivity::write(const word& baseName)
141 {
142  sensitivity::write(baseName);
143 }
144 
145 
147 {
148  // Term depending on the adjoint turbulence model
150  (
152  );
153  tmp<volTensorField> tturbulenceTerm(adjointRAS->FISensitivityTerm());
154  volTensorField& turbulenceTerm = tturbulenceTerm.ref();
155 
156  // nu effective
157  tmp<volScalarField> tnuEff(adjointRAS->nuEff());
158  const volScalarField& nuEff = tnuEff();
159 
160  tmp<volTensorField> tflowTerm
161  (
162  new volTensorField
163  (
164  IOobject
165  (
166  "flowTerm",
167  mesh_.time().timeName(),
168  mesh_,
171  ),
172  mesh_,
174  )
175  );
176  volTensorField& flowTerm = tflowTerm.ref();
177 
178  const volScalarField& p = primalVars_.p();
179  const volVectorField& U = primalVars_.U();
180  const volScalarField& pa = adjointVars_.pa();
181  const volVectorField& Ua = adjointVars_.Ua();
182  volTensorField gradU(fvc::grad(U));
183  volTensorField gradUa(fvc::grad(Ua));
184 
185  // Explicitly correct the boundary gradient to get rid of
186  // the tangential component
187  forAll(mesh_.boundary(), patchI)
188  {
189  const fvPatch& patch = mesh_.boundary()[patchI];
190  if (isA<wallFvPatch>(patch))
191  {
192  tmp<vectorField> tnf = mesh_.boundary()[patchI].nf();
193  const vectorField& nf = tnf();
194  gradU.boundaryFieldRef()[patchI] =
195  nf*U.boundaryField()[patchI].snGrad();
196  //gradUa.boundaryField()[patchI] =
197  // nf*Ua.boundaryField()[patchI].snGrad();
198  }
199  }
200 
201  volTensorField stress(nuEff*(gradU + T(gradU)));
202  autoPtr<volVectorField> stressXPtr
203  (
204  createZeroFieldPtr<vector>(mesh_, "stressX", stress.dimensions())
205  );
206  autoPtr<volVectorField> stressYPtr
207  (
208  createZeroFieldPtr<vector>(mesh_, "stressY", stress.dimensions())
209  );
210  autoPtr<volVectorField> stressZPtr
211  (
212  createZeroFieldPtr<vector>(mesh_, "stressZ", stress.dimensions())
213  );
214 
215  stressXPtr().replace(0, stress.component(0));
216  stressXPtr().replace(1, stress.component(1));
217  stressXPtr().replace(2, stress.component(2));
218 
219  stressYPtr().replace(0, stress.component(3));
220  stressYPtr().replace(1, stress.component(4));
221  stressYPtr().replace(2, stress.component(5));
222 
223  stressZPtr().replace(0, stress.component(6));
224  stressZPtr().replace(1, stress.component(7));
225  stressZPtr().replace(2, stress.component(8));
226 
227  volTensorField gradStressX(fvc::grad(stressXPtr()));
228  volTensorField gradStressY(fvc::grad(stressYPtr()));
229  volTensorField gradStressZ(fvc::grad(stressZPtr()));
230 
231  // Contribution from objective functions and constraints
232  volTensorField objectiveContributions
233  (
234  IOobject
235  (
236  "objectiveContributions",
237  mesh_.time().timeName(),
238  mesh_,
241  ),
242  mesh_,
244  );
246  forAll(functions, funcI)
247  {
248  objectiveContributions +=
249  functions[funcI].weight()
250  *functions[funcI].gradDxDbMultiplier();
251  }
252 
253  // Note:
254  // term4 (Ua & grad(stress)) is numerically tricky. Its div leads to third
255  // order spatial derivs in E-SI based computations Applying the product
256  // derivative rule (putting Ua inside the grad) gives better results in
257  // NACA0012, SA, WF. However, the original formulation should be kept at
258  // the boundary in order to respect the Ua boundary conditions (necessary
259  // for E-SI to give the same sens as FI). A mixed approach is hence
260  // followed
261  volTensorField term4
262  (
263  - nuEff*(gradUa & (gradU + T(gradU)))
264  + fvc::grad(nuEff * Ua & (gradU + T(gradU)))
265  );
266 
267  forAll(mesh_.boundary(), pI)
268  {
269  if (!isA<coupledFvPatch>(mesh_.boundary()[pI]))
270  {
271  term4.boundaryFieldRef()[pI] =
272  Ua.component(0)().boundaryField()[pI]
273  *gradStressX.boundaryField()[pI]
274  + Ua.component(1)().boundaryField()[pI]
275  *gradStressY.boundaryField()[pI]
276  + Ua.component(2)().boundaryField()[pI]
277  *gradStressZ.boundaryField()[pI];
278  }
279  }
280 
281  const autoPtr<ATCModel>& ATCModel =
283  (
285  ).getATCModel();
286 
287  // Compute dxdb multiplier
288  flowTerm =
289  // Term 1, ATC
291  // Term 2
292  - fvc::grad(p) * Ua
293  // Term 3
294  - nuEff*(gradU & (gradUa + T(gradUa)))
295  // Term 4
296  + term4
297  // Term 5
298  + (pa * gradU)
299  // Term 6, from the adjoint turbulence model
300  + turbulenceTerm.T()
301  // Term 7, term from objective functions
302  + objectiveContributions;
303 
304  flowTerm.correctBoundaryConditions();
305 
306  return (tflowTerm);
307 }
308 
309 
311 {
313  volTensorField& gradDxDbMult = tgradDxDbMult.ref();
314 
315  tmp<volVectorField> tadjointMeshMovementSource
316  (
317  new volVectorField
318  (
319  IOobject
320  (
321  "adjointMeshMovementSource",
322  mesh_.time().timeName(),
323  mesh_,
326  ),
327  mesh_,
328  dimensionedVector(gradDxDbMult.dimensions()/dimLength, Zero)
329  )
330  );
331 
332  volVectorField& source = tadjointMeshMovementSource.ref();
333 
334  source -= fvc::div(gradDxDbMult.T());
335 
336  // Terms from fvOptions
338  {
339  source += fvOptionsAdjoint_[oI].dxdbMult(adjointVars_);
340  }
341 
342  return (tadjointMeshMovementSource);
343 }
344 
345 
346 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
347 
348 } // End namespace incompressible
349 } // End namespace Foam
350 
351 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
Foam::incompressible::adjointSensitivity::derivatives_
scalarField derivatives_
Definition: adjointSensitivityIncompressible.H:84
Foam::objectiveManager
class for managing incompressible objective functions.
Definition: objectiveManager.H:54
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::GeometricField::component
tmp< GeometricField< cmptType, PatchField, GeoMesh > > component(const direction) const
Return a component of the field.
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::fvc::grad
tmp< GeometricField< typename outerProduct< vector, Type >::type, fvPatchField, volMesh >> grad(const GeometricField< Type, fvsPatchField, surfaceMesh > &ssf)
Definition: fvcGrad.C:54
Foam::incompressible::adjointSensitivity::calculateSensitivities
virtual const scalarField & calculateSensitivities()
Calculates and returns sensitivity fields.
Definition: adjointSensitivityIncompressible.C:116
Foam::incompressibleAdjointSolver
Base class for incompressibleAdjoint solvers.
Definition: incompressibleAdjointSolver.H:54
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::incompressible::adjointSensitivity::fvOptionsAdjoint_
fv::optionAdjointList & fvOptionsAdjoint_
Definition: adjointSensitivityIncompressible.H:88
Foam::incompressible::defineTypeNameAndDebug
defineTypeNameAndDebug(adjointEikonalSolver, 0)
Foam::dimLength
const dimensionSet dimLength(0, 1, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:53
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
boundaryAdjointContribution.H
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:785
Foam::incompressible::adjointSensitivity::assembleSensitivities
virtual void assembleSensitivities()=0
Assemble sensitivities.
wallFvPatch.H
Foam::fvc::div
tmp< GeometricField< Type, fvPatchField, volMesh > > div(const GeometricField< Type, fvsPatchField, surfaceMesh > &ssf)
Definition: fvcDiv.C:49
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::incompressibleVars::p
const volScalarField & p() const
Return const reference to pressure.
Definition: incompressibleVars.C:305
Foam::incompressible::defineRunTimeSelectionTable
defineRunTimeSelectionTable(adjointSensitivity, dictionary)
Foam::ATCModel
Base class for selecting the adjoint transpose convection model. Inherits from regIOobject to add loo...
Definition: ATCModel.H:60
Foam::incompressibleAdjointVars
Class including all adjoint fields for incompressible flows.
Definition: incompressibleAdjointVars.H:52
Foam::fv::optionAdjointList
Definition: fvOptionAdjointList.H:59
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
incompressibleAdjointSolver.H
Foam::dimensionedVector
dimensioned< vector > dimensionedVector
Dimensioned vector obtained from generic dimensioned type.
Definition: dimensionedVector.H:50
Foam::dimTime
const dimensionSet dimTime(0, 0, 1, 0, 0, 0, 0)
Definition: dimensionSets.H:54
Foam::tmp::ref
T & ref() const
Definition: tmpI.H:258
FatalIOErrorInLookup
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:397
Foam::sensitivity::mesh_
const fvMesh & mesh_
Definition: sensitivity.H:69
Foam::Field< scalar >
Foam::pow3
dimensionedScalar pow3(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:89
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::fvPatch
A finiteVolume patch using a polyPatch and a fvBoundaryMesh.
Definition: fvPatch.H:65
Foam::ATCModel::getFISensitivityTerm
virtual tmp< volTensorField > getFISensitivityTerm() const =0
Get the FI sensitivity derivatives term coming from the ATC.
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::incompressible::adjointSensitivity::New
static autoPtr< adjointSensitivity > New(const fvMesh &mesh, const dictionary &dict, incompressibleVars &primalVars, incompressibleAdjointVars &adjointVars, objectiveManager &objectiveManager, fv::optionAdjointList &fvOptionsAdjoint)
Return a reference to the selected turbulence model.
Definition: adjointSensitivityIncompressible.C:73
Foam::incompressibleAdjointMeanFlowVars::pa
const volScalarField & pa() const
Return const reference to pressure.
Definition: incompressibleAdjointMeanFlowVars.C:147
Foam::incompressibleVars::U
const volVectorField & U() const
Return const reference to velocity.
Definition: incompressibleVars.C:331
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:62
Foam::incompressible::adjointSensitivity::clearSensitivities
virtual void clearSensitivities()
Zero sensitivity fields and their constituents.
Definition: adjointSensitivityIncompressible.C:130
Foam::objectRegistry::lookupObject
const Type & lookupObject(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:434
Foam::incompressibleAdjointMeanFlowVars::Ua
const volVectorField & Ua() const
Return const reference to velocity.
Definition: incompressibleAdjointMeanFlowVars.C:173
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::sensitivity::fieldSensPtr_
autoPtr< volScalarField > fieldSensPtr_
Definition: sensitivity.H:74
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::incompressible::adjointSensitivity::adjointVars_
incompressibleAdjointVars & adjointVars_
Definition: adjointSensitivityIncompressible.H:86
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:84
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::incompressible::adjointSensitivity::primalVars_
incompressibleVars & primalVars_
Definition: adjointSensitivityIncompressible.H:85
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::GeometricField::T
tmp< GeometricField< Type, PatchField, GeoMesh > > T() const
Return transpose (only if it is a tensor field)
Definition: GeometricField.C:1046
Foam::fvMesh::boundary
const fvBoundaryMesh & boundary() const
Return reference to boundary mesh.
Definition: fvMesh.C:555
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
U
U
Definition: pEqn.H:72
Foam::sqr
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Definition: dimensionedSymmTensor.C:51
adjointSensitivityIncompressible.H
Foam::foamVersion::patch
const std::string patch
OpenFOAM patch number as a std::string.
runTimeSelectionTables.H
Macros to ease declaration of run-time selection tables.
Foam::GeometricField::ref
Internal & ref(const bool updateAccessTime=true)
Return a reference to the dimensioned internal field.
Definition: GeometricField.C:749
Foam::GeometricField::boundaryFieldRef
Boundary & boundaryFieldRef(const bool updateAccessTime=true)
Return a reference to the boundary field.
Definition: GeometricField.C:783
Foam::incompressibleAdjointVars::adjointTurbulence
const autoPtr< incompressibleAdjoint::adjointRASModel > & adjointTurbulence() const
Return const reference to the adjointRASModel.
Definition: incompressibleAdjointVars.C:70
Foam::incompressible::adjointSensitivity::adjointMeshMovementSource
tmp< volVectorField > adjointMeshMovementSource()
Compute source term for adjoint mesh movement equation.
Definition: adjointSensitivityIncompressible.C:310
Foam::incompressible::adjointSensitivity::getSensitivities
const scalarField & getSensitivities() const
Returns the sensitivity fields.
Definition: adjointSensitivityIncompressible.C:124
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:248
Foam::sensitivity::write
virtual void write(const word &baseName=word::null)
Write sensitivity fields.
Definition: sensitivity.C:77
Foam::objectiveManager::adjointSolverName
const word & adjointSolverName() const
Return name of the adjointSolver.
Definition: objectiveManager.C:259
Foam::GeometricField< tensor, fvPatchField, volMesh >
Foam::IOobject::NO_READ
Definition: IOobject.H:123
Foam::sensitivity
Abstract base class for adjoint sensitivities.
Definition: sensitivity.H:63
fvOptionsAdjoint
fv::IOoptionListAdjoint fvOptionsAdjoint(mesh)
Foam::dimensionedTensor
dimensioned< tensor > dimensionedTensor
Dimensioned tensor obtained from generic dimensioned type.
Definition: dimensionedTensor.H:52
Foam::incompressible::adjointSensitivity::computeGradDxDbMultiplier
tmp< volTensorField > computeGradDxDbMultiplier()
Definition: adjointSensitivityIncompressible.C:146
Foam::incompressibleVars
Base class for solution control classes.
Definition: incompressibleVars.H:54
Foam::GeometricField::boundaryField
const Boundary & boundaryField() const
Return const-reference to the boundary field.
Definition: GeometricFieldI.H:62
Foam::incompressible::adjointSensitivity::write
virtual void write(const word &baseName=word::null)
Write sensitivity fields.
Definition: adjointSensitivityIncompressible.C:140
Foam::objectiveManager::getObjectiveFunctions
PtrList< objective > & getObjectiveFunctions()
Return reference to objective functions.
Definition: objectiveManager.C:247
Foam::incompressible::adjointSensitivity::objectiveManager_
objectiveManager & objectiveManager_
Definition: adjointSensitivityIncompressible.H:87