Forward compatibility: flex
[foam-extend-3.2.git] / src / sampling / meshToMeshInterpolation / meshToMesh / calculateMeshToMeshWeights.C
blobcca38e057add76467e75c36f175a453402422e60
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "meshToMesh.H"
28 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
30 namespace Foam
33 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
35 void meshToMesh::calculateInverseDistanceWeights() const
37     if (debug)
38     {
39         Info<< "meshToMesh::calculateInverseDistanceWeights() : "
40             << "calculating inverse distance weighting factors" << endl;
41     }
43     if (inverseDistanceWeightsPtr_)
44     {
45         FatalErrorIn("meshToMesh::calculateInverseDistanceWeights()")
46             << "weighting factors already calculated"
47             << exit(FatalError);
48     }
50     inverseDistanceWeightsPtr_ = new scalarListList(toMesh_.nCells());
51     scalarListList& invDistCoeffs = *inverseDistanceWeightsPtr_;
53     // get reference to source mesh data
54     const labelListList& cc = fromMesh_.cellCells();
55     const vectorField& centreFrom = fromMesh_.C().internalField();
56     const vectorField& centreTo = toMesh_.C().internalField();
58     forAll (cellAddressing_, celli)
59     {
60         if (cellAddressing_[celli] != -1)
61         {
62             const vector& target = centreTo[celli];
63             scalar m = mag(target - centreFrom[cellAddressing_[celli]]);
65             const labelList& neighbours = cc[cellAddressing_[celli]];
67             // if the nearest cell is a boundary cell or there is a direct hit,
68             // pick up the value
69             if
70             (
71                 m < directHitTol()                            // Direct hit
72              || neighbours.empty()
73             )
74             {
75                 invDistCoeffs[celli].setSize(1);
76                 invDistCoeffs[celli][0] = 1.0;
77             }
78             else
79             {
80                 invDistCoeffs[celli].setSize(neighbours.size() + 1);
82                 // The first coefficient corresponds to the centre cell.
83                 // The rest is ordered in the same way as the cellCells list.
84                 scalar invDist = 1.0/m;
85                 invDistCoeffs[celli][0] = invDist;
86                 scalar sumInvDist = invDist;
88                 // now add the neighbours
89                 forAll (neighbours, ni)
90                 {
91                     invDist = 1.0/mag(target - centreFrom[neighbours[ni]]);
92                     invDistCoeffs[celli][ni + 1] = invDist;
93                     sumInvDist += invDist;
94                 }
96                 // divide by the total inverse-distance
97                 forAll (invDistCoeffs[celli], i)
98                 {
99                     invDistCoeffs[celli][i] /= sumInvDist;
100                 }
101             }
102         }
103     }
107 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
109 const scalarListList& meshToMesh::inverseDistanceWeights() const
111     if (!inverseDistanceWeightsPtr_)
112     {
113         calculateInverseDistanceWeights();
114     }
116     return *inverseDistanceWeightsPtr_;
120 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
122 } // End namespace Foam
124 // ************************************************************************* //