GeometricFieldReuseFunctions.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #ifndef Foam_GeometricFieldReuseFunctions_H
30 #define Foam_GeometricFieldReuseFunctions_H
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 
39 namespace Detail
40 {
41 
42 //- True if tmp GeometricField has a reusable pointer.
43 //
44 // With GeometricField::debug on, check if patches have reusable types
45 template<class Type, template<class> class PatchField, class GeoMesh>
46 inline bool reusable
47 (
49 )
50 {
51  const bool ok = tfld.movable();
52 
54  {
55  for (const auto& p : tfld().boundaryField())
56  {
57  if
58  (
59  !polyPatch::constraintType(p.patch().type())
60  && !isA<typename PatchField<Type>::Calculated>(p)
61  )
62  {
64  << "Attempt to reuse temporary with non-reusable BC "
65  << p.type() << endl;
66 
67  return false;
68  }
69  }
70  }
71 
72  return ok;
73 }
74 
75 } // End namespace Detail
76 
77 
78 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
79 // One-parameter versions
80 
81 template
82 <
83  class TypeR,
84  class Type1,
85  template<class> class PatchField,
86  class GeoMesh
87 >
89 {
90  //- Pass-through to New GeometricField
92  (
94  const word& name,
95  const dimensionSet& dimensions
96  )
97  {
99  (
100  name,
101  f1.mesh(),
102  dimensions
103  );
104  }
105 
106  //- Dissimilar types: return new field
108  (
110  const word& name,
111  const dimensionSet& dimensions
112  )
113  {
114  const auto& f1 = tf1();
115 
117  (
118  name,
119  f1.mesh(),
120  dimensions
121  );
122  }
123 };
124 
125 
126 template<class TypeR, template<class> class PatchField, class GeoMesh>
127 struct reuseTmpGeometricField<TypeR, TypeR, PatchField, GeoMesh>
128 {
129  //- Allow optional copy assignment of the initial content
130  //- for identical input and output types
132  (
134  const word& name,
135  const dimensionSet& dimensions,
136  const bool initCopy = false
137  )
138  {
139  if (Detail::reusable(tf1))
140  {
141  auto& f1 = tf1.constCast();
142 
143  f1.rename(name);
144  f1.dimensions().reset(dimensions);
145  return tf1;
146  }
147 
148  const auto& f1 = tf1();
149 
151  (
152  name,
153  f1.mesh(),
154  dimensions
155  );
156 
157  if (initCopy)
158  {
159  tresult.ref() == tf1();
160  }
161 
162  return tresult;
163  }
164 };
165 
166 
167 //- This global function forwards to reuseTmpGeometricField::New
168 template<class TypeR, template<class> class PatchField, class GeoMesh>
169 tmp
170 <
171  GeometricField<TypeR, PatchField, GeoMesh>
172 > New
173 (
174  const tmp<GeometricField<TypeR, PatchField, GeoMesh>>& tf1,
175  const word& name,
176  const dimensionSet& dimensions,
177  const bool initCopy = false
178 )
179 {
181  (
182  tf1,
183  name,
184  dimensions,
185  initCopy
186  );
187 }
188 
189 
190 template
191 <
192  class TypeR,
193  class Type1,
194  class Type12,
195  class Type2,
196  template<class> class PatchField,
197  class GeoMesh
198 >
199 struct reuseTmpTmpGeometricField
200 {
201  static tmp<GeometricField<TypeR, PatchField, GeoMesh>> New
202  (
205  const word& name,
206  const dimensionSet& dimensions
207  )
208  {
209  const auto& f1 = tf1();
210 
212  (
213  name,
214  f1.mesh(),
215  dimensions
216  );
217  }
218 };
219 
220 
221 template
222 <
223  class TypeR,
224  class Type1,
225  class Type12,
226  template<class> class PatchField,
227  class GeoMesh
228 >
229 struct reuseTmpTmpGeometricField
230 <
231  TypeR, Type1, Type12, TypeR, PatchField, GeoMesh
232 >
233 {
235  (
238  const word& name,
239  const dimensionSet& dimensions
240  )
241  {
242  if (Detail::reusable(tf2))
243  {
244  auto& f2 = tf2.constCast();
245 
246  f2.rename(name);
247  f2.dimensions().reset(dimensions);
248  return tf2;
249  }
250 
251  const auto& f1 = tf1();
252 
254  (
255  name,
256  f1.mesh(),
257  dimensions
258  );
259  }
260 };
261 
262 
263 template
264 <
265  class TypeR,
266  class Type2,
267  template<class> class PatchField,
268  class GeoMesh
269 >
270 struct reuseTmpTmpGeometricField
271 <
272  TypeR, TypeR, TypeR, Type2, PatchField, GeoMesh
273 >
274 {
276  (
279  const word& name,
280  const dimensionSet& dimensions
281  )
282  {
283  if (Detail::reusable(tf1))
284  {
285  auto& f1 = tf1.constCast();
286 
287  f1.rename(name);
288  f1.dimensions().reset(dimensions);
289  return tf1;
290  }
291 
292  const auto& f1 = tf1();
293 
295  (
296  name,
297  f1.mesh(),
298  dimensions
299  );
300  }
301 };
302 
303 
304 template<class TypeR, template<class> class PatchField, class GeoMesh>
305 struct reuseTmpTmpGeometricField
306 <
307  TypeR, TypeR, TypeR, TypeR, PatchField, GeoMesh
308 >
309 {
311  (
314  const word& name,
315  const dimensionSet& dimensions
316  )
317  {
318  if (Detail::reusable(tf1))
319  {
320  auto& f1 = tf1.constCast();
321 
322  f1.rename(name);
323  f1.dimensions().reset(dimensions);
324  return tf1;
325  }
326  if (Detail::reusable(tf2))
327  {
328  auto& f2 = tf2.constCast();
329 
330  f2.rename(name);
331  f2.dimensions().reset(dimensions);
332  return tf2;
333  }
334 
335  const auto& f1 = tf1();
336 
338  (
339  name,
340  f1.mesh(),
341  dimensions
342  );
343  }
344 };
345 
346 
347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 
349 } // End namespace Foam
350 
351 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
352 
353 #endif
354 
355 // ************************************************************************* //
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
Generic GeometricField class.
Definition: areaFieldsFwd.H:50
bool reusable(const tmp< GeometricField< Type, PatchField, GeoMesh >> &tfld)
True if tmp GeometricField has a reusable pointer.
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Definition: dimensionSet.H:105
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
static tmp< GeometricField< TypeR, PatchField, GeoMesh > > New(const GeometricField< Type1, PatchField, GeoMesh > &f1, const word &name, const dimensionSet &dimensions)
Pass-through to New GeometricField.
static tmp< GeometricField< TypeR, PatchField, GeoMesh > > New(const tmp< GeometricField< Type1, PatchField, GeoMesh >> &tf1, const tmp< GeometricField< Type2, PatchField, GeoMesh >> &tf2, const word &name, const dimensionSet &dimensions)
static tmp< GeometricField< Type, PatchField, GeoMesh > > New(const word &name, IOobjectOption::registerOption regOpt, const Mesh &mesh, const dimensionSet &dims, const word &patchFieldType=PatchField< Type >::calculatedType())
Return tmp field (NO_READ, NO_WRITE) from name, mesh, dimensions and patch type. [Takes current timeN...
const Mesh & mesh() const noexcept
Return mesh.
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:484
static bool constraintType(const word &patchType)
Return true if the given type is a constraint type.
Definition: polyPatch.C:269
#define WarningInFunction
Report a warning using Foam::Warning.
const Type * isA(const U &obj)
Check if dynamic_cast to Type is possible.
Definition: typeInfo.H:88
Generic mesh wrapper used by volMesh, surfaceMesh, pointMesh etc.
Definition: GeoMesh.H:42
volScalarField & p
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Namespace for OpenFOAM.
const dimensionSet & dimensions() const noexcept
Return dimensions.