adjointBoundaryConditionTemplates.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 "emptyFvPatch.H"
32 #include "adjointSolverManager.H"
33 #include "HashTable.H"
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 
41 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
42 
43 template<class Type>
44 tmp
45 <
47 >
49 {
50  // Return field
51  typedef typename outerProduct<vector, Type>::type GradType;
52  auto tresGrad = tmp<Field<GradType>>::New(patch_.size(), Zero);
53  auto& resGrad = tresGrad.ref();
54 
55  const labelList& faceCells = patch_.faceCells();
56  const fvMesh& mesh = patch_.boundaryMesh().mesh();
57  const cellList& cells = mesh.cells();
58 
59  // Go through the surfaceInterpolation scheme defined in gradSchemes for
60  // consistency
61  const GeometricField<Type, fvPatchField, volMesh>& field =
62  mesh.lookupObject<volVectorField>(name);
63 
64  // Gives problems when grad(AdjointVar) is computed using a limited scheme,
65  // since it is not possible to know a priori how many words to expect in the
66  // stream.
67  // Interpolation scheme is now read through interpolation schemes.
68  /*
69  word gradSchemeName ("grad(" + name + ')');
70  Istream& is = mesh.gradScheme(gradSchemeName);
71  word schemeData(is);
72  */
73 
74  tmp<surfaceInterpolationScheme<Type>> tinterpScheme
75  (
77  (
78  mesh,
79  mesh.interpolationScheme("interpolate(" + name + ")")
80  )
81  );
82 
83  GeometricField<Type, fvsPatchField, surfaceMesh> surfField
84  (
85  tinterpScheme().interpolate(field)
86  );
87 
88  // Auxiliary fields
89  const surfaceVectorField& Sf = mesh.Sf();
90  tmp<vectorField> tnf = patch_.nf();
91  const vectorField& nf = tnf();
92  const scalarField& V = mesh.V();
93  const labelUList& owner = mesh.owner();
94 
95  // Compute grad value of cell adjacent to the boundary
96  forAll(faceCells, fI)
97  {
98  const label cI = faceCells[fI];
99  const cell& cellI = cells[cI];
100  for (const label faceI : cellI) // global face numbering
101  {
102  label patchID = mesh.boundaryMesh().whichPatch(faceI);
103  if (patchID == -1) //face is internal
104  {
105  const label own = owner[faceI];
106  tensor flux = Sf[faceI]*surfField[faceI];
107  if (cI == own)
108  {
109  resGrad[fI] += flux;
110  }
111  else
112  {
113  resGrad[fI] -= flux;
114  }
115  }
116  else // Face is boundary. Covers coupled patches as well
117  {
118  if (!isA<emptyFvPatch>(mesh.boundary()[patchID]))
119  {
120  const fvPatch& patchForFlux = mesh.boundary()[patchID];
121  const label boundaryFaceI = faceI - patchForFlux.start();
122  const vectorField& Sfb = Sf.boundaryField()[patchID];
123  resGrad[fI] +=
124  Sfb[boundaryFaceI]
125  *surfField.boundaryField()[patchID][boundaryFaceI];
126  }
127  }
128  }
129  resGrad[fI] /= V[cI];
130  }
131 
132  // This has concluded the computation of the grad at the cell next to the
133  // boundary. We now need to compute the grad at the boundary face
134  const fvPatchField<Type>& bField = field.boundaryField()[patch_.index()];
135  resGrad = nf*bField.snGrad() + (resGrad - nf*(nf & resGrad));
136 
137  return tresGrad;
138 }
139 
140 
141 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
142 
143 template<class Type>
145 (
146  const fvPatch& p,
147  const DimensionedField<Type, volMesh>& iF,
148  const word& solverName
149 )
150 :
151  patch_(p),
152  managerName_("objectiveManager" + solverName),
153  adjointSolverName_(solverName),
154  simulationType_("incompressible"),
155  boundaryContrPtr_(nullptr),
156  addATCUaGradUTerm_(nullptr)
157 {
158  // Set the boundaryContribution pointer
159  setBoundaryContributionPtr();
160 }
161 
162 
163 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
164 
165 } // End namespace Foam
166 
167 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
Foam::scalarField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Definition: primitiveFieldsFwd.H:52
Foam::surfaceInterpolationScheme::New
static tmp< surfaceInterpolationScheme< Type > > New(const fvMesh &mesh, Istream &schemeData)
Return new tmp interpolation scheme.
Definition: surfaceInterpolationScheme.C:40
p
volScalarField & p
Definition: createFieldRefs.H:8
adjointBoundaryCondition.H
Foam::fvc::flux
tmp< surfaceScalarField > flux(const volVectorField &vvf)
Return the face-flux field obtained from the given volVectorField.
Foam::adjointBoundaryCondition::adjointBoundaryCondition
adjointBoundaryCondition(const fvPatch &p, const DimensionedField< Type, volMesh > &iF, const word &solverName)
Construct from field and base name.
Definition: adjointBoundaryCondition.C:184
HashTable.H
Foam::adjointBoundaryCondition::patch_
const fvPatch & patch_
Reference to patch.
Definition: adjointBoundaryCondition.H:59
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::outerProduct::type
typeOfRank< typename pTraits< arg1 >::cmptType, direction(pTraits< arg1 >::rank)+direction(pTraits< arg2 >::rank) >::type type
Definition: products.H:114
Foam::fvBoundaryMesh::mesh
const fvMesh & mesh() const
Return the mesh reference.
Definition: fvBoundaryMesh.H:102
adjointSolverManager.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::vectorField
Field< vector > vectorField
Specialisation of Field<T> for vector.
Definition: primitiveFieldsFwd.H:54
Foam::fvPatch::boundaryMesh
const fvBoundaryMesh & boundaryMesh() const
Return boundaryMesh reference.
Definition: fvPatch.H:203
Foam::fvPatch::size
virtual label size() const
Return size.
Definition: fvPatch.H:179
Foam::fvPatch::nf
tmp< vectorField > nf() const
Return face normals.
Definition: fvPatch.C:138
Foam::interpolate
bool interpolate(const vector &p1, const vector &p2, const vector &o, vector &n, scalar l)
Definition: curveTools.C:75
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
patchID
label patchID
Definition: boundaryProcessorFaPatchPoints.H:5
Foam::cellList
List< cell > cellList
A List of cells.
Definition: cellListFwd.H:47
field
rDeltaTY field()
Foam::fvPatch::index
label index() const
Return the index of this patch in the fvBoundaryMesh.
Definition: fvPatch.H:197
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::adjointBoundaryCondition::computePatchGrad
tmp< Field< typename Foam::outerProduct< Foam::vector, Type2 >::type > > computePatchGrad(word name)
Get gradient of field on a specific boundary.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
emptyFvPatch.H
Foam::volVectorField
GeometricField< vector, fvPatchField, volMesh > volVectorField
Definition: volFieldsFwd.H:62
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
Foam::fvPatch::faceCells
virtual const labelUList & faceCells() const
Return faceCells.
Definition: fvPatch.C:107
cells
const cellShapeList & cells
Definition: gmvOutputHeader.H:3
Foam::labelUList
UList< label > labelUList
A UList of labels.
Definition: UList.H:80
Foam::surfaceVectorField
GeometricField< vector, fvsPatchField, surfaceMesh > surfaceVectorField
Definition: surfaceFieldsFwd.H:59
Foam::tensor
Tensor< scalar > tensor
Tensor of scalars, i.e. Tensor<scalar>.
Definition: symmTensor.H:61
surfaceInterpolationScheme.H