ENH: patchCloud: return pTraits<Type>::max for unfound points
[OpenFOAM-1.7.x.git] / src / sampling / probes / probesTemplates.C
blobfdd4a1edede0aeaf991634bccacdabdcafa792b2
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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 \*---------------------------------------------------------------------------*/
26 #include "probes.H"
27 #include "volFields.H"
28 #include "IOmanip.H"
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 namespace Foam
35 //- comparison operator for probes class
36 template<class T>
37 class isNotEqOp
39 public:
41     void operator()(T& x, const T& y) const
42     {
43         const T unsetVal(-VGREAT*pTraits<T>::one);
45         if (x != unsetVal)
46         {
47             // Keep x.
49             // Note:chould check for y != unsetVal but multiple sample cells
50             // already handled in read().
51         }
52         else
53         {
54             // x is not set. y might be.
55             x = y;
56         }
57     }
63 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
65 template<class Type>
66 Foam::label Foam::probes::countFields
68     fieldGroup<Type>& fieldList,
69     const wordList& fieldTypes
70 ) const
72     fieldList.setSize(fieldNames_.size());
73     label nFields = 0;
75     forAll(fieldNames_, fieldI)
76     {
77         if
78         (
79             fieldTypes[fieldI]
80          == GeometricField<Type, fvPatchField, volMesh>::typeName
81         )
82         {
83             fieldList[nFields] = fieldNames_[fieldI];
84             nFields++;
85         }
86     }
88     fieldList.setSize(nFields);
90     return nFields;
94 template<class Type>
95 void Foam::probes::sampleAndWrite
97     const GeometricField<Type, fvPatchField, volMesh>& vField
100     Field<Type> values = sample(vField);
102     if (Pstream::master())
103     {
104         unsigned int w = IOstream::defaultPrecision() + 7;
105         OFstream& probeStream = *probeFilePtrs_[vField.name()];
107         probeStream << setw(w) << vField.time().value();
109         forAll(values, probeI)
110         {
111             probeStream << ' ' << setw(w) << values[probeI];
112         }
113         probeStream << endl;
114     }
118 template <class Type>
119 void Foam::probes::sampleAndWrite
121     const fieldGroup<Type>& fields
124     forAll(fields, fieldI)
125     {
126         if (loadFromFiles_)
127         {
128             sampleAndWrite
129             (
130                 GeometricField<Type, fvPatchField, volMesh>
131                 (
132                     IOobject
133                     (
134                         fields[fieldI],
135                         obr_.time().timeName(),
136                         refCast<const polyMesh>(obr_),
137                         IOobject::MUST_READ,
138                         IOobject::NO_WRITE,
139                         false
140                     ),
141                     refCast<const fvMesh>(obr_)
142                 )
143             );
144         }
145         else
146         {
147             objectRegistry::const_iterator iter = obr_.find(fields[fieldI]);
149             if
150             (
151                 iter != obr_.end()
152              && iter()->type()
153              == GeometricField<Type, fvPatchField, volMesh>::typeName
154             )
155             {
156                 sampleAndWrite
157                 (
158                     obr_.lookupObject
159                     <GeometricField<Type, fvPatchField, volMesh> >
160                     (
161                         fields[fieldI]
162                     )
163                 );
164             }
165         }
166     }
170 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
172 template<class Type>
173 Foam::tmp<Foam::Field<Type> >
174 Foam::probes::sample
176     const GeometricField<Type, fvPatchField, volMesh>& vField
177 ) const
179     const Type unsetVal(-VGREAT*pTraits<Type>::one);
181     tmp<Field<Type> > tValues
182     (
183         new Field<Type>(probeLocations_.size(), unsetVal)
184     );
186     Field<Type>& values = tValues();
188     forAll(probeLocations_, probeI)
189     {
190         if (elementList_[probeI] >= 0)
191         {
192             values[probeI] = vField[elementList_[probeI]];
193         }
194     }
196     Pstream::listCombineGather(values, isNotEqOp<Type>());
197     Pstream::listCombineScatter(values);
199     return tValues;
203 template<class Type>
204 Foam::tmp<Foam::Field<Type> >
205 Foam::probes::sample(const word& fieldName) const
207     return sample
208     (
209         obr_.lookupObject<GeometricField<Type, fvPatchField, volMesh> >
210         (
211             fieldName
212         )
213     );
217 // ************************************************************************* //