functionObject::timeControl: Added runTimes option to execute/write at specified list of run times

With
    executeControl  runTimes;
    executeTimes    (0.1 0.2 0.3);

the functionObject will be executed at 0.1s, 0.2s and 0.3s only.

With
    writeControl    runTimes;
    writeTimes      (0.1 0.2 0.3);

the functionObject will write at 0.1s, 0.2s and 0.3s only.
This commit is contained in:
Henry Weller
2023-06-14 17:31:52 +01:00
parent 3eed5c0c99
commit 09a2da22b2
11 changed files with 105 additions and 35 deletions

View File

@ -82,17 +82,17 @@ Foam::word Foam::Time::controlDictName("controlDict");
void Foam::Time::adjustDeltaT() void Foam::Time::adjustDeltaT()
{ {
const scalar timeToNextWrite = min const scalar timeToNextAction = min
( (
max max
( (
0, 0,
(writeTimeIndex_ + 1)*writeInterval_ - (value() - beginTime_) (writeTimeIndex_ + 1)*writeInterval_ - (value() - beginTime_)
), ),
functionObjects_.timeToNextWrite() functionObjects_.timeToNextAction()
); );
const scalar nSteps = timeToNextWrite/deltaT_; const scalar nSteps = timeToNextAction/deltaT_;
// Ensure nStepsToNextWrite does not overflow // Ensure nStepsToNextWrite does not overflow
if (nSteps < labelMax) if (nSteps < labelMax)
@ -101,7 +101,7 @@ void Foam::Time::adjustDeltaT()
// to accommodate the next write time before splitting // to accommodate the next write time before splitting
const label nStepsToNextWrite = label(max(nSteps, 1) + 0.99); const label nStepsToNextWrite = label(max(nSteps, 1) + 0.99);
deltaT_ = timeToNextWrite/nStepsToNextWrite; deltaT_ = timeToNextAction/nStepsToNextWrite;
} }
} }

View File

@ -157,7 +157,7 @@ bool Foam::functionObject::end()
} }
Foam::scalar Foam::functionObject::timeToNextWrite() Foam::scalar Foam::functionObject::timeToNextAction()
{ {
return vGreat; return vGreat;
} }

View File

@ -69,21 +69,24 @@ Description
endTime | End time | no | endTime | End time | no |
executeAtStart | Execute at start time switch | no | yes executeAtStart | Execute at start time switch | no | yes
executeControl | See time controls below | no | timeStep executeControl | See time controls below | no | timeStep
executeInterval | Steps between each execute phase | no | executeInterval | Steps between each execution | no |
executeTimes | List of execution times | no |
writeControl | See time controls below | no | timeStep writeControl | See time controls below | no | timeStep
writeInterval | Steps between each write phase | no | writeInterval | Steps between each write | no |
writeTimes | List of write times | no |
\endtable \endtable
Time controls: Time controls:
\table \table
Option | Description Option | Description
timeStep | Execute/write every 'Interval' time-steps timeStep | Execute/write every 'Interval' time-steps
writeTime | Execute/write every 'Interval' output times writeTime | Execute/write every 'Interval' write times
adjustableRunTime | Execute/write every 'Interval' run time period adjustableRunTime | Execute/write every 'Interval' run time period
runTime | Execute/write every 'Interval' run time period runTime | Execute/write every 'Interval' run time period
runTimes | Execute/write at specified list of run times
clockTime | Execute/write every 'Interval' clock time period clockTime | Execute/write every 'Interval' clock time period
cpuTime | Execute/write every 'Interval' CPU time period cpuTime | Execute/write every 'Interval' CPU time period
none | Execute/write every time-step none | No execution
\endtable \endtable
The sub-dictionary name \c \<functionObjectName\> is chosen by the user, and The sub-dictionary name \c \<functionObjectName\> is chosen by the user, and
@ -244,7 +247,7 @@ public:
//- Called by Time::adjustTimeStep(). Allows the functionObject to //- Called by Time::adjustTimeStep(). Allows the functionObject to
// insert a write time earlier than that already in use by the run // insert a write time earlier than that already in use by the run
// time. Returns the write time, or vGreat. // time. Returns the write time, or vGreat.
virtual scalar timeToNextWrite(); virtual scalar timeToNextAction();
//- Update topology using the given map //- Update topology using the given map
virtual void movePoints(const polyMesh& mesh); virtual void movePoints(const polyMesh& mesh);

