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