Skip to content
Snippets Groups Projects
Time.C 33 KiB
Newer Older
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
    \\  /    A nd           | www.openfoam.com
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
    Copyright (C) 2011-2017 OpenFOAM Foundation
    Copyright (C) 2015-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
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 "Time.H"
#include "PstreamReduceOps.H"
#include "argList.H"
#include "HashSet.H"
#include "profiling.H"
#include <sstream>

// * * * * * * * * * * * * * Static Member Data  * * * * * * * * * * * * * * //

    defineTypeNameAndDebug(Time, 0);
const Foam::Enum
<
    Foam::Time::stopAtControls
>
Mark OLESEN's avatar
Mark OLESEN committed
Foam::Time::stopAtControlNames
    { stopAtControls::saEndTime, "endTime" },
    { stopAtControls::saNoWriteNow, "noWriteNow" },
    { stopAtControls::saWriteNow, "writeNow" },
    { stopAtControls::saNextWrite, "nextWrite" },
    // Leave saUnknown untabulated - fallback to flag unknown settings
Mark OLESEN's avatar
Mark OLESEN committed
});


const Foam::Enum
<
    Foam::Time::writeControls
>
Mark OLESEN's avatar
Mark OLESEN committed
Foam::Time::writeControlNames
    { writeControls::wcNone, "none" },
    { writeControls::wcTimeStep, "timeStep" },
    { writeControls::wcRunTime, "runTime" },
    { writeControls::wcAdjustableRunTime, "adjustable" },
    { writeControls::wcAdjustableRunTime, "adjustableRunTime" },
    { writeControls::wcClockTime, "clockTime" },
    { writeControls::wcCpuTime, "cpuTime" },
    // Leave wcUnknown untabulated - fallback to flag unknown settings
Mark OLESEN's avatar
Mark OLESEN committed
});


Foam::Time::fmtflags Foam::Time::format_(Foam::Time::general);
int Foam::Time::precision_(6);

const int Foam::Time::maxPrecision_(3 - log10(SMALL));

Foam::word Foam::Time::controlDictName("controlDict");

int Foam::Time::printExecutionFormat_
(
    Foam::debug::infoSwitch("printExecutionFormat", 0)
);

registerInfoSwitch
(
    "printExecutionFormat",
    int,
    Foam::Time::printExecutionFormat_
);

// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //

void Foam::Time::adjustDeltaT()
{
    bool adjustTime = false;
    scalar timeToNextWrite = VGREAT;

    if (writeControl_ == wcAdjustableRunTime)
    {
        adjustTime = true;
        timeToNextWrite = max
        (
            0.0,
            (writeTimeIndex_ + 1)*writeInterval_ - (value() - startTime_)
        scalar nSteps = timeToNextWrite/deltaT_;
        // For tiny deltaT the label can overflow!
        if (nSteps < scalar(labelMax))
            // nSteps can be < 1 so make sure at least 1
            label nStepsToNextWrite = max(1, round(nSteps));

            scalar newDeltaT = timeToNextWrite/nStepsToNextWrite;

            // Control the increase of the time step to within a factor of 2
            // and the decrease within a factor of 5.
            if (newDeltaT >= deltaT_)
            {
                deltaT_ = min(newDeltaT, 2.0*deltaT_);
            }
            else
            {
                deltaT_ = max(newDeltaT, 0.2*deltaT_);
            }
}


void Foam::Time::setControls()
{
    // default is to resume calculation from "latestTime"
    const word startFrom = controlDict_.getOrDefault<word>
Mark Olesen's avatar
Mark Olesen committed
    (
        "startFrom",
        "latestTime"
    );

    if (startFrom == "startTime")
    {
        controlDict_.readEntry("startTime", startTime_);
    }
    else
    {
        // Search directory for valid time directories
        instantList timeDirs = findTimes(path(), constant());
        const label nTimes = timeDirs.size();

        if (startFrom == "firstTime")
        {
            if (nTimes > 1 && timeDirs.first().name() == constant())
            {
                startTime_ = timeDirs[1].value();
            }
            else if (nTimes)
                startTime_ = timeDirs.first().value();
            }
        }
        else if (startFrom == "latestTime")
        {
                startTime_ = timeDirs.last().value();
            FatalIOErrorInFunction(controlDict_)
mattijs's avatar
mattijs committed
                << "expected startTime, firstTime or latestTime"
                << " found '" << startFrom << "'"
                << exit(FatalIOError);
        }
    }

    setTime(startTime_, 0);

    readDict();
    deltaTSave_ = deltaT_;
    // Check if time directory exists
    // If not increase time precision to see if it is formatted differently.
    // Note: do not use raw fileHandler().exists(...) since does not check
    //       alternative processorsDDD directories naming
    if (fileHandler().filePath(timePath()).empty())
Loading
Loading full blame...