Time.H 20.7 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6 7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
    Copyright (C) 2011-2017 OpenFOAM Foundation
9
    Copyright (C) 2016-2020 OpenCFD Ltd.
10 11 12 13
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

14 15 16 17
    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.
18 19 20 21 22 23 24

    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
25
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
26 27 28 29 30

Class
    Foam::Time

Description
31
    Class to control time during OpenFOAM simulations that is also the
32 33 34 35 36 37 38 39 40 41 42 43 44 45
    top-level objectRegistry.

SourceFiles
    Time.C
    TimeIO.C
    findInstance.C

\*---------------------------------------------------------------------------*/

#ifndef Time_H
#define Time_H

#include "TimePaths.H"
#include "objectRegistry.H"
46
#include "unwatchedIOdictionary.H"
47 48 49 50 51 52
#include "FIFOStack.H"
#include "clock.H"
#include "cpuTime.H"
#include "TimeState.H"
#include "Switch.H"
#include "instantList.H"
53
#include "Enum.H"
54 55 56
#include "typeInfo.H"
#include "dlLibraryTable.H"
#include "functionObjectList.H"
57 58
#include "sigWriteNow.H"
#include "sigStopAtWriteNow.H"
59 60 61 62 63

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{
Henry Weller's avatar
Henry Weller committed
64

65
// Forward Declarations
66
class argList;
67
class profilingTrigger;
68
class OSstream;
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

/*---------------------------------------------------------------------------*\
                             Class Time Declaration
\*---------------------------------------------------------------------------*/

class Time
:
    public clock,
    public cpuTime,
    public TimePaths,
    public objectRegistry,
    public TimeState
{
public:

        //- Write control options
        enum writeControls
        {
87
            wcNone,                 //!< "none"
Mark Olesen's avatar
Mark Olesen committed
88 89
            wcTimeStep,             //!< "timeStep"
            wcRunTime,              //!< "runTime"
90
            wcAdjustableRunTime,    //!< "adjustable" / "adjustableRunTime"
Mark Olesen's avatar
Mark Olesen committed
91
            wcClockTime,            //!< "clockTime"
92 93
            wcCpuTime,              //!< "cpuTime"
            wcUnknown               //!< Dummy no-op
94 95
        };

Mark Olesen's avatar
Mark Olesen committed
96 97
        //- Stop-run control options, which are primarily used when
        //- altering the stopAt condition.
98 99
        enum stopAtControls
        {
Mark Olesen's avatar
Mark Olesen committed
100 101 102 103 104
            saEndTime,    //!< Stop when Time reaches prescribed endTime
            saNoWriteNow, //!< Adjust endTime to stop immediately w/o writing
            saWriteNow,   //!< adjust endTime to stop immediately w/ writing
            saNextWrite,  //!< stop at the next data write interval
            saUnknown     //!< Dummy no-op. Do not change current value.
105 106
        };

107
        //- Supported time directory name formats
108 109
        enum fmtflags
        {
Mark Olesen's avatar
Mark Olesen committed
110 111 112
            general    = 0,                   //!< default float notation
            fixed      = ios_base::fixed,     //!< fixed-point notation
            scientific = ios_base::scientific //!< scientific notation
113 114 115
        };


Mark Olesen's avatar
Mark Olesen committed
116 117 118 119 120 121 122
        //- Names for writeControls
        static const Enum<writeControls> writeControlNames;

        //- Names for stopAtControls
        static const Enum<stopAtControls> stopAtControlNames;


123 124 125 126 127 128 129 130
        //- Style for "ExecutionTime = " output
        //  0 = seconds (with trailing 's')
        //  1 = day-hh:mm:ss
        //
        // \note this is public so registered info switches can modify it.
        static int printExecutionFormat_;


Mark Olesen's avatar
Mark Olesen committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
private:

    // Private data

        //- Profiling trigger for time-loop (for run, loop)
        mutable profilingTrigger* loopProfiling_;

        //- Any loaded dynamic libraries. Make sure to construct before
        //  reading controlDict.
        dlLibraryTable libs_;

        //- The controlDict
        unwatchedIOdictionary controlDict_;


146 147 148 149
protected:

    // Protected data

150
        label startTimeIndex_;
151

152
        scalar startTime_;
153

154
        mutable scalar endTime_;
155

156
        mutable stopAtControls stopAt_;
157 158 159 160 161

        writeControls writeControl_;

        scalar writeInterval_;

162
        label purgeWrite_;
Mark Olesen's avatar
Mark Olesen committed
163

164
        mutable FIFOStack<word> previousWriteTimes_;
mattijs's avatar
mattijs committed
165

Mark Olesen's avatar
Mark Olesen committed
166 167 168 169
        //- The total number of sub-cycles, the current sub-cycle index,
        //- or 0 if time is not being sub-cycled
        label subCycling_;

170 171 172
        // One-shot writing
        bool writeOnce_;

173 174 175
        //- If time is being sub-cycled this is the previous TimeState
        autoPtr<TimeState> prevTimeState_;

Mark Olesen's avatar
Mark Olesen committed
176 177
        //- Signal handler for one-shot writing upon signal
        sigWriteNow sigWriteNow_;
178

Mark Olesen's avatar
Mark Olesen committed
179 180
        //- Signal handler for write and clean exit upon signal
        sigStopAtWriteNow sigStopAtWriteNow_;
181 182


183 184 185 186 187 188
        //- Time directory name format
        static fmtflags format_;

        //- Time directory name precision
        static int precision_;

189 190 191
        //- Maximum time directory name precision
        static const int maxPrecision_;

Mark Olesen's avatar
Mark Olesen committed
192

193 194 195 196 197 198
        //- Adjust the time step so that writing occurs at the specified time
        void adjustDeltaT();

        //- Set the controls from the current controlDict
        void setControls();

199
        //- Set file monitoring, profiling, etc
200 201
        //  Optionally force profiling without inspecting the controlDict
        void setMonitoring(const bool forceProfiling=false);
202

203 204 205 206 207 208
        //- Read the control dictionary and set the write controls etc.
        virtual void readDict();


private:

209
        //- The write stream option (format, compression, version)
210
        IOstreamOption writeStreamOption_;
211 212 213 214

        //- Default graph format
        word graphFormat_;

215
        //- Is runtime modification of dictionaries allowed?
216 217 218
        Switch runTimeModifiable_;

        //- Function objects executed at start and on ++, +=
219
        mutable functionObjectList functionObjects_;
220 221 222 223


public:

224 225 226 227
    // Static Data Members

        //- The default control dictionary name (normally "controlDict")
        static word controlDictName;
228

229 230 231

    //- Runtime type information
    TypeName("time");
232 233 234 235


    // Constructors

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
        //- Construct from dictionary name to read and argument list.
        //  Uses "system" and "constant" for its directories.
        inline Time
        (
            const word& ctrlDictName,
            const argList& args,
            const bool enableFunctionObjects = true,
            const bool enableLibs = true
        );

        //- Construct given name of dictionary to read and root/case paths.
        //  Uses "system" and "constant" for its directories.
        inline Time
        (
            const word& ctrlDictName,
            const fileName& rootPath,
            const fileName& caseName,
            const bool enableFunctionObjects = true,
            const bool enableLibs = true
        );

        //- Construct given dictionary and root/case paths.
        //  Uses "system" and "constant" for its directories.
        inline Time
        (
            const dictionary& dict,
            const fileName& rootPath,
            const fileName& caseName,
            const bool enableFunctionObjects = true,
            const bool enableLibs = true
        );

        //- Construct at endTime and root/case paths.
        //  Uses "system" and "constant" for its directories.
        inline Time
        (
            const fileName& rootPath,
            const fileName& caseName,
            const bool enableFunctionObjects = true,
            const bool enableLibs = true
        );

        //- Construct from dictionary name to read and argument list.
        //  Use specified system/constant directory names.
280 281
        Time
        (
282
            const word& ctrlDictName,
283
            const argList& args,
284 285 286 287
            const word& systemName,     //!< "system" directory name
            const word& constantName,   //!< "constant" directory name
            const bool enableFunctionObjects = true,
            const bool enableLibs = true
288 289
        );

290 291
        //- Construct given name of dictionary to read and root/case paths.
        //  Use specified system/constant directory names.
292 293
        Time
        (
294
            const word& ctrlDictName,
295 296
            const fileName& rootPath,
            const fileName& caseName,
297 298
            const word& systemName,     //!< "system" directory name
            const word& constantName,   //!< "constant" directory name
299 300
            const bool enableFunctionObjects = true,
            const bool enableLibs = true
301 302
        );

303 304
        //- Construct given dictionary and root/case paths.
        //  Use specified system/constant directory names.
305 306 307 308 309
        Time
        (
            const dictionary& dict,
            const fileName& rootPath,
            const fileName& caseName,
310 311
            const word& systemName,     //!< "system" directory name
            const word& constantName,   //!< "constant" directory name
312 313
            const bool enableFunctionObjects = true,
            const bool enableLibs = true
314 315
        );

316 317
        //- Construct given endTime and root/case paths.
        //  Use specified system/constant directory names.
318 319 320 321
        Time
        (
            const fileName& rootPath,
            const fileName& caseName,
322 323
            const word& systemName,     //!< "system" directory name
            const word& constantName,   //!< "constant" directory name
324 325
            const bool enableFunctionObjects = true,
            const bool enableLibs = true
326 327 328
        );


329 330
    // Selectors

331
        //- Construct (dummy) Time - no functionObjects or libraries
332 333
        static autoPtr<Time> New();

334
        //- Construct (dummy) Time - no functionObjects or libraries
335 336
        static autoPtr<Time> New(const fileName& caseDir);

337 338 339
        //- Construct (dummy) Time - no functionObjects or libraries
        static autoPtr<Time> New(const argList& args);

340

341 342
    //- Destructor
    virtual ~Time();
343 344


345
    // Member Functions
346 347 348

        // Database functions

349 350 351
            //- Return name from objectRegistry and not TimePaths
            using objectRegistry::name;

352
            //- Return root path
353
            using TimePaths::rootPath;
354 355

            //- Return case name
356
            using TimePaths::caseName;
357 358 359 360 361 362 363

            //- Return path
            fileName path() const
            {
                return rootPath()/caseName();
            }

364
            //- Return read access to the controlDict dictionary
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
            const dictionary& controlDict() const
            {
                return controlDict_;
            }

            virtual const fileName& dbDir() const
            {
                return fileName::null;
            }

            //- Return current time path
            fileName timePath() const
            {
                return path()/timeName();
            }

381 382
            //- The write stream option (format, compression, version)
            IOstreamOption writeStreamOption() const
383
            {
384
                return writeStreamOption_;
385 386
            }

387 388
            //- The write stream format
            IOstream::streamFormat writeFormat() const
389
            {
390
                return writeStreamOption_.format();
391 392
            }

393
            //- The write stream compression
394 395
            IOstream::compressionType writeCompression() const
            {
396
                return writeStreamOption_.compression();
397 398
            }

399 400 401 402 403 404
            //- The write stream version
            IOstream::versionNumber writeVersion() const
            {
                return writeStreamOption_.version();
            }

405 406 407 408 409 410
            //- Default graph format
            const word& graphFormat() const
            {
                return graphFormat_;
            }

411 412 413 414 415 416
            //- Supports re-reading
            const Switch& runTimeModifiable() const
            {
                return runTimeModifiable_;
            }

417 418 419
            //- Read control dictionary, update controls and time
            virtual bool read();

420 421
            //- Read the objects that have been modified
            void readModifiedObjects();
422 423

            //- Return the location of "dir" containing the file "name".
424
            //- (eg, used in reading mesh data)
mattijs's avatar
mattijs committed
425 426
            //  If name is null, search for the directory "dir" only.
            //  Does not search beyond stopInstance (if set) or constant.
427 428 429
            word findInstance
            (
                const fileName& dir,
mattijs's avatar
mattijs committed
430
                const word& name = word::null,
mattijs's avatar
mattijs committed
431 432
                const IOobject::readOption rOpt = IOobject::MUST_READ,
                const word& stopInstance = word::null
433 434
            ) const;

435
            //- Search the case for the time directory path
436
            //- corresponding to the given instance
437 438 439 440 441
            word findInstancePath
            (
                const fileName& directory,
                const instant& t
            ) const;
442

443
            //- Search the case for the time directory path
444
            //- corresponding to the given instance
445
            word findInstancePath(const instant& t) const;
446

447
            //- Write time dictionary to the \<time\>/uniform directory
448 449
            virtual bool writeTimeDict() const;

450
            //- Write using stream options
451 452
            virtual bool writeObject
            (
453
                IOstreamOption streamOpt,
454
                const bool valid
455 456
            ) const;

457 458
            //- Write the objects immediately (not at end of iteration)
            //- and continue the run
459 460
            bool writeNow();

461
            //- Write the objects now (not at end of iteration) and end the run
462 463
            bool writeAndEnd();

464 465 466
            //- Write the objects once (one shot) and continue the run
            void writeOnce();

467 468 469
            //- Print the elapsed ExecutionTime (cpu-time), ClockTime
            Ostream& printExecutionTime(OSstream& os) const;

470 471 472 473

        // Access

            //- Return time name of given scalar time
474
            //- formatted with the given precision
475 476
            static word timeName
            (
477
                const scalar t,
478 479
                const int precision = precision_
            );
480 481 482 483 484 485 486 487 488 489 490 491 492

            //- Return current time name
            virtual word timeName() const;

            //- Return start time index
            virtual label startTimeIndex() const;

            //- Return start time
            virtual dimensionedScalar startTime() const;

            //- Return end time
            virtual dimensionedScalar endTime() const;

Mark Olesen's avatar
Mark Olesen committed
493 494 495
            //- Return the stop control information
            virtual stopAtControls stopAt() const;

496 497 498
            //- Return true if adjustTimeStep is true
            virtual bool isAdjustTimeStep() const;

499 500 501 502 503 504
            //- Return the list of function objects
            const functionObjectList& functionObjects() const
            {
                return functionObjects_;
            }

mattijs's avatar
mattijs committed
505 506 507 508 509 510
            //- External access to the loaded libraries
            const dlLibraryTable& libs() const
            {
                return libs_;
            }

511 512 513 514 515 516
            //- External access to the loaded libraries
            dlLibraryTable& libs()
            {
                return libs_;
            }

Mark Olesen's avatar
Mark Olesen committed
517 518 519 520 521 522
            //- Zero (tests as false) if time is not being sub-cycled,
            //- otherwise the current sub-cycle index or the total number of
            //- sub-cycles.
            //  The interpretation of non-zero values is dependent on the
            //  routine.
            label subCycling() const
523 524 525 526 527 528 529
            {
                return subCycling_;
            }

            //- Return previous TimeState if time is being sub-cycled
            const TimeState& prevTimeState() const
            {
530
                return *prevTimeState_;
531 532
            }

533 534 535

        // Check

536 537 538
            //- Return true if run should continue,
            //  also invokes the functionObjectList::end() method
            //  when the time goes out of range
539
            //  \note
henry's avatar
henry committed
540
            //  For correct behaviour, the following style of time-loop
541
            //  is recommended:
542
            //  \code
543 544
            //      while (runTime.run())
            //      {
545
            //          ++runTime;
546 547 548
            //          solve;
            //          runTime.write();
            //      }
549
            //  \endcode
550 551
            virtual bool run() const;

henry's avatar
henry committed
552 553 554
            //- Return true if run should continue and if so increment time
            //  also invokes the functionObjectList::end() method
            //  when the time goes out of range
555
            //  \note
henry's avatar
henry committed
556 557
            //  For correct behaviour, the following style of time-loop
            //  is recommended:
558
            //  \code
henry's avatar
henry committed
559 560 561 562 563
            //      while (runTime.loop())
            //      {
            //          solve;
            //          runTime.write();
            //      }
564
            //  \endcode
henry's avatar
henry committed
565 566
            virtual bool loop();

567 568
            //- Return true if end of run,
            //  does not invoke any functionObject methods
569
            //  \note
570
            //      The rounding heuristics near endTime mean that
571
            //      \code run() \endcode and \code !end() \endcode may
572
            //      not yield the same result
573 574 575 576 577
            virtual bool end() const;


        // Edit

Mark Olesen's avatar
Mark Olesen committed
578 579 580 581 582 583 584
            //- Adjust the current stopAtControl.
            //  \param stopCtrl the new stop control, whereby
            //      stopAtControls::saUnknown is treated as a no-op.
            //  \note this value only persists until the next time the
            //      dictionary is read.
            //  \return true if the stopAt() value was changed.
            virtual bool stopAt(const stopAtControls stopCtrl) const;
585

586
            //- Reset the time and time-index to those of the given time
587
            virtual void setTime(const Time& t);
588 589

            //- Reset the time and time-index
590
            virtual void setTime(const instant& inst, const label newIndex);
591 592 593 594

            //- Reset the time and time-index
            virtual void setTime
            (
595
                const dimensionedScalar& newTime,
596 597 598 599
                const label newIndex
            );

            //- Reset the time and time-index
600
            virtual void setTime(const scalar newTime, const label newIndex);
601 602

            //- Reset end time
603
            virtual void setEndTime(const dimensionedScalar& endTime);
604 605

            //- Reset end time
606
            virtual void setEndTime(const scalar endTime);
607

608
            //- Reset time step, normally also calling adjustDeltaT()
609 610
            virtual void setDeltaT
            (
611 612
                const dimensionedScalar& deltaT,
                const bool adjust = true
613
            );
614

615
            //- Reset time step, normally also calling adjustDeltaT()
616 617
            virtual void setDeltaT
            (
618 619
                const scalar deltaT,
                const bool adjust = true
620
            );
621 622 623 624

            //- Set time to sub-cycle for the given number of steps
            virtual TimeState subCycle(const label nSubCycles);

Mark Olesen's avatar
Mark Olesen committed
625 626 627 628 629 630 631
            //- Adjust the reported sub-cycle index.
            //  \param index is the sub-cycle index.
            //      This index is ignored sub-cycling was
            //      not already registered, or if the index is zero or
            //      negative.
            virtual void subCycleIndex(const label index);

632 633
            //- Reset time after sub-cycling back to previous TimeState
            virtual void endSubCycle();
634 635 636 637 638 639 640 641 642 643

            //- Return non-const access to the list of function objects
            functionObjectList& functionObjects()
            {
                return functionObjects_;
            }


    // Member operators

644
        //- Set deltaT to that specified and increment time via operator++()
645
        virtual Time& operator+=(const dimensionedScalar& deltaT);
646

647
        //- Set deltaT to that specified and increment time via operator++()
648
        virtual Time& operator+=(const scalar deltaT);
649

650 651 652
        //- Prefix increment,
        //  also invokes the functionObjectList::start() or
        //  functionObjectList::execute() method, depending on the time-index
653 654
        virtual Time& operator++();

655
        //- Postfix increment, this is identical to the prefix increment
656 657 658 659 660 661 662 663 664 665
        virtual Time& operator++(int);
};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

666 667 668 669
#include "TimeI.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

670 671 672
#endif

// ************************************************************************* //