# How to let OpenFOAM abort a simulation when values exceed a given range?

When the absolute pressure becomes negative or $U$ exceeds the speed of light, things have pretty obviously gone wrong (be that bad boundary conditions, a too coarse mesh, a too large timestep etc.).

So, is there any way to have a simulation abort with an error message when values exceed definable non-physical ranges?

-
I think this kind of question is better asked on the OpenFOAM MessageBoard as it is very program-specific –  bgschaid Jul 5 '12 at 18:59
@bgschaid I did, I forgot to put a link here. But honestly, I prefer the SE format since anyone else having a similar question will directly see the accepted answer (yours in this case) instead of having to browse through a potential discussion leading to the solution followed by someone else asking a similar question with an actually different answer etc... –  kar Jul 6 '12 at 6:45

If you don't want to write C++: In the simpleFunctionObjects there is a functionObject called panicDump. Something like

functions
{
pressureOurOfRange {
type panicDump;
functionObjectLibs
(
"libsimpleFunctionObjects.so"
);
fieldName p;
minimum 0;
maximum 1e7;
}
}


in system/controlDict would stop the simulation if the pressure blows up. Together with the expressionField-functionObject from swak4Foam you can also test for non-scalar fields.

-
This is perfect, great job! –  kar Jul 6 '12 at 6:42
bgschaid: welcome to scicomp! –  Aron Ahmadia Jul 6 '12 at 11:37

This is basic C++ - just add whatever you are interested in to the code and recompile. For example, copy and paste the following code right before the runTime.write(); call (which is at the end of the main function of every solver) to execute the sanity check at the end of every time step:

// sanity check
if (min(p) < 0)
{
FatalError
<< "Negative pressure" << nl
<< exit(FatalError);
}

-
This answer seems highly unlikely to be specific enough to OpenFOAM to work. Can you suggest where in OpenFOAM he should put this if it is? –  Bill Barth Jun 30 '12 at 22:26
Thanks for your comment, I updated the answer. However, where to paste the snippet really depends on where you want to have the sanity check, e.g. at the end of each time step vs. every inner iteration of the pressure-velocity coupling. –  akid Jul 1 '12 at 13:36
upvoted, though wouldn't it be a bit more idiomatic to raise a Foam Exception than to just return -1? Warning: fragile doxygen link to error.h in Foam source. –  Aron Ahmadia Jul 1 '12 at 14:05
Thanks akid, I think having the check at every timestep (or even only at every writing timestep) is sufficient - anything that aborts the calculation sooner than "late" is welcome... But is there a possibility to achieve this in a way that does not require a modification of the respective solver used? Maybe some #codeStream magic in controlDict? –  kar Jul 2 '12 at 6:07
Aron, I updated the answer. –  akid Jul 2 '12 at 7:49