initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / finiteVolume / fvMesh / fvMeshMapper / fvPatchMapper.C
blob22642a68ad8138ccc95e15a4c8a7cfffb301e028
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "fvPatchMapper.H"
28 #include "fvPatch.H"
29 #include "fvBoundaryMesh.H"
30 #include "fvMesh.H"
31 #include "mapPolyMesh.H"
32 #include "faceMapper.H"
34 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
36 void Foam::fvPatchMapper::calcAddressing() const
38     if
39     (
40         directAddrPtr_
41      || interpolationAddrPtr_
42      || weightsPtr_
43     )
44     {
45         FatalErrorIn
46         (
47             "void fvPatchMapper::calcAddressing() const)"
48         )   << "Addressing already calculated"
49             << abort(FatalError);
50     }
52     // Mapping
53     const label oldPatchStart =
54         faceMap_.oldPatchStarts()[patch_.index()];
56     const label oldPatchEnd =
57         oldPatchStart + faceMap_.oldPatchSizes()[patch_.index()];
59     // Assemble the maps: slice to patch
60     if (direct())
61     {
62         // Direct mapping - slice to size
63         directAddrPtr_ = new labelList
64         (
65             patch_.patchSlice
66             (
67                 static_cast<const labelList&>(faceMap_.directAddressing())
68             )
69         );
70         labelList& addr = *directAddrPtr_;
72         // Adjust mapping to manage hits into other patches and into
73         // internal
74         forAll (addr, faceI)
75         {
76             if
77             (
78                 addr[faceI] >= oldPatchStart
79              && addr[faceI] < oldPatchEnd
80             )
81             {
82                 addr[faceI] -= oldPatchStart;
83             }
84             else
85             {
86                 addr[faceI] = 0;
87             }
88         }
90         if (fvMesh::debug)
91         {
92             if (min(addr) < 0)
93             {
94                 FatalErrorIn
95                 (
96                     "void fvPatchMapper::calcAddressing() const"
97                 )   << "Error in patch mapping for patch "
98                     << patch_.index() << " named " << patch_.name()
99                     << abort(FatalError);
100             }
101         }
102     }
103     else
104     {
105         // Interpolative mapping
106         interpolationAddrPtr_ =
107             new labelListList
108             (
109                 patch_.patchSlice(faceMap_.addressing())
110             );
111         labelListList& addr = *interpolationAddrPtr_;
113         weightsPtr_ =
114             new scalarListList
115             (
116                 patch_.patchSlice(faceMap_.weights())
117             );
118         scalarListList& w = *weightsPtr_;
120         // Adjust mapping to manage hits into other patches and into
121         // internal
122         forAll (addr, faceI)
123         {
124             labelList& curAddr = addr[faceI];
125             scalarList& curW = w[faceI];
127             if
128             (
129                 min(curAddr) >= oldPatchStart
130              && max(curAddr) < oldPatchEnd
131             )
132             {
133                 // No adjustment of weights, just subtract patch start
134                 forAll (curAddr, i)
135                 {
136                     curAddr[i] -= oldPatchStart;
137                 }
138             }
139             else
140             {
141                 // Need to recalculate weights to exclude hits into internal
142                 labelList newAddr(curAddr.size(), false);
143                 scalarField newWeights(curAddr.size());
144                 label nActive = 0;
146                 forAll (curAddr, lfI)
147                 {
148                     if
149                     (
150                         curAddr[lfI] >= oldPatchStart
151                      && curAddr[lfI] < oldPatchEnd
152                     )
153                     {
154                         newAddr[nActive] = curAddr[lfI] - oldPatchStart;
155                         newWeights[nActive] = curW[lfI];
156                         nActive++;
157                     }
158                 }
160                 // Cater for bad mapping
161                 if (nActive == 0)
162                 {
163                     newAddr[nActive] = 0;
164                     newWeights[nActive] = 1;
165                     nActive++;
166                 }
168                 newAddr.setSize(nActive);
169                 newWeights.setSize(nActive);
171                 // Re-scale the weights
172                 newWeights /= sum(newWeights);
174                 // Reset addressing and weights
175                 curAddr = newAddr;
176                 curW = newWeights;
177             }
178         }
180         if (fvMesh::debug)
181         {
182             forAll (addr, i)
183             {
184                 if (min(addr[i]) < 0)
185                 {
186                     FatalErrorIn
187                     (
188                         "void fvPatchMapper::calcAddressing() const"
189                     )   << "Error in patch mapping for patch "
190                         << patch_.index() << " named " << patch_.name()
191                         << abort(FatalError);
192                 }
193             }
194         }
195     }
199 void Foam::fvPatchMapper::clearOut()
201     deleteDemandDrivenData(directAddrPtr_);
202     deleteDemandDrivenData(interpolationAddrPtr_);
203     deleteDemandDrivenData(weightsPtr_);
207 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
209 // Construct from components
210 Foam::fvPatchMapper::fvPatchMapper
212     const fvPatch& patch,
213     const faceMapper& faceMap
216     patch_(patch),
217     faceMap_(faceMap),
218     sizeBeforeMapping_(faceMap.oldPatchSizes()[patch_.index()]),
219     directAddrPtr_(NULL),
220     interpolationAddrPtr_(NULL),
221     weightsPtr_(NULL)
225 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
227 Foam::fvPatchMapper::~fvPatchMapper()
229     clearOut();
233 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
235 const Foam::unallocLabelList& Foam::fvPatchMapper::directAddressing() const
237     if (!direct())
238     {
239         FatalErrorIn
240         (
241             "const unallocLabelList& fvPatchMapper::directAddressing() const"
242         )   << "Requested direct addressing for an interpolative mapper."
243             << abort(FatalError);
244     }
246     if (!directAddrPtr_)
247     {
248         calcAddressing();
249     }
251     return *directAddrPtr_;
255 const Foam::labelListList& Foam::fvPatchMapper::addressing() const
257     if (direct())
258     {
259         FatalErrorIn
260         (
261             "const labelListList& fvPatchMapper::addressing() const"
262         )   << "Requested interpolative addressing for a direct mapper."
263             << abort(FatalError);
264     }
266     if (!interpolationAddrPtr_)
267     {
268         calcAddressing();
269     }
271     return *interpolationAddrPtr_;
275 const Foam::scalarListList& Foam::fvPatchMapper::weights() const
277     if (direct())
278     {
279         FatalErrorIn
280         (
281             "const scalarListList& fvPatchMapper::weights() const"
282         )   << "Requested interpolative weights for a direct mapper."
283             << abort(FatalError);
284     }
286     if (!weightsPtr_)
287     {
288         calcAddressing();
289     }
291     return *weightsPtr_;
295 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
298 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
301 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
304 // ************************************************************************* //