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

sampledSets.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 "sampledSets.H"
00028 #include "dictionary.H"
00029 #include "Time.H"
00030 #include "volFields.H"
00031 #include "ListListOps.H"
00032 #include "SortableList.H"
00033 #include "volPointInterpolation.H"
00034 
00035 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00036 
00037 namespace Foam
00038 {
00039     defineTypeNameAndDebug(sampledSets, 0);
00040 }
00041 
00042 bool Foam::sampledSets::verbose_ = false;
00043 
00044 
00045 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00046 
00047 bool Foam::sampledSets::checkFieldTypes()
00048 {
00049     wordList fieldTypes(fieldNames_.size());
00050 
00051     // check files for a particular time
00052     if (loadFromFiles_)
00053     {
00054         forAll(fieldNames_, fieldi)
00055         {
00056             IOobject io
00057             (
00058                 fieldNames_[fieldi],
00059                 mesh_.time().timeName(),
00060                 mesh_,
00061                 IOobject::MUST_READ,
00062                 IOobject::NO_WRITE,
00063                 false
00064             );
00065 
00066             if (io.headerOk())
00067             {
00068                 fieldTypes[fieldi] = io.headerClassName();
00069             }
00070             else
00071             {
00072                 fieldTypes[fieldi] = "(notFound)";
00073             }
00074         }
00075     }
00076     else
00077     {
00078         // check objectRegistry
00079         forAll(fieldNames_, fieldi)
00080         {
00081             objectRegistry::const_iterator iter =
00082                 mesh_.find(fieldNames_[fieldi]);
00083 
00084             if (iter != mesh_.objectRegistry::end())
00085             {
00086                 fieldTypes[fieldi] = iter()->type();
00087             }
00088             else
00089             {
00090                 fieldTypes[fieldi] = "(notFound)";
00091             }
00092         }
00093     }
00094 
00095 
00096     label nFields = 0;
00097 
00098     // classify fieldTypes
00099     nFields += grep(scalarFields_, fieldTypes);
00100     nFields += grep(vectorFields_, fieldTypes);
00101     nFields += grep(sphericalTensorFields_, fieldTypes);
00102     nFields += grep(symmTensorFields_, fieldTypes);
00103     nFields += grep(tensorFields_, fieldTypes);
00104 
00105     if (Pstream::master())
00106     {
00107         if (debug)
00108         {
00109             Pout<< "timeName = " << mesh_.time().timeName() << nl
00110                 << "scalarFields    " << scalarFields_ << nl
00111                 << "vectorFields    " << vectorFields_ << nl
00112                 << "sphTensorFields " << sphericalTensorFields_ << nl
00113                 << "symTensorFields " << symmTensorFields_ <<nl
00114                 << "tensorFields    " << tensorFields_ <<nl;
00115         }
00116 
00117         if (nFields > 0)
00118         {
00119             if (debug)
00120             {
00121                 Pout<< "Creating directory "
00122                     << outputPath_/mesh_.time().timeName()
00123                     << nl << endl;
00124             }
00125 
00126             mkDir(outputPath_/mesh_.time().timeName());
00127         }
00128     }
00129 
00130     return nFields > 0;
00131 }
00132 
00133 
00134 void Foam::sampledSets::combineSampledSets
00135 (
00136     PtrList<coordSet>& masterSampledSets,
00137     labelListList& indexSets
00138 )
00139 {
00140     // Combine sampleSets from processors. Sort by curveDist. Return
00141     // ordering in indexSets.
00142     // Note: only master results are valid
00143 
00144     masterSampledSets_.clear();
00145     masterSampledSets_.setSize(size());
00146     indexSets_.setSize(size());
00147 
00148     const PtrList<sampledSet>& sampledSets = *this;
00149 
00150     forAll(sampledSets, seti)
00151     {
00152         const sampledSet& samplePts = sampledSets[seti];
00153 
00154         // Collect data from all processors
00155         List<List<point> > gatheredPts(Pstream::nProcs());
00156         gatheredPts[Pstream::myProcNo()] = samplePts;
00157         Pstream::gatherList(gatheredPts);
00158 
00159         List<labelList> gatheredSegments(Pstream::nProcs());
00160         gatheredSegments[Pstream::myProcNo()] = samplePts.segments();
00161         Pstream::gatherList(gatheredSegments);
00162 
00163         List<scalarList> gatheredDist(Pstream::nProcs());
00164         gatheredDist[Pstream::myProcNo()] = samplePts.curveDist();
00165         Pstream::gatherList(gatheredDist);
00166 
00167 
00168         // Combine processor lists into one big list.
00169         List<point> allPts
00170         (
00171             ListListOps::combine<List<point> >
00172             (
00173                 gatheredPts, accessOp<List<point> >()
00174             )
00175         );
00176         labelList allSegments
00177         (
00178             ListListOps::combine<labelList>
00179             (
00180                 gatheredSegments, accessOp<labelList>()
00181             )
00182         );
00183         scalarList allCurveDist
00184         (
00185             ListListOps::combine<scalarList>
00186             (
00187                 gatheredDist, accessOp<scalarList>()
00188             )
00189         );
00190 
00191         // Sort curveDist and use to fill masterSamplePts
00192         SortableList<scalar> sortedDist(allCurveDist);
00193         indexSets[seti] = sortedDist.indices();
00194 
00195         // Get reference point (note: only master has all points)
00196         point refPt;
00197 
00198         if (allPts.size())
00199         {
00200             refPt = samplePts.getRefPoint(allPts);
00201         }
00202         else
00203         {
00204             refPt = vector::zero;
00205         }
00206 
00207 
00208         masterSampledSets.set
00209         (
00210             seti,
00211             new coordSet
00212             (
00213                 samplePts.name(),
00214                 samplePts.axis(),
00215                 UIndirectList<point>(allPts, indexSets[seti]),
00216                 refPt
00217             )
00218         );
00219     }
00220 }
00221 
00222 
00223 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00224 
00225 Foam::sampledSets::sampledSets
00226 (
00227     const word& name,
00228     const objectRegistry& obr,
00229     const dictionary& dict,
00230     const bool loadFromFiles
00231 )
00232 :
00233     PtrList<sampledSet>(),
00234     name_(name),
00235     mesh_(refCast<const fvMesh>(obr)),
00236     loadFromFiles_(loadFromFiles),
00237     outputPath_(fileName::null),
00238     searchEngine_(mesh_, true),
00239     fieldNames_(),
00240     interpolationScheme_(word::null),
00241     writeFormat_(word::null)
00242 {
00243     if (Pstream::parRun())
00244     {
00245         outputPath_ = mesh_.time().path()/".."/name_;
00246     }
00247     else
00248     {
00249         outputPath_ = mesh_.time().path()/name_;
00250     }
00251     if (mesh_.name() != fvMesh::defaultRegion)
00252     {
00253         outputPath_ = outputPath_/mesh_.name();
00254     }
00255 
00256     read(dict);
00257 }
00258 
00259 
00260 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00261 
00262 Foam::sampledSets::~sampledSets()
00263 {}
00264 
00265 
00266 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00267 
00268 void Foam::sampledSets::verbose(const bool verbosity)
00269 {
00270     verbose_ = verbosity;
00271 }
00272 
00273 
00274 void Foam::sampledSets::execute()
00275 {
00276     // Do nothing - only valid on write
00277 }
00278 
00279 
00280 void Foam::sampledSets::end()
00281 {
00282     // Do nothing - only valid on write
00283 }
00284 
00285 
00286 void Foam::sampledSets::write()
00287 {
00288     if (size() && checkFieldTypes())
00289     {
00290         sampleAndWrite(scalarFields_);
00291         sampleAndWrite(vectorFields_);
00292         sampleAndWrite(sphericalTensorFields_);
00293         sampleAndWrite(symmTensorFields_);
00294         sampleAndWrite(tensorFields_);
00295     }
00296 }
00297 
00298 
00299 void Foam::sampledSets::read(const dictionary& dict)
00300 {
00301     dict_ = dict;
00302 
00303     fieldNames_ = wordList(dict_.lookup("fields"));
00304 
00305     interpolationScheme_ = "cell";
00306     dict_.readIfPresent("interpolationScheme", interpolationScheme_);
00307 
00308     writeFormat_ = "null";
00309     dict_.readIfPresent("setFormat", writeFormat_);
00310 
00311     scalarFields_.clear();
00312     vectorFields_.clear();
00313     sphericalTensorFields_.clear();
00314     symmTensorFields_.clear();
00315     tensorFields_.clear();
00316 
00317     PtrList<sampledSet> newList
00318     (
00319         dict_.lookup("sets"),
00320         sampledSet::iNew(mesh_, searchEngine_)
00321     );
00322     transfer(newList);
00323     combineSampledSets(masterSampledSets_, indexSets_);
00324 
00325     if (Pstream::master() && debug)
00326     {
00327         Pout<< "sample fields:" << fieldNames_ << nl
00328             << "sample sets:" << nl << "(" << nl;
00329 
00330         forAll(*this, si)
00331         {
00332             Pout << "  " << operator[](si) << endl;
00333         }
00334         Pout << ")" << endl;
00335     }
00336 }
00337 
00338 
00339 void Foam::sampledSets::correct()
00340 {
00341     // reset interpolation
00342     pointMesh::Delete(mesh_);
00343     volPointInterpolation::Delete(mesh_);
00344 
00345     searchEngine_.correct();
00346 
00347     PtrList<sampledSet> newList
00348     (
00349         dict_.lookup("sets"),
00350         sampledSet::iNew(mesh_, searchEngine_)
00351     );
00352     transfer(newList);
00353     combineSampledSets(masterSampledSets_, indexSets_);
00354 }
00355 
00356 
00357 void Foam::sampledSets::updateMesh(const mapPolyMesh&)
00358 {
00359     correct();
00360 }
00361 
00362 
00363 void Foam::sampledSets::movePoints(const pointField&)
00364 {
00365     correct();
00366 }
00367 
00368 
00369 void Foam::sampledSets::readUpdate(const polyMesh::readUpdateState state)
00370 {
00371     if (state != polyMesh::UNCHANGED)
00372     {
00373         correct();
00374     }
00375 }
00376 
00377 
00378 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd