leastSquaresCellCellStencil.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) 2017 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify i
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
30 #include "SVD.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace cellCellStencils
37 {
38  defineTypeNameAndDebug(leastSquares, 0);
39  addToRunTimeSelectionTable(cellCellStencil, leastSquares, mesh);
40 }
41 }
42 
43 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
44 
46 (
47  const point& sample,
48  const pointList& donorCcs,
49  scalarList& weights
50 ) const
51 {
52  // Implicit least squares weighting
53  // Number of donors
54  label nD = donorCcs.size();
55 
56  weights.setSize(nD);
57 
58  // List for distance vectors and LSQ weights
59  List<vector> d(nD);
60  scalarList LSQw(nD);
61 
62  // Sum of weights
63  scalar W = 0;
64 
65  // Sum of weighted distance vectors
66  vector dw(Zero);
67 
69 
70  bool shortC = false;
71 
72  // Compute distance vectors and fill rectangular matrix
73  forAll(donorCcs, j)
74  {
75  // Neighbour weights
76  d[j] = donorCcs[j] - sample;
77 
78  // Check for short-circuiting if zero distance
79  // is detected with respect to any donor
80  if (mag(d[j]) < ROOTVSMALL)
81  {
82  shortC = true;
83  break;
84  }
85 
86  LSQw[j] = 1.0/magSqr(d[j]);
87 
88  // T matrix
89  vector wd = LSQw[j]*d[j];
90  A[j][0] = wd.x();
91  A[j][1] = wd.y();
92  A[j][2] = wd.z();
93 
94  // Sum of weighted distance vectors
95  dw += wd;
96 
97  // Sum of weights
98  W += LSQw[j];
99  }
100 
101  if (!shortC)
102  {
103  // Use Singular Value Decomposition to avoid problems
104  // with 1D, 2D stencils
105  SVD svd(A.T()*A, SMALL);
106 
107  // Least squares vectors
108  RectangularMatrix<scalar> ATAinvAT(svd.VSinvUt()*A.T());
109 
110  scalar saveDiag(W);
111 
112  // Diagonal coefficient
113  for (label i = 0; i < 3; i++)
114  {
115  // Get row
116  scalarList Row(UList<scalar>(ATAinvAT[i], nD));
117 
118  forAll(donorCcs, j)
119  {
120  W -= Row[j]*LSQw[j]*dw[i];
121  }
122  }
123 
124  if (mag(W) < SMALL*mag(saveDiag))
125  {
126  shortC = true;
127  }
128  else
129  {
130  // Compute final neighbour weights with additional scaling
131  forAll(donorCcs, j)
132  {
133  weights[j] =
134  (
135  LSQw[j]
136  - ATAinvAT[0][j]*LSQw[j]*dw[0]
137  - ATAinvAT[1][j]*LSQw[j]*dw[1]
138  - ATAinvAT[2][j]*LSQw[j]*dw[2]
139  )
140  /W;
141  }
142  }
143  }
144 
145  if (shortC)
146  {
147  // Matrix ill conditioned. Use straight injection from central
148  // donor.
149  weights = 0.0;
150  weights[0] = 1.0;
151  }
152 }
153 
154 
155 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
156 
157 Foam::cellCellStencils::leastSquares::leastSquares
158 (
159  const fvMesh& mesh,
160  const dictionary& dict,
161  const bool doUpdate
162 )
163 :
164  inverseDistance(mesh, dict, false)
165 {
166  if (doUpdate)
167  {
168  update();
169  }
170 }
171 
172 
173 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
174 
176 {}
177 
178 
179 // ************************************************************************* //
Foam::Vector::x
const Cmpt & x() const
Access to the vector x component.
Definition: VectorI.H:73
Foam::cellCellStencils::inverseDistance
Inverse-distance-weighted interpolation stencil.
Definition: inverseDistanceCellCellStencil.H:67
SVD.H
update
mesh update()
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
A
static const Foam::dimensionedScalar A("", Foam::dimPressure, 611.21)
Foam::dimensioned::T
dimensioned< Type > T() const
Return transpose.
Definition: dimensionedSphericalTensor.C:38
Foam::Vector::z
const Cmpt & z() const
Access to the vector z component.
Definition: VectorI.H:85
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Foam::cellCellStencils::leastSquares::~leastSquares
virtual ~leastSquares()
Destructor.
Definition: leastSquaresCellCellStencil.C:175
Foam::List::setSize
void setSize(const label n)
Alias for resize()
Definition: List.H:222
Foam::RectangularMatrix< scalar >
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:85
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::cellCellStencils::addToRunTimeSelectionTable
addToRunTimeSelectionTable(cellCellStencil, cellVolumeWeight, mesh)
Foam::Vector::y
const Cmpt & y() const
Access to the vector y component.
Definition: VectorI.H:79
Foam::cellCellStencils::leastSquares::stencilWeights
virtual void stencilWeights(const point &sample, const pointList &donorCcs, scalarList &weights) const
Calculate lsq weights for single acceptor.
Definition: leastSquaresCellCellStencil.C:46
Foam::Vector< scalar >
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::SVD
Singular value decomposition of a rectangular matrix.
Definition: SVD.H:53
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::cellCellStencils::defineTypeNameAndDebug
defineTypeNameAndDebug(cellVolumeWeight, 0)
leastSquaresCellCellStencil.H
sample
Minimal example by using system/controlDict.functions: