1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
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
13 the Free Software Foundation, either version 3 of the License, or
14 (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
27 #include "volFields.H"
28 #include "dictionary.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 defineTypeNameAndDebug(probes, 0);
39 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
41 void Foam::probes::findElements(const fvMesh& mesh)
43 if (elementList_.empty())
45 elementList_.setSize(probeLocations_.size());
47 forAll(probeLocations_, probeI)
49 elementList_[probeI] = mesh.findCell(probeLocations_[probeI]);
51 if (debug && elementList_[probeI] != -1)
53 Pout<< "probes : found point " << probeLocations_[probeI]
54 << " in cell " << elementList_[probeI] << endl;
59 // Check if all probes have been found.
60 forAll(elementList_, probeI)
62 label cellI = elementList_[probeI];
64 // Check at least one processor with cell.
65 reduce(cellI, maxOp<label>());
69 if (Pstream::master())
71 WarningIn("probes::read()")
72 << "Did not find location " << probeLocations_[probeI]
73 << " in any cell. Skipping location." << endl;
78 // Make sure location not on two domains.
79 if (elementList_[probeI] != -1 && elementList_[probeI] != cellI)
81 WarningIn("probes::read()")
82 << "Location " << probeLocations_[probeI]
83 << " seems to be on multiple domains:"
84 << " cell " << elementList_[probeI]
85 << " on my domain " << Pstream::myProcNo()
86 << " and cell " << cellI << " on some other domain."
88 << "This might happen if the probe location is on"
89 << " a processor patch. Change the location slightly"
90 << " to prevent this." << endl;
98 bool Foam::probes::checkFieldTypes()
100 wordList fieldTypes(fieldNames_.size());
102 // check files for a particular time
105 forAll(fieldNames_, fieldI)
110 obr_.time().timeName(),
111 refCast<const polyMesh>(obr_),
119 fieldTypes[fieldI] = io.headerClassName();
123 fieldTypes[fieldI] = "(notFound)";
129 // check objectRegistry
130 forAll(fieldNames_, fieldI)
132 objectRegistry::const_iterator iter =
133 obr_.find(fieldNames_[fieldI]);
135 if (iter != obr_.end())
137 fieldTypes[fieldI] = iter()->type();
141 fieldTypes[fieldI] = "(notFound)";
149 // classify fieldTypes
150 nFields += countFields(scalarFields_, fieldTypes);
151 nFields += countFields(vectorFields_, fieldTypes);
152 nFields += countFields(sphericalTensorFields_, fieldTypes);
153 nFields += countFields(symmTensorFields_, fieldTypes);
154 nFields += countFields(tensorFields_, fieldTypes);
156 // concatenate all the lists into foundFields
157 wordList foundFields(nFields);
160 forAll(scalarFields_, i)
162 foundFields[fieldI++] = scalarFields_[i];
164 forAll(vectorFields_, i)
166 foundFields[fieldI++] = vectorFields_[i];
168 forAll(sphericalTensorFields_, i)
170 foundFields[fieldI++] = sphericalTensorFields_[i];
172 forAll(symmTensorFields_, i)
174 foundFields[fieldI++] = symmTensorFields_[i];
176 forAll(tensorFields_, i)
178 foundFields[fieldI++] = tensorFields_[i];
181 if (Pstream::master())
185 fileName probeSubDir = name_;
187 if (obr_.name() != polyMesh::defaultRegion)
189 probeSubDir = probeSubDir/obr_.name();
191 probeSubDir = probeSubDir/obr_.time().timeName();
193 if (Pstream::parRun())
195 // Put in undecomposed case
196 // (Note: gives problems for distributed data running)
197 probeDir = obr_.time().path()/".."/probeSubDir;
201 probeDir = obr_.time().path()/probeSubDir;
204 // Close the file if any fields have been removed.
205 forAllIter(HashPtrTable<OFstream>, probeFilePtrs_, iter)
207 if (findIndex(foundFields, iter.key()) == -1)
211 Pout<< "close stream: " << iter()->name() << endl;
214 delete probeFilePtrs_.remove(iter);
218 // Open new files for new fields. Keep existing files.
220 probeFilePtrs_.resize(2*foundFields.size());
222 forAll(foundFields, fieldI)
224 const word& fldName = foundFields[fieldI];
226 // Check if added field. If so open a stream for it.
228 if (!probeFilePtrs_.found(fldName))
230 // Create directory if does not exist.
233 OFstream* sPtr = new OFstream(probeDir/fldName);
237 Pout<< "open stream: " << sPtr->name() << endl;
240 probeFilePtrs_.insert(fldName, sPtr);
242 unsigned int w = IOstream::defaultPrecision() + 7;
244 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
246 *sPtr<< '#' << setw(IOstream::defaultPrecision() + 6)
247 << vector::componentNames[cmpt];
249 forAll(probeLocations_, probeI)
251 *sPtr<< ' ' << setw(w) << probeLocations_[probeI][cmpt];
256 *sPtr<< '#' << setw(IOstream::defaultPrecision() + 6)
263 Pout<< "Probing fields:" << foundFields << nl
264 << "Probing locations:" << probeLocations_ << nl
274 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
279 const objectRegistry& obr,
280 const dictionary& dict,
281 const bool loadFromFiles
286 loadFromFiles_(loadFromFiles),
291 sphericalTensorFields_(),
301 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
303 Foam::probes::~probes()
307 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
309 void Foam::probes::execute()
311 // Do nothing - only valid on write
315 void Foam::probes::end()
317 // Do nothing - only valid on write
321 void Foam::probes::write()
323 if (probeLocations_.size() && checkFieldTypes())
325 sampleAndWrite(scalarFields_);
326 sampleAndWrite(vectorFields_);
327 sampleAndWrite(sphericalTensorFields_);
328 sampleAndWrite(symmTensorFields_);
329 sampleAndWrite(tensorFields_);
334 void Foam::probes::read(const dictionary& dict)
336 dict.lookup("fields") >> fieldNames_;
337 dict.lookup("probeLocations") >> probeLocations_;
339 // Force all cell locations to be redetermined
340 elementList_.clear();
341 findElements(refCast<const fvMesh>(obr_));
346 // ************************************************************************* //