File size: 11,763 Bytes
5a600cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | /*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2024-2026 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "OldTimeField.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class FieldType>
const FieldType& Foam::OldTimeField<FieldType>::field() const
{
return static_cast<const FieldType&>(*this);
}
template<class FieldType>
FieldType& Foam::OldTimeField<FieldType>::fieldRef()
{
return static_cast<FieldType&>(*this);
}
template<class FieldType>
void Foam::OldTimeField<FieldType>::storeOldTimesInner(const nil&) const
{}
template<class FieldType>
template<class OldTimeBaseField>
void Foam::OldTimeField<FieldType>::storeOldTimesInner
(
const OldTimeBaseField& otbf
) const
{
otbf.storeOldTimesInner();
}
template<class FieldType>
void Foam::OldTimeField<FieldType>::storeOldTimesInner() const
{
if (tfield0_.valid())
{
if (notNull(tfield0_()) && tfield0_.isTmp())
{
// Propagate to store the old-old field
tfield0_.ref().OldTimeField<Field0Type>::storeOldTimesInner();
// Set the old-field to this field
tfield0_.ref() = field();
tfield0_.ref().OldTimeField<Field0Type>::timeIndex_ = timeIndex_;
// If we have an old-old field, then the old field is state and
// should be written in the same way as the field
if (tfield0_().OldTimeField<Field0Type>::tfield0_.valid())
{
tfield0_.ref().writeOpt() = field().writeOpt();
}
}
else if (isNull(tfield0_()))
{
// Reinstate the old-time field
resetOldTime();
}
else // if (notNull(tfield0_()) && !tfield0_.isTmp())
{
#ifdef FULLDEBUG
WarningInFunction
<< field().typeName << " old-time for field "
<< field().name() << " allocated despite derived "
<< field().type() << " old-time existing because the latter "
<< "has not yet been updated" << endl;
#endif
resetOldTime();
}
}
else
{
storeOldTimesInner(OldTimeBaseFieldType<FieldType>()(*this));
}
}
template<class FieldType>
void Foam::OldTimeField<FieldType>::nullOldestTimeInner()
{
if (tfield0_.valid() && notNull(tfield0_()))
{
if (tfield0_().OldTimeField<Field0Type>::tfield0_.valid())
{
if (!tfield0_.isTmp())
{
FatalErrorInFunction
<< field().typeName << "::nullOldestTime called for field "
<< field().name() << " rather than " << field().type()
<< "::nullOldestTime" << exit(FatalError);
}
tfield0_.ref().OldTimeField<Field0Type>::nullOldestTimeInner();
}
else
{
tfield0_ = tmp<Field0Type>(NullObjectRef<Field0Type>());
}
}
}
template<class FieldType>
template<class OldTimeBaseField>
void Foam::OldTimeField<FieldType>::setBase
(
const OldTimeBaseField& otbf,
const bool clear
) const
{
#ifdef FULLDEBUG
const bool baseHasOldTime = otbf.tfield0_.valid() && otbf.tfield0_.isTmp();
if ((clear || tfield0_.valid()) && baseHasOldTime)
{
WarningInFunction
<< otbf.field().typeName << " old-time for field "
<< field().name() << " overridden by derived "
<< field().typeName << " old-time" << endl;
}
#endif
if (clear && !tfield0_.valid())
{
otbf.tfield0_ = tmp<typename Field0Type::Base>();
}
else if (tfield0_.valid())
{
otbf.tfield0_ =
tmp<typename Field0Type::Base>
(
isNull(tfield0_())
? NullObjectRef<typename Field0Type::Base>()
: static_cast<const typename Field0Type::Base&>(tfield0_())
);
}
otbf.timeIndex_ = timeIndex_;
otbf.setBase(clear);
}
template<class FieldType>
void Foam::OldTimeField<FieldType>::setBase(const nil&, const bool clear) const
{}
template<class FieldType>
void Foam::OldTimeField<FieldType>::setBase(const bool clear) const
{
setBase(OldTimeBaseFieldType<FieldType>()(*this), clear);
}
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
template<class FieldType>
bool Foam::OldTimeField<FieldType>::readOldTimeIfPresent()
{
typeIOobject<Field0Type> io0
(
field().name() + "_0",
field().time().name(),
field().db(),
IOobject::READ_IF_PRESENT,
IOobject::AUTO_WRITE,
field().registerObject()
);
if (io0.headerOk())
{
tfield0_ = new Field0Type(io0, field().mesh());
setBase();
tfield0_.ref().OldTimeField<Field0Type>::timeIndex_ = timeIndex_ - 1;
tfield0_.ref().OldTimeField<Field0Type>::setBase();
if (!tfield0_.ref().OldTimeField<Field0Type>::readOldTimeIfPresent())
{
tfield0_.ref().OldTimeField<Field0Type>::oldTime();
}
return true;
}
else
{
return false;
}
}
template<class FieldType>
template<template<class> class OtherPrimitiveField>
void Foam::OldTimeField<FieldType>::copyOldTimes
(
const IOobject& io,
const OtherOldTime<OtherPrimitiveField>& otf
)
{
copyOldTimes(io.name(), otf);
}
template<class FieldType>
template<template<class> class OtherPrimitiveField>
void Foam::OldTimeField<FieldType>::copyOldTimes
(
const word& newName,
const OtherOldTime<OtherPrimitiveField>& otf
)
{
if (otf.tfield0_.valid() && otf.tfield0_.isTmp() && notNull(otf.tfield0_()))
{
tfield0_ = new Field0Type(newName + "_0", otf.tfield0_());
setBase();
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class FieldType>
Foam::OldTimeField<FieldType>::OldTimeField(const label timeIndex)
:
timeIndex_(timeIndex),
tfield0_(nullptr)
{}
template<class FieldType>
Foam::OldTimeField<FieldType>::OldTimeField(const OldTimeField<FieldType>& otf)
:
timeIndex_(otf.timeIndex_),
tfield0_(nullptr)
{
if (otf.tfield0_.valid() && otf.tfield0_.isTmp() && notNull(otf.tfield0_()))
{
tfield0_ = new Field0Type(otf.tfield0_());
setBase();
}
}
template<class FieldType>
Foam::OldTimeField<FieldType>::OldTimeField(OldTimeField<FieldType>&& otf)
:
timeIndex_(otf.timeIndex_),
tfield0_(nullptr)
{
if (otf.tfield0_.valid() && otf.tfield0_.isTmp() && notNull(otf.tfield0_()))
{
tfield0_ = tmp<Field0Type>(move(otf.tfield0_));
setBase();
}
}
// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * * //
template<class FieldType>
Foam::OldTimeField<FieldType>::~OldTimeField()
{
if (tfield0_.valid() && notNull(tfield0_()))
{
tfield0_ = tmp<Field0Type>();
setBase();
}
}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
template<class FieldType>
bool Foam::OldTimeField<FieldType>::isOldTime() const
{
return
field().name().size() > 2
&& field().name()(field().name().size() - 2, 2) == "_0";
}
template<class FieldType>
bool Foam::OldTimeField<FieldType>::hasStoredOldTimes() const
{
return timeIndex_ == field().time().timeIndex();
}
template<class FieldType>
void Foam::OldTimeField<FieldType>::resetOldTime() const
{
// Clear the field0Ptr to ensure the old-time field constructor
// does not construct the old-old-time field
tfield0_ = tmp<Field0Type>();
setBase();
// Construct a copy of the field
tfield0_ =
OldTimeFieldCopy<FieldType>()
(
IOobject
(
field().name() + "_0",
field().time().name(),
field().db(),
IOobject::NO_READ,
IOobject::NO_WRITE,
field().registerObject()
),
field()
);
setBase();
// Update the time index
timeIndex_ = field().time().timeIndex();
setBase();
}
template<class FieldType>
void Foam::OldTimeField<FieldType>::storeOldTimes() const
{
// Store if it has not yet been done in this time-step
if (!hasStoredOldTimes())
{
// Propagate through the old-time fields
if (!isOldTime())
{
storeOldTimesInner();
}
// Update the time index
timeIndex_ = field().time().timeIndex();
setBase(false);
}
}
template<class FieldType>
void Foam::OldTimeField<FieldType>::clearOldTimes()
{
if (tfield0_.valid())
{
tfield0_ = tmp<Field0Type>();
setBase();
}
}
template<class FieldType>
void Foam::OldTimeField<FieldType>::nullOldestTime()
{
if (!isOldTime())
{
nullOldestTimeInner();
setBase();
}
}
template<class FieldType>
Foam::label Foam::OldTimeField<FieldType>::nOldTimes
(
const bool includeNull
) const
{
if (tfield0_.valid())
{
if (isNull(tfield0_()))
{
return includeNull;
}
else
{
return tfield0_().nOldTimes(includeNull) + 1;
}
}
else
{
return 0;
}
}
template<class FieldType>
const typename Foam::OldTimeField<FieldType>::Field0Type&
Foam::OldTimeField<FieldType>::oldTime() const
{
if (!tfield0_.valid() || isNull(tfield0_()))
{
// Old-time field does not yet exist. Create it.
resetOldTime();
}
else
{
// Old-time field exists. Update as necessary.
storeOldTimes();
}
return tfield0_();
}
template<class FieldType>
typename Foam::OldTimeField<FieldType>::Field0Type&
Foam::OldTimeField<FieldType>::oldTimeRef()
{
static_cast<const OldTimeField<FieldType>&>(*this).oldTime();
// Note: Not tfield0_.ref(), because this might be a base field storing a
// reference only. It's valid to un-const this reference because the
// derived field is guaranteed to store the non-const pointer.
return const_cast<Field0Type&>(tfield0_());
}
template<class FieldType>
const typename Foam::OldTimeField<FieldType>::Field0Type&
Foam::OldTimeField<FieldType>::oldTime
(
const label n
) const
{
return n == 0 ? field() : oldTime().oldTime(n - 1);
}
template<class FieldType>
typename Foam::OldTimeField<FieldType>::Field0Type&
Foam::OldTimeField<FieldType>::oldTimeRef
(
const label n
)
{
return n == 0 ? fieldRef() : oldTimeRef().oldTimeRef(n - 1);
}
// ************************************************************************* //
|