OldTimeField.H
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) 2024-2025 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 Class
25  Foam::OldTimeField
26 
27 Description
28  Class to add into field types to provide old-time storage and retrieval
29 
30 SourceFiles
31  OldTimeFieldI.H
32  OldTimeField.C
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef OldTimeField_H
37 #define OldTimeField_H
38 
39 #include "Time.H"
40 #include "VoidT.H"
41 #include "GeometricFieldFwd.H"
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 // Forward declaration of classes
49 template<class FieldType>
50 class OldTimeField;
51 
52 
53 /*---------------------------------------------------------------------------*\
54  Class OldTimeField0Type Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 template<class FieldType>
58 struct OldTimeField0Type;
59 
60 template
61 <
62  template<class, class, template<class> class> class GeoField,
63  class Type,
64  class GeoMesh,
65  template<class> class PrimitiveField
66 >
67 struct OldTimeField0Type<GeoField<Type, GeoMesh, PrimitiveField>>
68 {
69  typedef GeoField<Type, GeoMesh, PrimitiveField> type;
70 };
71 
72 template<class Type>
73 class SubField;
74 
75 template
76 <
77  template<class, class, template<class> class> class GeoField,
78  class Type,
79  class GeoMesh
80 >
81 struct OldTimeField0Type<GeoField<Type, GeoMesh, SubField>>
82 {
83  typedef GeoField<Type, GeoMesh, Field> type;
84 };
85 
86 
87 /*---------------------------------------------------------------------------*\
88  Class OldTimeOtherFieldType Declaration
89 \*---------------------------------------------------------------------------*/
90 
91 template<class FieldType>
93 
94 template
95 <
96  template<class, class, template<class> class> class GeoField,
97  class Type,
98  class GeoMesh,
99  template<class> class PrimitiveField
100 >
101 struct OldTimeOtherFieldType<GeoField<Type, GeoMesh, PrimitiveField>>
102 {
103  template<template<class> class OtherPrimitiveField>
104  using type = GeoField<Type, GeoMesh, OtherPrimitiveField>;
105 };
106 
107 
108 /*---------------------------------------------------------------------------*\
109  Class OldTimeField Declaration
110 \*---------------------------------------------------------------------------*/
111 
112 template<class FieldType>
113 class OldTimeField
114 {
115 private:
116 
117  // Private Typedefs
118 
119  //- The old-time field type
120  typedef typename OldTimeField0Type<FieldType>::type Field0Type;
121 
122  //- The field type based on a different primitive field type
123  template<template<class> class OtherPrimitiveField>
124  using OtherFieldType =
126  template type<OtherPrimitiveField>;
127 
128  //- The old-time field class
130 
131  //- The old-time field class based on a different primitive field type
132  template<template<class> class OtherPrimitiveField>
133  using OtherOldTime =
135 
136 
137  // Private Data
138 
139  //- Current time index
140  mutable label timeIndex_;
141 
142  //- The old-time field pointer or reference
143  mutable tmp<Field0Type> tfield0_;
144 
145 
146  // Private Member Functions
147 
148  //- Get a reference to the field
149  const FieldType& field() const;
150 
151  //- Get a non-const reference to the field
152  FieldType& fieldRef();
153 
154  //- Store the old-time fields. Inner recursion.
155  void storeOldTimesInner() const;
156 
157  //- Set the oldest field pointer to nullObjectPtr. Inner recursion.
158  void nullOldestTimeInner();
159 
160  //- Set the field reference in the base class. Overload for when the
161  // base class is not an old-time field.
162  void setBase(const nil&) const;
163 
164  //- Set the field reference in the base class. Overload for when the
165  // base class is an old-time field.
166  template<class OldTimeBaseField>
167  void setBase(const OldTimeBaseField& otbf) const;
168 
169  //- Set the field reference in the base class
170  void setBase() const;
171 
172 
173 protected:
174 
175  // Protected Member Functions
176 
177  //- Read old-time field from file if it is present
178  bool readOldTimeIfPresent();
179 
180  //- Copy the old-times from the given field
181  template<template<class> class OtherPrimitiveField>
182  void copyOldTimes
183  (
184  const IOobject& io,
186  );
187 
188  //- Copy the old-times from the given field
189  template<template<class> class OtherPrimitiveField>
190  void copyOldTimes
191  (
192  const word& newName,
194  );
195 
196 
197 public:
198 
199  //- Declare friendship with other old-time fields
200  template<class OtherFieldType>
201  friend class OldTimeField;
202 
203  //- Declare friendship with the base old-time field casting struct
204  template<class OtherFieldType, typename>
205  friend struct OldTimeBaseFieldType;
206 
207 
208  // Constructors
209 
210  //- Construct from a time index
212 
213  //- Copy construct
215 
216  //- Move construct
218 
219 
220  //- Destructor
221  ~OldTimeField();
222 
223 
224  // Member Functions
225 
226  //- Return the time index of the field
227  inline label timeIndex() const;
228 
229  //- Return a non-const reference to the time index of the field
230  inline label& timeIndex();
231 
232  //- Return whether or not this is an old-time field
233  bool isOldTime() const;
234 
235  //- Return whether old-time fields have been stored yet
236  bool hasStoredOldTimes() const;
237 
238  //- Store the old-time fields
239  void storeOldTimes() const;
240 
241  //- Clear old-time fields
242  void clearOldTimes();
243 
244  //- Set the oldest field pointer to nullObjectPtr. This removes the
245  // field whilst maintaining a tag distinct from nullptr so that the
246  // field can be reinstated on the next storeOldTimes.
247  void nullOldestTime();
248 
249  //- Return the number of old-time fields stored
250  label nOldTimes(const bool includeNull=true) const;
251 
252  //- Return the old-time field
253  const Field0Type& oldTime() const;
254 
255  //- Return a non-const reference to the old-time field
256  Field0Type& oldTimeRef();
257 
258  //- Return the n-th old-time field
259  const Field0Type& oldTime(const label n) const;
260 
261  //- Return a non-const reference to the n-th old time field
262  Field0Type& oldTimeRef(const label n);
263 
264 
265  // Member Operators
266 
267  //- Disallow default bitwise assignment
268  void operator=(const OldTimeField<FieldType>&) = delete;
269 };
270 
271 
272 /*---------------------------------------------------------------------------*\
273  Struct OldTimeBaseFieldType Declaration
274 \*---------------------------------------------------------------------------*/
275 
276 template<class FieldType, typename = void>
278 {
279  nil operator()(const OldTimeField<FieldType>& otf) const
280  {
281  return nil();
282  }
283 };
284 
285 template<class FieldType>
286 struct OldTimeBaseFieldType<FieldType, VoidT<typename FieldType::Base::OldTime>>
287 {
288  typedef typename FieldType::Base::OldTime type;
289 
290  const type& operator()(const OldTimeField<FieldType>& otf) const
291  {
292  return static_cast<const type&>(otf.field());
293  }
294 };
295 
296 
297 /*---------------------------------------------------------------------------*\
298  Struct OldTimeFieldCopy Declaration
299 \*---------------------------------------------------------------------------*/
300 
301 template<class FieldType>
302 struct OldTimeFieldCopy
303 {
305 
306  tmp<Field0Type> operator()(const IOobject& io, const FieldType& field)
307  {
308  return tmp<Field0Type>(new Field0Type(io, field));
309  }
310 };
311 
312 template<class Type, class GeoMesh, template<class> class PrimitiveField>
313 struct OldTimeFieldCopy<GeometricField<Type, GeoMesh, PrimitiveField>>
314 {
316 
318 
319  typedef typename FieldType::Patch::Calculated Calculated;
320 
321  tmp<Field0Type> operator()(const IOobject& io, const FieldType& field)
322  {
323  return tmp<Field0Type>(new Field0Type(io, field, Calculated::typeName));
324  }
325 };
326 
327 
328 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
329 
330 } // End namespace Foam
331 
332 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
333 
334 #define USING_OLD_TIME_FIELD(FieldType) \
335  using OldTimeField<FieldType>::readOldTimeIfPresent; \
336  using OldTimeField<FieldType>::copyOldTimes; \
337  using OldTimeField<FieldType>::timeIndex; \
338  using OldTimeField<FieldType>::isOldTime; \
339  using OldTimeField<FieldType>::hasStoredOldTimes; \
340  using OldTimeField<FieldType>::storeOldTimes; \
341  using OldTimeField<FieldType>::clearOldTimes; \
342  using OldTimeField<FieldType>::nullOldestTime; \
343  using OldTimeField<FieldType>::nOldTimes; \
344  using OldTimeField<FieldType>::oldTime; \
345  using OldTimeField<FieldType>::oldTimeRef;
346 
347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 
349 #include "OldTimeFieldI.H"
350 
351 #ifdef NoRepository
352  #include "OldTimeField.C"
353 #endif
354 
355 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
356 
357 #endif
358 
359 // ************************************************************************* //
label n
Generic mesh wrapper used by volMesh, surfaceMesh, pointMesh etc.
Definition: GeoMesh.H:47
Generic GeometricField class.
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
Class to add into field types to provide old-time storage and retrieval.
Definition: OldTimeField.H:113
label timeIndex() const
Return the time index of the field.
Definition: OldTimeFieldI.H:31
bool readOldTimeIfPresent()
Read old-time field from file if it is present.
Definition: OldTimeField.C:125
friend class OldTimeField
Declare friendship with other old-time fields.
Definition: OldTimeField.H:200
void copyOldTimes(const IOobject &io, const OtherOldTime< OtherPrimitiveField > &)
Copy the old-times from the given field.
Definition: OldTimeField.C:162
label nOldTimes(const bool includeNull=true) const
Return the number of old-time fields stored.
Definition: OldTimeField.C:298
const Field0Type & oldTime() const
Return the old-time field.
Definition: OldTimeField.C:322
Field0Type & oldTimeRef()
Return a non-const reference to the old-time field.
Definition: OldTimeField.C:362
bool hasStoredOldTimes() const
Return whether old-time fields have been stored yet.
Definition: OldTimeField.C:250
bool isOldTime() const
Return whether or not this is an old-time field.
Definition: OldTimeField.C:241
void storeOldTimes() const
Store the old-time fields.
Definition: OldTimeField.C:257
~OldTimeField()
Destructor.
Definition: OldTimeField.C:228
void nullOldestTime()
Set the oldest field pointer to nullObjectPtr. This removes the.
Definition: OldTimeField.C:287
void operator=(const OldTimeField< FieldType > &)=delete
Disallow default bitwise assignment.
void clearOldTimes()
Clear old-time fields.
Definition: OldTimeField.C:276
Pre-declare related SubField type.
Definition: SubField.H:63
A zero-sized class without any storage. Used, for example, in HashSet.
Definition: nil.H:59
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
void VoidT
Definition: VoidT.H:42
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
nil operator()(const OldTimeField< FieldType > &otf) const
Definition: OldTimeField.H:278
tmp< Field0Type > operator()(const IOobject &io, const FieldType &field)
Definition: OldTimeField.H:305
OldTimeField0Type< FieldType >::type Field0Type
Definition: OldTimeField.H:303