View File

@ -278,7 +278,7 @@ bool Foam::functionObjectList::end()
} }
Foam::scalar Foam::functionObjectList::timeToNextWrite() Foam::scalar Foam::functionObjectList::timeToNextAction()
{ {
scalar result = vGreat; scalar result = vGreat;
@ -291,7 +291,7 @@ Foam::scalar Foam::functionObjectList::timeToNextWrite()
forAll(*this, oi) forAll(*this, oi)
{ {
result = min(result, operator[](oi).timeToNextWrite()); result = min(result, operator[](oi).timeToNextAction());
} }
} }

View File

@ -176,7 +176,7 @@ public:
bool end(); bool end();
//- Return the time to the next write //- Return the time to the next write
scalar timeToNextWrite(); scalar timeToNextAction();
//- Update topology using the given map //- Update topology using the given map
virtual void movePoints(const polyMesh& mesh); virtual void movePoints(const polyMesh& mesh);

View File

@ -31,7 +31,7 @@ License
namespace Foam namespace Foam
{ {
template<> template<>
const char* NamedEnum<timeControl::timeControls, 8>:: const char* NamedEnum<timeControl::timeControls, 9>::
names[] = names[] =
{ {
"timeStep", "timeStep",
@ -39,13 +39,14 @@ namespace Foam
"outputTime", "outputTime",
"adjustableRunTime", "adjustableRunTime",
"runTime", "runTime",
"runTimes",
"clockTime", "clockTime",
"cpuTime", "cpuTime",
"none" "none"
}; };
} }
const Foam::NamedEnum<Foam::timeControl::timeControls, 8> const Foam::NamedEnum<Foam::timeControl::timeControls, 9>
Foam::timeControl::timeControlNames_; Foam::timeControl::timeControlNames_;
@ -63,6 +64,7 @@ Foam::timeControl::timeControl
timeControl_(timeControls::timeStep), timeControl_(timeControls::timeStep),
intervalSteps_(0), intervalSteps_(0),
interval_(-1), interval_(-1),
timeDelta_(0),
executionIndex_(0) executionIndex_(0)
{ {
read(dict); read(dict);
@ -81,6 +83,7 @@ void Foam::timeControl::read(const dictionary& dict)
{ {
word controlName(prefix_ + "Control"); word controlName(prefix_ + "Control");
word intervalName(prefix_ + "Interval"); word intervalName(prefix_ + "Interval");
const word timesName(prefix_ + "Times");
// For backward compatibility support the deprecated 'outputControl' option // For backward compatibility support the deprecated 'outputControl' option
// now superseded by 'writeControl' for compatibility with Time // now superseded by 'writeControl' for compatibility with Time
@ -142,6 +145,23 @@ void Foam::timeControl::read(const dictionary& dict)
break; break;
} }
case timeControls::runTimes:
{
times_ = dict.lookup<scalarList>(timesName);
timeDelta_ = dict.lookupOrDefault("timeDelta", 1e-6);
forAll(times_, i)
{
timeIndices_.insert
(
int64_t((times_[i] + timeDelta_/2.0)/timeDelta_)
);
}
intervalSteps_ = dict.lookupOrDefault<label>(intervalName, 1);
break;
}
default: default:
{ {
break; break;
@ -195,6 +215,16 @@ bool Foam::timeControl::execute()
break; break;
} }
case timeControls::runTimes:
{
return timeIndices_.found
(
(time_.userTimeValue() + timeDelta_/2)/timeDelta_
);
break;
}
case timeControls::cpuTime: case timeControls::cpuTime:
{ {
const label executionIndex = label const label executionIndex = label
@ -244,7 +274,7 @@ bool Foam::timeControl::execute()
} }
Foam::scalar Foam::timeControl::timeToNextWrite() Foam::scalar Foam::timeControl::timeToNextAction()
{ {
switch (timeControl_) switch (timeControl_)
{ {
@ -272,6 +302,27 @@ Foam::scalar Foam::timeControl::timeToNextWrite()
break; break;
} }
case timeControls::runTimes:
{
if (time_.userTimeValue() + timeDelta_ < times_.last())
{
forAll(times_, i)
{
if (times_[i] > time_.userTimeValue() + timeDelta_)
{
return time_.userTimeToTime
(
times_[i] - time_.userTimeValue()
);
}
}
}
return vGreat;
break;
}
default: default:
{ {
FatalErrorInFunction FatalErrorInFunction

View File

@ -54,14 +54,15 @@ public:
//- The time control options //- The time control options
enum class timeControls enum class timeControls
{ {
timeStep, //!< execution is coupled to the time-step timeStep, //!< Execute/write every 'Interval' time-steps
writeTime, //!< execution is coupled to the write-time writeTime, //!< Execute/write every 'Interval' write times
outputTime, //!< execution is coupled to the output-time outputTime, //!< Execute/write every 'Interval' write times
adjustableRunTime, //!< Adjust time step for execution adjustableRunTime, //!< Execute/write every 'Interval' run time period
runTime, //!< run time for execution runTime, //!< Execute/write every 'Interval' run time period
clockTime, //!< clock time for execution runTimes, //!< Execute/write at specified list of run times
cpuTime, //!< cpu time for execution clockTime, //!< Execute/write every 'Interval' CPU time period
none //!< no execution cpuTime, //!< Execute/write every 'Interval' CPU time period
none //!< No execution
}; };
@ -76,7 +77,7 @@ private:
const word prefix_; const word prefix_;
//- String representation of timeControls enums //- String representation of timeControls enums
static const NamedEnum<timeControls, 8> timeControlNames_; static const NamedEnum<timeControls, 9> timeControlNames_;
//- Type of time control //- Type of time control
timeControls timeControl_; timeControls timeControl_;
@ -88,8 +89,17 @@ private:
//- Execution interval //- Execution interval
scalar interval_; scalar interval_;
//- List of times (for runTimes option)
scalarList times_;
//- Time delta used for time -> index (for runTimes option)
scalar timeDelta_;
//- Hash set of mesh mapping time indices (for runTimes option)
HashSet<int64_t, Hash<int64_t>> timeIndices_;
//- Index of previous execution //- Index of previous execution
label executionIndex_; mutable label executionIndex_;
public: public:
@ -124,7 +134,7 @@ public:
bool execute(); bool execute();
//- Return the time to the next write //- Return the time to the next write
scalar timeToNextWrite(); scalar timeToNextAction();
//- Return control //- Return control
inline timeControls control() const; inline timeControls control() const;

View File

@ -157,9 +157,13 @@ bool Foam::functionObjects::timeControl::end()
} }
Foam::scalar Foam::functionObjects::timeControl::timeToNextWrite() Foam::scalar Foam::functionObjects::timeControl::timeToNextAction()
{ {
return writeControl_.timeToNextWrite(); return min
(
executeControl_.timeToNextAction(),
writeControl_.timeToNextAction()
);
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2016-2022 OpenFOAM Foundation \\ / A nd | Copyright (C) 2016-2023 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -151,8 +151,9 @@ public:
//- Called when Time::run() determines that the time-loop exits //- Called when Time::run() determines that the time-loop exits
virtual bool end(); virtual bool end();
//- Return the time to the next write //- Return the time to the next execute or write
virtual scalar timeToNextWrite(); // if time requires adjustment
virtual scalar timeToNextAction();
//- Read and set the function object if its data have changed //- Read and set the function object if its data have changed
virtual bool read(const dictionary&); virtual bool read(const dictionary&);

View File

@ -83,7 +83,8 @@ bool Foam::functionObjects::meshToMeshAdjustTimeStepFunctionObject::read
Foam::scalar Foam::scalar
Foam::functionObjects::meshToMeshAdjustTimeStepFunctionObject::timeToNextWrite() Foam::functionObjects::meshToMeshAdjustTimeStepFunctionObject::
timeToNextAction()
{ {
return meshToMesh_.timeToNextMesh(); return meshToMesh_.timeToNextMesh();
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2022 OpenFOAM Foundation \\ / A nd | Copyright (C) 2022-2023 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -98,7 +98,7 @@ public:
} }
//- Return the time to the next write //- Return the time to the next write
virtual scalar timeToNextWrite(); virtual scalar timeToNextAction();
//- Do nothing //- Do nothing
virtual bool execute(); virtual bool execute();