/*---------------------------------------------------------------------------*\ ========= | \\ / 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 . \*---------------------------------------------------------------------------*/ #include "OldTimeField.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // template const FieldType& Foam::OldTimeField::field() const { return static_cast(*this); } template FieldType& Foam::OldTimeField::fieldRef() { return static_cast(*this); } template void Foam::OldTimeField::storeOldTimesInner(const nil&) const {} template template void Foam::OldTimeField::storeOldTimesInner ( const OldTimeBaseField& otbf ) const { otbf.storeOldTimesInner(); } template void Foam::OldTimeField::storeOldTimesInner() const { if (tfield0_.valid()) { if (notNull(tfield0_()) && tfield0_.isTmp()) { // Propagate to store the old-old field tfield0_.ref().OldTimeField::storeOldTimesInner(); // Set the old-field to this field tfield0_.ref() = field(); tfield0_.ref().OldTimeField::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::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()(*this)); } } template void Foam::OldTimeField::nullOldestTimeInner() { if (tfield0_.valid() && notNull(tfield0_())) { if (tfield0_().OldTimeField::tfield0_.valid()) { if (!tfield0_.isTmp()) { FatalErrorInFunction << field().typeName << "::nullOldestTime called for field " << field().name() << " rather than " << field().type() << "::nullOldestTime" << exit(FatalError); } tfield0_.ref().OldTimeField::nullOldestTimeInner(); } else { tfield0_ = tmp(NullObjectRef()); } } } template template void Foam::OldTimeField::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(); } else if (tfield0_.valid()) { otbf.tfield0_ = tmp ( isNull(tfield0_()) ? NullObjectRef() : static_cast(tfield0_()) ); } otbf.timeIndex_ = timeIndex_; otbf.setBase(clear); } template void Foam::OldTimeField::setBase(const nil&, const bool clear) const {} template void Foam::OldTimeField::setBase(const bool clear) const { setBase(OldTimeBaseFieldType()(*this), clear); } // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // template bool Foam::OldTimeField::readOldTimeIfPresent() { typeIOobject 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::timeIndex_ = timeIndex_ - 1; tfield0_.ref().OldTimeField::setBase(); if (!tfield0_.ref().OldTimeField::readOldTimeIfPresent()) { tfield0_.ref().OldTimeField::oldTime(); } return true; } else { return false; } } template template class OtherPrimitiveField> void Foam::OldTimeField::copyOldTimes ( const IOobject& io, const OtherOldTime& otf ) { copyOldTimes(io.name(), otf); } template template class OtherPrimitiveField> void Foam::OldTimeField::copyOldTimes ( const word& newName, const OtherOldTime& otf ) { if (otf.tfield0_.valid() && otf.tfield0_.isTmp() && notNull(otf.tfield0_())) { tfield0_ = new Field0Type(newName + "_0", otf.tfield0_()); setBase(); } } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template Foam::OldTimeField::OldTimeField(const label timeIndex) : timeIndex_(timeIndex), tfield0_(nullptr) {} template Foam::OldTimeField::OldTimeField(const OldTimeField& otf) : timeIndex_(otf.timeIndex_), tfield0_(nullptr) { if (otf.tfield0_.valid() && otf.tfield0_.isTmp() && notNull(otf.tfield0_())) { tfield0_ = new Field0Type(otf.tfield0_()); setBase(); } } template Foam::OldTimeField::OldTimeField(OldTimeField&& otf) : timeIndex_(otf.timeIndex_), tfield0_(nullptr) { if (otf.tfield0_.valid() && otf.tfield0_.isTmp() && notNull(otf.tfield0_())) { tfield0_ = tmp(move(otf.tfield0_)); setBase(); } } // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * * // template Foam::OldTimeField::~OldTimeField() { if (tfield0_.valid() && notNull(tfield0_())) { tfield0_ = tmp(); setBase(); } } // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // template bool Foam::OldTimeField::isOldTime() const { return field().name().size() > 2 && field().name()(field().name().size() - 2, 2) == "_0"; } template bool Foam::OldTimeField::hasStoredOldTimes() const { return timeIndex_ == field().time().timeIndex(); } template void Foam::OldTimeField::resetOldTime() const { // Clear the field0Ptr to ensure the old-time field constructor // does not construct the old-old-time field tfield0_ = tmp(); setBase(); // Construct a copy of the field tfield0_ = OldTimeFieldCopy() ( 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 void Foam::OldTimeField::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 void Foam::OldTimeField::clearOldTimes() { if (tfield0_.valid()) { tfield0_ = tmp(); setBase(); } } template void Foam::OldTimeField::nullOldestTime() { if (!isOldTime()) { nullOldestTimeInner(); setBase(); } } template Foam::label Foam::OldTimeField::nOldTimes ( const bool includeNull ) const { if (tfield0_.valid()) { if (isNull(tfield0_())) { return includeNull; } else { return tfield0_().nOldTimes(includeNull) + 1; } } else { return 0; } } template const typename Foam::OldTimeField::Field0Type& Foam::OldTimeField::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 typename Foam::OldTimeField::Field0Type& Foam::OldTimeField::oldTimeRef() { static_cast&>(*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(tfield0_()); } template const typename Foam::OldTimeField::Field0Type& Foam::OldTimeField::oldTime ( const label n ) const { return n == 0 ? field() : oldTime().oldTime(n - 1); } template typename Foam::OldTimeField::Field0Type& Foam::OldTimeField::oldTimeRef ( const label n ) { return n == 0 ? fieldRef() : oldTimeRef().oldTimeRef(n - 1); } // ************************************************************************* //