CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM (
-   -   Provoking a clean exit from the main loop (

olesen July 21, 2006 01:54

I am looking for a (simple) me
I am looking for a (simple) method to exit cleanly from the main calculation loop.
As pseudo-code:

for (runTime++; !runTime.end(); runTime++) {
if ( isConverged() or user_signal())
runTime.writeNow() and exit_loop;

Looking through Time*.H, it would appear that something like altering the stop flag to 'saWriteNow' might do the trick, but I would really appreciate some pointers about the correct approach.


mattijs July 21, 2006 04:25

You can create your own derive
You can create your own derived Time class. All the main functions (specifically 'end()') are virtual.

Modifying the stop flag to saWriteNow will probably do the trick. It does require the operator++ to be executed though (since the time needs to be past the endTime) so will only stop at the start of the next loop.

Let us know how you solved it.

olesen July 21, 2006 09:56

Hi Mattijs, Following your
Hi Mattijs,

Following your tip, I tried my preferred solution, which resembled the following ...

bool Foam::myTime::writeAndEnd() const
stopAt_ = saWriteNow;
endTime_ = value();
outputTime_ = true;
return write();

After too many messages about fiddling with protected fields, I ditched this for the brute-force method:

loop (!runTime.end()) {
bool written = runTime.write();

if ( isConverged() ) {
while (!written && !runTime.end()) {
written = (++runTime).write();

This is certainly not elegant, but it does ensure that my data are written and the calculations stop.

I'll see about getting back to the 'proper' solution when I have more time. Perhaps it would even be useful enough to add back into the Foam::Time class?


olesen July 25, 2006 08:45

Hi Mattijs, I finally had a
Hi Mattijs,

I finally had a few minutes more time and found the 'const' mistake. Attached is a version of myTime myTime.tar.gz

that adds the following two member functions:

//- Write immediately
bool writeNow();

//- Write immediately and increment endTime to finish
bool writeAndEnd();

I left the copyright as "OpenCFD Ltd."


hjasak July 25, 2006 10:14

Looks pretty good - thanks! I
Looks pretty good - thanks! I have added it to the Time class.


All times are GMT -4. The time now is 06:33.