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) 2013-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"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 template<class Type, class Stencil>
36 <
38 >
40 (
41  const VolField<Type>& vtf,
42  const word& name
43 ) const
44 {
45  typedef typename outerProduct<vector, Type>::type GradType;
46 
47  const fvMesh& mesh = vtf.mesh();
48 
49  // Get reference to least square vectors
50  const LeastSquaresVectors<Stencil>& lsv = LeastSquaresVectors<Stencil>::New
51  (
52  mesh
53  );
54 
55  tmp<VolField<GradType>> tlsGrad
56  (
58  (
59  name,
60  mesh,
61  dimensioned<GradType>
62  (
63  "zero",
64  vtf.dimensions()/dimLength,
65  Zero
66  ),
68  )
69  );
70  VolField<GradType>& lsGrad = tlsGrad.ref();
71  Field<GradType>& lsGradIf = lsGrad;
72 
73  const extendedCentredCellToCellStencil& stencil = lsv.stencil();
74  const List<List<label>>& stencilAddr = stencil.stencil();
75  const List<List<vector>>& lsvs = lsv.vectors();
76 
77  // Construct flat version of vtf
78  // including all values referred to by the stencil
79  List<Type> flatVtf(stencil.map().constructSize(), Zero);
80 
81  // Insert internal values
82  forAll(vtf, celli)
83  {
84  flatVtf[celli] = vtf[celli];
85  }
86 
87  // Insert boundary values
88  forAll(vtf.boundaryField(), patchi)
89  {
90  const fvPatchField<Type>& ptf = vtf.boundaryField()[patchi];
91 
92  label nCompact =
93  ptf.patch().start()
95  + mesh.nCells();
96 
97  forAll(ptf, i)
98  {
99  flatVtf[nCompact++] = ptf[i];
100  }
101  }
102 
103  // Do all swapping to complete flatVtf
104  stencil.map().distribute(flatVtf);
105 
106  // Accumulate the cell-centred gradient from the
107  // weighted least-squares vectors and the flattened field values
108  forAll(stencilAddr, celli)
109  {
110  const labelList& compactCells = stencilAddr[celli];
111  const List<vector>& lsvc = lsvs[celli];
112 
113  forAll(compactCells, i)
114  {
115  lsGradIf[celli] += lsvc[i]*flatVtf[compactCells[i]];
116  }
117  }
118 
119  // Correct the boundary conditions
120  lsGrad.correctBoundaryConditions();
122 
123  return tlsGrad;
124 }
125 
126 
127 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Generic GeometricField class.
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.
Motion of the mesh specified as a list of pointMeshMovers.
label nInternalFaces() const
label nCells() const
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
List< label > labelList
A List of labels.
Definition: labelList.H:56
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.
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