OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

sampledIsoSurface.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "sampledIsoSurface.H"
00028 #include "dictionary.H"
00029 #include "volFields.H"
00030 #include "volPointInterpolation.H"
00031 #include "addToRunTimeSelectionTable.H"
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037     defineTypeNameAndDebug(sampledIsoSurface, 0);
00038     addNamedToRunTimeSelectionTable
00039     (
00040         sampledSurface,
00041         sampledIsoSurface,
00042         word,
00043         isoSurface
00044     );
00045 }
00046 
00047 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00048 
00049 void Foam::sampledIsoSurface::getIsoFields() const
00050 {
00051     const fvMesh& fvm = static_cast<const fvMesh&>(mesh());
00052 
00053     // Get volField
00054     // ~~~~~~~~~~~~
00055 
00056     if (fvm.foundObject<volScalarField>(isoField_))
00057     {
00058         if (debug)
00059         {
00060             Info<< "sampledIsoSurface::getIsoField() : lookup volField "
00061                 << isoField_ << endl;
00062         }
00063         storedVolFieldPtr_.clear();
00064         volFieldPtr_ = &fvm.lookupObject<volScalarField>(isoField_);
00065     }
00066     else
00067     {
00068         // Bit of a hack. Read field and store.
00069 
00070         if (debug)
00071         {
00072             Info<< "sampledIsoSurface::getIsoField() : checking "
00073                 << isoField_ << " for same time " << fvm.time().timeName()
00074                 << endl;
00075         }
00076 
00077         if
00078         (
00079             storedVolFieldPtr_.empty()
00080          || (fvm.time().timeName() != storedVolFieldPtr_().instance())
00081         )
00082         {
00083             if (debug)
00084             {
00085                 Info<< "sampledIsoSurface::getIsoField() : reading volField "
00086                     << isoField_ << " from time " << fvm.time().timeName()
00087                     << endl;
00088             }
00089 
00090             storedVolFieldPtr_.reset
00091             (
00092                 new volScalarField
00093                 (
00094                     IOobject
00095                     (
00096                         isoField_,
00097                         fvm.time().timeName(),
00098                         fvm,
00099                         IOobject::MUST_READ,
00100                         IOobject::NO_WRITE,
00101                         false
00102                     ),
00103                     fvm
00104                 )
00105             );
00106             volFieldPtr_ = storedVolFieldPtr_.operator->();
00107         }
00108     }
00109 
00110 
00111 
00112     // Get pointField
00113     // ~~~~~~~~~~~~~~
00114 
00115     if (!subMeshPtr_.valid())
00116     {
00117         word pointFldName = "volPointInterpolate(" + isoField_ + ')';
00118 
00119         if (fvm.foundObject<pointScalarField>(pointFldName))
00120         {
00121             if (debug)
00122             {
00123                 Info<< "sampledIsoSurface::getIsoField() : lookup pointField "
00124                     << pointFldName << endl;
00125             }
00126             pointFieldPtr_ = &fvm.lookupObject<pointScalarField>(pointFldName);
00127         }
00128         else
00129         {
00130             // Not in registry. Interpolate.
00131 
00132             if (debug)
00133             {
00134                 Info<< "sampledIsoSurface::getIsoField() : checking pointField "
00135                     << pointFldName << " for same time "
00136                     << fvm.time().timeName() << endl;
00137             }
00138 
00139             if
00140             (
00141                 storedPointFieldPtr_.empty()
00142              || (fvm.time().timeName() != storedPointFieldPtr_().instance())
00143             )
00144             {
00145                 if (debug)
00146                 {
00147                     Info<< "sampledIsoSurface::getIsoField() :"
00148                         << " interpolating volField " << volFieldPtr_->name()
00149                         << " to get pointField " << pointFldName << endl;
00150                 }
00151 
00152                 storedPointFieldPtr_.reset
00153                 (
00154                     volPointInterpolation::New(fvm)
00155                     .interpolate(*volFieldPtr_).ptr()
00156                 );
00157                 storedPointFieldPtr_->checkOut();
00158                 pointFieldPtr_ = storedPointFieldPtr_.operator->();
00159             }
00160         }
00161 
00162 
00163         // If averaging redo the volField. Can only be done now since needs the
00164         // point field.
00165         if (average_)
00166         {
00167             storedVolFieldPtr_.reset(average(fvm, *pointFieldPtr_).ptr());
00168             volFieldPtr_ = storedVolFieldPtr_.operator->();
00169         }
00170 
00171 
00172         if (debug)
00173         {
00174             Info<< "sampledIsoSurface::getIsoField() : volField "
00175                 << volFieldPtr_->name() << " min:" << min(*volFieldPtr_).value()
00176                 << " max:" << max(*volFieldPtr_).value() << endl;
00177             Info<< "sampledIsoSurface::getIsoField() : pointField "
00178                 << pointFieldPtr_->name()
00179                 << " min:" << gMin(pointFieldPtr_->internalField())
00180                 << " max:" << gMax(pointFieldPtr_->internalField()) << endl;
00181         }
00182     }
00183     else
00184     {
00185         // Get subMesh variants
00186         const fvMesh& subFvm = subMeshPtr_().subMesh();
00187 
00188         // Either lookup on the submesh or subset the whole-mesh volField
00189 
00190         if (subFvm.foundObject<volScalarField>(isoField_))
00191         {
00192             if (debug)
00193             {
00194                 Info<< "sampledIsoSurface::getIsoField() :"
00195                     << " submesh lookup volField "
00196                     << isoField_ << endl;
00197             }
00198             storedVolSubFieldPtr_.clear();
00199             volSubFieldPtr_ = &subFvm.lookupObject<volScalarField>(isoField_);
00200         }
00201         else
00202         {
00203             if (debug)
00204             {
00205                 Info<< "sampledIsoSurface::getIsoField() : subsetting volField "
00206                     << isoField_ << endl;
00207             }
00208             storedVolSubFieldPtr_.reset
00209             (
00210                 subMeshPtr_().interpolate
00211                 (
00212                     *volFieldPtr_
00213                 ).ptr()
00214             );
00215             storedVolSubFieldPtr_->checkOut();
00216             volSubFieldPtr_ = storedVolSubFieldPtr_.operator->();
00217         }
00218 
00219 
00220         // Pointfield on submesh
00221 
00222         word pointFldName =
00223             "volPointInterpolate("
00224           + volSubFieldPtr_->name()
00225           + ')';
00226 
00227         if (subFvm.foundObject<pointScalarField>(pointFldName))
00228         {
00229             if (debug)
00230             {
00231                 Info<< "sampledIsoSurface::getIsoField() :"
00232                     << " submesh lookup pointField " << pointFldName << endl;
00233             }
00234             storedPointSubFieldPtr_.clear();
00235             pointSubFieldPtr_ = &subFvm.lookupObject<pointScalarField>
00236             (
00237                 pointFldName
00238             );
00239         }
00240         else
00241         {
00242             if (debug)
00243             {
00244                 Info<< "sampledIsoSurface::getIsoField() :"
00245                     << " interpolating submesh volField "
00246                     << volSubFieldPtr_->name()
00247                     << " to get submesh pointField " << pointFldName << endl;
00248             }
00249             storedPointSubFieldPtr_.reset
00250             (
00251                 volPointInterpolation::New
00252                 (
00253                     subFvm
00254                 ).interpolate(*volSubFieldPtr_).ptr()
00255             );
00256             storedPointSubFieldPtr_->checkOut();
00257             pointSubFieldPtr_ = storedPointSubFieldPtr_.operator->();
00258         }
00259 
00260 
00261 
00262         // If averaging redo the volField. Can only be done now since needs the
00263         // point field.
00264         if (average_)
00265         {
00266             storedVolSubFieldPtr_.reset
00267             (
00268                 average(subFvm, *pointSubFieldPtr_).ptr()
00269             );
00270             volSubFieldPtr_ = storedVolSubFieldPtr_.operator->();
00271         }
00272 
00273 
00274         if (debug)
00275         {
00276             Info<< "sampledIsoSurface::getIsoField() : volSubField "
00277                 << volSubFieldPtr_->name()
00278                 << " min:" << min(*volSubFieldPtr_).value()
00279                 << " max:" << max(*volSubFieldPtr_).value() << endl;
00280             Info<< "sampledIsoSurface::getIsoField() : pointSubField "
00281                 << pointSubFieldPtr_->name()
00282                 << " min:" << gMin(pointSubFieldPtr_->internalField())
00283                 << " max:" << gMax(pointSubFieldPtr_->internalField()) << endl;
00284         }
00285     }
00286 }
00287 
00288 
00289 Foam::tmp<Foam::volScalarField> Foam::sampledIsoSurface::average
00290 (
00291     const fvMesh& mesh,
00292     const pointScalarField& pfld
00293 ) const
00294 {
00295     tmp<volScalarField> tcellAvg
00296     (
00297         new volScalarField
00298         (
00299             IOobject
00300             (
00301                 "cellAvg",
00302                 mesh.time().timeName(),
00303                 mesh,
00304                 IOobject::NO_READ,
00305                 IOobject::NO_WRITE,
00306                 false
00307             ),
00308             mesh,
00309             dimensionedScalar("zero", dimless, scalar(0.0))
00310         )
00311     );
00312     volScalarField& cellAvg = tcellAvg();
00313 
00314     labelField nPointCells(mesh.nCells(), 0);
00315     {
00316         for (label pointI = 0; pointI < mesh.nPoints(); pointI++)
00317         {
00318             const labelList& pCells = mesh.pointCells(pointI);
00319 
00320             forAll(pCells, i)
00321             {
00322                 label cellI = pCells[i];
00323 
00324                 cellAvg[cellI] += pfld[pointI];
00325                 nPointCells[cellI]++;
00326             }
00327         }
00328     }
00329     forAll(cellAvg, cellI)
00330     {
00331         cellAvg[cellI] /= nPointCells[cellI];
00332     }
00333     // Give value to calculatedFvPatchFields
00334     cellAvg.correctBoundaryConditions();
00335 
00336     return tcellAvg;
00337 }
00338 
00339 
00340 Foam::tmp<Foam::pointScalarField> Foam::sampledIsoSurface::average
00341 (
00342     const pointMesh& pMesh,
00343     const volScalarField& fld
00344 ) const
00345 {
00346     tmp<pointScalarField> tpointAvg
00347     (
00348         new pointScalarField
00349         (
00350             IOobject
00351             (
00352                 "pointAvg",
00353                 fld.time().timeName(),
00354                 fld.db(),
00355                 IOobject::NO_READ,
00356                 IOobject::NO_WRITE,
00357                 false
00358             ),
00359             pMesh,
00360             dimensionedScalar("zero", dimless, scalar(0.0))
00361         )
00362     );
00363     pointScalarField& pointAvg = tpointAvg();
00364 
00365     for (label pointI = 0; pointI < fld.mesh().nPoints(); pointI++)
00366     {
00367         const labelList& pCells = fld.mesh().pointCells(pointI);
00368 
00369         forAll(pCells, i)
00370         {
00371             pointAvg[pointI] += fld[pCells[i]];
00372         }
00373         pointAvg[pointI] /= pCells.size();
00374     }
00375     // Give value to calculatedFvPatchFields
00376     pointAvg.correctBoundaryConditions();
00377 
00378     return tpointAvg;
00379 }
00380 
00381 
00382 bool Foam::sampledIsoSurface::updateGeometry() const
00383 {
00384     const fvMesh& fvm = static_cast<const fvMesh&>(mesh());
00385 
00386     // no update needed
00387     if (fvm.time().timeIndex() == prevTimeIndex_)
00388     {
00389         return false;
00390     }
00391 
00392     // Get any subMesh
00393     if (zoneID_.index() != -1 && !subMeshPtr_.valid())
00394     {
00395         const polyBoundaryMesh& patches = mesh().boundaryMesh();
00396 
00397         // Patch to put exposed internal faces into
00398         label exposedPatchI = patches.findPatchID(exposedPatchName_);
00399 
00400         if (debug)
00401         {
00402             Info<< "Allocating subset of size "
00403                 << mesh().cellZones()[zoneID_.index()].size()
00404                 << " with exposed faces into patch "
00405                 << patches[exposedPatchI].name() << endl;
00406         }
00407 
00408         subMeshPtr_.reset
00409         (
00410             new fvMeshSubset(fvm)
00411         );
00412         subMeshPtr_().setLargeCellSubset
00413         (
00414             labelHashSet(mesh().cellZones()[zoneID_.index()]),
00415             exposedPatchI
00416         );
00417     }
00418 
00419 
00420     prevTimeIndex_ = fvm.time().timeIndex();
00421     getIsoFields();
00422 
00423     // Clear any stored topo
00424     surfPtr_.clear();
00425     facesPtr_.clear();
00426 
00427     if (subMeshPtr_.valid())
00428     {
00429         surfPtr_.reset
00430         (
00431             new isoSurface
00432             (
00433                 *volSubFieldPtr_,
00434                 *pointSubFieldPtr_,
00435                 isoVal_,
00436                 regularise_,
00437                 mergeTol_
00438             )
00439         );
00440     }
00441     else
00442     {
00443         surfPtr_.reset
00444         (
00445             new isoSurface
00446             (
00447                 *volFieldPtr_,
00448                 *pointFieldPtr_,
00449                 isoVal_,
00450                 regularise_,
00451                 mergeTol_
00452             )
00453         );
00454     }
00455 
00456 
00457     if (debug)
00458     {
00459         Pout<< "sampledIsoSurface::updateGeometry() : constructed iso:"
00460             << nl
00461             << "    regularise     : " << regularise_ << nl
00462             << "    average        : " << average_ << nl
00463             << "    isoField       : " << isoField_ << nl
00464             << "    isoValue       : " << isoVal_ << nl;
00465         if (subMeshPtr_.valid())
00466         {
00467             Pout<< "    zone size      : " << subMeshPtr_().subMesh().nCells()
00468                 << nl;
00469         }
00470         Pout<< "    points         : " << points().size() << nl
00471             << "    tris           : " << surface().size() << nl
00472             << "    cut cells      : " << surface().meshCells().size()
00473             << endl;
00474     }
00475 
00476     return true;
00477 }
00478 
00479 
00480 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00481 
00482 Foam::sampledIsoSurface::sampledIsoSurface
00483 (
00484     const word& name,
00485     const polyMesh& mesh,
00486     const dictionary& dict
00487 )
00488 :
00489     sampledSurface(name, mesh, dict),
00490     isoField_(dict.lookup("isoField")),
00491     isoVal_(readScalar(dict.lookup("isoValue"))),
00492     mergeTol_(dict.lookupOrDefault("mergeTol", 1E-6)),
00493     regularise_(dict.lookupOrDefault("regularise", true)),
00494     average_(dict.lookupOrDefault("average", false)),
00495     zoneID_(dict.lookupOrDefault("zone", word::null), mesh.cellZones()),
00496     exposedPatchName_(word::null),
00497     surfPtr_(NULL),
00498     facesPtr_(NULL),
00499     prevTimeIndex_(-1),
00500     storedVolFieldPtr_(NULL),
00501     volFieldPtr_(NULL),
00502     storedPointFieldPtr_(NULL),
00503     pointFieldPtr_(NULL)
00504 {
00505     if (!sampledSurface::interpolate())
00506     {
00507         FatalIOErrorIn
00508         (
00509             "sampledIsoSurface::sampledIsoSurface"
00510             "(const word&, const polyMesh&, const dictionary&)",
00511             dict
00512         )   << "Non-interpolated iso surface not supported since triangles"
00513             << " span across cells." << exit(FatalIOError);
00514     }
00515 
00516     if (zoneID_.index() != -1)
00517     {
00518         dict.lookup("exposedPatchName") >> exposedPatchName_;
00519 
00520         if (mesh.boundaryMesh().findPatchID(exposedPatchName_) == -1)
00521         {
00522             FatalIOErrorIn
00523             (
00524                 "sampledIsoSurface::sampledIsoSurface"
00525                 "(const word&, const polyMesh&, const dictionary&)",
00526                 dict
00527             )   << "Cannot find patch " << exposedPatchName_
00528                 << " in which to put exposed faces." << endl
00529                 << "Valid patches are " << mesh.boundaryMesh().names()
00530                 << exit(FatalIOError);
00531         }
00532 
00533         if (debug && zoneID_.index() != -1)
00534         {
00535             Info<< "Restricting to cellZone " << zoneID_.name()
00536                 << " with exposed internal faces into patch "
00537                 << exposedPatchName_ << endl;
00538         }
00539     }
00540 }
00541 
00542 
00543 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00544 
00545 Foam::sampledIsoSurface::~sampledIsoSurface()
00546 {}
00547 
00548 
00549 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00550 
00551 bool Foam::sampledIsoSurface::needsUpdate() const
00552 {
00553     const fvMesh& fvm = static_cast<const fvMesh&>(mesh());
00554 
00555     return fvm.time().timeIndex() != prevTimeIndex_;
00556 }
00557 
00558 
00559 bool Foam::sampledIsoSurface::expire()
00560 {
00561     surfPtr_.clear();
00562     facesPtr_.clear();
00563     subMeshPtr_.clear();
00564 
00565     // already marked as expired
00566     if (prevTimeIndex_ == -1)
00567     {
00568         return false;
00569     }
00570 
00571     // force update
00572     prevTimeIndex_ = -1;
00573     return true;
00574 }
00575 
00576 
00577 bool Foam::sampledIsoSurface::update()
00578 {
00579     return updateGeometry();
00580 }
00581 
00582 
00583 Foam::tmp<Foam::scalarField> Foam::sampledIsoSurface::sample
00584 (
00585     const volScalarField& vField
00586 ) const
00587 {
00588     return sampleField(vField);
00589 }
00590 
00591 
00592 Foam::tmp<Foam::vectorField> Foam::sampledIsoSurface::sample
00593 (
00594     const volVectorField& vField
00595 ) const
00596 {
00597     return sampleField(vField);
00598 }
00599 
00600 
00601 Foam::tmp<Foam::sphericalTensorField> Foam::sampledIsoSurface::sample
00602 (
00603     const volSphericalTensorField& vField
00604 ) const
00605 {
00606     return sampleField(vField);
00607 }
00608 
00609 
00610 Foam::tmp<Foam::symmTensorField> Foam::sampledIsoSurface::sample
00611 (
00612     const volSymmTensorField& vField
00613 ) const
00614 {
00615     return sampleField(vField);
00616 }
00617 
00618 
00619 Foam::tmp<Foam::tensorField> Foam::sampledIsoSurface::sample
00620 (
00621     const volTensorField& vField
00622 ) const
00623 {
00624     return sampleField(vField);
00625 }
00626 
00627 
00628 Foam::tmp<Foam::scalarField> Foam::sampledIsoSurface::interpolate
00629 (
00630     const interpolation<scalar>& interpolator
00631 ) const
00632 {
00633     return interpolateField(interpolator);
00634 }
00635 
00636 
00637 Foam::tmp<Foam::vectorField> Foam::sampledIsoSurface::interpolate
00638 (
00639     const interpolation<vector>& interpolator
00640 ) const
00641 {
00642     return interpolateField(interpolator);
00643 }
00644 
00645 Foam::tmp<Foam::sphericalTensorField> Foam::sampledIsoSurface::interpolate
00646 (
00647     const interpolation<sphericalTensor>& interpolator
00648 ) const
00649 {
00650     return interpolateField(interpolator);
00651 }
00652 
00653 
00654 Foam::tmp<Foam::symmTensorField> Foam::sampledIsoSurface::interpolate
00655 (
00656     const interpolation<symmTensor>& interpolator
00657 ) const
00658 {
00659     return interpolateField(interpolator);
00660 }
00661 
00662 
00663 Foam::tmp<Foam::tensorField> Foam::sampledIsoSurface::interpolate
00664 (
00665     const interpolation<tensor>& interpolator
00666 ) const
00667 {
00668     return interpolateField(interpolator);
00669 }
00670 
00671 
00672 void Foam::sampledIsoSurface::print(Ostream& os) const
00673 {
00674     os  << "sampledIsoSurface: " << name() << " :"
00675         << "  field   :" << isoField_
00676         << "  value   :" << isoVal_;
00677         //<< "  faces:" << faces().size()       // note: possibly no geom yet
00678         //<< "  points:" << points().size();
00679 }
00680 
00681 
00682 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd