Merge /u/wyldckat/foam-extend32/ branch master into master
[foam-extend-3.2.git] / src / sampling / sampledSurface / isoSurface / sampledIsoSurfaceCell.C
blob217fe23252b4f98aab0b9867e049673411691f1d
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 "sampledIsoSurfaceCell.H"
27 #include "dictionary.H"
28 #include "volFields.H"
29 #include "volPointInterpolation.H"
30 #include "addToRunTimeSelectionTable.H"
31 #include "fvMesh.H"
32 #include "isoSurfaceCell.H"
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 namespace Foam
38     defineTypeNameAndDebug(sampledIsoSurfaceCell, 0);
39     addNamedToRunTimeSelectionTable(sampledSurface, sampledIsoSurfaceCell, word, isoSurfaceCell);
42 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
44 bool Foam::sampledIsoSurfaceCell::updateGeometry() const
46     const fvMesh& fvm = static_cast<const fvMesh&>(mesh());
48     // no update needed
49     if (fvm.time().timeIndex() == prevTimeIndex_)
50     {
51         return false;
52     }
54     prevTimeIndex_ = fvm.time().timeIndex();
56     // Clear any stored topo
57     facesPtr_.clear();
59     // Optionally read volScalarField
60     autoPtr<volScalarField> readFieldPtr_;
62     // 1. see if field in database
63     // 2. see if field can be read
64     const volScalarField* cellFldPtr = NULL;
65     if (fvm.foundObject<volScalarField>(isoField_))
66     {
67         if (debug)
68         {
69             Info<< "sampledIsoSurfaceCell::updateGeometry() : lookup "
70                 << isoField_ << endl;
71         }
73         cellFldPtr = &fvm.lookupObject<volScalarField>(isoField_);
74     }
75     else
76     {
77         // Bit of a hack. Read field and store.
79         if (debug)
80         {
81             Info<< "sampledIsoSurfaceCell::updateGeometry() : reading "
82                 << isoField_ << " from time " <<fvm.time().timeName()
83                 << endl;
84         }
86         readFieldPtr_.reset
87         (
88             new volScalarField
89             (
90                 IOobject
91                 (
92                     isoField_,
93                     fvm.time().timeName(),
94                     fvm,
95                     IOobject::MUST_READ,
96                     IOobject::NO_WRITE,
97                     false
98                 ),
99                 fvm
100             )
101         );
103         cellFldPtr = readFieldPtr_.operator->();
104     }
105     const volScalarField& cellFld = *cellFldPtr;
107     tmp<pointScalarField> pointFld
108     (
109         volPointInterpolation::New(fvm).interpolate(cellFld)
110     );
112     if (average_)
113     {
114         //- From point field and interpolated cell.
115         scalarField cellAvg(fvm.nCells(), scalar(0.0));
116         labelField nPointCells(fvm.nCells(), 0);
117         {
118             for (label pointI = 0; pointI < fvm.nPoints(); pointI++)
119             {
120                 const labelList& pCells = fvm.pointCells(pointI);
122                 forAll(pCells, i)
123                 {
124                     label cellI = pCells[i];
126                     cellAvg[cellI] += pointFld().internalField()[pointI];
127                     nPointCells[cellI]++;
128                 }
129             }
130         }
131         forAll(cellAvg, cellI)
132         {
133             cellAvg[cellI] /= nPointCells[cellI];
134         }
136         const isoSurfaceCell iso
137         (
138             fvm,
139             cellAvg,
140             pointFld().internalField(),
141             isoVal_,
142             regularise_
143         );
145         const_cast<sampledIsoSurfaceCell&>
146         (
147             *this
148         ).triSurface::operator=(iso);
149         meshCells_ = iso.meshCells();
150     }
151     else
152     {
153         //- Direct from cell field and point field. Gives bad continuity.
154         const isoSurfaceCell iso
155         (
156             fvm,
157             cellFld.internalField(),
158             pointFld().internalField(),
159             isoVal_,
160             regularise_
161         );
163         const_cast<sampledIsoSurfaceCell&>
164         (
165             *this
166         ).triSurface::operator=(iso);
167         meshCells_ = iso.meshCells();
168     }
171     if (debug)
172     {
173         Pout<< "sampledIsoSurfaceCell::updateGeometry() : constructed iso:"
174             << nl
175             << "    regularise     : " << regularise_ << nl
176             << "    average        : " << average_ << nl
177             << "    isoField       : " << isoField_ << nl
178             << "    isoValue       : " << isoVal_ << nl
179             << "    points         : " << points().size() << nl
180             << "    tris           : " << triSurface::size() << nl
181             << "    cut cells      : " << meshCells_.size() << endl;
182     }
184     return true;
188 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
190 Foam::sampledIsoSurfaceCell::sampledIsoSurfaceCell
192     const word& name,
193     const polyMesh& mesh,
194     const dictionary& dict
197     sampledSurface(name, mesh, dict),
198     isoField_(dict.lookup("isoField")),
199     isoVal_(readScalar(dict.lookup("isoValue"))),
200     regularise_(dict.lookupOrDefault("regularise", true)),
201     average_(dict.lookupOrDefault("average", true)),
202     zoneName_(word::null),
203     facesPtr_(NULL),
204     prevTimeIndex_(-1),
205     meshCells_(0)
207 //    dict.readIfPresent("zone", zoneName_);
209 //    if (debug && zoneName_.size())
210 //    {
211 //        if (mesh.cellZones().findZoneID(zoneName_) < 0)
212 //        {
213 //            Info<< "cellZone \"" << zoneName_
214 //                << "\" not found - using entire mesh" << endl;
215 //        }
216 //    }
220 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
222 Foam::sampledIsoSurfaceCell::~sampledIsoSurfaceCell()
226 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
228 bool Foam::sampledIsoSurfaceCell::needsUpdate() const
230     const fvMesh& fvm = static_cast<const fvMesh&>(mesh());
232     return fvm.time().timeIndex() != prevTimeIndex_;
236 bool Foam::sampledIsoSurfaceCell::expire()
238     facesPtr_.clear();
240     // already marked as expired
241     if (prevTimeIndex_ == -1)
242     {
243         return false;
244     }
246     // force update
247     prevTimeIndex_ = -1;
248     return true;
252 bool Foam::sampledIsoSurfaceCell::update()
254     return updateGeometry();
258 Foam::tmp<Foam::scalarField>
259 Foam::sampledIsoSurfaceCell::sample
261     const volScalarField& vField
262 ) const
264     return sampleField(vField);
268 Foam::tmp<Foam::vectorField>
269 Foam::sampledIsoSurfaceCell::sample
271     const volVectorField& vField
272 ) const
274     return sampleField(vField);
278 Foam::tmp<Foam::sphericalTensorField>
279 Foam::sampledIsoSurfaceCell::sample
281     const volSphericalTensorField& vField
282 ) const
284     return sampleField(vField);
288 Foam::tmp<Foam::symmTensorField>
289 Foam::sampledIsoSurfaceCell::sample
291     const volSymmTensorField& vField
292 ) const
294     return sampleField(vField);
298 Foam::tmp<Foam::tensorField>
299 Foam::sampledIsoSurfaceCell::sample
301     const volTensorField& vField
302 ) const
304     return sampleField(vField);
308 Foam::tmp<Foam::scalarField>
309 Foam::sampledIsoSurfaceCell::interpolate
311     const interpolation<scalar>& interpolator
312 ) const
314     return interpolateField(interpolator);
318 Foam::tmp<Foam::vectorField>
319 Foam::sampledIsoSurfaceCell::interpolate
321     const interpolation<vector>& interpolator
322 ) const
324     return interpolateField(interpolator);
327 Foam::tmp<Foam::sphericalTensorField>
328 Foam::sampledIsoSurfaceCell::interpolate
330     const interpolation<sphericalTensor>& interpolator
331 ) const
333     return interpolateField(interpolator);
337 Foam::tmp<Foam::symmTensorField>
338 Foam::sampledIsoSurfaceCell::interpolate
340     const interpolation<symmTensor>& interpolator
341 ) const
343     return interpolateField(interpolator);
347 Foam::tmp<Foam::tensorField>
348 Foam::sampledIsoSurfaceCell::interpolate
350     const interpolation<tensor>& interpolator
351 ) const
353     return interpolateField(interpolator);
357 void Foam::sampledIsoSurfaceCell::print(Ostream& os) const
359     os  << "sampledIsoSurfaceCell: " << name() << " :"
360         << "  field:" << isoField_
361         << "  value:" << isoVal_;
362         //<< "  faces:" << faces().size()   // possibly no geom yet
363         //<< "  points:" << points().size();
367 // ************************************************************************* //