OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

Time.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "Time.H"
00028 #include "PstreamReduceOps.H"
00029 
00030 #include <sstream>
00031 
00032 // * * * * * * * * * * * * * Static Member Data  * * * * * * * * * * * * * * //
00033 
00034 defineTypeNameAndDebug(Foam::Time, 0);
00035 
00036 template<>
00037 const char* Foam::NamedEnum<Foam::Time::stopAtControls, 4>::names[] =
00038 {
00039     "endTime",
00040     "noWriteNow",
00041     "writeNow",
00042     "nextWrite"
00043 };
00044 
00045 const Foam::NamedEnum<Foam::Time::stopAtControls, 4>
00046     Foam::Time::stopAtControlNames_;
00047 
00048 template<>
00049 const char* Foam::NamedEnum<Foam::Time::writeControls, 5>::names[] =
00050 {
00051     "timeStep",
00052     "runTime",
00053     "adjustableRunTime",
00054     "clockTime",
00055     "cpuTime"
00056 };
00057 
00058 const Foam::NamedEnum<Foam::Time::writeControls, 5>
00059     Foam::Time::writeControlNames_;
00060 
00061 Foam::Time::fmtflags Foam::Time::format_(Foam::Time::general);
00062 int Foam::Time::precision_(6);
00063 
00064 Foam::word Foam::Time::controlDictName("controlDict");
00065 
00066 
00067 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00068 
00069 void Foam::Time::adjustDeltaT()
00070 {
00071     if (writeControl_ == wcAdjustableRunTime)
00072     {
00073         scalar timeToNextWrite = max
00074         (
00075             0.0,
00076             (outputTimeIndex_ + 1)*writeInterval_ - (value() - startTime_)
00077         );
00078 
00079         label nStepsToNextWrite = label(timeToNextWrite/deltaT_ - SMALL) + 1;
00080         scalar newDeltaT = timeToNextWrite/nStepsToNextWrite;
00081 
00082         // Control the increase of the time step to within a factor of 2
00083         // and the decrease within a factor of 5.
00084         if (newDeltaT >= deltaT_)
00085         {
00086             deltaT_ = min(newDeltaT, 2.0*deltaT_);
00087         }
00088         else
00089         {
00090             deltaT_ = max(newDeltaT, 0.2*deltaT_);
00091         }
00092     }
00093 }
00094 
00095 
00096 void Foam::Time::setControls()
00097 {
00098     // default is to resume calculation from "latestTime"
00099     word startFrom = controlDict_.lookupOrDefault<word>
00100     (
00101         "startFrom",
00102         "latestTime"
00103     );
00104 
00105     if (startFrom == "startTime")
00106     {
00107         controlDict_.lookup("startTime") >> startTime_;
00108     }
00109     else
00110     {
00111         // Search directory for valid time directories
00112         instantList timeDirs = findTimes(path());
00113 
00114         if (startFrom == "firstTime")
00115         {
00116             if (timeDirs.size())
00117             {
00118                 startTime_ = timeDirs[0].value();
00119             }
00120         }
00121         else if (startFrom == "latestTime")
00122         {
00123             if (timeDirs.size())
00124             {
00125                 startTime_ = timeDirs[timeDirs.size()-1].value();
00126             }
00127         }
00128         else
00129         {
00130             WarningIn("Time::setControls()")
00131                 << "    expected startTime, firstTime or latestTime"
00132                 << " found '" << startFrom
00133                 << "' in dictionary " << controlDict_.name() << nl
00134                 << "    Setting time to " << startTime_ << endl;
00135         }
00136     }
00137 
00138     setTime(startTime_, 0);
00139 
00140     readDict();
00141     deltaTSave_ = deltaT_;
00142     deltaT0_ = deltaTSave_;
00143 
00144     if (Pstream::parRun())
00145     {
00146         scalar sumStartTime = startTime_;
00147         reduce(sumStartTime, sumOp<scalar>());
00148         if
00149         (
00150             mag(Pstream::nProcs()*startTime_ - sumStartTime)
00151           > Pstream::nProcs()*deltaT_/10.0
00152         )
00153         {
00154             FatalErrorIn("Time::setControls()")
00155                 << "Start time is not the same for all processors" << nl
00156                 << "processor " << Pstream::myProcNo() << " has startTime "
00157                 << startTime_ << exit(FatalError);
00158         }
00159     }
00160 
00161     IOdictionary timeDict
00162     (
00163         IOobject
00164         (
00165             "time",
00166             timeName(),
00167             "uniform",
00168             *this,
00169             IOobject::READ_IF_PRESENT,
00170             IOobject::NO_WRITE,
00171             false
00172         )
00173     );
00174 
00175     if (timeDict.readIfPresent("deltaT", deltaTSave_))
00176     {
00177         deltaT0_ = deltaTSave_;
00178     }
00179 
00180     if (timeDict.readIfPresent("index", startTimeIndex_))
00181     {
00182         timeIndex_ = startTimeIndex_;
00183     }
00184 }
00185 
00186 
00187 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00188 
00189 Foam::Time::Time
00190 (
00191     const word& controlDictName,
00192     const fileName& rootPath,
00193     const fileName& caseName,
00194     const word& systemName,
00195     const word& constantName
00196 )
00197 :
00198     TimePaths
00199     (
00200         rootPath,
00201         caseName,
00202         systemName,
00203         constantName
00204     ),
00205 
00206     objectRegistry(*this),
00207 
00208     controlDict_
00209     (
00210         IOobject
00211         (
00212             controlDictName,
00213             system(),
00214             *this,
00215             IOobject::MUST_READ,
00216             IOobject::NO_WRITE,
00217             false
00218         )
00219     ),
00220 
00221     startTimeIndex_(0),
00222     startTime_(0),
00223     endTime_(0),
00224 
00225     stopAt_(saEndTime),
00226     writeControl_(wcTimeStep),
00227     writeInterval_(GREAT),
00228     purgeWrite_(0),
00229     subCycling_(false),
00230 
00231     writeFormat_(IOstream::ASCII),
00232     writeVersion_(IOstream::currentVersion),
00233     writeCompression_(IOstream::UNCOMPRESSED),
00234     graphFormat_("raw"),
00235     runTimeModifiable_(true),
00236 
00237     readLibs_(controlDict_, "libs"),
00238     functionObjects_(*this)
00239 {
00240     setControls();
00241 }
00242 
00243 
00244 Foam::Time::Time
00245 (
00246     const dictionary& dict,
00247     const fileName& rootPath,
00248     const fileName& caseName,
00249     const word& systemName,
00250     const word& constantName
00251 )
00252 :
00253     TimePaths
00254     (
00255         rootPath,
00256         caseName,
00257         systemName,
00258         constantName
00259     ),
00260 
00261     objectRegistry(*this),
00262 
00263     controlDict_
00264     (
00265         IOobject
00266         (
00267             controlDictName,
00268             system(),
00269             *this,
00270             IOobject::NO_READ,
00271             IOobject::NO_WRITE,
00272             false
00273         ),
00274         dict
00275     ),
00276 
00277     startTimeIndex_(0),
00278     startTime_(0),
00279     endTime_(0),
00280 
00281     stopAt_(saEndTime),
00282     writeControl_(wcTimeStep),
00283     writeInterval_(GREAT),
00284     purgeWrite_(0),
00285     subCycling_(false),
00286 
00287     writeFormat_(IOstream::ASCII),
00288     writeVersion_(IOstream::currentVersion),
00289     writeCompression_(IOstream::UNCOMPRESSED),
00290     graphFormat_("raw"),
00291     runTimeModifiable_(true),
00292 
00293     readLibs_(controlDict_, "libs"),
00294     functionObjects_(*this)
00295 {
00296     setControls();
00297 }
00298 
00299 
00300 Foam::Time::Time
00301 (
00302     const fileName& rootPath,
00303     const fileName& caseName,
00304     const word& systemName,
00305     const word& constantName
00306 )
00307 :
00308     TimePaths
00309     (
00310         rootPath,
00311         caseName,
00312         systemName,
00313         constantName
00314     ),
00315 
00316     objectRegistry(*this),
00317 
00318     controlDict_
00319     (
00320         IOobject
00321         (
00322             controlDictName,
00323             system(),
00324             *this,
00325             IOobject::NO_READ,
00326             IOobject::NO_WRITE,
00327             false
00328         )
00329     ),
00330 
00331     startTimeIndex_(0),
00332     startTime_(0),
00333     endTime_(0),
00334 
00335     stopAt_(saEndTime),
00336     writeControl_(wcTimeStep),
00337     writeInterval_(GREAT),
00338     purgeWrite_(0),
00339     subCycling_(false),
00340 
00341     writeFormat_(IOstream::ASCII),
00342     writeVersion_(IOstream::currentVersion),
00343     writeCompression_(IOstream::UNCOMPRESSED),
00344     graphFormat_("raw"),
00345     runTimeModifiable_(true),
00346 
00347     readLibs_(controlDict_, "libs"),
00348     functionObjects_(*this)
00349 {}
00350 
00351 
00352 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00353 
00354 Foam::Time::~Time()
00355 {
00356     // destroy function objects first
00357     functionObjects_.clear();
00358 }
00359 
00360 
00361 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00362 
00363 Foam::word Foam::Time::timeName(const scalar t)
00364 {
00365     std::ostringstream buf;
00366     buf.setf(ios_base::fmtflags(format_), ios_base::floatfield);
00367     buf.precision(precision_);
00368     buf << t;
00369     return buf.str();
00370 }
00371 
00372 
00373 Foam::word Foam::Time::timeName() const
00374 {
00375     return dimensionedScalar::name();
00376 }
00377 
00378 
00379 // Search the construction path for times
00380 Foam::instantList Foam::Time::times() const
00381 {
00382     return findTimes(path());
00383 }
00384 
00385 
00386 Foam::word Foam::Time::findInstancePath(const instant& t) const
00387 {
00388     instantList timeDirs = findTimes(path());
00389 
00390     forAllReverse(timeDirs, timeI)
00391     {
00392         if (timeDirs[timeI] == t)
00393         {
00394             return timeDirs[timeI].name();
00395         }
00396     }
00397 
00398     return word::null;
00399 }
00400 
00401 
00402 Foam::instant Foam::Time::findClosestTime(const scalar t) const
00403 {
00404     instantList timeDirs = findTimes(path());
00405 
00406     // there is only one time (likely "constant") so return it
00407     if (timeDirs.size() == 1)
00408     {
00409         return timeDirs[0];
00410     }
00411 
00412     if (t < timeDirs[1].value())
00413     {
00414         return timeDirs[1];
00415     }
00416     else if (t > timeDirs[timeDirs.size()-1].value())
00417     {
00418         return timeDirs[timeDirs.size()-1];
00419     }
00420 
00421     label nearestIndex = -1;
00422     scalar deltaT = GREAT;
00423 
00424     for (label timeI=1; timeI < timeDirs.size(); ++timeI)
00425     {
00426         scalar diff = mag(timeDirs[timeI].value() - t);
00427         if (diff < deltaT)
00428         {
00429             deltaT = diff;
00430             nearestIndex = timeI;
00431         }
00432     }
00433 
00434     return timeDirs[nearestIndex];
00435 }
00436 
00437 
00438 // This should work too,
00439 // if we don't worry about checking "constant" explicitly
00440 //
00441 // Foam::instant Foam::Time::findClosestTime(const scalar t) const
00442 // {
00443 //     instantList timeDirs = findTimes(path());
00444 //     label timeIndex = min(findClosestTimeIndex(timeDirs, t), 0);
00445 //     return timeDirs[timeIndex];
00446 // }
00447 
00448 Foam::label Foam::Time::findClosestTimeIndex
00449 (
00450     const instantList& timeDirs,
00451     const scalar t
00452 )
00453 {
00454     label nearestIndex = -1;
00455     scalar deltaT = GREAT;
00456 
00457     forAll(timeDirs, timeI)
00458     {
00459         if (timeDirs[timeI].name() == "constant") continue;
00460 
00461         scalar diff = mag(timeDirs[timeI].value() - t);
00462         if (diff < deltaT)
00463         {
00464             deltaT = diff;
00465             nearestIndex = timeI;
00466         }
00467     }
00468 
00469     return nearestIndex;
00470 }
00471 
00472 
00473 Foam::label Foam::Time::startTimeIndex() const
00474 {
00475     return startTimeIndex_;
00476 }
00477 
00478 
00479 Foam::dimensionedScalar Foam::Time::startTime() const
00480 {
00481     return dimensionedScalar("startTime", dimTime, startTime_);
00482 }
00483 
00484 
00485 Foam::dimensionedScalar Foam::Time::endTime() const
00486 {
00487     return dimensionedScalar("endTime", dimTime, endTime_);
00488 }
00489 
00490 
00491 bool Foam::Time::run() const
00492 {
00493     bool running = value() < (endTime_ - 0.5*deltaT_);
00494 
00495     if (!subCycling_)
00496     {
00497         // only execute when the condition is no longer true
00498         // ie, when exiting the control loop
00499         if (!running && timeIndex_ != startTimeIndex_)
00500         {
00501             // Note, end() also calls an indirect start() as required
00502             functionObjects_.end();
00503         }
00504     }
00505 
00506     return running;
00507 }
00508 
00509 
00510 bool Foam::Time::loop()
00511 {
00512     bool running = run();
00513 
00514     if (running)
00515     {
00516         operator++();
00517     }
00518 
00519     return running;
00520 }
00521 
00522 
00523 bool Foam::Time::end() const
00524 {
00525     return value() > (endTime_ + 0.5*deltaT_);
00526 }
00527 
00528 
00529 void Foam::Time::setTime(const Time& t)
00530 {
00531     value() = t.value();
00532     dimensionedScalar::name() = t.dimensionedScalar::name();
00533     timeIndex_ = t.timeIndex_;
00534 }
00535 
00536 
00537 void Foam::Time::setTime(const instant& inst, const label newIndex)
00538 {
00539     value() = inst.value();
00540     dimensionedScalar::name() = inst.name();
00541     timeIndex_ = newIndex;
00542 
00543     IOdictionary timeDict
00544     (
00545         IOobject
00546         (
00547             "time",
00548             timeName(),
00549             "uniform",
00550             *this,
00551             IOobject::READ_IF_PRESENT,
00552             IOobject::NO_WRITE,
00553             false
00554         )
00555     );
00556 
00557     timeDict.readIfPresent("deltaT", deltaT_);
00558     timeDict.readIfPresent("deltaT0", deltaT0_);
00559     timeDict.readIfPresent("index", timeIndex_);
00560 }
00561 
00562 
00563 void Foam::Time::setTime(const dimensionedScalar& newTime, const label newIndex)
00564 {
00565     setTime(newTime.value(), newIndex);
00566 }
00567 
00568 
00569 void Foam::Time::setTime(const scalar newTime, const label newIndex)
00570 {
00571     value() = newTime;
00572     dimensionedScalar::name() = timeName(timeToUserTime(newTime));
00573     timeIndex_ = newIndex;
00574 }
00575 
00576 
00577 void Foam::Time::setEndTime(const dimensionedScalar& endTime)
00578 {
00579     setEndTime(endTime.value());
00580 }
00581 
00582 
00583 void Foam::Time::setEndTime(const scalar endTime)
00584 {
00585     endTime_ = endTime;
00586 }
00587 
00588 
00589 void Foam::Time::setDeltaT(const dimensionedScalar& deltaT)
00590 {
00591     setDeltaT(deltaT.value());
00592 }
00593 
00594 
00595 void Foam::Time::setDeltaT(const scalar deltaT)
00596 {
00597     deltaT_ = deltaT;
00598     deltaTchanged_ = true;
00599     adjustDeltaT();
00600 }
00601 
00602 
00603 Foam::TimeState Foam::Time::subCycle(const label nSubCycles)
00604 {
00605     subCycling_ = true;
00606 
00607     TimeState ts = *this;
00608     setTime(*this - deltaT(), (timeIndex() - 1)*nSubCycles);
00609     deltaT_ /= nSubCycles;
00610     deltaT0_ /= nSubCycles;
00611     deltaTSave_ = deltaT0_;
00612 
00613     return ts;
00614 }
00615 
00616 
00617 void Foam::Time::endSubCycle(const TimeState& ts)
00618 {
00619     subCycling_ = false;
00620     TimeState::operator=(ts);
00621 }
00622 
00623 
00624 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00625 
00626 Foam::Time& Foam::Time::operator+=(const dimensionedScalar& deltaT)
00627 {
00628     return operator+=(deltaT.value());
00629 }
00630 
00631 
00632 Foam::Time& Foam::Time::operator+=(const scalar deltaT)
00633 {
00634     setDeltaT(deltaT);
00635     return operator++();
00636 }
00637 
00638 
00639 Foam::Time& Foam::Time::operator++()
00640 {
00641     readModifiedObjects();
00642 
00643     if (!subCycling_)
00644     {
00645         if (timeIndex_ == startTimeIndex_)
00646         {
00647             functionObjects_.start();
00648         }
00649         else
00650         {
00651             functionObjects_.execute();
00652         }
00653     }
00654 
00655     deltaT0_ = deltaTSave_;
00656     deltaTSave_ = deltaT_;
00657     setTime(value() + deltaT_, timeIndex_ + 1);
00658 
00659     // If the time is very close to zero reset to zero
00660     if (mag(value()) < 10*SMALL*deltaT_)
00661     {
00662         setTime(0.0, timeIndex_);
00663     }
00664 
00665     switch (writeControl_)
00666     {
00667         case wcTimeStep:
00668             outputTime_ = !(timeIndex_ % label(writeInterval_));
00669         break;
00670 
00671         case wcRunTime:
00672         case wcAdjustableRunTime:
00673         {
00674             label outputIndex =
00675                 label(((value() - startTime_) + 0.5*deltaT_)/writeInterval_);
00676 
00677             if (outputIndex > outputTimeIndex_)
00678             {
00679                 outputTime_ = true;
00680                 outputTimeIndex_ = outputIndex;
00681             }
00682             else
00683             {
00684                 outputTime_ = false;
00685             }
00686         }
00687         break;
00688 
00689         case wcCpuTime:
00690         {
00691             label outputIndex = label(elapsedCpuTime()/writeInterval_);
00692             if (outputIndex > outputTimeIndex_)
00693             {
00694                 outputTime_ = true;
00695                 outputTimeIndex_ = outputIndex;
00696             }
00697             else
00698             {
00699                 outputTime_ = false;
00700             }
00701         }
00702         break;
00703 
00704         case wcClockTime:
00705         {
00706             label outputIndex = label(elapsedClockTime()/writeInterval_);
00707             if (outputIndex > outputTimeIndex_)
00708             {
00709                 outputTime_ = true;
00710                 outputTimeIndex_ = outputIndex;
00711             }
00712             else
00713             {
00714                 outputTime_ = false;
00715             }
00716         }
00717         break;
00718     }
00719 
00720     // see if endTime needs adjustment to stop at the next run()/end() check
00721     if (!end())
00722     {
00723         if (stopAt_ == saNoWriteNow)
00724         {
00725             endTime_ = value();
00726         }
00727         else if (stopAt_ == saWriteNow)
00728         {
00729             endTime_ = value();
00730             outputTime_ = true;
00731         }
00732         else if (stopAt_ == saNextWrite && outputTime_ == true)
00733         {
00734             endTime_ = value();
00735         }
00736     }
00737 
00738     return *this;
00739 }
00740 
00741 
00742 Foam::Time& Foam::Time::operator++(int)
00743 {
00744     return operator++();
00745 }
00746 
00747 
00748 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd