BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / sampling / sampledSurface / distanceSurface / distanceSurface.C
blob6b557df91844b2fce50573d970ddc6e85b496016
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "distanceSurface.H"
27 #include "dictionary.H"
28 #include "volFields.H"
29 #include "volPointInterpolation.H"
30 #include "addToRunTimeSelectionTable.H"
31 #include "fvMesh.H"
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 namespace Foam
37     defineTypeNameAndDebug(distanceSurface, 0);
38     addToRunTimeSelectionTable(sampledSurface, distanceSurface, word);
41 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
43 void Foam::distanceSurface::createGeometry()
45     if (debug)
46     {
47         Pout<< "distanceSurface::createGeometry :updating geometry." << endl;
48     }
50     // Clear any stored topologies
51     facesPtr_.clear();
53     // Clear derived data
54     clearGeom();
56     const fvMesh& fvm = static_cast<const fvMesh&>(mesh());
58     // Distance to cell centres
59     // ~~~~~~~~~~~~~~~~~~~~~~~~
61     cellDistancePtr_.reset
62     (
63         new volScalarField
64         (
65             IOobject
66             (
67                 "cellDistance",
68                 fvm.time().timeName(),
69                 fvm.time(),
70                 IOobject::NO_READ,
71                 IOobject::NO_WRITE,
72                 false
73             ),
74             fvm,
75             dimensionedScalar("zero", dimLength, 0)
76         )
77     );
78     volScalarField& cellDistance = cellDistancePtr_();
80     // Internal field
81     {
82         const pointField& cc = fvm.C();
83         scalarField& fld = cellDistance.internalField();
85         List<pointIndexHit> nearest;
86         surfPtr_().findNearest
87         (
88             cc,
89             scalarField(cc.size(), GREAT),
90             nearest
91         );
93         if (signed_)
94         {
95             List<searchableSurface::volumeType> volType;
97             surfPtr_().getVolumeType(cc, volType);
99             forAll(volType, i)
100             {
101                 searchableSurface::volumeType vT = volType[i];
103                 if (vT == searchableSurface::OUTSIDE)
104                 {
105                     fld[i] = Foam::mag(cc[i] - nearest[i].hitPoint());
106                 }
107                 else if (vT == searchableSurface::INSIDE)
108                 {
109                     fld[i] = -Foam::mag(cc[i] - nearest[i].hitPoint());
110                 }
111                 else
112                 {
113                     FatalErrorIn
114                     (
115                         "void Foam::distanceSurface::createGeometry()"
116                     )   << "getVolumeType failure, neither INSIDE or OUTSIDE"
117                         << exit(FatalError);
118                 }
119             }
120         }
121         else
122         {
123             forAll(nearest, i)
124             {
125                 fld[i] = Foam::mag(cc[i] - nearest[i].hitPoint());
126             }
127         }
128     }
130     // Patch fields
131     {
132         forAll(fvm.C().boundaryField(), patchI)
133         {
134             const pointField& cc = fvm.C().boundaryField()[patchI];
135             fvPatchScalarField& fld = cellDistance.boundaryField()[patchI];
137             List<pointIndexHit> nearest;
138             surfPtr_().findNearest
139             (
140                 cc,
141                 scalarField(cc.size(), GREAT),
142                 nearest
143             );
145             if (signed_)
146             {
147                 List<searchableSurface::volumeType> volType;
149                 surfPtr_().getVolumeType(cc, volType);
151                 forAll(volType, i)
152                 {
153                     searchableSurface::volumeType vT = volType[i];
155                     if (vT == searchableSurface::OUTSIDE)
156                     {
157                         fld[i] = Foam::mag(cc[i] - nearest[i].hitPoint());
158                     }
159                     else if (vT == searchableSurface::INSIDE)
160                     {
161                         fld[i] = -Foam::mag(cc[i] - nearest[i].hitPoint());
162                     }
163                     else
164                     {
165                         FatalErrorIn
166                         (
167                             "void Foam::distanceSurface::createGeometry()"
168                         )   << "getVolumeType failure, "
169                             << "neither INSIDE or OUTSIDE"
170                             << exit(FatalError);
171                     }
172                 }
173             }
174             else
175             {
176                 forAll(nearest, i)
177                 {
178                     fld[i] = Foam::mag(cc[i] - nearest[i].hitPoint());
179                 }
180             }
181         }
182     }
185     // On processor patches the mesh.C() will already be the cell centre
186     // on the opposite side so no need to swap cellDistance.
189     // Distance to points
190     pointDistance_.setSize(fvm.nPoints());
191     {
192         const pointField& pts = fvm.points();
194         List<pointIndexHit> nearest;
195         surfPtr_().findNearest
196         (
197             pts,
198             scalarField(pts.size(), GREAT),
199             nearest
200         );
202         if (signed_)
203         {
204             List<searchableSurface::volumeType> volType;
206             surfPtr_().getVolumeType(pts, volType);
208             forAll(volType, i)
209             {
210                 searchableSurface::volumeType vT = volType[i];
212                 if (vT == searchableSurface::OUTSIDE)
213                 {
214                     pointDistance_[i] =
215                         Foam::mag(pts[i] - nearest[i].hitPoint());
216                 }
217                 else if (vT == searchableSurface::INSIDE)
218                 {
219                     pointDistance_[i] =
220                         -Foam::mag(pts[i] - nearest[i].hitPoint());
221                 }
222                 else
223                 {
224                     FatalErrorIn
225                     (
226                         "void Foam::distanceSurface::createGeometry()"
227                     )   << "getVolumeType failure, neither INSIDE or OUTSIDE"
228                         << exit(FatalError);
229                 }
230             }
231         }
232         else
233         {
234             forAll(nearest, i)
235             {
236                 pointDistance_[i] = Foam::mag(pts[i]-nearest[i].hitPoint());
237             }
238         }
239     }
242     if (debug)
243     {
244         Pout<< "Writing cell distance:" << cellDistance.objectPath() << endl;
245         cellDistance.write();
246         pointScalarField pDist
247         (
248             IOobject
249             (
250                 "pointDistance",
251                 fvm.time().timeName(),
252                 fvm.time(),
253                 IOobject::NO_READ,
254                 IOobject::NO_WRITE,
255                 false
256             ),
257             pointMesh::New(fvm),
258             dimensionedScalar("zero", dimLength, 0)
259         );
260         pDist.internalField() = pointDistance_;
262         Pout<< "Writing point distance:" << pDist.objectPath() << endl;
263         pDist.write();
264     }
267     //- Direct from cell field and point field.
268     isoSurfPtr_.reset
269     (
270         new isoSurface
271         (
272             cellDistance,
273             pointDistance_,
274             distance_,
275             regularise_
276         )
277         //new isoSurfaceCell
278         //(
279         //    fvm,
280         //    cellDistance,
281         //    pointDistance_,
282         //    distance_,
283         //    regularise_
284         //)
285     );
287     if (debug)
288     {
289         print(Pout);
290         Pout<< endl;
291     }
295 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
297 Foam::distanceSurface::distanceSurface
299     const word& name,
300     const polyMesh& mesh,
301     const dictionary& dict
304     sampledSurface(name, mesh, dict),
305     surfPtr_
306     (
307         searchableSurface::New
308         (
309             dict.lookup("surfaceType"),
310             IOobject
311             (
312                 dict.lookupOrDefault("surfaceName", name),  // name
313                 mesh.time().constant(),                     // directory
314                 "triSurface",                               // instance
315                 mesh.time(),                                // registry
316                 IOobject::MUST_READ,
317                 IOobject::NO_WRITE
318             ),
319             dict
320         )
321     ),
322     distance_(readScalar(dict.lookup("distance"))),
323     signed_(readBool(dict.lookup("signed"))),
324     regularise_(dict.lookupOrDefault("regularise", true)),
325     average_(dict.lookupOrDefault("average", false)),
326     zoneKey_(keyType::null),
327     needsUpdate_(true),
328     isoSurfPtr_(NULL),
329     facesPtr_(NULL)
331 //    dict.readIfPresent("zone", zoneKey_);
333 //    if (debug && zoneKey_.size() && mesh.cellZones().findZoneID(zoneKey_) < 0)
334 //    {
335 //        Info<< "cellZone " << zoneKey_
336 //            << " not found - using entire mesh" << endl;
337 //    }
341 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
343 Foam::distanceSurface::~distanceSurface()
347 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
349 bool Foam::distanceSurface::needsUpdate() const
351     return needsUpdate_;
355 bool Foam::distanceSurface::expire()
357     if (debug)
358     {
359         Pout<< "distanceSurface::expire :"
360             << " have-facesPtr_:" << facesPtr_.valid()
361             << " needsUpdate_:" << needsUpdate_ << endl;
362     }
364     // Clear any stored topologies
365     facesPtr_.clear();
367     // Clear derived data
368     clearGeom();
370     // already marked as expired
371     if (needsUpdate_)
372     {
373         return false;
374     }
376     needsUpdate_ = true;
377     return true;
381 bool Foam::distanceSurface::update()
383     if (debug)
384     {
385         Pout<< "distanceSurface::update :"
386             << " have-facesPtr_:" << facesPtr_.valid()
387             << " needsUpdate_:" << needsUpdate_ << endl;
388     }
390     if (!needsUpdate_)
391     {
392         return false;
393     }
395     createGeometry();
397     needsUpdate_ = false;
398     return true;
402 Foam::tmp<Foam::scalarField> Foam::distanceSurface::sample
404     const volScalarField& vField
405 ) const
407     return sampleField(vField);
411 Foam::tmp<Foam::vectorField> Foam::distanceSurface::sample
413     const volVectorField& vField
414 ) const
416     return sampleField(vField);
420 Foam::tmp<Foam::sphericalTensorField> Foam::distanceSurface::sample
422     const volSphericalTensorField& vField
423 ) const
425     return sampleField(vField);
429 Foam::tmp<Foam::symmTensorField> Foam::distanceSurface::sample
431     const volSymmTensorField& vField
432 ) const
434     return sampleField(vField);
438 Foam::tmp<Foam::tensorField> Foam::distanceSurface::sample
440     const volTensorField& vField
441 ) const
443     return sampleField(vField);
447 Foam::tmp<Foam::scalarField> Foam::distanceSurface::interpolate
449     const interpolation<scalar>& interpolator
450 ) const
452     return interpolateField(interpolator);
456 Foam::tmp<Foam::vectorField> Foam::distanceSurface::interpolate
458     const interpolation<vector>& interpolator
459 ) const
461     return interpolateField(interpolator);
464 Foam::tmp<Foam::sphericalTensorField> Foam::distanceSurface::interpolate
466     const interpolation<sphericalTensor>& interpolator
467 ) const
469     return interpolateField(interpolator);
473 Foam::tmp<Foam::symmTensorField> Foam::distanceSurface::interpolate
475     const interpolation<symmTensor>& interpolator
476 ) const
478     return interpolateField(interpolator);
482 Foam::tmp<Foam::tensorField> Foam::distanceSurface::interpolate
484     const interpolation<tensor>& interpolator
485 ) const
487     return interpolateField(interpolator);
491 void Foam::distanceSurface::print(Ostream& os) const
493     os  << "distanceSurface: " << name() << " :"
494         << "  surface:" << surfPtr_().name()
495         << "  distance:" << distance_
496         << "  faces:" << faces().size()
497         << "  points:" << points().size();
501 // ************************************************************************* //