initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / sampling / probes / probesTemplates.C
blob0d6f52a1cd28d546ec5f5f3961779b623d214924
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 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 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
19     for more details.
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 \*---------------------------------------------------------------------------*/
27 #include "probes.H"
28 #include "volFields.H"
29 #include "IOmanip.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 namespace Foam
36 //- comparison operator for probes class
37 template<class T>
38 class isNotEqOp
40 public:
42     void operator()(T& x, const T& y) const
43     {
44         const T unsetVal(-VGREAT*pTraits<T>::one);
46         if (x != unsetVal)
47         {
48             // Keep x.
50             // Note:chould check for y != unsetVal but multiple sample cells
51             // already handled in read().
52         }
53         else
54         {
55             // x is not set. y might be.
56             x = y;
57         }
58     }
64 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
66 template<class Type>
67 Foam::label Foam::probes::countFields
69     fieldGroup<Type>& fieldList,
70     const wordList& fieldTypes
71 ) const
73     fieldList.setSize(fieldNames_.size());
74     label nFields = 0;
76     forAll(fieldNames_, fieldI)
77     {
78         if
79         (
80             fieldTypes[fieldI]
81          == GeometricField<Type, fvPatchField, volMesh>::typeName
82         )
83         {
84             fieldList[nFields] = fieldNames_[fieldI];
85             nFields++;
86         }
87     }
89     fieldList.setSize(nFields);
91     return nFields;
95 template<class Type>
96 void Foam::probes::sampleAndWrite
98     const GeometricField<Type, fvPatchField, volMesh>& vField
101     Field<Type> values = sample(vField);
103     if (Pstream::master())
104     {
105         unsigned int w = IOstream::defaultPrecision() + 7;
106         OFstream& probeStream = *probeFilePtrs_[vField.name()];
108         probeStream << setw(w) << vField.time().value();
110         forAll(values, probeI)
111         {
112             probeStream << ' ' << setw(w) << values[probeI];
113         }
114         probeStream << endl;
115     }
119 template <class Type>
120 void Foam::probes::sampleAndWrite
122     const fieldGroup<Type>& fields
125     forAll(fields, fieldI)
126     {
127         if (loadFromFiles_)
128         {
129             sampleAndWrite
130             (
131                 GeometricField<Type, fvPatchField, volMesh>
132                 (
133                     IOobject
134                     (
135                         fields[fieldI],
136                         obr_.time().timeName(),
137                         refCast<const polyMesh>(obr_),
138                         IOobject::MUST_READ,
139                         IOobject::NO_WRITE,
140                         false
141                     ),
142                     refCast<const fvMesh>(obr_)
143                 )
144             );
145         }
146         else
147         {
148             objectRegistry::const_iterator iter = obr_.find(fields[fieldI]);
150             if
151             (
152                 iter != obr_.end()
153              && iter()->type()
154              == GeometricField<Type, fvPatchField, volMesh>::typeName
155             )
156             {
157                 sampleAndWrite
158                 (
159                     obr_.lookupObject
160                     <GeometricField<Type, fvPatchField, volMesh> >
161                     (
162                         fields[fieldI]
163                     )
164                 );
165             }
166         }
167     }
171 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
173 template<class Type>
174 Foam::tmp<Foam::Field<Type> >
175 Foam::probes::sample
177     const GeometricField<Type, fvPatchField, volMesh>& vField
178 ) const
180     const Type unsetVal(-VGREAT*pTraits<Type>::one);
182     tmp<Field<Type> > tValues
183     (
184         new Field<Type>(probeLocations_.size(), unsetVal)
185     );
187     Field<Type>& values = tValues();
189     forAll(probeLocations_, probeI)
190     {
191         if (cellList_[probeI] >= 0)
192         {
193             values[probeI] = vField[cellList_[probeI]];
194         }
195     }
197     Pstream::listCombineGather(values, isNotEqOp<Type>());
198     Pstream::listCombineScatter(values);
200     return tValues;
204 template<class Type>
205 Foam::tmp<Foam::Field<Type> >
206 Foam::probes::sample(const word& fieldName) const
208     return sample
209     (
210         obr_.lookupObject<GeometricField<Type, fvPatchField, volMesh> >
211         (
212             fieldName
213         )
214     );
218 // ************************************************************************* //