1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
28 #include "volFields.H"
29 #include "dictionary.H"
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 defineTypeNameAndDebug(probes, 0);
40 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
42 void Foam::probes::findCells(const fvMesh& mesh)
44 if (cellList_.empty())
46 cellList_.setSize(probeLocations_.size());
48 forAll(probeLocations_, probeI)
50 cellList_[probeI] = mesh.findCell(probeLocations_[probeI]);
52 if (debug && cellList_[probeI] != -1)
54 Pout<< "probes : found point " << probeLocations_[probeI]
55 << " in cell " << cellList_[probeI] << endl;
60 // Check if all probes have been found.
61 forAll(cellList_, probeI)
63 label cellI = cellList_[probeI];
65 // Check at least one processor with cell.
66 reduce(cellI, maxOp<label>());
70 if (Pstream::master())
72 WarningIn("probes::read()")
73 << "Did not find location " << probeLocations_[probeI]
74 << " in any cell. Skipping location." << endl;
79 // Make sure location not on two domains.
80 if (cellList_[probeI] != -1 && cellList_[probeI] != cellI)
82 WarningIn("probes::read()")
83 << "Location " << probeLocations_[probeI]
84 << " seems to be on multiple domains:"
85 << " cell " << cellList_[probeI]
86 << " on my domain " << Pstream::myProcNo()
87 << " and cell " << cellI << " on some other domain."
89 << "This might happen if the probe location is on"
90 << " a processor patch. Change the location slightly"
91 << " to prevent this." << endl;
99 bool Foam::probes::checkFieldTypes()
101 wordList fieldTypes(fieldNames_.size());
103 // check files for a particular time
106 forAll(fieldNames_, fieldI)
111 obr_.time().timeName(),
112 refCast<const polyMesh>(obr_),
120 fieldTypes[fieldI] = io.headerClassName();
124 fieldTypes[fieldI] = "(notFound)";
130 // check objectRegistry
131 forAll(fieldNames_, fieldI)
133 objectRegistry::const_iterator iter =
134 obr_.find(fieldNames_[fieldI]);
136 if (iter != obr_.end())
138 fieldTypes[fieldI] = iter()->type();
142 fieldTypes[fieldI] = "(notFound)";
150 // classify fieldTypes
151 nFields += countFields(scalarFields_, fieldTypes);
152 nFields += countFields(vectorFields_, fieldTypes);
153 nFields += countFields(sphericalTensorFields_, fieldTypes);
154 nFields += countFields(symmTensorFields_, fieldTypes);
155 nFields += countFields(tensorFields_, fieldTypes);
157 // concatenate all the lists into foundFields
158 wordList foundFields(nFields);
161 forAll(scalarFields_, i)
163 foundFields[fieldI++] = scalarFields_[i];
165 forAll(vectorFields_, i)
167 foundFields[fieldI++] = vectorFields_[i];
169 forAll(sphericalTensorFields_, i)
171 foundFields[fieldI++] = sphericalTensorFields_[i];
173 forAll(symmTensorFields_, i)
175 foundFields[fieldI++] = symmTensorFields_[i];
177 forAll(tensorFields_, i)
179 foundFields[fieldI++] = tensorFields_[i];
182 if (Pstream::master())
186 fileName probeSubDir = name_;
188 if (obr_.name() != polyMesh::defaultRegion)
190 probeSubDir = probeSubDir/obr_.name();
192 probeSubDir = probeSubDir/obr_.time().timeName();
194 if (Pstream::parRun())
196 // Put in undecomposed case
197 // (Note: gives problems for distributed data running)
198 probeDir = obr_.time().path()/".."/probeSubDir;
202 probeDir = obr_.time().path()/probeSubDir;
205 // Close the file if any fields have been removed.
206 forAllIter(HashPtrTable<OFstream>, probeFilePtrs_, iter)
208 if (findIndex(foundFields, iter.key()) == -1)
212 Pout<< "close stream: " << iter()->name() << endl;
215 delete probeFilePtrs_.remove(iter);
219 // Open new files for new fields. Keep existing files.
221 probeFilePtrs_.resize(2*foundFields.size());
223 forAll(foundFields, fieldI)
225 const word& fldName = foundFields[fieldI];
227 // Check if added field. If so open a stream for it.
229 if (!probeFilePtrs_.found(fldName))
231 // Create directory if does not exist.
234 OFstream* sPtr = new OFstream(probeDir/fldName);
238 Pout<< "open stream: " << sPtr->name() << endl;
241 probeFilePtrs_.insert(fldName, sPtr);
243 unsigned int w = IOstream::defaultPrecision() + 7;
245 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
247 *sPtr<< '#' << setw(IOstream::defaultPrecision() + 6)
248 << vector::componentNames[cmpt];
250 forAll(probeLocations_, probeI)
252 *sPtr<< ' ' << setw(w) << probeLocations_[probeI][cmpt];
257 *sPtr<< '#' << setw(IOstream::defaultPrecision() + 6)
264 Pout<< "Probing fields:" << foundFields << nl
265 << "Probing locations:" << probeLocations_ << nl
275 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
280 const objectRegistry& obr,
281 const dictionary& dict,
282 const bool loadFromFiles
287 loadFromFiles_(loadFromFiles),
292 sphericalTensorFields_(),
302 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
304 Foam::probes::~probes()
308 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
310 void Foam::probes::execute()
312 // Do nothing - only valid on write
316 void Foam::probes::end()
318 // Do nothing - only valid on write
322 void Foam::probes::write()
324 if (probeLocations_.size() && checkFieldTypes())
326 sampleAndWrite(scalarFields_);
327 sampleAndWrite(vectorFields_);
328 sampleAndWrite(sphericalTensorFields_);
329 sampleAndWrite(symmTensorFields_);
330 sampleAndWrite(tensorFields_);
335 void Foam::probes::read(const dictionary& dict)
337 dict.lookup("fields") >> fieldNames_;
338 dict.lookup("probeLocations") >> probeLocations_;
340 // Force all cell locations to be redetermined
342 findCells(refCast<const fvMesh>(obr_));
347 // ************************************************************************* //