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

probes.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 "probes.H"
00028 #include "volFields.H"
00029 #include "dictionary.H"
00030 #include "Time.H"
00031 #include "IOmanip.H"
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037     defineTypeNameAndDebug(probes, 0);
00038 }
00039 
00040 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00041 
00042 void Foam::probes::findCells(const fvMesh& mesh)
00043 {
00044     if (cellList_.empty())
00045     {
00046         cellList_.setSize(probeLocations_.size());
00047 
00048         forAll(probeLocations_, probeI)
00049         {
00050             cellList_[probeI] = mesh.findCell(probeLocations_[probeI]);
00051 
00052             if (debug && cellList_[probeI] != -1)
00053             {
00054                 Pout<< "probes : found point " << probeLocations_[probeI]
00055                     << " in cell " << cellList_[probeI] << endl;
00056             }
00057         }
00058 
00059 
00060         // Check if all probes have been found.
00061         forAll(cellList_, probeI)
00062         {
00063             label cellI = cellList_[probeI];
00064 
00065             // Check at least one processor with cell.
00066             reduce(cellI, maxOp<label>());
00067 
00068             if (cellI == -1)
00069             {
00070                 if (Pstream::master())
00071                 {
00072                     WarningIn("probes::read()")
00073                         << "Did not find location " << probeLocations_[probeI]
00074                         << " in any cell. Skipping location." << endl;
00075                 }
00076             }
00077             else
00078             {
00079                 // Make sure location not on two domains.
00080                 if (cellList_[probeI] != -1 && cellList_[probeI] != cellI)
00081                 {
00082                     WarningIn("probes::read()")
00083                         << "Location " << probeLocations_[probeI]
00084                         << " seems to be on multiple domains:"
00085                         << " cell " << cellList_[probeI]
00086                         << " on my domain " << Pstream::myProcNo()
00087                         << " and cell " << cellI << " on some other domain."
00088                         << endl
00089                         << "This might happen if the probe location is on"
00090                         << " a processor patch. Change the location slightly"
00091                         << " to prevent this." << endl;
00092                 }
00093             }
00094         }
00095     }
00096 }
00097 
00098 
00099 bool Foam::probes::checkFieldTypes()
00100 {
00101     wordList fieldTypes(fieldNames_.size());
00102 
00103     // check files for a particular time
00104     if (loadFromFiles_)
00105     {
00106         forAll(fieldNames_, fieldI)
00107         {
00108             IOobject io
00109             (
00110                 fieldNames_[fieldI],
00111                 obr_.time().timeName(),
00112                 refCast<const polyMesh>(obr_),
00113                 IOobject::MUST_READ,
00114                 IOobject::NO_WRITE,
00115                 false
00116             );
00117 
00118             if (io.headerOk())
00119             {
00120                 fieldTypes[fieldI] = io.headerClassName();
00121             }
00122             else
00123             {
00124                 fieldTypes[fieldI] = "(notFound)";
00125             }
00126         }
00127     }
00128     else
00129     {
00130         // check objectRegistry
00131         forAll(fieldNames_, fieldI)
00132         {
00133             objectRegistry::const_iterator iter =
00134                 obr_.find(fieldNames_[fieldI]);
00135 
00136             if (iter != obr_.end())
00137             {
00138                 fieldTypes[fieldI] = iter()->type();
00139             }
00140             else
00141             {
00142                 fieldTypes[fieldI] = "(notFound)";
00143             }
00144         }
00145     }
00146 
00147 
00148     label nFields = 0;
00149 
00150     // classify fieldTypes
00151     nFields += countFields(scalarFields_, fieldTypes);
00152     nFields += countFields(vectorFields_, fieldTypes);
00153     nFields += countFields(sphericalTensorFields_, fieldTypes);
00154     nFields += countFields(symmTensorFields_, fieldTypes);
00155     nFields += countFields(tensorFields_, fieldTypes);
00156 
00157     // concatenate all the lists into foundFields
00158     wordList foundFields(nFields);
00159 
00160     label fieldI = 0;
00161     forAll(scalarFields_, i)
00162     {
00163         foundFields[fieldI++] = scalarFields_[i];
00164     }
00165     forAll(vectorFields_, i)
00166     {
00167         foundFields[fieldI++] = vectorFields_[i];
00168     }
00169     forAll(sphericalTensorFields_, i)
00170     {
00171         foundFields[fieldI++] = sphericalTensorFields_[i];
00172     }
00173     forAll(symmTensorFields_, i)
00174     {
00175         foundFields[fieldI++] = symmTensorFields_[i];
00176     }
00177     forAll(tensorFields_, i)
00178     {
00179         foundFields[fieldI++] = tensorFields_[i];
00180     }
00181 
00182     if (Pstream::master())
00183     {
00184         fileName probeDir;
00185 
00186         fileName probeSubDir = name_;
00187 
00188         if (obr_.name() != polyMesh::defaultRegion)
00189         {
00190             probeSubDir = probeSubDir/obr_.name();
00191         }
00192         probeSubDir = probeSubDir/obr_.time().timeName();
00193 
00194         if (Pstream::parRun())
00195         {
00196             // Put in undecomposed case
00197             // (Note: gives problems for distributed data running)
00198             probeDir = obr_.time().path()/".."/probeSubDir;
00199         }
00200         else
00201         {
00202             probeDir = obr_.time().path()/probeSubDir;
00203         }
00204 
00205         // Close the file if any fields have been removed.
00206         forAllIter(HashPtrTable<OFstream>, probeFilePtrs_, iter)
00207         {
00208             if (findIndex(foundFields, iter.key()) == -1)
00209             {
00210                 if (debug)
00211                 {
00212                     Pout<< "close stream: " << iter()->name() << endl;
00213                 }
00214 
00215                 delete probeFilePtrs_.remove(iter);
00216             }
00217         }
00218 
00219         // Open new files for new fields. Keep existing files.
00220 
00221         probeFilePtrs_.resize(2*foundFields.size());
00222 
00223         forAll(foundFields, fieldI)
00224         {
00225             const word& fldName = foundFields[fieldI];
00226 
00227             // Check if added field. If so open a stream for it.
00228 
00229             if (!probeFilePtrs_.found(fldName))
00230             {
00231                 // Create directory if does not exist.
00232                 mkDir(probeDir);
00233 
00234                 OFstream* sPtr = new OFstream(probeDir/fldName);
00235 
00236                 if (debug)
00237                 {
00238                     Pout<< "open  stream: " << sPtr->name() << endl;
00239                 }
00240 
00241                 probeFilePtrs_.insert(fldName, sPtr);
00242 
00243                 unsigned int w = IOstream::defaultPrecision() + 7;
00244 
00245                 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
00246                 {
00247                     *sPtr<< '#' << setw(IOstream::defaultPrecision() + 6)
00248                         << vector::componentNames[cmpt];
00249 
00250                     forAll(probeLocations_, probeI)
00251                     {
00252                         *sPtr<< ' ' << setw(w) << probeLocations_[probeI][cmpt];
00253                     }
00254                     *sPtr << endl;
00255                 }
00256 
00257                 *sPtr<< '#' << setw(IOstream::defaultPrecision() + 6)
00258                     << "Time" << endl;
00259             }
00260         }
00261 
00262         if (debug)
00263         {
00264             Pout<< "Probing fields:" << foundFields << nl
00265                 << "Probing locations:" << probeLocations_ << nl
00266                 << endl;
00267         }
00268     }
00269 
00270 
00271     return nFields > 0;
00272 }
00273 
00274 
00275 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00276 
00277 Foam::probes::probes
00278 (
00279     const word& name,
00280     const objectRegistry& obr,
00281     const dictionary& dict,
00282     const bool loadFromFiles
00283 )
00284 :
00285     name_(name),
00286     obr_(obr),
00287     loadFromFiles_(loadFromFiles),
00288     fieldNames_(0),
00289     probeLocations_(0),
00290     scalarFields_(),
00291     vectorFields_(),
00292     sphericalTensorFields_(),
00293     symmTensorFields_(),
00294     tensorFields_(),
00295     cellList_(0),
00296     probeFilePtrs_(0)
00297 {
00298     read(dict);
00299 }
00300 
00301 
00302 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00303 
00304 Foam::probes::~probes()
00305 {}
00306 
00307 
00308 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00309 
00310 void Foam::probes::execute()
00311 {
00312     // Do nothing - only valid on write
00313 }
00314 
00315 
00316 void Foam::probes::end()
00317 {
00318     // Do nothing - only valid on write
00319 }
00320 
00321 
00322 void Foam::probes::write()
00323 {
00324     if (probeLocations_.size() && checkFieldTypes())
00325     {
00326         sampleAndWrite(scalarFields_);
00327         sampleAndWrite(vectorFields_);
00328         sampleAndWrite(sphericalTensorFields_);
00329         sampleAndWrite(symmTensorFields_);
00330         sampleAndWrite(tensorFields_);
00331     }
00332 }
00333 
00334 
00335 void Foam::probes::read(const dictionary& dict)
00336 {
00337     dict.lookup("fields") >> fieldNames_;
00338     dict.lookup("probeLocations") >> probeLocations_;
00339 
00340     // Force all cell locations to be redetermined
00341     cellList_.clear();
00342     findCells(refCast<const fvMesh>(obr_));
00343     checkFieldTypes();
00344 }
00345 
00346 
00347 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd