OldTimeField.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) 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 \*---------------------------------------------------------------------------*/
25 
26 #include "OldTimeField.H"
27 
28 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
29 
30 template<class FieldType>
31 const FieldType& Foam::OldTimeField<FieldType>::field() const
32 {
33  return static_cast<const FieldType&>(*this);
34 }
35 
36 
37 template<class FieldType>
39 {
40  return static_cast<FieldType&>(*this);
41 }
42 
43 
44 template<class FieldType>
46 {
47  if (tfield0_.valid())
48  {
49  if (notNull(tfield0_()))
50  {
51  // Propagate to store the old-old field
52  tfield0_.ref().OldTimeField<Field0Type>::storeOldTimesInner();
53 
54  // Set the old-field to this field
55  tfield0_.ref() = field();
56  tfield0_.ref().OldTimeField<Field0Type>::timeIndex_ = timeIndex_;
57 
58  // If we have an old-old field, then the old field is state and
59  // should be written in the same way as the field
60  if (tfield0_().OldTimeField<Field0Type>::tfield0_.valid())
61  {
62  tfield0_.ref().writeOpt() = field().writeOpt();
63  }
64  }
65  else
66  {
67  // Reinstate the old-time field
68  oldTime();
69  }
70  }
71 }
72 
73 
74 template<class FieldType>
76 {
77  if (tfield0_.valid() && notNull(tfield0_()))
78  {
79  if (tfield0_().OldTimeField<Field0Type>::tfield0_.valid())
80  {
81  tfield0_.ref().OldTimeField<Field0Type>::nullOldestTimeInner();
82  }
83  else
84  {
85  tfield0_ = tmp<Field0Type>(NullObjectRef<FieldType>());
86  }
87  }
88 }
89 
90 
91 template<class FieldType>
92 template<class OldTimeBaseField>
93 void Foam::OldTimeField<FieldType>::setBase(const OldTimeBaseField& otbf) const
94 {
95  if (!tfield0_.valid())
96  {
97  otbf.tfield0_ = tmp<typename Field0Type::Base>();
98  }
99  else
100  {
101  otbf.tfield0_ = tmp<typename Field0Type::Base>(tfield0_());
102  }
103 
104  otbf.timeIndex_ = timeIndex_;
105 
106  otbf.setBase();
107 }
108 
109 
110 template<class FieldType>
111 void Foam::OldTimeField<FieldType>::setBase(const nil&) const
112 {}
113 
114 
115 template<class FieldType>
117 {
118  setBase(OldTimeBaseFieldType<FieldType>()(*this));
119 }
120 
121 
122 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
123 
124 template<class FieldType>
126 {
128  (
129  field().name() + "_0",
130  field().time().name(),
131  field().db(),
132  IOobject::READ_IF_PRESENT,
133  IOobject::AUTO_WRITE,
134  field().registerObject()
135  );
136 
137  if (io0.headerOk())
138  {
139  tfield0_ = new Field0Type(io0, field().mesh());
140  setBase();
141 
142  tfield0_.ref().OldTimeField<Field0Type>::timeIndex_ = timeIndex_ - 1;
143  tfield0_.ref().OldTimeField<Field0Type>::setBase();
144 
145  if (!tfield0_.ref().OldTimeField<Field0Type>::readOldTimeIfPresent())
146  {
147  tfield0_.ref().OldTimeField<Field0Type>::oldTime();
148  }
149 
150  return true;
151  }
152  else
153  {
154  return false;
155  }
156 }
157 
158 
159 template<class FieldType>
160 template<template<class> class OtherPrimitiveField>
162 (
163  const IOobject& io,
165 )
166 {
167  copyOldTimes(io.name(), otf);
168 }
169 
170 
171 template<class FieldType>
172 template<template<class> class OtherPrimitiveField>
174 (
175  const word& newName,
177 )
178 {
179  if (otf.tfield0_.valid() && notNull(otf.tfield0_()))
180  {
181  tfield0_ = new Field0Type(newName + "_0", otf.tfield0_());
182  setBase();
183  }
184 }
185 
186 
187 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
188 
189 template<class FieldType>
191 :
192  timeIndex_(timeIndex),
193  tfield0_(nullptr)
194 {}
195 
196 
197 template<class FieldType>
199 :
200  timeIndex_(otf.timeIndex_),
201  tfield0_(nullptr)
202 {
203  if (otf.tfield0_.valid() && notNull(otf.tfield0_()))
204  {
205  tfield0_ = new Field0Type(otf.tfield0_());
206  setBase();
207  }
208 }
209 
210 
211 template<class FieldType>
213 :
214  timeIndex_(otf.timeIndex_),
215  tfield0_(nullptr)
216 {
217  if (otf.tfield0_.valid() && notNull(otf.tfield0_()))
218  {
219  tfield0_ = tmp<Field0Type>(move(otf.tfield0_));
220  setBase();
221  }
222 }
223 
224 
225 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * * //
226 
227 template<class FieldType>
229 {
230  if (tfield0_.valid() && notNull(tfield0_()))
231  {
232  tfield0_ = tmp<Field0Type>();
233  setBase();
234  }
235 }
236 
237 
238 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
239 
240 template<class FieldType>
242 {
243  return
244  field().name().size() > 2
245  && field().name()(field().name().size() - 2, 2) == "_0";
246 }
247 
248 
249 template<class FieldType>
251 {
252  return timeIndex_ == field().time().timeIndex();
253 }
254 
255 
256 template<class FieldType>
258 {
259  // Store if it has not yet been done in this time-step
260  if (!hasStoredOldTimes())
261  {
262  // Propagate through the old-time fields
263  if (!isOldTime())
264  {
265  storeOldTimesInner();
266  }
267 
268  // Update the time index
269  timeIndex_ = field().time().timeIndex();
270  setBase();
271  }
272 }
273 
274 
275 template<class FieldType>
277 {
278  if (tfield0_.valid())
279  {
280  tfield0_ = tmp<Field0Type>();
281  setBase();
282  }
283 }
284 
285 
286 template<class FieldType>
288 {
289  if (!isOldTime())
290  {
291  nullOldestTimeInner();
292  }
293 }
294 
295 
296 template<class FieldType>
298 (
299  const bool includeNull
300 ) const
301 {
302  if (tfield0_.valid())
303  {
304  if (isNull(tfield0_()))
305  {
306  return includeNull;
307  }
308  else
309  {
310  return tfield0_().nOldTimes(includeNull) + 1;
311  }
312  }
313  else
314  {
315  return 0;
316  }
317 }
318 
319 
320 template<class FieldType>
321 const typename Foam::OldTimeField<FieldType>::Field0Type&
323 {
324  if (!tfield0_.valid() || isNull(tfield0_()))
325  {
326  // Old-time field does not yet exist. Create it.
327 
328  // Clear the field0Ptr to ensure the old-time field constructor
329  // does not construct the old-old-time field
330  tfield0_ = tmp<Field0Type>();
331  setBase();
332 
333  // Construct a copy of the field
334  tfield0_ =
336  (
337  IOobject
338  (
339  field().name() + "_0",
340  field().time().name(),
341  field().db(),
344  field().registerObject()
345  ),
346  field()
347  );
348  setBase();
349  }
350  else
351  {
352  // Old-time field exists. Update as necessary.
353  storeOldTimes();
354  }
355 
356  return tfield0_();
357 }
358 
359 
360 template<class FieldType>
361 typename Foam::OldTimeField<FieldType>::Field0Type&
363 {
364  static_cast<const OldTimeField<FieldType>&>(*this).oldTime();
365 
366  // Note: Not tfield0_.ref(), because this might be a base field storing a
367  // reference only. It's valid to un-const this reference because the
368  // derived field is guaranteed to store the non-const pointer.
369  return const_cast<Field0Type&>(tfield0_());
370 }
371 
372 
373 template<class FieldType>
374 const typename Foam::OldTimeField<FieldType>::Field0Type&
376 (
377  const label n
378 ) const
379 {
380  return n == 0 ? field() : oldTime().oldTime(n - 1);
381 }
382 
383 
384 template<class FieldType>
385 typename Foam::OldTimeField<FieldType>::Field0Type&
387 (
388  const label n
389 )
390 {
391  return n == 0 ? fieldRef() : oldTimeRef().oldTimeRef(n - 1);
392 }
393 
394 
395 // ************************************************************************* //
label n
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
const word & name() const
Return name.
Definition: IOobject.H:307
Class to add into field types to provide old-time storage and retrieval.
Definition: OldTimeField.H:113
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 clearOldTimes()
Clear old-time fields.
Definition: OldTimeField.C:276
bool valid() const
Is this temporary object valid,.
Definition: tmpI.H:183
Templated form of IOobject providing type information for file reading and header type checking.
Definition: IOobject.H:530
bool headerOk()
Read header (uses typeGlobalFile to find file) and check.
A class for handling words, derived from string.
Definition: word.H:62
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
bool valid(const PtrList< ModelType > &l)
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
bool notNull(const T &t)
Return true if t is not a reference to the nullObject of type T.
Definition: nullObjectI.H:64
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
bool isNull(const T &t)
Return true if t is a reference to the nullObject of type T.
Definition: nullObjectI.H:58
label timeIndex
Definition: getTimeIndex.H:4