CentredFitSnGradData.C 7.28 KB
Newer Older
Henry's avatar
Henry committed
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
Henry's avatar
Henry committed
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
    Copyright (C) 2013-2016 OpenFOAM Foundation
Mark Olesen's avatar
Mark Olesen committed
9
    Copyright (C) 2020 OpenCFD Ltd.
Henry's avatar
Henry committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "CentredFitSnGradData.H"
#include "surfaceFields.H"
#include "SVD.H"
#include "extendedCentredCellToFaceStencil.H"

// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //

template<class Polynomial>
Foam::CentredFitSnGradData<Polynomial>::CentredFitSnGradData
(
    const fvMesh& mesh,
    const extendedCentredCellToFaceStencil& stencil,
    const scalar linearLimitFactor,
    const scalar centralWeight
)
:
    FitData
    <
        CentredFitSnGradData<Polynomial>,
        extendedCentredCellToFaceStencil,
        Polynomial
    >
    (
        mesh, stencil, true, linearLimitFactor, centralWeight
    ),
    coeffs_(mesh.nFaces())
{
Mark Olesen's avatar
Mark Olesen committed
56
    DebugInFunction
57
        << "Constructing CentredFitSnGradData<Polynomial>" << nl;
Henry's avatar
Henry committed
58 59 60

    calcFit();

Mark Olesen's avatar
Mark Olesen committed
61
    DebugInfo << "    Finished constructing polynomialFit data" << endl;
Henry's avatar
Henry committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

template<class Polynomial>
void Foam::CentredFitSnGradData<Polynomial>::calcFit
(
    scalarList& coeffsi,
    const List<point>& C,
    const scalar wLin,
    const scalar deltaCoeff,
    const label facei
)
{
    vector idir(1,0,0);
    vector jdir(0,1,0);
    vector kdir(0,0,1);
    this->findFaceDirs(idir, jdir, kdir, facei);

    // Setup the point weights
    scalarList wts(C.size(), scalar(1));
    wts[0] = this->centralWeight();
    wts[1] = this->centralWeight();

    // Reference point
    point p0 = this->mesh().faceCentres()[facei];

    // p0 -> p vector in the face-local coordinate system
    vector d;

    // Local coordinate scaling
    scalar scale = 1;

    // Matrix of the polynomial components
97
    scalarRectangularMatrix B(C.size(), this->minSize(), Zero);
Henry's avatar
Henry committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

    forAll(C, ip)
    {
        const point& p = C[ip];
        const vector p0p = p - p0;

        d.x() = p0p & idir;
        d.y() = p0p & jdir;
        d.z() = p0p & kdir;

        if (ip == 0)
        {
            scale = cmptMax(cmptMag((d)));
        }

        // Scale the radius vector
        d /= scale;

        Polynomial::addCoeffs(B[ip], d, wts[ip], this->dim());
    }

    // Additional weighting for constant and linear terms
120
    for (label i = 0; i < B.m(); i++)
Henry's avatar
Henry committed
121
    {
122 123
        B(i, 0) *= wts[0];
        B(i, 1) *= wts[0];
Henry's avatar
Henry committed
124 125 126 127 128 129 130 131 132 133
    }

    // Set the fit
    label stencilSize = C.size();
    coeffsi.setSize(stencilSize);

    bool goodFit = false;
    for (int iIt = 0; iIt < 8 && !goodFit; iIt++)
    {
        SVD svd(B, SMALL);
134
        scalarRectangularMatrix invB(svd.VSinvUt());
Henry's avatar
Henry committed
135 136 137

        for (label i=0; i<stencilSize; i++)
        {
138
            coeffsi[i] = wts[1]*wts[i]*invB(1, i)/scale;
Henry's avatar
Henry committed
139 140 141 142
        }

        goodFit =
        (
143
            mag(wts[0]*wts[0]*invB(0, 0) - wLin)
Henry's avatar
Henry committed
144
          < this->linearLimitFactor()*wLin)
145
         && (mag(wts[0]*wts[1]*invB(0, 1) - (1 - wLin)
Henry's avatar
Henry committed
146 147 148 149 150 151 152 153 154 155
        ) < this->linearLimitFactor()*(1 - wLin))
         && coeffsi[0] < 0 && coeffsi[1] > 0
         && mag(coeffsi[0] + deltaCoeff) < 0.5*deltaCoeff
         && mag(coeffsi[1] - deltaCoeff) < 0.5*deltaCoeff;

        if (!goodFit)
        {
            // (not good fit so increase weight in the centre and weight
            //  for constant and linear terms)

156 157
            WarningInFunction
                << "Cannot fit face " << facei << " iteration " << iIt
Henry's avatar
Henry committed
158 159 160 161 162 163
                << " with sum of weights " << sum(coeffsi) << nl
                << "    Weights " << coeffsi << nl
                << "    Linear weights " << wLin << " " << 1 - wLin << nl
                << "    deltaCoeff " << deltaCoeff << nl
                << "    sing vals " << svd.S() << nl
                << "Components of goodFit:\n"
164 165 166 167
                << "    wts[0]*wts[0]*invB(0, 0) = "
                << wts[0]*wts[0]*invB(0, 0) << nl
                << "    wts[0]*wts[1]*invB(0, 1) = "
                << wts[0]*wts[1]*invB(0, 1)
Henry's avatar
Henry committed
168 169 170 171 172
                << " dim = " << this->dim() << endl;

            wts[0] *= 10;
            wts[1] *= 10;

173
            for (label j = 0; j < B.n(); j++)
Henry's avatar
Henry committed
174
            {
175 176
                B(0, j) *= 10;
                B(1, j) *= 10;
Henry's avatar
Henry committed
177 178
            }

179
            for (label i = 0; i < B.m(); i++)
Henry's avatar
Henry committed
180
            {
181 182
                B(i, 0) *= 10;
                B(i, 1) *= 10;
Henry's avatar
Henry committed
183 184 185 186 187 188 189 190 191 192 193 194
            }
        }
    }

    if (goodFit)
    {
        // Remove the uncorrected coefficients
        coeffsi[0] += deltaCoeff;
        coeffsi[1] -= deltaCoeff;
    }
    else
    {
195 196
        WarningInFunction
            << "Could not fit face " << facei
Henry's avatar
Henry committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
            << "    Coefficients = " << coeffsi
            << ", reverting to uncorrected." << endl;

        coeffsi = 0;
    }
}


template<class Polynomial>
void Foam::CentredFitSnGradData<Polynomial>::calcFit()
{
    const fvMesh& mesh = this->mesh();

    // Get the cell/face centres in stencil order.
    // Centred face stencils no good for triangles or tets.
    // Need bigger stencils
213
    List<List<point>> stencilPoints(mesh.nFaces());
Henry's avatar
Henry committed
214 215 216 217 218
    this->stencil().collectData(mesh.C(), stencilPoints);

    // find the fit coefficients for every face in the mesh

    const surfaceScalarField& w = mesh.surfaceInterpolation::weights();
219
    const surfaceScalarField& dC = mesh.nonOrthDeltaCoeffs();
Henry's avatar
Henry committed
220 221 222 223 224 225 226 227 228 229 230 231 232

    for (label facei = 0; facei < mesh.nInternalFaces(); facei++)
    {
        calcFit
        (
            coeffs_[facei],
            stencilPoints[facei],
            w[facei],
            dC[facei],
            facei
        );
    }

233 234
    const surfaceScalarField::Boundary& bw = w.boundaryField();
    const surfaceScalarField::Boundary& bdC = dC.boundaryField();
Henry's avatar
Henry committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

    forAll(bw, patchi)
    {
        const fvsPatchScalarField& pw = bw[patchi];
        const fvsPatchScalarField& pdC = bdC[patchi];

        if (pw.coupled())
        {
            label facei = pw.patch().start();

            forAll(pw, i)
            {
                calcFit
                (
                    coeffs_[facei],
                    stencilPoints[facei],
                    pw[i],
                    pdC[i],
                    facei
                );
                facei++;
            }
        }
    }
}


// ************************************************************************* //