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

sampledSurfaces.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 "sampledSurfaces.H"
00028 #include "volFields.H"
00029 #include "dictionary.H"
00030 #include "Time.H"
00031 #include "IOmanip.H"
00032 #include "ListListOps.H"
00033 #include "mergePoints.H"
00034 #include "volPointInterpolation.H"
00035 
00036 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00037 
00038 namespace Foam
00039 {
00040     //- Used to offset faces in Pstream::combineOffset
00041     template <>
00042     class offsetOp<face>
00043     {
00044 
00045     public:
00046 
00047         face operator()
00048         (
00049             const face& x,
00050             const label offset
00051         ) const
00052         {
00053             face result(x.size());
00054 
00055             forAll(x, xI)
00056             {
00057                 result[xI] = x[xI] + offset;
00058             }
00059             return result;
00060         }
00061     };
00062 
00063 
00064     defineTypeNameAndDebug(sampledSurfaces, 0);
00065 }
00066 
00067 
00068 bool Foam::sampledSurfaces::verbose_(false);
00069 Foam::scalar Foam::sampledSurfaces::mergeTol_(1e-10);
00070 
00071 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00072 
00073 Foam::label Foam::sampledSurfaces::classifyFieldTypes()
00074 {
00075     label nFields = 0;
00076 
00077     scalarFields_.clear();
00078     vectorFields_.clear();
00079     sphericalTensorFields_.clear();
00080     symmTensorFields_.clear();
00081     tensorFields_.clear();
00082 
00083     forAll(fieldNames_, fieldI)
00084     {
00085         const word& fieldName = fieldNames_[fieldI];
00086         word fieldType = "";
00087 
00088         // check files for a particular time
00089         if (loadFromFiles_)
00090         {
00091             IOobject io
00092             (
00093                 fieldName,
00094                 mesh_.time().timeName(),
00095                 mesh_,
00096                 IOobject::MUST_READ,
00097                 IOobject::NO_WRITE,
00098                 false
00099             );
00100 
00101             if (io.headerOk())
00102             {
00103                 fieldType = io.headerClassName();
00104             }
00105             else
00106             {
00107                 continue;
00108             }
00109         }
00110         else
00111         {
00112             // check objectRegistry
00113             objectRegistry::const_iterator iter = mesh_.find(fieldName);
00114 
00115             if (iter != mesh_.objectRegistry::end())
00116             {
00117                 fieldType = iter()->type();
00118             }
00119             else
00120             {
00121                 continue;
00122             }
00123         }
00124 
00125 
00126         if (fieldType == volScalarField::typeName)
00127         {
00128             scalarFields_.append(fieldName);
00129             nFields++;
00130         }
00131         else if (fieldType == volVectorField::typeName)
00132         {
00133             vectorFields_.append(fieldName);
00134             nFields++;
00135         }
00136         else if (fieldType == volSphericalTensorField::typeName)
00137         {
00138             sphericalTensorFields_.append(fieldName);
00139             nFields++;
00140         }
00141         else if (fieldType == volSymmTensorField::typeName)
00142         {
00143             symmTensorFields_.append(fieldName);
00144             nFields++;
00145         }
00146         else if (fieldType == volTensorField::typeName)
00147         {
00148             tensorFields_.append(fieldName);
00149             nFields++;
00150         }
00151 
00152     }
00153 
00154     return nFields;
00155 }
00156 
00157 
00158 void Foam::sampledSurfaces::writeGeometry() const
00159 {
00160     // Write to time directory under outputPath_
00161     // skip surface without faces (eg, a failed cut-plane)
00162 
00163     const fileName outputDir = outputPath_/mesh_.time().timeName();
00164 
00165     forAll(*this, surfI)
00166     {
00167         const sampledSurface& s = operator[](surfI);
00168 
00169         if (Pstream::parRun())
00170         {
00171             if (Pstream::master() && mergeList_[surfI].faces.size())
00172             {
00173                 genericFormatter_->write
00174                 (
00175                     outputDir,
00176                     s.name(),
00177                     mergeList_[surfI].points,
00178                     mergeList_[surfI].faces
00179                 );
00180             }
00181         }
00182         else if (s.faces().size())
00183         {
00184             genericFormatter_->write
00185             (
00186                 outputDir,
00187                 s.name(),
00188                 s.points(),
00189                 s.faces()
00190             );
00191         }
00192     }
00193 }
00194 
00195 
00196 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00197 
00198 Foam::sampledSurfaces::sampledSurfaces
00199 (
00200     const word& name,
00201     const objectRegistry& obr,
00202     const dictionary& dict,
00203     const bool loadFromFiles
00204 )
00205 :
00206     PtrList<sampledSurface>(),
00207     name_(name),
00208     mesh_(refCast<const fvMesh>(obr)),
00209     loadFromFiles_(loadFromFiles),
00210     outputPath_(fileName::null),
00211     fieldNames_(),
00212     interpolationScheme_(word::null),
00213     writeFormat_(word::null),
00214     mergeList_(),
00215     genericFormatter_(NULL),
00216     scalarFields_(),
00217     vectorFields_(),
00218     sphericalTensorFields_(),
00219     symmTensorFields_(),
00220     tensorFields_()
00221 {
00222     if (Pstream::parRun())
00223     {
00224         outputPath_ = mesh_.time().path()/".."/name_;
00225     }
00226     else
00227     {
00228         outputPath_ = mesh_.time().path()/name_;
00229     }
00230 
00231     read(dict);
00232 }
00233 
00234 
00235 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00236 
00237 Foam::sampledSurfaces::~sampledSurfaces()
00238 {}
00239 
00240 
00241 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00242 
00243 void Foam::sampledSurfaces::verbose(const bool verbosity)
00244 {
00245     verbose_ = verbosity;
00246 }
00247 
00248 
00249 void Foam::sampledSurfaces::execute()
00250 {
00251     // Do nothing - only valid on write
00252 }
00253 
00254 
00255 void Foam::sampledSurfaces::end()
00256 {
00257     // Do nothing - only valid on write
00258 }
00259 
00260 
00261 void Foam::sampledSurfaces::write()
00262 {
00263     if (size())
00264     {
00265         // finalize surfaces, merge points etc.
00266         update();
00267 
00268         const label nFields = classifyFieldTypes();
00269 
00270         if (Pstream::master())
00271         {
00272             if (debug)
00273             {
00274                 Pout<< "timeName = " << mesh_.time().timeName() << nl
00275                     << "scalarFields    " << scalarFields_ << nl
00276                     << "vectorFields    " << vectorFields_ << nl
00277                     << "sphTensorFields " << sphericalTensorFields_ << nl
00278                     << "symTensorFields " << symmTensorFields_ <<nl
00279                     << "tensorFields    " << tensorFields_ <<nl;
00280 
00281                 Pout<< "Creating directory "
00282                     << outputPath_/mesh_.time().timeName() << nl << endl;
00283 
00284             }
00285 
00286             mkDir(outputPath_/mesh_.time().timeName());
00287         }
00288 
00289         // write geometry first if required, or when no fields would otherwise
00290         // be written
00291         if (nFields == 0 || genericFormatter_->separateFiles())
00292         {
00293             writeGeometry();
00294         }
00295 
00296         sampleAndWrite(scalarFields_);
00297         sampleAndWrite(vectorFields_);
00298         sampleAndWrite(sphericalTensorFields_);
00299         sampleAndWrite(symmTensorFields_);
00300         sampleAndWrite(tensorFields_);
00301     }
00302 }
00303 
00304 
00305 void Foam::sampledSurfaces::read(const dictionary& dict)
00306 {
00307     fieldNames_ = wordList(dict.lookup("fields"));
00308 
00309     const label nFields = fieldNames_.size();
00310 
00311     scalarFields_.reset(nFields);
00312     vectorFields_.reset(nFields);
00313     sphericalTensorFields_.reset(nFields);
00314     symmTensorFields_.reset(nFields);
00315     tensorFields_.reset(nFields);
00316 
00317     interpolationScheme_ = dict.lookupOrDefault<word>
00318     (
00319         "interpolationScheme",
00320         "cell"
00321     );
00322     writeFormat_ = dict.lookupOrDefault<word>
00323     (
00324         "surfaceFormat",
00325         "null"
00326     );
00327 
00328 
00329     // define the generic (geometry) writer
00330     genericFormatter_ = surfaceWriter<bool>::New(writeFormat_);
00331 
00332 
00333     PtrList<sampledSurface> newList
00334     (
00335         dict.lookup("surfaces"),
00336         sampledSurface::iNew(mesh_)
00337     );
00338 
00339     transfer(newList);
00340 
00341     if (Pstream::parRun())
00342     {
00343         mergeList_.setSize(size());
00344     }
00345 
00346     // ensure all surfaces and merge information are expired
00347     expire();
00348 
00349     if (Pstream::master() && debug)
00350     {
00351         Pout<< "sample fields:" << fieldNames_ << nl
00352             << "sample surfaces:" << nl << "(" << nl;
00353 
00354         forAll(*this, surfI)
00355         {
00356             Pout<< "  " << operator[](surfI) << endl;
00357         }
00358         Pout<< ")" << endl;
00359     }
00360 }
00361 
00362 
00363 void Foam::sampledSurfaces::updateMesh(const mapPolyMesh&)
00364 {
00365     expire();
00366 }
00367 
00368 
00369 void Foam::sampledSurfaces::movePoints(const pointField&)
00370 {
00371     expire();
00372 }
00373 
00374 
00375 void Foam::sampledSurfaces::readUpdate(const polyMesh::readUpdateState state)
00376 {
00377     if (state != polyMesh::UNCHANGED)
00378     {
00379         expire();
00380     }
00381 }
00382 
00383 
00384 bool Foam::sampledSurfaces::needsUpdate() const
00385 {
00386     forAll(*this, surfI)
00387     {
00388         if (operator[](surfI).needsUpdate())
00389         {
00390             return true;
00391         }
00392     }
00393 
00394     return false;
00395 }
00396 
00397 
00398 bool Foam::sampledSurfaces::expire()
00399 {
00400     bool justExpired = false;
00401 
00402     forAll(*this, surfI)
00403     {
00404         if (operator[](surfI).expire())
00405         {
00406             justExpired = true;
00407         }
00408 
00409         // clear merge information
00410         if (Pstream::parRun())
00411         {
00412             mergeList_[surfI].clear();
00413         }
00414     }
00415 
00416     // reset interpolation
00417     pointMesh::Delete(mesh_);
00418     volPointInterpolation::Delete(mesh_);
00419 
00420     // true if any surfaces just expired
00421     return justExpired;
00422 }
00423 
00424 
00425 bool Foam::sampledSurfaces::update()
00426 {
00427     bool updated = false;
00428 
00429     if (!needsUpdate())
00430     {
00431         return updated;
00432     }
00433 
00434     // serial: quick and easy, no merging required
00435     if (!Pstream::parRun())
00436     {
00437         forAll(*this, surfI)
00438         {
00439             if (operator[](surfI).update())
00440             {
00441                 updated = true;
00442             }
00443         }
00444 
00445         return updated;
00446     }
00447 
00448     // dimension as fraction of mesh bounding box
00449     scalar mergeDim = mergeTol_ * mesh_.globalData().bb().mag();
00450 
00451     if (Pstream::master() && debug)
00452     {
00453         Pout<< nl << "Merging all points within "
00454             << mergeDim << " meter" << endl;
00455     }
00456 
00457     forAll(*this, surfI)
00458     {
00459         sampledSurface& s = operator[](surfI);
00460 
00461         if (s.update())
00462         {
00463             updated = true;
00464         }
00465         else
00466         {
00467             continue;
00468         }
00469 
00470 
00471         // Collect points from all processors
00472         List<pointField> gatheredPoints(Pstream::nProcs());
00473         gatheredPoints[Pstream::myProcNo()] = s.points();
00474         Pstream::gatherList(gatheredPoints);
00475 
00476         if (Pstream::master())
00477         {
00478             mergeList_[surfI].points = ListListOps::combine<pointField>
00479             (
00480                 gatheredPoints,
00481                 accessOp<pointField>()
00482             );
00483         }
00484 
00485         // Collect faces from all processors and renumber using sizes of
00486         // gathered points
00487         List<faceList> gatheredFaces(Pstream::nProcs());
00488         gatheredFaces[Pstream::myProcNo()] = s.faces();
00489         Pstream::gatherList(gatheredFaces);
00490 
00491         if (Pstream::master())
00492         {
00493             mergeList_[surfI].faces = static_cast<const faceList&>
00494             (
00495                 ListListOps::combineOffset<faceList>
00496                 (
00497                     gatheredFaces,
00498                     ListListOps::subSizes
00499                     (
00500                         gatheredPoints,
00501                         accessOp<pointField>()
00502                     ),
00503                     accessOp<faceList>(),
00504                     offsetOp<face>()
00505                 )
00506             );
00507         }
00508 
00509         pointField newPoints;
00510         labelList oldToNew;
00511 
00512         bool hasMerged = mergePoints
00513         (
00514             mergeList_[surfI].points,
00515             mergeDim,
00516             false,                  // verbosity
00517             oldToNew,
00518             newPoints
00519         );
00520 
00521         if (hasMerged)
00522         {
00523             // Store point mapping
00524             mergeList_[surfI].pointsMap.transfer(oldToNew);
00525 
00526             // Copy points
00527             mergeList_[surfI].points.transfer(newPoints);
00528 
00529             // Relabel faces
00530             faceList& faces = mergeList_[surfI].faces;
00531 
00532             forAll(faces, faceI)
00533             {
00534                 inplaceRenumber(mergeList_[surfI].pointsMap, faces[faceI]);
00535             }
00536 
00537             if (Pstream::master() && debug)
00538             {
00539                 Pout<< "For surface " << surfI << " merged from "
00540                     << mergeList_[surfI].pointsMap.size() << " points down to "
00541                     << mergeList_[surfI].points.size()    << " points" << endl;
00542             }
00543         }
00544     }
00545 
00546     return updated;
00547 }
00548 
00549 
00550 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd