probesTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "probes.H"
27 #include "volFields.H"
28 #include "surfaceFields.H"
29 #include "IOmanip.H"
30 #include "interpolation.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 template<class T>
38 class isNotEqOp
39 {
40 public:
41 
42  void operator()(T& x, const T& y) const
43  {
44  const T unsetVal(-vGreat*pTraits<T>::one);
45 
46  if (x != unsetVal)
47  {
48  // Keep x.
49 
50  // Note: should 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  }
59 };
60 
61 }
62 
63 
64 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
65 
66 template<class Type>
67 void Foam::probes::sampleAndWrite(const VolField<Type>& vField)
68 {
69  Field<Type> values(sample(vField));
70 
71  if (Pstream::master())
72  {
73  const unsigned int w = IOstream::defaultPrecision() + 7;
74  OFstream& os = probeFilePtrs_[vField.name()];
75 
76  os << setw(w) << vField.time().userTimeValue();
77 
78  forAll(values, probei)
79  {
80  OStringStream buf;
81  buf << values[probei];
82  os << ' ' << setw(w) << buf.str().c_str();
83  }
84  os << endl;
85  }
86 }
87 
88 
89 template<class Type>
90 void Foam::probes::sampleAndWrite(const SurfaceField<Type>& sField)
91 {
92  Field<Type> values(sample(sField));
93 
94  if (Pstream::master())
95  {
96  const unsigned int w = IOstream::defaultPrecision() + 7;
97  OFstream& os = probeFilePtrs_[sField.name()];
98 
99  os << sField.time().userTimeValue();
100 
101  forAll(values, probei)
102  {
103  OStringStream buf;
104  buf << values[probei];
105  os << ' ' << setw(w) << buf.str().c_str();
106  }
107  os << endl;
108  }
109 }
110 
111 
112 template<class Type>
113 void Foam::probes::sampleAndWrite(const fieldGroup<Type>& fields)
114 {
115  forAll(fields, fieldi)
116  {
117  objectRegistry::const_iterator iter = mesh_.find(fields[fieldi]);
118 
119  if
120  (
121  iter != objectRegistry::end()
122  && iter()->type() == VolField<Type>::typeName
123  )
124  {
125  sampleAndWrite
126  (
127  mesh_.lookupObject<VolField<Type>>
128  (
129  fields[fieldi]
130  )
131  );
132  }
133  }
134 }
135 
136 
137 template<class Type>
138 void Foam::probes::sampleAndWriteSurfaceFields(const fieldGroup<Type>& fields)
139 {
140  forAll(fields, fieldi)
141  {
142  objectRegistry::const_iterator iter = mesh_.find(fields[fieldi]);
143 
144  if
145  (
146  iter != objectRegistry::end()
147  && iter()->type() == SurfaceField<Type>::typeName
148  )
149  {
150  sampleAndWrite
151  (
152  mesh_.lookupObject<SurfaceField<Type>>
153  (
154  fields[fieldi]
155  )
156  );
157  }
158  }
159 }
160 
161 
162 template<class Type>
164 Foam::probes::sample(const VolField<Type>& vField) const
165 {
166  const Type unsetVal(-vGreat*pTraits<Type>::one);
167 
168  tmp<Field<Type>> tValues
169  (
170  new Field<Type>(locations_.size(), unsetVal)
171  );
172 
173  Field<Type>& values = tValues.ref();
174 
175  if (fixedLocations_)
176  {
177  autoPtr<interpolation<Type>> interpolator
178  (
179  interpolation<Type>::New(interpolationScheme_, vField)
180  );
181 
182  forAll(locations_, probei)
183  {
184  if (cellList_[probei] >= 0)
185  {
186  const vector& position = locations_[probei];
187 
188  values[probei] = interpolator().interpolate
189  (
190  position,
191  cellList_[probei],
192  -1
193  );
194  }
195  }
196  }
197  else
198  {
199  forAll(locations_, probei)
200  {
201  if (cellList_[probei] >= 0)
202  {
203  values[probei] = vField[cellList_[probei]];
204  }
205  }
206  }
207 
210 
211  return tValues;
212 }
213 
214 
215 template<class Type>
217 Foam::probes::sample(const SurfaceField<Type>& sField) const
218 {
219  const Type unsetVal(-vGreat*pTraits<Type>::one);
220 
221  tmp<Field<Type>> tValues
222  (
223  new Field<Type>(locations_.size(), unsetVal)
224  );
225 
226  Field<Type>& values = tValues.ref();
227 
228  forAll(locations_, probei)
229  {
230  if (faceList_[probei] >= 0)
231  {
232  values[probei] = sField[faceList_[probei]];
233  }
234  }
235 
238 
239  return tValues;
240 }
241 
242 
243 template<class Type>
245 Foam::probes::sample(const word& fieldName) const
246 {
247  return sample
248  (
249  mesh_.lookupObject<VolField<Type>>
250  (
251  fieldName
252  )
253  );
254 }
255 
256 
257 template<class Type>
259 Foam::probes::sampleSurfaceFields(const word& fieldName) const
260 {
261  return sample
262  (
263  mesh_.lookupObject<SurfaceField<Type>>
264  (
265  fieldName
266  )
267  );
268 }
269 
270 
271 // ************************************************************************* //
Istream and Ostream manipulators taking arguments.
scalar y
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Pre-declare SubField and related Field type.
Definition: Field.H:83
Generic GeometricField class.
friend class const_iterator
Declare friendship with the const_iterator.
Definition: HashTable.H:197
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:473
static void listCombineGather(const List< commsStruct > &comms, List< T > &Value, const CombineOp &cop, const int tag, const label comm)
static void listCombineScatter(const List< commsStruct > &comms, List< T > &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
Abstract base class for interpolation.
void operator()(T &x, const T &y) const
Traits class for primitives.
Definition: pTraits.H:53
HashPtrTable< OFstream > probeFilePtrs_
Current open files.
Definition: probes.H:134
A class for managing temporary objects.
Definition: tmp.H:55
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:197
A class for handling words, derived from string.
Definition: word.H:63
Info<< "Calculating turbulent flame speed field St\n"<< endl;volScalarField St(IOobject("St", runTime.name(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE), flameWrinkling->Xi() *Su);multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:234
point position(const polyMesh &mesh, const barycentric &coordinates, const label celli, const label facei, const label faceTrii, const scalar stepFraction)
Return the position given the coordinates and tet topology.
Definition: trackingI.H:224
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
String typeName(const std::type_info &info)
Return the un-mangled name given the standard type info.
void T(GeometricField< Type, GeoMesh, PrimitiveField1 > &gf, const GeometricField< Type, GeoMesh, PrimitiveField2 > &gf1)
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:112
Foam::surfaceFields.