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 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<FieldType>::storeOldTimesInner();
53 
54  // Set the old-field to this field
55  tfield0_.ref() == field();
56  tfield0_.ref().OldTimeField<FieldType>::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<FieldType>::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<FieldType>::tfield0_.valid())
80  {
81  tfield0_.ref().OldTimeField<FieldType>::nullOldestTimeInner();
82  }
83  else
84  {
85  tfield0_ = tmp<FieldType>(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_.clear();
98  }
99  else
100  {
101  otbf.tfield0_ = tmp<typename FieldType::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 FieldType(io0, field().mesh());
140  setBase();
141 
142  tfield0_.ref().OldTimeField<FieldType>::timeIndex_ = timeIndex_ - 1;
143  tfield0_.ref().OldTimeField<FieldType>::setBase();
144 
145  if (!tfield0_.ref().OldTimeField<FieldType>::readOldTimeIfPresent())
146  {
147  tfield0_.ref().OldTimeField<FieldType>::oldTime();
148  }
149 
150  return true;
151  }
152  else
153  {
154  return false;
155  }
156 }
157 
158 
159 template<class FieldType>
161 (
162  const IOobject& io,
163  const OldTimeField<FieldType>& otf
164 )
165 {
166  copyOldTimes(io.name(), otf);
167 }
168 
169 
170 template<class FieldType>
172 (
173  const word& newName,
174  const OldTimeField<FieldType>& otf
175 )
176 {
177  if (otf.tfield0_.valid() && notNull(otf.tfield0_()))
178  {
179  tfield0_ = new FieldType(newName + "_0", otf.tfield0_());
180  setBase();
181  }
182 }
183 
184 
185 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
186 
187 template<class FieldType>
189 :
190  timeIndex_(timeIndex),
191  tfield0_(nullptr)
192 {}
193 
194 
195 template<class FieldType>
197 :
198  timeIndex_(otf.timeIndex_),
199  tfield0_(nullptr)
200 {
201  if (otf.tfield0_.valid() && notNull(otf.tfield0_()))
202  {
203  tfield0_ = new FieldType(otf.tfield0_());
204  setBase();
205  }
206 }
207 
208 
209 template<class FieldType>
211 :
212  timeIndex_(otf.timeIndex_),
213  tfield0_(nullptr)
214 {
215  if (otf.tfield0_.valid() && notNull(otf.tfield0_()))
216  {
217  tfield0_ = tmp<FieldType>(move(otf.tfield0_));
218  setBase();
219  }
220 }
221 
222 
223 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * * //
224 
225 template<class FieldType>
227 {
228  if (tfield0_.valid() && notNull(tfield0_()))
229  {
230  tfield0_.clear();
231  setBase();
232  }
233 }
234 
235 
236 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
237 
238 template<class FieldType>
240 {
241  return
242  field().name().size() > 2
243  && field().name()(field().name().size() - 2, 2) == "_0";
244 }
245 
246 
247 template<class FieldType>
249 {
250  // Store if the time index is not up to date with database's index
251  if
252  (
253  tfield0_.valid()
254  && timeIndex_ != field().time().timeIndex()
255  && !isOldTime()
256  )
257  {
258  storeOldTimesInner();
259  }
260 
261  // Correct time index
262  if (timeIndex_ != field().time().timeIndex())
263  {
264  timeIndex_ = field().time().timeIndex();
265  setBase();
266  }
267 }
268 
269 
270 template<class FieldType>
272 {
273  if (tfield0_.valid() && notNull(tfield0_()))
274  {
275  tfield0_.clear();
276  }
277 }
278 
279 
280 template<class FieldType>
282 {
283  if (!isOldTime())
284  {
285  nullOldestTimeInner();
286  }
287 }
288 
289 
290 template<class FieldType>
292 (
293  const bool includeNull
294 ) const
295 {
296  if (tfield0_.valid())
297  {
298  if (isNull(tfield0_()))
299  {
300  return includeNull;
301  }
302  else
303  {
304  return tfield0_->nOldTimes(includeNull) + 1;
305  }
306  }
307  else
308  {
309  return 0;
310  }
311 }
312 
313 
314 template<class FieldType>
316 {
317  if (!tfield0_.valid() || isNull(tfield0_()))
318  {
319  // Old-time field does not yet exist. Create it.
320 
321  // Clear the field0Ptr to ensure the old-time field constructor
322  // does not construct the old-old-time field
323  tfield0_.clear();
324  setBase();
325 
326  // Construct a copy of the field
327  tfield0_ = new FieldType
328  (
329  IOobject
330  (
331  field().name() + "_0",
332  field().time().name(),
333  field().db(),
336  field().registerObject()
337  ),
338  field()
339  );
340  setBase();
341  }
342  else
343  {
344  // Old-time field exists. Update as necessary.
345  storeOldTimes();
346  }
347 
348  return tfield0_();
349 }
350 
351 
352 template<class FieldType>
354 {
355  static_cast<const OldTimeField<FieldType>&>(*this).oldTime();
356 
357  // Note: Not tfield0_.ref(), because this might be a base field storing a
358  // reference only. It's valid to un-const this reference because the
359  // derived field is guaranteed to store the non-const pointer.
360  return const_cast<FieldType&>(tfield0_());
361 }
362 
363 
364 template<class FieldType>
365 const FieldType& Foam::OldTimeField<FieldType>::oldTime(const label n) const
366 {
367  return n == 0 ? field() : oldTime().oldTime(n - 1);
368 }
369 
370 
371 template<class FieldType>
373 {
374  return n == 0 ? fieldRef() : oldTimeRef().oldTimeRef(n - 1);
375 }
376 
377 
378 // ************************************************************************* //
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:310
Class to add into field types to provide old-time storage and retrieval.
Definition: OldTimeField.H:93
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:166
const FieldType & oldTime() const
Return the old time field.
Definition: OldTimeField.C:315
label nOldTimes(const bool includeNull=true) const
Return the number of old time fields stored.
Definition: OldTimeField.C:292
void copyOldTimes(const IOobject &io, const OldTimeField< FieldType > &otf)
Copy the old times from the given field.
Definition: OldTimeField.C:161
FieldType & oldTimeRef()
Return a non-const reference to the old time field.
Definition: OldTimeField.C:353
bool isOldTime() const
Return whether or not this is an old-time field.
Definition: OldTimeField.C:239
void storeOldTimes() const
Store the old-time fields.
Definition: OldTimeField.C:248
~OldTimeField()
Destructor.
Definition: OldTimeField.C:226
void nullOldestTime()
Set the oldest field pointer to nullObjectPtr. This removes the.
Definition: OldTimeField.C:281
void clearOldTimes()
Clear old time fields.
Definition: OldTimeField.C:271
bool valid() const
Is this temporary object valid,.
Definition: tmpI.H:167
void clear() const
If object pointer points to valid object:
Definition: tmpI.H:237
Templated form of IOobject providing type information for file reading and header type checking.
Definition: IOobject.H:531
bool headerOk()
Read header (uses typeGlobalFile to find file) and check.
A class for handling words, derived from string.
Definition: word.H:62
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
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
bool notNull(const T &t)
Return true if t is not a reference to the nullObject of type T.
Definition: nullObjectI.H:64
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