leastSquaresGrad.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "leastSquaresGrad.H"
27 #include "leastSquaresVectors.H"
28 #include "gaussGrad.H"
29 #include "fvMesh.H"
30 #include "GeometricField.H"
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
35 template<class Type>
37 <
39 >
41 (
42  const VolField<Type>& vsf,
43  const word& name
44 ) const
45 {
46  typedef typename outerProduct<vector, Type>::type GradType;
47 
48  const fvMesh& mesh = vsf.mesh();
49 
50  tmp<VolField<GradType>> tlsGrad
51  (
53  (
54  name,
55  mesh,
56  dimensioned<GradType>
57  (
58  "zero",
59  vsf.dimensions()/dimLength,
60  Zero
61  ),
63  )
64  );
65  VolField<GradType>& lsGrad = tlsGrad.ref();
66 
67  // Get reference to least square vectors
68  const leastSquaresVectors& lsv = leastSquaresVectors::New(mesh);
69 
70  const surfaceVectorField& ownLs = lsv.pVectors();
71  const surfaceVectorField& neiLs = lsv.nVectors();
72 
73  const labelUList& own = mesh.owner();
74  const labelUList& nei = mesh.neighbour();
75 
76  forAll(own, facei)
77  {
78  label ownFacei = own[facei];
79  label neiFacei = nei[facei];
80 
81  Type deltaVsf = vsf[neiFacei] - vsf[ownFacei];
82 
83  lsGrad[ownFacei] += ownLs[facei]*deltaVsf;
84  lsGrad[neiFacei] -= neiLs[facei]*deltaVsf;
85  }
86 
87  // Boundary faces
88  forAll(vsf.boundaryField(), patchi)
89  {
90  const fvsPatchVectorField& patchOwnLs = ownLs.boundaryField()[patchi];
91 
92  const labelUList& faceCells =
93  vsf.boundaryField()[patchi].patch().faceCells();
94 
95  if (vsf.boundaryField()[patchi].coupled())
96  {
97  const Field<Type> neiVsf
98  (
99  vsf.boundaryField()[patchi].patchNeighbourField()
100  );
101 
102  forAll(neiVsf, patchFacei)
103  {
104  lsGrad[faceCells[patchFacei]] +=
105  patchOwnLs[patchFacei]
106  *(neiVsf[patchFacei] - vsf[faceCells[patchFacei]]);
107  }
108  }
109  else
110  {
111  const fvPatchField<Type>& patchVsf = vsf.boundaryField()[patchi];
112 
113  forAll(patchVsf, patchFacei)
114  {
115  lsGrad[faceCells[patchFacei]] +=
116  patchOwnLs[patchFacei]
117  *(patchVsf[patchFacei] - vsf[faceCells[patchFacei]]);
118  }
119  }
120  }
121 
122 
123  lsGrad.correctBoundaryConditions();
125 
126  return tlsGrad;
127 }
128 
129 
130 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Generic GeometricField class.
const labelUList & owner() const
Internal face owner.
Definition: fvMesh.H:490
const labelUList & neighbour() const
Internal face neighbour.
Definition: fvMesh.H:496
virtual tmp< VolField< typename outerProduct< vector, Type >::type > > calcGrad(const VolField< Type > &vsf, const word &name) const
Return the gradient of the given field to the gradScheme::grad.
A class for managing temporary objects.
Definition: tmp.H:55
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
label patchi
U correctBoundaryConditions()
static const zero Zero
Definition: zero.H:97
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
const dimensionSet & dimLength
Definition: dimensions.C:141
String typeName(const std::type_info &info)
Return the un-mangled name given the standard type info.
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
SurfaceField< vector > surfaceVectorField
UList< label > labelUList
Definition: UList.H:65
fvsPatchField< vector > fvsPatchVectorField
tmp< DimensionedField< TypeR, GeoMesh, Field > > New(const tmp< DimensionedField< TypeR, GeoMesh, Field >> &tdf1, const word &name, const dimensionSet &dimensions)
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488