FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "sampledSets.H"
00027 #include <OpenFOAM/dictionary.H>
00028 #include <OpenFOAM/Time.H>
00029 #include <finiteVolume/volFields.H>
00030 #include <OpenFOAM/ListListOps.H>
00031 #include <OpenFOAM/SortableList.H>
00032 #include <finiteVolume/volPointInterpolation.H>
00033 
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 
00192         if (Pstream::master() && allCurveDist.size() == 0)
00193         {
00194             WarningIn("sampledSets::combineSampledSets(..)")
00195                 << "Sample set " << samplePts.name()
00196                 << " has zero points." << endl;
00197         }
00198 
00199         // Sort curveDist and use to fill masterSamplePts
00200         SortableList<scalar> sortedDist(allCurveDist);
00201         indexSets[seti] = sortedDist.indices();
00202 
00203         // Get reference point (note: only master has all points)
00204         point refPt;
00205 
00206         if (allPts.size())
00207         {
00208             refPt = samplePts.getRefPoint(allPts);
00209         }
00210         else
00211         {
00212             refPt = vector::zero;
00213         }
00214 
00215 
00216         masterSampledSets.set
00217         (
00218             seti,
00219             new coordSet
00220             (
00221                 samplePts.name(),
00222                 samplePts.axis(),
00223                 UIndirectList<point>(allPts, indexSets[seti]),
00224                 refPt
00225             )
00226         );
00227     }
00228 }
00229 
00230 
00231 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00232 
00233 Foam::sampledSets::sampledSets
00234 (
00235     const word& name,
00236     const objectRegistry& obr,
00237     const dictionary& dict,
00238     const bool loadFromFiles
00239 )
00240 :
00241     PtrList<sampledSet>(),
00242     name_(name),
00243     mesh_(refCast<const fvMesh>(obr)),
00244     loadFromFiles_(loadFromFiles),
00245     outputPath_(fileName::null),
00246     searchEngine_(mesh_, true),
00247     fieldNames_(),
00248     interpolationScheme_(word::null),
00249     writeFormat_(word::null)
00250 {
00251     if (Pstream::parRun())
00252     {
00253         outputPath_ = mesh_.time().path()/".."/name_;
00254     }
00255     else
00256     {
00257         outputPath_ = mesh_.time().path()/name_;
00258     }
00259     if (mesh_.name() != fvMesh::defaultRegion)
00260     {
00261         outputPath_ = outputPath_/mesh_.name();
00262     }
00263 
00264     read(dict);
00265 }
00266 
00267 
00268 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00269 
00270 Foam::sampledSets::~sampledSets()
00271 {}
00272 
00273 
00274 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00275 
00276 void Foam::sampledSets::verbose(const bool verbosity)
00277 {
00278     verbose_ = verbosity;
00279 }
00280 
00281 
00282 void Foam::sampledSets::execute()
00283 {
00284     // Do nothing - only valid on write
00285 }
00286 
00287 
00288 void Foam::sampledSets::end()
00289 {
00290     // Do nothing - only valid on write
00291 }
00292 
00293 
00294 void Foam::sampledSets::write()
00295 {
00296     if (size() && checkFieldTypes())
00297     {
00298         sampleAndWrite(scalarFields_);
00299         sampleAndWrite(vectorFields_);
00300         sampleAndWrite(sphericalTensorFields_);
00301         sampleAndWrite(symmTensorFields_);
00302         sampleAndWrite(tensorFields_);
00303     }
00304 }
00305 
00306 
00307 void Foam::sampledSets::read(const dictionary& dict)
00308 {
00309     dict_ = dict;
00310 
00311     fieldNames_ = wordList(dict_.lookup("fields"));
00312 
00313     interpolationScheme_ = "cell";
00314     dict_.readIfPresent("interpolationScheme", interpolationScheme_);
00315 
00316     dict_.lookup("setFormat") >> writeFormat_;
00317 
00318     scalarFields_.clear();
00319     vectorFields_.clear();
00320     sphericalTensorFields_.clear();
00321     symmTensorFields_.clear();
00322     tensorFields_.clear();
00323 
00324     PtrList<sampledSet> newList
00325     (
00326         dict_.lookup("sets"),
00327         sampledSet::iNew(mesh_, searchEngine_)
00328     );
00329     transfer(newList);
00330     combineSampledSets(masterSampledSets_, indexSets_);
00331 
00332     if (Pstream::master() && debug)
00333     {
00334         Pout<< "sample fields:" << fieldNames_ << nl
00335             << "sample sets:" << nl << "(" << nl;
00336 
00337         forAll(*this, si)
00338         {
00339             Pout << "  " << operator[](si) << endl;
00340         }
00341         Pout << ")" << endl;
00342     }
00343 }
00344 
00345 
00346 void Foam::sampledSets::correct()
00347 {
00348     // reset interpolation
00349     pointMesh::Delete(mesh_);
00350     volPointInterpolation::Delete(mesh_);
00351 
00352     searchEngine_.correct();
00353 
00354     PtrList<sampledSet> newList
00355     (
00356         dict_.lookup("sets"),
00357         sampledSet::iNew(mesh_, searchEngine_)
00358     );
00359     transfer(newList);
00360     combineSampledSets(masterSampledSets_, indexSets_);
00361 }
00362 
00363 
00364 void Foam::sampledSets::updateMesh(const mapPolyMesh&)
00365 {
00366     correct();
00367 }
00368 
00369 
00370 void Foam::sampledSets::movePoints(const pointField&)
00371 {
00372     correct();
00373 }
00374 
00375 
00376 void Foam::sampledSets::readUpdate(const polyMesh::readUpdateState state)
00377 {
00378     if (state != polyMesh::UNCHANGED)
00379     {
00380         correct();
00381     }
00382 }
00383 
00384 
00385 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines